I like DbUtils QueryRunner, but it forces me to read the entire ResultSet
into memory rather than allowing me to use cursors. So I developed a little
library to do that, which I called dbstream, which is on GitHub:
https://github.com/rhuffman/dbstream/

It provides an abstract StreamingResultSetHandler that will return a
Stream<T>, which each element is built from one row of the query. It
utilizes cursors by keeping the ResultSet (and underlying Connection) open
until the Stream is closed. There are three concrete subclasses of the
handler:

   - ArrayStreamingHandler: returns a Stream<Object[]>, where each Object[]
   is the values from a row of the result
   - ObjectStreamingHandler: returns a Stream<Object>, where each object is
   a bean built from a row of the result
   - MapStreamingHandler: returns a Stream<Map<String,Object>>, where each
   object is a map of column names and values built from a row of the result

Of course, the QueryRunner.query methods close their database objects
before returning, so I needed different query methods to execute the query.
I extended QueryRunner to create StreamingQueryRunner with these two
methods:

   - public <T> Stream<T> queryAsStream(
       String sql,
       StreamingResultSetHandler<T> handler,
       Object... args)

   - public <T> Stream<T> queryAsStream(
       Connection connection,
       String sql,
       StreamingResultSetHandler<T> handler,
       Object... args)


I'm probably going to publish it to Maven Central. However, would the
DbUtils developers be amenable to doing something like this directly in
DbUtils? I could integrate the work into DbUtils and give you a pull
request. It might take me a bit because I would have to convert the tests
from Spock, but it wouldn't be too bad.

Reply via email to