Something went wrong. Try again.
SQLite file format and SQL engine in pure OCaml
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940type failure = Refused of Catalog.Error.t | Defect of exn
let pp_failure ppf = function | Refused e -> Catalog.Error.pp ppf e | Defect e -> Fmt.pf ppf "defect: %s" (Printexc.to_string e)
let statements db sql = let known name = List.exists (Catalog.Ident.equal name) (Catalog.Spi.tables (Sql.catalog db)) in Sql.Private.Lexer.script ~known sql
let exec db text = match Sql.exec db text with | outcome -> Ok outcome | exception Catalog.Error e -> Error (Refused e) | exception ((Invalid_argument _ | Failure _ | Not_found) as e) -> Error (Defect e)
(* Each statement runs through [Sql.exec] alone, so the rows of each SELECT are handed over before the next statement is parsed. *)let run ?(on_statement = ignore) db sql ~on_row = let rec go statements = match Seq.uncons statements with | None -> Ok () | Some (Error msg, _) -> Error (Refused (Prepare msg)) | Some (Ok (text, _), rest) -> ( on_statement text; match exec db text with | Ok (Sql.Rows rows) -> List.iter on_row rows; go rest | Ok Sql.Done -> go rest | Error _ as e -> e) in go (statements db sql)
let replay db texts = List.iter (fun text -> match exec db text with Ok _ | Error _ -> ()) texts