Something went wrong. Try again.
SQLite file format and SQL engine in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899open Cmdliner
let log_src = Logs.Src.create "sql"
module Log = (val Logs.src_log log_src : Logs.LOG)
let err_msgf fmt = Fmt.kstr (fun s -> Error (`Msg s)) fmt
let kv_pair = let parse s = match String.split_on_char '=' s with | [ k; v ] -> Ok (k, v) | _ -> err_msgf "expected KEY=VALUE, got %S" s in let pp ppf (k, v) = Fmt.pf ppf "%s=%s" k v in Arg.conv (parse, pp)
let db_arg = Arg.( required & pos 0 (some string) None & info [] ~docv:"DB" ~doc: "Database file path, or $(b,:memory:) for a new database in memory \ that is gone when the command ends, as sqlite3 reads the name.")
(* sqlite3's word for each mode, which is what [PRAGMA journal_mode] answers with; [`Journal] is the library's own name for the delete mode. *)let journal_modes : (string * Sqlite.journal_mode) list = [ ("wal", `Wal); ("delete", `Journal) ]
let journal_mode = Arg.( value & opt (some (enum journal_modes)) None & info [ "journal-mode" ] ~docv:"MODE" ~doc: "How a commit reaches the database file. $(b,wal) appends the pages \ it changed to a $(b,-wal) log; $(b,delete) writes the pre-images of \ the pages it overwrites to a $(b,-journal), writes the new pages \ into the database file, and deletes the journal. Without this, a \ database that is already there keeps the mode page 1 records for \ it, and one being created is created in $(b,wal).")
let cli_path env p = Eio.Path.(Eio.Stdenv.fs env / p)let clock env = Eio.Stdenv.clock env
(* sqlite3_open reads the name ":memory:" as a new, private database in memory (sqlite.org/inmemorydb.html), and so does every caller of it, the sqlite3 shell included; any other name, "./:memory:" among them, is a file. *)let memory = ":memory:"
let with_db env ~sw ?create ?read_only ?journal_mode db f = if String.equal db memory then begin let t = Sqlite.in_memory ~clock:(clock env) () in Fun.protect ~finally:(fun () -> Sqlite.close t) (fun () -> f t) end else Sqlite.with_open ~sw ?create ?read_only ?journal_mode ~clock:(clock env) (cli_path env db) f
let logging = Observe.setup "sql"
(* Eio's cross-platform answer, read here rather than the backend error nested inside it: that one carries an errno and a syscall, which Eio asks not to be matched on. *)let fs_reason = function | Eio.Fs.E (Eio.Fs.Not_found _) -> "no such file or directory" | Eio.Fs.E (Eio.Fs.Permission_denied _) -> "permission denied" | Eio.Fs.E (Eio.Fs.Already_exists _) -> "file already exists" | Sqlite.Is_a_directory -> "is a directory" | e -> Fmt.str "%a" Eio.Exn.pp_err e
let unable_to_open ~noun path e = Fmt.str "unable to open %s %S: %s" noun path (fs_reason e)
exception Unable_to_open of string
let user_error ~db f = let fail tag msg = Fmt.epr "%s: %s@." tag msg; exit 1 in try f () with | Sqlite.Error (Prepare _ as e) -> fail "Parse error" (Fmt.str "%a" Sqlite.Error.pp e) (* sqlite3 opens lazily, so its SQLITE_NOTADB arrives from the first prepare, under that phase's tag. *) | Sqlite.Error (Not_a_database _ as e) -> fail "Parse error" (Fmt.str "%a" Sqlite.Error.pp e) | Sqlite.Error e -> (* The error line is sqlite3's message; the constructor, and for a damaged file which check met the damage, are for a log. *) Log.info (fun m -> m "%a" Sqlite.Error.dump e); fail "Error" (Fmt.str "%a" Sqlite.Error.pp e) | Unable_to_open line -> fail "Error" line | Eio.Io (((Eio.Fs.E _ | Sqlite.Is_a_directory) as e), _) -> fail "Error" (unable_to_open ~noun:"database" db e)