Package org.jdbi.v3.core.statement
Class PreparedBatch
- java.lang.Object
-
- org.jdbi.v3.core.statement.BaseStatement<This>
-
- org.jdbi.v3.core.statement.SqlStatement<PreparedBatch>
-
- org.jdbi.v3.core.statement.PreparedBatch
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable,Configurable<PreparedBatch>,ResultBearing
public class PreparedBatch extends SqlStatement<PreparedBatch> implements ResultBearing
Represents a prepared batch statement. Multiple bindings are added to the compiled statement and then executed in a single operation. This is, generally, a very efficient way to execute large numbers of the same statement where the statement only varies by the arguments bound to it. The statement starts with an empty binding. You bind a single batch of parameters with the usualSqlStatementbinding methods, and then calladd()to add the current binding as a batch and then clear it. An entire batch can be bound and added in one go withadd(Map)oradd(Object...).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classPreparedBatch.ExecutedBatchprivate static classPreparedBatch.ExecutedBatchConsumer-
Nested classes/interfaces inherited from class org.jdbi.v3.core.statement.BaseStatement
BaseStatement.StatementCustomizerInvocation
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<PreparedBinding>bindings(package private) java.util.Map<NamedArgumentFinderFactory.PrepareKey,java.util.function.Function<java.lang.String,java.util.Optional<java.util.function.Function<java.lang.Object,Argument>>>>preparedFinders-
Fields inherited from class org.jdbi.v3.core.statement.SqlStatement
stmt
-
Fields inherited from class org.jdbi.v3.core.statement.BaseStatement
typedThis
-
-
Constructor Summary
Constructors Constructor Description PreparedBatch(Handle handle, java.lang.CharSequence sql)PreparedBatch(Handle handle, java.lang.String sql)Backwards compatible constructor that takes an explicit string argument.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description PreparedBatchadd()Add the current binding as a saved batch and clear the binding.PreparedBatchadd(java.lang.Object... args)Bind arguments positionally, add the binding as a saved batch, and then clear the current binding.PreparedBatchadd(java.util.Map<java.lang.String,?> args)Bind arguments from a Map, add the binding as a saved batch, then clear the current binding.(package private) PreparedBatchbindNamedArgumentFinder(NamedArgumentFinderFactory factory, java.lang.String prefix, java.lang.Object value, java.lang.reflect.Type type, java.util.function.Supplier<NamedArgumentFinder> backupArgumentFinder)(package private) java.util.function.Function<java.lang.Object,Argument>buildArgument(QualifiedType<?> type)int[]execute()Execute the batch and return the number of rows affected for each batch part.<R> Rexecute(ResultProducer<R> producer)Executes the batch, returning the result obtained from the givenResultProducer.private <R> Rexecute(ResultProducer<R> producer, java.util.function.Consumer<PreparedBatch.ExecutedBatch> batchConsumer)ResultIterator<java.lang.Integer>executeAndGetModCount()Execute the batch and return the mod counts as inexecute, but as a Jdbi result iterator instead of an array.ResultBearingexecuteAndReturnGeneratedKeys(java.lang.String... columnNames)Deprecated.UseexecutePreparedBatch(String...)which has the same functionality but also returns the per-batch modified row counts.BatchResultBearingexecutePreparedBatch(java.lang.String... columnNames)Execute the batch and give access to any generated keys returned by the operation.protected PreparedBindinggetBinding()private PreparedBatch.ExecutedBatchinternalBatchExecute()<R> RscanResultSet(ResultSetScanner<R> resultSetScanner)Invokes the mapper with a result set supplier, and returns the value returned by the mapper.intsize()Returns the number of bindings in this batch.-
Methods inherited from class org.jdbi.v3.core.statement.SqlStatement
afterExecution, beforeBinding, beforeExecution, beforeTemplating, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bind, bindArray, bindArray, bindArray, bindArray, bindArray, bindArray, bindArray, bindArray, bindASCIIStream, bindASCIIStream, bindBean, bindBean, bindBeanList, bindBinaryStream, bindBinaryStream, bindBySqlType, bindBySqlType, bindByType, bindByType, bindByType, bindByType, bindByType, bindByType, bindFields, bindFields, bindList, bindList, bindList, bindList, bindList, bindList, bindList, bindMap, bindMethods, bindMethods, bindMethodsList, bindNamedArgumentFinder, bindNull, bindNull, bindNVarchar, bindNVarchar, bindPojo, bindPojo, bindPojo, bindPojo, bindPojo, bindPojo, cleanupHandleCommit, cleanupHandleRollback, cleanupStatement, createStatement, defineList, defineList, defineNamedBindings, getSql, internalExecute, mapperForType, mapperForType, mapperForType, parseSql, setQueryTimeout, toString
-
Methods inherited from class org.jdbi.v3.core.statement.BaseStatement
addCustomizers, attachToHandleForCleanup, callCustomizers, cleanUpForException, close, equals, getConfig, getContext, getHandle, hashCode
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.jdbi.v3.core.config.Configurable
addCustomizer, configure, define, getConfig, registerArgument, registerArgument, registerArrayType, registerArrayType, registerArrayType, registerArrayType, registerCodecFactory, registerCollector, registerCollector, registerColumnMapper, registerColumnMapper, registerColumnMapper, registerColumnMapper, registerColumnMapper, registerColumnMapper, registerExtension, registerRowMapper, registerRowMapper, registerRowMapper, registerRowMapper, setMapKeyColumn, setMapValueColumn, setSqlArrayArgumentStrategy, setSqlLogger, setSqlParser, setTemplateEngine, setTimingCollector
-
Methods inherited from interface org.jdbi.v3.core.result.ResultBearing
collectInto, collectInto, collectInto, collectRows, map, map, map, mapTo, mapTo, mapTo, mapTo, mapToBean, mapToMap, mapToMap, mapToMap, reduceResultSet, reduceRows, reduceRows, reduceRows
-
-
-
-
Field Detail
-
bindings
private final java.util.List<PreparedBinding> bindings
-
preparedFinders
final java.util.Map<NamedArgumentFinderFactory.PrepareKey,java.util.function.Function<java.lang.String,java.util.Optional<java.util.function.Function<java.lang.Object,Argument>>>> preparedFinders
-
-
Constructor Detail
-
PreparedBatch
public PreparedBatch(Handle handle, java.lang.CharSequence sql)
-
PreparedBatch
public PreparedBatch(Handle handle, java.lang.String sql)
Backwards compatible constructor that takes an explicit string argument.- See Also:
PreparedBatch(Handle, CharSequence)
-
-
Method Detail
-
bindNamedArgumentFinder
PreparedBatch bindNamedArgumentFinder(NamedArgumentFinderFactory factory, java.lang.String prefix, java.lang.Object value, java.lang.reflect.Type type, java.util.function.Supplier<NamedArgumentFinder> backupArgumentFinder)
- Overrides:
bindNamedArgumentFinderin classSqlStatement<PreparedBatch>
-
getBinding
protected PreparedBinding getBinding()
- Overrides:
getBindingin classSqlStatement<PreparedBatch>
-
buildArgument
java.util.function.Function<java.lang.Object,Argument> buildArgument(QualifiedType<?> type)
-
scanResultSet
public <R> R scanResultSet(ResultSetScanner<R> resultSetScanner)
Description copied from interface:ResultBearingInvokes the mapper with a result set supplier, and returns the value returned by the mapper.- Specified by:
scanResultSetin interfaceResultBearing- Type Parameters:
R- result type returned by the mapper.- Parameters:
resultSetScanner- result set scanner- Returns:
- the value returned by the mapper.
-
execute
public int[] execute()
Execute the batch and return the number of rows affected for each batch part. Note that some database drivers might return special values likeStatement.SUCCESS_NO_INFOorStatement.EXECUTE_FAILED.- Returns:
- the number of rows affected per batch part
- See Also:
Statement.executeBatch()
-
executeAndGetModCount
public ResultIterator<java.lang.Integer> executeAndGetModCount()
Execute the batch and return the mod counts as inexecute, but as a Jdbi result iterator instead of an array.- Returns:
- the number of rows affected per batch part
- See Also:
execute(),Statement.executeBatch()
-
executeAndReturnGeneratedKeys
@Deprecated public ResultBearing executeAndReturnGeneratedKeys(java.lang.String... columnNames)
Deprecated.UseexecutePreparedBatch(String...)which has the same functionality but also returns the per-batch modified row counts.Execute the batch and give access to any generated keys returned by the operation.- Parameters:
columnNames- The column names for generated keys.- Returns:
- A
ResultBearingobject that can be used to access the results of the batch.
-
executePreparedBatch
public BatchResultBearing executePreparedBatch(java.lang.String... columnNames)
Execute the batch and give access to any generated keys returned by the operation.- Parameters:
columnNames- The column names for generated keys.- Returns:
- A
BatchResultBearingobject that can be used to access the results of the batch and the per-batch modified row counts.
-
execute
public <R> R execute(ResultProducer<R> producer)
Executes the batch, returning the result obtained from the givenResultProducer.- Type Parameters:
R- the type of the result- Parameters:
producer- the result producer.- Returns:
- value returned by the result producer.
-
execute
private <R> R execute(ResultProducer<R> producer, java.util.function.Consumer<PreparedBatch.ExecutedBatch> batchConsumer)
-
internalBatchExecute
private PreparedBatch.ExecutedBatch internalBatchExecute()
-
add
public PreparedBatch add()
Add the current binding as a saved batch and clear the binding.- Returns:
- this
-
add
public PreparedBatch add(java.lang.Object... args)
Bind arguments positionally, add the binding as a saved batch, and then clear the current binding.- Parameters:
args- the positional arguments to bind- Returns:
- this
-
add
public PreparedBatch add(java.util.Map<java.lang.String,?> args)
Bind arguments from a Map, add the binding as a saved batch, then clear the current binding.- Parameters:
args- map to bind arguments from for named parameters on the statement- Returns:
- this
-
size
public int size()
Returns the number of bindings in this batch.- Returns:
- the number of bindings in this batch.
-
-