Something went wrong. Try again.
SQLite file format and SQL engine in pure OCaml
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889open Cmdliner
let read_kv_tty t = let rows = ref [] in Sqlite.Kv.iter t ~f:(fun k v -> rows := [ Console.View.text k; Console.View.text v ] :: !rows); let rows = List.rev !rows in let table = Console.Table.( view ~border:Console.Border.rounded [ column (Console.View.text "key"); column (Console.View.text "value") ] rows) in Fmt.pr "%a@." Console.View.pp table
let read_kv_pipe t = Sqlite.Kv.iter t ~f:(fun k v -> Fmt.pr "%s|%s@." k v)
(* A name folds case-insensitively, as every other door of this engine resolves one. A name no table holds is a typo, refused in [Table.iter_table]'s own words rather than a [Not_found] that reaches Cmdliner as exit 125. *)let schema_for_table t name = let named (tb : Sqlite.Table.t) = Sqlite.Ident.equal tb.schema.table name in match List.find_opt named (Sqlite.Table.tables t) with | Some tb -> tb.schema | None -> Sqlite.Error.fail_prepare "no such table: %s" name
(* Both paths spell a cell the same way, so the drawn table and the stream agree. *)let table_row_views values = List.map (fun v -> Console.View.text (Sqlite.Value.to_shell v)) values
let read_table_tty t name = let schema = schema_for_table t name in let cols = List.map (fun (c : Sqlite.Schema.Table.column) -> Console.Table.column (Console.View.text c.name)) schema.columns in let rows = ref [] in Sqlite.Table.iter_table t name ~f:(fun _rowid values -> rows := table_row_views values :: !rows); let rows = List.rev !rows in let table = Console.Table.view ~border:Console.Border.rounded cols rows in Fmt.pr "%a@." Console.View.pp table
let read_table_pipe t name = Sqlite.Table.iter_table t name ~f:(fun _rowid values -> List_mode.print values)
let read () db table_name = Common.user_error ~db @@ fun () -> Eio_main.run @@ fun env -> Eio.Switch.run @@ fun sw -> (* Reading creates nothing: a path that is not there is a typo to report. *) Common.with_db env ~sw ~create:false db @@ fun t -> match (table_name, Console_tty.is_terminal Unix.stdout) with | None, true -> read_kv_tty t | None, false -> read_kv_pipe t | Some name, true -> read_table_tty t name | Some name, false -> read_table_pipe t name
let cmd = let table_name = Arg.( value & opt (some string) None & info [ "t"; "table" ] ~docv:"TABLE" ~doc:"Table to read. Defaults to the kv table.") in let info = Cmd.info "read" ~doc:"Read all entries from a database." ~man: [ `S Manpage.s_description; `P "Dump all key-value pairs from a SQLite database."; `P "When stdout is a terminal, output is rendered as a table. \ Otherwise, entries are printed as pipe-separated values."; `P "Use $(b,--table) to read a specific table. Without it, reads the \ default $(b,kv) table."; `S Manpage.s_examples; `P "$(iname) mydb.db"; `P "$(iname) mydb.db -t users"; ] in Cmd.v info Term.(const read $ Common.logging $ Common.db_arg $ table_name)