Search in sources :

Example 1 with SelectStatement

use of io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement in project sharding-jdbc by shardingjdbc.

the class WhereClauseParser method parseComparisonCondition.

private void parseComparisonCondition(final ShardingRule shardingRule, final SQLStatement sqlStatement, final List<SelectItem> items) {
    lexerEngine.skipIfEqual(Symbol.LEFT_PAREN);
    SQLExpression left = basicExpressionParser.parse(sqlStatement);
    if (lexerEngine.skipIfEqual(Symbol.EQ)) {
        parseEqualCondition(shardingRule, sqlStatement, left);
        lexerEngine.skipIfEqual(Symbol.RIGHT_PAREN);
        return;
    }
    if (lexerEngine.skipIfEqual(DefaultKeyword.IN)) {
        parseInCondition(shardingRule, sqlStatement, left);
        lexerEngine.skipIfEqual(Symbol.RIGHT_PAREN);
        return;
    }
    if (lexerEngine.skipIfEqual(DefaultKeyword.BETWEEN)) {
        parseBetweenCondition(shardingRule, sqlStatement, left);
        lexerEngine.skipIfEqual(Symbol.RIGHT_PAREN);
        return;
    }
    if (sqlStatement instanceof SelectStatement && isRowNumberCondition(items, left)) {
        if (lexerEngine.skipIfEqual(Symbol.LT)) {
            parseRowCountCondition((SelectStatement) sqlStatement, false);
            return;
        }
        if (lexerEngine.skipIfEqual(Symbol.LT_EQ)) {
            parseRowCountCondition((SelectStatement) sqlStatement, true);
            return;
        }
        if (lexerEngine.skipIfEqual(Symbol.GT)) {
            parseOffsetCondition((SelectStatement) sqlStatement, false);
            return;
        }
        if (lexerEngine.skipIfEqual(Symbol.GT_EQ)) {
            parseOffsetCondition((SelectStatement) sqlStatement, true);
            return;
        }
    }
    List<Keyword> otherConditionOperators = new LinkedList<>(Arrays.asList(getCustomizedOtherConditionOperators()));
    otherConditionOperators.addAll(Arrays.asList(Symbol.LT, Symbol.LT_EQ, Symbol.GT, Symbol.GT_EQ, Symbol.LT_GT, Symbol.BANG_EQ, Symbol.BANG_GT, Symbol.BANG_LT, DefaultKeyword.LIKE, DefaultKeyword.IS));
    if (lexerEngine.skipIfEqual(otherConditionOperators.toArray(new Keyword[otherConditionOperators.size()]))) {
        lexerEngine.skipIfEqual(DefaultKeyword.NOT);
        parseOtherCondition(sqlStatement);
    }
    if (lexerEngine.skipIfEqual(DefaultKeyword.NOT)) {
        lexerEngine.nextToken();
        lexerEngine.skipIfEqual(Symbol.LEFT_PAREN);
        parseOtherCondition(sqlStatement);
        lexerEngine.skipIfEqual(Symbol.RIGHT_PAREN);
    }
    lexerEngine.skipIfEqual(Symbol.RIGHT_PAREN);
}
Also used : SelectStatement(io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement) SQLExpression(io.shardingjdbc.core.parsing.parser.expression.SQLExpression) DefaultKeyword(io.shardingjdbc.core.parsing.lexer.token.DefaultKeyword) Keyword(io.shardingjdbc.core.parsing.lexer.token.Keyword) LinkedList(java.util.LinkedList)

Example 2 with SelectStatement

use of io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement in project sharding-jdbc by shardingjdbc.

the class ParsingSQLRouter method route.

