Something went wrong. Try again.
SQL frontend over the catalog engine
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*)
(** Tables as a write computes their rows.
A table is read once from its {!Catalog.Spi.Schema.t}: how each column stores a value, its [DEFAULT] and its generation expression. A row a statement writes takes them before the store sees it, as sqlite3's VDBE applies them before its b-tree: each value converted by its column's affinity (OP_Affinity), an omitted column given its [DEFAULT] (sqlite3ColumnDefault), the generated columns computed from the rest (sqlite3ComputeGeneratedColumns) and a [STRICT] table's types checked (OP_TypeCheck). Nothing here reads a relation. *)
(** {1:storage Storage} *)
(** The type for how a column stores a value: by the affinity of its declared type in an ordinary table, or as the type it declares in a [STRICT] one. *)type storage = Affinity of Value_ops.affinity | Strict of Strict.t
val affinity : storage -> Value_ops.affinity(** [affinity s] is the affinity a value written to a column stored as [s] is converted by: that of its declared type, or that of its [STRICT] type ({!Strict.affinity}). *)
(** {1:tables Tables} *)
type t(** The type for tables. *)
val v : ?encoding:Encoding.t -> Catalog.Spi.Schema.t -> t(** [v ~encoding s] is the table [s] declares, its text stored in [encoding] (defaults to {!Encoding.Utf8}): its [DEFAULT], generation and row expressions order text and read its bytes under it. What [v] derives from [s] is worked out on first use, so a table whose [DEFAULT] cannot be evaluated stays readable. *)
val name : t -> string(** [name t] is the name of [t]. *)
val schema : t -> Catalog.Spi.Schema.t(** [schema t] is the schema [t] was made from. *)
val columns : t -> string list(** [columns t] is the column names of [t], in declared order. *)
val storage : t -> storage list(** [storage t] is how each column of [t] stores a value, in declared order: its [STRICT] type in a [STRICT] table, the affinity of its declared type otherwise.
@raise Catalog.Error ["missing datatype for t.c"] or ["unknown datatype for t.c: \"x\""] for a [STRICT] column that declares none of the six types. *)
val is_strict : t -> bool(** [is_strict t] is [true] iff [t] is a [STRICT] table, whose values {!check} checks. *)
val rowid_alias : t -> int option(** [rowid_alias t] is {!Catalog.Spi.Schema.rowid_alias} of [t]'s schema. *)
val catalog : t -> Query.catalog(** [catalog t] is the catalog an expression over one row of [t] is evaluated through: a generation expression, a partial index predicate, an expression key. It reaches no relation ({!Query.closed}), and answers the collating sequence and affinity of [t]'s columns. *)
val collate : t -> Ast.expr -> Ast.expr(** [collate t e] is the expression [e] over one row of [t], in scope under [t]'s name, with the collating sequence of each column it compares made explicit, which is how a comparison reads it. *)
(** {1:values Values} *)
val expression : t -> what:string -> string -> Ast.expr(** [expression t ~what text] is the SQL [text] a store holds for an expression over one row of [t], such as an index key or a partial index's [WHERE], parsed and {!collate}d.
@raise Catalog.Error naming [what] if [text] does not parse. *)
val coerce : t -> Catalog.Value.t list -> Catalog.Value.t list(** [coerce t values] is each value of a row of [t] converted by its column's {!affinity}, with no type checked: a row's datatypes are checked once its [NOT NULL] constraints have passed ({!check}).
@raise Invalid_argument if [values] does not have one value per column. *)
val check : t -> Catalog.Value.t list -> unit(** [check t values] checks each value of a row of [t] against the type its [STRICT] column declares, as sqlite3's OP_TypeCheck does. It checks nothing outside a [STRICT] table.
@raise Catalog.Error [Constraint (Datatype _)] naming the leftmost column whose type refuses its value. *)
val store_value : t -> int -> Catalog.Value.t -> Catalog.Value.t(** [store_value t j v] is [v] converted by the {!affinity} of column [j], so a NUMERIC column stores [42] and a TEXT column ['42'], and checked against its [STRICT] type.
@raise Catalog.Error if a [STRICT] column [j] cannot hold [v]. *)
(** {1:defaults Defaults} *)
val defaults : t -> Catalog.Value.t array(** [defaults t] is the [DEFAULT] of each column of [t], NULL where it declares none, evaluated once on first use, before the column's affinity. A write that takes a default per row, which [CURRENT_TIMESTAMP] or [random()] tell apart, asks {!default}. *)
val default : ?scope:Query.binding list -> t -> Query.catalog -> Catalog.Value.t array -> string -> Catalog.Value.t(** [default ~scope t cat params c] is the [DEFAULT] of the column [c] of [t] evaluated over [cat] with the parameters [params] and the outer row [scope], as a write that omits [c] stores it, or NULL where [c] declares none or [t] has no such column. *)
val settle : Value_ops.affinity -> Ast.expr -> Catalog.Value.t option(** [settle aff e] is the [DEFAULT] [e] settled from its text under the affinity [aff], as sqlite3 settles the default a row older than its column reads (sqlite3ValueFromExpr), or [None] if [e] does not settle: a literal, a sign or a CAST over one settle, and nothing else does. *)
(** {1:generated Generated columns} *)
val generated : t -> (string * Ast.expr) list(** [generated t] is the expression of each generated column of [t], VIRTUAL and STORED, {!collate}d. A read computes the VIRTUAL ones and a write both.
@raise Catalog.Error if a generation expression does not parse. *)
(** {1:checks CHECK constraints} *)
val checks : t -> (string * Ast.expr) list(** [checks t] is each [CHECK] constraint of [t], in declared order, as its source text, which a violation names, and its expression, {!collate}d. A store holds the text; the engine evaluates it ({!Conflict.checks}).
@raise Catalog.Error if a constraint does not parse. *)
val place_rowid : t -> int64 -> Catalog.Value.t list -> Catalog.Value.t list(** [place_rowid t rowid row] is [row] with its [INTEGER PRIMARY KEY] column set to [rowid], or [row] if [t] declares none. *)
val generate : t -> ?rowid:int64 -> Catalog.Value.t list -> Catalog.Value.t list(** [generate t ~rowid row] is the row [row] being written to [t], one value per column and each stored by its column's affinity, completed as sqlite3 completes it before any constraint reads it: the [INTEGER PRIMARY KEY] set to [rowid], then every generated column, STORED and VIRTUAL, computed from the rest in the order their expressions read each other (insert.c, sqlite3ComputeGeneratedColumns). In a [STRICT] table with a generated column, the columns written are {!check}ed first. [rowid] defaults to the row's own key, and a table without a rowid has none.
@raise Catalog.Error where a [STRICT] column cannot hold what is written to it or what its expression computed, and [generated column loop on "c"] if generated columns form a cycle. *)
(** {1:rows Rows} *)
type row(** The type for a row of a table whose generated columns are computed on demand. It holds one array of values, refilled for each row it stands on. *)
val row : t -> row(** [row t] is a row of [t] on no values yet, whose VIRTUAL generated columns a read of them computes. *)
val start : row -> int64 option -> unit(** [start r rowid] puts [r] on a new row of rowid [rowid], [None] for a table without one: every generated column owes its value again. The caller then fills the stored columns' {!slots}. *)
val slots : row -> Catalog.Value.t array(** [slots r] is the values of [r], one per column, which a caller fills in place after {!start}. A generated column's slot is overwritten when it is computed. *)
val column : row -> int -> Catalog.Value.t(** [column r j] is the column [j] of [r]. A VIRTUAL generated column is computed here, once per row, after the columns it reads, and stored with its affinity and [STRICT] type, as {!store_value} does.
@raise Catalog.Error where a [STRICT] column cannot hold what its expression computed, or [generated column loop on "c"] if generated columns form a cycle. *)
val rowid : row -> int64 option(** [rowid r] is the rowid {!start} gave [r]. *)
val values : row -> Catalog.Value.t list(** [values r] is every column of [r], generated ones included, computed from left to right, so the leftmost bad column is refused first. *)
(** {1:insert Column lists} *)
val written_columns : t -> string list(** [written_columns t] is the columns of [t] that are not generated, in order: the columns an [INSERT] with no column list writes, one value each. *)
val check_arity : t -> named:string list -> int -> unit(** [check_arity t ~named n] checks that an [INSERT] into [t] supplies [n] values: one per name of the column list [named], or with [named] empty one per {!written_columns} (insert.c, sqlite3Insert).
@raise Catalog.Error in sqlite3's words otherwise. *)
type writer(** The type for what an [INSERT]'s column list asks of each row it writes, resolved against its table once for the statement. *)
val writer : t -> has_rowid:bool -> named:string list -> writer(** [writer t ~has_rowid ~named] is the writer of the column list [named] of an [INSERT] into [t], which has a rowid iff [has_rowid]. [t] keeps the writer of the list it was last asked for, so a statement run once per row resolves its list for the first row alone. *)
val write : writer -> default_for:(string -> Catalog.Value.t) -> Catalog.Value.t list -> Catalog.Value.t list * Catalog.Value.t option(** [write w ~default_for vals] is the full row, in declared order and each value converted by its column's affinity ({!coerce}), of the [INSERT] values [vals], and the value written for the rowid. With an empty list [vals] go in order to the {!written_columns}, and a generated column's slot is NULL until the row is {!generate}d. With a list, a column it does not name takes [default_for]; an entry naming the rowid, by a rowid name or the [INTEGER PRIMARY KEY], is the key, the last of them where several do (src/insert.c), and leaves the row unless it names that column; a column named twice takes its first value. A list naming the rowid alone writes every column's default.
@raise Catalog.Error on a name no column answers to or a wrong number of values ({!check_arity}), in sqlite3's words. *)