Something went wrong. Try again.
SQL frontend over the catalog engine
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126(* The schema table over the in-memory store: every table, index, view and trigger a statement makes is a row of sqlite_master, under the rowid an insert into it takes, with the text sqlite3 keeps (schematab.html). Each expected answer is the sqlite3 CLI's (3.54.1) for the same statements. *)
module Value = Catalog.Value
let vrows = Alcotest.list (Alcotest.testable (Fmt.Dump.list Value.pp) (List.equal Value.equal))
let i n = Value.Int nlet t s = Value.Text slet run db sql = match Sql.exec db sql with Sql.Rows r -> r | Sql.Done -> []
let refused db msg sql = match Sql.exec db sql with | _ -> Alcotest.failf "%s: accepted, sqlite3 refuses it with %S" sql msg | exception Catalog.Error e -> Alcotest.(check string) sql msg (Fmt.str "%a" Catalog.Error.pp e)
let fresh () = let db = Sql.v (Catalog.of_mem []) in ignore (run db "CREATE TABLE a(x UNIQUE); CREATE VIEW u AS SELECT 0; create view if \ not exists v as select 1 ; CREATE TABLE b(y, z, PRIMARY KEY(y, z)) \ WITHOUT ROWID; create unique index bi on b(z) where z > 0; CREATE \ TRIGGER tr AFTER INSERT ON a BEGIN SELECT 1; END; DROP VIEW u"); db
(* A row per object in the order they were made, the dropped view's rowid a gap; a UNIQUE constraint's index has a row with no text, and a WITHOUT ROWID table's PRIMARY KEY, which is the table itself, none. The text is CREATE, the kind in upper case and the statement from the name on (build.c, sqlite3EndTable). Rows numbered by position would answer 3 for v, and tables listed before views would put b second. *)let test_a_row_per_object () = let db = fresh () in Alcotest.check vrows "sqlite3's rows" [ [ i 1L; t "table"; t "a"; t "a"; t "CREATE TABLE a(x UNIQUE)" ]; [ i 2L; t "index"; t "sqlite_autoindex_a_1"; t "a"; Value.Null ]; [ i 4L; t "view"; t "v"; t "v"; t "CREATE VIEW v as select 1" ]; [ i 5L; t "table"; t "b"; t "b"; t "CREATE TABLE b(y, z, PRIMARY KEY(y, z)) WITHOUT ROWID"; ]; [ i 6L; t "index"; t "bi"; t "b"; t "CREATE UNIQUE INDEX bi on b(z) where z > 0"; ]; [ i 7L; t "trigger"; t "tr"; t "a"; t "CREATE TRIGGER tr AFTER INSERT ON a BEGIN SELECT 1; END"; ]; ] (run db "SELECT rowid, type, name, tbl_name, sql FROM sqlite_master")
(* A store with no pages keeps no b-tree for any object, and sqlite3 answers 0 for the root page of one it keeps none for, as for a view (fileformat2.html, 2.6). *)let test_no_root_page () = let db = fresh () in Alcotest.check vrows "every root page is 0" [ [ i 0L; i 6L ] ] (run db "SELECT max(rootpage), count(*) FROM sqlite_schema")
(* DROP TABLE takes the table's row, its indexes' and its triggers' with it, and the next object takes one past the largest rowid left. *)let test_a_drop_takes_its_rows () = let db = fresh () in ignore (run db "DROP TABLE a"); Alcotest.check vrows "what is left" [ [ i 4L; t "v" ]; [ i 5L; t "b" ]; [ i 6L; t "bi" ] ] (run db "SELECT rowid, name FROM sqlite_schema"); ignore (run db "DROP INDEX bi; CREATE TABLE c(q)"); Alcotest.check vrows "the next rowid" [ [ i 6L; t "c" ] ] (run db "SELECT rowid, name FROM sqlite_master WHERE type = 'table' AND name = \ 'c'")
(* A transaction undoes the rows its statements wrote. *)let test_a_rollback_undoes_its_rows () = let db = fresh () in ignore (run db "BEGIN; CREATE TABLE d(x UNIQUE); DROP VIEW v; ROLLBACK"); Alcotest.check vrows "as before" [ [ i 6L; i 7L ] ] (run db "SELECT count(*), max(rowid) FROM sqlite_master")
(* build.c, sqlite3LocateTable: the temp database's schema table is always there, under each of its names, and holds no row; neither schema table is dropped (sqlite3DropTable). *)let test_temp_and_drop () = let db = fresh () in Alcotest.check vrows "temp holds nothing" [ [ i 0L ] ] (run db "SELECT count(*) FROM temp.sqlite_master"); Alcotest.check vrows "under its own name either" [] (run db "SELECT * FROM sqlite_temp_schema"); refused db "table sqlite_master may not be dropped" "DROP TABLE sqlite_schema"; refused db "table sqlite_temp_master may not be dropped" "DROP TABLE temp.sqlite_master"
let suite = ( "schema_table", [ Alcotest.test_case "a row per object" `Quick test_a_row_per_object; Alcotest.test_case "no root page" `Quick test_no_root_page; Alcotest.test_case "a drop takes its rows" `Quick test_a_drop_takes_its_rows; Alcotest.test_case "a rollback undoes its rows" `Quick test_a_rollback_undoes_its_rows; Alcotest.test_case "temp and drop" `Quick test_temp_and_drop; ] )