type Db
The handle of an open database (Db.open(url)); the type itself is declared in the prelude.
Example:
use std.orm { Db }
@table("items")
data Item {
@pk id: i64
name: str
price: i64
}
fn main() io, db {
let db = Db.open("sqlite::memory:")!
db.insert(Item{ id: 1, name: "tea", price: 3 })
print("{db.Item.count()}")
db.close()
}Output
1
pub(api) type Db = std.core.DbThread safety. a handle may be shared by tasks; `atomic(db)` transactions belong to one task.
Since. 1.0.0-beta
Methods
open | (url: str) -> Db | DbError dbOpens a database |
close | () dbCloses the connection; later calls on this handle fail. |
tryInsert | (x: &T) -> () | DbError dbInserts a row. tryInsert returns Conflict for a duplicate key or a violated unique index. |
insert | (x: &T) dbInserts a row; a failure panics (use tryInsert to get a DbError). |
upsert | (x: &T) dbInserts the row or replaces the row with the same primary key. |
tryUpdate | (x: &T) -> () | DbError dbReplaces all columns of the row with this primary key; NotFound / panic if there is none. |
update | (x: &T) dbUpdates the row with the primary key of x. |
tryDelete | (x: &T) -> () | DbError dbDeletes the row with the primary key of x. |
delete | (x: &T) dbDeletes the row with the primary key of x. |
deleteById | (id: K) -> () | DbError dbdb.deleteById(User, id). |
find | (id: K) -> T? dbdb.find(User, id) |
count | () -> i64 dbNumber of rows of the table |
all | () -> List<T> dbAll rows of the table |
execute | (sql: str) -> () | DbError dbRuns SQL that returns no rows (DDL, maintenance). |
lastInsertId | () -> i64 dbRow id of the last insert (the key an @auto row got). |
begin | () -> () | DbError dbStarts a transaction; end it with commit or rollback (prefer transaction or atomic(db)). |
commit | () -> () | DbError dbCommits the open transaction. |
rollback | () -> () | DbError dbRolls back the open transaction. |
transaction | (body: fn() -> R F) -> R db, FRuns body in a transaction (a savepoint when nested) |
Defined in: std/orm/orm.oxl:210