Search in sources :

Example 1 with PreparedBinding

use of org.jdbi.v3.core.statement.internal.PreparedBinding in project jdbi by jdbi.

the class PreparedBatch method add.

/**
 * Add the current binding as a saved batch and clear the binding.
 * @return this
 */
public PreparedBatch add() {
    final PreparedBinding currentBinding = getBinding();
    if (currentBinding.isEmpty()) {
        throw new IllegalStateException("Attempt to add() an empty batch, you probably didn't mean to do this " + "- call add() *after* setting batch parameters");
    }
    bindings.add(currentBinding);
    getContext().setBinding(new PreparedBinding(getContext()));
    return this;
}
Also used : PreparedBinding(org.jdbi.v3.core.statement.internal.PreparedBinding)

Example 2 with PreparedBinding

use of org.jdbi.v3.core.statement.internal.PreparedBinding in project jdbi by jdbi.

the class PreparedBatch method bindNamedArgumentFinder.

@Override
PreparedBatch bindNamedArgumentFinder(NamedArgumentFinderFactory<?> factory, String prefix, Object value, Type type, Supplier<NamedArgumentFinder> backupArgumentFinder) {
    PreparedBinding binding = getBinding();
    PrepareKey key = factory.keyFor(prefix, value);
    preparedFinders.computeIfAbsent(key, pk -> factory.prepareFor(getConfig(), this::buildArgument, prefix, value, type));
    binding.prepareKeys.put(key, value);
    binding.backupArgumentFinders.add(backupArgumentFinder);
    return this;
}
Also used : PrepareKey(org.jdbi.v3.core.argument.internal.NamedArgumentFinderFactory.PrepareKey) PreparedBinding(org.jdbi.v3.core.statement.internal.PreparedBinding)

Example 3 with PreparedBinding

use of org.jdbi.v3.core.statement.internal.PreparedBinding in project jdbi by jdbi.

the class PreparedBatch method internalBatchExecute.

private ExecutedBatch internalBatchExecute() {
    if (!getBinding().isEmpty()) {
        add();
    }
    beforeTemplating();
    final StatementContext ctx = getContext();
    ParsedSql parsedSql = parseSql();
    String sql = parsedSql.getSql();
    ParsedParameters parsedParameters = parsedSql.getParameters();
    try {
        try {
            StatementBuilder statementBuilder = getHandle().getStatementBuilder();
            @SuppressWarnings("PMD.CloseResource") Connection connection = getHandle().getConnection();
            stmt = statementBuilder.create(connection, sql, ctx);
            addCleanable(() -> statementBuilder.close(connection, sql, stmt));
            getConfig(SqlStatements.class).customize(stmt);
        } catch (SQLException e) {
            throw new UnableToCreateStatementException(e, ctx);
        }
        if (bindings.isEmpty()) {
            return new ExecutedBatch(stmt, new int[0]);
        }
        beforeBinding();
        try {
            ArgumentBinder<?> binder = new ArgumentBinder.Prepared(this, parsedParameters, bindings.get(0));
            for (Binding binding : bindings) {
                ctx.setBinding(binding);
                binder.bind(binding);
                stmt.addBatch();
            }
        } catch (SQLException e) {
            throw new UnableToExecuteStatementException("Exception while binding parameters", e, ctx);
        }
        beforeExecution();
        try {
            final int[] rs = SqlLoggerUtil.wrap(stmt::executeBatch, ctx, getConfig(SqlStatements.class).getSqlLogger());
            afterExecution();
            ctx.setBinding(new PreparedBinding(ctx));
            return new ExecutedBatch(stmt, rs);
        } catch (SQLException e) {
            throw new UnableToExecuteStatementException(Batch.mungeBatchException(e), ctx);
        }
    } finally {
        bindings.clear();
    }
}
Also used : PreparedBinding(org.jdbi.v3.core.statement.internal.PreparedBinding) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedBinding(org.jdbi.v3.core.statement.internal.PreparedBinding)

Aggregations

PreparedBinding (org.jdbi.v3.core.statement.internal.PreparedBinding)3 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 PrepareKey (org.jdbi.v3.core.argument.internal.NamedArgumentFinderFactory.PrepareKey)1