Skip to content

Instantly share code, notes, and snippets.

@naaman
Created March 5, 2013 23:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naaman/5095220 to your computer and use it in GitHub Desktop.
Save naaman/5095220 to your computer and use it in GitHub Desktop.
anorm hack to use row cursors
// hack to get a Stream with a cursor-backed query because anorm doesn't combine the two
private def resultSetToStream(sql: SimpleSql[Row])(implicit c: Connection): Stream[SqlRow] = {
val stmt = sql.getFilledStatement(c, getGeneratedKeys = false)
stmt.setFetchSize(1000)
val rs = stmt.executeQuery()
val rsMetaData = Sql.metaData(rs)
val columns = List.range(1, rsMetaData.columnCount + 1)
def data(rs: java.sql.ResultSet) = columns.map(nb => rs.getObject(nb))
Useful.unfold(rs)(rs => if (!rs.next()) { rs.getStatement.close(); None } else Some((new SqlRow(rsMetaData, data(rs)), rs)))
}
@lizepeng
Copy link

I tried, and I have to say

  1. do not forget to "connection.setAutoCommit(false)" before calling this method.
  2. do not forget to "conn.commit()" before closing the connection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment