Class QueryContext<T>

java.lang.Object
nl.knaw.dans.lib.dataverse.QueryContext<T>
All Implemented Interfaces:
AutoCloseable

public final class QueryContext<T> extends Object implements 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 Details

    • executeFor

      public List<T> executeFor(Iterable<Object[]> sets) throws SQLException
      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 SQLException
      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. 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 query
      mapper - 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

      public void close() throws SQLException
      Specified by:
      close in interface AutoCloseable
      Throws:
      SQLException