Class Database
- Since:
- 1.0.0
- Author:
- Mark Allen
-
Nested Class Summary
Nested Classes -
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, orDELETEin "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, orDELETEin "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. with Postgres/Oracle'sRETURNINGclause.<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. with Postgres/Oracle'sRETURNINGclause.<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. with Postgres/Oracle'sRETURNINGclause.<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. with Postgres/Oracle'sRETURNINGclause.<T> Optional<T> participate(Transaction transaction, ReturningTransactionalOperation<T> transactionalOperation) Performs an operation in the context of a pre-existing transaction, optionall returning a value.voidparticipate(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.voidreadDatabaseMetaData(DatabaseMetaDataReader databaseMetaDataReader) Exposes a temporary handle to JDBCDatabaseMetaData, which provides comprehensive vendor-specific information about this database as a whole.<T> Optional<T> transaction(ReturningTransactionalOperation<T> transactionalOperation) Performs an operation transactionally and optionally returns a value.voidtransaction(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.voidtransaction(TransactionIsolation transactionIsolation, TransactionalOperation transactionalOperation) Performs an operation transactionally with the given isolation level.static Database.BuilderwithDataSource(DataSource dataSource) Provides aDatabasebuilder for the givenDataSource.
-
Method Details
-
withDataSource
Provides aDatabasebuilder 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
transactionalOperationcompletes.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
transactionalOperationcompletes.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 whichResultSetrows should be marshaledparameters-PreparedStatementparameters, 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 whichResultSetrows should be marshaledparameters-PreparedStatementparameters, 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 whichResultSetrows should be marshaledparameters-PreparedStatementparameters, 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 whichResultSetrows should be marshaledparameters-PreparedStatementparameters, 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-PreparedStatementparameters, 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-PreparedStatementparameters, 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'sRETURNINGclause.- Type Parameters:
T- the type to be returned- Parameters:
sql- the SQL query to executeresultSetRowType- the type to which theResultSetrow should be marshaledparameters-PreparedStatementparameters, 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'sRETURNINGclause.- Type Parameters:
T- the type to be returned- Parameters:
statement- the SQL statement to executeresultSetRowType- the type to whichResultSetrows should be marshaledparameters-PreparedStatementparameters, 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'sRETURNINGclause.- Type Parameters:
T- the type to be returned- Parameters:
sql- the SQL to executeresultSetRowType- the type to whichResultSetrows should be marshaledparameters-PreparedStatementparameters, 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'sRETURNINGclause.- Type Parameters:
T- the type to be returned- Parameters:
statement- the SQL statement to executeresultSetRowType- the type to whichResultSetrows should be marshaledparameters-PreparedStatementparameters, 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, orDELETEin "batch" over a set of parameter groups.Useful for bulk-inserting or updating large amounts of data.
- Parameters:
sql- the SQL to executeparameterGroups- Groups ofPreparedStatementparameters- 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, orDELETEin "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 ofPreparedStatementparameters- Returns:
- the number of rows affected by the SQL statement per-group
-
readDatabaseMetaData
Exposes a temporary handle to JDBCDatabaseMetaData, which provides comprehensive vendor-specific information about this database as a whole.This method acquires
DatabaseMetaDataon its own newly-borrowed connection, which it manages internally.It does not participate in the active transaction, if one exists.
The connection is closed as soon as
DatabaseMetaDataReader.read(DatabaseMetaData)completes.See
DatabaseMetaDataJavadoc for details. -
getDatabaseType
- Since:
- 3.0.0
-
getTimeZone
- Since:
- 3.0.0
-