Class StatementContext

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class StatementContext
    extends java.lang.Object
    implements java.io.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 Detail

      • cleanables

        private final java.util.Set<Cleanable> cleanables
      • rawSql

        private java.lang.String rawSql
      • renderedSql

        private java.lang.String renderedSql
      • statement

        private java.sql.PreparedStatement statement
      • connection

        private java.sql.Connection connection
      • 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
    • Method Detail

      • 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 the ConfigRegistry.
        Returns:
        the ConfigRegistry used 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 attribute
        value - 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 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> 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.
      • 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 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​(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
      • 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 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 java.time.Instant getExecutionMoment()
        Returns the query execution start as an Instant.
        Returns:
        the Instant at 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 an Instant.
        Returns:
        the Instant at 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 an Instant.
        Returns:
        the Instant at 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 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 java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
      • isClean

        boolean isClean()
      • 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:
        equals in class java.lang.Object
      • hashCode

        public final int hashCode()
        Overrides:
        hashCode in class java.lang.Object