Something went wrong. Try again.
SQL frontend over the catalog engine
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*)
(** Writes over a {!Connection.t}.
An [INSERT], [UPDATE] or [DELETE] computes each row it writes on the register VM and hands it to the connection's store as its {!Table} stores it: under its columns' affinities, with the [DEFAULT] of an omitted column, its generated columns computed and a [STRICT] table's types checked. Each row meets its table's constraints before the store sees it, settled as {!Conflict} settles them: [NOT NULL], [CHECK], then each uniqueness constraint, whose rows in the way are read off the store and which an [OR] clause, the constraint's own [ON CONFLICT] or an upsert resolves. A violation is raised through {!Connection.fail}. Each row the store takes is counted on the connection ({!Connection.add}), and each rowid an insert lands at becomes its {!Connection.last_insert_rowid}. Under {!Connection.foreign_keys} each row is counted against the foreign keys of its table and of the tables that reference it before the store sees it, and a parent row deleted or rewritten takes their actions once the store holds the change ({!Foreign_key}); the statement's keys are located before a row is read.
A statement's names and calls are checked as sqlite3 checks them while it prepares the statement, before a row is read: {{!prepare}the checks} are the ones every engine over a catalog runs. *)
val insert : Connection.t -> Ast.insert -> Ast.value list list(** [insert c i] writes the rows of [i]'s [VALUES], [SELECT] or [DEFAULT VALUES] into its table and is its [RETURNING] list over each row it wrote, [[]] for none. A row in the way of a unique key is skipped, replaced or rewritten by [i]'s upsert as its resolution says; an upsert's target is resolved, or refused, before a row is read. A [SELECT] that reads no table the statement writes, under no [RETURNING], hands each row over to be written before the next is computed (insert.c, sqlite3Insert); otherwise every row is computed first.
@raise Catalog.Error in sqlite3's words where the source does not supply one value per column, a name of the column list is no column of the table, an upsert's target names no unique key, a [STRICT] column cannot hold its value, a row breaks a constraint whose resolution aborts, or the store refuses a row. *)
val update : Connection.t -> Ast.update -> Ast.value list list(** [update c u] rewrites the rows of [u]'s table its [WHERE] selects, under its [WITH] and joined with its [FROM], with its [SET] values, and is its [RETURNING] list over each row as rewritten. Every row is found before the first is rewritten. An assignment to the rowid on a table with no [INTEGER PRIMARY KEY] moves the row to that key.
@raise Catalog.Error on a name that resolves to nothing, a [STRICT] column that cannot hold its value, or a row the store refuses. *)
val delete : Connection.t -> Ast.delete -> Ast.value list list(** [delete c d] removes the rows of [d]'s table its [WHERE] selects, under its [WITH], and is its [RETURNING] list over each row as it was. Every row is found before the first is removed. *)
(** {1:prepare What a statement checks as it is prepared} *)
val check_statement : Query.catalog -> Ast.stmt -> where:Ast.expr option -> returning:Ast.result_col list -> Ast.expr list -> unit(** [check_statement cat st ~where ~returning exprs] resolves the names of [st] ({!Resolve.check_statement}) and checks the calls of its [SET] expressions [exprs], its [where], its [returning] and the selects they hold ({!Query.check_expr_calls}).
@raise Catalog.Error as sqlite3 refuses the statement. *)
val check_insert_calls : Query.catalog -> Ast.insert -> unit(** [check_insert_calls cat i] checks the calls of [i]'s [VALUES] rows and its [RETURNING] list: a single row as a list of expressions, several as the select sqlite3 codes them as ({!Query.check_select}), which refuses an aggregate among them as ["misuse of aggregate: f()"].
@raise Catalog.Error as sqlite3 refuses the statement. *)
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] resolves every name [returning] reads, with the placeholders bound to [params], against the row of [table], whose columns are [cols] and which has a rowid iff [rowid], before the first row moves. [[]] checks nothing.
@raise Catalog.Error for a name the row does not have. *)
val collated : Query.catalog -> frame:string -> table:string -> Ast.expr -> Ast.expr(** [collated cat ~frame ~table e] is [e] with the declared [COLLATE] of each column of the row [frame] of [table] made explicit, for an expression run over one named row rather than a [FROM] ({!Resolve.collate_over}). *)
val check_do_update : Query.catalog -> table:string -> cols:string list -> rowid:bool -> constrained:bool -> Ast.upsert -> unit(** [check_do_update cat ~table ~cols ~rowid ~constrained up] resolves the [SET] and [WHERE] of [up]'s [DO UPDATE] over [table], whose columns are [cols] and which has a rowid iff [rowid], as sqlite3 prepares them: each assignment's value before its column, none admitting an aggregate or a window function. A table with no uniqueness constraint ([constrained] is [false]) never codes the clause, so nothing is checked.
@raise Catalog.Error as sqlite3 refuses the statement. *)
val check_set_column : cols:string list -> rowid:bool -> string -> unit(** [check_set_column ~cols ~rowid c] accepts the assignment of an [UPDATE] or a [DO UPDATE] to [c] over a row with columns [cols], which has a rowid iff [rowid]: a column of the row, or a spelling of the rowid no column takes.
@raise Catalog.Error ["no such column: c"] otherwise. *)
(** {1:fk Foreign keys of the schema} *)
val drop_deletes : Connection.t -> string -> bool(** [drop_deletes c table] is [true] iff a [DROP TABLE] of [table] codes the [DELETE] sqlite3FkDropTable runs before the drop ({!Foreign_key.Make.drop_deletes}), which counts its rows. *)
val delete_for_drop : Connection.t -> string -> unit(** [delete_for_drop c table] deletes every row of [table] where that [DELETE] runs ({!Foreign_key.Make.drop_delete_runs}), each through the row program of a [DELETE] that fires no trigger: the foreign keys referencing it are counted and their actions taken. *)
val foreign_key_violations : Connection.t -> string list -> Foreign_key.violation list(** [foreign_key_violations c names] is {!Foreign_key.Make.violations} of the tables [names] of [c]'s catalog.
@raise Catalog.Error as it raises. *)
(** {1:autoinc AUTOINCREMENT counters} *)
val register_programs : Connection.t -> Ast.stmt -> unit(** [register_programs c stmt] registers with the statement [stmt] about to run on [c] the counter of every [AUTOINCREMENT] table its programs could insert into ({!Autoinc.Programs}), read from the store's [sqlite_sequence], where it has one. Each key an [INSERT] then draws or names raises its table's counter. *)
val flush_counters : Connection.t -> unit(** [flush_counters c] writes the counters of the statement that completed on [c] back to [sqlite_sequence] ({!Autoinc.flush}). *)
val discard_counters : Connection.t -> unit(** [discard_counters c] drops the counters of the statement an error halted on [c], unwritten ({!Autoinc.discard}). *)