Something went wrong. Try again.
SQLite file format and SQL engine in pure OCaml
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596(* One handle on the database named by argv(1), driven a line at a time from stdin, so a test can hold it in a given state while it asks something of another process. argv(2) is [write] for a writable handle or [read] for a read-only one; the handle's busy timeout is [0.], so it never waits.
Each command line is answered with one line on stdout:
- [put TABLE KEY VALUE] puts [VALUE] under [KEY] in the key-value table [TABLE]: [ok]; - [get TABLE KEY] is the value, or [none]; - [begin], [commit] and [rollback] run the statement: [ok]; - [after SECONDS VERB] sleeps [SECONDS], then runs [VERB] as above: [ok]; - [exec SQL] runs the statements of the rest of the line ({!Sqlite.Raw.exec}): [ok]; - [hold SQL] runs the rest of the line as a script ({!Sqlite.Raw.run}), answering [row] at each row it returns and waiting there for the next line of stdin before it goes on: [ok] once the script ends; - [close] closes the handle and answers [ok], after which the process exits.
A command refused because another handle holds the file answers [busy] and the message; any other failure answers [error] and the exception. An end of stdin closes the handle. A separate executable because POSIX record locks do not exclude within one process: only another process meets the locks a handle holds. *)
let answer line = print_endline line; flush stdout
let table db name = if String.equal name "kv" then `Kv else `Named (Sqlite.Kv.Table.v db ~name)
let put db name key value = match table db name with | `Kv -> Sqlite.Kv.put db key value | `Named t -> Sqlite.Kv.Table.put t key value
let find db name key = match table db name with | `Kv -> Sqlite.Kv.find db key | `Named t -> Sqlite.Kv.Table.find t key
let command db words = match words with | [ "put"; name; key; value ] -> put db name key value; "ok" | [ "get"; name; key ] -> Option.value ~default:"none" (find db name key) | [ (("begin" | "commit" | "rollback") as verb) ] -> Sqlite.Raw.exec db (String.uppercase_ascii verb) ~params:[]; "ok" | [ "after"; seconds; (("begin" | "commit" | "rollback") as verb) ] -> Unix.sleepf (float_of_string seconds); Sqlite.Raw.exec db (String.uppercase_ascii verb) ~params:[]; "ok" | "exec" :: sql -> Sqlite.Raw.exec db (String.concat " " sql) ~params:[]; "ok" | "hold" :: sql -> Sqlite.Raw.run db (String.concat " " sql) ~params:[] ~on_row:(fun _ -> answer "row"; ignore (input_line stdin : string)); "ok" | _ -> "error unknown command: " ^ String.concat " " words
let rec serve db = match input_line stdin with | exception End_of_file -> Sqlite.close db | "close" -> Sqlite.close db; answer "ok" | line -> let words = String.split_on_char ' ' line in (match command db words with | reply -> answer reply | exception Sqlite.Error (Busy message) -> answer ("busy " ^ message) | exception ((Sqlite.Error _ | Invalid_argument _ | Eio.Io _) as e) -> answer ("error " ^ Printexc.to_string e)); serve db
let () = match Sys.argv with | [| _; path; ("write" | "read") as mode |] -> Eio_main.run @@ fun env -> Eio.Switch.run @@ fun sw -> let db = Sqlite.open_ ~sw ~busy_timeout:0. ~read_only:(mode = "read") Eio.Path.(Eio.Stdenv.fs env / path) in answer "open"; serve db | _ -> prerr_endline "usage: db_session.exe PATH (write | read)"; exit 2