private RoutingResult route(final List<Object> parameters, final SQLStatement sqlStatement) {
    Collection<String> tableNames = sqlStatement.getTables().getTableNames();
    RoutingEngine routingEngine;
    if (sqlStatement instanceof UseStatement) {
        routingEngine = new IgnoreRoutingEngine();
    } else if (sqlStatement instanceof DDLStatement) {
        routingEngine = new TableBroadcastRoutingEngine(shardingRule, sqlStatement);
    } else if (sqlStatement instanceof ShowDatabasesStatement || sqlStatement instanceof ShowTablesStatement) {
        routingEngine = new DatabaseBroadcastRoutingEngine(shardingRule);
    } else if (sqlStatement instanceof DALStatement) {
        routingEngine = new UnicastRoutingEngine(shardingRule, sqlStatement);
    } else if (tableNames.isEmpty() && sqlStatement instanceof SelectStatement) {
        routingEngine = new UnicastRoutingEngine(shardingRule, sqlStatement);
    } else if (tableNames.isEmpty()) {
        routingEngine = new DatabaseBroadcastRoutingEngine(shardingRule);
    } else if (1 == tableNames.size() || shardingRule.isAllBindingTables(tableNames) || shardingRule.isAllInDefaultDataSource(tableNames)) {
        routingEngine = new StandardRoutingEngine(shardingRule, parameters, tableNames.iterator().next(), sqlStatement);
    } else {
        // TODO config for cartesian set
        routingEngine = new ComplexRoutingEngine(shardingRule, parameters, tableNames, sqlStatement);
    }
    return routingEngine.route();
}
Also used : ComplexRoutingEngine(io.shardingjdbc.core.routing.type.complex.ComplexRoutingEngine) IgnoreRoutingEngine(io.shardingjdbc.core.routing.type.ignore.IgnoreRoutingEngine) DDLStatement(io.shardingjdbc.core.parsing.parser.sql.ddl.DDLStatement) DatabaseBroadcastRoutingEngine(io.shardingjdbc.core.routing.type.broadcast.DatabaseBroadcastRoutingEngine) ShowDatabasesStatement(io.shardingjdbc.core.parsing.parser.dialect.mysql.statement.ShowDatabasesStatement) StandardRoutingEngine(io.shardingjdbc.core.routing.type.standard.StandardRoutingEngine) TableBroadcastRoutingEngine(io.shardingjdbc.core.routing.type.broadcast.TableBroadcastRoutingEngine) SelectStatement(io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement) UnicastRoutingEngine(io.shardingjdbc.core.routing.type.unicast.UnicastRoutingEngine) UseStatement(io.shardingjdbc.core.parsing.parser.dialect.mysql.statement.UseStatement) ComplexRoutingEngine(io.shardingjdbc.core.routing.type.complex.ComplexRoutingEngine) DatabaseBroadcastRoutingEngine(io.shardingjdbc.core.routing.type.broadcast.DatabaseBroadcastRoutingEngine) RoutingEngine(io.shardingjdbc.core.routing.type.RoutingEngine) IgnoreRoutingEngine(io.shardingjdbc.core.routing.type.ignore.IgnoreRoutingEngine) UnicastRoutingEngine(io.shardingjdbc.core.routing.type.unicast.UnicastRoutingEngine) StandardRoutingEngine(io.shardingjdbc.core.routing.type.standard.StandardRoutingEngine) TableBroadcastRoutingEngine(io.shardingjdbc.core.routing.type.broadcast.TableBroadcastRoutingEngine) ShowTablesStatement(io.shardingjdbc.core.parsing.parser.dialect.mysql.statement.ShowTablesStatement) DALStatement(io.shardingjdbc.core.parsing.parser.sql.dal.DALStatement)

Example 3 with SelectStatement

use of io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement in project sharding-jdbc by shardingjdbc.

the class SQLRewriteEngine method appendOrderByToken.

private void appendOrderByToken(final SQLBuilder sqlBuilder, final int count, final List<SQLToken> sqlTokens) {
    SelectStatement selectStatement = (SelectStatement) sqlStatement;
    StringBuilder orderByLiterals = new StringBuilder();
    orderByLiterals.append(" ").append(DefaultKeyword.ORDER).append(" ").append(DefaultKeyword.BY).append(" ");
    int i = 0;
    for (OrderItem each : selectStatement.getOrderByItems()) {
        String columnLabel = SQLUtil.getOriginalValue(each.getColumnLabel(), databaseType);
        if (0 == i) {
            orderByLiterals.append(columnLabel).append(" ").append(each.getOrderDirection().name());
        } else {
            orderByLiterals.append(",").append(columnLabel).append(" ").append(each.getOrderDirection().name());
        }
        i++;
    }
    orderByLiterals.append(" ");
    sqlBuilder.appendLiterals(orderByLiterals.toString());
    int beginPosition = ((SelectStatement) sqlStatement).getGroupByLastPosition();
    appendRest(sqlBuilder, count, sqlTokens, beginPosition);
}
Also used : SelectStatement(io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement) OrderItem(io.shardingjdbc.core.parsing.parser.context.OrderItem)

Example 4 with SelectStatement

use of io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement in project sharding-jdbc by shardingjdbc.

the class ShardingStatement method getResultSet.

