Skip to content

Instantly share code, notes, and snippets.

@lildata
Forked from aldonline/gist:6959105
Last active April 13, 2016 01:15
Show Gist options
  • Save lildata/0c5862930c5b59446067 to your computer and use it in GitHub Desktop.
Save lildata/0c5862930c5b59446067 to your computer and use it in GitHub Desktop.
object x {
// stream a sql query
def sql( str:String ):Stream[ResultSet] = withStatement { s =>
val rs = s executeQuery str
new Iterator[ResultSet] { def hasNext = rs.next ; def next = rs }.toStream
}
// loan a sql statement
def withStatement[R]( f:(Statement) => R ): R =
withConn { c => f( c.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY) ) }
// loan a sql connection
def withConn[R]( f:(java.sql.Connection) => R ):R = {
val conn = DriverManager getConnection "jdbc://..."
try f(conn) finally conn.close()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment