module std.orm
std.orm — tables over SQLite and PostgreSQL (II.1, §12.8, §5.9). Decisions D-3150..D-3169, PostgreSQL D-3900..D-3904.
A data marked @table("name") is a table: @pk names the primary key (one field, @auto for an integer key the
database assigns when it is 0), @index(.field, …) (and @index(.a, .b, unique: true)) create indexes. The schema
is created on first use and new fields are added as columns; @owner, @shard and the other attributes of
§12.8/§13 are accepted and ignored for now. Scalars are columns, everything else (lists, nested data, unions) is one
serialized column; see runtime/include/oxl_orm.h for the mapping.
let db = Db.open("sqlite:shop.db")! // or "sqlite::memory:", "builtin://shop"
let pg = Db.open("postgres://user:pw@host:5432/shop?schema=app")! // PostgreSQL via libpq; `postgres:` alone: $OXL_PG_DSN
db.insert(user) // panics on failure; tryInsert gives a DbError
let u = db.find(User, id) // User?
let taken = from u in db.User where u.email == email take 1Queries on db.T are compiled to SQL when the program is built (where with fields, literals, parameters, arithmetic,
comparisons, && || !, in [..], startsWith, endsWith, contains, lower, upper; order by, skip, take,
select); anything else is the error query-not-sql. They run with the effect db and return a List.
Example:
use std.orm { Db }
@table("notes")
data Note {
@pk id: i64
text: str
}
fn main() io, db {
let db = Db.open("sqlite::memory:")!
db.insert(Note{ id: 1, text: "buy tea" })
for n in from n in db.Note where n.id == 1 select n.text {
print(n)
}
db.close()
}buy tea
Типы
type Db | The handle of an open database (Db.open(url)); the type itself is declared in the prelude. |
type DbError | A failed database operation |
type DbErrorKind | Why a database operation failed (see DbError). |
data Many | The rows of the table T that point to this row with their ref field (§12.8, D-4780) |
type many | The lower-case spelling of Many<T> used in field declarations |
data Query | A query being built from from x in db.T … |
data Ref | Relation to one row of the table T (§12.8, D-4780) |
type ref | The lower-case spelling of Ref<T> used in field declarations |
type Table | псевдоним · std.orm |