(** Interactive SQL shells. A shell takes its input one line at a time, so it works both for a stdin REPL and for an event-driven transport such as a network channel. The backend runs statements and lists tables; the shell collects lines, runs dot commands, prints results with {!Console.Table} and writes prompts. *) (** The type for the parts of the output of a statement. A statement may give several, such as a note and a status, so {!BACKEND.exec} returns a list. *) type output = | Rows of string list option * string list list (** Result rows as text, with optional column headers, printed as a {!Console.Table} and a row count. *) | Status of string (** The status of a write, such as ["3 rows"], printed as ["ok ..."]. *) | Note of string (** A dimmed note, such as ["(created table users)"]. *) | Raw of string (** A line printed as is, such as an [EXPLAIN] line. *) (** Shell backends. *) module type BACKEND = sig type t (** The type for SQL stores. *) val exec : t -> string -> (output list, string) result (** [exec t sql] is the output of the statement [sql], in print order, or [Error msg], which the shell prints as ["error: msg"]. *) val tables : t -> (string * int) list (** [tables t] is each table with its row count, for [.tables]. *) val dot : t -> string -> string option (** [dot t cmd] is [Some out] with the output of the dot command [cmd] of the backend, the word after the dot such as ["log"], or [None] if the backend does not know [cmd]. *) val help : string (** [help] is the text [.help] adds for the dot commands and dialect of the backend. *) val banner : t -> string (** [banner t] is the greeting of an interactive session. *) end type t (** The type for shell sessions. *) val start : t -> unit (** [start t] writes the banner and the first prompt. *) val feed_line : t -> string -> [ `Continue | `Quit ] (** [feed_line t line] handles one input line: a dot command, a blank line, or part of a statement, collected until its final [;]. It writes any result and the next prompt, and is [`Quit] after [.quit] or [.exit]. *) val feed_statement : t -> string -> unit (** [feed_statement t sql] runs the one statement [sql], adding a final [;] if it has none, through {!feed_line}. It serves a caller that runs a single statement, such as [ssh host 'SELECT ...'] or a [-c] flag. *) module Make (B : BACKEND) : sig val v : B.t -> ?emit_err:(string -> unit) -> ?width:(unit -> int) -> styled:bool -> emit:(string -> unit) -> unit -> t (** [v backend ~emit_err ~width ~styled ~emit ()] is a new shell over [backend] that writes all its output with [emit]. [styled] turns on ANSI colours and the prompt. [width ()], if given, is read before each result table, so a remote terminal can resize. Lines end with ['\n']. Statement errors go to [emit_err] (defaults to [emit]). *) end