Something went wrong. Try again.
SQL frontend over the catalog engine
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144(* A tiny in-memory backend -- a single table [t] of (key, value) text pairs -- exercises the shell's rendering, dot commands, multi-line accumulation and error path without a real SQL engine. *)module Mock = struct type t = (string * string) list ref
(* Reduce a (possibly multi-line) statement to a canonical single-spaced form, dropping a trailing ';', so the toy matcher recognises it. *) let normalize s = let s = String.map (function '\n' | '\t' -> ' ' | c -> c) (String.trim s) in let s = if String.length s > 0 && Char.equal s.[String.length s - 1] ';' then String.sub s 0 (String.length s - 1) else s in String.concat " " (List.filter (fun w -> String.length w > 0) (String.split_on_char ' ' s))
let exec store sql = match String.lowercase_ascii (normalize sql) with | "select * from t" -> Ok [ Sql_shell.Rows (Some [ "k"; "v" ], List.map (fun (k, v) -> [ k; v ]) !store); ] | "insert" -> store := !store @ [ ("a", "1") ]; Ok [ Sql_shell.Status "1 row" ] | "explain" -> Ok [ Sql_shell.Raw "scan t"; Sql_shell.Note "(1 page)" ] | "boom" -> Error "syntax error" | _ -> Error "unknown statement"
let tables store = [ ("t", List.length !store) ] let dot _ = function "answer" -> Some "42\n" | _ -> None let help = ".answer the answer to everything\n" let banner _ = "mock shell"end
module Shell = Sql_shell.Make (Mock)
(* Feed [lines] to a fresh shell and return everything it emitted. *)let run lines = let out = Buffer.create 256 in let store = ref [] in let shell = Shell.v store ~styled:false ~emit:(fun chunk -> Buffer.add_string out chunk) () in Sql_shell.start shell; List.iter (fun l -> ignore (Sql_shell.feed_line shell l : [ `Continue | `Quit ])) lines; Buffer.contents out
let has out sub = Alcotest.(check bool) (Fmt.str "output contains %S" sub) true (Re.execp (Re.compile (Re.str sub)) out)
let test_banner () = has (run []) "mock shell"
let test_select () = let out = run [ "insert;"; "select * from t;" ] in has out "ok 1 row"; has out "a"; has out "1"
let test_error () = has (run [ "boom;" ]) "error: syntax error"
(* One statement may yield several output pieces, rendered in order. *)let test_raw_note () = let out = run [ "explain;" ] in has out "scan t"; has out "(1 page)"
(* A statement split across lines is only run once the ';' arrives. *)let test_multiline () = has (run [ "insert;"; "select *"; "from t;" ]) "a"
let test_dot_tables () = let out = run [ "insert;"; ".tables" ] in has out "t"; has out "1 row"
let test_dot_help () = let out = run [ ".help" ] in has out ".tables"; has out ".answer"
let test_dot_backend () = has (run [ ".answer" ]) "42"let test_dot_unknown () = has (run [ ".nope" ]) "unknown command .nope"
(* [feed_statement] is the one-shot driver: it runs a whole statement without the caller managing line accumulation or the terminating ';'. *)let run_statement sql = let out = Buffer.create 256 in let store = ref [] in let shell = Shell.v store ~styled:false ~emit:(fun chunk -> Buffer.add_string out chunk) () in Sql_shell.feed_statement shell sql; Buffer.contents out
let test_feed_statement_adds_semicolon () = has (run_statement "insert") "ok 1 row"
let test_feed_statement_multiline () = (* The select is split across two lines yet runs as one statement. *) has (run_statement "select *\nfrom t") "(no rows)"
let quits line = let shell = Shell.v (ref []) ~styled:false ~emit:(fun _ -> ()) () in match Sql_shell.feed_line shell line with `Quit -> true | `Continue -> false
let test_quit () = Alcotest.(check bool) ".quit quits" true (quits ".quit"); Alcotest.(check bool) ".exit quits" true (quits ".exit"); Alcotest.(check bool) "plain SQL keeps going" false (quits "select * from t;")
let suite = ( "sql_shell", [ Alcotest.test_case "banner" `Quick test_banner; Alcotest.test_case "select renders rows" `Quick test_select; Alcotest.test_case "error path" `Quick test_error; Alcotest.test_case "raw + note output" `Quick test_raw_note; Alcotest.test_case "multi-line accumulation" `Quick test_multiline; Alcotest.test_case "feed_statement adds ';'" `Quick test_feed_statement_adds_semicolon; Alcotest.test_case "feed_statement multi-line" `Quick test_feed_statement_multiline; Alcotest.test_case ".tables" `Quick test_dot_tables; Alcotest.test_case ".help" `Quick test_dot_help; Alcotest.test_case "backend dot command" `Quick test_dot_backend; Alcotest.test_case "unknown dot command" `Quick test_dot_unknown; Alcotest.test_case ".quit / .exit" `Quick test_quit; ] )