OxL Reference

type Db

The handle of an open database (Db.open(url)); the type itself is declared in the prelude.

Example:

oxl✓ checked in CI
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.Db

Thread 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 db
Opens a database
close() db
Closes the connection; later calls on this handle fail.
tryInsert(x: &T) -> () | DbError db
Inserts a row. tryInsert returns Conflict for a duplicate key or a violated unique index.
insert(x: &T) db
Inserts a row; a failure panics (use tryInsert to get a DbError).
upsert(x: &T) db
Inserts the row or replaces the row with the same primary key.
tryUpdate(x: &T) -> () | DbError db
Replaces all columns of the row with this primary key; NotFound / panic if there is none.
update(x: &T) db
Updates the row with the primary key of x.
tryDelete(x: &T) -> () | DbError db
Deletes the row with the primary key of x.
delete(x: &T) db
Deletes the row with the primary key of x.
deleteById(id: K) -> () | DbError db
db.deleteById(User, id).
find(id: K) -> T? db
db.find(User, id)
count() -> i64 db
Number of rows of the table
all() -> List<T> db
All rows of the table
execute(sql: str) -> () | DbError db
Runs SQL that returns no rows (DDL, maintenance).
lastInsertId() -> i64 db
Row id of the last insert (the key an @auto row got).
begin() -> () | DbError db
Starts a transaction; end it with commit or rollback (prefer transaction or atomic(db)).
commit() -> () | DbError db
Commits the open transaction.
rollback() -> () | DbError db
Rolls back the open transaction.
transaction(body: fn() -> R F) -> R db, F
Runs body in a transaction (a savepoint when nested)

Defined in: std/orm/orm.oxl:210

See also