Something went wrong. Try again.
SQLite file format and SQL engine in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*)
(** Tests for {!Sqlite.Private.Decl}: what a CREATE TABLE declares, read back without a database.
These are questions the engine asks on every open and every write, and they are answered from the statement's text alone -- so they can be asked here directly, with no file and no b-tree. *)
module D = Sqlite.Private.Decl
let cols sql = D.parse_create_table sql
(* A PRIMARY KEY declared on the column and one declared as a table constraint are the same key. *)let test_primary_key_either_spelling () = let named = D.schema_of_sql ~encoding:Sql.Encoding.Utf8 ~tbl_name:"t" "CREATE TABLE t (a INT PRIMARY KEY, b TEXT)" in let table_level = D.schema_of_sql ~encoding:Sql.Encoding.Utf8 ~tbl_name:"t" "CREATE TABLE t (a INT, b TEXT, PRIMARY KEY (a))" in Alcotest.(check (list string)) "column-level" [ "a" ] (D.pk_column_names named); Alcotest.(check (list string)) "table-level" [ "a" ] (D.pk_column_names table_level)
(* A composite key keeps its declared order, which is the order its index compares in. *)let test_composite_key_order () = let s = D.schema_of_sql ~encoding:Sql.Encoding.Utf8 ~tbl_name:"t" "CREATE TABLE t (a INT, b INT, PRIMARY KEY (b, a))" in Alcotest.(check (list string)) "declared order" [ "b"; "a" ] (D.pk_column_names s)
(* Text this engine cannot read is not an error: it is a table whose facts are unknown, kept so the rest of the database stays readable. *)let test_unreadable_text_yields_no_columns () = Alcotest.(check int) "no columns" 0 (List.length (cols "NOT A STATEMENT"))
(* Column order and names come straight from the declaration. *)let test_column_names () = Alcotest.(check (list string)) "declared order" [ "a"; "b"; "c" ] (D.column_names (cols "CREATE TABLE t (a INT, b TEXT, c BLOB)"))
(* A column's position is what an index key records, so it is looked up by name once and used by number after. *)let test_column_index () = let names = D.column_names (cols "CREATE TABLE t (a INT, b TEXT)") in Alcotest.(check (option int)) "first" (Some 0) (D.column_index names "a"); Alcotest.(check (option int)) "second" (Some 1) (D.column_index names "b"); Alcotest.(check (option int)) "absent" None (D.column_index names "zz"); (* A query reaches the column however it spells it, so the lookup folds ASCII case -- and only ASCII, so an accented capital is a different name. *) Alcotest.(check (option int)) "another spelling" (Some 1) (D.column_index names "B"); let accented = D.column_names (cols "CREATE TABLE t (\"\xc3\xa9\")") in Alcotest.(check (option int)) "as written" (Some 0) (D.column_index accented "\xc3\xa9"); Alcotest.(check (option int)) "the accented capital is another column" None (D.column_index accented "\xc3\x89")
(* A declared COLLATE is what an index over the column orders by when the index names none of its own. *)let test_declared_collation () = let c = cols "CREATE TABLE t (a TEXT COLLATE NOCASE, b TEXT)" in Alcotest.(check bool) "declaring COLLATE changes the column's sequence" false (Option.equal Sql.Collation.equal (D.declared_collation c 0) (D.declared_collation c 1))
(* A CHECK is kept with its source text, because that text is what the error message quotes. *)let test_checks_keep_their_source () = let s = D.schema_of_sql ~encoding:Sql.Encoding.Utf8 ~tbl_name:"t" "CREATE TABLE t (a INT, CHECK (a > 0))" in match D.table_checks s with | [ (text, _) ] -> Alcotest.(check bool) "the predicate's own text" true (Re.execp (Re.compile (Re.str "a > 0")) text) | l -> Alcotest.failf "expected one check, got %d" (List.length l)
(* A WITHOUT ROWID table has no rowid, so an INTEGER PRIMARY KEY on one aliases nothing: it is an ordinary key column, held in the record and assignable by an UPDATE (lang_createtable.html#rowid). *)let test_without_rowid_integer_key_is_not_an_alias () = let with_ = cols "CREATE TABLE t (n INTEGER PRIMARY KEY, s TEXT) WITHOUT ROWID" and rowid = cols "CREATE TABLE t (n INTEGER PRIMARY KEY, s TEXT)" in Alcotest.(check (option int)) "a rowid table's INTEGER PRIMARY KEY is the rowid" (Some 0) (D.rowid_alias_index rowid); Alcotest.(check (option int)) "a WITHOUT ROWID table's is a column like any other" None (D.rowid_alias_index with_)
(* Every PRIMARY KEY column of a WITHOUT ROWID table is NOT NULL, STRICT or not (sqlite/src/build.c, convertToWithoutRowidTable: "Mark every PRIMARY KEY column as NOT NULL"). sqlite3 3.54 on [CREATE TABLE m (p TEXT, q INT, r TEXT, PRIMARY KEY (q, p)) WITHOUT ROWID] answers table_info [0|p|TEXT|1||2 / 1|q|INT|1||1 / 2|r|TEXT|0||0]. *)let test_without_rowid_key_columns_are_not_null () = let not_null sql = List.map (fun (c : Sqlite.Private.Model.column) -> c.not_null <> None) (cols sql) in Alcotest.(check (list bool)) "the key columns, and only those" [ true; true; false ] (not_null "CREATE TABLE m (p TEXT, q INT, r TEXT, PRIMARY KEY (q, p)) WITHOUT \ ROWID"); Alcotest.(check (list bool)) "including an INTEGER PRIMARY KEY, which is no rowid here" [ true; false ] (not_null "CREATE TABLE i (n INTEGER PRIMARY KEY, s TEXT) WITHOUT ROWID"); Alcotest.(check (list bool)) "a rowid table keeps SQLite's long-standing NULL-in-a-PRIMARY-KEY quirk" [ false; false ] (not_null "CREATE TABLE j (n TEXT PRIMARY KEY, s TEXT)")
(* A UNIQUE constraint needs an index of its own; the key it declares is what that index is built over. *)let test_unique_constraint_key () = let s = D.schema_of_sql ~encoding:Sql.Encoding.Utf8 ~tbl_name:"t" "CREATE TABLE t (a INT, b TEXT UNIQUE)" in Alcotest.(check bool) "a key was derived" true (List.length (D.parse_unique_constraints s) > 0)
(* A COLLATE naming no built-in sequence is refused, and the sentence is the one sqlite3 writes: "collation sequence", not "collating sequence" (src/callback.c, sqlite3LocateCollSeq, "no such collation sequence: %s"). Probed against the sqlite3 CLI (Apple build, 3.54.0), which answers it at both doors:
{v $ sqlite3 x.db "CREATE TABLE u (a TEXT COLLATE nosuchcoll)" Parse error: no such collation sequence: nosuchcoll $ sqlite3 x.db "CREATE TABLE t (a); SELECT * FROM t ORDER BY a COLLATE nosuchcoll" Parse error: no such collation sequence: nosuchcoll v}
This is the schema-side door: the key of a UNIQUE constraint, resolved off the declaration while the table is loaded, which answers with an [Error] rather than a raise because a sequence an application registered is absent only from the connection that lacks it. The query-side door is {!Sql.Private.Collate.resolve}, pinned by ocaml-sql's own suite. *)let test_an_unknown_collation_is_refused_in_sqlite3_s_words () = let s = D.schema_of_sql ~encoding:Sql.Encoding.Utf8 ~tbl_name:"t" "CREATE TABLE t (a TEXT COLLATE NOSUCH UNIQUE)" in match D.parse_unique_constraints s with | [ Error e ] -> Alcotest.(check string) "named as sqlite3 names it" "no such collation sequence: NOSUCH" e | l -> Alcotest.failf "expected one unresolvable constraint, got %d" (List.length l)
let suite = ( "decl", [ Alcotest.test_case "primary key, either spelling" `Quick test_primary_key_either_spelling; Alcotest.test_case "composite key order" `Quick test_composite_key_order; Alcotest.test_case "unreadable text" `Quick test_unreadable_text_yields_no_columns; Alcotest.test_case "column names" `Quick test_column_names; Alcotest.test_case "column index" `Quick test_column_index; Alcotest.test_case "declared collation" `Quick test_declared_collation; Alcotest.test_case "an unknown collation is refused in sqlite3's words" `Quick test_an_unknown_collation_is_refused_in_sqlite3_s_words; Alcotest.test_case "checks keep their source" `Quick test_checks_keep_their_source; Alcotest.test_case "unique constraint key" `Quick test_unique_constraint_key; Alcotest.test_case "WITHOUT ROWID integer key is not an alias" `Quick test_without_rowid_integer_key_is_not_an_alias; Alcotest.test_case "WITHOUT ROWID key columns are NOT NULL" `Quick test_without_rowid_key_columns_are_not_null; ] )