ConnectionMethods

little.sql.ConnectionMethods$package.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 result set
     case Query(resultSet) =>
       while (resultSet.next())
         printf("id: %d, name: %s%n", resultSet.getInt("id"), resultSet.getString("name"))
   }
 }
}

Attributes

Graph
Supertypes
class AnyVal
trait Matchable
class Any

Members list

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

Attributes

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 sequence of parameter values that satisfy the supplied SQL.

Value parameters

generator

parameter value generator

sql

SQL from which prepared statement is created

Attributes

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

Executes SQL and passes result to supplied function.

Executes SQL and passes result 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

Attributes

def executeUpdate(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

Attributes

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

Executes query and maps first row of result set using supplied function.

Executes query and maps first row of result set 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

Attributes

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 result set.

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

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

Attributes

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 result set.

Executes query and invokes supplied function for each row of result set.

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

Attributes

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 result set using supplied function.

Executes query and maps each row of result set 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

Attributes

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

Executes query and passes result set to supplied function.

Executes query and passes result set 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

Attributes

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

Executes update and passes update count to supplied function.

Executes update and passes update count to supplied function.

Value parameters

f

function

params

parameters

queryTimeout

maximum number of seconds to wait for execution

sql

SQL update

Attributes

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

Attributes

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

Attributes

Returns

value from supplied function