Something went wrong. Try again.
SQLite file format and SQL engine in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110(* The SQLite backend for the generic SQL shell, exercised over an in-memory database: a SELECT renders its rows, .tables lists the schema with counts, and an unknown table is reported as an error rather than crashing. *)
(* A fresh in-memory db with a [users(name)] table holding one row. *)let db_with_user () = let db = Sqlite.in_memory () in Sqlite.Raw.exec db "CREATE TABLE users (name TEXT)" ~params:[]; Sqlite.Raw.exec db "INSERT INTO users VALUES ('alice')" ~params:[]; Sqlite.sync db; db
(* Feed [lines] to a fresh shell over [db] and return everything it emitted. The backend locks with an Eio mutex, so this runs inside an event loop. *)let run db lines = Eio_main.run @@ fun _env -> let out = Buffer.create 256 in let backend = Sqlite_shell.v db in let shell = Sqlite_shell.shell backend ~styled:false ~emit:(fun chunk -> Buffer.add_string out chunk) () in List.iter (fun l -> ignore (Sql_shell.feed_line shell l : [ `Continue | `Quit ])) lines; Buffer.contents out
let has out sub = Alcotest.(check bool) (Fmt.str "output contains %S" sub) true (Re.execp (Re.compile (Re.str sub)) out)
let test_select () = has (run (db_with_user ()) [ "SELECT * FROM users;" ]) "alice"
let test_insert_status () = let out = run (db_with_user ()) [ "INSERT INTO users VALUES ('bob');" ] in has out "ok"
let test_tables () = let out = run (db_with_user ()) [ ".tables" ] in has out "users"; (* one row in the table, surfaced by the row count *) has out "1 row"
let test_error () = has (run (db_with_user ()) [ "SELECT * FROM nope;" ]) "error:"
(* An ordinary SQL error leaves the handle usable: the statements after it, a count and the table listing run on the same session. *)let test_error_then_valid () = let out = run (db_with_user ()) [ "SELEC name FROM users;"; "INSERT INTO users VALUES ('bob');"; ".tables"; ] in has out "error:"; has out "ok"; has out "2 rows"
(* A statement the schema refuses is an ordinary SQL error too: SQLite undoes it, and the shell answers it and goes on. *)let test_violation_then_valid () = let db = db_with_user () in Sqlite.Raw.exec db "CREATE TABLE ids (id INTEGER UNIQUE)" ~params:[]; Sqlite.Raw.exec db "INSERT INTO ids VALUES (7)" ~params:[]; Sqlite.sync db; let out = run db [ "INSERT INTO ids VALUES (7);"; "INSERT INTO ids VALUES (8);"; ".tables"; ] in has out "error:"; has out "ok"; has out "ids"; has out "2 rows"
let test_banner () = let out = Eio_main.run @@ fun _env -> let buf = Buffer.create 64 in let backend = Sqlite_shell.v ~banner:"custom banner" (db_with_user ()) in let shell = Sqlite_shell.shell backend ~styled:false ~emit:(fun chunk -> Buffer.add_string buf chunk) () in Sql_shell.start shell; Buffer.contents buf in has out "custom banner"
let suite = ( "sqlite_shell", [ Alcotest.test_case "select renders rows" `Quick test_select; Alcotest.test_case "insert status" `Quick test_insert_status; Alcotest.test_case ".tables with counts" `Quick test_tables; Alcotest.test_case "error on unknown table" `Quick test_error; Alcotest.test_case "a statement after an error runs" `Quick test_error_then_valid; Alcotest.test_case "a statement after a constraint violation runs" `Quick test_violation_then_valid; Alcotest.test_case "custom banner" `Quick test_banner; ] )