Search in sources :

Example 1 with BatchPreparedStatementUnit

use of io.shardingjdbc.core.executor.type.batch.BatchPreparedStatementUnit in project sharding-jdbc by shardingjdbc.

the class ShardingPreparedStatement method addBatch.

@Override
public void addBatch() throws SQLException {
    try {
        for (BatchPreparedStatementUnit each : routeBatch()) {
            each.getStatement().addBatch();
            each.mapAddBatchCount(parameterSets.size());
        }
        parameterSets.add(getParameters());
    } finally {
        currentResultSet = null;
        clearParameters();
    }
}
Also used : BatchPreparedStatementUnit(io.shardingjdbc.core.executor.type.batch.BatchPreparedStatementUnit)

Example 2 with BatchPreparedStatementUnit

use of io.shardingjdbc.core.executor.type.batch.BatchPreparedStatementUnit in project sharding-jdbc by shardingjdbc.

the class ShardingPreparedStatement method getPreparedBatchStatement.

private BatchPreparedStatementUnit getPreparedBatchStatement(final SQLExecutionUnit sqlExecutionUnit) throws SQLException {
    Optional<BatchPreparedStatementUnit> preparedBatchStatement = Iterators.tryFind(batchStatementUnits.iterator(), new Predicate<BatchPreparedStatementUnit>() {

        @Override
        public boolean apply(final BatchPreparedStatementUnit input) {
            return Objects.equals(input.getSqlExecutionUnit(), sqlExecutionUnit);
        }
    });
    if (preparedBatchStatement.isPresent()) {
        return preparedBatchStatement.get();
    }
    BatchPreparedStatementUnit result = new BatchPreparedStatementUnit(sqlExecutionUnit, generatePreparedStatement(sqlExecutionUnit));
    batchStatementUnits.add(result);
    return result;
}
Also used : BatchPreparedStatementUnit(io.shardingjdbc.core.executor.type.batch.BatchPreparedStatementUnit)

Example 3 with BatchPreparedStatementUnit

use of io.shardingjdbc.core.executor.type.batch.BatchPreparedStatementUnit in project sharding-jdbc by shardingjdbc.

the class ShardingPreparedStatement method routeBatch.

private List<BatchPreparedStatementUnit> routeBatch() throws SQLException {
    List<BatchPreparedStatementUnit> result = new ArrayList<>();
    routeResult = routingEngine.route(getParameters());
    for (SQLExecutionUnit each : routeResult.getExecutionUnits()) {
        BatchPreparedStatementUnit batchStatementUnit = getPreparedBatchStatement(each);
        replaySetParameter(batchStatementUnit.getStatement());
        result.add(batchStatementUnit);
    }
    return result;
}
Also used : BatchPreparedStatementUnit(io.shardingjdbc.core.executor.type.batch.BatchPreparedStatementUnit) ArrayList(java.util.ArrayList) SQLExecutionUnit(io.shardingjdbc.core.routing.SQLExecutionUnit)

Example 4 with BatchPreparedStatementUnit

use of io.shardingjdbc.core.executor.type.batch.BatchPreparedStatementUnit 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)

Aggregations

BatchPreparedStatementUnit (io.shardingjdbc.core.executor.type.batch.BatchPreparedStatementUnit)4 SQLExecutionUnit (io.shardingjdbc.core.routing.SQLExecutionUnit)2 SQLBuilder (io.shardingjdbc.core.rewrite.SQLBuilder)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1