Search in sources :

Example 11 with SQLExecutionUnit

use of io.shardingjdbc.core.routing.SQLExecutionUnit 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 12 with SQLExecutionUnit

use of io.shardingjdbc.core.routing.SQLExecutionUnit 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)

Example 13 with SQLExecutionUnit

use of io.shardingjdbc.core.routing.SQLExecutionUnit in project sharding-jdbc by shardingjdbc.

the class ShardingStatement method generateExecutor.

private StatementExecutor generateExecutor(final String sql) throws SQLException {
    clearPrevious();
    ShardingContext shardingContext = connection.getShardingContext();
    routeResult = new StatementRoutingEngine(shardingContext.getShardingRule(), shardingContext.getDatabaseType(), shardingContext.isShowSQL()).route(sql);
    Collection<StatementUnit> statementUnits = new LinkedList<>();
    for (SQLExecutionUnit each : routeResult.getExecutionUnits()) {
        Collection<Connection> connections;
        SQLType sqlType = routeResult.getSqlStatement().getType();
        if (SQLType.DDL == sqlType) {
            connections = connection.getConnectionsForDDL(each.getDataSource());
        } else {
            connections = Collections.singletonList(connection.getConnection(each.getDataSource(), routeResult.getSqlStatement().getType()));
        }
        for (Connection connection : connections) {
            Statement statement = connection.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
            replayMethodsInvocation(statement);
            statementUnits.add(new StatementUnit(each, statement));
            routedStatements.add(statement);
        }
    }
    return new StatementExecutor(connection.getShardingContext().getExecutorEngine(), routeResult.getSqlStatement().getType(), statementUnits);
}
Also used : 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) Connection(java.sql.Connection) ShardingConnection(io.shardingjdbc.core.jdbc.core.connection.ShardingConnection) StatementRoutingEngine(io.shardingjdbc.core.routing.StatementRoutingEngine) SQLExecutionUnit(io.shardingjdbc.core.routing.SQLExecutionUnit) SQLType(io.shardingjdbc.core.constant.SQLType) StatementExecutor(io.shardingjdbc.core.executor.type.statement.StatementExecutor) StatementUnit(io.shardingjdbc.core.executor.type.statement.StatementUnit) LinkedList(java.util.LinkedList) ShardingContext(io.shardingjdbc.core.jdbc.core.ShardingContext)

Aggregations

SQLExecutionUnit (io.shardingjdbc.core.routing.SQLExecutionUnit)13 LinkedList (java.util.LinkedList)6 StatementUnit (io.shardingjdbc.core.executor.type.statement.StatementUnit)5 SQLBuilder (io.shardingjdbc.core.rewrite.SQLBuilder)4 Connection (java.sql.Connection)4 Statement (java.sql.Statement)4 BaseStatementUnit (io.shardingjdbc.core.executor.BaseStatementUnit)3 BatchPreparedStatementUnit (io.shardingjdbc.core.executor.type.batch.BatchPreparedStatementUnit)3 SQLRouteResult (io.shardingjdbc.core.routing.SQLRouteResult)3 SQLException (java.sql.SQLException)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 SQLType (io.shardingjdbc.core.constant.SQLType)2 PreparedStatementUnit (io.shardingjdbc.core.executor.type.prepared.PreparedStatementUnit)2 InsertStatement (io.shardingjdbc.core.parsing.parser.sql.dml.insert.InsertStatement)2 SelectStatement (io.shardingjdbc.core.parsing.parser.sql.dql.select.SelectStatement)2 RoutingResult (io.shardingjdbc.core.routing.type.RoutingResult)2 TableUnit (io.shardingjdbc.core.routing.type.TableUnit)2 StatementExecutor (io.shardingjdbc.core.executor.type.statement.StatementExecutor)1 ShardingContext (io.shardingjdbc.core.jdbc.core.ShardingContext)1