(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*) (** INSERT, DELETE and UPDATE. A row write takes these steps in order: align the row to the columns, compute the generated columns, apply [NOT NULL], check [CHECK] and the foreign keys, fire the [BEFORE] triggers, resolve uniqueness, write, and fire the [AFTER] triggers. [Write] also runs [INSERT ... ON CONFLICT] and [UPDATE ... FROM]. It works on a {!Handle.t}, reads the schema through {!Cat} and stores rows through {!Rows}. *) (** Row storage on a handle: {!Store.Make} applied to {!Handle.t}, with the cached largest rowid and the [AUTOINCREMENT] counter of {!Autoinc}. *) module Rows : sig val addr : Model.generic_table -> int64 -> Sql.Ast.value list -> Store.addr (** [addr gt rowid row] is {!Store.Make.addr}. *) val insert : Handle.t -> table:string -> ?key:Store.key -> ?resolved:bool -> Sql.Ast.value list -> int64 (** [insert db ~table ~key ~resolved row] is {!Store.Make.insert}. *) val update : Handle.t -> table:string -> reals:bool -> touched:(Model.index -> bool) -> ?old:Sql.Ast.value list -> ?at:Btree.Table.Cursor.t -> Store.addr -> ?to_rowid:int64 -> Sql.Ast.value list -> unit (** [update db ~table ~reals ~touched ~old ~at a ~to_rowid row] is {!Store.Make.update}. *) val delete : Handle.t -> table:string -> ?old:Sql.Ast.value list -> ?at:Btree.Table.Cursor.t -> Store.addr -> unit (** [delete db ~table ~old ~at a] is {!Store.Make.delete}. *) val read : Handle.t -> string -> int64 -> Sql.Ast.value list option (** [read db name rowid] is {!Store.Make.read}. *) end val install : insert_select_iter: (Handle.t -> Sql.Private.Query.catalog -> Sql.Ast.value array -> Sql.Private.Query.binding list -> Sql.Ast.select -> f:(Sql.Ast.value list -> unit) -> unit) -> unit (** [install ~insert_select_iter] sets the function that hands each source row of an [INSERT ... SELECT] over as it is computed, provided by the [SELECT] executor. Until it is called, an [INSERT ... SELECT] fails with [Failure]. *) val exec_insert : Handle.t -> Sql.Private.Query.catalog -> Sql.Ast.value array -> run_trigger_body:(Handle.t -> Sql.Ast.stmt -> unit) -> Sql.Ast.insert -> (int64 option * Sql.Ast.value list) list (** [exec_insert db cat params ~run_trigger_body i] runs [i] and is the inserted rows, with all their columns and the [INTEGER PRIMARY KEY] column set, for its [RETURNING] clause, and [[]] without one. An [ON CONFLICT] clause skips or updates the conflicting rows. *) val register_programs : Handle.t -> Sql.Ast.stmt -> unit (** [register_programs db stmt] registers with the running statement ({!Autoinc.register}) the [AUTOINCREMENT] counter of every table an [INSERT] of a program [stmt] could run names, in the order sqlite3 codes them: [stmt]'s own [INSERT], the trigger programs its writes could fire, [WHEN] clauses and all, the foreign-key actions and the deletes a [REPLACE] makes, each followed through the programs they in turn could run. *) val foreign_key_violations : Handle.t -> string list -> Sql.Private.Foreign_key.violation list (** [foreign_key_violations db names] is {!Sql.Private.Foreign_key.Make.violations} over [db]: the rows [PRAGMA foreign_key_check] lists for the tables [names]. *) val drop_deletes : Handle.t -> string -> bool (** [drop_deletes db table] is {!Sql.Private.Foreign_key.Make.drop_deletes} over [db]: a [DROP TABLE] of [table] codes the [DELETE] sqlite3FkDropTable runs. *) val drop_delete_runs : Handle.t -> string -> bool (** [drop_delete_runs db table] is {!Sql.Private.Foreign_key.Make.drop_delete_runs} over [db]: that [DELETE] runs. *) val foreign_key_plan : Handle.t -> string -> changed:string list option -> string list (** [foreign_key_plan db table ~changed] is the [EXPLAIN QUERY PLAN] line of each loop over a child table that the foreign keys referencing [table] run when a row of it is deleted ([changed] is [None]) or an [UPDATE] assigns [changed] (fkey.c, fkScanChildren): one per key, the newest first, and for an [UPDATE] two passes of the keys whose parent columns it assigns. Each is a [SEARCH] where a rowid or an index serves the child columns, else a [SCAN]. It is [[]] without foreign keys enforced. @raise Catalog.Error [{v foreign key mismatch - "child" referencing "parent" v}] if the parent columns of a key it reaches are not a key of [table]. *) val clears : Handle.t -> string -> bool (** [clears db table] is [true] iff a [DELETE FROM table] with no [WHERE] and no [RETURNING] empties [table] at once, as sqlite3 does (delete.c, sqlite3DeleteFrom): [table] has no [DELETE] trigger and no foreign key {!Sql.Private.Foreign_key.Make.required} counts. *) val delete_for_drop : Handle.t -> run_trigger_body:(Handle.t -> Sql.Ast.stmt -> unit) -> string -> unit (** [delete_for_drop db ~run_trigger_body table] deletes every row of [table] as the [DELETE FROM] a [DROP TABLE] runs first under foreign keys does (fkey.c, sqlite3FkDropTable): each through the row program of a delete, with [table]'s own triggers off and the foreign-key actions on, counted as changes. *) val exec_delete : ?select: (Sql.Ast.select -> Sql.Ast.value array -> Sql.Ast.value list list option) -> ?scan:(Sql.Ast.select -> f:(Sql.Ast.value list -> unit) -> bool) -> Handle.t -> Sql.Private.Query.catalog -> Sql.Ast.value array -> run_trigger_body:(Handle.t -> Sql.Ast.stmt -> unit) -> Sql.Ast.delete -> (int64 option * Sql.Ast.value list) list (** [exec_delete ~select ~scan db cat params ~run_trigger_body d] runs [d] and is the deleted rows, in table order, with their values before the delete, for its RETURNING; without one the answer may leave them out. With [select], the rows a [WHERE] keeps are read by running the select [SELECT rowid, cols FROM table WHERE ...] through it, with [params] and, in a trigger's step, the values of its OLD and NEW bound after them, so they take a query's access path; [select] answers [None] to decline. With [scan], which hands each row of that select to [f] as it reads it, or answers [false] having handed none, a delete whose rows nothing but itself writes removes each row as the scan reaches it, as sqlite3's one-pass DELETE does. *) val exec_update : ?select: (Sql.Ast.select -> Sql.Ast.value array -> Sql.Ast.value list list option) -> ?scan: (refused:(string -> bool) -> Sql.Ast.select -> f:(Sql.Ast.value list -> unit) -> bool) -> Handle.t -> Sql.Private.Query.catalog -> Sql.Ast.value array -> run_trigger_body:(Handle.t -> Sql.Ast.stmt -> unit) -> Sql.Ast.update -> (int64 option * Sql.Ast.value list) list (** [exec_update ~select ~scan db cat params ~run_trigger_body u] runs [u] and is the updated rows, in table order, with their new values, for its RETURNING; without one the answer may leave them out. [select] and [scan] are as in {!exec_delete}, for an UPDATE with no [FROM]; [scan] is handed [refused], the indexes the SET reaches, and declines a walk of one, as sqlite3 rewrites in one pass unless the index its loop walks is one the UPDATE writes. *) val store_row : Handle.t -> run_trigger_body:(Handle.t -> Sql.Ast.stmt -> unit) -> gt:Model.generic_table -> checks:Sql.Private.Conflict.checks -> conflict:Sql.Ast.insert_conflict -> Sql.Ast.value list -> (int64 * Sql.Ast.value list) option (** [store_row db ~run_trigger_body ~gt ~checks ~conflict row] stores [row], one value per column each stored by its column's affinity ({!Row.store_values}), in [gt] with every step of an insert, in sqlite3's order: the [BEFORE] triggers, the key, the generated columns ({!Row.generate}), [NOT NULL], [CHECK], the uniqueness constraints, the foreign keys, the write and the [AFTER] triggers. It is the rowid and the stored row for [RETURNING], or [None] if a conflict resolution or a [BEFORE] trigger drops the row. [checks] is the [CHECK] constraints of [gt] and [conflict] the [OR] clause of the statement. *) val remove_row : Handle.t -> run_trigger_body:(Handle.t -> Sql.Ast.stmt -> unit) -> table:string -> cols:string list -> ?held:bool -> Store.addr -> Sql.Ast.value list -> bool (** [remove_row db ~run_trigger_body ~table ~cols ~held a row] deletes the row at [a], whose values are [row], with every step of a delete, in sqlite3's order: the [BEFORE] triggers, the foreign keys that reference it, the removal and the [AFTER] triggers. It is [false] iff a [BEFORE] trigger raises [IGNORE] or removes the row itself, and the row is not deleted here. [held] (default [false]) says [row] is the row stored at [a], as {!Rows.read} answers it, so the removal does not read it again where nothing has written since. *)