Interface Query
Obtain instances via Database.query(String).
Positional parameters via ? are not supported; use named parameters (e.g. :id) and bind(String, Object).
Example usage:
// Query returning one row
Optional<Employee> employee = database.query("SELECT * FROM employee WHERE id = :id")
.bind("id", 42)
.fetchObject(Employee.class);
// Query returning multiple rows
List<Employee> employees = database.query("SELECT * FROM employee WHERE dept = :dept")
.bind("dept", "Engineering")
.fetchList(Employee.class);
// DML with no result
long rowsAffected = database.query("UPDATE employee SET active = :active WHERE id = :id")
.bind("id", 42)
.bind("active", false)
.execute();
// DML with RETURNING clause
Optional<Employee> updated = database.query("UPDATE employee SET salary = :salary WHERE id = :id RETURNING *")
.bind("id", 42)
.bind("salary", new BigDecimal("150000"))
.executeForObject(Employee.class);
Implementations of this interface are intended for use by a single thread.
- Since:
- 4.0.0
- Author:
- Mark Allen
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionBinds a named parameter to a value.Binds all entries from the given map as named parameters.customize(@NonNull PreparedStatementCustomizer preparedStatementCustomizer) Customizes thePreparedStatementbefore execution.execute()Executes a DML statement (INSERT, UPDATE, DELETE) with no resultset.Executes a DML statement in batch over groups of named parameters.executeForList(@NonNull Class<T> resultType) Executes a DML statement that returns multiple rows (e.g., withRETURNINGclause).executeForObject(@NonNull Class<T> resultType) Executes a DML statement that returns a single row (e.g., withRETURNINGclause).Executes the query and returns all results as a list.fetchObject(@NonNull Class<T> resultType) Executes the query and returns a single result.<T,R> @Nullable R Associates an identifier with this query for logging/diagnostics.
-
Method Details
-
bind
Binds a named parameter to a value.- Parameters:
name- the parameter name (without the leading:)value- the value to bind (may benull). RawCollectionand array values are not expanded; useParameters.inList(java.util.Collection),Parameters.sqlArrayOf(String, Object[]), orParameters.arrayOf(Class, Object)as appropriate.- Returns:
- this builder, for chaining
-
bindAll
-
id
-
customize
Customizes thePreparedStatementbefore execution.If called multiple times, the most recent customizer wins.
- Parameters:
preparedStatementCustomizer- customization callback- Returns:
- this builder, for chaining
-
fetchObject
Executes the query and returns a single result.- Type Parameters:
T- the result type- Parameters:
resultType- the type to marshal each row to- Returns:
- the single result, or empty if no rows
- Throws:
DatabaseException- if more than one row is returned
-
fetchList
-
fetchStream
<T,R> @Nullable R fetchStream(@NonNull Class<T> resultType, @NonNull Function<Stream<@Nullable T>, R> streamFunction) Executes the query and provides aStreambacked by the underlyingResultSet.This approach is useful for processing very large resultsets (e.g. millions of rows), where it's impractical to load all rows into memory at once.
JDBC resources are closed automatically when
streamFunctionreturns (or throws), so the stream must be fully consumed within that callback. Do not escape the stream from the function.The stream must be consumed within the scope of the transaction or connection that created it.
- Type Parameters:
T- the result typeR- the return type- Parameters:
resultType- the type to marshal each row tostreamFunction- function that consumes the stream and returns a result- Returns:
- the value returned by
streamFunction
-
execute
-
executeBatch
@NonNull List<Long> executeBatch(@NonNull List<@NonNull Map<@NonNull String, @Nullable Object>> parameterGroups) Executes a DML statement in batch over groups of named parameters.Any parameters already bound on this
Queryapply to all groups; group values override them.- Parameters:
parameterGroups- groups of named parameter values (without the leading:)- Returns:
- the number of rows affected by the SQL statement per-group
-
executeForObject
Executes a DML statement that returns a single row (e.g., withRETURNINGclause).- Type Parameters:
T- the result type- Parameters:
resultType- the type to marshal the row to- Returns:
- the single result, or empty if no rows
- Throws:
DatabaseException- if more than one row is returned
-
executeForList
-