Class Database
- Since:
- 1.0.0
- Author:
- Mark Allen
-
Nested Class Summary
-
Method Summary
Modifier and TypeMethodDescriptionGets a reference to the current transaction, if any.Executes a SQL Data Manipulation Language (DML) statement, such asINSERT
,UPDATE
, orDELETE
; or a SQL statement that returns nothing, such as a DDL statement.Executes a SQL Data Manipulation Language (DML) statement, such asINSERT
,UPDATE
, orDELETE
; or a SQL statement that returns nothing, such as a DDL statement.executeBatch
(Statement statement, List<List<Object>> parameterGroups) Executes a SQL Data Manipulation Language (DML) statement, such asINSERT
,UPDATE
, orDELETE
in "batch" over a set of parameter groups.executeBatch
(String sql, List<List<Object>> parameterGroups) Executes a SQL Data Manipulation Language (DML) statement, such asINSERT
,UPDATE
, orDELETE
in "batch" over a set of parameter groups.<T> List
<T> executeForList
(Statement statement, Class<T> resultSetRowType, Object... parameters) Executes a SQL Data Manipulation Language (DML) statement, such asINSERT
,UPDATE
, orDELETE
, which returns any number of rows, e.g.<T> List
<T> executeForList
(String sql, Class<T> resultSetRowType, Object... parameters) Executes a SQL Data Manipulation Language (DML) statement, such asINSERT
,UPDATE
, orDELETE
, which returns any number of rows, e.g.<T> Optional
<T> executeForObject
(Statement statement, Class<T> resultSetRowType, Object... parameters) Executes a SQL Data Manipulation Language (DML) statement, such asINSERT
,UPDATE
, orDELETE
, which returns 0 or 1 rows, e.g.<T> Optional
<T> executeForObject
(String sql, Class<T> resultSetRowType, Object... parameters) Executes a SQL Data Manipulation Language (DML) statement, such asINSERT
,UPDATE
, orDELETE
, which returns 0 or 1 rows, e.g.static Database.Builder
forDataSource
(DataSource dataSource) Provides aDatabase
builder for the givenDataSource
.<T> Optional
<T> participate
(Transaction transaction, ReturningTransactionalOperation<T> transactionalOperation) Performs an operation in the context of a pre-existing transaction, optionall returning a value.void
participate
(Transaction transaction, TransactionalOperation transactionalOperation) Performs an operation in the context of a pre-existing transaction.<T> List
<T> queryForList
(Statement statement, Class<T> resultSetRowType, Object... parameters) Performs a SQL query that is expected to return any number of result rows.<T> List
<T> queryForList
(String sql, Class<T> resultSetRowType, Object... parameters) Performs a SQL query that is expected to return any number of result rows.<T> Optional
<T> queryForObject
(Statement statement, Class<T> resultSetRowType, Object... parameters) Performs a SQL query that is expected to return 0 or 1 result rows.<T> Optional
<T> queryForObject
(String sql, Class<T> resultSetRowType, Object... parameters) Performs a SQL query that is expected to return 0 or 1 result rows.<T> Optional
<T> transaction
(ReturningTransactionalOperation<T> transactionalOperation) Performs an operation transactionally and optionally returns a value.void
transaction
(TransactionalOperation transactionalOperation) Performs an operation transactionally.<T> Optional
<T> transaction
(TransactionIsolation transactionIsolation, ReturningTransactionalOperation<T> transactionalOperation) Performs an operation transactionally with the given isolation level, optionally returning a value.void
transaction
(TransactionIsolation transactionIsolation, TransactionalOperation transactionalOperation) Performs an operation transactionally with the given isolation level.
-
Method Details
-
forDataSource
Provides aDatabase
builder for the givenDataSource
. -
currentTransaction
Gets a reference to the current transaction, if any.- Returns:
- the current transaction
-
transaction
Performs an operation transactionally.The transaction will be automatically rolled back if an exception bubbles out of
transactionalOperation
.- Parameters:
transactionalOperation
- the operation to perform transactionally
-
transaction
public void transaction(@Nonnull TransactionIsolation transactionIsolation, @Nonnull TransactionalOperation transactionalOperation) Performs an operation transactionally with the given isolation level.The transaction will be automatically rolled back if an exception bubbles out of
transactionalOperation
.- Parameters:
transactionIsolation
- the desired database transaction isolation leveltransactionalOperation
- the operation to perform transactionally
-
transaction
@Nonnull public <T> Optional<T> transaction(@Nonnull ReturningTransactionalOperation<T> transactionalOperation) Performs an operation transactionally and optionally returns a value.The transaction will be automatically rolled back if an exception bubbles out of
transactionalOperation
.- Type Parameters:
T
- the type to be returned- Parameters:
transactionalOperation
- the operation to perform transactionally- Returns:
- the result of the transactional operation
-
transaction
@Nonnull public <T> Optional<T> transaction(@Nonnull TransactionIsolation transactionIsolation, @Nonnull ReturningTransactionalOperation<T> transactionalOperation) Performs an operation transactionally with the given isolation level, optionally returning a value.The transaction will be automatically rolled back if an exception bubbles out of
transactionalOperation
.- Type Parameters:
T
- the type to be returned- Parameters:
transactionIsolation
- the desired database transaction isolation leveltransactionalOperation
- the operation to perform transactionally- Returns:
- the result of the transactional operation
-
participate
public void participate(@Nonnull Transaction transaction, @Nonnull TransactionalOperation transactionalOperation) Performs an operation in the context of a pre-existing transaction.No commit or rollback on the transaction will occur when
transactionalOperation
completes.However, if an exception bubbles out of
transactionalOperation
, the transaction will be marked as rollback-only.- Parameters:
transaction
- the transaction in which to participatetransactionalOperation
- the operation that should participate in the transaction
-
participate
@Nonnull public <T> Optional<T> participate(@Nonnull Transaction transaction, @Nonnull ReturningTransactionalOperation<T> transactionalOperation) Performs an operation in the context of a pre-existing transaction, optionall returning a value.No commit or rollback on the transaction will occur when
transactionalOperation
completes.However, if an exception bubbles out of
transactionalOperation
, the transaction will be marked as rollback-only.- Type Parameters:
T
- the type to be returned- Parameters:
transaction
- the transaction in which to participatetransactionalOperation
- the operation that should participate in the transaction- Returns:
- the result of the transactional operation
-
queryForObject
@Nonnull public <T> Optional<T> queryForObject(@Nonnull String sql, @Nonnull Class<T> resultSetRowType, @Nullable Object... parameters) Performs a SQL query that is expected to return 0 or 1 result rows.- Type Parameters:
T
- the type to be returned- Parameters:
sql
- the SQL query to executeresultSetRowType
- the type to whichResultSet
rows should be marshaledparameters
-PreparedStatement
parameters, if any- Returns:
- a single result (or no result)
- Throws:
DatabaseException
- if > 1 row is returned
-
queryForObject
public <T> Optional<T> queryForObject(@Nonnull Statement statement, @Nonnull Class<T> resultSetRowType, @Nullable Object... parameters) Performs a SQL query that is expected to return 0 or 1 result rows.- Type Parameters:
T
- the type to be returned- Parameters:
statement
- the SQL statement to executeresultSetRowType
- the type to whichResultSet
rows should be marshaledparameters
-PreparedStatement
parameters, if any- Returns:
- a single result (or no result)
- Throws:
DatabaseException
- if > 1 row is returned
-
queryForList
@Nonnull public <T> List<T> queryForList(@Nonnull String sql, @Nonnull Class<T> resultSetRowType, @Nullable Object... parameters) Performs a SQL query that is expected to return any number of result rows.- Type Parameters:
T
- the type to be returned- Parameters:
sql
- the SQL query to executeresultSetRowType
- the type to whichResultSet
rows should be marshaledparameters
-PreparedStatement
parameters, if any- Returns:
- a list of results
-
queryForList
@Nonnull public <T> List<T> queryForList(@Nonnull Statement statement, @Nonnull Class<T> resultSetRowType, @Nullable Object... parameters) Performs a SQL query that is expected to return any number of result rows.- Type Parameters:
T
- the type to be returned- Parameters:
statement
- the SQL statement to executeresultSetRowType
- the type to whichResultSet
rows should be marshaledparameters
-PreparedStatement
parameters, if any- Returns:
- a list of results
-
execute
Executes a SQL Data Manipulation Language (DML) statement, such asINSERT
,UPDATE
, orDELETE
; or a SQL statement that returns nothing, such as a DDL statement.- Parameters:
sql
- the SQL to executeparameters
-PreparedStatement
parameters, if any- Returns:
- the number of rows affected by the SQL statement
-
execute
Executes a SQL Data Manipulation Language (DML) statement, such asINSERT
,UPDATE
, orDELETE
; or a SQL statement that returns nothing, such as a DDL statement.- Parameters:
statement
- the SQL statement to executeparameters
-PreparedStatement
parameters, if any- Returns:
- the number of rows affected by the SQL statement
-
executeForObject
@Nonnull public <T> Optional<T> executeForObject(@Nonnull String sql, @Nonnull Class<T> resultSetRowType, @Nullable Object... parameters) Executes a SQL Data Manipulation Language (DML) statement, such asINSERT
,UPDATE
, orDELETE
, which returns 0 or 1 rows, e.g. with Postgres/Oracle'sRETURNING
clause.- Type Parameters:
T
- the type to be returned- Parameters:
sql
- the SQL query to executeresultSetRowType
- the type to which theResultSet
row should be marshaledparameters
-PreparedStatement
parameters, if any- Returns:
- a single result (or no result)
- Throws:
DatabaseException
- if > 1 row is returned
-
executeForObject
public <T> Optional<T> executeForObject(@Nonnull Statement statement, @Nonnull Class<T> resultSetRowType, @Nullable Object... parameters) Executes a SQL Data Manipulation Language (DML) statement, such asINSERT
,UPDATE
, orDELETE
, which returns 0 or 1 rows, e.g. with Postgres/Oracle'sRETURNING
clause.- Type Parameters:
T
- the type to be returned- Parameters:
statement
- the SQL statement to executeresultSetRowType
- the type to whichResultSet
rows should be marshaledparameters
-PreparedStatement
parameters, if any- Returns:
- a single result (or no result)
- Throws:
DatabaseException
- if > 1 row is returned
-
executeForList
@Nonnull public <T> List<T> executeForList(@Nonnull String sql, @Nonnull Class<T> resultSetRowType, @Nullable Object... parameters) Executes a SQL Data Manipulation Language (DML) statement, such asINSERT
,UPDATE
, orDELETE
, which returns any number of rows, e.g. with Postgres/Oracle'sRETURNING
clause.- Type Parameters:
T
- the type to be returned- Parameters:
sql
- the SQL to executeresultSetRowType
- the type to whichResultSet
rows should be marshaledparameters
-PreparedStatement
parameters, if any- Returns:
- a list of results
-
executeForList
@Nonnull public <T> List<T> executeForList(@Nonnull Statement statement, @Nonnull Class<T> resultSetRowType, @Nullable Object... parameters) Executes a SQL Data Manipulation Language (DML) statement, such asINSERT
,UPDATE
, orDELETE
, which returns any number of rows, e.g. with Postgres/Oracle'sRETURNING
clause.- Type Parameters:
T
- the type to be returned- Parameters:
statement
- the SQL statement to executeresultSetRowType
- the type to whichResultSet
rows should be marshaledparameters
-PreparedStatement
parameters, if any- Returns:
- a list of results
-
executeBatch
@Nonnull public List<Long> executeBatch(@Nonnull String sql, @Nonnull List<List<Object>> parameterGroups) Executes a SQL Data Manipulation Language (DML) statement, such asINSERT
,UPDATE
, orDELETE
in "batch" over a set of parameter groups.Useful for bulk-inserting or updating large amounts of data.
- Parameters:
sql
- the SQL to executeparameterGroups
- Groups ofPreparedStatement
parameters- Returns:
- the number of rows affected by the SQL statement per-group
-
executeBatch
@Nonnull public List<Long> executeBatch(@Nonnull Statement statement, @Nonnull List<List<Object>> parameterGroups) Executes a SQL Data Manipulation Language (DML) statement, such asINSERT
,UPDATE
, orDELETE
in "batch" over a set of parameter groups.Useful for bulk-inserting or updating large amounts of data.
- Parameters:
statement
- the SQL statement to executeparameterGroups
- Groups ofPreparedStatement
parameters- Returns:
- the number of rows affected by the SQL statement per-group
-