public class Database extends DatabaseAccessor implements DataOperations
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. |
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 a 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 a 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 a 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 a 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 a 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 a 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> T |
queryForObject(@NotNull String sql,
Object[] args,
@NotNull ResultSetRowMapper<T> resultSetRowMapper)
Executes a query given a 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 a 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. |
int |
update(@NotNull PreparedStatementCreator creator,
boolean getGeneratedKeys)
Performs a single update operation (like insert, delete, update) using a
PreparedStatementCreator to
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)
DataOperations
update
in interface DataOperations
sql
- static SQL statement to executegetGeneratedKeys
- a boolean valuepublic <T> T query(@NotNull @NotNull String sql, ResultSetExtractor<T> extractor)
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 ResultSetpublic <T> List<T> queryForList(@NotNull @NotNull String sql, ResultSetRowMapper<T> resultSetRowMapper)
DataOperations
ResultSetRowMapper
implementation.queryForList
in interface DataOperations
T
- the result typesql
- the query to executeresultSetRowMapper
- a callback that will map one object per ResultSet rowpublic <T> T queryForObject(@NotNull @NotNull String sql, ResultSetRowMapper<T> resultSetRowMapper)
DataOperations
ResultSetRowMapper
implementation.
Note: use of this method is discouraged when the query doesn't supply exactly one row. If more rows are supplied then this method will return only the first one.
queryForObject
in interface DataOperations
T
- the result typesql
- the query to executeresultSetRowMapper
- a callback that will map the object per ResultSet rowpublic <T> T execute(@NotNull @NotNull PreparedStatementCreator creator, @NotNull @NotNull PreparedStatementFunction<T> callback)
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 logicpublic <T> T execute(@NotNull @NotNull String sql, @NotNull @NotNull PreparedStatementFunction<T> callback)
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 logicpublic int update(@NotNull @NotNull PreparedStatementCreator creator, @Nullable @Nullable PreparedStatementSetter setter, boolean getGeneratedKey)
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 valuepublic int update(@NotNull @NotNull PreparedStatementCreator creator, boolean getGeneratedKeys)
DataOperations
PreparedStatementCreator
to
to provide SQL and any required parameters.update
in interface DataOperations
creator
- a callback that provides the PreparedStatement with required parametersgetGeneratedKeys
- a boolean valuespublic int update(@NotNull @NotNull String sql, @Nullable @Nullable PreparedStatementSetter setter, boolean getGeneratedKey)
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 valuepublic int update(@NotNull @NotNull String sql, Object[] params, boolean getGeneratedKey, int... sqlTypes)
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
public 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)
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 ResultSetPreparedStatementSetter
public <T> T query(@NotNull @NotNull PreparedStatementCreator creator, @NotNull @NotNull ResultSetExtractor<T> extractor)
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 ResultSetPreparedStatementCreator
public <T> List<T> query(@NotNull @NotNull PreparedStatementCreator psc, @NotNull @NotNull ResultSetRowMapper<T> resultSetRowMapper)
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 rowpublic <T> T query(@NotNull @NotNull String sql, @Nullable @Nullable PreparedStatementSetter setter, @NotNull @NotNull ResultSetExtractor<T> extractor)
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 ResultSetpublic <T> List<T> query(@NotNull @NotNull String sql, @Nullable @Nullable PreparedStatementSetter pss, @NotNull @NotNull ResultSetRowMapper<T> resultSetRowMapper)
DataOperations
PreparedStatementSetter
implementation that will bind values to the query.
Each row of the ResultSet will be map 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 rowpublic <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)
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
Types
public <T> List<T> queryForList(@NotNull @NotNull String sql, @Nullable @Nullable Object[] args, @NotNull @NotNull ResultSetRowMapper<T> resultSetRowMapper)
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 rowpublic <T> List<T> queryForList(@NotNull @NotNull String sql, @Nullable @Nullable Object[] args, @NotNull @NotNull ResultSetRowMapper<T> resultSetRowMapper, int... sqlTypes)
DataOperations
Types
will be bound to the query.
Each row of the ResultSet will be map 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
Types
public <T> T queryForObject(@NotNull @NotNull String sql, Object[] args, @NotNull @NotNull ResultSetRowMapper<T> resultSetRowMapper)
DataOperations
ResultSetRowMapper
implementation.
Note: use of this method is discouraged when the query doesn't supply exactly one row. If more rows are supplied then this method will return only the first one.
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 rowpublic <T> T queryForObject(@NotNull @NotNull String sql, Object[] args, ResultSetRowMapper<T> resultSetRowMapper, int... sqlTypes)
DataOperations
Types
will be bound to the query.
Each row of the ResultSet will be map to a result object via a ResultSetRowMapper
implementation.
Note: use of this method is discouraged when the query doesn't supply exactly one row. If more rows are supplied then this method will return only the first one.
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
Types
Copyright © 2021. All rights reserved.