ConnectionMethods

final implicit class ConnectionMethods(connection: Connection) extends AnyVal

Provides extension methods for java.sql.Connection.

import scala.language.implicitConversions

import little.sql.{ *, given }

val connector = Connector("jdbc:h2:~/test", "sa", "s3cr3t", "org.h2.Driver")

connector.withConnection { conn =>
 val statements = Seq(
   "drop table prog_lang if exists",
   "create table prog_lang (id int, name text)",
   "insert into prog_lang (id, name) values (1, 'basic'), (2, 'pascal'), (3, 'c')",
   "select * from prog_lang"
 )

 statements.foreach { sql =>
   // Execute SQL and handle execution result accordingly
   conn.execute(sql) {
     // If update is executed print update count
     case Update(count) ⇒ println(s"Update Count: $count")

     // If query is executed print values of each row in ResultSet
     case Query(resultSet) =>
       while (resultSet.next())
         printf("id: %d, name: %s%n", resultSet.getInt("id"), resultSet.getString("name"))
   }
 }
}
class AnyVal
trait Matchable
class Any

Value members

Concrete methods

def batch(generator: () => Iterable[String]): Array[Int]

Executes batch of generated statements and returns results.

Executes batch of generated statements and returns results.

Value parameters:
generator

SQL generator

def batch(sql: String)(generator: () => Iterable[Seq[InParam]]): Array[Int]

Executes batch of statements with generated parameter values and returns results.

Executes batch of statements with generated parameter values and returns results.

The generator must return sets of parameter values that satisfy the supplied SQL.

Value parameters:
generator

parameter value generator

sql

SQL from which prepared statement is created

def execute[T](sql: String, params: Seq[InParam], queryTimeout: Int, maxRows: Int, fetchSize: Int)(f: Execution => T): T

Executes SQL and passes Execution to supplied function.

Executes SQL and passes Execution to supplied function.

Value parameters:
f

function

fetchSize

number of result set rows to fetch on each retrieval from database

maxRows

maximum number of rows to return in result set

params

parameters

queryTimeout

maximum number of seconds to wait for execution

sql

SQL

def first[T](sql: String, params: Seq[InParam], queryTimeout: Int)(f: ResultSet => T): Option[T]

Executes query and maps first row of ResultSet using supplied function.

Executes query and maps first row of ResultSet using supplied function.

If the result set is not empty, and if the supplied function's return value is not null, then Some value is returned; otherwise, None is returned.

Value parameters:
f

function

params

parameters

queryTimeout

maximum number of seconds to wait for execution

sql

SQL query

Returns:

value from supplied function

def flatMap[T](sql: String, params: Seq[InParam], queryTimeout: Int, maxRows: Int, fetchSize: Int)(f: ResultSet => Iterable[T]): Seq[T]

Executes query and builds a collection using the elements mapped from each row of ResultSet.

Executes query and builds a collection using the elements mapped from each row of ResultSet.

Value parameters:
f

map function

fetchSize

number of result set rows to fetch on each retrieval from database

maxRows

maximum number of rows to return in result set

params

parameters

queryTimeout

maximum number of seconds to wait for execution

sql

SQL query

def foreach(sql: String, params: Seq[InParam], queryTimeout: Int, maxRows: Int, fetchSize: Int)(f: ResultSet => Unit): Unit

Executes query and invokes supplied function for each row of ResultSet.

Executes query and invokes supplied function for each row of ResultSet.

Value parameters:
f

function

fetchSize

number of result set rows to fetch on each retrieval from database

maxRows

maximum number of rows to return in result set

params

parameters

queryTimeout

maximum number of seconds to wait for execution

sql

SQL query

def map[T](sql: String, params: Seq[InParam], queryTimeout: Int, maxRows: Int, fetchSize: Int)(f: ResultSet => T): Seq[T]

Executes query and maps each row of ResultSet using supplied function.

Executes query and maps each row of ResultSet using supplied function.

Value parameters:
f

map function

fetchSize

number of result set rows to fetch on each retrieval from database

maxRows

maximum number of rows to return in result set

params

parameters

queryTimeout

maximum number of seconds to wait for execution

sql

SQL query

def query[T](sql: String, params: Seq[InParam], queryTimeout: Int, maxRows: Int, fetchSize: Int)(f: ResultSet => T): T

Executes query and passes ResultSet to supplied function.

Executes query and passes ResultSet to supplied function.

Value parameters:
f

function

fetchSize

number of result set rows to fetch on each retrieval from database

maxRows

maximum number of rows to return in result set

params

parameters

queryTimeout

maximum number of seconds to wait for execution

sql

SQL query

def update(sql: String, params: Seq[InParam], queryTimeout: Int): Long

Executes update and returns update count.

Executes update and returns update count.

Value parameters:
params

parameters

queryTimeout

maximum number of seconds to wait for execution

sql

SQL update

def withPreparedStatement[T](sql: String)(f: PreparedStatement => T): T

Creates PreparedStatement and passes it to supplied function. Statement is closed on function's return.

Creates PreparedStatement and passes it to supplied function. Statement is closed on function's return.

Value parameters:
f

function

sql

SQL statement

Returns:

value from supplied function

def withStatement[T](f: Statement => T): T

Creates Statement and passes it to supplied function. Statement is closed on function's return.

Creates Statement and passes it to supplied function. Statement is closed on function's return.

Value parameters:
f

function

Returns:

value from supplied function