Interface ResultSetScanner<T>

Type Parameters:
T - the collected result type
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface ResultSetScanner<T>
Scan over rows of result sets, mapping and collecting the rows to a result type. Unlike the Row and ColumnMappers, this interface lets you control the ResultSet scrolling - the implementation should ResultSet.next() over rows it wants to consider.
See Also:
  • Method Details

    • scanResultSet

      T scanResultSet(Supplier<ResultSet> resultSetSupplier, StatementContext ctx) throws SQLException
      Scans the lazily-supplied ResultSet into a result. The ResultSet is not produced (and typically, the statement the result came from is not executed) until resultSetSupplier.get() is called.

      Implementors that call resultSetSupplier.get() must ensure that the statement context is closed when the scanResultSet(Supplier, StatementContext) method exits. Otherwise, database resource may not be freed.

       public T scanResultSet(Supplier<ResultSet> resultSetSupplier, StatementContext ctx) {
           ...
           try (StatementContext context = ctx) {
               ResultSet resultSet = resultSetSupplier.get();
               // generate and return result from the result set.
           }
       }
       

      Alternatively, implementors may return some intermediate result object (e.g. ResultIterable) without calling resultSetSupplier.get(), in which case the burden of closing resources falls to whichever object ultimately calls resultSetSupplier.get().

       public T scanResultSet(Supplier<ResultSet> resultSetSupplier, StatementContext ctx) {
           ...
           // the implementation of SomeOtherClass.createResponse which may call resultSetSupplier.get()
           // is now responsible to register the result set for cleanup and to close the StatementContext.
           return SomeOtherClass.createResponse(resultSetSupplier, ..., ctx);
       }
       
      Parameters:
      resultSetSupplier - supplies a ResultSet.
      ctx - the statement context.
      Returns:
      the mapped result
      Throws:
      SQLException - if anything goes wrong