com.sinosoft.one.data.jade.statement.SimpleInterpreter.java Source code

Java tutorial

Introduction

Here is the source code for com.sinosoft.one.data.jade.statement.SimpleInterpreter.java

Source

/*
 * Copyright 2009-2012 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License i distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.sinosoft.one.data.jade.statement;

import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;

import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 
 * @author  [in355hz@gmail.com]
 */
//NOTE: ???
public class SimpleInterpreter implements Interpreter {

    private static final Pattern NAMED_PARAM_PATTERN = Pattern.compile("(\\:([a-zA-Z0-9_\\.]+))");

    public void interpret(StatementRuntime runtime) {

        final List<Object> parametersAsList = new LinkedList<Object>();

        // ??  :name ??
        Matcher matcher = NAMED_PARAM_PATTERN.matcher(runtime.getSQL());
        if (!matcher.find()) {
            return;
        }

        final StringBuilder builder = new StringBuilder();

        int index = 0;

        do {
            // ??????
            String name = matcher.group(1);
            if (NumberUtils.isDigits(name)) {
                name = matcher.group();//??
            }

            Object value = null;

            // ?  a.b.c ?? 
            int find = name.indexOf('.');
            if (find >= 0) {

                //   BeanWrapper ?
                Object bean = runtime.getParameters().get(name.substring(0, find));
                if (bean != null) {
                    BeanWrapper beanWrapper = new BeanWrapperImpl(bean);
                    value = beanWrapper.getPropertyValue(name.substring(find + 1));
                }

            } else {
                // ?
                value = runtime.getParameters().get(name);
            }

            // ?
            builder.append(runtime.getSQL().substring(index, matcher.start()));

            if (value instanceof Collection<?>) {

                //  IN (...) ?
                builder.append('(');

                Collection<?> collection = (Collection<?>) value;

                if (collection.isEmpty()) {
                    builder.append("NULL");
                } else {
                    builder.append('?');
                }

                for (int i = 1; i < collection.size(); i++) {
                    builder.append(", ?");
                }

                builder.append(')');

                // ??
                parametersAsList.addAll(collection);

            } else {
                // ?
                builder.append('?');

                // ??
                parametersAsList.add(value);
            }

            index = matcher.end();

        } while (matcher.find());

        // ?
        builder.append(runtime.getSQL().substring(index));
        runtime.setSQL(builder.toString());
        runtime.setArgs(parametersAsList.toArray());
    }

}