Something went wrong. Try again.
SQL frontend over the catalog engine
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*)
(** Triggers: a program a store keeps as the text of its [CREATE TRIGGER] ({!Catalog.Spi.definitions}), checked when it is created and fired by the writes of its table (lang_createtrigger.html).
Firing takes four steps: {!of_definitions} parses the text, {!matching} selects the triggers of a write, {!bindings} makes the [OLD] and [NEW] rows visible to the body, and {!Make.fire} runs the bodies, each as a program of the connection ({!Connection.program}). The statement executor that runs a body is passed to each firing. *)
val check_create : target:[ `Table | `View ] option -> taken:bool -> Ast.create_trigger -> bool(** [check_create ~target ~taken tg] is [true] where [tg] is to be defined, given what its table names ([None] for nothing) and whether a trigger of its name exists, and [false] for a [CREATE TRIGGER IF NOT EXISTS] of a taken name.
@raise Catalog.Error in sqlite3's words, asked in sqlite3BeginTrigger's order: no such table, the name taken, a timing its target cannot have, a placeholder in its body. *)
type event = [ `Insert | `Delete | `Update of string list ](** The type for trigger events. [`Update cols] carries the columns the [UPDATE] assigns, which [UPDATE OF] triggers match. *)
val of_sql : string -> Ast.create_trigger option(** [of_sql sql] is the trigger [sql] defines, or [None] where [sql] is not one [CREATE TRIGGER] statement. *)
val of_definitions : (int64 * Catalog.Spi.Definition.t) list -> Ast.create_trigger list(** [of_definitions defs] is the triggers of [defs], in their order, parsed from their [CREATE TRIGGER] text. A trigger whose text is not one [CREATE TRIGGER] statement is skipped rather than refused. *)
val matching : Ast.create_trigger list -> timing:Ast.trigger_timing -> event:event -> table:string -> Ast.create_trigger list(** [matching tgs ~timing ~event ~table] is the triggers of [tgs] on [table] with [timing] and [event], newest first. [UPDATE OF cols] matches if the [UPDATE] assigns one of [cols], and a plain [UPDATE] matches any [UPDATE]. [tgs] is in the order the store keeps them, and the result is its reverse, as sqlite3 adds each trigger at the head of the list of its table. *)
val bindings : cols:string list -> old:Ast.value list option -> ?old_rowid:int64 -> new_:Ast.value list option -> ?new_rowid:int64 -> unit -> Query.binding list(** [bindings ~cols ~old ~old_rowid ~new_ ~new_rowid ()] is the scope that binds the [OLD] row [old] and the [NEW] row [new_], with columns [cols]. Only [OLD.x] and [NEW.x] read them: an unqualified column in a body is a column of the row of its own statement. A [None] row binds nothing, so [NEW.x] does not resolve in a [DELETE] trigger. [old_rowid] and [new_rowid] bind the rowid slot ({!Resolve.rowid_sentinel}), so [OLD.rowid] resolves under each of its names. *)
val before_update_row : Ast.create_trigger list -> Table.t -> changed:string list -> ?rowid:int64 -> Catalog.Value.t list -> Catalog.Value.t list(** [before_update_row tgs tbl ~changed ~rowid row] is the [NEW] row a BEFORE UPDATE trigger of [tgs] reads when an [UPDATE] assigning [changed] rewrites a row of [tbl] to [row]: sqlite3 loads a column the [UPDATE] does not assign only where such a trigger reads it as [NEW.x] (or any column, where it reads one past the 32nd), [NULL] otherwise, and computes the generated columns from that row with [rowid] (update.c, newmask). [row] itself where [tbl] generates no column or no such trigger fires. *)
val step_policy : Connection.frame -> Ast.insert_conflict -> Ast.insert_conflict(** [step_policy f own] is the conflict policy of a step whose [OR] clause is [own], run in [f]: [f.orconf], or [own] if [f.orconf] is [Unspecified] (trigger.c, codeTriggerProgram). *)
val raised : Connection.t -> Ir.raise_action -> Ast.value option -> bool(** [raised c action message] handles [RAISE(action, message)]. It is [false] for [IGNORE], which abandons the trigger program and keeps its earlier writes.
@raise Catalog.Error through {!Connection.fail}, with [message], or [""] if it is NULL, for [ABORT], [FAIL] and [ROLLBACK]. *)
val program : Connection.t -> orconf:Ast.insert_conflict -> (unit -> 'a) -> 'a(** [program c ~orconf f] is [f ()] run as a program of no name, as sqlite3 runs a foreign-key action (fkey.c, sqlite3FkActions): pushed on [c]'s stack with no [OLD] or [NEW] and the policy [orconf], and never skipped as running ({!Connection.program}).
@raise Catalog.Error ["triggers nested too deep"] where the stack is full.*)
(** {1:fire Firing} *)
(** What firing asks of the writer of a row. *)module type S = sig type t (** The type for writers. *)
val triggers : t -> Ast.create_trigger list (** [triggers w] is the parsed triggers of the schema [w] writes. *)
val connection : t -> Connection.t (** [connection w] is the connection [w] writes through: each program a firing runs is pushed on its stack ({!Connection.program}), and a [RAISE()] is raised through it as a constraint violation is ({!Connection.fail}). *)
val catalog : t -> Query.catalog (** [catalog w] is the catalog [WHEN] clauses are evaluated against. *)end
module Make (R : S) : sig val fire : R.t -> run_body:(R.t -> Ast.stmt -> unit) -> orconf:Ast.insert_conflict -> timing:Ast.trigger_timing -> event:event -> table:string -> cols:string list -> old:Ast.value list option -> ?old_rowid:int64 -> new_:Ast.value list option -> ?new_rowid:int64 -> unit -> bool (** [fire w ~run_body ~orconf ~timing ~event ~table ~cols ~old ~old_rowid ~new_ ~new_rowid ()] runs the triggers matching the write of one row and is [false] iff a [RAISE(IGNORE)] abandons them, in which case the later triggers do not run for this row. Only a [BEFORE] firing can skip the write. Each trigger runs as a program of the connection ({!Connection.program}) whose scope is the {!bindings} of the row and whose policy is [orconf], the conflict policy of the firing statement. Unless {!Connection.recursive_triggers}, a trigger whose program is already on the stack is skipped. [run_body] runs a body statement.
@raise Catalog.Error ["triggers nested too deep"] if a trigger would run on a full stack. *)end