Package org.dalesbred
Class Database
java.lang.Object
org.dalesbred.Database
The main abstraction of the library: represents a configured connection to database and provides a way to execute callbacks in transactions.
Usually you'll need only single instance of this in your application, unless you need to connect several different databases or need different default settings for different use cases.
-
Constructor Summary
ConstructorDescriptionDatabase
(@NotNull DataSource dataSource) Constructs a new Database that uses givenDataSource
and auto-detects the dialect to use.Database
(@NotNull DataSource dataSource, @NotNull Dialect dialect) Constructs a new Database that uses givenDataSource
andDialect
.Database
(@NotNull ConnectionProvider connectionProvider) Constructs a new Database that uses givenConnectionProvider
and auto-detects the dialect to use.Database
(@NotNull ConnectionProvider connectionProvider, @NotNull Dialect dialect) Constructs a new Database that uses givenConnectionProvider
andDialect
.Database
(@NotNull TransactionManager transactionManager) Constructs a new Database that uses givenTransactionManager
and auto-detects the dialect to use.Database
(@NotNull TransactionManager transactionManager, @NotNull Dialect dialect) Constructs a new Database that uses givenTransactionManager
andDialect
. -
Method Summary
Modifier and TypeMethodDescription<T> T
executeQuery
(@NotNull ResultSetProcessor<T> processor, @NotNull String sql, Object... args) Executes a query and processes the results with givenResultSetProcessor
.<T> T
executeQuery
(@NotNull ResultSetProcessor<T> processor, @NotNull SqlQuery query) Executes a query and processes the results with givenResultSetProcessor
.<T> @NotNull List<T>
Executes a query and converts the results to instances of given class using default mechanisms.<T> @NotNull List<T>
Executes a query and converts the results to instances of given class using default mechanisms.<T> @NotNull List<T>
Executes a query and processes each row of the result with givenRowMapper
to produce a list of results.<T> @NotNull List<T>
Executes a query and processes each row of the result with givenRowMapper
to produce a list of results.<K,
V> @NotNull Map<K, V> findMap
(@NotNull Class<K> keyType, @NotNull Class<V> valueType, @NotNull String sql, Object... args) Executes a query that returns at least two values and creates a map from the results, using the first value as the key and rest of the values for instantiatingV
.<K,
V> @NotNull Map<K, V> Executes a query that returns at least two values and creates a map from the results, using the first value as the key and rest of the values for instantiatingV
.<T> @NotNull Optional<T>
findOptional
(@NotNull Class<T> cl, @NotNull String sql, Object... args) Finds a unique result from database, converting the database row to given class using default mechanisms.<T> @NotNull Optional<T>
findOptional
(@NotNull Class<T> cl, @NotNull SqlQuery query) Finds a unique result from database, converting the database row to given class using default mechanisms.<T> @NotNull Optional<T>
findOptional
(@NotNull RowMapper<T> rowMapper, @NotNull String sql, Object... args) Find a unique result from database, using givenRowMapper
to convert row.<T> @NotNull Optional<T>
findOptional
(@NotNull RowMapper<T> rowMapper, @NotNull SqlQuery query) Find a unique result from database, using givenRowMapper
to convert row.@NotNull OptionalDouble
findOptionalDouble
(@NotNull String sql, Object... args) Finds a unique result from database, converting the database row to double using default mechanisms.@NotNull OptionalDouble
findOptionalDouble
(@NotNull SqlQuery query) Finds a unique result from database, converting the database row to double using default mechanisms.@NotNull OptionalInt
findOptionalInt
(@NotNull String sql, Object... args) Finds a unique result from database, converting the database row to int using default mechanisms.@NotNull OptionalInt
findOptionalInt
(@NotNull SqlQuery query) Finds a unique result from database, converting the database row to int using default mechanisms.@NotNull OptionalLong
findOptionalLong
(@NotNull String sql, Object... args) Finds a unique result from database, converting the database row to long using default mechanisms.@NotNull OptionalLong
findOptionalLong
(@NotNull SqlQuery query) Finds a unique result from database, converting the database row to long using default mechanisms.@NotNull ResultTable
Executes a query and creates aResultTable
from the results.@NotNull ResultTable
Executes a query and creates aResultTable
from the results.<T> T
findUnique
(@NotNull Class<T> cl, @NotNull String sql, Object... args) Finds a unique result from database, converting the database row to given class using default mechanisms.<T> T
findUnique
(@NotNull Class<T> cl, @NotNull SqlQuery query) Finds a unique result from database, converting the database row to given class using default mechanisms.<T> T
findUnique
(@NotNull RowMapper<T> mapper, @NotNull String sql, Object... args) Finds a unique result from database, using givenRowMapper
to convert the row.<T> T
findUnique
(@NotNull RowMapper<T> mapper, @NotNull SqlQuery query) Finds a unique result from database, using givenRowMapper
to convert the row.boolean
findUniqueBoolean
(@NotNull String sql, Object... args) A convenience method for retrieving a single non-null boolean.boolean
findUniqueBoolean
(@NotNull SqlQuery query) A convenience method for retrieving a single non-null boolean.int
findUniqueInt
(@NotNull String sql, Object... args) A convenience method for retrieving a single non-null integer.int
findUniqueInt
(@NotNull SqlQuery query) A convenience method for retrieving a single non-null integer.long
findUniqueLong
(@NotNull String sql, Object... args) A convenience method for retrieving a single non-null long.long
findUniqueLong
(@NotNull SqlQuery query) A convenience method for retrieving a single non-null long.<T> T
findUniqueOrNull
(@NotNull Class<T> cl, @NotNull String sql, Object... args) Alias forfindOptional(cl, sql, args).orElse(null)
.<T> T
findUniqueOrNull
(@NotNull Class<T> cl, @NotNull SqlQuery query) Alias forfindOptional(cl, query).orElse(null)
.<T> T
findUniqueOrNull
(@NotNull RowMapper<T> rowMapper, @NotNull String sql, Object... args) Alias for {findUniqueOrNull(rowMapper, SqlQuery.query(sql, args))}.<T> T
findUniqueOrNull
(@NotNull RowMapper<T> rowMapper, @NotNull SqlQuery query) Alias forfindOptional(rowMapper, query).orElse(null)
.static @NotNull Database
forDataSource
(@NotNull DataSource dataSource) Returns a new Database that uses givenDataSource
to retrieve connections.static @NotNull Database
forJndiDataSource
(@NotNull String jndiName) Returns a new Database that usesDataSource
with given JNDI-name.static @NotNull Database
forUrlAndCredentials
(@NotNull String url, @Nullable String username, @Nullable String password) Returns a new Database that uses given connection options to open connection.@Nullable Duration
If default timeout is set to non null (by default it's null) all queries will have this timeout value as default, unless is specified directly onSqlQuery
or is set directly on JDBC Connection parameters.@NotNull TypeConversionRegistry
ReturnsTypeConversionRegistry
that can be used to register new type-conversions.boolean
Returns true if and only if the current thread has an active transaction for this database.boolean
If flag is set to true (by default it's false) queries without active transaction will not throw exception but will start a fresh transaction.void
setAllowImplicitTransactions
(boolean allowImplicitTransactions) If flag is set to true (by default it's false) queries without active transaction will not throw exception but will start a fresh transaction.void
setDefaultTimeout
(@NotNull Duration timeout) If default timeout is set to non null (by default it's null) all queries will have this timeout value as default, unless is specified directly onSqlQuery
or is set directly on JDBC Connection parameters.@NotNull String
toString()
Returns a string containing useful debug information about the state of this object.int
Executes an update against the database and returns the amount of affected rows.int
Executes an update against the database and returns the amount of affected rows.<T> T
updateAndProcessGeneratedKeys
(@NotNull ResultSetProcessor<T> generatedKeysProcessor, @NotNull List<String> columnNames, @NotNull String sql, Object... args) <T> T
updateAndProcessGeneratedKeys
(@NotNull ResultSetProcessor<T> generatedKeysProcessor, @NotNull List<String> columnNames, @NotNull SqlQuery query) Executes an update against the database and return generated keys as extracted by generatedKeysProcessor.int[]
updateBatch
(@NotNull String sql, @NotNull List<? extends List<?>> argumentLists) Executes a batch update against the database, returning an array of modification counts for each argument list.<T> T
updateBatchAndProcessGeneratedKeys
(@NotNull ResultSetProcessor<T> generatedKeysProcessor, @NotNull List<String> columnNames, @NotNull String sql, @NotNull List<? extends List<?>> argumentLists) Executes batch of updates against the database and return generated keys as extracted by generatedKeysProcessor.void
updateUnique
(@NotNull String sql, Object... args) Execute an update against the database and assert that a single row will be modified.void
updateUnique
(@NotNull SqlQuery query) Execute an update against the database and assert that a single row will be modified.<T> T
withTransaction
(@NotNull Isolation isolation, @NotNull TransactionCallback<T> callback) Executes a block of code with given isolation.<T> T
withTransaction
(@NotNull Propagation propagation, @NotNull Isolation isolation, @NotNull TransactionCallback<T> callback) Executes a block of code with given propagation and isolation.<T> T
withTransaction
(@NotNull Propagation propagation, @NotNull TransactionCallback<T> callback) Executes a block of code with given propagation and configuration default isolation.<T> T
withTransaction
(@NotNull TransactionCallback<T> callback) Executes a block of code within a context of a transaction, usingPropagation.REQUIRED
propagation.<T> T
withTransaction
(@NotNull TransactionSettings settings, @NotNull TransactionCallback<T> callback) Executes a block of code with given transaction settings.void
withVoidTransaction
(@NotNull Isolation isolation, @NotNull VoidTransactionCallback callback) Executes a block of code with given isolation.void
withVoidTransaction
(@NotNull Propagation propagation, @NotNull Isolation isolation, @NotNull VoidTransactionCallback callback) Executes a block of code with given propagation and isolation.void
withVoidTransaction
(@NotNull Propagation propagation, @NotNull VoidTransactionCallback callback) Executes a block of code with given propagation and configuration default isolation.void
withVoidTransaction
(@NotNull TransactionSettings settings, @NotNull VoidTransactionCallback callback) Executes a block of code with given transaction settings.void
withVoidTransaction
(@NotNull VoidTransactionCallback callback) Executes a block of code within a context of a transaction, usingPropagation.REQUIRED
propagation.
-
Constructor Details
-
Database
Constructs a new Database that uses givenConnectionProvider
and auto-detects the dialect to use. -
Database
public Database(@NotNull @NotNull ConnectionProvider connectionProvider, @NotNull @NotNull Dialect dialect) Constructs a new Database that uses givenConnectionProvider
andDialect
. -
Database
Constructs a new Database that uses givenTransactionManager
and auto-detects the dialect to use. -
Database
public Database(@NotNull @NotNull TransactionManager transactionManager, @NotNull @NotNull Dialect dialect) Constructs a new Database that uses givenTransactionManager
andDialect
. -
Database
Constructs a new Database that uses givenDataSource
and auto-detects the dialect to use. -
Database
Constructs a new Database that uses givenDataSource
andDialect
.
-
-
Method Details
-
forDataSource
Returns a new Database that uses givenDataSource
to retrieve connections. -
forJndiDataSource
Returns a new Database that usesDataSource
with given JNDI-name. -
forUrlAndCredentials
@NotNull public static @NotNull Database forUrlAndCredentials(@NotNull @NotNull String url, @Nullable @Nullable String username, @Nullable @Nullable String password) Returns a new Database that uses given connection options to open connection. The database opens connections directly fromDriverManager
without performing connection pooling.- See Also:
-
withTransaction
Executes a block of code within a context of a transaction, usingPropagation.REQUIRED
propagation. -
withTransaction
public <T> T withTransaction(@NotNull @NotNull Propagation propagation, @NotNull @NotNull TransactionCallback<T> callback) Executes a block of code with given propagation and configuration default isolation. -
withTransaction
public <T> T withTransaction(@NotNull @NotNull Isolation isolation, @NotNull @NotNull TransactionCallback<T> callback) Executes a block of code with given isolation. -
withTransaction
public <T> T withTransaction(@NotNull @NotNull Propagation propagation, @NotNull @NotNull Isolation isolation, @NotNull @NotNull TransactionCallback<T> callback) Executes a block of code with given propagation and isolation. -
withTransaction
public <T> T withTransaction(@NotNull @NotNull TransactionSettings settings, @NotNull @NotNull TransactionCallback<T> callback) Executes a block of code with given transaction settings.- See Also:
-
withVoidTransaction
Executes a block of code within a context of a transaction, usingPropagation.REQUIRED
propagation. -
withVoidTransaction
public void withVoidTransaction(@NotNull @NotNull Propagation propagation, @NotNull @NotNull VoidTransactionCallback callback) Executes a block of code with given propagation and configuration default isolation. -
withVoidTransaction
public void withVoidTransaction(@NotNull @NotNull Isolation isolation, @NotNull @NotNull VoidTransactionCallback callback) Executes a block of code with given isolation. -
withVoidTransaction
public void withVoidTransaction(@NotNull @NotNull Propagation propagation, @NotNull @NotNull Isolation isolation, @NotNull @NotNull VoidTransactionCallback callback) Executes a block of code with given propagation and isolation. -
withVoidTransaction
public void withVoidTransaction(@NotNull @NotNull TransactionSettings settings, @NotNull @NotNull VoidTransactionCallback callback) Executes a block of code with given transaction settings.- See Also:
-
hasActiveTransaction
public boolean hasActiveTransaction()Returns true if and only if the current thread has an active transaction for this database. -
executeQuery
public <T> T executeQuery(@NotNull @NotNull ResultSetProcessor<T> processor, @NotNull @NotNull SqlQuery query) Executes a query and processes the results with givenResultSetProcessor
. All other findXXX-methods are just convenience methods for this one. -
executeQuery
public <T> T executeQuery(@NotNull @NotNull ResultSetProcessor<T> processor, @NotNull @NotNull String sql, Object... args) Executes a query and processes the results with givenResultSetProcessor
. -
findAll
@NotNull public <T> @NotNull List<T> findAll(@NotNull @NotNull RowMapper<T> rowMapper, @NotNull @NotNull SqlQuery query) Executes a query and processes each row of the result with givenRowMapper
to produce a list of results. -
findAll
@NotNull public <T> @NotNull List<T> findAll(@NotNull @NotNull RowMapper<T> rowMapper, @NotNull @NotNull String sql, Object... args) Executes a query and processes each row of the result with givenRowMapper
to produce a list of results. -
findAll
@NotNull public <T> @NotNull List<T> findAll(@NotNull @NotNull Class<T> cl, @NotNull @NotNull SqlQuery query) Executes a query and converts the results to instances of given class using default mechanisms. -
findAll
@NotNull public <T> @NotNull List<T> findAll(@NotNull @NotNull Class<T> cl, @NotNull @NotNull String sql, Object... args) Executes a query and converts the results to instances of given class using default mechanisms. -
findUnique
Finds a unique result from database, using givenRowMapper
to convert the row.- Throws:
NonUniqueResultException
- if there is more then one rowEmptyResultException
- if there are no rows
-
findUnique
public <T> T findUnique(@NotNull @NotNull RowMapper<T> mapper, @NotNull @NotNull String sql, Object... args) Finds a unique result from database, using givenRowMapper
to convert the row.- Throws:
NonUniqueResultException
- if there is more then one rowEmptyResultException
- if there are no rows
-
findUnique
Finds a unique result from database, converting the database row to given class using default mechanisms.- Throws:
NonUniqueResultException
- if there is more then one rowEmptyResultException
- if there are no rows
-
findUnique
public <T> T findUnique(@NotNull @NotNull Class<T> cl, @NotNull @NotNull String sql, Object... args) Finds a unique result from database, converting the database row to given class using default mechanisms.- Throws:
NonUniqueResultException
- if there is more then one rowEmptyResultException
- if there are no rows
-
findOptional
@NotNull public <T> @NotNull Optional<T> findOptional(@NotNull @NotNull RowMapper<T> rowMapper, @NotNull @NotNull SqlQuery query) Find a unique result from database, using givenRowMapper
to convert row. Returns empty if there are no results or if single null result is returned.- Throws:
NonUniqueResultException
- if there are multiple result rows
-
findOptional
@NotNull public <T> @NotNull Optional<T> findOptional(@NotNull @NotNull RowMapper<T> rowMapper, @NotNull @NotNull String sql, Object... args) Find a unique result from database, using givenRowMapper
to convert row. Returns empty if there are no results or if single null result is returned.- Throws:
NonUniqueResultException
- if there are multiple result rows
-
findOptional
@NotNull public <T> @NotNull Optional<T> findOptional(@NotNull @NotNull Class<T> cl, @NotNull @NotNull SqlQuery query) Finds a unique result from database, converting the database row to given class using default mechanisms. Returns empty if there are no results or if single null result is returned.- Throws:
NonUniqueResultException
- if there are multiple result rows
-
findOptional
@NotNull public <T> @NotNull Optional<T> findOptional(@NotNull @NotNull Class<T> cl, @NotNull @NotNull String sql, Object... args) Finds a unique result from database, converting the database row to given class using default mechanisms. Returns empty if there are no results or if single null result is returned.- Throws:
NonUniqueResultException
- if there are multiple result rows
-
findOptionalInt
Finds a unique result from database, converting the database row to int using default mechanisms. Returns empty if there are no results or if single null result is returned.- Throws:
NonUniqueResultException
- if there are multiple result rows
-
findOptionalInt
Finds a unique result from database, converting the database row to int using default mechanisms. Returns empty if there are no results or if single null result is returned.- Throws:
NonUniqueResultException
- if there are multiple result rows
-
findOptionalLong
@NotNull public @NotNull OptionalLong findOptionalLong(@NotNull @NotNull String sql, Object... args) Finds a unique result from database, converting the database row to long using default mechanisms. Returns empty if there are no results or if single null result is returned.- Throws:
NonUniqueResultException
- if there are multiple result rows
-
findOptionalLong
Finds a unique result from database, converting the database row to long using default mechanisms. Returns empty if there are no results or if single null result is returned.- Throws:
NonUniqueResultException
- if there are multiple result rows
-
findOptionalDouble
@NotNull public @NotNull OptionalDouble findOptionalDouble(@NotNull @NotNull String sql, Object... args) Finds a unique result from database, converting the database row to double using default mechanisms. Returns empty if there are no results or if single null result is returned.- Throws:
NonUniqueResultException
- if there are multiple result rows
-
findOptionalDouble
Finds a unique result from database, converting the database row to double using default mechanisms. Returns empty if there are no results or if single null result is returned.- Throws:
NonUniqueResultException
- if there are multiple result rows
-
findUniqueOrNull
@Nullable public <T> T findUniqueOrNull(@NotNull @NotNull RowMapper<T> rowMapper, @NotNull @NotNull SqlQuery query) Alias forfindOptional(rowMapper, query).orElse(null)
. -
findUniqueOrNull
@Nullable public <T> T findUniqueOrNull(@NotNull @NotNull RowMapper<T> rowMapper, @NotNull @NotNull String sql, Object... args) Alias for {findUniqueOrNull(rowMapper, SqlQuery.query(sql, args))}. -
findUniqueOrNull
@Nullable public <T> T findUniqueOrNull(@NotNull @NotNull Class<T> cl, @NotNull @NotNull SqlQuery query) Alias forfindOptional(cl, query).orElse(null)
. -
findUniqueOrNull
@Nullable public <T> T findUniqueOrNull(@NotNull @NotNull Class<T> cl, @NotNull @NotNull String sql, Object... args) Alias forfindOptional(cl, sql, args).orElse(null)
. -
findUniqueBoolean
A convenience method for retrieving a single non-null boolean.- Throws:
NonUniqueResultException
- if there is more then one rowEmptyResultException
- if there are no rows
-
findUniqueBoolean
A convenience method for retrieving a single non-null boolean.- Throws:
NonUniqueResultException
- if there is more then one rowEmptyResultException
- if there are no rows
-
findUniqueInt
A convenience method for retrieving a single non-null integer.- Throws:
NonUniqueResultException
- if there is more then one rowEmptyResultException
- if there are no rows
-
findUniqueInt
A convenience method for retrieving a single non-null integer.- Throws:
NonUniqueResultException
- if there is more then one rowEmptyResultException
- if there are no rows
-
findUniqueLong
A convenience method for retrieving a single non-null long.- Throws:
NonUniqueResultException
- if there is more then one rowEmptyResultException
- if there are no rows
-
findUniqueLong
A convenience method for retrieving a single non-null long.- Throws:
NonUniqueResultException
- if there is more then one rowEmptyResultException
- if there are no rows
-
findMap
@NotNull public <K,V> @NotNull Map<K,V> findMap(@NotNull @NotNull Class<K> keyType, @NotNull @NotNull Class<V> valueType, @NotNull @NotNull SqlQuery query) Executes a query that returns at least two values and creates a map from the results, using the first value as the key and rest of the values for instantiatingV
.If the keys of the result are not distinct, the result contains the last binding of given key.
-
findMap
@NotNull public <K,V> @NotNull Map<K,V> findMap(@NotNull @NotNull Class<K> keyType, @NotNull @NotNull Class<V> valueType, @NotNull @NotNull String sql, Object... args) Executes a query that returns at least two values and creates a map from the results, using the first value as the key and rest of the values for instantiatingV
.If the keys of the result are not distinct, the result contains the last binding of given key.
-
findTable
Executes a query and creates aResultTable
from the results. -
findTable
Executes a query and creates aResultTable
from the results. -
update
Executes an update against the database and returns the amount of affected rows. -
update
Executes an update against the database and returns the amount of affected rows. -
updateUnique
Execute an update against the database and assert that a single row will be modified.- Throws:
NonUniqueUpdateException
- if zero or more then one rows were updated
-
updateUnique
Execute an update against the database and assert that a single row will be modified.- Throws:
NonUniqueUpdateException
- if zero or more then one rows were updated
-
updateAndProcessGeneratedKeys
public <T> T updateAndProcessGeneratedKeys(@NotNull @NotNull ResultSetProcessor<T> generatedKeysProcessor, @NotNull @NotNull List<String> columnNames, @NotNull @NotNull SqlQuery query) Executes an update against the database and return generated keys as extracted by generatedKeysProcessor.- Parameters:
generatedKeysProcessor
- processor for handling the generated keyscolumnNames
- names of columns that contain the generated keys. Can be empty, in which case the returned columns depend on the databasequery
- to execute- Returns:
- Result of processing the results with
generatedKeysProcessor
.
-
updateAndProcessGeneratedKeys
public <T> T updateAndProcessGeneratedKeys(@NotNull @NotNull ResultSetProcessor<T> generatedKeysProcessor, @NotNull @NotNull List<String> columnNames, @NotNull @NotNull String sql, Object... args) -
updateBatch
public int[] updateBatch(@NotNull @NotNull String sql, @NotNull @NotNull List<? extends List<?>> argumentLists) Executes a batch update against the database, returning an array of modification counts for each argument list. -
updateBatchAndProcessGeneratedKeys
public <T> T updateBatchAndProcessGeneratedKeys(@NotNull @NotNull ResultSetProcessor<T> generatedKeysProcessor, @NotNull @NotNull List<String> columnNames, @NotNull @NotNull String sql, @NotNull @NotNull List<? extends List<?>> argumentLists) Executes batch of updates against the database and return generated keys as extracted by generatedKeysProcessor.- Parameters:
generatedKeysProcessor
- processor for handling the generated keyscolumnNames
- names of columns that contain the generated keys. Can be empty, in which case the returned columns depend on the databasesql
- to executeargumentLists
- List of argument lists for items of batch- Returns:
- Result of processing the results with
generatedKeysProcessor
.
-
getTypeConversionRegistry
ReturnsTypeConversionRegistry
that can be used to register new type-conversions. -
isAllowImplicitTransactions
public boolean isAllowImplicitTransactions()If flag is set to true (by default it's false) queries without active transaction will not throw exception but will start a fresh transaction. -
setAllowImplicitTransactions
public void setAllowImplicitTransactions(boolean allowImplicitTransactions) If flag is set to true (by default it's false) queries without active transaction will not throw exception but will start a fresh transaction. -
getDefaultTimeout
If default timeout is set to non null (by default it's null) all queries will have this timeout value as default, unless is specified directly onSqlQuery
or is set directly on JDBC Connection parameters. -
setDefaultTimeout
If default timeout is set to non null (by default it's null) all queries will have this timeout value as default, unless is specified directly onSqlQuery
or is set directly on JDBC Connection parameters.- Throws:
IllegalArgumentException
- if timeout is negative- See Also:
-
toString
Returns a string containing useful debug information about the state of this object.
-