Skip to content

Instantly share code, notes, and snippets.

@LeifW
Last active August 29, 2015 14:01
Show Gist options
  • Save LeifW/bbc01ed5d96ecb4567b1 to your computer and use it in GitHub Desktop.
Save LeifW/bbc01ed5d96ecb4567b1 to your computer and use it in GitHub Desktop.
scalaz-stream + jdbc
import java.sql.DriverManager
import scalaz.concurrent.Task
import scalaz.stream.io
import scalaz.stream.Process.End
class Query(query: String, chunkSize: Int) {
Class forName "org.postgresql.Driver"
val connection = DriverManager.getConnection(url, "username", "password")
val statement = connection.createStatement
// This will cause the postgres driver to create a cursor and give ResultSets of that size
statement.setFetchSize(chunkSize)
val resultSet = statement.executeQuery(query)
def next = resultSet.next
def isAfterLast = resultSet.isAfterLast
def getString(i:Int) = resultSet.getString(i)
def close {
resultSet.close
statement.close
connection.close
}
}
val chunkSize = 100
val query: scalaz.stream.Process[Task, IndexedSeq[String]] = io.resource(
Task.delay(new Query("select * from foo", chunkSize))
)(rs =>
Task.delay(rs.close)
)(rs =>
Task.delay{
if (rs.isAfterLast) throw End
for (_ <- 1 to chunkSize if rs.next) yield rs.getString(1)
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment