Reputation: 32294
In a related Scala question, I asked the following:
When I need to read millions of database rows from a PostgreSQL database using the JDBC driver, I always use a cursor, otherwise I will get an OutOfMemoryError. Here is the pattern (pseudocode) that I use:
begin transaction
execute("declare cursor...")
while (true) {
boolean processedSomeRows = false
resultSet = executeQuery("fetch forward...")
while (resultSet.next()) {
processedSomeRows = true
...
}
if (!processedSomeRows) break
}
close cursor
commit
How can this be done in idiomatic Haskell?
Upvotes: 3
Views: 253
Reputation: 10551
There is quite new concept for dealing with streams like sql cursor: iteratees, or enumerator, or conduit. For example, in terms of the conduit library, from Persistent Book:
runResourceT $ withStmt "declare cursor..." []
$$ mapM_ doSomethingWithSingleResult
withStmt "declare cursor..." []
creates source
with rows, mapM_ doSomethingWithSingleResult
creates sink
for process single rows, and $$
connects source
with sink
.
Upvotes: 3