(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*) (** Queries and expressions over a {!Catalog.Spi.t}. [Engine] lowers and runs queries over any store that implements {!Catalog.Spi}, such as a B-tree, the in-memory reference backend or an Irmin tree. {!run_select} runs the whole read dialect on the register VM: joins, aggregates with [GROUP BY] and [HAVING], window functions, compounds such as [UNION], [WITH] including recursive ones, correlated and [FROM] subqueries, and [VALUES] and table-function sources. {{!write_path}The write path} evaluates the expressions of a write; [Write] runs the writes themselves and [Sql.exec] the statements. *) val unsupported : string -> 'a (** [unsupported what] raises [Catalog.Error (Unsupported _)] naming [what], a shape of statement or expression this engine does not run. *) val unlowerable : string -> (unit -> 'a) -> 'a (** [unlowerable what f] is [f ()], with a shape the register VM does not compile ({!Ir.Unsupported}) raised as {!unsupported}[ what]. *) val run_select : ?scope:Query.binding list -> Query.catalog -> Ast.select -> Ast.value array -> Ast.value list list (** [run_select ~scope cat s params] is the rows of the [SELECT] [s] over [cat], run on the register VM. A backend builds [cat] with {!val-catalog} over its {!Catalog.Spi.t}. [scope] is an enclosing row, such as the outer row of a correlated subquery or the [OLD] and [NEW] rows of a trigger, substituted before the query runs. @raise Catalog.Error on a correlated subquery it cannot substitute. *) val iter_select : ?scope:Query.binding list -> Query.catalog -> Ast.select -> Ast.value array -> f:(Ast.value list -> unit) -> unit (** [iter_select ~scope cat s params ~f] calls [f] on each row of [run_select ~scope cat s params], in that order, each as the register VM produces it where the rows need not be computed whole first: a select that folds no group or window and has no [ORDER BY], [DISTINCT] or [OFFSET], and each arm of a [UNION ALL] that has none of its own. An exception of [f] stops the run and is raised again. *) val select_runner : Query.catalog -> Query.binding list -> Ast.select -> Ast.value array -> Ast.value list list (** [select_runner cat scope s params] is [run_select ~scope cat s params], in the shape of {!Query.field-subquery}. A backend that builds a {!Query.catalog} field by field puts it, or a runner of its own, in that field. *) val catalog : Connection.t -> Query.catalog (** [catalog c] is {!Query.of_catalog} of [c]'s catalog with {!select_runner} as its subquery runner, [c]'s cap on a recursive CTE, and [c]'s answers to the functions and the table-valued functions of a connection ({!Connection.call}, {!Connection.table_function}), reading the views and the schema table its store keeps ({!View.over}, {!Schema_table.over}), and a table-valued function of the connection as a table of its name where no other relation holds the name. *) val combine_compound : Query.catalog -> Ast.value array -> Ast.select -> Ast.value list list -> (Ast.set_op * Ast.value list list) list -> Ast.value list list (** [combine_compound cat params s base_rows compound_rows] is the result of the compound select [s] from the rows of its arms: [base_rows] for the first arm and one [(op, rows)] for each following arm. It applies the set operators, then the [ORDER BY], [LIMIT] and [OFFSET] of [s] on the register VM, so a backend can compute the rows of each arm its own way. @raise Catalog.Error if an [ORDER BY] term names no result column (lang_select.html section 4). *) (** {1:write_path The write path} These functions evaluate the expressions of a write: an [INSERT ... VALUES] tuple, a [SET] assignment, a [WHERE], the [DO UPDATE] of an upsert, a [RETURNING] item and an [UPDATE ... FROM] join. Each runs on the register VM over rows the caller holds, one cursor of one row for each named row, so a backend works on values and never evaluates SQL itself. Each raises {!Catalog.exception-Error} on an expression the VM does not compile. *) val from_bindings : ?scope:Query.binding list -> Query.catalog -> Ast.value array -> Ast.from_clause -> Ast.join list -> Query.binding list list (** [from_bindings ~scope cat params from joins] is the rows of the join [FROM from joins], with no projection and no [WHERE], each named by source so that it can serve as an outer scope. It is the join of an [UPDATE ... FROM] (lang_update.html), run on the access paths {!Query.plan_sources} picks. [scope] is an enclosing row, such as the [OLD] and [NEW] rows of a trigger. *) val constants : Query.catalog -> Ast.value array -> Ast.expr list -> Ast.value list (** [constants cat params exprs] is the value of each of [exprs] over the one empty row a [VALUES] tuple is evaluated over: a column name among them resolves against nothing and is refused. *) val eval_const : ?scope:Query.binding list -> Query.catalog -> Ast.value array -> Ast.expr -> Ast.value (** [eval_const ~scope cat params e] is the value of [e] with no table row, such as an [INSERT ... VALUES] cell, a column [DEFAULT] or the [WHEN] of a trigger. A column reference raises {!Catalog.exception-Error} unless [scope] binds it. *) val eval_in_row : ?scope:Query.binding list -> ?rowid:int64 -> ?stored:Self_ref.t -> Query.catalog -> Ast.value array -> string -> string list -> Ast.value list -> Ast.expr -> Ast.value (** [eval_in_row ~scope ~rowid ~stored cat params table cols vals e] is the value of [e] over the row [vals] of [table], with [vals] aligned to [cols]. It serves an [UPDATE ... SET] value, a [CHECK], a generated column and a partial index predicate. [scope] adds outer bindings. [rowid] is the rowid of the row; without it, [e] cannot name the rowid. [stored] says that [e] comes from the schema and which kind of expression it is. A date function that reads the clock ({!Func.reads_the_clock}) then fails with an error such as [non-deterministic use of julianday() in a CHECK constraint]. [stored] changes nothing else. *) val eval_upsert : ?scope:Query.binding list -> Query.catalog -> Ast.value array -> table:string -> cols:string list -> current:Ast.value list -> excluded:Ast.value list -> Ast.expr -> Ast.value (** [eval_upsert ~scope cat params ~table ~cols ~current ~excluded e] is the value of [e] with the values [current] of the conflicting row bound under [table] and the values [excluded] of the row being inserted bound under [excluded], both aligned to [cols]. It serves the [DO UPDATE] assignments and [WHERE] of an upsert (lang_upsert.html). *) val row_values : ?scope:Query.binding list -> ?stored:Self_ref.t -> Query.catalog -> Ast.value array -> string -> string list -> rowid:bool -> Ast.expr list -> ?rowid:int64 -> Ast.value list -> Ast.value list (** [row_values ~scope ~stored cat params table cols ~rowid exprs] evaluates [exprs] over rows of [table] as {!eval_in_row} does, lowered once for every row it is applied to rather than once a row: applied to [?rowid vals] it is the value of each of [exprs] over the row [vals], aligned to [cols]. [rowid] says that every row comes with its rowid. The lowering happens when [row_values] is applied to [exprs], as sqlite3 codes a statement when it prepares it, so what it refuses is refused over no row at all; a shape the engine does not model is refused at the first row. [scope] is read once, when [row_values] is applied to [exprs]. *) val row_filter : ?scope:Query.binding list -> Query.catalog -> Ast.value array -> string -> string list -> rowid:bool -> Ast.expr option -> ?rowid:int64 -> Ast.value list -> bool (** [row_filter ~scope cat params table cols ~rowid where] is {!row_matches} lowered once for all the rows it is applied to, as {!row_values} is. *) val row_matches : ?scope:Query.binding list -> ?rowid:int64 -> Query.catalog -> Ast.value array -> string -> string list -> Ast.expr option -> Ast.value list -> bool (** [row_matches ~scope ~rowid cat params table cols where vals] is [true] iff the row [vals] of [table], aligned to [cols], satisfies [where]. [None] matches every row. It picks the rows of a [DELETE] or an [UPDATE]. [scope] and [rowid] are as in {!eval_in_row}. *) val returning_rows : Query.catalog -> Ast.value array -> table:string -> cols:string list -> Ast.result_col list -> (int64 option * Ast.value list) list -> Ast.value list list (** [returning_rows cat params ~table ~cols returning rows] is the [RETURNING] list [returning] evaluated over each affected row of [table], whose values [cols] names. [*] is the whole row (lang_returning.html). Each row comes with its rowid, or [None] for a table without one, in which case a [RETURNING] item cannot name it. *) val check_returning : Query.catalog -> Ast.value array -> table:string -> cols:string list -> rowid:bool -> Ast.result_col list -> unit (** [check_returning cat params ~table ~cols ~rowid returning] checks, before any row exists, that every name [returning] reads is a column of [cols], or the rowid if [rowid] is [true]. A statement with a [RETURNING] list calls it when it is prepared, as sqlite3 does, so a bad name fails before the table changes. @raise Catalog.Error for a name the row does not have. *) (** {1:schema Storage schemas} *) val table_schema : ?source:string -> ?indexes:Catalog.Spi.Schema.index list -> Ast.create_table -> Catalog.Spi.Schema.t (** [table_schema ~source ~indexes ct] is the table [ct] declares as a storage schema: its columns with their constraints, [DEFAULT] and generated expression, its [PRIMARY KEY] as written, its [UNIQUE], [CHECK] and foreign key constraints, its table options, and [indexes] (defaults to none). An expression is the text [source], the statement [ct] was parsed from, holds for it where given, else [ct]'s expression printed ({!Render.expr}). @raise Catalog.Error as sqlite3 refuses a table it cannot build. *) val index_schema : Ast.create_index -> Catalog.Spi.Schema.index (** [index_schema ci] is the index [ci] declares as a storage schema index, its expressions and [WHERE] printed ({!Render.expr}). *)