DB

case class DB(conn: Connection, connectionAttributes: DBConnectionAttributes, settingsProvider: SettingsProvider) extends DBConnection

Basic Database Accessor

Using DBSession:

 import scalikejdbc._
 case class User(id: Int, name: String)

 using(ConnectionPool(name).borrow()) { conn =>

   val users = DB(conn) readOnly { session =>
     session.list("select * from user") { rs =>
       User(rs.int("id"), rs.string("name"))
     }
   }

   DB(conn) autoCommit { session =>
     session.update("insert into user values (?,?)", 123, "Alice")
   }

   DB(conn) localTx { session =>
     session.update("insert into user values (?,?)", 123, "Alice")
   }

 }

Using SQL:

 import scalikejdbc._
 case class User(id: Int, name: String)

 using(ConnectionPool.borrow()) { conn =>

   val users = DB(conn) readOnly { implicit session =>
     SQL("select * from user").map { rs =>
       User(rs.int("id"), rs.string("name"))
     }.list.apply()
   }

   DB(conn) autoCommit { implicit session =>
     SQL("insert into user values (?,?)").bind(123, "Alice").update.apply()
   }

   DB(conn) localTx { implicit session =>
     SQL("insert into user values (?,?)").bind(123, "Alice").update.apply()
   }

 }
Companion
object
trait Serializable
trait Product
trait Equals
trait AutoCloseable
trait LogSupport
class Object
trait Matchable
class Any

Type members

Inherited types

type Closable = { def close(): Unit; }
Inherited from
LoanPattern

Value members

Inherited methods

def autoClose(autoClose: Boolean): DBConnection

Switches auto close mode.

Switches auto close mode.

Value Params
autoClose

auto close enabled if true

Inherited from
DBConnection
def autoCommit[A](execution: DBSession => A): A

Provides auto-commit session block.

Provides auto-commit session block.

Type Params
A

return type

Value Params
execution

block

Returns

result value

Inherited from
DBConnection

Returns auto-commit session.

Returns auto-commit session.

Returns

session

Inherited from
DBConnection
def autoCommitWithConnection[A](execution: Connection => A): A

Provides auto-commit session block.

Provides auto-commit session block.

Type Params
A

return type

Value Params
execution

block

Returns

result value

Inherited from
DBConnection
def begin(): Unit

Begins a new transaction.

Begins a new transaction.

Inherited from
DBConnection
def beginIfNotYet(): Unit

Begins a new transaction if the other one does not already start.

Begins a new transaction if the other one does not already start.

Inherited from
DBConnection
def close(): Unit

Close the connection.

Close the connection.

Inherited from
DBConnection
def commit(): Unit

Commits the current transaction.

Commits the current transaction.

Inherited from
DBConnection

Returns the current transaction. If the transaction has not started yet, IllegalStateException will be thrown.

Returns the current transaction. If the transaction has not started yet, IllegalStateException will be thrown.

Returns

tx

Inherited from
DBConnection
def describe(table: String): String

Returns describe style string value for the table

Returns describe style string value for the table

Value Params
table

table name (with schema optionally)

Returns

described information

Inherited from
DBConnection
def futureLocalTx[A](execution: DBSession => Future[A])(implicit ec: ExecutionContext): Future[A]

Easy way to checkout the current connection to be used in a transaction that needs to be committed/rolled back depending on Future results.

Easy way to checkout the current connection to be used in a transaction that needs to be committed/rolled back depending on Future results.

Type Params
A

future result type

Value Params
execution

block that takes a session and returns a future

Returns

future result

Inherited from
DBConnection
def futureUsing[R <: Closable, A](resource: R)(f: R => Future[A])(implicit ec: ExecutionContext): Future[A]

Guarantees a Closeable resource will be closed after being passed to a block that takes the resource as a parameter and returns a Future.

Guarantees a Closeable resource will be closed after being passed to a block that takes the resource as a parameter and returns a Future.

Inherited from
LoanPattern
def getAllColumns(meta: DatabaseMetaData, schema: String, table: String): ResultSet

Returns columns resultset in schema.tableNames

