Package nl.knaw.dans.lib.dataverse
Class QueryContext<T>
java.lang.Object
nl.knaw.dans.lib.dataverse.QueryContext<T>
- All Implemented Interfaces:
AutoCloseable
Context for executing a SQL query with multiple sets of parameters. Automatically manages the lifecycle of the database connection and prepared statement. This class is intended to be used for
read-only queries and will set the connection to read-only mode.
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()executeFor(Iterable<Object[]> sets) Executes the query for each set of parameters in the provided iterable, mapping each ResultSet row to an object of type T using the default mapper function.<U> List<U>executeFor(Iterable<Object[]> sets, Function<ResultSet, U> mapper) Executes the query for each set of parameters in the provided iterable, mapping each ResultSet row to an object of type T using the provided mapper function.
-
Method Details
-
executeFor
Executes the query for each set of parameters in the provided iterable, mapping each ResultSet row to an object of type T using the default mapper function. Automatically closes the ResultSet after processing each set of parameters. Note that types of parameters in the parameter sets must be compatible with the SQL query.- Parameters:
sets- an iterable of parameter sets, where each parameter set is an array of objects corresponding to the parameters in the SQL query- Returns:
- a list of objects of type T resulting from executing the query for each set of parameters
- Throws:
SQLException- when database access fails
-
executeFor
public <U> List<U> executeFor(Iterable<Object[]> sets, Function<ResultSet, U> mapper) throws SQLExceptionExecutes the query for each set of parameters in the provided iterable, mapping each ResultSet row to an object of type T using the provided mapper function. Automatically closes the ResultSet after processing each set of parameters. Note that types of parameters in the parameter sets must be compatible with the SQL query.- Type Parameters:
U- the type of objects to return in the list- Parameters:
sets- an iterable of parameter sets, where each parameter set is an array of objects corresponding to the parameters in the SQL querymapper- a function that maps a ResultSet row to an object of type U- Returns:
- a list of objects of type U resulting from executing the query for each set of parameters
- Throws:
SQLException- when database access fails
-
close
- Specified by:
closein interfaceAutoCloseable- Throws:
SQLException
-