You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by Robert Huffman <ro...@gmail.com> on 2019/03/08 00:03:35 UTC

[dbutils] Taking advantage of database cursors in QueryRunner.

My apologies, but this is a repost. I failed to include a subject the first
time.


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. It is on 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.