Package org.jdbi.v3.core.statement
Class StatementContext
- java.lang.Object
-
- org.jdbi.v3.core.statement.StatementContext
-
- All Implemented Interfaces:
java.io.Closeable,java.lang.AutoCloseable
public class StatementContext extends java.lang.Object implements java.io.CloseableThe statement context provides access to statement-local configuration. Context is inherited from the parentHandleinitially and changes will not outlive the statement. The context will be passed through most major jdbi APIs.DISCLAIMER: The class is not intended to be extended. The final modifier is absent to allow mock tools to create a mock object of this class in the user code.
-
-
Field Summary
Fields Modifier and Type Field Description private Bindingbindingprivate java.util.Set<Cleanable>cleanablesprivate java.time.InstantcompletionMomentprivate booleanconcurrentUpdatableprivate ConfigRegistryconfigprivate java.sql.Connectionconnectionprivate java.time.InstantexceptionMomentprivate java.time.InstantexecutionMomentprivate ExtensionMethodextensionMethodprivate java.lang.String[]generatedKeysColumnNamesprivate ParsedSqlparsedSqlprivate java.lang.StringrawSqlprivate java.lang.StringrenderedSqlprivate booleanreturningGeneratedKeysprivate java.sql.PreparedStatementstatement
-
Constructor Summary
Constructors Modifier Constructor Description privateStatementContext(ConfigRegistry config, ExtensionMethod extensionMethod)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddCleanable(Cleanable cleanable)Registers aCleanableto be invoked when the statement context is closed.voidclose()(package private) static StatementContextcreate(ConfigRegistry config, ExtensionMethod extensionMethod)voiddefine(java.lang.String key, java.lang.Object value)Define an attribute for in this context.booleanequals(java.lang.Object o)java.util.Optional<Argument>findArgumentFor(java.lang.reflect.Type type, java.lang.Object value)Obtain an argument for given value in this contextjava.util.Optional<Argument>findArgumentFor(QualifiedType<?> type, java.lang.Object value)Obtain an argument for given value in this contextjava.util.Optional<java.util.stream.Collector<?,?,?>>findCollectorFor(java.lang.reflect.Type containerType)Obtain a collector for the given type.<T> java.util.Optional<ColumnMapper<T>>findColumnMapperFor(java.lang.Class<T> type)Obtain a column mapper for the given type in this context.java.util.Optional<ColumnMapper<?>>findColumnMapperFor(java.lang.reflect.Type type)Obtain a column mapper for the given type in this context.<T> java.util.Optional<ColumnMapper<T>>findColumnMapperFor(GenericType<T> type)Obtain a column mapper for the given type in this context.<T> java.util.Optional<ColumnMapper<T>>findColumnMapperFor(QualifiedType<T> type)Obtain a column mapper for the given qualified type in this context.java.util.Optional<java.lang.reflect.Type>findElementTypeFor(java.lang.reflect.Type containerType)Returns the element type for the given container type.<T> java.util.Optional<RowMapper<T>>findMapperFor(java.lang.Class<T> type)Obtain a mapper for the given type in this context.java.util.Optional<RowMapper<?>>findMapperFor(java.lang.reflect.Type type)Obtain a mapper for the given type in this context.<T> java.util.Optional<RowMapper<T>>findMapperFor(GenericType<T> type)Obtain a mapper for the given type in this context.<T> java.util.Optional<RowMapper<T>>findMapperFor(QualifiedType<T> type)Obtain a mapper for the given qualified type in this context.<T> java.util.Optional<RowMapper<T>>findRowMapperFor(java.lang.Class<T> type)Obtain a row mapper for the given type in this context.java.util.Optional<RowMapper<?>>findRowMapperFor(java.lang.reflect.Type type)Obtain a row mapper for the given type in this context.<T> java.util.Optional<RowMapper<T>>findRowMapperFor(GenericType<T> type)Obtain a row mapper for the given type in this context.java.util.Optional<SqlArrayType<?>>findSqlArrayTypeFor(java.lang.reflect.Type elementType)Obtain anSqlArrayTypefor the given array element type in this contextjava.lang.ObjectgetAttribute(java.lang.String key)Obtain the value of an attributejava.util.Map<java.lang.String,java.lang.Object>getAttributes()Returns the attributes applied in this context.BindinggetBinding()Returns the statement binding.java.time.InstantgetCompletionMoment()If query execution was successful, returns the query execution end as anInstant.ConfigRegistrygetConfig()Returns theConfigRegistry.<C extends JdbiConfig<C>>
CgetConfig(java.lang.Class<C> configClass)Gets the configuration object of the given type, associated with this context.java.sql.ConnectiongetConnection()Obtain the JDBC connection being used for this statementlonggetElapsedTime(java.time.temporal.ChronoUnit unit)Convenience method to measure elapsed time between start of query execution and completion or exception as appropriate.java.time.InstantgetExceptionMoment()If query execution failed, returns the query execution end as anInstant.java.time.InstantgetExecutionMoment()Returns the query execution start as anInstant.ExtensionMethodgetExtensionMethod()java.lang.String[]getGeneratedKeysColumnNames()Returns the generated key column names or empty if none were generated.private java.util.Collection<StatementContextListener>getListeners()ParsedSqlgetParsedSql()Obtain the parsed SQL statementjava.lang.StringgetRawSql()Obtain the initial sql for the statement used to create the statementjava.lang.StringgetRenderedSql()Obtain the rendered SQL statementSqlArrayArgumentStrategygetSqlArrayArgumentStrategy()Returns the strategy used by this context to bind array-type arguments to SQL statements.java.sql.PreparedStatementgetStatement()Obtain the actual prepared statement being used.inthashCode()(package private) booleanisClean()booleanisConcurrentUpdatable()Return if the statement should be concurrent updatable.booleanisReturningGeneratedKeys()Whether the statement being generated is expected to return generated keys.private voidnotifyCleanableAdded(Cleanable cleanable)private voidnotifyCleanableRemoved(Cleanable cleanable)private voidnotifyContextCleaned()private voidnotifyContextCreated()(package private) StatementContextsetBinding(Binding b)voidsetCompletionMoment(java.time.Instant completionMoment)Sets the query execution end.voidsetConcurrentUpdatable(boolean concurrentUpdatable)Set the context to create a concurrent updatable result set.(package private) StatementContextsetConnection(java.sql.Connection connection)voidsetExceptionMoment(java.time.Instant exceptionMoment)Sets the query execution end.voidsetExecutionMoment(java.time.Instant executionMoment)Sets the query execution start.voidsetGeneratedKeysColumnNames(java.lang.String[] generatedKeysColumnNames)Set the generated key column names.(package private) voidsetParsedSql(ParsedSql parsedSql)(package private) StatementContextsetRawSql(java.lang.String rawSql)(package private) voidsetRenderedSql(java.lang.String renderedSql)voidsetReturningGeneratedKeys(boolean b)Sets whether the current statement returns generated keys.(package private) voidsetStatement(java.sql.PreparedStatement stmt)
-
-
-
Field Detail
-
config
private final ConfigRegistry config
-
extensionMethod
private final ExtensionMethod extensionMethod
-
cleanables
private final java.util.Set<Cleanable> cleanables
-
rawSql
private java.lang.String rawSql
-
renderedSql
private java.lang.String renderedSql
-
parsedSql
private ParsedSql parsedSql
-
statement
private java.sql.PreparedStatement statement
-
connection
private java.sql.Connection connection
-
binding
private Binding binding
-
returningGeneratedKeys
private boolean returningGeneratedKeys
-
generatedKeysColumnNames
private java.lang.String[] generatedKeysColumnNames
-
concurrentUpdatable
private boolean concurrentUpdatable
-
executionMoment
private java.time.Instant executionMoment
-
completionMoment
private java.time.Instant completionMoment
-
exceptionMoment
private java.time.Instant exceptionMoment
-
-
Constructor Detail
-
StatementContext
private StatementContext(ConfigRegistry config, ExtensionMethod extensionMethod)
-
-
Method Detail
-
create
static StatementContext create(ConfigRegistry config, ExtensionMethod extensionMethod)
-
getConfig
public <C extends JdbiConfig<C>> C getConfig(java.lang.Class<C> configClass)
Gets the configuration object of the given type, associated with this context.- Type Parameters:
C- the configuration type- Parameters:
configClass- the configuration type- Returns:
- the configuration object of the given type, associated with this context.
-
getConfig
public ConfigRegistry getConfig()
Returns theConfigRegistry.- Returns:
- the
ConfigRegistryused by this context.
-
getAttributes
public java.util.Map<java.lang.String,java.lang.Object> getAttributes()
Returns the attributes applied in this context.- Returns:
- the defined attributes.
-
getAttribute
public java.lang.Object getAttribute(java.lang.String key)
Obtain the value of an attribute- Parameters:
key- the name of the attribute- Returns:
- the value of the attribute
-
define
public void define(java.lang.String key, java.lang.Object value)Define an attribute for in this context.- Parameters:
key- the key for the attributevalue- the value for the attribute
-
findArgumentFor
public java.util.Optional<Argument> findArgumentFor(java.lang.reflect.Type type, java.lang.Object value)
Obtain an argument for given value in this context- Parameters:
type- the type of the argument.value- the argument value.- Returns:
- an Argument for the given value.
-
findArgumentFor
public java.util.Optional<Argument> findArgumentFor(QualifiedType<?> type, java.lang.Object value)
Obtain an argument for given value in this context- Parameters:
type- the type of the argument.value- the argument value.- Returns:
- an Argument for the given value.
-
getSqlArrayArgumentStrategy
public SqlArrayArgumentStrategy getSqlArrayArgumentStrategy()
Returns the strategy used by this context to bind array-type arguments to SQL statements.- Returns:
- the strategy used to bind array-type arguments to SQL statements
-
findSqlArrayTypeFor
public java.util.Optional<SqlArrayType<?>> findSqlArrayTypeFor(java.lang.reflect.Type elementType)
Obtain anSqlArrayTypefor the given array element type in this context- Parameters:
elementType- the array element type.- Returns:
- an
SqlArrayTypefor the given element type.
-
findMapperFor
public <T> java.util.Optional<RowMapper<T>> findMapperFor(java.lang.Class<T> type)
Obtain a mapper for the given type in this context.- Type Parameters:
T- the type to map- Parameters:
type- the target type to map to- Returns:
- a mapper for the given type, or empty if no row or column mappers is registered for the given type.
-
findMapperFor
public <T> java.util.Optional<RowMapper<T>> findMapperFor(GenericType<T> type)
Obtain a mapper for the given type in this context.- Type Parameters:
T- the type to map- Parameters:
type- the target type to map to- Returns:
- a mapper for the given type, or empty if no row or column mappers is registered for the given type.
-
findMapperFor
public java.util.Optional<RowMapper<?>> findMapperFor(java.lang.reflect.Type type)
Obtain a mapper for the given type in this context.- Parameters:
type- the target type to map to- Returns:
- a mapper for the given type, or empty if no row or column mappers is registered for the given type.
-
findMapperFor
public <T> java.util.Optional<RowMapper<T>> findMapperFor(QualifiedType<T> type)
Obtain a mapper for the given qualified type in this context.- Parameters:
type- the target qualified type to map to- Returns:
- a mapper for the given qualified type, or empty if no row or column mappers is registered for the given type.
-
findColumnMapperFor
public <T> java.util.Optional<ColumnMapper<T>> findColumnMapperFor(java.lang.Class<T> type)
Obtain a column mapper for the given type in this context.- Type Parameters:
T- the type to map- Parameters:
type- the target type to map to- Returns:
- a ColumnMapper for the given type, or empty if no column mapper is registered for the given type.
-
findColumnMapperFor
public <T> java.util.Optional<ColumnMapper<T>> findColumnMapperFor(GenericType<T> type)
Obtain a column mapper for the given type in this context.- Type Parameters:
T- the type to map- Parameters:
type- the target type to map to- Returns:
- a ColumnMapper for the given type, or empty if no column mapper is registered for the given type.
-
findColumnMapperFor
public java.util.Optional<ColumnMapper<?>> findColumnMapperFor(java.lang.reflect.Type type)
Obtain a column mapper for the given type in this context.- Parameters:
type- the target type to map to- Returns:
- a ColumnMapper for the given type, or empty if no column mapper is registered for the given type.
-
findColumnMapperFor
public <T> java.util.Optional<ColumnMapper<T>> findColumnMapperFor(QualifiedType<T> type)
Obtain a column mapper for the given qualified type in this context.- Parameters:
type- the qualified target type to map to- Returns:
- a ColumnMapper for the given type, or empty if no column mapper is registered for the given type.
-
findRowMapperFor
public java.util.Optional<RowMapper<?>> findRowMapperFor(java.lang.reflect.Type type)
Obtain a row mapper for the given type in this context.- Parameters:
type- the target type to map to- Returns:
- a RowMapper for the given type, or empty if no row mapper is registered for the given type.
-
findRowMapperFor
public <T> java.util.Optional<RowMapper<T>> findRowMapperFor(java.lang.Class<T> type)
Obtain a row mapper for the given type in this context.- Type Parameters:
T- the type to map- Parameters:
type- the target type to map to- Returns:
- a RowMapper for the given type, or empty if no row mapper is registered for the given type.
-
findRowMapperFor
public <T> java.util.Optional<RowMapper<T>> findRowMapperFor(GenericType<T> type)
Obtain a row mapper for the given type in this context.- Type Parameters:
T- the type to map- Parameters:
type- the target type to map to- Returns:
- a RowMapper for the given type, or empty if no row mapper is registered for the given type.
-
findCollectorFor
public java.util.Optional<java.util.stream.Collector<?,?,?>> findCollectorFor(java.lang.reflect.Type containerType)
Obtain a collector for the given type.- Parameters:
containerType- the container type.- Returns:
- a Collector for the given container type, or empty null if no collector is registered for the given type.
-
findElementTypeFor
public java.util.Optional<java.lang.reflect.Type> findElementTypeFor(java.lang.reflect.Type containerType)
Returns the element type for the given container type.- Parameters:
containerType- the container type.- Returns:
- the element type for the given container type, if available.
-
setRawSql
StatementContext setRawSql(java.lang.String rawSql)
-
getRawSql
public java.lang.String getRawSql()
Obtain the initial sql for the statement used to create the statement- Returns:
- the initial sql
-
setRenderedSql
void setRenderedSql(java.lang.String renderedSql)
-
getRenderedSql
public java.lang.String getRenderedSql()
Obtain the rendered SQL statementNot available until until statement execution time
- Returns:
- the sql statement after processing template directives.
-
setParsedSql
void setParsedSql(ParsedSql parsedSql)
-
getParsedSql
public ParsedSql getParsedSql()
Obtain the parsed SQL statementNot available until until statement execution time
- Returns:
- the sql statement as it will be executed against the database
-
setStatement
void setStatement(java.sql.PreparedStatement stmt)
-
getStatement
public java.sql.PreparedStatement getStatement()
Obtain the actual prepared statement being used.Not available until execution time
- Returns:
- Obtain the actual prepared statement being used.
-
setConnection
StatementContext setConnection(java.sql.Connection connection)
-
getConnection
public java.sql.Connection getConnection()
Obtain the JDBC connection being used for this statement- Returns:
- the JDBC connection
-
setBinding
StatementContext setBinding(Binding b)
-
getBinding
public Binding getBinding()
Returns the statement binding.- Returns:
- The statement binding.
-
setReturningGeneratedKeys
public void setReturningGeneratedKeys(boolean b)
Sets whether the current statement returns generated keys.- Parameters:
b- return generated keys?
-
isReturningGeneratedKeys
public boolean isReturningGeneratedKeys()
Whether the statement being generated is expected to return generated keys.- Returns:
- whether the statement being generated is expected to return generated keys.
-
getGeneratedKeysColumnNames
public java.lang.String[] getGeneratedKeysColumnNames()
Returns the generated key column names or empty if none were generated.- Returns:
- the generated key column names. Returns an empty array if none exist.
-
setGeneratedKeysColumnNames
public void setGeneratedKeysColumnNames(java.lang.String[] generatedKeysColumnNames)
Set the generated key column names.- Parameters:
generatedKeysColumnNames- the generated key column names
-
isConcurrentUpdatable
public boolean isConcurrentUpdatable()
Return if the statement should be concurrent updatable. If this returns true, the concurrency level of the created ResultSet will beResultSet.CONCUR_UPDATABLE, otherwise the result set is not updatable, and will have concurrency levelResultSet.CONCUR_READ_ONLY.- Returns:
- if the statement generated should be concurrent updatable.
-
setConcurrentUpdatable
public void setConcurrentUpdatable(boolean concurrentUpdatable)
Set the context to create a concurrent updatable result set. This cannot be combined withisReturningGeneratedKeys(), only one option may be selected. It does not make sense to combine these either, as one applies to queries, and the other applies to updates.- Parameters:
concurrentUpdatable- if the result set should be concurrent updatable.
-
getExecutionMoment
@Nullable public java.time.Instant getExecutionMoment()
Returns the query execution start as anInstant.- Returns:
- the
Instantat which query execution began
-
setExecutionMoment
public void setExecutionMoment(java.time.Instant executionMoment)
Sets the query execution start. This is not part of the Jdbi API and should not be called by code outside JDBI.- Parameters:
executionMoment- Sets the start of query execution.
-
getCompletionMoment
@Nullable public java.time.Instant getCompletionMoment()
If query execution was successful, returns the query execution end as anInstant.- Returns:
- the
Instantat which query execution ended, if it did so successfully
-
setCompletionMoment
public void setCompletionMoment(java.time.Instant completionMoment)
Sets the query execution end. This is not part of the Jdbi API and should not be called by code outside JDBI.- Parameters:
completionMoment- Sets the end of query execution.
-
getExceptionMoment
@Nullable public java.time.Instant getExceptionMoment()
If query execution failed, returns the query execution end as anInstant.- Returns:
- the
Instantat which query execution ended, if it did so with an exception
-
setExceptionMoment
public void setExceptionMoment(java.time.Instant exceptionMoment)
Sets the query execution end. This is not part of the Jdbi API and should not be called by code outside JDBI.- Parameters:
exceptionMoment- Sets the end of query execution.
-
getElapsedTime
public long getElapsedTime(java.time.temporal.ChronoUnit unit)
Convenience method to measure elapsed time between start of query execution and completion or exception as appropriate. Do not call with a null argument or before a query has executed/exploded.- Parameters:
unit- the time unit to convert to- Returns:
- the elapsed time in the given unit
-
addCleanable
public void addCleanable(Cleanable cleanable)
Registers aCleanableto be invoked when the statement context is closed. Cleanables can be registered on a statement context, which will be cleaned up when the statement finishes or (in the case of a ResultIterator), the object representing the results is closed.Resources cleaned up by Jdbi include
ResultSet,Statement,Handle,Array, andStatementBuilder.- Parameters:
cleanable- the Cleanable to clean on close
-
close
public void close()
- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable
-
getExtensionMethod
public ExtensionMethod getExtensionMethod()
-
isClean
boolean isClean()
-
getListeners
private java.util.Collection<StatementContextListener> getListeners()
-
notifyContextCreated
private void notifyContextCreated()
-
notifyContextCleaned
private void notifyContextCleaned()
-
notifyCleanableRemoved
private void notifyCleanableRemoved(Cleanable cleanable)
-
notifyCleanableAdded
private void notifyCleanableAdded(Cleanable cleanable)
-
equals
public final boolean equals(java.lang.Object o)
- Overrides:
equalsin classjava.lang.Object
-
hashCode
public final int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
-