OxL Справочник OxL
РусскийEnglish

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.

oxl
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 1

Queries 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:

oxl✓ проверяется в CI
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 DbThe handle of an open database (Db.open(url)); the type itself is declared in the prelude.
type DbErrorA failed database operation
type DbErrorKindWhy a database operation failed (see DbError).
data ManyThe rows of the table T that point to this row with their ref field (§12.8, D-4780)
type manyThe lower-case spelling of Many<T> used in field declarations
data QueryA query being built from from x in db.T …
data RefRelation to one row of the table T (§12.8, D-4780)
type refThe lower-case spelling of Ref<T> used in field declarations
type Tableпсевдоним · std.orm

См. также