Class DbQueryManager
- All Implemented Interfaces:
Cloneable
- Direct Known Subclasses:
AbstractGenericQueryManager
,DatabaseContent
,DatabaseContentStore
,DatabaseRawStore
,DatabaseRemember
,DatabaseResources
,DatabaseScheduling
,DatabaseSessions
,DatabaseSessionValidator
,DatabaseTaskOptions
,DatabaseTasks
,DatabaseUsers
Datasource
that's provided to the constructor of the DbQueryManager
.
A collection of convenience methods have been provided to quickly
execute queries in a variety of manners without having to worry about the
logic behind it or having to remember to close the queries at the
appropriate moment. These methods optionally interact with the
DbPreparedStatementHandler
and DbResultSetHandler
classes to make it possible to fully customize the executed queries. The
following categories of worry-free methods exist:
- execute an update query directly
- execute a customizable update query
- execute a customizable select query
- check the result rows of a customizable select query
- obtain the first value of a customizable select query
- fetch the first row of a customizable select query
- fetch the first bean of a customizable select query
- fetch all rows of a customizable select query
- fetch all beans of a customizable select query
Lower-level methods are also available for the sake of repetitive
code-reduction. To obtain execute regular statements directly,
use the executeQuery
method.
Finally, since DbStatement
and
DbPreparedStatement
instances preserve a reference to their
result set, it's easy to iterate over the rows of a result set with the
fetch
or fetchAll
methods.
- Since:
- 1.0
- See Also:
-
Constructor Summary
ConstructorDescriptionDbQueryManager
(Datasource datasource) Instantiates a newDbQueryManager
object and ties it to the provided datasource. -
Method Summary
Modifier and TypeMethodDescriptionclone()
Simply clones the instance with the default clone method.boolean
executeFetchAll
(ReadQuery query, DbRowProcessor rowProcessor) Safely and quickly fetches all the rows from the results of a select query.boolean
executeFetchAll
(ReadQuery query, DbRowProcessor rowProcessor, PreparedStatementHandler handler) Safely fetches all the rows from the results of a customizable select query.boolean
executeFetchAll
(ReadQuery query, RowProcessor rowProcessor) Convenience alternative toexecuteFetchAll(ReadQuery, DbRowProcessor)
that uses a simplifiedRowProcessor
that can be implemented with a lambda.boolean
executeFetchAll
(ReadQuery query, RowProcessor rowProcessor, PreparedStatementHandler handler) Convenience alternative toexecuteFetchAll(ReadQuery, DbRowProcessor, PreparedStatementHandler)
that uses a simplifiedRowProcessor
that can be implemented with a lambda.<BeanType> List<BeanType>
executeFetchAllBeans
(ReadQuery query, Class<BeanType> beanClass) Safely and quickly fetches the all the bean instances from the results of a select query.<BeanType> List<BeanType>
executeFetchAllBeans
(ReadQuery query, Class<BeanType> beanClass, PreparedStatementHandler handler) Safely fetches the all the bean instances from the results of a customizable select query.boolean
executeFetchFirst
(ReadQuery query, DbRowProcessor rowProcessor) Safely and quickly fetches the first row from the results of a select query.boolean
executeFetchFirst
(ReadQuery query, DbRowProcessor rowProcessor, PreparedStatementHandler handler) Safely fetches the first row from the results of a customizable select query.boolean
executeFetchFirst
(ReadQuery query, RowProcessor rowProcessor) Convenience alternative toexecuteFetchFirst(ReadQuery, DbRowProcessor)
that uses a simplifiedRowProcessor
that can be implemented with a lambda.boolean
executeFetchFirst
(ReadQuery query, RowProcessor rowProcessor, PreparedStatementHandler handler) Convenience alternative toexecuteFetchFirst(ReadQuery, DbRowProcessor, PreparedStatementHandler)
that uses a simplifiedRowProcessor
that can be implemented with a lambda.<BeanType> BeanType
executeFetchFirstBean
(ReadQuery query, Class<BeanType> beanClass) Safely and quickly fetches the first bean instance from the results of a select query.<BeanType> BeanType
executeFetchFirstBean
(ReadQuery query, Class<BeanType> beanClass, PreparedStatementHandler handler) Safely fetches the first bean instance from the results of a customizable select query.boolean
executeGetFirstBoolean
(ReadQuery query) Safely and quickly retrieves the first cell as aboolean
from the results of a select query.boolean
executeGetFirstBoolean
(ReadQuery query, PreparedStatementHandler handler) Safely retrieves the first cell as aboolean
from the results of a customizable select query.byte
executeGetFirstByte
(ReadQuery query) Safely and quickly retrieves the first cell as abyte
from the results of a select query.byte
executeGetFirstByte
(ReadQuery query, PreparedStatementHandler handler) Safely retrieves the first cell as abyte
from the results of a customizable select query.byte[]
executeGetFirstBytes
(ReadQuery query) Safely and quickly retrieves the first cell as abyte
array from the results of a select query.byte[]
executeGetFirstBytes
(ReadQuery query, PreparedStatementHandler handler) Safely retrieves the first cell as abyte
array from the results of a customizable select query.executeGetFirstDate
(ReadQuery query) Safely and quickly retrieves the first cell as a sqlDate
from the results of a select query.executeGetFirstDate
(ReadQuery query, Calendar cal) Safely and quickly retrieves the first cell as a sqlDate
from the results of a select query.executeGetFirstDate
(ReadQuery query, Calendar cal, PreparedStatementHandler handler) Safely retrieves the first cell as a sqlDate
from the results of a customizable select query.executeGetFirstDate
(ReadQuery query, PreparedStatementHandler handler) Safely retrieves the first cell as a sqlDate
from the results of a customizable select query.double
executeGetFirstDouble
(ReadQuery query) Safely and quickly retrieves the first cell as adouble
from the results of a select query.double
executeGetFirstDouble
(ReadQuery query, PreparedStatementHandler handler) Safely retrieves the first cell as adouble
from the results of a customizable select query.float
executeGetFirstFloat
(ReadQuery query) Safely and quickly retrieves the first cell as afloat
from the results of a select query.float
executeGetFirstFloat
(ReadQuery query, PreparedStatementHandler handler) Safely retrieves the first cell as afloat
from the results of a customizable select query.int
executeGetFirstInt
(ReadQuery query) Safely and quickly retrieves the first cell as aint
from the results of a select query.int
executeGetFirstInt
(ReadQuery query, PreparedStatementHandler handler) Safely retrieves the first cell as aint
from the results of a customizable select query.long
executeGetFirstLong
(ReadQuery query) Safely and quickly retrieves the first cell as along
from the results of a select query.long
executeGetFirstLong
(ReadQuery query, PreparedStatementHandler handler) Safely retrieves the first cell as along
from the results of a customizable select query.short
executeGetFirstShort
(ReadQuery query) Safely and quickly retrieves the first cell as ashort
from the results of a select query.short
executeGetFirstShort
(ReadQuery query, PreparedStatementHandler handler) Safely retrieves the first cell as ashort
from the results of a customizable select query.executeGetFirstString
(ReadQuery query) Safely and quickly retrieves the first cell as aString
from the results of a select query.executeGetFirstString
(ReadQuery query, PreparedStatementHandler handler) Safely retrieves the first cell as aString
from the results of a customizable select query.executeGetFirstTime
(ReadQuery query) Safely and quickly retrieves the first cell as a sqlTime
from the results of a select query.executeGetFirstTime
(ReadQuery query, Calendar cal) Safely and quickly retrieves the first cell as a sqlTime
from the results of a select query.executeGetFirstTime
(ReadQuery query, Calendar cal, PreparedStatementHandler handler) Safely retrieves the first cell as a sqlTime
from the results of a customizable select query.executeGetFirstTime
(ReadQuery query, PreparedStatementHandler handler) Safely retrieves the first cell as a sqlTime
from the results of a customizable select query.Safely and quickly retrieves the first cell as a sqlTimestamp
from the results of a select query.executeGetFirstTimestamp
(ReadQuery query, Calendar cal) Safely and quickly retrieves the first cell as a sqlTimestamp
from the results of a select query.executeGetFirstTimestamp
(ReadQuery query, Calendar cal, PreparedStatementHandler handler) Safely retrieves the first cell as a sqlTimestamp
from the results of a customizable select query.executeGetFirstTimestamp
(ReadQuery query, PreparedStatementHandler handler) Safely retrieves the first cell as a sqlTimestamp
from the results of a customizable select query.boolean
executeHasResultRows
(ReadQuery query) Safely and quickly verifies if a select query returns any rows.boolean
executeHasResultRows
(ReadQuery query, PreparedStatementHandler handler) Safely verifies if a customizable select query returns any rows.executeQuery
(ReadQuery query) Executes a query statement in a connection of thisDbQueryManager
'sDatasource
.<ResultType>
ResultTypeexecuteQuery
(ReadQuery query, PreparedStatementHandler handler) Executes a customizable select statement.<ResultType>
ResultTypeexecuteResultQuery
(ReadQuery query, DbResultSetHandler handler) Executes a select statement and handle the results in a custom fashion.int
executeUpdate
(String sql) Safely and quickly executes an update statement.int
executeUpdate
(Query query) Safely and quickly executes an update statement.int
executeUpdate
(Query query, PreparedStatementHandler handler) Safely execute an update statement.<ResultType>
ResultTypeexecuteUseFirstAsciiStream
(ReadQuery query, InputStreamUser user) Safely and quickly retrieves the first cell as an ASCIIInputStream
from the results of a select query.<ResultType>
ResultTypeexecuteUseFirstAsciiStream
(ReadQuery query, InputStreamUser user, PreparedStatementHandler handler) Safely retrieves the first cell as an ASCIIInputStream
from the results of a customizable select query.<ResultType>
ResultTypeexecuteUseFirstBinaryStream
(ReadQuery query, InputStreamUser user) Safely and quickly retrieves the first cell as a binaryInputStream
from the results of a select query.<ResultType>
ResultTypeexecuteUseFirstBinaryStream
(ReadQuery query, InputStreamUser user, PreparedStatementHandler handler) Safely retrieves the first cell as an binaryInputStream
from the results of a customizable select query.<ResultType>
ResultTypeexecuteUseFirstCharacterStream
(ReadQuery query, ReaderUser user) Safely and quickly retrieves the first cell as a characterReader
from the results of a select query.<ResultType>
ResultTypeexecuteUseFirstCharacterStream
(ReadQuery query, ReaderUser user, PreparedStatementHandler handler) Safely retrieves the first cell as an characterReader
from the results of a customizable select query.boolean
Fetches the next row of a result set without processing it in any way.boolean
fetch
(ResultSet resultSet, DbRowProcessor rowProcessor) Fetches the next row of a result set and processes it through aDbRowProcessor
.boolean
fetch
(ResultSet resultSet, RowProcessor rowProcessor) Convenience alternative tofetch(ResultSet, DbRowProcessor)
that uses a simplifiedRowProcessor
that can be implemented with a lambda.boolean
fetchAll
(ResultSet resultSet, DbRowProcessor rowProcessor) Fetches all the next rows of a result set and processes it through aDbRowProcessor
.boolean
fetchAll
(ResultSet resultSet, RowProcessor rowProcessor) Convenience alternative tofetchAll(ResultSet, DbRowProcessor)
that uses a simplifiedRowProcessor
that can be implemented with a lambda.Obtains aDbConnection
of thisDbQueryManager
'sDatasource
.Retrieves theDatasource
of thisDbQueryManager
.<ResultType>
ResultTypeinTransaction
(TransactionUser user) Ensures that all the instructions that are executed in the providedDbTransactionUser
instance are executed inside a transaction and committed afterwards.void
Convenience method that ensures that all the instructions that are executed in the providedTransactionUserWithoutResult
instance, are executed inside a transaction and committed afterwards.<ResultType>
ResultTypeReserves a database connection for a particular thread for all the instructions that are executed in the providedDbConnectionUser
instance.
-
Constructor Details
-
DbQueryManager
Instantiates a newDbQueryManager
object and ties it to the provided datasource.- Parameters:
datasource
- the datasource that will be used to obtain database connections from- Since:
- 1.0
-
-
Method Details
-
executeUpdate
Safely and quickly executes an update statement. It relies on the wrappedDbStatement.executeUpdate(String)
method, but also automatically closes the statement after its execution.This method is typically used in situations where one static update query needs to be executed without any parametrization or other processing.
Example
var manager = new DbQueryManager(datasource); var count = manager.executeUpdate("INSERT INTO person (name) VALUES ('me')");
- Parameters:
sql
- the sql query that has to be executed- Returns:
- the row count for the executed query
- Throws:
DatabaseException
- seeDbStatement.executeUpdate(String)
- Since:
- 1.0
- See Also:
-
executeUpdate
Safely and quickly executes an update statement. It relies on the wrappedDbStatement.executeUpdate(Query)
method, but also automatically closes the statement after its execution.This method is typically used in situations where one static update query needs to be executed without any parametrization or other processing.
Example
var manager = new DbQueryManager(datasource); var insert = new Insert(datasource) .into("person").field("name", "me"); var count = manager.executeUpdate(insert);
- Parameters:
query
- the query builder instance that needs to be executed- Returns:
- the row count for the executed query
- Throws:
DatabaseException
- seeDbStatement.executeUpdate(Query)
- Since:
- 1.0
- See Also:
-
executeUpdate
Safely execute an update statement. It relies on the wrappedDbPreparedStatement.executeUpdate()
method, but also automatically closes the statement after its execution and allows customization of the prepared statement through an optional instance ofPreparedStatementHandler
.This method is typically used when you need to fully customize a query at runtime, but still want to benefit of a safety net that ensures that the allocated statement will be closed.
Example
var manager = new DbQueryManager(datasource); var insert = new Insert(datasource) .into("person").fieldParameter("name"); final var name = "me"; var count = manager.executeUpdate(insert, statement -> statement.setString("name", name));
- Parameters:
query
- the query builder instance that needs to be executedhandler
- an instance ofPreparedStatementHandler
that will be used to customize the query execution; ornull
if you don't want to customize it at all- Returns:
- the row count for the executed query
- Throws:
DatabaseException
- seeDbPreparedStatement.executeUpdate()
- Since:
- 1.0
- See Also:
-
executeHasResultRows
Safely and quickly verifies if a select query returns any rows. It relies on the wrappedDbResultSet.hasResultRows()
method, but also automatically closes the statement after its execution.Example
var manager = new DbQueryManager(datasource); var select = new Select(datasource) .from("person"); var result = manager.executeHasResultRows(select);
- Parameters:
query
- the query builder instance that needs to be executed- Returns:
true
when rows were returned by the query; orfalse
otherwise- Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.hasResultRows()
- Since:
- 1.0
- See Also:
-
executeHasResultRows
public boolean executeHasResultRows(ReadQuery query, PreparedStatementHandler handler) throws DatabaseException Safely verifies if a customizable select query returns any rows. It relies on the wrappedDbResultSet.hasResultRows()
method, but also automatically closes the statement after its execution and allows customization of the prepared statement through an optional instance ofPreparedStatementHandler
.Example
var manager = new DbQueryManager(datasource); var select = new Select(datasource) .from("person").whereParameter("name", "="); var String name = "you"; var result = manager.executeHasResultRows(select, statement -> statement.setString("name", name));
- Parameters:
query
- the query builder instance that needs to be executedhandler
- an instance ofPreparedStatementHandler
that will be used to customize the query execution- Returns:
true
when rows were returned by the query; orfalse
otherwise- Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.hasResultRows()
- Since:
- 1.0
- See Also:
-
executeGetFirstString
Safely and quickly retrieves the first cell as aString
from the results of a select query. It relies on the wrappedDbResultSet.getFirstString()
method, but also automatically closes the statement after its execution.Example
var manager = new DbQueryManager(datasource); var select = new Select(datasource) .field("name").from("person"); var result = manager.executeGetFirstString(select);
- Parameters:
query
- the query builder instance that needs to be executed- Returns:
- the first
String
in the query's result set - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstString()
- Since:
- 1.0
- See Also:
-
executeGetFirstString
public String executeGetFirstString(ReadQuery query, PreparedStatementHandler handler) throws DatabaseException Safely retrieves the first cell as aString
from the results of a customizable select query. It relies on the wrappedDbResultSet.getFirstString()
method, but also automatically closes the statement after its execution and allows customization of the prepared statement through an optional instance ofPreparedStatementHandler
.Example
var manager = new DbQueryManager(datasource); var select = new Select(datasource) .field("first").from("person").whereParameter("last", "="); final var last = "Smith"; var result = manager.executeGetFirstString(select, statement -> statement.setString("name", name));
- Parameters:
query
- the query builder instance that needs to be executedhandler
- an instance ofPreparedStatementHandler
that will be used to customize the query execution; ornull
if you don't want to customize it at all- Returns:
- the first
String
in the query's result set - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstString()
- Since:
- 1.0
- See Also:
-
executeGetFirstBoolean
Safely and quickly retrieves the first cell as aboolean
from the results of a select query. It relies on the wrappedDbResultSet.getFirstBoolean()
method, but also automatically closes the statement after its execution.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executed- Returns:
- the first
boolean
in the query's result set - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstBoolean()
- Since:
- 1.0
- See Also:
-
executeGetFirstBoolean
public boolean executeGetFirstBoolean(ReadQuery query, PreparedStatementHandler handler) throws DatabaseException Safely retrieves the first cell as aboolean
from the results of a customizable select query. It relies on the wrappedDbResultSet.getFirstBoolean()
method, but also automatically closes the statement after its execution and allows customization of the prepared statement through an optional instance ofPreparedStatementHandler
.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executedhandler
- an instance ofPreparedStatementHandler
that will be used to customize the query execution; ornull
if you don't want to customize it at all- Returns:
- the first
boolean
in the query's result set - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstBoolean()
- Since:
- 1.0
- See Also:
-
executeGetFirstByte
Safely and quickly retrieves the first cell as abyte
from the results of a select query. It relies on the wrappedDbResultSet.getFirstByte()
method, but also automatically closes the statement after its execution.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executed- Returns:
- the first
byte
in the query's result set - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstByte()
- Since:
- 1.0
- See Also:
-
executeGetFirstByte
public byte executeGetFirstByte(ReadQuery query, PreparedStatementHandler handler) throws DatabaseException Safely retrieves the first cell as abyte
from the results of a customizable select query. It relies on the wrappedDbResultSet.getFirstByte()
method, but also automatically closes the statement after its execution and allows customization of the prepared statement through an optional instance ofPreparedStatementHandler
.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executedhandler
- an instance ofPreparedStatementHandler
that will be used to customize the query execution; ornull
if you don't want to customize it at all- Returns:
- the first
byte
in the query's result set - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstByte()
- Since:
- 1.0
- See Also:
-
executeGetFirstShort
Safely and quickly retrieves the first cell as ashort
from the results of a select query. It relies on the wrappedDbResultSet.getFirstShort()
method, but also automatically closes the statement after its execution.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executed- Returns:
- the first
short
in the query's result set - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstShort()
- Since:
- 1.0
- See Also:
-
executeGetFirstShort
public short executeGetFirstShort(ReadQuery query, PreparedStatementHandler handler) throws DatabaseException Safely retrieves the first cell as ashort
from the results of a customizable select query. It relies on the wrappedDbResultSet.getFirstShort()
method, but also automatically closes the statement after its execution and allows customization of the prepared statement through an optional instance ofPreparedStatementHandler
.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executedhandler
- an instance ofPreparedStatementHandler
that will be used to customize the query execution; ornull
if you don't want to customize it at all- Returns:
- the first
short
in the query's result set - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstShort()
- Since:
- 1.0
- See Also:
-
executeGetFirstInt
Safely and quickly retrieves the first cell as aint
from the results of a select query. It relies on the wrappedDbResultSet.getFirstInt()
method, but also automatically closes the statement after its execution.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executed- Returns:
- the first
int
in the query's result set - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstInt()
- Since:
- 1.0
- See Also:
-
executeGetFirstInt
public int executeGetFirstInt(ReadQuery query, PreparedStatementHandler handler) throws DatabaseException Safely retrieves the first cell as aint
from the results of a customizable select query. It relies on the wrappedDbResultSet.getFirstInt()
method, but also automatically closes the statement after its execution and allows customization of the prepared statement through an optional instance ofPreparedStatementHandler
.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executedhandler
- an instance ofPreparedStatementHandler
that will be used to customize the query execution; ornull
if you don't want to customize it at all- Returns:
- the first
int
in the query's result set - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstInt()
- Since:
- 1.0
- See Also:
-
executeGetFirstLong
Safely and quickly retrieves the first cell as along
from the results of a select query. It relies on the wrappedDbResultSet.getFirstLong()
method, but also automatically closes the statement after its execution.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executed- Returns:
- the first
long
in the query's result set - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstLong()
- Since:
- 1.0
- See Also:
-
executeGetFirstLong
public long executeGetFirstLong(ReadQuery query, PreparedStatementHandler handler) throws DatabaseException Safely retrieves the first cell as along
from the results of a customizable select query. It relies on the wrappedDbResultSet.getFirstLong()
method, but also automatically closes the statement after its execution and allows customization of the prepared statement through an optional instance ofPreparedStatementHandler
.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executedhandler
- an instance ofPreparedStatementHandler
that will be used to customize the query execution; ornull
if you don't want to customize it at all- Returns:
- the first
long
in the query's result set - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstLong()
- Since:
- 1.0
- See Also:
-
executeGetFirstFloat
Safely and quickly retrieves the first cell as afloat
from the results of a select query. It relies on the wrappedDbResultSet.getFirstFloat()
method, but also automatically closes the statement after its execution.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executed- Returns:
- the first
float
in the query's result set - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstFloat()
- Since:
- 1.0
- See Also:
-
executeGetFirstFloat
public float executeGetFirstFloat(ReadQuery query, PreparedStatementHandler handler) throws DatabaseException Safely retrieves the first cell as afloat
from the results of a customizable select query. It relies on the wrappedDbResultSet.getFirstFloat()
method, but also automatically closes the statement after its execution and allows customization of the prepared statement through an optional instance ofPreparedStatementHandler
.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executedhandler
- an instance ofPreparedStatementHandler
that will be used to customize the query execution; ornull
if you don't want to customize it at all- Returns:
- the first
float
in the query's result set - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstFloat()
- Since:
- 1.0
- See Also:
-
executeGetFirstDouble
Safely and quickly retrieves the first cell as adouble
from the results of a select query. It relies on the wrappedDbResultSet.getFirstDouble()
method, but also automatically closes the statement after its execution.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executed- Returns:
- the first
double
in the query's result set - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstDouble()
- Since:
- 1.0
- See Also:
-
executeGetFirstDouble
public double executeGetFirstDouble(ReadQuery query, PreparedStatementHandler handler) throws DatabaseException Safely retrieves the first cell as adouble
from the results of a customizable select query. It relies on the wrappedDbResultSet.getFirstDouble()
method, but also automatically closes the statement after its execution and allows customization of the prepared statement through an optional instance ofPreparedStatementHandler
.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executedhandler
- an instance ofPreparedStatementHandler
that will be used to customize the query execution; ornull
if you don't want to customize it at all- Returns:
- the first
double
in the query's result set - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstDouble()
- Since:
- 1.0
- See Also:
-
executeGetFirstBytes
Safely and quickly retrieves the first cell as abyte
array from the results of a select query. It relies on the wrappedDbResultSet.getFirstBytes()
method, but also automatically closes the statement after its execution.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executed- Returns:
- the first
byte
array in the query's result set - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstBytes()
- Since:
- 1.0
- See Also:
-
executeGetFirstBytes
public byte[] executeGetFirstBytes(ReadQuery query, PreparedStatementHandler handler) throws DatabaseException Safely retrieves the first cell as abyte
array from the results of a customizable select query. It relies on the wrappedDbResultSet.getFirstBytes()
method, but also automatically closes the statement after its execution and allows customization of the prepared statement through an optional instance ofPreparedStatementHandler
.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executedhandler
- an instance ofPreparedStatementHandler
that will be used to customize the query execution; ornull
if you don't want to customize it at all- Returns:
- the first
byte
array in the query's result set - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstBytes()
- Since:
- 1.0
- See Also:
-
executeGetFirstDate
Safely and quickly retrieves the first cell as a sqlDate
from the results of a select query. It relies on the wrappedDbResultSet.getFirstDate()
method, but also automatically closes the statement after its execution.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executed- Returns:
- the first sql
Date
in the query's result set - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstDate()
- Since:
- 1.0
- See Also:
-
executeGetFirstDate
public Date executeGetFirstDate(ReadQuery query, PreparedStatementHandler handler) throws DatabaseException Safely retrieves the first cell as a sqlDate
from the results of a customizable select query. It relies on the wrappedDbResultSet.getFirstDate()
method, but also automatically closes the statement after its execution and allows customization of the prepared statement through an optional instance ofPreparedStatementHandler
.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executedhandler
- an instance ofPreparedStatementHandler
that will be used to customize the query execution; ornull
if you don't want to customize it at all- Returns:
- the first sql
Date
in the query's result set - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstDate()
- Since:
- 1.0
- See Also:
-
executeGetFirstDate
Safely and quickly retrieves the first cell as a sqlDate
from the results of a select query. It relies on the wrappedDbResultSet.getFirstDate(Calendar)
method, but also automatically closes the statement after its execution.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executedcal
- theCalendar
object to use in constructing the date- Returns:
- the first sql
Date
in the query's result set - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstDate(Calendar)
- Since:
- 1.0
- See Also:
-
executeGetFirstDate
public Date executeGetFirstDate(ReadQuery query, Calendar cal, PreparedStatementHandler handler) throws DatabaseException Safely retrieves the first cell as a sqlDate
from the results of a customizable select query. It relies on the wrappedDbResultSet.getFirstDate(Calendar)
method, but also automatically closes the statement after its execution and allows customization of the prepared statement through an optional instance ofPreparedStatementHandler
.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executedcal
- theCalendar
object to use in constructing the datehandler
- an instance ofPreparedStatementHandler
that will be used to customize the query execution; ornull
if you don't want to customize it at all- Returns:
- the first sql
Date
in the query's result set - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstDate(Calendar)
- Since:
- 1.0
- See Also:
-
executeGetFirstTime
Safely and quickly retrieves the first cell as a sqlTime
from the results of a select query. It relies on the wrappedDbResultSet.getFirstTime()
method, but also automatically closes the statement after its execution.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executed- Returns:
- the first sql
Time
in the query's result set - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstTime()
- Since:
- 1.0
- See Also:
-
executeGetFirstTime
public Time executeGetFirstTime(ReadQuery query, PreparedStatementHandler handler) throws DatabaseException Safely retrieves the first cell as a sqlTime
from the results of a customizable select query. It relies on the wrappedDbResultSet.getFirstTime()
method, but also automatically closes the statement after its execution and allows customization of the prepared statement through an optional instance ofPreparedStatementHandler
.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executedhandler
- an instance ofPreparedStatementHandler
that will be used to customize the query execution; ornull
if you don't want to customize it at all- Returns:
- the first sql
Time
in the query's result set - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstTime()
- Since:
- 1.0
- See Also:
-
executeGetFirstTime
Safely and quickly retrieves the first cell as a sqlTime
from the results of a select query. It relies on the wrappedDbResultSet.getFirstTime(Calendar)
method, but also automatically closes the statement after its execution.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executedcal
- theCalendar
object to use in constructing the time- Returns:
- the first sql
Time
in the query's result set - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstTime(Calendar)
- Since:
- 1.0
- See Also:
-
executeGetFirstTime
public Time executeGetFirstTime(ReadQuery query, Calendar cal, PreparedStatementHandler handler) throws DatabaseException Safely retrieves the first cell as a sqlTime
from the results of a customizable select query. It relies on the wrappedDbResultSet.getFirstTime(Calendar)
method, but also automatically closes the statement after its execution and allows customization of the prepared statement through an optional instance ofPreparedStatementHandler
.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executedcal
- theCalendar
object to use in constructing the timehandler
- an instance ofPreparedStatementHandler
that will be used to customize the query execution; ornull
if you don't want to customize it at all- Returns:
- the first sql
Time
in the query's result set - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstTime(Calendar)
- Since:
- 1.0
- See Also:
-
executeGetFirstTimestamp
Safely and quickly retrieves the first cell as a sqlTimestamp
from the results of a select query. It relies on the wrappedDbResultSet.getFirstTimestamp()
method, but also automatically closes the statement after its execution.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executed- Returns:
- the first sql
Timestamp
in the query's result set - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstTimestamp()
- Since:
- 1.0
- See Also:
-
executeGetFirstTimestamp
public Timestamp executeGetFirstTimestamp(ReadQuery query, PreparedStatementHandler handler) throws DatabaseException Safely retrieves the first cell as a sqlTimestamp
from the results of a customizable select query. It relies on the wrappedDbResultSet.getFirstTimestamp()
method, but also automatically closes the statement after its execution and allows customization of the prepared statement through an optional instance ofPreparedStatementHandler
.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executedhandler
- an instance ofPreparedStatementHandler
that will be used to customize the query execution; ornull
if you don't want to customize it at all- Returns:
- the first sql
Timestamp
in the query's result set - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstTimestamp()
- Since:
- 1.0
- See Also:
-
executeGetFirstTimestamp
Safely and quickly retrieves the first cell as a sqlTimestamp
from the results of a select query. It relies on the wrappedDbResultSet.getFirstTimestamp(Calendar)
method, but also automatically closes the statement after its execution.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executedcal
- theCalendar
object to use in constructing the timestamp- Returns:
- the first sql
Timestamp
in the query's result set - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstTimestamp(Calendar)
- Since:
- 1.0
- See Also:
-
executeGetFirstTimestamp
public Timestamp executeGetFirstTimestamp(ReadQuery query, Calendar cal, PreparedStatementHandler handler) throws DatabaseException Safely retrieves the first cell as a sqlTimestamp
from the results of a customizable select query. It relies on the wrappedDbResultSet.getFirstTimestamp(Calendar)
method, but also automatically closes the statement after its execution and allows customization of the prepared statement through an optional instance ofPreparedStatementHandler
.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executedcal
- theCalendar
object to use in constructing the timestamphandler
- an instance ofPreparedStatementHandler
that will be used to customize the query execution; ornull
if you don't want to customize it at all- Returns:
- the first sql
Timestamp
in the query's result set - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstTimestamp(Calendar)
- Since:
- 1.0
- See Also:
-
executeUseFirstAsciiStream
public <ResultType> ResultType executeUseFirstAsciiStream(ReadQuery query, InputStreamUser user) throws DatabaseException, InnerClassException Safely and quickly retrieves the first cell as an ASCIIInputStream
from the results of a select query. It relies on the wrappedDbResultSet.getFirstAsciiStream()
method, but also automatically closes the statement after its execution.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executeduser
- an instance ofInputStreamUser
that contains the logic that will be executed with this stream- Returns:
- the return value from the
useInputStream
method of the providedInputStreamUser
instance - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstAsciiStream()
InnerClassException
- when errors occurs inside theInputStreamUser
- Since:
- 1.0
- See Also:
-
executeUseFirstAsciiStream
public <ResultType> ResultType executeUseFirstAsciiStream(ReadQuery query, InputStreamUser user, PreparedStatementHandler handler) throws DatabaseException, InnerClassException Safely retrieves the first cell as an ASCIIInputStream
from the results of a customizable select query. It relies on the wrappedDbResultSet.getFirstAsciiStream()
method, but also automatically closes the statement after its execution and allows customization of the prepared statement through an optional instance ofPreparedStatementHandler
.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executeduser
- an instance ofInputStreamUser
that contains the logic that will be executed with this streamhandler
- an instance ofPreparedStatementHandler
that will be used to customize the query execution; ornull
if you don't want to customize it at all- Returns:
- the return value from the
useInputStream
method of the providedInputStreamUser
instance - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstAsciiStream()
InnerClassException
- when errors occurs inside theInputStreamUser
- Since:
- 1.0
- See Also:
-
executeUseFirstCharacterStream
public <ResultType> ResultType executeUseFirstCharacterStream(ReadQuery query, ReaderUser user) throws DatabaseException, InnerClassException Safely and quickly retrieves the first cell as a characterReader
from the results of a select query. It relies on the wrappedDbResultSet.getFirstCharacterStream()
method, but also automatically closes the statement after its execution.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executeduser
- an instance ofReaderUser
that contains the logic that will be executed with this reader- Returns:
- the return value from the
useReader
method of the providedReaderUser
instance - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstCharacterStream()
InnerClassException
- when errors occurs inside theReaderUser
- Since:
- 1.0
- See Also:
-
executeUseFirstCharacterStream
public <ResultType> ResultType executeUseFirstCharacterStream(ReadQuery query, ReaderUser user, PreparedStatementHandler handler) throws DatabaseException, InnerClassException Safely retrieves the first cell as an characterReader
from the results of a customizable select query. It relies on the wrappedDbResultSet.getFirstCharacterStream()
method, but also automatically closes the statement after its execution and allows customization of the prepared statement through an optional instance ofPreparedStatementHandler
.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executeduser
- an instance ofReaderUser
that contains the logic that will be executed with this readerhandler
- an instance ofPreparedStatementHandler
that will be used to customize the query execution; ornull
if you don't want to customize it at all- Returns:
- the return value from the
useReader
method of the providedReaderUser
instance - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstCharacterStream()
InnerClassException
- when errors occurs inside theReaderUser
- Since:
- 1.0
- See Also:
-
executeUseFirstBinaryStream
public <ResultType> ResultType executeUseFirstBinaryStream(ReadQuery query, InputStreamUser user) throws DatabaseException, InnerClassException Safely and quickly retrieves the first cell as a binaryInputStream
from the results of a select query. It relies on the wrappedDbResultSet.getFirstBinaryStream()
method, but also automatically closes the statement after its execution.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executeduser
- an instance ofInputStreamUser
that contains the logic that will be executed with this stream- Returns:
- the return value from the
useInputStream
method of the providedInputStreamUser
instance - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstBinaryStream()
InnerClassException
- when errors occurs inside theInputStreamUser
- Since:
- 1.0
- See Also:
-
executeUseFirstBinaryStream
public <ResultType> ResultType executeUseFirstBinaryStream(ReadQuery query, InputStreamUser user, PreparedStatementHandler handler) throws DatabaseException, InnerClassException Safely retrieves the first cell as an binaryInputStream
from the results of a customizable select query. It relies on the wrappedDbResultSet.getFirstBinaryStream()
method, but also automatically closes the statement after its execution and allows customization of the prepared statement through an optional instance ofPreparedStatementHandler
.Refer to
executeGetFirstString
for an example code snippet, it's 100% analogous.- Parameters:
query
- the query builder instance that needs to be executeduser
- an instance ofInputStreamUser
that contains the logic that will be executed with this streamhandler
- an instance ofPreparedStatementHandler
that will be used to customize the query execution; ornull
if you don't want to customize it at all- Returns:
- the return value from the
useInputStream
method of the providedInputStreamUser
instance - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andDbResultSet.getFirstBinaryStream()
InnerClassException
- when errors occurs inside theInputStreamUser
- Since:
- 1.0
- See Also:
-
executeFetchFirst
public boolean executeFetchFirst(ReadQuery query, DbRowProcessor rowProcessor) throws DatabaseException Safely and quickly fetches the first row from the results of a select query. It relies on the wrappedfetch(ResultSet, DbRowProcessor)
method, but automatically closes the statement after its execution.Example
var manager = new DbQueryManager(datasource); var select = new Select(datasource) .from("person").where("name", "=", "me"); var processor = new YourProcessor(); var result = manager.executeFetchFirst(select, resultSet -> resultSet.getString("name"));
- Parameters:
query
- the query builder instance that needs to be executedrowProcessor
- aDbRowProcessor
instance, if it'snull
no processing will be performed on the fetched row- Returns:
true
if a row was retrieved; orfalse
if there are no more rows .- Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andfetch(ResultSet, DbRowProcessor)
- Since:
- 1.0
- See Also:
-
executeFetchFirst
public boolean executeFetchFirst(ReadQuery query, RowProcessor rowProcessor) throws DatabaseException Convenience alternative toexecuteFetchFirst(ReadQuery, DbRowProcessor)
that uses a simplifiedRowProcessor
that can be implemented with a lambda.- Parameters:
query
- the query builder instance that needs to be executedrowProcessor
- aRowProcessor
instance, if it'snull
no processing will be performed on the fetched row- Returns:
true
if a row was retrieved; orfalse
if there are no more rows .- Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andfetchAll(ResultSet, DbRowProcessor)
- Since:
- 1.0
- See Also:
-
executeFetchFirst
public boolean executeFetchFirst(ReadQuery query, DbRowProcessor rowProcessor, PreparedStatementHandler handler) throws DatabaseException Safely fetches the first row from the results of a customizable select query. It relies on the wrappedfetch(ResultSet, DbRowProcessor)
method, but also automatically closes the statement after its execution and allows customization of the prepared statement through an optional instance ofPreparedStatementHandler
.Example
var manager = new DbQueryManager(datasource); var select = new Select(datasource) .from("person").whereParameter("name", "="); var processor = new YourProcessor(); final var name = "you"; var result = manager.executeFetchFirst(select, resultSet -> resultSet.getString("name"), statement -> statement.setString("name", name));
- Parameters:
query
- the query builder instance that needs to be executedrowProcessor
- aDbRowProcessor
instance, if it'snull
no processing will be performed on the fetched rowhandler
- an instance ofPreparedStatementHandler
that will be used to customize the query execution; ornull
if you don't want to customize it at all- Returns:
true
if a row was retrieved; orfalse
if there are no more rows .- Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andfetch(ResultSet, DbRowProcessor)
- Since:
- 1.0
- See Also:
-
executeFetchFirst
public boolean executeFetchFirst(ReadQuery query, RowProcessor rowProcessor, PreparedStatementHandler handler) Convenience alternative toexecuteFetchFirst(ReadQuery, DbRowProcessor, PreparedStatementHandler)
that uses a simplifiedRowProcessor
that can be implemented with a lambda.- Parameters:
query
- the query builder instance that needs to be executedrowProcessor
- aRowProcessor
instance, if it'snull
no processing will be performed on the fetched rowhandler
- an instance ofPreparedStatementHandler
that will be used to customize the query execution; ornull
if you don't want to customize it at all- Returns:
true
if a row was retrieved; orfalse
if there are no more rows .- Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andfetchAll(ResultSet, DbRowProcessor)
- Since:
- 1.0
- See Also:
-
executeFetchFirstBean
public <BeanType> BeanType executeFetchFirstBean(ReadQuery query, Class<BeanType> beanClass) throws DatabaseException Safely and quickly fetches the first bean instance from the results of a select query. It relies on the wrappedexecuteFetchFirst(ReadQuery, DbRowProcessor)
method, but automatically uses an appropriateDbBeanFetcher
instance and returns the resulting bean.Example
var manager = new DbQueryManager(datasource); var select = new Select(datasource) .from("person").fields(Person.class); var person = manager.executeFetchFirstBean(select, Person.class);
- Parameters:
query
- the query builder instance that needs to be executedbeanClass
- the class of the bean- Returns:
- and instance of the bean if it was retrieved successfully; or
null
if it couldn't be found - Throws:
DatabaseException
- seeDbBeanFetcher
andexecuteFetchFirst(ReadQuery, DbRowProcessor)
- Since:
- 1.0
- See Also:
-
executeFetchFirstBean
public <BeanType> BeanType executeFetchFirstBean(ReadQuery query, Class<BeanType> beanClass, PreparedStatementHandler handler) throws DatabaseException Safely fetches the first bean instance from the results of a customizable select query. It relies on the wrappedexecuteFetchFirst(ReadQuery, DbRowProcessor)
method, but automatically uses an appropriateDbBeanFetcher
instance, returns the resulting bean and allows customization of the prepared statement through an optional instance ofPreparedStatementHandler
.Example
var manager = new DbQueryManager(datasource); var select = new Select(datasource) .from("person").fields(Person.class).whereParameter("name", "="); final var name = "you"; var person = manager.executeFetchFirstBean(select, Person.class, statement -> statement.setString("name", name));
- Parameters:
query
- the query builder instance that needs to be executedbeanClass
- the class of the beanhandler
- an instance ofPreparedStatementHandler
that will be used to customize the query execution; ornull
if you don't want to customize it at all- Returns:
- and instance of the bean if it was retrieved successfully; or
null
if it couldn't be found - Throws:
DatabaseException
- seeDbBeanFetcher
andexecuteFetchFirst(ReadQuery, DbRowProcessor)
- Since:
- 1.0
- See Also:
-
executeFetchAll
public boolean executeFetchAll(ReadQuery query, DbRowProcessor rowProcessor) throws DatabaseException Safely and quickly fetches all the rows from the results of a select query. It relies on the wrappedfetchAll(ResultSet, DbRowProcessor)
method, but automatically closes the statement after its execution.Example
var manager = new DbQueryManager(datasource); var select = new Select(datasource) .from("person").where("gender", "=", "m"); var result = manager.executeFetchAll(select, resultSet -> resultSet.getString("name"));
- Parameters:
query
- the query builder instance that needs to be executedrowProcessor
- aDbRowProcessor
instance, if it'snull
no processing will be performed on the fetched rows- Returns:
true
if rows were retrieved; orfalse
if there are no more rows .- Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andfetchAll(ResultSet, DbRowProcessor)
- Since:
- 1.0
- See Also:
-
executeFetchAll
Convenience alternative toexecuteFetchAll(ReadQuery, DbRowProcessor)
that uses a simplifiedRowProcessor
that can be implemented with a lambda.- Parameters:
query
- the query builder instance that needs to be executedrowProcessor
- aRowProcessor
instance, if it'snull
no processing will be performed on the fetched rows- Returns:
true
if a row was retrieved; orfalse
if there are no more rows .- Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andfetchAll(ResultSet, DbRowProcessor)
- Since:
- 1.0
- See Also:
-
executeFetchAll
public boolean executeFetchAll(ReadQuery query, DbRowProcessor rowProcessor, PreparedStatementHandler handler) throws DatabaseException Safely fetches all the rows from the results of a customizable select query. It relies on the wrappedfetchAll(ResultSet, DbRowProcessor)
method, but also automatically closes the statement after its execution and allows customization of the prepared statement through an optional instance ofPreparedStatementHandler
.Example
var manager = new DbQueryManager(datasource); var select = new Select(datasource) .from("person").whereParameter("gender", "="); final String name = "m"; var result = manager.executeFetchAll(select, resultSet -> resultSet.getString("name"), statement -> statement.setString("name", name));
- Parameters:
query
- the query builder instance that needs to be executedrowProcessor
- aDbRowProcessor
instance, if it'snull
no processing will be performed on the fetched rowhandler
- an instance ofPreparedStatementHandler
that will be used to customize the query execution; ornull
if you don't want to customize it at all- Returns:
true
if rows were retrieved; orfalse
if there are no more rows .- Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andfetchAll(ResultSet, DbRowProcessor)
- Since:
- 1.0
- See Also:
-
executeFetchAll
public boolean executeFetchAll(ReadQuery query, RowProcessor rowProcessor, PreparedStatementHandler handler) throws DatabaseException Convenience alternative toexecuteFetchAll(ReadQuery, DbRowProcessor, PreparedStatementHandler)
that uses a simplifiedRowProcessor
that can be implemented with a lambda.- Parameters:
query
- the query builder instance that needs to be executedrowProcessor
- aRowProcessor
instance, if it'snull
no processing will be performed on the fetched rowshandler
- an instance ofPreparedStatementHandler
that will be used to customize the query execution; ornull
if you don't want to customize it at all- Returns:
true
if a row was retrieved; orfalse
if there are no more rows .- Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
andfetchAll(ResultSet, DbRowProcessor)
- Since:
- 1.0
- See Also:
-
executeFetchAllBeans
public <BeanType> List<BeanType> executeFetchAllBeans(ReadQuery query, Class<BeanType> beanClass) throws DatabaseException Safely and quickly fetches the all the bean instances from the results of a select query. It relies on the wrappedexecuteFetchAll(ReadQuery, DbRowProcessor)
method, but automatically uses an appropriateDbBeanFetcher
instance and returns the results.Example
var manager = new DbQueryManager(datasource); var select = new Select(datasource) .from("person").fields(Person.class).where("gender", "=", "m"); var people = manager.executeFetchAllBeans(select, Person.class);
- Parameters:
query
- the query builder instance that needs to be executedbeanClass
- the class of the bean- Returns:
a List instance with all the beans, the list is empty if no beans could be returned
- Throws:
DatabaseException
- seeDbBeanFetcher
andexecuteFetchAll(ReadQuery, DbRowProcessor)
- Since:
- 1.0
- See Also:
-
executeFetchAllBeans
public <BeanType> List<BeanType> executeFetchAllBeans(ReadQuery query, Class<BeanType> beanClass, PreparedStatementHandler handler) throws DatabaseException Safely fetches the all the bean instances from the results of a customizable select query. It relies on the wrappedexecuteFetchAll(ReadQuery, DbRowProcessor)
method, but automatically uses an appropriateDbBeanFetcher
instance, returns the results and allows customization of the prepared statement through an optional instance ofPreparedStatementHandler
.Example
var manager = new DbQueryManager(datasource); var select = new Select(datasource) .from("person").fields(Person.class).whereParameter("gender", "="); final String name = "m"; var people = manager.executeFetchAllBeans(select, Person.class, statement -> statement.setString("name", name));
- Parameters:
query
- the query builder instance that needs to be executedbeanClass
- the class of the beanhandler
- an instance ofPreparedStatementHandler
that will be used to customize the query execution; ornull
if you don't want to customize it at all- Returns:
a List instance with all the beans, the list is empty if no beans could be returned
- Throws:
DatabaseException
- seeDbBeanFetcher
andexecuteFetchAll(ReadQuery, DbRowProcessor)
- Since:
- 1.0
- See Also:
-
executeQuery
public <ResultType> ResultType executeQuery(ReadQuery query, PreparedStatementHandler handler) throws DatabaseException Executes a customizable select statement. It relies on the wrappedDbPreparedStatement.executeQuery()
method, but also automatically closes the statement after its execution and allows complete customization of the prepared statement through an optional instance ofPreparedStatementHandler
.This method is typically used when you need to fully customize a query at runtime, but still want to benefit of a safety net that ensures that the allocated statement will be closed. Often another more specialized method in this class will already serve your needs, so be sure to verify that you actually need to intervene on every front.
Example
var manager = new DbQueryManager(datasource); var select = new Select(datasource); select .field("first") .field("last") .from("person") .whereParameter("name", "="); final var name = "you"; String result = (String)manager.executeQuery(select, new DbPreparedStatementHandler() { public void setParameters(DbPreparedStatement statement) { statement .setString("name", name); } public Object concludeResults(DbResultSet result set) throws SQLException { return result set.getString("first")+" "+result set.getString("last"); } });
- Parameters:
query
- the query builder instance that needs to be executedhandler
- an instance ofPreparedStatementHandler
that will be used to customize the query execution; ornull
if you don't want to customize it at all- Returns:
- the object that was returned by the overridden
concludeResults
method; ornull
if this method wasn't overridden - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
- Since:
- 1.0
- See Also:
-
executeResultQuery
public <ResultType> ResultType executeResultQuery(ReadQuery query, DbResultSetHandler handler) throws DatabaseException Executes a select statement and handle the results in a custom fashion. It relies on the wrappedDbPreparedStatement.executeQuery()
method, but also automatically closes the statement after its execution and allows interaction with the result set through an optional instance ofDbResultSetHandler
.This method is typically used when you need to interact with the results of a query, but still want to benefit of a safety net that ensures that the allocated statement will be closed. Often another more specialized method in this class will already serve your needs, so be sure to verify that there isn't another one that's better suited.
Example
var manager = new DbQueryManager(datasource); var select = new Select(datasource); select .field("first") .field("last") .from("person"); String result = (String)manager.executeResultQuery(select, new DbResultSetHandler() { public Object concludeResults(DbResultSet result set) throws SQLException { return result set.getString("first")+" "+result set.getString("last"); } });
- Parameters:
query
- the query builder instance that needs to be executedhandler
- an instance of
that will be used to handle the results of the query execution; orDbResultSetHandler
null
if you don't want to customize it at all- Returns:
- the object that was returned by the overridden
concludeResults
method; ornull
if this method wasn't overridden - Throws:
DatabaseException
- seeDbPreparedStatement.executeQuery()
- Since:
- 1.0
- See Also:
-
reserveConnection
public <ResultType> ResultType reserveConnection(DbConnectionUser user) throws InnerClassException, DatabaseException Reserves a database connection for a particular thread for all the instructions that are executed in the providedDbConnectionUser
instance.This is typically used to ensure that a series of operations is done with the same connection, even though a database pool is used in the background.
Example
Person person; final var store_data = new Insert(datasource).into("person").fields(person); final var get_last_id = new Select(datasource).from("person").field("LAST_INSERT_ID()"); final var manager = new DbQueryManager(datasource); int id = ((Integer)manager.reserveConnection(new DbConnectionUser() { public Integer useConnection(DbConnection connection) { manager.executeUpdate(store_data); return new Integer(manager.executeGetFirstInt(get_last_id)); } })).intValue();
- Parameters:
user
- an instance ofDbConnectionUser
that contains the logic that will be executed- Returns:
- the return value from the
useConnection
method of the providedDbConnectionUser
instance - Throws:
DatabaseException
- when errors occurs during the reservation of a connection for this threadInnerClassException
- when errors occurs inside theDbConnectionUser
- Since:
- 1.0
- See Also:
-
inTransaction
public <ResultType> ResultType inTransaction(TransactionUser user) throws InnerClassException, DatabaseException Ensures that all the instructions that are executed in the providedDbTransactionUser
instance are executed inside a transaction and committed afterwards. This doesn't mean that a new transaction will always be created. If a transaction is already active, it will simply be re-used. The commit will also only be take place if a new transaction has actually been started, otherwise it's the responsibility of the enclosing code to execute the commit. If a runtime exception occurs during the execution and a new transaction has been started beforehand, it will be automatically rolled back.If you need to explicitly roll back an active transaction, use the
rollback
method of theDbTransactionUser
class. If you use a regular rollback method, it's possible that you're inside a nested transaction executed and that after the rollback, other logic continues to be executed outside the transaction. Using the correctrollback
method, stops the execution of the activeDbTransactionUser
and breaks out of any number of them nesting.It's recommended to always use transactions through this method since it ensures that transactional code can be re-used and enclosed in other transactional code. Correctly using the regular transaction-related methods requires great care and planning and often results in error-prone and not reusable code.
Example
final var insert = new Insert(mDatasource).into("valuelist").field("value", 232); final var manager = new DbQueryManager(datasource); manager.inTransaction(() -> { manager.executeUpdate(insert); manager.executeUpdate(insert); });
- Parameters:
user
- an instance ofTransactionUser
that contains the logic that will be executed- Returns:
- the return value from the
useTransaction
method of the providedDbTransactionUser
instance - Throws:
DatabaseException
- when errors occurs during the handling of the transactionInnerClassException
- when errors occurs inside theDbTransactionUser
- Since:
- 1.0
- See Also:
-
inTransaction
public void inTransaction(TransactionUserWithoutResult user) throws InnerClassException, DatabaseException Convenience method that ensures that all the instructions that are executed in the providedTransactionUserWithoutResult
instance, are executed inside a transaction and committed afterwards.Everything of
inTransaction(TransactionUser)
applies but instead of requiring a result to be returned, it's assumed that none will, making your code cleaner.- Parameters:
user
- an instance ofTransactionUserWithoutResult
that contains the logic that will be executed- Throws:
DatabaseException
- when errors occurs during the handling of the transactionInnerClassException
- when errors occurs inside theDbTransactionUser
- Since:
- 1.0
- See Also:
-
executeQuery
Executes a query statement in a connection of thisDbQueryManager
'sDatasource
. Functions exactly as the wrappedDbStatement.executeQuery(ReadQuery)
method.Note that the statement will not be automatically closed since using this method implies that you still have to work with the result set.
- Parameters:
query
- the query builder instance that should be executed- Returns:
- the statement that has been executed
- Throws:
DatabaseException
- seeDbStatement.executeQuery(ReadQuery)
- Since:
- 1.0
- See Also:
-
fetch
Fetches the next row of a result set without processing it in any way.- Parameters:
resultSet
- a validResultSet
instance- Returns:
true
if a new row was retrieved; orfalse
if there are no more rows .- Throws:
DatabaseException
- when an error occurred during the fetch of the next row in the result set- Since:
- 1.0
- See Also:
-
fetch
Fetches the next row of a result set and processes it through aDbRowProcessor
.- Parameters:
resultSet
- a validResultSet
instancerowProcessor
- aDbRowProcessor
instance, if it'snull
no processing will be performed on the fetched row- Returns:
true
if a new row was retrieved; orfalse
if there are no more rows .- Throws:
DatabaseException
- when an error occurred during the fetch of the next row in the result set- Since:
- 1.0
- See Also:
-
fetch
Convenience alternative tofetch(ResultSet, DbRowProcessor)
that uses a simplifiedRowProcessor
that can be implemented with a lambda.- Parameters:
resultSet
- a validResultSet
instancerowProcessor
- aRowProcessor
instance, if it'snull
no processing will be performed on the fetched row- Returns:
true
if a new row was retrieved; orfalse
if there are no more rows .- Throws:
DatabaseException
- when an error occurred during the fetch of the next row in the result set- Since:
- 1.0
- See Also:
-
fetchAll
Fetches all the next rows of a result set and processes it through aDbRowProcessor
.- Parameters:
resultSet
- a validResultSet
instancerowProcessor
- aDbRowProcessor
instance, if it'snull
no processing will be performed on the fetched rows- Returns:
true
if rows were fetched; orfalse
if the result set contained no rows.- Throws:
DatabaseException
- when an error occurred during the fetch of the next rows in the result set- Since:
- 1.0
- See Also:
-
fetchAll
Convenience alternative tofetchAll(ResultSet, DbRowProcessor)
that uses a simplifiedRowProcessor
that can be implemented with a lambda.- Parameters:
resultSet
- a validResultSet
instancerowProcessor
- aRowProcessor
instance, if it'snull
no processing will be performed on the fetched rows- Returns:
true
if rows were fetched; orfalse
if the result set contained no rows.- Throws:
DatabaseException
- when an error occurred during the fetch of the next rows in the result set- Since:
- 1.0
- See Also:
-
getConnection
Obtains aDbConnection
of thisDbQueryManager
'sDatasource
. Functions exactly as the wrappedDatasource.getConnection()
method.- Returns:
- the requested
DbConnection
- Throws:
DatabaseException
- seeDatasource.getConnection()
- Since:
- 1.0
- See Also:
-
getDatasource
Retrieves theDatasource
of thisDbQueryManager
.- Returns:
- the requested
Datasource
- Since:
- 1.0
-
clone
Simply clones the instance with the default clone method. This creates a shallow copy of all fields and the clone will in fact just be another reference to the same underlying data. The independence of each cloned instance is consciously not respected since they rely on resources that can't be cloned.
-