All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.javaclub.cdl.client.matrix.parser.SQLParserFactory Maven / Gradle / Ivy

There is a newer version: 2.3.9
Show newest version
/**
 * Copyright 1999-2015 dangdang.com.
 * 

* 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 is 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.github.javaclub.cdl.client.matrix.parser; import java.util.List; import java.util.Map; import lombok.AccessLevel; import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; import com.alibaba.druid.sql.ast.SQLStatement; import com.alibaba.druid.sql.ast.statement.SQLDeleteStatement; import com.alibaba.druid.sql.ast.statement.SQLInsertStatement; import com.alibaba.druid.sql.ast.statement.SQLSelectStatement; import com.alibaba.druid.sql.ast.statement.SQLUpdateStatement; import com.alibaba.druid.sql.dialect.db2.parser.DB2StatementParser; import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlReplaceStatement; import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser; import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser; import com.alibaba.druid.sql.dialect.sqlserver.parser.SQLServerStatementParser; import com.alibaba.druid.sql.parser.SQLStatementParser; import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor; import com.github.javaclub.cdl.client.matrix.exception.SQLParserException; import com.github.javaclub.cdl.client.matrix.jdbc.DatabaseType; import com.github.javaclub.cdl.client.matrix.parser.visitor.VisitorLogProxy; import com.github.javaclub.cdl.client.parameter.ParameterContext; /** * SQL解析器工厂. * * @author gaohongtao, zhangliang */ @NoArgsConstructor(access = AccessLevel.PRIVATE) public final class SQLParserFactory { /** * 创建解析器引擎对象. * * @param databaseType 数据库类型 * @param sql SQL语句 * @param parameters SQL中参数的值 * @param shardingColumns 分片列名称集合 * @return 解析器引擎对象 * @throws SQLParserException SQL解析异常 */ public static SQLParseEngine create(final DatabaseType databaseType, final String sql, final List parameters, final Map shardingColumns) throws SQLParserException { SQLStatement sqlStatement = getSQLStatementParser(databaseType, sql).parseStatement(); return new SQLParseEngine(sqlStatement, parameters, getSQLVisitor(databaseType, sqlStatement), shardingColumns); } public static SQLParseEngine create(final DatabaseType databaseType, final String sql, final List parameters, final Map shardingColumns,Map parameterSettings) throws SQLParserException { SQLStatement sqlStatement = getSQLStatementParser(databaseType, sql).parseStatement(); SQLParseEngine sQLParseEngine = new SQLParseEngine(sqlStatement, parameters, getSQLVisitor(databaseType, sqlStatement), shardingColumns); sQLParseEngine.setParameterSettings(parameterSettings); return sQLParseEngine; } private static SQLStatementParser getSQLStatementParser(final DatabaseType databaseType, final String sql) { switch (databaseType) { case H2: case MySQL: return new MySqlStatementParser(sql); case Oracle: return new OracleStatementParser(sql); case SQLServer: return new SQLServerStatementParser(sql); case DB2: return new DB2StatementParser(sql); default: throw new UnsupportedOperationException(String.format("Cannot support database type [%s]", databaseType)); } } private static SQLASTOutputVisitor getSQLVisitor(final DatabaseType databaseType, final SQLStatement sqlStatement) { if (sqlStatement instanceof SQLSelectStatement) { return VisitorLogProxy.enhance(SQLVisitorRegistry.getSelectVistor(databaseType)); } if (sqlStatement instanceof SQLInsertStatement) { return VisitorLogProxy.enhance(SQLVisitorRegistry.getInsertVistor(databaseType)); } if (sqlStatement instanceof SQLUpdateStatement) { return VisitorLogProxy.enhance(SQLVisitorRegistry.getUpdateVistor(databaseType)); } if (sqlStatement instanceof SQLDeleteStatement) { return VisitorLogProxy.enhance(SQLVisitorRegistry.getDeleteVistor(databaseType)); } if(sqlStatement instanceof MySqlReplaceStatement){ return VisitorLogProxy.enhance(SQLVisitorRegistry.getReplaceVistor(databaseType)); } throw new SQLParserException("Unsupported SQL statement: [%s]", sqlStatement); } }