Something went wrong. Try again.
SQLite file format and SQL engine in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*)
(** Statement execution.
[Driver] parses a script, runs each statement, and runs each change as a statement of the handle's connection, atomic over the file store's transaction door ({!Sql.Private.Connection.atomic}). It runs DDL, [ALTER TABLE], trigger bodies and [RETURNING] clauses. Running a [SELECT] comes from the executor through {!S}. *)
val holds : Handle.t -> string -> bool(** [holds t name] is [true] iff [t] holds a table, view, index or trigger named [name]: the [known] a parse of a statement for [t] takes. *)
val parse_query : ?known:(string -> bool) -> string -> Handle.parsed_query(** [parse_query ~known sql] is the one [SELECT], [EXPLAIN] or row-returning [PRAGMA] statement of [sql], with [known] as {!Sql.Private.Lexer.parse_statements} reads it.
@raise Catalog.Error if [sql] does not parse. @raise Catalog.Error if [sql] is another statement, or more than one. *)
val parse_script : ?known:(string -> bool) -> string -> (string * Sql.Ast.stmt) Seq.t(** [parse_script ~known sql] is the statements of the [;]-separated script [sql], each with its source text and its [?] parameters numbered. Each statement is parsed when the sequence reaches it, from where the one before it ended, and [known] says which tables exist as {!Sql.Private.Lexer.script} reads it. The sequence is read once.
@raise Catalog.Error if a statement does not parse. *)
val script : Handle.t -> stmt:('s -> Sql.Ast.stmt) -> 's Seq.t -> ('s Seq.t -> 'a) -> 'a(** [script t ~stmt stmts f] runs [f stmts] once as one script door of [t] ({!Session.script_op}), in the write session from the start where the statement [stmt] reads of the first element of [stmts] writes, as sqlite3 opens its transaction for writing at such a statement. *)
(** The signature of the [SELECT] executor. *)module type S = sig type t = Handle.t
val vm_select_rows : ?memo:Handle.memo -> t -> Sql.Private.Query.catalog -> Sql.Ast.value array -> Sql.Ast.select -> Sql.Ast.value list list (** [vm_select_rows ~memo db cat params s] is the rows of [s]. [memo] is where the runs of the statement [s] keep the work they share. *)
val vm_select_iter : ?memo:Handle.memo -> t -> Sql.Private.Query.catalog -> Sql.Ast.value array -> Sql.Ast.select -> f:(Sql.Row.t -> unit) -> unit (** [vm_select_iter ~memo db cat params s ~f] calls [f] on each row of [s], as it is produced, readable only inside that call. [memo] is as for {!vm_select_rows}. *)
val explain_rows : t -> Sql.Ast.explain -> Sql.Ast.value list list (** [explain_rows db e] is the rows of [EXPLAIN QUERY PLAN]. *)
val dml_rows : t -> Sql.Ast.select -> Sql.Ast.value array -> Sql.Ast.value list list option (** [dml_rows db s params] is the rows of [s], the select a DELETE or an UPDATE finds its rows by, as a query computes them, or [None] if neither a fast path nor the VM runs [s]. [s] is built from the statement's WHERE with the declared sequences of its columns made explicit ({!Sql.Private.Resolve.collate_over}). *)
val dml_iter : ?refused:(string -> bool) -> t -> Sql.Ast.select -> Sql.Ast.value array -> f:(Sql.Ast.value list -> unit) -> bool (** [dml_iter ~refused db s params ~f] calls [f] on each row of {!dml_rows} as the VM produces it, and is [false], having called [f] on none, where [s] has a fast path or does not compile, or where its loop walks an index [refused] names, the indexes an UPDATE writes. [f] may write the table [s] scans. *)end
module Make (R : S) : sig val exec : R.t -> string -> params:Sql.Ast.value list -> unit (** [exec db sql ~params] runs the [;]-separated script [sql].
@raise Invalid_argument if a statement is a [SELECT]. *)
val query : R.t -> string -> params:Sql.Ast.value list -> Sql.Ast.value list list (** [query db sql ~params] is the rows of the one [SELECT], [EXPLAIN] or row-returning [PRAGMA] statement [sql]. *)
val iter : R.t -> string -> params:Sql.Ast.value list -> f:(Sql.Row.t -> unit) -> unit (** [iter db sql ~params ~f] calls [f] on each row of {!query}, during the read that produces it; a row is readable only inside the call of [f] that receives it. *)
val run_script : R.t -> string -> params:Sql.Ast.value list -> on_row:(Sql.Ast.value list -> unit) -> unit (** [run_script db sql ~params ~on_row] runs the script [sql] and calls [on_row] on each row of each [SELECT]. *)
val run_stmt : R.t -> Sql.Private.Query.catalog -> Sql.Ast.value array -> sql:string -> on_select: (Sql.Private.Query.catalog -> Sql.Ast.value array -> Sql.Ast.select -> unit) -> Sql.Ast.stmt -> unit (** [run_stmt db cat params ~sql ~on_select stmt] runs [stmt], whose source is [sql], with its undo scope, and passes each [SELECT] to [on_select]. *)
val run_trigger_body : R.t -> Sql.Ast.stmt -> unit (** [run_trigger_body db stmt] runs the trigger body statement [stmt] as a step of the statement that fired it, in the frame of [db]: under its [OLD] and [NEW] scope and its conflict policy ({!Sql.Private.Trigger.step_policy}), with no undo of its own.
@raise Invalid_argument if [stmt] is not an [INSERT], [UPDATE], [DELETE] or [SELECT]. *)
val autocommit_op : R.t -> (unit -> 'a) -> 'a (** [autocommit_op db f] runs the change [f] as its own transaction if no transaction is open.
@raise Invalid_argument if called from the callback of a scan. *)
val with_undo : R.t -> (unit -> 'a) -> 'a (** [with_undo db f] runs [f] as one statement of the connection of [db] ({!Sql.Private.Connection.atomic}). If [f] raises, it undoes the statement, keeps it for a [FAIL] conflict, or also rolls the transaction back for a [ROLLBACK] conflict, then re-raises. *)
val user_version : R.t -> int (** [user_version db] is the user version in the database header. *)
val set_user_version : R.t -> int -> unit (** [set_user_version db v] sets the user version in the database header to [v]. *)end