Something went wrong. Try again.
SQLite file format and SQL engine in pure OCaml
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091open! Hermetic
let default_banner = "SQLite shell: one SQL statement per line.\n\ SELECT, INSERT, UPDATE, DELETE, CREATE TABLE, DROP TABLE; .tables lists the \ schema."
type t = { db : Sqlite.t; lock : Eio.Mutex.t; banner : string }
let v ?(banner = default_banner) db = { db; lock = Eio.Mutex.create (); banner }
(* A SQLite statement can suspend on block-device IO mid-run, and a single handle is not safe to use concurrently (even a read mutates the page cache), so the connection fibers sharing [db] take this one lock around each statement. It is the only place the lock is acquired. *)let with_db t f = Eio.Mutex.use_rw ~protect:true t.lock (fun () -> f t.db)
module Backend = struct type nonrec t = t
(* The message of an ordinary SQL error: a statement that does not parse, one the schema or a [RAISE()] refuses, or a constraint it violates. SQLite undoes such a statement, so the handle is as it was. *) let sql_error = function | Sqlite.Error ( Busy _ | Readonly _ | Readonly_rollback _ | Readonly_dbmoved _ | Not_a_database _ ) -> None | Sqlite.Error e -> Fmt.kstr Option.some "%a" Sqlite.Error.pp e | _ -> None
(* Run [sql] and gather every result row as text, like the sqlite3 CLI in list mode. Goes through [with_db], the one place that takes the lock. An SQL error is caught inside the lock and handed back with its backtrace: raised through [use_rw] it would disable the lock for every later statement. *) let run t sql = let rows = ref [] in let run db = match Sqlite.Raw.run db sql ~params:[] ~on_row:(fun row -> rows := List.map Sqlite.Value.to_text row :: !rows) with | () -> Ok () | exception e when Option.is_some (sql_error e) -> Error (e, Printexc.get_raw_backtrace ()) in Result.map (fun () -> List.rev !rows) (with_db t run)
(* [run], with its SQL error raised once the lock is released. *) let collect t sql = match run t sql with | Ok rows -> rows | Error (e, bt) -> Printexc.raise_with_backtrace e bt
let exec t sql = match run t sql with | Ok [] -> Ok [ Sql_shell.Status "ok" ] | Ok rows -> Ok [ Sql_shell.Rows (None, rows) ] | Error (e, bt) -> ( match sql_error e with | Some msg -> Error msg | None -> Printexc.raise_with_backtrace e bt)
let count t name = match collect t (Fmt.str "SELECT count(*) FROM %s" name) with | [ value ] :: _ -> ( try int_of_string value with Failure _ -> 0) | _ -> 0
let tables t = let names = List.filter_map (function [ name ] -> Some name | _ -> None) (collect t "SELECT name FROM sqlite_master WHERE type = 'table' ORDER BY name") in List.map (fun name -> (name, count t name)) names
let dot _ _ = None
let help = "Any SQLite statement, one per line: SELECT, INSERT, UPDATE, DELETE,\n\ CREATE TABLE, DROP TABLE.\n"
let banner t = t.bannerend
module Shell = Sql_shell.Make (Backend)
let shell t ?emit_err ?width ~styled ~emit () = Shell.v t ?emit_err ?width ~styled ~emit ()