Class StatementContext

java.lang.Object
org.jdbi.v3.core.statement.StatementContext
All Implemented Interfaces:
Closeable, AutoCloseable

public class StatementContext extends Object implements Closeable
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.

  • Field Details

    • config

      private final ConfigRegistry config
    • extensionMethod

      private final ExtensionMethod extensionMethod
    • cleanables

      private final Set<Cleanable> cleanables
    • rawSql

      private String rawSql
    • renderedSql

      private String renderedSql
    • parsedSql

      private ParsedSql parsedSql
    • statement

      private PreparedStatement statement
    • connection

      private Connection connection
    • binding

      private Binding binding
    • returningGeneratedKeys

      private boolean returningGeneratedKeys
    • generatedKeysColumnNames

      private String[] generatedKeysColumnNames
    • concurrentUpdatable

      private boolean concurrentUpdatable
    • executionMoment

      private Instant executionMoment
    • completionMoment

      private Instant completionMoment
    • exceptionMoment

      private Instant exceptionMoment
  • Constructor Details

  • Method Details

    • create

      static StatementContext create(ConfigRegistry config, ExtensionMethod extensionMethod)
    • getConfig

      public <C extends JdbiConfig<C>> C getConfig(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 the ConfigRegistry.
      Returns:
      the ConfigRegistry used by this context.
    • getAttributes

      public Map<String,Object> getAttributes()
      Returns the attributes applied in this context.
      Returns:
      the defined attributes.
    • getAttribute

      public Object getAttribute(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(String key, Object value)
      Define an attribute for in this context.
      Parameters:
      key - the key for the attribute
      value - the value for the attribute
    • findArgumentFor

      public Optional<Argument> findArgumentFor(Type type, 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 Optional<Argument> findArgumentFor(QualifiedType<?> type, 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 Optional<SqlArrayType<?>> findSqlArrayTypeFor(Type elementType)
      Obtain an SqlArrayType for the given array element type in this context
      Parameters:
      elementType - the array element type.
      Returns:
      an SqlArrayType for the given element type.
    • findMapperFor

      public <T> Optional<RowMapper<T>> findMapperFor(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> 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 Optional<RowMapper<?>> findMapperFor(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> 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> Optional<ColumnMapper<T>> findColumnMapperFor(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> 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 Optional<ColumnMapper<?>> findColumnMapperFor(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> 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 Optional<RowMapper<?>> findRowMapperFor(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> Optional<RowMapper<T>> findRowMapperFor(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> 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 Optional<Collector<?,?,?>> findCollectorFor(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 Optional<Type> findElementTypeFor(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(String rawSql)
    • getRawSql

      public String getRawSql()
      Obtain the initial sql for the statement used to create the statement
      Returns:
      the initial sql
    • setRenderedSql

      void setRenderedSql(String renderedSql)
    • getRenderedSql

      public String getRenderedSql()
      Obtain the rendered SQL statement

      Not 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 statement

      Not available until until statement execution time

      Returns:
      the sql statement as it will be executed against the database
    • setStatement

      void setStatement(PreparedStatement stmt)
    • getStatement

      public 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(Connection connection)
    • getConnection

      public 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 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(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 be ResultSet.CONCUR_UPDATABLE, otherwise the result set is not updatable, and will have concurrency level ResultSet.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 with isReturningGeneratedKeys(), 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 Instant getExecutionMoment()
      Returns the query execution start as an Instant.
      Returns:
      the Instant at which query execution began
    • setExecutionMoment

      public void setExecutionMoment(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 Instant getCompletionMoment()
      If query execution was successful, returns the query execution end as an Instant.
      Returns:
      the Instant at which query execution ended, if it did so successfully
    • setCompletionMoment

      public void setCompletionMoment(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 Instant getExceptionMoment()
      If query execution failed, returns the query execution end as an Instant.
      Returns:
      the Instant at which query execution ended, if it did so with an exception
    • setExceptionMoment

      public void setExceptionMoment(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(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 a Cleanable 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, and StatementBuilder.

      Parameters:
      cleanable - the Cleanable to clean on close
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • getExtensionMethod

      public ExtensionMethod getExtensionMethod()
    • isClean

      boolean isClean()
    • getListeners

      private 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(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public final int hashCode()
      Overrides:
      hashCode in class Object