Something went wrong. Try again.
SQL frontend over the catalog engine
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*)
(** Expressions the schema stores and re-evaluates against one row of one table.
A CHECK constraint, a partial index predicate, an index key expression and a generated column are stored in [sqlite_master] and evaluated later against one row, with no query around them. So they may hold no subquery, no bound parameter, no aggregate, no window function, no non-deterministic function, no unknown function and no column outside the table. SQLite checks this in [sqlite3ResolveSelfReference] (src/resolve.c) and words each refusal by the context, which {!t} names. *)
(** The type for the contexts of stored expressions. *)type t = | Check_constraint (** [CREATE TABLE ... CHECK (e)]. *) | Partial_index (** [CREATE INDEX ... WHERE e]. *) | Index_expression (** [CREATE INDEX ... (e)]. *) | Generated_column (** [CREATE TABLE ... AS (e)]. *)
val pp : t Fmt.t(** [pp ppf t] formats [to_string t] on [ppf]. *)
val to_string : t -> string(** [to_string t] is the name of [t] in a refusal found when the expression is resolved, in SQLite's words: ["CHECK constraints"], ["partial index WHERE clauses"], ["index expressions"] or ["generated columns"]. *)
val in_ : t -> string(** [in_ t] is the name of [t] in a refusal raised when the expression runs, in SQLite's words: ["a CHECK constraint"], ["a generated column"] or ["an index"], for both index contexts. *)
val check : t -> ?rowid:bool -> table:string -> cols:string list -> Ast.expr -> unit(** [check t ~rowid ~table ~cols e] accepts [e] as an expression of context [t] over the table [table] with columns [cols], or raises. [rowid] is [true] if the table has an implicit rowid (defaults to [false]).
Three rules depend on the context, as in SQLite (src/resolve.c): - Only a CHECK constraint may call a non-deterministic function. - An index key expression and a generated column refuse the ["."] operator, and with it the names [rowid], [oid] and [_rowid_]. The other two contexts resolve a qualified name against [table]. - A CHECK constraint and a partial index predicate ignore a schema qualifier, which the parser drops ({!Ast.ignore_schema_qualifiers}). The other two contexts fail on one with ["no such column: s.t.c"].
@raise Catalog.Error worded as sqlite3 words it: ["subqueries prohibited in ..."], ["parameters prohibited in ..."], ["non-deterministic functions prohibited in ..."], ["the \".\" operator prohibited in ..."], ["misuse of aggregate function f()"], ["misuse of window function f()"], ["no such function: f"] or ["no such column: c"]. *)
val check_names : ?rowid:bool -> table:string -> cols:string list -> Ast.expr -> unit(** [check_names ~rowid ~table ~cols e] accepts [e] if its names resolve against the table [table] with columns [cols], or raises. [rowid] is as in {!check}. It refuses only an aggregate, a window function, an unknown function and an unknown column, as SQLite's [sqlite3ResolveExprNames] does without context flags. The [WHERE] of an [ON CONFLICT] target is checked this way. A qualifier naming an unknown schema fails with ["no such column: s.t.c"].
@raise Catalog.Error with the messages of {!check} for these four refusals.*)
val check_generated_loop : (string * Ast.expr) list -> unit(** [check_generated_loop gens] accepts the generated columns [gens] of a table, as [(column, expression)] pairs in declaration order, or raises if a generated column depends on itself, directly or through other generated columns.
@raise Catalog.Error [{v generated column loop on "c" v}], with [c] the last declared column that no order of computation reaches, as sqlite3 words it. *)