Something went wrong. Try again.
SQLite file format and SQL engine in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241(*--------------------------------------------------------------------------- Copyright (c) 2026 Thomas Gazagnaire. All rights reserved. SPDX-License-Identifier: ISC ---------------------------------------------------------------------------*)
(** Tests for {!Sqlite.Driver}: the statement driver over the handle.
The module is internal, so it is driven through the public doors it backs -- statements reaching {!Sqlite.Raw.exec} and {!Sqlite.Raw.query}, and the header's user-version through {!Sqlite.user_version} / {!Sqlite.set_user_version}. What this suite pins is the orchestration the extraction isolated, each asserted on the exact rows left behind: how far a failing statement's changes are undone (the [`Statement] / [`Row] / [`Transaction] scope the failing constraint's own ON CONFLICT names), the nesting of SAVEPOINT / ROLLBACK TO / RELEASE, and the autocommit boundary a statement runs its own transaction inside unless an explicit one is open. *)
let value = Alcotest.testable Sqlite.Value.pp ( = )let rows = Alcotest.(list (list value))let exec db sql = Sqlite.Raw.exec db sql ~params:[]let select db sql = Sqlite.Raw.query db sql ~params:[]
(* A statement expected to raise, run for its partial effect on the store. The scope the driver then unwinds to is what the following assertion reads. *)let expect_fail db sql = try exec db sql; Alcotest.failf "expected %S to fail, but it did not" sql with Sqlite.Error _ -> ()
(* The sentence [sql] is refused with, whichever door raises it: a statement refused while it is prepared raises [Sqlite.Error (Prepare _)] and one refused as it runs raises [Failure], and what a caller reads is the same string. *)let refusal db sql = match exec db sql with | () -> Alcotest.failf "expected %S to be refused" sql | exception Sqlite.Error (Prepare m) -> m | exception Sqlite.Error m_e -> let m = Fmt.str "%a" Sqlite.Error.pp m_e in m
(* The default resolution is ABORT: a failing statement undoes what it changed before it raised ([`Statement]) and leaves every earlier statement in place. The row committed by the first INSERT survives; the row the second one wrote before it collided does not. *)let test_abort_undoes_the_statement_not_the_earlier_one () = let db = Sqlite.in_memory () in exec db "CREATE TABLE t (a INTEGER PRIMARY KEY)"; exec db "INSERT INTO t VALUES (1)"; expect_fail db "INSERT INTO t VALUES (2), (1)"; Alcotest.check rows "the aborted statement's own row (2) is rolled back too" [ [ Sqlite.Int 1L ] ] (select db "SELECT a FROM t ORDER BY a")
(* OR FAIL stops at the offending row and keeps the rows before it ([`Row]): the statement journal is committed rather than rolled back, so a multi-row INSERT OR FAIL that collides on its third value stores the first two and nothing after. *)let test_or_fail_keeps_the_rows_before_the_violator () = let db = Sqlite.in_memory () in exec db "CREATE TABLE t (a INTEGER PRIMARY KEY)"; expect_fail db "INSERT OR FAIL INTO t VALUES (1), (2), (1), (3)"; Alcotest.check rows "the rows before the collision are kept, the rest dropped" [ [ Sqlite.Int 1L ]; [ Sqlite.Int 2L ] ] (select db "SELECT a FROM t ORDER BY a")
(* OR ROLLBACK backs the whole enclosing transaction out ([`Transaction]) and ends it, so what follows runs in autocommit. The row the transaction wrote before the collision goes with it, and the next statement -- issued with no explicit BEGIN, since the transaction is already gone -- stands on its own. *)let test_or_rollback_backs_out_the_transaction () = let db = Sqlite.in_memory () in exec db "CREATE TABLE t (a INTEGER PRIMARY KEY)"; exec db "BEGIN"; exec db "INSERT INTO t VALUES (5)"; expect_fail db "INSERT OR ROLLBACK INTO t VALUES (5)"; exec db "INSERT INTO t VALUES (9)"; Alcotest.check rows "the rolled-back (5) is gone; the autocommit (9) remains" [ [ Sqlite.Int 9L ] ] (select db "SELECT a FROM t ORDER BY a")
(* SAVEPOINT nesting: ROLLBACK TO an inner savepoint undoes the work since it and leaves the savepoint open, so an outer RELEASE still commits both the work before the inner one and the work after the rollback. *)let test_savepoint_rollback_to_inner_keeps_the_outer () = let db = Sqlite.in_memory () in exec db "CREATE TABLE t (a INTEGER)"; exec db "SAVEPOINT s1"; exec db "INSERT INTO t VALUES (1)"; exec db "SAVEPOINT s2"; exec db "INSERT INTO t VALUES (2)"; exec db "ROLLBACK TO s2"; exec db "INSERT INTO t VALUES (3)"; exec db "RELEASE s1"; Alcotest.check rows "the inner (2) is undone; (1) and (3) are committed" [ [ Sqlite.Int 1L ]; [ Sqlite.Int 3L ] ] (select db "SELECT a FROM t ORDER BY a")
(* The autocommit boundary: a mutation inside an explicit transaction is not its own transaction, so a ROLLBACK discards it, while a statement issued with no transaction open commits on its own and a BEGIN ... COMMIT pair commits at the COMMIT. *)let test_autocommit_boundary_brackets_a_transaction () = let db = Sqlite.in_memory () in exec db "CREATE TABLE t (a INTEGER)"; exec db "BEGIN"; exec db "INSERT INTO t VALUES (1)"; exec db "ROLLBACK"; Alcotest.check rows "the transaction's insert is discarded by ROLLBACK" [] (select db "SELECT a FROM t"); exec db "INSERT INTO t VALUES (2)"; exec db "BEGIN"; exec db "INSERT INTO t VALUES (3)"; exec db "COMMIT"; Alcotest.check rows "the autocommit (2) and the committed (3) both survive" [ [ Sqlite.Int 2L ]; [ Sqlite.Int 3L ] ] (select db "SELECT a FROM t ORDER BY a")
(* The header's user-version is zero on a fresh database, and each write replaces it -- the driver runs the read under a read boundary and the write under the autocommit one. *)let test_user_version_round_trips () = let db = Sqlite.in_memory () in Alcotest.(check int) "a fresh database reads zero" 0 (Sqlite.user_version db); Sqlite.set_user_version db 42; Alcotest.(check int) "the written value reads back" 42 (Sqlite.user_version db); Sqlite.set_user_version db 7; Alcotest.(check int) "a second write replaces the first" 7 (Sqlite.user_version db)
(* A DROP COLUMN the schema re-test refuses leaves the handle reading the table it started from, not the one the ALTER was part-way through building.
sqlite3 refuses this one for the CHECK constraint that would be left naming a column nobody declares ("error in table t after drop column: no such column: a", sqlite/src/alter.c, sqlite3AlterDropColumn). A driver that decides that only after rewriting its own schema has to unwind the rewrite, and one that decides it before is simply the table it was: either way, the very next statement on the same handle reads the column and the constraint keeps working. Asserted on the same connection, since the file is the easy half -- a refusal that rolled the pages back but left the handle describing a table with two columns answers every later statement from the wrong shape. *)let test_refused_drop_column_leaves_the_handle_reading_the_table () = let db = Sqlite.in_memory () in exec db "CREATE TABLE t (a INTEGER, b INTEGER, CHECK (a > 0))"; exec db "INSERT INTO t VALUES (1, 2)"; expect_fail db "ALTER TABLE t DROP COLUMN a"; Alcotest.check rows "the dropped column still reads" [ [ Sqlite.Int 1L; Sqlite.Int 2L ] ] (select db "SELECT a, b FROM t"); exec db "INSERT INTO t VALUES (3, 4)"; Alcotest.check rows "and still takes rows" [ [ Sqlite.Int 1L; Sqlite.Int 2L ]; [ Sqlite.Int 3L; Sqlite.Int 4L ] ] (select db "SELECT a, b FROM t ORDER BY a"); expect_fail db "INSERT INTO t VALUES (-1, 5)"; Alcotest.check rows "with the CHECK the refusal was about still enforced" [ [ Sqlite.Int 1L; Sqlite.Int 2L ]; [ Sqlite.Int 3L; Sqlite.Int 4L ] ] (select db "SELECT a, b FROM t ORDER BY a")
(* A table must declare at least one column that is not generated, and DROP COLUMN is asked that question about the table it would leave. sqlite3 refuses the CREATE TABLE that declares only generated columns (sqlite/src/build.c, sqlite3EndTable: "must have at least one non-generated column") and reaches the same refusal through the schema re-test a DROP COLUMN runs, reported as the table it is about (src/alter.c, sqlite3AlterDropColumn calling renameTestSchema).
It is asked before the generation expressions are resolved again, which the two fixtures below separate. The sqlite3 CLI (Apple build, 3.54.0) answers both with "error in table gg after drop column: must have at least one non-generated column" and leave the stored CREATE TABLE as it was:
- [gg (x, y AS (x + 1))]: dropping x also leaves y's expression naming a column nobody declares, and sqlite3 still reports the count rule, so that rule comes first; - [gg (x, y AS (1))]: nothing is left unresolved, so the count rule is the whole of what is wrong. *)let test_drop_column_may_not_leave_only_generated_columns () = let refused = "must have at least one non-generated column" in let each label sql = let db = Sqlite.in_memory () in exec db sql; exec db "INSERT INTO gg (x) VALUES (1)"; Alcotest.(check string) label ("error in table gg after drop column: " ^ refused) (refusal db "ALTER TABLE gg DROP COLUMN x"); Alcotest.check rows "and the table is the one it was" [ [ Sqlite.Int 1L; Sqlite.Int 2L ] ] (select db "SELECT x, y FROM gg") in each "the generated column reads the dropped one" "CREATE TABLE gg (x, y AS (x + 1))"; each "the generated column reads nothing at all" "CREATE TABLE gg (x, y AS (2))"
(* CREATE TABLE AS stores its column list as identifiers: a keyword is quoted and a quote in a name doubled, so the text parses back to the same names. sqlite3 3.54.1: CREATE TABLE t AS SELECT 1 AS "order", 2 AS "a""b"; CREATE TABLE "group" AS SELECT 3 AS a; SELECT sql FROM sqlite_schema; -- CREATE TABLE t("order","a""b") -- CREATE TABLE "group"(a) A keyword left bare stores CREATE TABLE t(order,...), which no reader parses. *)let test_create_table_as_quotes_its_names () = let db = Sqlite.in_memory () in exec db {|CREATE TABLE t AS SELECT 1 AS "order", 2 AS "a""b"|}; exec db {|CREATE TABLE "group" AS SELECT 3 AS a|}; Alcotest.check rows "the stored text, as sqlite3 writes it" [ [ Sqlite.Text {|CREATE TABLE t("order","a""b")|} ]; [ Sqlite.Text {|CREATE TABLE "group"(a)|} ]; ] (select db "SELECT sql FROM sqlite_schema ORDER BY rowid")
let suite = ( "driver", [ Alcotest.test_case "CREATE TABLE AS quotes its names" `Quick test_create_table_as_quotes_its_names; Alcotest.test_case "abort undoes the statement" `Quick test_abort_undoes_the_statement_not_the_earlier_one; Alcotest.test_case "a refused drop column leaves the handle reading" `Quick test_refused_drop_column_leaves_the_handle_reading_the_table; Alcotest.test_case "drop column may not leave only generated columns" `Quick test_drop_column_may_not_leave_only_generated_columns; Alcotest.test_case "or fail keeps the rows before the violator" `Quick test_or_fail_keeps_the_rows_before_the_violator; Alcotest.test_case "or rollback backs out the transaction" `Quick test_or_rollback_backs_out_the_transaction; Alcotest.test_case "savepoint rollback to inner keeps the outer" `Quick test_savepoint_rollback_to_inner_keeps_the_outer; Alcotest.test_case "autocommit boundary brackets a transaction" `Quick test_autocommit_boundary_brackets_a_transaction; Alcotest.test_case "user version round-trips" `Quick test_user_version_round_trips; ] )