public class AsyncDatabase extends Object implements AsyncDataOperations
An instance of AsyncDatabase
is available by passing a valid DataSource
and an implementation of
the Executor
interface.
This class:
Executor.execute(Runnable)
CompletableFuture
objects that WILL STORE usable future resultsResultSet
sMethods of this class use various callback interfaces. A reading of those is greatly suggested.
Since the callback interfaces make AsyncDatabase's methods parameterizable, there should be no need to subclass this class.
Modifier and Type | Field and Description |
---|---|
protected Executor |
executor |
protected DataOperations |
operations |
Constructor and Description |
---|
AsyncDatabase(@NotNull DataOperations operations,
@NotNull Executor executor)
Constructs a new
AsyncDatabase that will use the given DataOperations 's implementations asynchronously via Executor . |
AsyncDatabase(@NotNull DataSource dataSource,
@NotNull Executor executor)
Constructs a new
AsyncDatabase instance by wrapping the given DataSource object in the default Database
implementation. |
Modifier and Type | Method and Description |
---|---|
CompletableFuture<Void> |
batchUpdate(@NotNull String sql,
@NotNull BatchSetter batchSetter)
Performs multiple update operations using a single SQL statement.
|
CompletableFuture<Void> |
batchUpdate(@NotNull String sql,
@Nullable List<Object[]> batchArgs,
int... sqlTypes)
Performs multiple update operations using a single SQL statement.
|
<T> CompletableFuture<Void> |
batchUpdate(@NotNull String sql,
@Nullable List<T> batchArgs,
@NotNull ParametrizedBatchSetter<T> paramsBatchSetter)
Performs multiple update operations using a single SQL statement.
|
<T> CompletableFuture<T> |
execute(@NotNull PreparedStatementCreator creator,
@NotNull PreparedStatementFunction<T> callback)
Executes a JDBC data access operation, implemented as
PreparedStatementFunction callback
working on a PreparedStatement. |
<T> CompletableFuture<T> |
execute(@NotNull StatementFunction<T> callback)
Executes a JDBC data access operation, implemented as
StatementFunction callback, using an
active connection. |
<T> CompletableFuture<T> |
execute(@NotNull String sql,
@NotNull PreparedStatementFunction<T> callback)
Executes a JDBC data access operation, implemented as
PreparedStatementFunction callback
working on a PreparedStatement. |
Optional<Database> |
getDatabase()
Returns the Database class instance which wraps the given DataSource's instance.
|
Optional<DataSource> |
getDataSource()
Returns an Optional instance containing the
DataSource used by this AsyncDatabase 's instance. |
<T> CompletableFuture<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> CompletableFuture<T> |
query(@NotNull PreparedStatementCreator creator,
@NotNull ResultSetExtractor<T> extractor)
Executes a query using a PreparedStatement, then reading the ResultSet with a
ResultSetExtractor implementation. |
<T> CompletableFuture<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> CompletableFuture<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> CompletableFuture<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> CompletableFuture<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> CompletableFuture<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> CompletableFuture<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> CompletableFuture<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> CompletableFuture<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> CompletableFuture<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> CompletableFuture<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> CompletableFuture<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> CompletableFuture<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. |
CompletableFuture<Integer> |
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. |
CompletableFuture<Integer> |
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. |
CompletableFuture<Integer> |
update(@NotNull String sql,
boolean getGeneratedKeys)
Performs a single update operation (like insert, delete, update).
|
CompletableFuture<Integer> |
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.
|
CompletableFuture<Integer> |
update(@NotNull String sql,
@Nullable PreparedStatementSetter setter,
boolean getGeneratedKey)
Performs a single update operation (like insert, delete, update).
|
protected final DataOperations operations
protected final Executor executor
public AsyncDatabase(@NotNull @NotNull DataSource dataSource, @NotNull @NotNull Executor executor)
AsyncDatabase
instance by wrapping the given DataSource
object in the default Database
implementation.public AsyncDatabase(@NotNull @NotNull DataOperations operations, @NotNull @NotNull Executor executor)
AsyncDatabase
that will use the given DataOperations
's implementations asynchronously via Executor
.public Optional<Database> getDatabase()
AsyncDatabase(DataOperations, Executor)
constructor is used to create a new instance of this class.Database
(it can be used for synchronous operations).IllegalStateException
- if there is no Database
availableOptional
public Optional<DataSource> getDataSource()
DataSource
used by this AsyncDatabase
's instance.
The AsyncDatabase class can obtain the DataSource from the DataOperations
implementation.DataSourceSupplier
nor DatabaseAccessor
then an empty
Optional will be returned.IllegalStateException
- if there was a problem while trying to retrieve the DataSource
's instanceOptional
public <T> CompletableFuture<T> execute(@NotNull @NotNull StatementFunction<T> callback) throws DataAccessException
AsyncDataOperations
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 AsyncDataOperations
T
- the result typecallback
- a callback that holds the operation logicDataAccessException
- if there is any problempublic CompletableFuture<Integer> update(@NotNull @NotNull String sql, boolean getGeneratedKeys) throws DataAccessException
AsyncDataOperations
update
in interface AsyncDataOperations
sql
- static SQL statement to executegetGeneratedKeys
- a boolean valueDataAccessException
- if there is any problempublic <T> CompletableFuture<T> query(@NotNull @NotNull String sql, ResultSetExtractor<T> extractor) throws DataAccessException
AsyncDataOperations
ResultSet
using the ResultSetExtractor
implementation.query
in interface AsyncDataOperations
T
- the result typesql
- the query to executeextractor
- a callback that will extract all rows from the ResultSetDataAccessException
- if there is any problempublic <T> CompletableFuture<List<T>> queryForList(@NotNull @NotNull String sql, ResultSetRowMapper<T> resultSetRowMapper) throws DataAccessException
AsyncDataOperations
ResultSetRowMapper
implementation.queryForList
in interface AsyncDataOperations
T
- the result typesql
- the query to executeresultSetRowMapper
- a callback that will map one object per ResultSet rowDataAccessException
- if there is any problempublic <T> CompletableFuture<T> queryForObject(@NotNull @NotNull String sql, ResultSetRowMapper<T> resultSetRowMapper) throws DataAccessException
AsyncDataOperations
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 AsyncDataOperations
T
- the result typesql
- the query to executeresultSetRowMapper
- a callback that will map the object per ResultSet rowDataAccessException
- if there is any problempublic <T> CompletableFuture<T> execute(@NotNull @NotNull PreparedStatementCreator creator, @NotNull @NotNull PreparedStatementFunction<T> callback) throws DataAccessException
AsyncDataOperations
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 AsyncDataOperations
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> CompletableFuture<T> execute(@NotNull @NotNull String sql, @NotNull @NotNull PreparedStatementFunction<T> callback) throws DataAccessException
AsyncDataOperations
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 AsyncDataOperations
T
- the result typesql
- the SQL statement to executecallback
- a callback that holds the operation logicDataAccessException
- if there is any problempublic CompletableFuture<Integer> update(@NotNull @NotNull PreparedStatementCreator creator, @Nullable @Nullable PreparedStatementSetter setter, boolean getGeneratedKey) throws DataAccessException
AsyncDataOperations
PreparedStatementCreator
to provide SQL
and any required parameters. A PreparedStatementSetter
can be passed as helper that sets bind parameters.update
in interface AsyncDataOperations
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 CompletableFuture<Integer> update(@NotNull @NotNull PreparedStatementCreator creator, boolean getGeneratedKeys) throws DataAccessException
AsyncDataOperations
PreparedStatementCreator
to
to provide SQL and any required parameters.update
in interface AsyncDataOperations
creator
- a callback that provides the PreparedStatement with required parametersgetGeneratedKeys
- a boolean valuesDataAccessException
- if there is any problempublic CompletableFuture<Integer> update(@NotNull @NotNull String sql, @Nullable @Nullable PreparedStatementSetter setter, boolean getGeneratedKey) throws DataAccessException
AsyncDataOperations
PreparedStatementSetter
can be passed as helper that sets bind parameters.update
in interface AsyncDataOperations
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 CompletableFuture<Integer> update(@NotNull @NotNull String sql, Object[] params, boolean getGeneratedKey, int... sqlTypes) throws DataAccessException
AsyncDataOperations
update
in interface AsyncDataOperations
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 CompletableFuture<Void> batchUpdate(@NotNull @NotNull String sql, @NotNull @NotNull BatchSetter batchSetter) throws IllegalStateException
AsyncDataOperations
NOTE: this method will be unusable if the driver doesn't support batch updates.
batchUpdate
in interface AsyncDataOperations
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 CompletableFuture<Void> batchUpdate(@NotNull @NotNull String sql, @Nullable @Nullable List<Object[]> batchArgs, int... sqlTypes) throws IllegalStateException
AsyncDataOperations
batchUpdate
in interface AsyncDataOperations
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> CompletableFuture<Void> batchUpdate(@NotNull @NotNull String sql, @Nullable @Nullable List<T> batchArgs, @NotNull @NotNull ParametrizedBatchSetter<T> paramsBatchSetter) throws IllegalStateException
AsyncDataOperations
batchUpdate
in interface AsyncDataOperations
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> CompletableFuture<T> query(@NotNull @NotNull PreparedStatementCreator creator, @Nullable @Nullable PreparedStatementSetter setter, @NotNull @NotNull ResultSetExtractor<T> extractor) throws DataAccessException
AsyncDataOperations
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 AsyncDataOperations
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> CompletableFuture<T> query(@NotNull @NotNull PreparedStatementCreator creator, @NotNull @NotNull ResultSetExtractor<T> extractor) throws DataAccessException
AsyncDataOperations
ResultSetExtractor
implementation.query
in interface AsyncDataOperations
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> CompletableFuture<List<T>> query(@NotNull @NotNull PreparedStatementCreator psc, @NotNull @NotNull ResultSetRowMapper<T> resultSetRowMapper) throws DataAccessException
AsyncDataOperations
ResultSetRowMapper
implementation.query
in interface AsyncDataOperations
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> CompletableFuture<T> query(@NotNull @NotNull String sql, @Nullable @Nullable PreparedStatementSetter setter, @NotNull @NotNull ResultSetExtractor<T> extractor) throws DataAccessException
AsyncDataOperations
ResultSetExtractor
implementation.query
in interface AsyncDataOperations
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> CompletableFuture<List<T>> query(@NotNull @NotNull String sql, @Nullable @Nullable PreparedStatementSetter pss, @NotNull @NotNull ResultSetRowMapper<T> resultSetRowMapper) throws DataAccessException
AsyncDataOperations
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 AsyncDataOperations
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> CompletableFuture<T> query(@NotNull @NotNull String sql, @Nullable @Nullable Object[] args, @NotNull @NotNull ResultSetExtractor<T> extractor) throws DataAccessException
AsyncDataOperations
ResultSetExtractor
implementation will read the ResultSet.query
in interface AsyncDataOperations
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> CompletableFuture<T> query(@NotNull @NotNull String sql, @Nullable @Nullable Object[] args, @NotNull @NotNull ResultSetExtractor<T> extractor, int... sqlTypes) throws DataAccessException
AsyncDataOperations
Types
will be bound to the query.
The ResultSetExtractor
implementation will read the ResultSet.query
in interface AsyncDataOperations
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> CompletableFuture<List<T>> queryForList(@NotNull @NotNull String sql, @Nullable @Nullable Object[] args, @NotNull @NotNull ResultSetRowMapper<T> resultSetRowMapper) throws DataAccessException
AsyncDataOperations
ResultSetRowMapper
implementation.queryForList
in interface AsyncDataOperations
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> CompletableFuture<List<T>> queryForList(@NotNull @NotNull String sql, @Nullable @Nullable Object[] args, @NotNull @NotNull ResultSetRowMapper<T> resultSetRowMapper, int... sqlTypes) throws DataAccessException
AsyncDataOperations
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 AsyncDataOperations
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
public <T> CompletableFuture<T> queryForObject(@NotNull @NotNull String sql, Object[] args, @NotNull @NotNull ResultSetRowMapper<T> resultSetRowMapper) throws DataAccessException
AsyncDataOperations
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 AsyncDataOperations
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> CompletableFuture<T> queryForObject(@NotNull @NotNull String sql, Object[] args, ResultSetRowMapper<T> resultSetRowMapper, int... sqlTypes) throws DataAccessException
AsyncDataOperations
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 AsyncDataOperations
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
Copyright © 2021. All rights reserved.