DB

scalikejdbc.DB
See theDB companion object
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()
   }

 }

Attributes

Companion
object
Source
DB.scala
Graph
Supertypes
trait Serializable
trait Product
trait Equals
trait DBConnection
trait AutoCloseable
trait LoanPattern
class Object
trait Matchable
class Any
Show all

Members list

Type members

Inherited types

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

Attributes

Inherited from:
LoanPattern
Source
LoanPattern.scala

Value members

Inherited methods

def autoClose(autoClose: Boolean): DBConnection

Switches auto close mode.

Switches auto close mode.

Value parameters

autoClose

auto close enabled if true

Attributes

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

Provides auto-commit session block.

Provides auto-commit session block.

Type parameters

A

return type

Value parameters

execution

block

Attributes

Returns

result value

Inherited from:
DBConnection
Source
DBConnection.scala

Returns auto-commit session.

Returns auto-commit session.

Attributes

Returns

session

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

Provides auto-commit session block.

Provides auto-commit session block.

Type parameters

A

return type

Value parameters

execution

block

Attributes

Returns

result value

Inherited from:
DBConnection
Source
DBConnection.scala
def begin(): Unit

Begins a new transaction.

Begins a new transaction.

Attributes

Inherited from:
DBConnection
Source
DBConnection.scala
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.

Attributes

Inherited from:
DBConnection
Source
DBConnection.scala
def close(): Unit

Close the connection.

Close the connection.

Attributes

Inherited from:
DBConnection
Source
DBConnection.scala
def commit(): Unit

Commits the current transaction.

Commits the current transaction.

Attributes

Inherited from:
DBConnection
Source
DBConnection.scala
def currentTx: 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.

Attributes

Returns

tx

Inherited from:
DBConnection
Source
DBConnection.scala
def describe(table: String): String

Returns describe style string value for the table

Returns describe style string value for the table

Value parameters

table

table name (with schema optionally)

Attributes

Returns

described information

Inherited from:
DBConnection
Source
DBConnection.scala
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 parameters

A

future result type

Value parameters

execution

block that takes a session and returns a future

Attributes

Returns

future result

Inherited from:
DBConnection
Source
DBConnection.scala
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.

Attributes

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

Returns columns resultset in schema.tableNames

Returns columns resultset in schema.tableNames

Value parameters

meta

database meta data

schema

schema name

table

table name

Attributes

Returns

resultset related to columns

Inherited from:
DBConnection
Source
DBConnection.scala
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

Attributes

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

Returns table information if exists

Returns table information if exists

Value parameters

table

table name (with schema optionally)

Attributes

Returns

table information

Inherited from:
DBConnection
Source
DBConnection.scala
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 parameters

tableNamePattern

table name pattern (with schema optionally)

Attributes

Returns

table information

Inherited from:
DBConnection
Source
DBConnection.scala
def isTxAlreadyStarted: Boolean

Returns is the current transaction already started.

Returns is the current transaction already started.

Attributes

Returns

result

Inherited from:
DBConnection
Source
DBConnection.scala
def isTxNotActive: Boolean

Returns is the current transaction is active.

Returns is the current transaction is active.

Attributes

Returns

result

Inherited from:
DBConnection
Source
DBConnection.scala
def isTxNotYetStarted: Boolean

Returns is the current transaction hasn't started yet.

Returns is the current transaction hasn't started yet.

Attributes

Returns

result

Inherited from:
DBConnection
Source
DBConnection.scala

Set isolation level.

Set isolation level.

Attributes

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

Provides local-tx session block.

Provides local-tx session block.

Type parameters

A

return type

Value parameters

execution

block

Attributes

Returns

result value

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

Provides local-tx session block.

Provides local-tx session block.

Type parameters

A

return type

Value parameters

execution

block

Attributes

Returns

result value

Inherited from:
DBConnection
Source
DBConnection.scala
def newTx: Tx

Starts a new transaction and returns it.

Starts a new transaction and returns it.

Attributes

Returns

tx

Inherited from:
DBConnection
Source
DBConnection.scala
def productElementNames: Iterator[String]

Attributes

Inherited from:
Product
def productIterator: Iterator[Any]

Attributes

Inherited from:
Product
def readOnly[A](execution: DBSession => A): A

Provides read-only session block.

Provides read-only session block.

Type parameters

A

return type

Value parameters

execution

block

Attributes

Returns

result value

Inherited from:
DBConnection
Source
DBConnection.scala

Returns read-only session.

Returns read-only session.

Attributes

Returns

session

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

Provides read-only session block.

Provides read-only session block.

Type parameters

A

return type

Value parameters

execution

block

Attributes

Returns

result value

Inherited from:
DBConnection
Source
DBConnection.scala
def rollback(): Unit

Rolls back the current transaction.

Rolls back the current transaction.

Attributes

Inherited from:
DBConnection
Source
DBConnection.scala
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.

Attributes

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

Returns table name list

Returns table name list

Value parameters

tableNamePattern

table name pattern

tableTypes

table types

Attributes

Returns

table name list

Inherited from:
DBConnection
Source
DBConnection.scala
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.

Attributes

Returns

tx

Inherited from:
DBConnection
Source
DBConnection.scala
def using[R <: Closable, A](resource: R)(f: R => A): A

Attributes

Inherited from:
LoanPattern
Source
LoanPattern.scala
def withinTx[A](execution: DBSession => A): A

Provides within-tx session block.

Provides within-tx session block.

Type parameters

A

return type

Value parameters

execution

block

Attributes

Returns

result value

Inherited from:
DBConnection
Source
DBConnection.scala

Returns within-tx session.

Returns within-tx session.

Attributes

Returns

session

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

Provides within-tx session block.

Provides within-tx session block.

Type parameters

A

return type

Value parameters

execution

block

Attributes

Returns

result value

Inherited from:
DBConnection
Source
DBConnection.scala

Inherited fields

protected val log: Log

Logger

Logger

Attributes

Inherited from:
LogSupport (hidden)
Source
LogSupport.scala