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
ConstructorsConstructorDescriptionDbQueryManager(Datasource datasource) Instantiates a newDbQueryManagerobject and ties it to the provided datasource. -
Method Summary
Modifier and TypeMethodDescriptionclone()Simply clones the instance with the default clone method.booleanexecuteFetchAll(ReadQuery query, DbRowProcessor rowProcessor) Safely and quickly fetches all the rows from the results of a select query.booleanexecuteFetchAll(ReadQuery query, DbRowProcessor rowProcessor, PreparedStatementHandler handler) Safely fetches all the rows from the results of a customizable select query.booleanexecuteFetchAll(ReadQuery query, RowProcessor rowProcessor) Convenience alternative toexecuteFetchAll(ReadQuery, DbRowProcessor)that uses a simplifiedRowProcessorthat can be implemented with a lambda.booleanexecuteFetchAll(ReadQuery query, RowProcessor rowProcessor, PreparedStatementHandler handler) Convenience alternative toexecuteFetchAll(ReadQuery, DbRowProcessor, PreparedStatementHandler)that uses a simplifiedRowProcessorthat 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.booleanexecuteFetchFirst(ReadQuery query, DbRowProcessor rowProcessor) Safely and quickly fetches the first row from the results of a select query.booleanexecuteFetchFirst(ReadQuery query, DbRowProcessor rowProcessor, PreparedStatementHandler handler) Safely fetches the first row from the results of a customizable select query.booleanexecuteFetchFirst(ReadQuery query, RowProcessor rowProcessor) Convenience alternative toexecuteFetchFirst(ReadQuery, DbRowProcessor)that uses a simplifiedRowProcessorthat can be implemented with a lambda.booleanexecuteFetchFirst(ReadQuery query, RowProcessor rowProcessor, PreparedStatementHandler handler) Convenience alternative toexecuteFetchFirst(ReadQuery, DbRowProcessor, PreparedStatementHandler)that uses a simplifiedRowProcessorthat can be implemented with a lambda.<BeanType> BeanTypeexecuteFetchFirstBean(ReadQuery query, Class<BeanType> beanClass) Safely and quickly fetches the first bean instance from the results of a select query.<BeanType> BeanTypeexecuteFetchFirstBean(ReadQuery query, Class<BeanType> beanClass, PreparedStatementHandler handler) Safely fetches the first bean instance from the results of a customizable select query.booleanexecuteGetFirstBoolean(ReadQuery query) Safely and quickly retrieves the first cell as abooleanfrom the results of a select query.booleanexecuteGetFirstBoolean(ReadQuery query, PreparedStatementHandler handler) Safely retrieves the first cell as abooleanfrom the results of a customizable select query.byteexecuteGetFirstByte(ReadQuery query) Safely and quickly retrieves the first cell as abytefrom the results of a select query.byteexecuteGetFirstByte(ReadQuery query, PreparedStatementHandler handler) Safely retrieves the first cell as abytefrom the results of a customizable select query.byte[]executeGetFirstBytes(ReadQuery query) Safely and quickly retrieves the first cell as abytearray from the results of a select query.byte[]executeGetFirstBytes(ReadQuery query, PreparedStatementHandler handler) Safely retrieves the first cell as abytearray from the results of a customizable select query.executeGetFirstDate(ReadQuery query) Safely and quickly retrieves the first cell as a sqlDatefrom the results of a select query.executeGetFirstDate(ReadQuery query, Calendar cal) Safely and quickly retrieves the first cell as a sqlDatefrom the results of a select query.executeGetFirstDate(ReadQuery query, Calendar cal, PreparedStatementHandler handler) Safely retrieves the first cell as a sqlDatefrom the results of a customizable select query.executeGetFirstDate(ReadQuery query, PreparedStatementHandler handler) Safely retrieves the first cell as a sqlDatefrom the results of a customizable select query.doubleexecuteGetFirstDouble(ReadQuery query) Safely and quickly retrieves the first cell as adoublefrom the results of a select query.doubleexecuteGetFirstDouble(ReadQuery query, PreparedStatementHandler handler) Safely retrieves the first cell as adoublefrom the results of a customizable select query.floatexecuteGetFirstFloat(ReadQuery query) Safely and quickly retrieves the first cell as afloatfrom the results of a select query.floatexecuteGetFirstFloat(ReadQuery query, PreparedStatementHandler handler) Safely retrieves the first cell as afloatfrom the results of a customizable select query.intexecuteGetFirstInt(ReadQuery query) Safely and quickly retrieves the first cell as aintfrom the results of a select query.intexecuteGetFirstInt(ReadQuery query, PreparedStatementHandler handler) Safely retrieves the first cell as aintfrom the results of a customizable select query.longexecuteGetFirstLong(ReadQuery query) Safely and quickly retrieves the first cell as alongfrom the results of a select query.longexecuteGetFirstLong(ReadQuery query, PreparedStatementHandler handler) Safely retrieves the first cell as alongfrom the results of a customizable select query.shortexecuteGetFirstShort(ReadQuery query) Safely and quickly retrieves the first cell as ashortfrom the results of a select query.shortexecuteGetFirstShort(ReadQuery query, PreparedStatementHandler handler) Safely retrieves the first cell as ashortfrom the results of a customizable select query.executeGetFirstString(ReadQuery query) Safely and quickly retrieves the first cell as aStringfrom the results of a select query.executeGetFirstString(ReadQuery query, PreparedStatementHandler handler) Safely retrieves the first cell as aStringfrom the results of a customizable select query.executeGetFirstTime(ReadQuery query) Safely and quickly retrieves the first cell as a sqlTimefrom the results of a select query.executeGetFirstTime(ReadQuery query, Calendar cal) Safely and quickly retrieves the first cell as a sqlTimefrom the results of a select query.executeGetFirstTime(ReadQuery query, Calendar cal, PreparedStatementHandler handler) Safely retrieves the first cell as a sqlTimefrom the results of a customizable select query.executeGetFirstTime(ReadQuery query, PreparedStatementHandler handler) Safely retrieves the first cell as a sqlTimefrom the results of a customizable select query.Safely and quickly retrieves the first cell as a sqlTimestampfrom the results of a select query.executeGetFirstTimestamp(ReadQuery query, Calendar cal) Safely and quickly retrieves the first cell as a sqlTimestampfrom the results of a select query.executeGetFirstTimestamp(ReadQuery query, Calendar cal, PreparedStatementHandler handler) Safely retrieves the first cell as a sqlTimestampfrom the results of a customizable select query.executeGetFirstTimestamp(ReadQuery query, PreparedStatementHandler handler) Safely retrieves the first cell as a sqlTimestampfrom the results of a customizable select query.booleanexecuteHasResultRows(ReadQuery query) Safely and quickly verifies if a select query returns any rows.booleanexecuteHasResultRows(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.intexecuteUpdate(String sql) Safely and quickly executes an update statement.intexecuteUpdate(Query query) Safely and quickly executes an update statement.intexecuteUpdate(Query query, PreparedStatementHandler handler) Safely execute an update statement.<ResultType>
ResultTypeexecuteUseFirstAsciiStream(ReadQuery query, InputStreamUser user) Safely and quickly retrieves the first cell as an ASCIIInputStreamfrom the results of a select query.<ResultType>
ResultTypeexecuteUseFirstAsciiStream(ReadQuery query, InputStreamUser user, PreparedStatementHandler handler) Safely retrieves the first cell as an ASCIIInputStreamfrom the results of a customizable select query.<ResultType>
ResultTypeexecuteUseFirstBinaryStream(ReadQuery query, InputStreamUser user) Safely and quickly retrieves the first cell as a binaryInputStreamfrom the results of a select query.<ResultType>
ResultTypeexecuteUseFirstBinaryStream(ReadQuery query, InputStreamUser user, PreparedStatementHandler handler) Safely retrieves the first cell as an binaryInputStreamfrom the results of a customizable select query.<ResultType>
ResultTypeexecuteUseFirstCharacterStream(ReadQuery query, ReaderUser user) Safely and quickly retrieves the first cell as a characterReaderfrom the results of a select query.<ResultType>
ResultTypeexecuteUseFirstCharacterStream(ReadQuery query, ReaderUser user, PreparedStatementHandler handler) Safely retrieves the first cell as an characterReaderfrom the results of a customizable select query.booleanFetches the next row of a result set without processing it in any way.booleanfetch(ResultSet resultSet, DbRowProcessor rowProcessor) Fetches the next row of a result set and processes it through aDbRowProcessor.booleanfetch(ResultSet resultSet, RowProcessor rowProcessor) Convenience alternative tofetch(ResultSet, DbRowProcessor)that uses a simplifiedRowProcessorthat can be implemented with a lambda.booleanfetchAll(ResultSet resultSet, DbRowProcessor rowProcessor) Fetches all the next rows of a result set and processes it through aDbRowProcessor.booleanfetchAll(ResultSet resultSet, RowProcessor rowProcessor) Convenience alternative tofetchAll(ResultSet, DbRowProcessor)that uses a simplifiedRowProcessorthat can be implemented with a lambda.Obtains aDbConnectionof thisDbQueryManager'sDatasource.Retrieves theDatasourceof thisDbQueryManager.<ResultType>
ResultTypeinTransaction(TransactionUser user) Ensures that all the instructions that are executed in the providedDbTransactionUserinstance are executed inside a transaction and committed afterwards.voidConvenience method that ensures that all the instructions that are executed in the providedTransactionUserWithoutResultinstance, 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 providedDbConnectionUserinstance.
-
Constructor Details
-
DbQueryManager
Instantiates a newDbQueryManagerobject 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 ofPreparedStatementHandlerthat will be used to customize the query execution; ornullif 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:
truewhen rows were returned by the query; orfalseotherwise- 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 ofPreparedStatementHandlerthat will be used to customize the query execution- Returns:
truewhen rows were returned by the query; orfalseotherwise- Throws:
DatabaseException- seeDbPreparedStatement.executeQuery()andDbResultSet.hasResultRows()- Since:
- 1.0
- See Also:
-
executeGetFirstString
Safely and quickly retrieves the first cell as aStringfrom 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
Stringin 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 aStringfrom 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 ofPreparedStatementHandlerthat will be used to customize the query execution; ornullif you don't want to customize it at all- Returns:
- the first
Stringin 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 abooleanfrom the results of a select query. It relies on the wrappedDbResultSet.getFirstBoolean()method, but also automatically closes the statement after its execution.Refer to
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executed- Returns:
- the first
booleanin 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 abooleanfrom 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
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executedhandler- an instance ofPreparedStatementHandlerthat will be used to customize the query execution; ornullif you don't want to customize it at all- Returns:
- the first
booleanin 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 abytefrom the results of a select query. It relies on the wrappedDbResultSet.getFirstByte()method, but also automatically closes the statement after its execution.Refer to
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executed- Returns:
- the first
bytein 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 abytefrom 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
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executedhandler- an instance ofPreparedStatementHandlerthat will be used to customize the query execution; ornullif you don't want to customize it at all- Returns:
- the first
bytein 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 ashortfrom the results of a select query. It relies on the wrappedDbResultSet.getFirstShort()method, but also automatically closes the statement after its execution.Refer to
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executed- Returns:
- the first
shortin 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 ashortfrom 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
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executedhandler- an instance ofPreparedStatementHandlerthat will be used to customize the query execution; ornullif you don't want to customize it at all- Returns:
- the first
shortin 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 aintfrom the results of a select query. It relies on the wrappedDbResultSet.getFirstInt()method, but also automatically closes the statement after its execution.Refer to
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executed- Returns:
- the first
intin 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 aintfrom 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
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executedhandler- an instance ofPreparedStatementHandlerthat will be used to customize the query execution; ornullif you don't want to customize it at all- Returns:
- the first
intin 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 alongfrom the results of a select query. It relies on the wrappedDbResultSet.getFirstLong()method, but also automatically closes the statement after its execution.Refer to
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executed- Returns:
- the first
longin 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 alongfrom 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
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executedhandler- an instance ofPreparedStatementHandlerthat will be used to customize the query execution; ornullif you don't want to customize it at all- Returns:
- the first
longin 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 afloatfrom the results of a select query. It relies on the wrappedDbResultSet.getFirstFloat()method, but also automatically closes the statement after its execution.Refer to
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executed- Returns:
- the first
floatin 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 afloatfrom 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
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executedhandler- an instance ofPreparedStatementHandlerthat will be used to customize the query execution; ornullif you don't want to customize it at all- Returns:
- the first
floatin 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 adoublefrom the results of a select query. It relies on the wrappedDbResultSet.getFirstDouble()method, but also automatically closes the statement after its execution.Refer to
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executed- Returns:
- the first
doublein 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 adoublefrom 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
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executedhandler- an instance ofPreparedStatementHandlerthat will be used to customize the query execution; ornullif you don't want to customize it at all- Returns:
- the first
doublein 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 abytearray 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
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executed- Returns:
- the first
bytearray 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 abytearray 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
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executedhandler- an instance ofPreparedStatementHandlerthat will be used to customize the query execution; ornullif you don't want to customize it at all- Returns:
- the first
bytearray 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 sqlDatefrom the results of a select query. It relies on the wrappedDbResultSet.getFirstDate()method, but also automatically closes the statement after its execution.Refer to
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executed- Returns:
- the first sql
Datein 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 sqlDatefrom 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
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executedhandler- an instance ofPreparedStatementHandlerthat will be used to customize the query execution; ornullif you don't want to customize it at all- Returns:
- the first sql
Datein 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 sqlDatefrom 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
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executedcal- theCalendarobject to use in constructing the date- Returns:
- the first sql
Datein 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 sqlDatefrom 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
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executedcal- theCalendarobject to use in constructing the datehandler- an instance ofPreparedStatementHandlerthat will be used to customize the query execution; ornullif you don't want to customize it at all- Returns:
- the first sql
Datein 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 sqlTimefrom the results of a select query. It relies on the wrappedDbResultSet.getFirstTime()method, but also automatically closes the statement after its execution.Refer to
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executed- Returns:
- the first sql
Timein 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 sqlTimefrom 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
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executedhandler- an instance ofPreparedStatementHandlerthat will be used to customize the query execution; ornullif you don't want to customize it at all- Returns:
- the first sql
Timein 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 sqlTimefrom 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
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executedcal- theCalendarobject to use in constructing the time- Returns:
- the first sql
Timein 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 sqlTimefrom 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
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executedcal- theCalendarobject to use in constructing the timehandler- an instance ofPreparedStatementHandlerthat will be used to customize the query execution; ornullif you don't want to customize it at all- Returns:
- the first sql
Timein 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 sqlTimestampfrom the results of a select query. It relies on the wrappedDbResultSet.getFirstTimestamp()method, but also automatically closes the statement after its execution.Refer to
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executed- Returns:
- the first sql
Timestampin 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 sqlTimestampfrom 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
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executedhandler- an instance ofPreparedStatementHandlerthat will be used to customize the query execution; ornullif you don't want to customize it at all- Returns:
- the first sql
Timestampin 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 sqlTimestampfrom 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
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executedcal- theCalendarobject to use in constructing the timestamp- Returns:
- the first sql
Timestampin 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 sqlTimestampfrom 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
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executedcal- theCalendarobject to use in constructing the timestamphandler- an instance ofPreparedStatementHandlerthat will be used to customize the query execution; ornullif you don't want to customize it at all- Returns:
- the first sql
Timestampin 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 ASCIIInputStreamfrom the results of a select query. It relies on the wrappedDbResultSet.getFirstAsciiStream()method, but also automatically closes the statement after its execution.Refer to
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executeduser- an instance ofInputStreamUserthat contains the logic that will be executed with this stream- Returns:
- the return value from the
useInputStreammethod of the providedInputStreamUserinstance - 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 ASCIIInputStreamfrom 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
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executeduser- an instance ofInputStreamUserthat contains the logic that will be executed with this streamhandler- an instance ofPreparedStatementHandlerthat will be used to customize the query execution; ornullif you don't want to customize it at all- Returns:
- the return value from the
useInputStreammethod of the providedInputStreamUserinstance - 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 characterReaderfrom the results of a select query. It relies on the wrappedDbResultSet.getFirstCharacterStream()method, but also automatically closes the statement after its execution.Refer to
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executeduser- an instance ofReaderUserthat contains the logic that will be executed with this reader- Returns:
- the return value from the
useReadermethod of the providedReaderUserinstance - 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 characterReaderfrom 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
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executeduser- an instance ofReaderUserthat contains the logic that will be executed with this readerhandler- an instance ofPreparedStatementHandlerthat will be used to customize the query execution; ornullif you don't want to customize it at all- Returns:
- the return value from the
useReadermethod of the providedReaderUserinstance - 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 binaryInputStreamfrom the results of a select query. It relies on the wrappedDbResultSet.getFirstBinaryStream()method, but also automatically closes the statement after its execution.Refer to
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executeduser- an instance ofInputStreamUserthat contains the logic that will be executed with this stream- Returns:
- the return value from the
useInputStreammethod of the providedInputStreamUserinstance - 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 binaryInputStreamfrom 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
executeGetFirstStringfor an example code snippet, it's 100% analogous.- Parameters:
query- the query builder instance that needs to be executeduser- an instance ofInputStreamUserthat contains the logic that will be executed with this streamhandler- an instance ofPreparedStatementHandlerthat will be used to customize the query execution; ornullif you don't want to customize it at all- Returns:
- the return value from the
useInputStreammethod of the providedInputStreamUserinstance - 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- aDbRowProcessorinstance, if it'snullno processing will be performed on the fetched row- Returns:
trueif a row was retrieved; orfalseif 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 simplifiedRowProcessorthat can be implemented with a lambda.- Parameters:
query- the query builder instance that needs to be executedrowProcessor- aRowProcessorinstance, if it'snullno processing will be performed on the fetched row- Returns:
trueif a row was retrieved; orfalseif 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- aDbRowProcessorinstance, if it'snullno processing will be performed on the fetched rowhandler- an instance ofPreparedStatementHandlerthat will be used to customize the query execution; ornullif you don't want to customize it at all- Returns:
trueif a row was retrieved; orfalseif 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 simplifiedRowProcessorthat can be implemented with a lambda.- Parameters:
query- the query builder instance that needs to be executedrowProcessor- aRowProcessorinstance, if it'snullno processing will be performed on the fetched rowhandler- an instance ofPreparedStatementHandlerthat will be used to customize the query execution; ornullif you don't want to customize it at all- Returns:
trueif a row was retrieved; orfalseif 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 appropriateDbBeanFetcherinstance 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
nullif it couldn't be found - Throws:
DatabaseException- seeDbBeanFetcherandexecuteFetchFirst(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 appropriateDbBeanFetcherinstance, 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 ofPreparedStatementHandlerthat will be used to customize the query execution; ornullif you don't want to customize it at all- Returns:
- and instance of the bean if it was retrieved successfully; or
nullif it couldn't be found - Throws:
DatabaseException- seeDbBeanFetcherandexecuteFetchFirst(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- aDbRowProcessorinstance, if it'snullno processing will be performed on the fetched rows- Returns:
trueif rows were retrieved; orfalseif 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 simplifiedRowProcessorthat can be implemented with a lambda.- Parameters:
query- the query builder instance that needs to be executedrowProcessor- aRowProcessorinstance, if it'snullno processing will be performed on the fetched rows- Returns:
trueif a row was retrieved; orfalseif 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- aDbRowProcessorinstance, if it'snullno processing will be performed on the fetched rowhandler- an instance ofPreparedStatementHandlerthat will be used to customize the query execution; ornullif you don't want to customize it at all- Returns:
trueif rows were retrieved; orfalseif 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 simplifiedRowProcessorthat can be implemented with a lambda.- Parameters:
query- the query builder instance that needs to be executedrowProcessor- aRowProcessorinstance, if it'snullno processing will be performed on the fetched rowshandler- an instance ofPreparedStatementHandlerthat will be used to customize the query execution; ornullif you don't want to customize it at all- Returns:
trueif a row was retrieved; orfalseif 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 appropriateDbBeanFetcherinstance 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- seeDbBeanFetcherandexecuteFetchAll(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 appropriateDbBeanFetcherinstance, 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 ofPreparedStatementHandlerthat will be used to customize the query execution; ornullif 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- seeDbBeanFetcherandexecuteFetchAll(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 ofPreparedStatementHandlerthat will be used to customize the query execution; ornullif you don't want to customize it at all- Returns:
- the object that was returned by the overridden
concludeResultsmethod; ornullif 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 ofthat will be used to handle the results of the query execution; orDbResultSetHandlernullif you don't want to customize it at all- Returns:
- the object that was returned by the overridden
concludeResultsmethod; ornullif 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 providedDbConnectionUserinstance.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 ofDbConnectionUserthat contains the logic that will be executed- Returns:
- the return value from the
useConnectionmethod of the providedDbConnectionUserinstance - 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 providedDbTransactionUserinstance 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
rollbackmethod of theDbTransactionUserclass. 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 correctrollbackmethod, stops the execution of the activeDbTransactionUserand 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 ofTransactionUserthat contains the logic that will be executed- Returns:
- the return value from the
useTransactionmethod of the providedDbTransactionUserinstance - 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 providedTransactionUserWithoutResultinstance, 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 ofTransactionUserWithoutResultthat 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 validResultSetinstance- Returns:
trueif a new row was retrieved; orfalseif 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 validResultSetinstancerowProcessor- aDbRowProcessorinstance, if it'snullno processing will be performed on the fetched row- Returns:
trueif a new row was retrieved; orfalseif 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 simplifiedRowProcessorthat can be implemented with a lambda.- Parameters:
resultSet- a validResultSetinstancerowProcessor- aRowProcessorinstance, if it'snullno processing will be performed on the fetched row- Returns:
trueif a new row was retrieved; orfalseif 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 validResultSetinstancerowProcessor- aDbRowProcessorinstance, if it'snullno processing will be performed on the fetched rows- Returns:
trueif rows were fetched; orfalseif 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 simplifiedRowProcessorthat can be implemented with a lambda.- Parameters:
resultSet- a validResultSetinstancerowProcessor- aRowProcessorinstance, if it'snullno processing will be performed on the fetched rows- Returns:
trueif rows were fetched; orfalseif 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 aDbConnectionof thisDbQueryManager'sDatasource. Functions exactly as the wrappedDatasource.getConnection()method.- Returns:
- the requested
DbConnection - Throws:
DatabaseException- seeDatasource.getConnection()- Since:
- 1.0
- See Also:
-
getDatasource
Retrieves theDatasourceof 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.
-