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

Java tutorial

Introduction

Here is the source code for com.sinosoft.one.data.jade.statement.StatementMetaData.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 com.sinosoft.one.data.jade.annotation.SQL;
import com.sinosoft.one.data.jade.annotation.SQLType;
import com.sinosoft.one.data.jade.annotation.ShardBy;
import org.springframework.data.repository.query.Param;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.regex.Pattern;

/**
 * {@link com.sinosoft.one.data.jade.statement.StatementMetaData} ??DAO?
 * <p>
 * 
 * 
 * @author  [qieqie.wang@gmail.com]
 */
public class StatementMetaData {

    /**
     * DAOclassMetaData
     */
    private final DAOMetaData daoMetaData;

    /**
     * DAO
     */
    private final Method method;

    /**
     * DAOSQL?
     */
    private final String sql;

    /**
     * ????method?
     */
    private final Class<?>[] genericReturnTypes;

    /**
     * ?method?
     * <p>
     * ??? ,?null
     */
    private final Param[] sqlParams;

    /**
     * <code>@{@link com.sinosoft.one.data.jade.annotation.ShardBy}</code>?(0)?method?
     */
    private final int shardByIndex;

    private final int parameterCount;

    // --------------------------------------------

    public StatementMetaData(DAOMetaData daoMetaData, Method method, String sqlQuerie) {
        this.daoMetaData = daoMetaData;
        this.method = method;
        this.sql = method.getAnnotation(SQL.class) == null ? sqlQuerie : method.getAnnotation(SQL.class).value();

        this.genericReturnTypes = GenericUtils.getActualClass(method.getGenericReturnType());

        Annotation[][] annotations = method.getParameterAnnotations();
        this.parameterCount = annotations.length;
        this.sqlParams = new Param[annotations.length];
        int shardByIndex = -1;
        for (int index = 0; index < annotations.length; index++) {
            for (Annotation annotation : annotations[index]) {
                if (annotation instanceof ShardBy) {
                    if (shardByIndex >= 0) {
                        throw new IllegalArgumentException("duplicated @" + ShardBy.class.getName());
                    }
                    shardByIndex = index;
                } else if (annotation instanceof Param) {
                    this.sqlParams[index] = (Param) annotation;
                }
            }
        }
        this.shardByIndex = shardByIndex;
    }

    public DAOMetaData getDAOMetaData() {
        return daoMetaData;
    }

    public Method getMethod() {
        return method;
    }

    public String getSQL() {
        return sql;
    }

    public int getParameterCount() {
        return parameterCount;
    }

    public Param getSQLParamAt(int argIndex) {
        return sqlParams[argIndex];
    }

    public int getShardByIndex() {
        return shardByIndex;
    }

    public Class<?>[] getGenericReturnTypes() {
        return genericReturnTypes;
    }

    public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
        return method.getAnnotation(annotationClass);
    }

    public boolean equals(Object obj) {
        if (obj instanceof StatementMetaData) {
            StatementMetaData modifier = (StatementMetaData) obj;
            return daoMetaData.equals(modifier.daoMetaData) && method.equals(modifier.method);
        }
        return false;
    }

    public int hashCode() {
        return daoMetaData.hashCode() ^ method.hashCode();
    }

    public String toString() {
        return daoMetaData.getDAOClass().getName() + '#' + method.getName();
    }

    /**
     * ??
     */
    public 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), //
    };
    /**
     * ?
     */
    public static Pattern CALL_PATTERN = Pattern.compile("^((\\s*|\\s*\\{\\s*)CALL|exec|execute)\\s+",
            Pattern.CASE_INSENSITIVE);

    private SQLType sqlType;

    public SQLType getSQLType() {
        if (sqlType == null) {
            SQL sql = method.getAnnotation(SQL.class);
            SQLType sqlType;
            if (sql == null) {
                sqlType = SQLType.AUTO_DETECT;
            } else {
                sqlType = sql.type();
            }

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