public class Database extends DatabaseAccessor implements DataOperations, DataSourceSupplier
An instance of this class is available by passing a valid DataSource
.
This class:
ResultSet
sMethods of this class use various callback interfaces. A reading of those is greatly suggested.
Since the callback interfaces make Database
's methods parameterizable, there should be no need to subclass this class.
dataSource, logger
Constructor and Description |
---|
Database(@NotNull DataSource dataSource)
Constructs a new Database object with the given
DataSource . |
Modifier and Type | Method and Description |
---|---|
void |
batchUpdate(@NotNull String sql,
@NotNull BatchSetter batchSetter)
Performs multiple update operations using a single SQL statement.
|
void |
batchUpdate(@NotNull String sql,
@Nullable List<Object[]> batchArgs,
int... sqlTypes)
Performs multiple update operations using a single SQL statement.
|
<T> void |
batchUpdate(@NotNull String sql,
@Nullable List<T> batchArgs,
@NotNull ParametrizedBatchSetter<T> paramsBatchSetter)
Performs multiple update operations using a single SQL statement.
|
<T> T |
execute(@NotNull PreparedStatementCreator creator,
@NotNull PreparedStatementFunction<T> callback)
Executes a JDBC data access operation, implemented as
PreparedStatementFunction callback
working on a PreparedStatement. |
<T> T |
execute(@NotNull StatementFunction<T> callback)
Executes a JDBC data access operation, implemented as
StatementFunction callback, using an
active connection. |
<T> T |
execute(@NotNull String sql,
@NotNull PreparedStatementFunction<T> callback)
Executes a JDBC data access operation, implemented as
PreparedStatementFunction callback
working on a PreparedStatement. |
@NotNull DataSource |
get()
Gets a DataSource.
|
protected @Nullable String |
getSql(Object o) |
<T> T |
query(@NotNull PreparedStatementCreator creator,
@Nullable PreparedStatementSetter setter,
@NotNull ResultSetExtractor<T> extractor)
Executes a query using a PreparedStatement, created by a
PreparedStatementCreator and with his values set
by a PreparedStatementSetter . |
<T> T |
query(@NotNull PreparedStatementCreator creator,
@NotNull ResultSetExtractor<T> extractor)
Executes a query using a PreparedStatement, then reading the ResultSet with a
ResultSetExtractor implementation. |
<T> List<T> |
query(@NotNull PreparedStatementCreator psc,
@NotNull ResultSetRowMapper<T> resultSetRowMapper)
Executes a query using a PreparedStatement, mapping each row to a result object via a
ResultSetRowMapper implementation. |
<T> T |
query(@NotNull String sql,
@Nullable Object[] args,
@NotNull ResultSetExtractor<T> extractor)
Executes a query given an SQL statement: it will be used to create a PreparedStatement.
|
<T> T |
query(@NotNull String sql,
@Nullable Object[] args,
@NotNull ResultSetExtractor<T> extractor,
int... sqlTypes)
Executes a query given an SQL statement: it will be used to create a PreparedStatement.
|
<T> T |
query(@NotNull String sql,
@Nullable PreparedStatementSetter setter,
@NotNull ResultSetExtractor<T> extractor)
Executes a query using an SQL statement, then reading the ResultSet with a
ResultSetExtractor implementation. |
<T> List<T> |
query(@NotNull String sql,
@Nullable PreparedStatementSetter pss,
@NotNull ResultSetRowMapper<T> resultSetRowMapper)
Executes a query using an SQL statement and a
PreparedStatementSetter implementation that will bind values to the query. |
<T> T |
query(@NotNull String sql,
ResultSetExtractor<T> extractor)
Executes a query given static SQL statement, then it reads the
ResultSet using the ResultSetExtractor implementation. |
<T> List<T> |
queryForList(@NotNull String sql,
@Nullable Object[] args,
@NotNull ResultSetRowMapper<T> resultSetRowMapper)
Executes a query given an SQL statement: it will be used to create a PreparedStatement.
|
<T> List<T> |
queryForList(@NotNull String sql,
@Nullable Object[] args,
@NotNull ResultSetRowMapper<T> resultSetRowMapper,
int... sqlTypes)
Executes a query given an SQL statement: it will be used to create a PreparedStatement.
|
<T> List<T> |
queryForList(@NotNull String sql,
ResultSetRowMapper<T> resultSetRowMapper)
Executes a query given static SQL statement, then it maps each
ResultSet row to a result object using the
ResultSetRowMapper implementation. |
<T> @NotNull List<T> |
queryForListOrElseGet(@NotNull String sql,
@Nullable Object[] args,
@NotNull ResultSetRowMapper<T> resultSetRowMapper,
@NotNull Supplier<List<T>> supplier,
int... sqlTypes)
Executes a query given an SQL statement: it will be used to create a PreparedStatement.
|
<T> List<T> |
queryForListOrElseGet(@NotNull String sql,
ResultSetRowMapper<T> resultSetRowMapper,
@NotNull Supplier<List<T>> supplier)
Executes a query given static SQL statement, then it maps each
ResultSet row to a result object using the
ResultSetRowMapper implementation. |
<T> T |
queryForObject(@NotNull String sql,
Object[] args,
@NotNull ResultSetRowMapper<T> resultSetRowMapper)
Executes a query given an SQL statement: it will be used to create a PreparedStatement.
|
<T> T |
queryForObject(@NotNull String sql,
Object[] args,
ResultSetRowMapper<T> resultSetRowMapper,
int... sqlTypes)
Executes a query given an SQL statement: it will be used to create a PreparedStatement.
|
<T> T |
queryForObject(@NotNull String sql,
ResultSetRowMapper<T> resultSetRowMapper)
Executes a query given static SQL statement, then it maps the first
ResultSet row to a result object using the
ResultSetRowMapper implementation. |
<T> T |
queryForObjectOrElseGet(@NotNull String sql,
Object[] args,
ResultSetRowMapper<T> resultSetRowMapper,
@NotNull Supplier<T> supplier,
int... sqlTypes)
Executes a query given an SQL statement: it will be used to create a PreparedStatement.
|
<T> T |
queryForObjectOrElseGet(@NotNull String sql,
ResultSetRowMapper<T> resultSetRowMapper,
@NotNull Supplier<T> supplier)
Executes a query given static SQL statement, then it maps the first
ResultSet row to a result object using the
ResultSetRowMapper implementation. |
<T> T |
queryOrElseGet(@NotNull String sql,
@Nullable Object[] args,
@NotNull ResultSetExtractor<T> extractor,
Supplier<T> supplier,
int... sqlTypes)
Executes a query given an SQL statement: it will be used to create a PreparedStatement.
|
<T> T |
queryOrElseGet(@NotNull String sql,
ResultSetExtractor<T> extractor,
@NotNull Supplier<T> supplier)
Executes a query given static SQL statement, then it reads the
ResultSet using the ResultSetExtractor implementation. |
int |
update(@NotNull PreparedStatementCreator creator,
boolean getGeneratedKeys)
Performs a single update operation (like insert, delete, update) using a
PreparedStatementCreator to provide SQL and any required parameters. |
int |
update(@NotNull PreparedStatementCreator creator,
@Nullable PreparedStatementSetter setter,
boolean getGeneratedKey)
Performs a single update operation (like insert, delete, update) using a
PreparedStatementCreator to provide SQL
and any required parameters. |
int |
update(@NotNull String sql,
boolean getGeneratedKeys)
Performs a single update operation (like insert, delete, update).
|
int |
update(@NotNull String sql,
Object[] params,
boolean getGeneratedKey,
int... sqlTypes)
Performs a single update operation (like insert, update or delete statement)
via PreparedStatement, binding the given parameters.
|
int |
update(@NotNull String sql,
@Nullable PreparedStatementSetter setter,
boolean getGeneratedKey)
Performs a single update operation (like insert, delete, update).
|
getDataSource, setLogger
public Database(@NotNull @NotNull DataSource dataSource)
DataSource
.dataSource
- the DataSourcepublic <T> T execute(@NotNull @NotNull StatementFunction<T> callback) throws DataAccessException
DataOperations
StatementFunction
callback, using an
active connection.
The callback CAN return a result object (if it exists), for example a single object or a collection of objects.execute
in interface DataOperations
T
- the result typecallback
- a callback that holds the operation logicDataAccessException
- if there is any problempublic int update(@NotNull @NotNull String sql, boolean getGeneratedKeys) throws DataAccessException
DataOperations
update
in interface DataOperations
sql
- static SQL statement to executegetGeneratedKeys
- a boolean valueDataAccessException
- if there is any problempublic <T> T query(@NotNull @NotNull String sql, ResultSetExtractor<T> extractor) throws DataAccessException
DataOperations
ResultSet
using the ResultSetExtractor
implementation.query
in interface DataOperations
T
- the result typesql
- the query to executeextractor
- a callback that will extract all rows from the ResultSetDataAccessException
- if there is any problempublic <T> T queryOrElseGet(@NotNull @NotNull String sql, ResultSetExtractor<T> extractor, @NotNull @NotNull Supplier<T> supplier) throws DataAccessException
DataOperations
ResultSet
using the ResultSetExtractor
implementation.If a null
result is returned, then the given supplier will be invoked, supplying the specified result (it can be null).
queryOrElseGet
in interface DataOperations
T
- the result typesql
- the query to executeextractor
- a callback that will extract all rows from the ResultSetsupplier
- the supplier of another resultDataAccessException
- if there is any problempublic <T> List<T> queryForList(@NotNull @NotNull String sql, ResultSetRowMapper<T> resultSetRowMapper) throws DataAccessException
DataOperations
ResultSetRowMapper
implementation.queryForList
in interface DataOperations
T
- the result typesql
- the query to executeresultSetRowMapper
- a callback that will map one object per ResultSet rowDataAccessException
- if there is any problempublic <T> List<T> queryForListOrElseGet(@NotNull @NotNull String sql, ResultSetRowMapper<T> resultSetRowMapper, @NotNull @NotNull Supplier<List<T>> supplier) throws DataAccessException
DataOperations
ResultSetRowMapper
implementation.If a null
collection is returned, then the given supplier will be invoked, supplying the specified list (it can be null).
queryForListOrElseGet
in interface DataOperations
T
- the result typesql
- the query to executeresultSetRowMapper
- a callback that will map one object per ResultSet rowsupplier
- the supplier of another listDataAccessException
- if there is any problempublic <T> T queryForObject(@NotNull @NotNull String sql, ResultSetRowMapper<T> resultSetRowMapper) throws DataAccessException, IncorrectDataSizeException
DataOperations
ResultSetRowMapper
implementation.
The ResultSet must have exactly ONE row.queryForObject
in interface DataOperations
T
- the result typesql
- the query to executeresultSetRowMapper
- a callback that will map the object per ResultSet rowDataAccessException
- if there is any problemIncorrectDataSizeException
- if the query doesn't return exactly one resultpublic <T> T queryForObjectOrElseGet(@NotNull @NotNull String sql, ResultSetRowMapper<T> resultSetRowMapper, @NotNull @NotNull Supplier<T> supplier) throws DataAccessException, IncorrectDataSizeException
DataOperations
ResultSetRowMapper
implementation.If a null
result is returned, then the given supplier will be invoked, supplying the specified result (it can be null).
queryForObjectOrElseGet
in interface DataOperations
T
- the result typesql
- the query to executeresultSetRowMapper
- a callback that will map one object per ResultSet rowsupplier
- the supplier of another resultDataAccessException
- if there is any problemIncorrectDataSizeException
- if the query return more than one resultTypes
public <T> T execute(@NotNull @NotNull PreparedStatementCreator creator, @NotNull @NotNull PreparedStatementFunction<T> callback) throws DataAccessException
DataOperations
PreparedStatementFunction
callback
working on a PreparedStatement.
The callback CAN return a result object (if it exists), for example a singlet or a collection of objects.execute
in interface DataOperations
T
- the result typecreator
- a callback that creates a PreparedStatement object given a connectioncallback
- a callback that holds the operation logicDataAccessException
- if there is any problempublic <T> T execute(@NotNull @NotNull String sql, @NotNull @NotNull PreparedStatementFunction<T> callback) throws DataAccessException
DataOperations
PreparedStatementFunction
callback
working on a PreparedStatement.
The callback CAN return a result object (if it exists), for example a singlet or a collection of objects.execute
in interface DataOperations
T
- the result typesql
- the SQL statement to executecallback
- a callback that holds the operation logicDataAccessException
- if there is any problempublic int update(@NotNull @NotNull PreparedStatementCreator creator, @Nullable @Nullable PreparedStatementSetter setter, boolean getGeneratedKey) throws DataAccessException
DataOperations
PreparedStatementCreator
to provide SQL
and any required parameters. A PreparedStatementSetter
can be passed as helper that sets bind parameters.update
in interface DataOperations
creator
- a callback that provides the PreparedStatement with bind parameterssetter
- a helper that sets bind parameters. If it's null then this will be an update with static SQLgetGeneratedKey
- a boolean valueDataAccessException
- if there is any problempublic int update(@NotNull @NotNull PreparedStatementCreator creator, boolean getGeneratedKeys) throws DataAccessException
DataOperations
PreparedStatementCreator
to provide SQL and any required parameters.update
in interface DataOperations
creator
- a callback that provides the PreparedStatement with required parametersgetGeneratedKeys
- a boolean valuesDataAccessException
- if there is any problempublic int update(@NotNull @NotNull String sql, @Nullable @Nullable PreparedStatementSetter setter, boolean getGeneratedKey) throws DataAccessException
DataOperations
PreparedStatementSetter
can be passed as helper that sets bind parameters.update
in interface DataOperations
sql
- the SQL containing bind parameterssetter
- a helper that sets bind parameters. If it's null then this will be an update with static SQLgetGeneratedKey
- a boolean valueDataAccessException
- if there is any problempublic int update(@NotNull @NotNull String sql, Object[] params, boolean getGeneratedKey, int... sqlTypes) throws DataAccessException
DataOperations
update
in interface DataOperations
sql
- the SQL containing bind parametersparams
- arguments to be bind to the given SQLgetGeneratedKey
- a boolean valuesqlTypes
- an integer array containing the type of the query's parameters, expressed as Types
DataAccessException
- if there is any problempublic void batchUpdate(@NotNull @NotNull String sql, @NotNull @NotNull BatchSetter batchSetter) throws IllegalStateException
DataOperations
NOTE: this method will be unusable if the driver doesn't support batch updates.
batchUpdate
in interface DataOperations
sql
- the SQL containing bind parameters. It will be
reused because all statements in a batch use the same SQLbatchSetter
- a callback that sets parameters on the PreparedStatement created by this methodIllegalStateException
- if the driver doesn't support batch updatespublic void batchUpdate(@NotNull @NotNull String sql, @Nullable @Nullable List<Object[]> batchArgs, int... sqlTypes) throws IllegalStateException
DataOperations
batchUpdate
in interface DataOperations
sql
- The SQL containing bind parameters. It will be
reused because all statements in a batch use the same SQLbatchArgs
- A list of object arrays containing the batch argumentssqlTypes
- an integer array containing the type of the query's parameters, expressed as Types
IllegalStateException
- If the driver doesn't support batch updatespublic <T> void batchUpdate(@NotNull @NotNull String sql, @Nullable @Nullable List<T> batchArgs, @NotNull @NotNull ParametrizedBatchSetter<T> paramsBatchSetter) throws IllegalStateException
DataOperations
batchUpdate
in interface DataOperations
T
- the parameter typesql
- the SQL containing bind parameters. It will be
reused because all statements in a batch use the same SQLbatchArgs
- a list of objects containing the batch argumentsparamsBatchSetter
- a callback that sets parameters on the PreparedStatement created by this methodIllegalStateException
- if the driver doesn't support batch updatespublic <T> T query(@NotNull @NotNull PreparedStatementCreator creator, @Nullable @Nullable PreparedStatementSetter setter, @NotNull @NotNull ResultSetExtractor<T> extractor) throws DataAccessException
DataOperations
PreparedStatementCreator
and with his values set
by a PreparedStatementSetter
.
Most other query methods use this method, but application code will always work with either a creator or a setter.
query
in interface DataOperations
T
- the result typecreator
- a callback that creates a PreparedStatementsetter
- a callback that sets values on the PreparedStatement. If null, the SQL will be treated as static SQL with no bind parametersextractor
- a callback that will extract results given a ResultSetDataAccessException
- if there is any problemPreparedStatementSetter
public <T> T query(@NotNull @NotNull PreparedStatementCreator creator, @NotNull @NotNull ResultSetExtractor<T> extractor) throws DataAccessException
DataOperations
ResultSetExtractor
implementation.query
in interface DataOperations
T
- the result typecreator
- a callback that creates a PreparedStatementextractor
- a callback that will extract results given a ResultSetDataAccessException
- if there is any problemPreparedStatementCreator
public <T> List<T> query(@NotNull @NotNull PreparedStatementCreator psc, @NotNull @NotNull ResultSetRowMapper<T> resultSetRowMapper) throws DataAccessException
DataOperations
ResultSetRowMapper
implementation.query
in interface DataOperations
T
- the result typepsc
- a callback that creates a PreparedStatementresultSetRowMapper
- a callback that will map one object per ResultSet rowDataAccessException
- if there is any problempublic <T> T query(@NotNull @NotNull String sql, @Nullable @Nullable PreparedStatementSetter setter, @NotNull @NotNull ResultSetExtractor<T> extractor) throws DataAccessException
DataOperations
ResultSetExtractor
implementation.query
in interface DataOperations
T
- the result typesql
- the query to executesetter
- a callback that sets values on the PreparedStatement. If null, the SQL will be treated as static SQL with no bind parametersextractor
- a callback that will extract results given a ResultSetDataAccessException
- if there is any problempublic <T> List<T> query(@NotNull @NotNull String sql, @Nullable @Nullable PreparedStatementSetter pss, @NotNull @NotNull ResultSetRowMapper<T> resultSetRowMapper) throws DataAccessException
DataOperations
PreparedStatementSetter
implementation that will bind values to the query.
Each row of the ResultSet will be mapped to a result object via a ResultSetRowMapper implementation.query
in interface DataOperations
T
- the result typesql
- the query to executepss
- a callback that sets values on the PreparedStatement. If null, the SQL will be treated as static SQL with no bind parametersresultSetRowMapper
- a callback that will map one object per ResultSet rowDataAccessException
- if there is any problempublic <T> T query(@NotNull @NotNull String sql, @Nullable @Nullable Object[] args, @NotNull @NotNull ResultSetExtractor<T> extractor) throws DataAccessException
DataOperations
ResultSetExtractor
implementation will read the ResultSet.query
in interface DataOperations
T
- the result typesql
- the query to executeargs
- arguments to bind to the queryextractor
- a callback that will extract results given a ResultSetDataAccessException
- if there is any problempublic <T> T query(@NotNull @NotNull String sql, @Nullable @Nullable Object[] args, @NotNull @NotNull ResultSetExtractor<T> extractor, int... sqlTypes) throws DataAccessException
DataOperations
Types
will be bound to the query.
The ResultSetExtractor
implementation will read the ResultSet.query
in interface DataOperations
T
- the result typesql
- the query to executeargs
- arguments to bind to the queryextractor
- a callback that will extract results given a ResultSetsqlTypes
- an integer array containing the type of the query's parameters, expressed as Types
DataAccessException
- if there is any problemTypes
public <T> T queryOrElseGet(@NotNull @NotNull String sql, @Nullable @Nullable Object[] args, @NotNull @NotNull ResultSetExtractor<T> extractor, Supplier<T> supplier, int... sqlTypes) throws DataAccessException
DataOperations
Types
will be bound to the query.
The ResultSetExtractor
implementation will read the ResultSet.If a null
result is returned, then the given supplier will be invoked, supplying the specified result (it can be null).
queryOrElseGet
in interface DataOperations
T
- the result typesql
- the query to executeargs
- arguments to bind to the queryextractor
- a callback that will extract results given a ResultSetsupplier
- the supplier of another resultsqlTypes
- an integer array containing the type of the query's parameters, expressed as Types
DataAccessException
- if there is any problemTypes
public <T> List<T> queryForList(@NotNull @NotNull String sql, @Nullable @Nullable Object[] args, @NotNull @NotNull ResultSetRowMapper<T> resultSetRowMapper) throws DataAccessException
DataOperations
ResultSetRowMapper
implementation.queryForList
in interface DataOperations
T
- the result typesql
- the query to executeargs
- arguments to bind to the queryresultSetRowMapper
- a callback that will map one object per ResultSet rowDataAccessException
- if there is any problempublic <T> List<T> queryForList(@NotNull @NotNull String sql, @Nullable @Nullable Object[] args, @NotNull @NotNull ResultSetRowMapper<T> resultSetRowMapper, int... sqlTypes) throws DataAccessException
DataOperations
Types
will be bound to the query.
Each row of the ResultSet will be mapped to a result object via a ResultSetRowMapper
implementation.queryForList
in interface DataOperations
T
- the result typesql
- the query to executeargs
- arguments to bind to the queryresultSetRowMapper
- a callback that will map one object per ResultSet rowsqlTypes
- an integer array containing the type of the query's parameters, expressed as Types
DataAccessException
- if there is any problemTypes
@NotNull public <T> @NotNull List<T> queryForListOrElseGet(@NotNull @NotNull String sql, @Nullable @Nullable Object[] args, @NotNull @NotNull ResultSetRowMapper<T> resultSetRowMapper, @NotNull @NotNull Supplier<List<T>> supplier, int... sqlTypes) throws DataAccessException
DataOperations
Types
will be bound to the query.
Each row of the ResultSet will be mapped to a result object via a ResultSetRowMapper
implementation.null
collection is returned, then the given supplier will be invoked, supplying the specified list (it can be null).queryForListOrElseGet
in interface DataOperations
T
- the result typesql
- the query to executeargs
- arguments to bind to the queryresultSetRowMapper
- a callback that will map one object per ResultSet rowsupplier
- the supplier of another listsqlTypes
- an integer array containing the type of the query's parameters, expressed as Types
DataAccessException
- if there is any problemTypes
public <T> T queryForObject(@NotNull @NotNull String sql, Object[] args, @NotNull @NotNull ResultSetRowMapper<T> resultSetRowMapper) throws DataAccessException, IncorrectDataSizeException
DataOperations
The first ResultSet row will be mapped to a result object using the ResultSetRowMapper
implementation.
queryForObject
in interface DataOperations
T
- the result typesql
- the query to executeargs
- arguments to bind to the queryresultSetRowMapper
- a callback that will map one object per ResultSet rowDataAccessException
- if there is any problemIncorrectDataSizeException
- if the query doesn't return exactly one resultpublic <T> T queryForObject(@NotNull @NotNull String sql, Object[] args, ResultSetRowMapper<T> resultSetRowMapper, int... sqlTypes) throws DataAccessException, IncorrectDataSizeException
DataOperations
Types
will be bound to the query.
The first ResultSet row will be mapped to a result object using the ResultSetRowMapper
implementation.
queryForObject
in interface DataOperations
T
- the result typesql
- the query to executeargs
- arguments to bind to the queryresultSetRowMapper
- a callback that will map one object per ResultSet rowsqlTypes
- an integer array containing the type of the query's parameters, expressed as Types
DataAccessException
- if there is any problemIncorrectDataSizeException
- if the query doesn't return exactly one resultTypes
@Nullable public <T> T queryForObjectOrElseGet(@NotNull @NotNull String sql, Object[] args, ResultSetRowMapper<T> resultSetRowMapper, @NotNull @NotNull Supplier<T> supplier, int... sqlTypes) throws DataAccessException, IncorrectDataSizeException
DataOperations
Types
will be bound to the query.
The first ResultSet row will be mapped to a result object using the ResultSetRowMapper
implementation.
If a null
result is returned, then the given supplier will be invoked, supplying the specified result (it can be null).
queryForObjectOrElseGet
in interface DataOperations
T
- the result typesql
- the query to executeargs
- arguments to bind to the queryresultSetRowMapper
- a callback that will map one object per ResultSet rowsupplier
- the supplier of another resultsqlTypes
- an integer array containing the type of the query's parameters, expressed as Types
DataAccessException
- if there is any problemIncorrectDataSizeException
- if the query return more than one resultTypes
@NotNull public @NotNull DataSource get()
DataSourceSupplier
get
in interface DataSourceSupplier
get
in interface Supplier<DataSource>
Copyright © 2021. All rights reserved.