I have no problem with the following example:
record ProductItem(Long productId, BigDecimal price) {
}
List<ProductItem> items = dsl().select(
PRODUCT.PRODUCTID,
PRODUCT.PRICE
)
.from(PRODUCT)
.fetch(Records.mapping(ProductItem::new));
But when i try to use the same example with fetchStream()
instead of fetch()
, the fetchStream()
does not provide any additional parameters.
It would be cool if the following would work:
List<ProductItem> items = dsl().select(
PRODUCT.PRODUCTID,
PRODUCT.PRICE
)
.from(PRODUCT)
.fetchSize(250)
.fetchStream(Records.mapping(ProductItem::new));
Is there any other way to acchieve what i would need (typesafe streaming mapping) with the jooq capabilities? Could this be a new feature for jooq?
I found the following to be working. So if this is safe to use, i guess i can use it instead.
.fetchStream()
.map(Records.mapping(ProductItem::new));
The reason why ResultQuery.fetch(RecordMapper)
and related methods exist is because fetch()
is an eager fetching operation, which fetches the entire JDBC ResultSet
into memory, into some client data structure. In this case, a List<E>
. Before the method completes, the underlying JDBC ResultSet
and PreparedStatement
will be closed. So there needs to be some way of passing mapping and other transformation functions to the fetch()
(or ResultQuery.collect()
) methods.
This isn't the case for fetchStream()
or fetchLazy()
, which produce a resourceful object that keeps open JDBC resources behind the scenes, and offers mapping methods directly on the stream. There's simply no benefit of offering fetchStream(RecordMapper)
and all the other dozens of overloads, given that you can do the same thing directly on the Stream
API.
Don't forget to wrap your stream in try-with-resources!
try (Stream<Record> s = dsl().select(..).from(..).fetchStream()) {
// Now map, collect, etc.
}