(*--------------------------------------------------------------------------- Copyright (c) 2025 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*) type catalog = { columns : string -> string list; (** [columns name] is the column names of the table [name], in declaration order. *) rows : string -> Catalog.Value.t list list; (** [rows name] is the rows of [name], each aligned to {!columns}. *) rows_rowid : string -> (int64 option * Catalog.Value.t list) list; (** [rows_rowid name] is the rows of [name] with their rowids, or [None] for a source without one, such as a view, a derived table or [VALUES]. *) has_rowid : string -> bool; (** [has_rowid name] is [true] iff the relation [name] has an implicit rowid. A plain table has one; a view, a [WITHOUT ROWID] table, a [VALUES] list and a materialised CTE do not, so [rowid] is an unknown column there. *) holds : string -> bool; (** [holds name] is [true] iff a [FROM] may name the relation [name]: a table or a view of the schema, or one the backend always has. It never reads a view's columns, so it answers for a view whose definition is circular. *) binds : string -> bool; (** [binds name] is [true] iff this catalog answers for the relation [name] out of rows of its own, such as a [WITH] table it materialised or a row set standing for a table, rather than reading the backend's relation of that name. A backend may read a relation its catalog does not bind through its own access paths. *) index_scan : string -> (string * Catalog.Value.t) list -> (string * Catalog.Value.t list list) option; (** [index_scan table eqs] is [Some (path, rows)] if an index on [table] serves the column equalities [eqs], and [None] for a full scan. The caller filters the rows again, so it only saves work. [path] names the access as {!best_index_for} does. *) best_index_for : string -> string list -> Catalog.Spi.access_path option; (** [best_index_for table cols] is the best access path for an equality on [cols] in [table], the one that binds the most key columns. The planner costs it and {!Explain} prints it. *) schema : string -> Catalog.Spi.Schema.t option; (** [schema table] is the schema the store declares for [table], its indexes among it, which the planner weighs the loops over [table] by as sqlite3 does ({!Where_loop}); [None] for a relation the catalog binds itself, a view, and a store that declares none. *) index_range : string -> Catalog.Spi.range -> (int64 option * Catalog.Value.t list) list option; (** [index_range table r] is the rows of [table] read through one of its keys in its order ({!Catalog.Spi.index_range}), or [None] where the store cannot read it, leaving the engine to read the relation. *) affinity : string -> string -> Value_ops.affinity option; (** [affinity table col] is the affinity of the declared type of [col], or [None] for a relation without a schema, such as a derived table or a view. The planner follows a chain of equalities only between columns with the same affinity. *) decl_type : string -> string -> string option; (** [decl_type table col] is the declared type of [col] as sqlite3 reports it, or [None] where it declares none and for a relation without a schema. *) row_count : string -> upto:int -> int; (** [row_count table ~upto] is the number of rows of [table], counted up to [upto]. The planner uses it to choose between an index seek and a hash. A relation that is not a table returns [upto]. *) collation : string -> string -> string option; (** [collation table col] is the declared [COLLATE] sequence of [col], in upper case, or [None] for BINARY and for a derived source. *) table_function : string -> Func.table_function option; (** [table_function name] is [Some f] for a table-valued function of the backend, such as the [pragma_*] functions. [None] leaves [name] to {!Func}. *) scalar_function : string -> (Catalog.Value.t list -> Catalog.Value.t) option; (** [scalar_function name] is [Some f] for a scalar function of the backend, with [f args] its value. The backend provides the functions whose value depends on the connection, such as [last_insert_rowid()] and the change counters. [None] leaves [name] to {!Func}. *) subquery : catalog -> (string * (string * Catalog.Value.t) list) list -> Ast.select -> Catalog.Value.t array -> Catalog.Value.t list list; (** [subquery cat scope s params] is the rows of [s] over [cat], with the outer scope [scope]. It runs every select reached from the catalog: a scalar, [IN] or [EXISTS] subquery, a [FROM] subquery, a CTE body, and the parts of a recursive CTE. [cat] may be a derived catalog that hides a relation. The builder of the catalog chooses it, so a backend can count what its runner reads. *) prepare : catalog -> Ast.select -> Catalog.Value.t array -> unit -> Catalog.Value.t list list; (** [prepare cat s params] is [s] made ready to run over [cat] with no outer scope: each application to [()] is the rows of [s] over what the relations of [cat] hold at that moment. What [s] and [cat] alone decide is worked out at most once, so a recursive CTE prepares each arm once and runs it every round, as sqlite3 codes the recursive step once (select.c, generateWithRecursiveQuery). {!of_catalog}'s runs [s] through {!field-subquery} at each application. *) stream : string -> Catalog.Value.t list Seq.t option; (** [stream name] is [Some rows] where the relation [name] is a recursive CTE this catalog binds: its rows produced as [rows] is read, each forcing of [rows] running the recursion again, so that a reader that stops early stops it, as sqlite3 reads a recursive CTE as a co-routine (select.c, generateWithRecursiveQuery). [None] for any other relation, whose rows are {!field-rows}'s. *) subquery_memo : (Ast.select * Catalog.Value.t array, Catalog.Value.t list list) Hashtbl.t; (** The rows of the closed subqueries run over this catalog, keyed by the subquery and the values its parameters read, which a subquery bound to an outer row ({!Resolve.bind_scope}) changes from row to row: a statement runs each once under each binding, as sqlite3 runs an uncorrelated subquery once. Each catalog needs its own table. *) mutable subquery_plans : (Ast.select, Closure.plan) Hashtbl.t option; (** The plan a store compiled for each subquery it ran over this catalog, keyed by the subquery with the outer row bound as parameters ({!Resolve.bind_scope}): every outer row runs the one plan. Each catalog needs its own table, made by the first subquery that keeps a plan, so a statement that keeps none allocates none. *) recursion_cap : int; (** The rows a recursive CTE may produce over this catalog before it is refused with [Catalog.Error (Runtime _)], positive. A derived catalog keeps the cap of the catalog it derives from. *) encoding : Encoding.t; (** The encoding the backend stores text in. A statement over the catalog orders text under BINARY as {!Collation.binary} of it, and its byte-level functions see the bytes stored in it. A derived catalog keeps the encoding of the catalog it derives from. *) } (** The type for catalogs. *)