From f318c372bb4929d4b5fc8f6b8b56237e6efdc69d Mon Sep 17 00:00:00 2001 From: Nepomuk Seiler Date: Mon, 20 May 2013 12:14:07 +0200 Subject: [PATCH 1/2] #21 Added options to RichConnection. Customizing Statement creation is more tricky --- src/main/scala/net/noerd/prequel/RichConnection.scala | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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 ) From b63f7c96cdab4f0ea38fe8add13186863570d980 Mon Sep 17 00:00:00 2001 From: Nepomuk Seiler Date: Mon, 20 May 2013 12:38:21 +0200 Subject: [PATCH 2/2] Added customize block --- src/main/scala/net/noerd/prequel/Transaction.scala | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) 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