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
}
}
- Companion
- class
Type members
Types
Value members
Concrete methods
Begins a auto-commit block easily with ConnectionPool.
Begins a auto-commit block easily with ConnectionPool.
- Type Params
- A
return type
- Value Params
- context
connection pool context
- execution
execution
- Returns
result value
Returns auto-commit session instance. You SHOULD close this instance by yourself.
Returns auto-commit session instance. You SHOULD close this instance by yourself.
- Value Params
- context
connection pool context
- Returns
session
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.
- Type Params
- A
return type
- Value Params
- context
connection pool context
- execution
execution
- Returns
result value
Get a connection and returns a DB instance.
Get a connection and returns a DB instance.
- Value Params
- conn
connection
- Returns
DB instance
Returns a DB instance by using an implicit Connection object.
Returns a DB instance by using an implicit Connection object.
- Value Params
- conn
connection
- Returns
DB instance
Returns describe style string value for the table
Returns describe style string value for the table
- Value Params
- context
connection pool context as implicit parameter
- table
table name (with schema optionally)
- Returns
described information
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.
- Type Params
- A
future result type
- Value Params
- context
connection pool context
- execution
execution that returns a future value
- Returns
future result value
Returns all the table names
Returns all the table names
- Value Params
- context
connection pool context as implicit parameter
- Returns
table information
Returns table information
Returns table information
- Value Params
- context
connection pool context as implicit parameter
- table
table name (with schema optionally)
- Returns
table information
Returns multiple table information
Returns multiple table information
- Value Params
- context
connection pool context as implicit parameter
- tableNamePattern
table name pattern (with schema optionally)
- Returns
table information
Begins a local-tx block easily with ConnectionPool.
Begins a local-tx block easily with ConnectionPool.
- Type Params
- A
return type
- Value Params
- context
connection pool context
- execution
execution
- Returns
result value
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.
- Type Params
- A
return type
- Value Params
- context
connection pool context
- execution
execution
- Returns
result value
Begins a read-only block easily with ConnectionPool.
Begins a read-only block easily with ConnectionPool.
- Type Params
- A
return type
- Value Params
- context
connection pool context
- execution
execution
- Returns
result value
Returns read-only session instance. You SHOULD close this instance by yourself.
Returns read-only session instance. You SHOULD close this instance by yourself.
- Value Params
- context
connection pool context
- Returns
session
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.
- Type Params
- A
return type
- Value Params
- context
connection pool context
- execution
execution
- Returns
result value
Returns table name list
Returns table name list
- Value Params
- context
connection pool context as implicit parameter
- tableNamePattern
table name pattern (with schema optionally)
- Returns
table name list
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.
- Type Params
- A
return type
- Value Params
- db
DB instance as an implicit parameter
- execution
execution
- Returns
result value
Returns within-tx session instance. You SHOULD close this instance by yourself.
Returns within-tx session instance. You SHOULD close this instance by yourself.
- Value Params
- db
DB instance as an implicit parameter
- Returns
session
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.
- Type Params
- A
return type
- Value Params
- db
DB instance as an implicit parameter
- execution
execution
- Returns
result value
Inherited methods
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