Package org.jdbi.v3.core.statement
Class SqlStatements
- java.lang.Object
-
- org.jdbi.v3.core.statement.SqlStatements
-
- All Implemented Interfaces:
JdbiConfig<SqlStatements>
public final class SqlStatements extends java.lang.Object implements JdbiConfig<SqlStatements>
Configuration holder forSqlStatements.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classSqlStatements.StatementCacheKey
-
Field Summary
Fields Modifier and Type Field Description private booleanallowUnusedBindingsprivate booleanattachAllStatementsForCleanupprivate booleanattachCallbackStatementsForCleanupprivate java.util.Map<java.lang.String,java.lang.Object>attributesprivate java.util.Collection<StatementContextListener>contextListenersprivate java.util.Collection<StatementCustomizer>customizersprivate java.lang.IntegerqueryTimeoutstatic intSQL_TEMPLATE_CACHE_SIZEThe default size of the SQL template cache.private SqlLoggersqlLoggerprivate SqlParsersqlParserprivate JdbiCache<SqlStatements.StatementCacheKey,java.util.function.Function<StatementContext,java.lang.String>>templateCacheprivate TemplateEnginetemplateEngine
-
Constructor Summary
Constructors Modifier Constructor Description SqlStatements()privateSqlStatements(SqlStatements that)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description SqlStatementsaddContextListener(StatementContextListener listener)SqlStatementsaddCustomizer(StatementCustomizer customizer)Provides a means for custom statement modification.private static JdbiCacheLoader<SqlStatements.StatementCacheKey,java.util.function.Function<StatementContext,java.lang.String>>cacheLoaderFunction(StatementContext ctx)<T> TcacheStats()Returns cache statistics for the internal template cache.SqlStatementscreateCopy()Returns a copy of this configuration object.(package private) voidcustomize(java.sql.Statement statement)SqlStatementsdefine(java.lang.String key, java.lang.Object value)Define an attribute forStatementContextfor statements executed by Jdbi.SqlStatementsdefineMap(java.util.Map<java.lang.String,?> values)Defines attributes for each key/value pair in the Map.java.lang.ObjectgetAttribute(java.lang.String key)Obtain the value of an attributejava.util.Map<java.lang.String,java.lang.Object>getAttributes()Returns the attributes which will be applied toSQL statementscreated by Jdbi.(package private) java.util.Collection<StatementContextListener>getContextListeners()(package private) java.util.Collection<StatementCustomizer>getCustomizers()java.lang.IntegergetQueryTimeout()SqlLoggergetSqlLogger()Returns the current logger.SqlParsergetSqlParser()TemplateEnginegetTemplateEngine()Returns theTemplateEnginewhich renders the SQL template.TimingCollectorgetTimingCollector()Deprecated.usegetSqlLogger()insteadbooleanisAttachAllStatementsForCleanup()If true, all statements created withinJdbi.withHandle(org.jdbi.v3.core.HandleCallback<R, X>),Jdbi.useHandle(org.jdbi.v3.core.HandleConsumer<X>),Jdbi.inTransaction(org.jdbi.v3.core.HandleCallback<R, X>)andJdbi.useTransaction(org.jdbi.v3.core.HandleConsumer<X>)are attached to theHandleobject for cleanup.booleanisAttachCallbackStatementsForCleanup()If true, statements created withinJdbi.withHandle(org.jdbi.v3.core.HandleCallback<R, X>),Jdbi.useHandle(org.jdbi.v3.core.HandleConsumer<X>),Jdbi.inTransaction(org.jdbi.v3.core.HandleCallback<R, X>)andJdbi.useTransaction(org.jdbi.v3.core.HandleConsumer<X>)will be attached to theHandleobject in the callback for cleanup.booleanisUnusedBindingAllowed()(package private) java.lang.StringpreparedRender(java.lang.String template, StatementContext ctx)voidsetAttachAllStatementsForCleanup(boolean attachAllStatementsForCleanup)Sets whether all statements created will automatically attached to the correspondingHandleobject automatically.voidsetAttachCallbackStatementsForCleanup(boolean attachCallbackStatementsForCleanup)Sets whether statements created within theJdbi.withHandle(org.jdbi.v3.core.HandleCallback<R, X>),Jdbi.useHandle(org.jdbi.v3.core.HandleConsumer<X>),Jdbi.inTransaction(org.jdbi.v3.core.HandleCallback<R, X>)andJdbi.useTransaction(org.jdbi.v3.core.HandleConsumer<X>)callback methods will automatically attached to theHandleobject and therefore cleaned up when the callback ends.SqlStatementssetQueryTimeout(java.lang.Integer seconds)Jdbi does not implement its own timeout mechanism: it simply callsStatement.setQueryTimeout(int), leaving timeout handling to your jdbc driver.SqlStatementssetSqlLogger(SqlLogger sqlLogger)Sets aSqlLoggerinstance to log all SQL operations.SqlStatementssetSqlParser(SqlParser sqlParser)Sets theSqlParserused to parse parameters in SQL statements executed by Jdbi.SqlStatementssetTemplateCache(JdbiCacheBuilder cacheBuilder)Sets the cache used to avoid repeatedly parsing SQL statements.SqlStatementssetTemplateEngine(TemplateEngine templateEngine)Sets theTemplateEngineused to render SQL for allSQL statementsexecuted by Jdbi.SqlStatementssetTimingCollector(TimingCollector timingCollector)Deprecated.SqlStatementssetUnusedBindingAllowed(boolean unusedBindingAllowed)Sets whether or not an exception should be thrown when any arguments are given to a query but not actually used in it.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.jdbi.v3.core.config.JdbiConfig
setRegistry
-
-
-
-
Field Detail
-
SQL_TEMPLATE_CACHE_SIZE
public static final int SQL_TEMPLATE_CACHE_SIZE
The default size of the SQL template cache.- See Also:
- Constant Field Values
-
attributes
private final java.util.Map<java.lang.String,java.lang.Object> attributes
-
templateEngine
private TemplateEngine templateEngine
-
templateCache
private JdbiCache<SqlStatements.StatementCacheKey,java.util.function.Function<StatementContext,java.lang.String>> templateCache
-
sqlParser
private SqlParser sqlParser
-
sqlLogger
private SqlLogger sqlLogger
-
queryTimeout
private java.lang.Integer queryTimeout
-
allowUnusedBindings
private boolean allowUnusedBindings
-
attachAllStatementsForCleanup
private boolean attachAllStatementsForCleanup
-
attachCallbackStatementsForCleanup
private boolean attachCallbackStatementsForCleanup
-
customizers
private final java.util.Collection<StatementCustomizer> customizers
-
contextListeners
private final java.util.Collection<StatementContextListener> contextListeners
-
-
Constructor Detail
-
SqlStatements
public SqlStatements()
-
SqlStatements
private SqlStatements(SqlStatements that)
-
-
Method Detail
-
define
public SqlStatements define(java.lang.String key, java.lang.Object value)
Define an attribute forStatementContextfor statements executed by Jdbi.- Parameters:
key- the key for the attributevalue- the value for the attribute- Returns:
- this
-
defineMap
public SqlStatements defineMap(java.util.Map<java.lang.String,?> values)
Defines attributes for each key/value pair in the Map.- Parameters:
values- map of attributes to define.- Returns:
- this
-
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
-
getAttributes
public java.util.Map<java.lang.String,java.lang.Object> getAttributes()
Returns the attributes which will be applied toSQL statementscreated by Jdbi.- Returns:
- the defined attributes.
-
addCustomizer
public SqlStatements addCustomizer(StatementCustomizer customizer)
Provides a means for custom statement modification. Common customizations have their own methods, such asQuery.setMaxRows(int)- Parameters:
customizer- instance to be used to customize a statement- Returns:
- this
-
addContextListener
public SqlStatements addContextListener(StatementContextListener listener)
-
getTemplateEngine
public TemplateEngine getTemplateEngine()
Returns theTemplateEnginewhich renders the SQL template.- Returns:
- the template engine which renders the SQL template prior to parsing parameters.
-
setTemplateEngine
public SqlStatements setTemplateEngine(TemplateEngine templateEngine)
Sets theTemplateEngineused to render SQL for allSQL statementsexecuted by Jdbi. The default engine replaces<name>-style tokens with attributesdefinedon the statement context.- Parameters:
templateEngine- the new template engine.- Returns:
- this
-
setTemplateCache
@Beta public SqlStatements setTemplateCache(JdbiCacheBuilder cacheBuilder)
Sets the cache used to avoid repeatedly parsing SQL statements.- Parameters:
cacheBuilder- the cache builder to use to create the cache.- Returns:
- this
-
getSqlParser
public SqlParser getSqlParser()
-
setSqlParser
public SqlStatements setSqlParser(SqlParser sqlParser)
Sets theSqlParserused to parse parameters in SQL statements executed by Jdbi. The default parses colon-prefixed named parameter tokens, e.g.:name.- Parameters:
sqlParser- the new SQL parser.- Returns:
- this
-
getTimingCollector
@Deprecated public TimingCollector getTimingCollector()
Deprecated.usegetSqlLogger()instead- Returns:
- the timing collector
-
setTimingCollector
@Deprecated public SqlStatements setTimingCollector(TimingCollector timingCollector)
Deprecated.Sets theTimingCollectorused to collect timing about theSQL statementsexecuted by Jdbi. The default collector does nothing.- Parameters:
timingCollector- the new timing collector- Returns:
- this
-
getSqlLogger
public SqlLogger getSqlLogger()
Returns the current logger.- Returns:
- A
SqlLoggerinstance
-
setSqlLogger
public SqlStatements setSqlLogger(SqlLogger sqlLogger)
Sets aSqlLoggerinstance to log all SQL operations.- Parameters:
sqlLogger- The logger. Usingnullturns off all logging- Returns:
- this
-
getQueryTimeout
@Beta public java.lang.Integer getQueryTimeout()
-
setQueryTimeout
@Beta public SqlStatements setQueryTimeout(@Nullable java.lang.Integer seconds)
Jdbi does not implement its own timeout mechanism: it simply callsStatement.setQueryTimeout(int), leaving timeout handling to your jdbc driver.- Parameters:
seconds- the time in seconds to wait for a query to complete; 0 to disable the timeout; null to leave it at defaults (i.e. Jdbi will not callsetQueryTimeout(int))- Returns:
- this
-
isUnusedBindingAllowed
public boolean isUnusedBindingAllowed()
-
setUnusedBindingAllowed
public SqlStatements setUnusedBindingAllowed(boolean unusedBindingAllowed)
Sets whether or not an exception should be thrown when any arguments are given to a query but not actually used in it. Unused bindings tend to be bugs or oversights, but are not always. Defaults to false: unused bindings are not allowed.- Parameters:
unusedBindingAllowed- the new setting- Returns:
- this
- See Also:
Argument
-
isAttachAllStatementsForCleanup
public boolean isAttachAllStatementsForCleanup()
If true, all statements created withinJdbi.withHandle(org.jdbi.v3.core.HandleCallback<R, X>),Jdbi.useHandle(org.jdbi.v3.core.HandleConsumer<X>),Jdbi.inTransaction(org.jdbi.v3.core.HandleCallback<R, X>)andJdbi.useTransaction(org.jdbi.v3.core.HandleConsumer<X>)are attached to theHandleobject for cleanup.- Returns:
- True if statements are attached to their handle for cleanup
- Since:
- 3.38.0
-
setAttachAllStatementsForCleanup
@Beta public void setAttachAllStatementsForCleanup(boolean attachAllStatementsForCleanup)
Sets whether all statements created will automatically attached to the correspondingHandleobject automatically. This can be useful when mostly short-lived handles are used because closing the handle will now clean up all outstanding resources from any statement. The default is false.- Parameters:
attachAllStatementsForCleanup- If true, all statements are automatically attached to the Handle- Since:
- 3.38.0
-
isAttachCallbackStatementsForCleanup
public boolean isAttachCallbackStatementsForCleanup()
If true, statements created withinJdbi.withHandle(org.jdbi.v3.core.HandleCallback<R, X>),Jdbi.useHandle(org.jdbi.v3.core.HandleConsumer<X>),Jdbi.inTransaction(org.jdbi.v3.core.HandleCallback<R, X>)andJdbi.useTransaction(org.jdbi.v3.core.HandleConsumer<X>)will be attached to theHandleobject in the callback for cleanup.- Returns:
- True if statements are attached to their handle within Jdbi callbacks
- Since:
- 3.38.0
-
setAttachCallbackStatementsForCleanup
public void setAttachCallbackStatementsForCleanup(boolean attachCallbackStatementsForCleanup)
Sets whether statements created within theJdbi.withHandle(org.jdbi.v3.core.HandleCallback<R, X>),Jdbi.useHandle(org.jdbi.v3.core.HandleConsumer<X>),Jdbi.inTransaction(org.jdbi.v3.core.HandleCallback<R, X>)andJdbi.useTransaction(org.jdbi.v3.core.HandleConsumer<X>)callback methods will automatically attached to theHandleobject and therefore cleaned up when the callback ends. The default is true.- Parameters:
attachCallbackStatementsForCleanup- If true, statements created within the Jdbi callbacks are attached to the handle- Since:
- 3.38.0
-
cacheStats
@Beta public <T> T cacheStats()
Returns cache statistics for the internal template cache. This returns a cache specific object, so the user needs to know what caching library is in use.
-
customize
void customize(java.sql.Statement statement) throws java.sql.SQLException- Throws:
java.sql.SQLException
-
createCopy
public SqlStatements createCopy()
Description copied from interface:JdbiConfigReturns a copy of this configuration object. Changes to the copy should not modify the original, and vice-versa.- Specified by:
createCopyin interfaceJdbiConfig<SqlStatements>- Returns:
- a copy of this configuration object.
-
getCustomizers
java.util.Collection<StatementCustomizer> getCustomizers()
-
getContextListeners
java.util.Collection<StatementContextListener> getContextListeners()
-
preparedRender
java.lang.String preparedRender(java.lang.String template, StatementContext ctx)
-
cacheLoaderFunction
private static JdbiCacheLoader<SqlStatements.StatementCacheKey,java.util.function.Function<StatementContext,java.lang.String>> cacheLoaderFunction(StatementContext ctx)
-
-