Something went wrong. Try again.
SQLite file format and SQL engine in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*)
(** Table declarations.
[Decl] computes the properties of a table from its [CREATE TABLE] text and its parse alone: columns and affinities, defaults, index keys and their collating sequences, and [UNIQUE] and [CHECK] constraints. It needs no database. *)
(** {1:read Declarations} *)
val parse_create_table : string -> Model.column list(** [parse_create_table sql] is the columns [sql] declares, or [[]] if this engine cannot parse it, so the rest of the database stays readable. *)
val columns_of_parsed : Sql.Ast.create_table option -> Model.column list(** [columns_of_parsed ct] is the columns of [ct], with their affinities and constraints. *)
val column_not_null : Sql.Ast.create_table -> string list -> Sql.Ast.column_def -> Sql.Ast.insert_conflict option(** [column_not_null ct pk c] is the [ON CONFLICT] resolution of the [NOT NULL] of [c], if any. A column of the [PRIMARY KEY] [pk] is [NOT NULL]. *)
val schema_of_sql : encoding:Sql.Encoding.t -> tbl_name:string -> string -> Model.schema(** [schema_of_sql ~encoding ~tbl_name sql] is the schema [sql] declares, parsed once, of a table whose text is stored in [encoding]: its DEFAULT, CHECK, generation and index expressions order text and read its bytes as a statement over that file does. *)
val schema_of_ast : encoding:Sql.Encoding.t -> tbl_name:string -> sql:string -> Sql.Ast.create_table -> Model.schema(** [schema_of_ast ~encoding ~tbl_name ~sql ct] is like {!schema_of_sql} with [ct] the parse of [sql]. *)
val schema_of_columns : encoding:Sql.Encoding.t -> tbl_name:string -> columns:Model.column list -> sql:string -> Model.schema(** [schema_of_columns ~encoding ~tbl_name ~columns ~sql] is the schema of a table with the known columns [columns]. *)
(** {1:derived Derived fields}
What the record format derives from a declaration. *)
val index_predicate : Model.schema -> Sql.Ast.expr option -> Sql.Ast.expr option(** [index_predicate schema where] is the predicate [where] of a partial index, collated with {!Sql.Private.Table.collate}, or [None] for an index that is not partial. Every reader uses this one form, so they agree on which rows the index holds. *)
(** {1:columns Columns} *)
val column_names : Model.column list -> string list(** [column_names columns] is the names of [columns], in order. *)
val pk_column_names : Model.schema -> string list(** [pk_column_names schema] is the names of the [PRIMARY KEY] columns of [schema]. *)
val column_index : string list -> string -> int option(** [column_index names name] is the position of [name] in [names], compared as identifiers, or [None]. *)
(** {1:keys Index keys and constraints} *)
val default_collation_name : string(** [default_collation_name] is ["BINARY"], which the [PRAGMA]s print for a key without a collating sequence. *)
val key_collation_name : Model.column list -> named:string option -> int -> string(** [key_collation_name columns ~named pos] is the name of the collating sequence of the key column [pos], as the schema spells it and as sqlite3 prints it. *)
val key_collation : Model.column list -> named:string option -> int -> Sql.Collation.t(** [key_collation columns ~named pos] is the collating sequence of the key column [pos]: [named], else the declared [COLLATE] of the column, else BINARY (datatype3.html section 7).
@raise Catalog.Error for an unknown sequence. *)
val declared_collation : Model.column list -> int -> Sql.Collation.t option(** [declared_collation columns pos] is the declared [COLLATE] of the column [pos], if any. *)
val where_table : Model.generic_table -> Sql.Private.Where_table.t(** [where_table gt] is [gt] as the planner weighs it ({!Sql.Private.Where_loop}): its columns, how its rows are stored and its indexes, each with what the last [ANALYZE] measured of it. *)
val where_index : Model.index -> Sql.Private.Where_table.index(** [where_index idx] is [idx] as {!where_table} holds it. *)
val where_named : Model.generic_table -> Sql.Private.Where_table.index -> Model.index(** [where_named gt idx] is the index of [gt] that {!where_table} made [idx] of.
@raise Not_found if [gt] has none of its name. *)
val index_key_read : Sql.Ast.indexed_key -> Sql.Ast.indexed_key(** [index_key_read k] is [k] as [CREATE INDEX] reads it: a [COLLATE] over a column name is that column with the outermost sequence, as sqlite3 removes top-level [COLLATE]s first. Other keys are unchanged. *)
val resolve_index_keys : Model.schema -> origin:Model.index_origin -> ?on_conflict:Sql.Ast.insert_conflict -> Sql.Ast.indexed_key list -> (Model.index_key, string) result(** [resolve_index_keys schema ~origin ~on_conflict keys] is the key of an index over [keys] on the table [schema] declares: the source, order and collating sequence of each position, or [Error msg] naming what this engine lacks. An expression key has the collating sequence of its expression. *)
val unique_constraints : without_rowid:bool -> Model.schema -> (Model.index_key, string) result list(** [unique_constraints ~without_rowid schema] is the key of each [UNIQUE] constraint of the table, and of the [PRIMARY KEY] if it needs its own index.*)
val parse_unique_constraints : Model.schema -> (Model.index_key, string) result list(** [parse_unique_constraints schema] is [unique_constraints ~without_rowid:false schema], for the rowid tables this engine writes. *)
val table_checks : Model.schema -> (string * Sql.Ast.expr) list(** [table_checks schema] is the [CHECK] constraints of [schema], as source text for messages and expressions, as the table it writes through holds them ({!Sql.Private.Table.checks}). *)
val table_defaults : Model.schema -> (string * Sql.Ast.expr) list(** [table_defaults schema] is the [DEFAULT] expression of each column that has one, unevaluated. A column without a [DEFAULT] is absent, which differs from a default of NULL. *)
val rowid_alias : Model.schema -> int option(** [rowid_alias schema] is {!rowid_alias_index} of [schema]'s columns, worked out once per schema. *)
val has_autoincrement : Model.schema -> bool(** [has_autoincrement schema] is [true] if [schema] declares an [AUTOINCREMENT] column, and [false] for one whose text does not parse. *)
val rowid_alias_index : Model.column list -> int option(** [rowid_alias_index columns] is the position of the [INTEGER PRIMARY KEY] that aliases the rowid, if any. Its value is the rowid and is not stored in the record. *)
val written_rowid : Model.column list -> has_rowid:bool -> string -> bool(** [written_rowid columns ~has_rowid name] is [true] iff the column name [name] refers to the rowid: a name of the rowid that no column declares, in a rowid table. It is [false] for the [INTEGER PRIMARY KEY], a declared column. *)
(** {1:storage Storage schemas} *)
val storage_schema : ?indexes:Model.index list -> without_rowid:bool -> Model.schema -> Catalog.Spi.Schema.t(** [storage_schema ~indexes ~without_rowid s] is the table [s] declares as a {!Catalog.Spi.Schema.t}, as {!Sql.Private.Engine.table_schema} reads its statement, with [without_rowid] as the store keeps its rows and every explicit [CREATE INDEX] of [indexes] (defaults to [[]]). A constraint's index is the constraint's, which the schema carries. A table whose statement does not parse has its columns' types, [NOT NULL] and [COLLATE] and its [PRIMARY KEY] alone. *)