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
Type members
Inherited types
Value members
Inherited methods
Switches auto close mode.
Switches auto close mode.
- Value Params
- autoClose
auto close enabled if true
- Inherited from
- DBConnection
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
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
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
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
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
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
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
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
Returns all the column names on the matched table name
Returns all the column names on the matched table name
- Inherited from
- DBConnection
Returns table information if exists
Returns table information if exists
- Value Params
- table
table name (with schema optionally)
- Returns
table information
- Inherited from
- DBConnection
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
Returns is the current transaction already started.
Returns is the current transaction already started.
- Returns
result
- Inherited from
- DBConnection
Returns is the current transaction is active.
Returns is the current transaction is active.
- Returns
result
- Inherited from
- DBConnection
Returns is the current transaction hasn't started yet.
Returns is the current transaction hasn't started yet.
- Returns
result
- Inherited from
- DBConnection
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
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
Starts a new transaction and returns it.
Starts a new transaction and returns it.
- Returns
tx
- Inherited from
- DBConnection
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
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
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
Returns table name list
Returns table name list
- Value Params
- tableNamePattern
table name pattern
- tableTypes
table types
- Returns
table name list
- 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
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
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