Search in sources :

Example 1 with SQLBuilder

use of io.shardingjdbc.core.rewrite.SQLBuilder in project sharding-jdbc by shardingjdbc.

the class StatementExecutorTest method createStatementUnits.

private Collection<StatementUnit> createStatementUnits(final String sql, final Statement statement, final String dataSource) {
    Collection<StatementUnit> result = new LinkedList<>();
    SQLBuilder sqlBuilder = new SQLBuilder();
    sqlBuilder.appendLiterals(sql);
    result.add(new StatementUnit(new SQLExecutionUnit(dataSource, sqlBuilder.toSQL(Collections.<String, String>emptyMap(), null)), statement));
    return result;
}
Also used : SQLBuilder(io.shardingjdbc.core.rewrite.SQLBuilder) SQLExecutionUnit(io.shardingjdbc.core.routing.SQLExecutionUnit) StatementUnit(io.shardingjdbc.core.executor.type.statement.StatementUnit) LinkedList(java.util.LinkedList)

Example 2 with SQLBuilder

use of io.shardingjdbc.core.rewrite.SQLBuilder in project sharding-jdbc by shardingjdbc.

the class ParsingSQLRouter method route.

@Override
public SQLRouteResult route(final String logicSQL, final List<Object> parameters, final SQLStatement sqlStatement) {
    SQLRouteResult result = new SQLRouteResult(sqlStatement);
    if (sqlStatement instanceof InsertStatement && null != ((InsertStatement) sqlStatement).getGeneratedKey()) {
        processGeneratedKey(parameters, (InsertStatement) sqlStatement, result);
    }
    RoutingResult routingResult = route(parameters, sqlStatement);
    SQLRewriteEngine rewriteEngine = new SQLRewriteEngine(shardingRule, logicSQL, databaseType, sqlStatement);
    boolean isSingleRouting = routingResult.isSingleRouting();
    if (sqlStatement instanceof SelectStatement && null != ((SelectStatement) sqlStatement).getLimit()) {
        processLimit(parameters, (SelectStatement) sqlStatement, isSingleRouting);
    }
    SQLBuilder sqlBuilder = rewriteEngine.rewrite(!isSingleRouting);
    if (routingResult instanceof CartesianRoutingResult) {
        for (CartesianDataSource cartesianDataSource : ((CartesianRoutingResult) routingResult).getRoutingDataSources()) {
            for (CartesianTableReference cartesianTableReference : cartesianDataSource.getRoutingTableReferences()) {
                result.getExecutionUnits().add(new SQLExecutionUnit(cartesianDataSource.getDataSource(), rewriteEngine.generateSQL(cartesianTableReference, sqlBuilder)));
            }
        }
    } else {
        for (TableUnit each : routingResult.getTableUnits().getTableUnits()) {
            result.getExecutionUnits().add(new SQLExecutionUnit(each.getDataSourceName(), rewriteEngine.generateSQL(each, sqlBuilder)));
        }
    }
    if (showSQL) {
        SQLLogger.logSQL(logicSQL, sqlStatement, result.getExecutionUnits(), parameters);
    }
    return result;
}
Also used : RoutingResult(io.shardingjdbc.core.routing.type.RoutingResult) CartesianRoutingResult(io.shardingjdbc.core.routing.type.complex.CartesianRoutingResult) SelectStatement(io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement) SQLBuilder(io.shardingjdbc.core.rewrite.SQLBuilder) SQLRouteResult(io.shardingjdbc.core.routing.SQLRouteResult) CartesianRoutingResult(io.shardingjdbc.core.routing.type.complex.CartesianRoutingResult) CartesianDataSource(io.shardingjdbc.core.routing.type.complex.CartesianDataSource) CartesianTableReference(io.shardingjdbc.core.routing.type.complex.CartesianTableReference) SQLExecutionUnit(io.shardingjdbc.core.routing.SQLExecutionUnit) SQLRewriteEngine(io.shardingjdbc.core.rewrite.SQLRewriteEngine) InsertStatement(io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement) TableUnit(io.shardingjdbc.core.routing.type.TableUnit)

