type output = | Rows of string list option * string list list | Status of string | Note of string | Raw of string module type BACKEND = sig type t val exec : t -> string -> (output list, string) result val tables : t -> (string * int) list val dot : t -> string -> string option val help : string val banner : t -> string end (* The shell closes over its backend's operations at construction, so the flat accessors below work on any [t] without naming the backend module. *) type t = { exec : string -> (output list, string) result; tables : unit -> (string * int) list; dot : string -> string option; help : string; banner : string; styled : bool; width : (unit -> int) option; emit : string -> unit; emit_err : string -> unit; (* statement errors; defaults to [emit] *) buf : Buffer.t; (* accumulates one (possibly multi-line) statement *) } let paint t style s = if t.styled then Console.View.(to_string ~styled:true (styled style (text s))) else s let ok t = paint t Console.Style.(fg Console.Color.green) let error t = paint t Console.Style.(fg Console.Color.red) let dim t = paint t Console.Style.faint let prompt t ~cont = if t.styled then t.emit (paint t Console.Style.(fg Console.Color.cyan) (if cont then " ...> " else "sql> ")) let plural n = if n = 1 then "" else "s" let render_rows t headers rows = match rows with | [] -> t.emit (dim t "(no rows)\n") | first :: _ -> let width = List.length first in let headers = match headers with | Some h when List.length h = width -> h | _ -> List.init width (fun i -> Fmt.str "c%d" (i + 1)) in let table = Console.Table.view (List.map (fun h -> Console.Table.column (Console.View.text h)) headers) (List.map (List.map Console.View.text) rows) in let width = Option.map (fun width -> width ()) t.width in t.emit (Console.View.to_string (Console.View.sized ?width table)); t.emit "\n"; let n = List.length rows in t.emit (dim t (Fmt.str "%d row%s\n" n (plural n))) let render_output t = function | Rows (headers, rows) -> render_rows t headers rows | Status status -> t.emit (ok t (Fmt.str "ok %s\n" status)) | Note note -> t.emit (dim t (note ^ "\n")) | Raw line -> t.emit (line ^ "\n") let run_statement t sql = match t.exec sql with | Ok outputs -> List.iter (render_output t) outputs | Error msg -> t.emit_err (error t (Fmt.str "error: %s\n" msg)) let run_tables t = match t.tables () with | [] -> t.emit (dim t "(no tables)\n") | tables -> List.iter (fun (name, n) -> t.emit (Fmt.str "%s %s\n" name (dim t (Fmt.str "(%d row%s)" n (plural n))))) tables let run_help t = t.emit (Fmt.str "SQL statements end with ';'.\n\ .tables list tables\n\ .help show this help\n\ .quit leave (or Ctrl-D)\n\ %s" t.help) let run_dot t cmd = match cmd with | "tables" -> run_tables t | "help" -> run_help t | other -> ( match t.dot other with | Some out -> t.emit out | None -> t.emit (error t (Fmt.str "unknown command .%s (try .help)\n" other))) let feed_line t line = let trimmed = String.trim line in let nonempty = String.length trimmed > 0 in let pending = Buffer.length t.buf > 0 in if (not pending) && nonempty && Char.equal trimmed.[0] '.' then ( match String.sub trimmed 1 (String.length trimmed - 1) with | "quit" | "exit" -> `Quit | cmd -> run_dot t cmd; prompt t ~cont:false; `Continue) else if (not pending) && not nonempty then ( prompt t ~cont:false; `Continue) else ( Buffer.add_string t.buf line; Buffer.add_char t.buf '\n'; let complete = nonempty && Char.equal trimmed.[String.length trimmed - 1] ';' in if complete then ( let sql = Buffer.contents t.buf in Buffer.clear t.buf; run_statement t sql; prompt t ~cont:false) else prompt t ~cont:true; `Continue) let feed_statement t sql = let trimmed = String.trim sql in let sql = if String.length trimmed > 0 && Char.equal trimmed.[String.length trimmed - 1] ';' then sql else sql ^ ";" in List.iter (fun line -> ignore (feed_line t line : [ `Continue | `Quit ])) (String.split_on_char '\n' sql) let start t = t.emit t.banner; t.emit "\n"; prompt t ~cont:false module Make (B : BACKEND) = struct let v backend ?emit_err ?width ~styled ~emit () = { exec = B.exec backend; tables = (fun () -> B.tables backend); dot = B.dot backend; help = B.help; banner = B.banner backend; styled; width; emit; emit_err = (match emit_err with Some f -> f | None -> emit); buf = Buffer.create 64; } end