Package org.jdbi.v3.core.statement
Class StatementContext
java.lang.Object
org.jdbi.v3.core.statement.StatementContext
- All Implemented Interfaces:
Closeable
,AutoCloseable
The statement context provides access to statement-local configuration.
Context is inherited from the parent
Handle
initially 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.
-
Method Summary
Modifier and TypeMethodDescriptionvoid
addCleanable
(Cleanable cleanable) Registers aCleanable
to be invoked when the statement context is closed.void
close()
void
Define an attribute for in this context.final boolean
findArgumentFor
(Type type, Object value) Obtain an argument for given value in this contextfindArgumentFor
(QualifiedType<?> type, Object value) Obtain an argument for given value in this contextfindCollectorFor
(Type containerType) Obtain a collector for the given type.<T> Optional<ColumnMapper<T>>
findColumnMapperFor
(Class<T> type) Obtain a column mapper for the given type in this context.Optional<ColumnMapper<?>>
findColumnMapperFor
(Type type) Obtain a column mapper for the given type in this context.<T> Optional<ColumnMapper<T>>
findColumnMapperFor
(GenericType<T> type) Obtain a column mapper for the given type in this context.<T> Optional<ColumnMapper<T>>
findColumnMapperFor
(QualifiedType<T> type) Obtain a column mapper for the given qualified type in this context.findElementTypeFor
(Type containerType) Returns the element type for the given container type.findMapperFor
(Class<T> type) Obtain a mapper for the given type in this context.findMapperFor
(Type type) Obtain a mapper for the given type in this context.findMapperFor
(GenericType<T> type) Obtain a mapper for the given type in this context.findMapperFor
(QualifiedType<T> type) Obtain a mapper for the given qualified type in this context.findRowMapperFor
(Class<T> type) Obtain a row mapper for the given type in this context.findRowMapperFor
(Type type) Obtain a row mapper for the given type in this context.findRowMapperFor
(GenericType<T> type) Obtain a row mapper for the given type in this context.Optional<SqlArrayType<?>>
findSqlArrayTypeFor
(Type elementType) Obtain anSqlArrayType
for the given array element type in this contextgetAttribute
(String key) Obtain the value of an attributeReturns the attributes applied in this context.Returns the statement binding.If query execution was successful, returns the query execution end as anInstant
.Returns theConfigRegistry
.<C extends JdbiConfig<C>>
CGets the configuration object of the given type, associated with this context.Obtain the JDBC connection being used for this statementlong
getElapsedTime
(ChronoUnit unit) Convenience method to measure elapsed time between start of query execution and completion or exception as appropriate.If query execution failed, returns the query execution end as anInstant
.Returns the query execution start as anInstant
.String[]
Returns the generated key column names or empty if none were generated.Obtain the parsed SQL statementObtain the initial sql for the statement used to create the statementObtain the rendered SQL statementReturns the strategy used by this context to bind array-type arguments to SQL statements.Obtain the actual prepared statement being used.final int
hashCode()
boolean
Return if the statement should be concurrent updatable.boolean
Whether the statement being generated is expected to return generated keys.void
setCompletionMoment
(Instant completionMoment) Sets the query execution end.void
setConcurrentUpdatable
(boolean concurrentUpdatable) Set the context to create a concurrent updatable result set.void
setExceptionMoment
(Instant exceptionMoment) Sets the query execution end.void
setExecutionMoment
(Instant executionMoment) Sets the query execution start.void
setGeneratedKeysColumnNames
(String[] generatedKeysColumnNames) Set the generated key column names.void
setReturningGeneratedKeys
(boolean b) Sets whether the current statement returns generated keys.
-
Method Details
-
getConfig
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
Returns theConfigRegistry
.- Returns:
- the
ConfigRegistry
used by this context.
-
getAttributes
Returns the attributes applied in this context.- Returns:
- the defined attributes.
-
getAttribute
Obtain the value of an attribute- Parameters:
key
- the name of the attribute- Returns:
- the value of the attribute
-
define
Define an attribute for in this context.- Parameters:
key
- the key for the attributevalue
- the value for the attribute
-
findArgumentFor
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
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
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
Obtain anSqlArrayType
for the given array element type in this context- Parameters:
elementType
- the array element type.- Returns:
- an
SqlArrayType
for the given element type.
-
findMapperFor
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
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
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
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
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
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
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
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
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
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
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
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
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.
-
getRawSql
Obtain the initial sql for the statement used to create the statement- Returns:
- the initial sql
-
getRenderedSql
Obtain the rendered SQL statementNot available until until statement execution time
- Returns:
- the sql statement after processing template directives.
-
getParsedSql
Obtain the parsed SQL statementNot available until until statement execution time
- Returns:
- the sql statement as it will be executed against the database
-
getStatement
Obtain the actual prepared statement being used.Not available until execution time
- Returns:
- Obtain the actual prepared statement being used.
-
getConnection
Obtain the JDBC connection being used for this statement- Returns:
- the JDBC connection
-
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
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
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
Returns the query execution start as anInstant
.- Returns:
- the
Instant
at which query execution began
-
setExecutionMoment
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
If query execution was successful, returns the query execution end as anInstant
.- Returns:
- the
Instant
at which query execution ended, if it did so successfully
-
setCompletionMoment
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
If query execution failed, returns the query execution end as anInstant
.- Returns:
- the
Instant
at which query execution ended, if it did so with an exception
-
setExceptionMoment
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
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
Registers aCleanable
to 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:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
getExtensionMethod
-
equals
-
hashCode
public final int hashCode()
-