object DB extends LoanPattern with Serializable
Basic Database Accessor
You can start with DB and blocks if using scalikejdbc.ConnectionPool.singleton().
Using DBSession:
ConnectionPool.singleton("jdbc:...","user","password") case class User(id: Int, name: String) val users = DB readOnly { session => session.list("select * from user") { rs => User(rs.int("id"), rs.string("name")) } } DB autoCommit { session => session.update("insert into user values (?,?)", 123, "Alice") } DB localTx { session => session.update("insert into user values (?,?)", 123, "Alice") } using(DB(ConnectionPool.borrow())) { db => db.begin() try { DB withTx { session => session.update("update user set name = ? where id = ?", "Alice", 123) } db.commit() } catch { case e => db.rollbackIfActive() throw e } }
Using SQL:
ConnectionPool.singleton("jdbc:...","user","password") case class User(id: Int, name: String) val users = DB readOnly { implicit session => SQL("select * from user").map { rs => User(rs.int("id"), rs.string("name")) }.list.apply() } DB autoCommit { implicit session => SQL("insert into user values (?,?)").bind(123, "Alice").update.apply() } DB localTx { implicit session => SQL("insert into user values (?,?)").bind(123, "Alice").update.apply() } using(DB(ConnectionPool.borrow())) { db => db.begin() try { DB withTx { implicit session => SQL("update user set name = ? where id = ?").bind("Alice", 123).update.apply() } db.commit() } catch { case e => db.rollbackIfActive() throw e } }
- Source
- DB.scala
- Alphabetic
- By Inheritance
- DB
- Serializable
- Serializable
- LoanPattern
- AnyRef
- Any
- Hide All
- Show All
- Public
- All
Type Members
- type CPContext = ConnectionPoolContext
-
type
Closable = AnyRef { def close(): Unit }
- Definition Classes
- LoanPattern
Value Members
-
final
def
!=(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
final
def
##(): Int
- Definition Classes
- AnyRef → Any
-
final
def
==(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
- val NoCPContext: NoConnectionPoolContext.type
-
final
def
asInstanceOf[T0]: T0
- Definition Classes
- Any
-
def
autoCommit[A](execution: (DBSession) ⇒ A)(implicit context: CPContext = NoCPContext, settings: SettingsProvider = SettingsProvider.default): A
Begins a auto-commit block easily with ConnectionPool.
Begins a auto-commit block easily with ConnectionPool.
- A
return type
- execution
execution
- context
connection pool context
- returns
result value
-
def
autoCommitSession(settings: SettingsProvider = SettingsProvider.default)(implicit context: CPContext = NoCPContext): DBSession
Returns auto-commit session instance.
Returns auto-commit session instance. You SHOULD close this instance by yourself.
- context
connection pool context
- returns
session
-
def
autoCommitWithConnection[A](execution: (Connection) ⇒ A)(implicit context: CPContext = NoCPContext, settings: SettingsProvider = SettingsProvider.default): A
Begins a auto-commit block easily with ConnectionPool and pass not session but connection to execution block.
Begins a auto-commit block easily with ConnectionPool and pass not session but connection to execution block.
- A
return type
- execution
execution
- context
connection pool context
- returns
result value
-
def
clone(): AnyRef
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
def
connect(conn: Connection = ConnectionPool.borrow(), settings: SettingsProvider = SettingsProvider.default): DB
Get a connection and returns a DB instance.
Get a connection and returns a DB instance.
- conn
connection
- returns
DB instance
-
def
connected(implicit conn: Connection, settings: SettingsProvider = SettingsProvider.default): DB
Returns a DB instance by using an implicit Connection object.
Returns a DB instance by using an implicit Connection object.
- conn
connection
- returns
DB instance
-
def
describe(table: String, settings: SettingsProvider = SettingsProvider.default)(implicit context: CPContext = NoCPContext): String
Returns describe style string value for the table
Returns describe style string value for the table
- table
table name (with schema optionally)
- context
connection pool context as implicit parameter
- returns
described information
-
final
def
eq(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
def
equals(arg0: Any): Boolean
- Definition Classes
- AnyRef → Any
-
def
finalize(): Unit
- Attributes
- protected[java.lang]
- Definition Classes
- AnyRef
- Annotations
- @throws( classOf[java.lang.Throwable] )
-
def
futureLocalTx[A](execution: (DBSession) ⇒ Future[A])(implicit context: CPContext = NoCPContext, ec: ExecutionContext, settings: SettingsProvider = SettingsProvider.default): Future[A]
Begins a local-tx block that returns a Future value easily with ConnectionPool.
Begins a local-tx block that returns a Future value easily with ConnectionPool.
- A
future result type
- execution
execution that returns a future value
- context
connection pool context
- returns
future result value
-
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.
- Definition Classes
- LoanPattern
-
def
getAllTableNames(settings: SettingsProvider = SettingsProvider.default)(implicit context: CPContext = NoCPContext): List[String]
Returns all the table names
Returns all the table names
- context
connection pool context as implicit parameter
- returns
table information
-
final
def
getClass(): Class[_]
- Definition Classes
- AnyRef → Any
- def getColumnNames(table: String, settings: SettingsProvider = SettingsProvider.default)(implicit context: CPContext = NoCPContext): List[String]
-
def
getTable(table: String, settings: SettingsProvider = SettingsProvider.default)(implicit context: CPContext = NoCPContext): Option[Table]
Returns table information
Returns table information
- table
table name (with schema optionally)
- context
connection pool context as implicit parameter
- returns
table information
-
def
getTableNames(tableNamePattern: String, settings: SettingsProvider = SettingsProvider.default)(implicit context: CPContext = NoCPContext): List[String]
Returns multiple table information
Returns multiple table information
- tableNamePattern
table name pattern (with schema optionally)
- context
connection pool context as implicit parameter
- returns
table information
-
def
hashCode(): Int
- Definition Classes
- AnyRef → Any
-
final
def
isInstanceOf[T0]: Boolean
- Definition Classes
- Any
-
def
localTx[A](execution: (DBSession) ⇒ A)(implicit context: CPContext = NoCPContext, boundary: TxBoundary[A] = defaultTxBoundary[A], settings: SettingsProvider = SettingsProvider.default): A
Begins a local-tx block easily with ConnectionPool.
Begins a local-tx block easily with ConnectionPool.
- A
return type
- execution
execution
- context
connection pool context
- returns
result value
-
def
localTxWithConnection[A](execution: (Connection) ⇒ A)(implicit context: CPContext = NoCPContext, boundary: TxBoundary[A] = defaultTxBoundary[A], settings: SettingsProvider = SettingsProvider.default): A
Begins a local-tx block easily with ConnectionPool and pass not session but connection to execution block.
Begins a local-tx block easily with ConnectionPool and pass not session but connection to execution block.
- A
return type
- execution
execution
- context
connection pool context
- returns
result value
-
final
def
ne(arg0: AnyRef): Boolean
- Definition Classes
- AnyRef
-
final
def
notify(): Unit
- Definition Classes
- AnyRef
-
final
def
notifyAll(): Unit
- Definition Classes
- AnyRef
-
def
readOnly[A](execution: (DBSession) ⇒ A)(implicit context: CPContext = NoCPContext, settings: SettingsProvider = SettingsProvider.default): A
Begins a read-only block easily with ConnectionPool.
Begins a read-only block easily with ConnectionPool.
- A
return type
- execution
execution
- context
connection pool context
- returns
result value
-
def
readOnlySession(settings: SettingsProvider = SettingsProvider.default)(implicit context: CPContext = NoCPContext): DBSession
Returns read-only session instance.
Returns read-only session instance. You SHOULD close this instance by yourself.
- context
connection pool context
- returns
session
-
def
readOnlyWithConnection[A](execution: (Connection) ⇒ A)(implicit context: CPContext = NoCPContext, settings: SettingsProvider = SettingsProvider.default): A
Begins a read-only block easily with ConnectionPool and pass not session but connection to execution block.
Begins a read-only block easily with ConnectionPool and pass not session but connection to execution block.
- A
return type
- execution
execution
- context
connection pool context
- returns
result value
-
def
showTables(tableNamePattern: String = "%", tableTypes: Array[String] = Array("TABLE", "VIEW"), settings: SettingsProvider = SettingsProvider.default)(implicit context: CPContext = NoCPContext): String
Returns table name list
Returns table name list
- tableNamePattern
table name pattern (with schema optionally)
- context
connection pool context as implicit parameter
- returns
table name list
-
final
def
synchronized[T0](arg0: ⇒ T0): T0
- Definition Classes
- AnyRef
-
def
toString(): String
- Definition Classes
- AnyRef → Any
-
def
using[R <: Closable, A](resource: R)(f: (R) ⇒ A): A
- Definition Classes
- LoanPattern
-
final
def
wait(): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long, arg1: Int): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
final
def
wait(arg0: Long): Unit
- Definition Classes
- AnyRef
- Annotations
- @throws( ... )
-
def
withinTx[A](execution: (DBSession) ⇒ A)(implicit db: DB): A
Begins a within-tx block easily with a DB instance as an implicit parameter.
Begins a within-tx block easily with a DB instance as an implicit parameter.
- A
return type
- execution
execution
- db
DB instance as an implicit parameter
- returns
result value
-
def
withinTxSession()(implicit db: DB): DBSession
Returns within-tx session instance.
Returns within-tx session instance. You SHOULD close this instance by yourself.
- db
DB instance as an implicit parameter
- returns
session
-
def
withinTxWithConnection[A](execution: (Connection) ⇒ A)(implicit db: DB): A
Begins a within-tx block easily with a DB instance as an implicit parameter and pass not session but connection to execution block.
Begins a within-tx block easily with a DB instance as an implicit parameter and pass not session but connection to execution block.
- A
return type
- execution
execution
- db
DB instance as an implicit parameter
- returns
result value