@Override
public ResultSet getResultSet() throws SQLException {
    if (null != currentResultSet) {
        return currentResultSet;
    }
    if (1 == routedStatements.size() && routeResult.getSqlStatement() instanceof DQLStatement) {
        currentResultSet = routedStatements.iterator().next().getResultSet();
        return currentResultSet;
    }
    List<ResultSet> resultSets = new ArrayList<>(routedStatements.size());
    List<QueryResult> queryResults = new ArrayList<>(routedStatements.size());
    for (Statement each : routedStatements) {
        ResultSet resultSet = each.getResultSet();
        resultSets.add(resultSet);
        queryResults.add(new JDBCQueryResult(resultSet));
    }
    if (routeResult.getSqlStatement() instanceof SelectStatement || routeResult.getSqlStatement() instanceof DALStatement) {
        MergeEngine mergeEngine = MergeEngineFactory.newInstance(connection.getShardingContext().getShardingRule(), queryResults, routeResult.getSqlStatement());
        currentResultSet = new ShardingResultSet(resultSets, mergeEngine.merge(), this);
    }
    return currentResultSet;
}
Also used : SelectStatement(io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement) QueryResult(io.shardingjdbc.core.merger.QueryResult) JDBCQueryResult(io.shardingjdbc.core.merger.JDBCQueryResult) MergeEngine(io.shardingjdbc.core.merger.MergeEngine) DQLStatement(io.shardingjdbc.core.parsing.parser.sql.dql.DQLStatement) DQLStatement(io.shardingjdbc.core.parsing.parser.sql.dql.DQLStatement) SelectStatement(io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement) InsertStatement(io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement) DALStatement(io.shardingjdbc.core.parsing.parser.sql.dal.DALStatement) Statement(java.sql.Statement) ShardingResultSet(io.shardingjdbc.core.jdbc.core.resultset.ShardingResultSet) ResultSet(java.sql.ResultSet) GeneratedKeysResultSet(io.shardingjdbc.core.jdbc.core.resultset.GeneratedKeysResultSet) ShardingResultSet(io.shardingjdbc.core.jdbc.core.resultset.ShardingResultSet) ArrayList(java.util.ArrayList) DALStatement(io.shardingjdbc.core.parsing.parser.sql.dal.DALStatement) JDBCQueryResult(io.shardingjdbc.core.merger.JDBCQueryResult)

Example 5 with SelectStatement

use of io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement in project sharding-jdbc by shardingjdbc.

the class SQLRewriteEngineTest method setUp.

@Before
public void setUp() throws IOException {
    URL url = SQLRewriteEngineTest.class.getClassLoader().getResource("yaml/rewrite-rule.yaml");
    Preconditions.checkNotNull(url, "Cannot found rewrite rule yaml configuration.");
    YamlShardingConfiguration yamlShardingConfig = YamlShardingConfiguration.unmarshal(new File(url.getFile()));
    shardingRule = yamlShardingConfig.getShardingRule(yamlShardingConfig.getDataSources().keySet());
    selectStatement = new SelectStatement();
    tableTokens = new HashMap<>(1, 1);
    tableTokens.put("table_x", "table_1");
}
Also used : SelectStatement(io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement) YamlShardingConfiguration(io.shardingjdbc.core.yaml.sharding.YamlShardingConfiguration) File(java.io.File) URL(java.net.URL) Before(org.junit.Before)

Aggregations

SelectStatement (io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement)25 OrderItem (io.shardingjdbc.core.parsing.parser.context.OrderItem)12 ResultSet (java.sql.ResultSet)10 Before (org.junit.Before)9 Test (org.junit.Test)9 TestQueryResult (io.shardingjdbc.core.merger.fixture.TestQueryResult)8 MemoryQueryResultRow (io.shardingjdbc.core.merger.dql.common.MemoryQueryResultRow)6 ResultSetMetaData (java.sql.ResultSetMetaData)6 DALStatement (io.shardingjdbc.core.parsing.parser.sql.dal.DALStatement)3 GeneratedKeysResultSet (io.shardingjdbc.core.jdbc.core.resultset.GeneratedKeysResultSet)2 ShardingResultSet (io.shardingjdbc.core.jdbc.core.resultset.ShardingResultSet)2 JDBCQueryResult (io.shardingjdbc.core.merger.JDBCQueryResult)2 MergeEngine (io.shardingjdbc.core.merger.MergeEngine)2 QueryResult (io.shardingjdbc.core.merger.QueryResult)2 AggregationSelectItem (io.shardingjdbc.core.parsing.parser.context.selectitem.AggregationSelectItem)2 InsertStatement (io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement)2 DQLStatement (io.shardingjdbc.core.parsing.parser.sql.dql.DQLStatement)2 ArrayList (java.util.ArrayList)2 DQLMergeEngine (io.shardingjdbc.core.merger.dql.DQLMergeEngine)1 DefaultKeyword (io.shardingjdbc.core.parsing.lexer.token.DefaultKeyword)1