Something went wrong. Try again.
SQL frontend over the catalog engine
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*)
(** The refusals of a [CREATE TABLE].
sqlite3 refuses a table it cannot hold as it reads the statement: each column as it is added, its name, [DEFAULT], [COLLATE] and [REFERENCES] (build.c, sqlite3AddColumn, sqlite3AddDefaultValue, sqlite3AddCollateType, sqlite3CreateForeignKey), then the table's constraints and its stored expressions (sqlite3EndTable). These are those checks, in its order and words, over the parse alone. *)
val primary_key_columns : Ast.create_table -> string list(** [primary_key_columns ct] is the columns of the [PRIMARY KEY] of [ct], declared on a column or on the table. *)
val constant_default : Ast.expr -> bool(** [constant_default e] is [true] iff [e] can be a [DEFAULT]: it has no column reference, subquery, parameter, [RAISE], aggregate or window function. A constant call such as [abs(5)] can. *)
val check_columns : Ast.create_table -> unit(** [check_columns ct] accepts the columns of [ct] if sqlite3 would as it reads them, or raises: column by column, a name declared before is refused, then a [DEFAULT] that is no constant ({!constant_default}), then a [COLLATE] that names no sequence, then a [REFERENCES] naming more than one parent column ({!check_width}). Names compare as identifiers, so [x] and [X] are one column.
@raise Catalog.Error with ["duplicate column name: x"], the later declaration's spelling, ["default value of column [x] is not constant"], ["no such collation sequence: c"] or the refusal of {!check_width}. *)
val check_width : Ast.foreign_key -> unit(** [check_width fk] accepts [fk] if it names as many parent columns as child columns, or none, which is the parent's primary key (build.c, sqlite3CreateForeignKey).
@raise Catalog.Error with ["foreign key on x should reference only one column of table p"] for an {!Ast.Column_constraint}, [p] spelled as the statement writes it, and ["number of columns in foreign key does not match the number of columns in the referenced table"] for an {!Ast.Table_constraint}. *)
val check_names : Ast.create_table -> unit(** [check_names ct] is the part of {!check_columns} a stored [CREATE TABLE] meets too: no column name is declared twice.
@raise Catalog.Error with ["duplicate column name: x"], the later declaration's spelling. *)
val check_generated_column : Ast.create_table -> unit(** [check_generated_column ct] is the part of {!check} that sqlite3 decides while parsing a column: a generated column has no default and is not a key.
@raise Catalog.Error with the message of sqlite3. *)
val check : Ast.create_table -> unit(** [check ct] accepts [ct] if sqlite3 would, once its columns are read ({!check_columns}): a generated column has no default and is not a key, every table constraint names a column of the table, every foreign key is as wide as its parent columns ({!check_width}) and then names columns of the table, a table has a column that is not generated, and each [CHECK] and generation expression reads the row alone ({!Self_ref.check}).
@raise Catalog.Error with the message of sqlite3. *)
val checked : string -> (Ast.create_table, string) result(** [checked sql] is the parse of the [CREATE TABLE] text [sql] if it passes {!check}, and [Error reason] otherwise, which a caller words as it refuses the text: as a malformed file on open, or naming the object on an [ALTER].*)