Skip to content
This repository has been archived by the owner on Jun 4, 2019. It is now read-only.

First extension for RichConnection #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/main/scala/net/noerd/prequel/RichConnection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 )
Expand Down
11 changes: 6 additions & 5 deletions src/main/scala/net/noerd/prequel/Transaction.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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 )
}


Expand Down Expand Up @@ -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

Expand Down