com.wantscart.jade.core.JadeOperationFactoryImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.wantscart.jade.core.JadeOperationFactoryImpl.java

Source

/*
 * Copyright 2009-2010 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.wantscart.jade.core;

import com.wantscart.jade.annotation.SQL;
import com.wantscart.jade.annotation.SQLType;
import com.wantscart.jade.provider.DataAccess;
import com.wantscart.jade.provider.Modifier;
import org.apache.commons.lang.StringUtils;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.util.Assert;

import java.util.regex.Pattern;

/**
 * : {@link JadeOperation} 
 *
 * @author han.liao
 */
public class JadeOperationFactoryImpl implements JadeOperationFactory {

    private static Pattern[] SELECT_PATTERNS = new Pattern[] {
            //
            Pattern.compile("^\\s*SELECT\\s+", Pattern.CASE_INSENSITIVE), //
            Pattern.compile("^\\s*SHOW\\s+", Pattern.CASE_INSENSITIVE), //
            Pattern.compile("^\\s*DESC\\s+", Pattern.CASE_INSENSITIVE), //
            Pattern.compile("^\\s*DESCRIBE\\s+", Pattern.CASE_INSENSITIVE), //
    };

    private RowMapperFactory rowMapperFactory = new RowMapperFactoryImpl();

    @Override
    public JadeOperation getOperation(DataAccess dataAccess, Modifier modifier) {

        //   Annotation
        SQL sql = modifier.getAnnotation(SQL.class);
        Assert.notNull(sql, "@SQL is required for method " + modifier);

        String sqlString = sql.value();
        SQLType sqlType = sql.type();

        if (sqlType == SQLType.TEMPLATE) {
            TableSchema schema = TableSchema.getSchema(modifier.getDefinition().getDAOGenericsClazz());
            for (String T : TableSchema.ALL_TEMPLATE) {
                sqlString = StringUtils.replace(sqlString, T, schema.getByTemplate(T));
            }
            sqlType = SQLType.AUTO_DETECT;
        }

        if (sqlType == SQLType.AUTO_DETECT) {
            for (int i = 0; i < SELECT_PATTERNS.length; i++) {
                // ??  SELECT ?
                if (SELECT_PATTERNS[i].matcher(sqlString).find()) {
                    sqlType = SQLType.READ;
                    break;
                }
            }
            if (sqlType == SQLType.AUTO_DETECT) {
                sqlType = SQLType.WRITE;
            }
        }

        //
        if (SQLType.READ == sqlType) {
            //   RowMapper
            RowMapper rowMapper = rowMapperFactory.getRowMapper(modifier);
            // SELECT 
            return new SelectOperation(dataAccess, sqlString, modifier, rowMapper);

        } else if (SQLType.WRITE == sqlType) {
            // INSERT / UPDATE / DELETE 
            return new UpdateOperation(dataAccess, sqlString, modifier);
        }

        // 
        throw new AssertionError("Unknown SQL type: " + sqlType);
    }
}