diff --git a/src/main/scala/net/noerd/prequel/RichConnection.scala b/src/main/scala/net/noerd/prequel/RichConnection.scala index c9bac5a..706c5d8 100644 --- a/src/main/scala/net/noerd/prequel/RichConnection.scala +++ b/src/main/scala/net/noerd/prequel/RichConnection.scala @@ -3,6 +3,8 @@ package net.noerd.prequel import java.sql.Connection import java.sql.Statement +import java.sql.ResultSet.TYPE_FORWARD_ONLY +import java.sql.ResultSet.CONCUR_READ_ONLY import java.sql.Statement.RETURN_GENERATED_KEYS import java.sql.Statement.NO_GENERATED_KEYS @@ -15,8 +17,8 @@ private[prequel] class RichConnection( val wrapped: Connection ) { * Creates a new statement executes the given block with it. * The statement is automatically closed once the block has finished. */ - def usingStatement[ T ]( block: (Statement) => T ): T = { - val statement = wrapped.createStatement + def usingStatement[ T ]( block: (Statement) => T , resultSetType: Int = TYPE_FORWARD_ONLY, resultSetConcurrency: Int = CONCUR_READ_ONLY): T = { + val statement = wrapped.createStatement(resultSetType, resultSetConcurrency) try { block( statement ) diff --git a/src/main/scala/net/noerd/prequel/Transaction.scala b/src/main/scala/net/noerd/prequel/Transaction.scala index d91d29f..ccd8f12 100644 --- a/src/main/scala/net/noerd/prequel/Transaction.scala +++ b/src/main/scala/net/noerd/prequel/Transaction.scala @@ -45,9 +45,9 @@ class Transaction( val connection: Connection, val formatter: SQLFormatter ) { * @param params are the optional parameters used in the query * @param block is a function converting the row to something else */ - def select[ T ]( sql: String, params: Formattable* )( block: ResultSetRow => T ): Seq[ T ] = { + def select[ T ]( sql: String, params: Formattable* )( block: ResultSetRow => T , customize: Statement => Unit = (_ => Unit)): Seq[ T ] = { val results = new ArrayBuffer[ T ] - _selectIntoBuffer( Some( results ), sql, params.toSeq )( block ) + _selectIntoBuffer( Some( results ), sql, params.toSeq )( block, customize ) results } @@ -60,8 +60,8 @@ class Transaction( val connection: Connection, val formatter: SQLFormatter ) { * @param params are the optional parameters used in the query * @param block is a function fully processing each row */ - def selectAndProcess( sql: String, params: Formattable* )( block: ResultSetRow => Unit ): Unit = { - _selectIntoBuffer( None, sql, params.toSeq )( block ) + def selectAndProcess( sql: String, params: Formattable* )( block: ResultSetRow => Unit, customize: Statement => Unit = (_ => Unit) ): Unit = { + _selectIntoBuffer( None, sql, params.toSeq )( block, customize ) } @@ -242,8 +242,9 @@ class Transaction( val connection: Connection, val formatter: SQLFormatter ) { private def _selectIntoBuffer[ T ]( buffer: Option[ ArrayBuffer[T] ], sql: String, params: Seq[ Formattable ] - )( block: ( ResultSetRow ) => T ): Unit = { + )( block: ( ResultSetRow ) => T, customize: Statement => Unit): Unit = { connection.usingStatement { statement => + customize(statement) val rs = statement.executeQuery( formatter.formatSeq( sql, params ) ) val append = buffer.isDefined