Returns columns resultset in schema.tableNames

Value Params
meta

database meta data

schema

schema name

table

table name

Returns

resultset related to columns

Inherited from
DBConnection
def getColumnNames(tableName: String, tableTypes: Array[String]): List[String]

Returns all the column names on the matched table name

Returns all the column names on the matched table name

Inherited from
DBConnection
def getTable(table: String, tableTypes: Array[String]): Option[Table]

Returns table information if exists

Returns table information if exists

Value Params
table

table name (with schema optionally)

Returns

table information

Inherited from
DBConnection
def getTableNames(tableNamePattern: String, tableTypes: Array[String]): List[String]

Returns all the table information that match the pattern

Returns all the table information that match the pattern

Value Params
tableNamePattern

table name pattern (with schema optionally)

Returns

table information

Inherited from
DBConnection
def isTxAlreadyStarted: Boolean

Returns is the current transaction already started.

Returns is the current transaction already started.

Returns

result

Inherited from
DBConnection
def isTxNotActive: Boolean

Returns is the current transaction is active.

Returns is the current transaction is active.

Returns

result

Inherited from
DBConnection
def isTxNotYetStarted: Boolean

Returns is the current transaction hasn't started yet.

Returns is the current transaction hasn't started yet.

Returns

result

Inherited from
DBConnection

Set isolation level.

Set isolation level.

Inherited from
DBConnection
def localTx[A](execution: DBSession => A)(implicit boundary: TxBoundary[A]): A

Provides local-tx session block.

Provides local-tx session block.

Type Params
A

return type

Value Params
execution

block

Returns

result value

Inherited from
DBConnection
def localTxWithConnection[A](execution: Connection => A)(implicit boundary: TxBoundary[A]): A

Provides local-tx session block.

Provides local-tx session block.

Type Params
A

return type

Value Params
execution

block

Returns

result value

Inherited from
DBConnection
def newTx: Tx

Starts a new transaction and returns it.

Starts a new transaction and returns it.

Returns

tx

Inherited from
DBConnection
def productElementNames: Iterator[String]
Inherited from
Product
def productIterator: Iterator[Any]
Inherited from
Product
def readOnly[A](execution: DBSession => A): A

Provides read-only session block.

Provides read-only session block.

Type Params
A

return type

Value Params
execution

block

Returns

result value

Inherited from
DBConnection

Returns read-only session.

Returns read-only session.

Returns

session

Inherited from
DBConnection
def readOnlyWithConnection[A](execution: Connection => A): A

Provides read-only session block.

Provides read-only session block.

Type Params
A

return type

Value Params
execution

block

Returns

result value

Inherited from
DBConnection
def rollback(): Unit

Rolls back the current transaction.

Rolls back the current transaction.

Inherited from
DBConnection
def rollbackIfActive(): Unit

Rolls back the current transaction if the transaction is still active.

Rolls back the current transaction if the transaction is still active.

Inherited from
DBConnection
def showTables(tableNamePattern: String, tableTypes: Array[String]): String

Returns table name list

Returns table name list

Value Params
tableNamePattern

table name pattern

tableTypes

table types

Returns

table name list

Inherited from
DBConnection
def tx: Tx

Returns the current transaction. If the transaction has not started yet, IllegalStateException will be thrown.

Returns the current transaction. If the transaction has not started yet, IllegalStateException will be thrown.

Returns

tx

Inherited from
DBConnection
def using[R <: Closable, A](resource: R)(f: R => A): A
Inherited from
LoanPattern
def withinTx[A](execution: DBSession => A): A

Provides within-tx session block.

Provides within-tx session block.

Type Params
A

return type

Value Params
execution

block

Returns

result value

Inherited from
DBConnection

Returns within-tx session.

Returns within-tx session.

Returns

session

Inherited from
DBConnection
def withinTxWithConnection[A](execution: Connection => A): A

Provides within-tx session block.

Provides within-tx session block.

Type Params
A

return type

Value Params
execution

block

Returns

result value

Inherited from
DBConnection

Inherited fields

protected val log: Log

Logger

Logger

Inherited from
LogSupport