Example 3 with SQLBuilder

use of io.shardingjdbc.core.rewrite.SQLBuilder in project sharding-jdbc by shardingjdbc.

the class BatchPreparedStatementExecutorTest method createPreparedStatementUnits.

private Collection<BatchPreparedStatementUnit> createPreparedStatementUnits(final String sql, final PreparedStatement preparedStatement, final String dataSource, final int addBatchTimes) {
    Collection<BatchPreparedStatementUnit> result = new LinkedList<>();
    SQLBuilder sqlBuilder = new SQLBuilder();
    sqlBuilder.appendLiterals(sql);
    BatchPreparedStatementUnit batchPreparedStatementUnit = new BatchPreparedStatementUnit(new SQLExecutionUnit(dataSource, sqlBuilder.toSQL(Collections.<String, String>emptyMap(), null)), preparedStatement);
    for (int i = 0; i < addBatchTimes; i++) {
        batchPreparedStatementUnit.mapAddBatchCount(i);
    }
    result.add(batchPreparedStatementUnit);
    return result;
}
Also used : SQLBuilder(io.shardingjdbc.core.rewrite.SQLBuilder) BatchPreparedStatementUnit(io.shardingjdbc.core.executor.type.batch.BatchPreparedStatementUnit) SQLExecutionUnit(io.shardingjdbc.core.routing.SQLExecutionUnit) LinkedList(java.util.LinkedList)

Example 4 with SQLBuilder

use of io.shardingjdbc.core.rewrite.SQLBuilder in project sharding-jdbc by shardingjdbc.

the class PreparedStatementExecutorTest method createPreparedStatementUnits.

private Collection<PreparedStatementUnit> createPreparedStatementUnits(final String sql, final PreparedStatement preparedStatement, final String dataSource) {
    Collection<PreparedStatementUnit> result = new LinkedList<>();
    SQLBuilder sqlBuilder = new SQLBuilder();
    sqlBuilder.appendLiterals(sql);
    result.add(new PreparedStatementUnit(new SQLExecutionUnit(dataSource, sqlBuilder.toSQL(Collections.<String, String>emptyMap(), null)), preparedStatement));
    return result;
}
Also used : SQLBuilder(io.shardingjdbc.core.rewrite.SQLBuilder) SQLExecutionUnit(io.shardingjdbc.core.routing.SQLExecutionUnit) PreparedStatementUnit(io.shardingjdbc.core.executor.type.prepared.PreparedStatementUnit) LinkedList(java.util.LinkedList)

Aggregations

SQLBuilder (io.shardingjdbc.core.rewrite.SQLBuilder)4 SQLExecutionUnit (io.shardingjdbc.core.routing.SQLExecutionUnit)4 LinkedList (java.util.LinkedList)3 BatchPreparedStatementUnit (io.shardingjdbc.core.executor.type.batch.BatchPreparedStatementUnit)1 PreparedStatementUnit (io.shardingjdbc.core.executor.type.prepared.PreparedStatementUnit)1 StatementUnit (io.shardingjdbc.core.executor.type.statement.StatementUnit)1 InsertStatement (io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement)1 SelectStatement (io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement)1 SQLRewriteEngine (io.shardingjdbc.core.rewrite.SQLRewriteEngine)1 SQLRouteResult (io.shardingjdbc.core.routing.SQLRouteResult)1 RoutingResult (io.shardingjdbc.core.routing.type.RoutingResult)1 TableUnit (io.shardingjdbc.core.routing.type.TableUnit)1 CartesianDataSource (io.shardingjdbc.core.routing.type.complex.CartesianDataSource)1 CartesianRoutingResult (io.shardingjdbc.core.routing.type.complex.CartesianRoutingResult)1 CartesianTableReference (io.shardingjdbc.core.routing.type.complex.CartesianTableReference)1