Skip to content

Commit

Permalink
Don't use Caqti but sqlite3-ocaml
Browse files Browse the repository at this point in the history
  • Loading branch information
ushitora-anqou committed Aug 26, 2023
1 parent 2ccc030 commit 40981a0
Show file tree
Hide file tree
Showing 11 changed files with 422 additions and 493 deletions.
4 changes: 1 addition & 3 deletions dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
(alcotest :with-test)
(ocamlformat
(= 0.26.0))
caqti
caqti-driver-sqlite3
caqti-lwt
csv
dream
dune
Expand All @@ -41,6 +38,7 @@
ppx_deriving
(ppx_yojson_conv
(<= v0.15.1))
sqlite3
yojson))

(using menhir 2.1)
14 changes: 3 additions & 11 deletions lib/command.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
open Util
open Lwt.Infix

let to_json filename =
match Parser.parse_file filename with
Expand All @@ -21,8 +20,7 @@ let check filename =
try
let m, notes = Loader.load_file filename in
match
Lwt_main.run
(Sql_writer.dump_on_memory m >>= fun con -> Verifier.verify con notes)
Sql_writer.dump_on_memory m |> fun con -> Verifier.verify con notes
with
| Ok () ->
Printf.printf "%d accounts\n%d transactions\n" (List.length m.accounts)
Expand All @@ -32,14 +30,8 @@ let check filename =

let dump in_filename out_filename =
let m, _ = Loader.load_file in_filename in
Lwt_main.run
@@
let%lwt _ =
Sql_writer.dump
("sqlite3://" ^ Filename.concat (Sys.getcwd ()) out_filename)
m
in
Lwt.return_unit
let filepath = Filename.concat (Sys.getcwd ()) out_filename in
Sql_writer.dump filepath m |> ignore

let serve =
let interface, port =
Expand Down
41 changes: 41 additions & 0 deletions lib/datastore.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
type connection = Sqlite3.db
type prepared_stmt = Sqlite3.stmt
type value = Int of int | Text of string | Null

let value_of_sqlite3_data = function
| Sqlite3.Data.INT i -> Int (Int64.to_int i)
| Sqlite3.Data.TEXT s -> Text s
| Sqlite3.Data.NULL -> Null
| _ -> failwith "value_of_sqlite3_data: Invalid datatype"

let sqlite3_data_of_value = function
| Int i -> Sqlite3.Data.INT (Int64.of_int i)
| Text s -> Sqlite3.Data.TEXT s
| Null -> Sqlite3.Data.NULL

let in_memory_database = ":memory:"
let connect path = Sqlite3.db_open path
let disconnect con = Sqlite3.db_close con |> ignore
let prepare conn sql = Sqlite3.prepare conn sql

exception Sqlite_error of string

let raise_if_not_success rc =
if Sqlite3.Rc.is_success rc then ()
else raise (Sqlite_error (Sqlite3.Rc.to_string rc))

let query stmt values =
try
Sqlite3.reset stmt |> raise_if_not_success;
values
|> List.map sqlite3_data_of_value
|> Sqlite3.bind_values stmt |> raise_if_not_success;
let result, rows =
Sqlite3.fold stmt ~init:[] ~f:(fun acc row ->
(Array.to_list row |> List.map value_of_sqlite3_data) :: acc)
in
raise_if_not_success result;
Ok (List.rev rows)
with Sqlite_error msg -> Error msg

let execute stmt values = query stmt values |> Result.map (fun _ -> ())
10 changes: 10 additions & 0 deletions lib/datastore.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type connection
type prepared_stmt
type value = Int of int | Text of string | Null

val in_memory_database : string
val connect : string -> connection
val disconnect : connection -> unit
val prepare : connection -> string -> prepared_stmt
val execute : prepared_stmt -> value list -> (unit, string) result
val query : prepared_stmt -> value list -> (value list list, string) result
10 changes: 1 addition & 9 deletions lib/dune
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,4 @@
(name qash)
(preprocess
(pps lwt_ppx ppx_deriving.make ppx_deriving.show ppx_yojson_conv))
(libraries
caqti-driver-sqlite3
caqti-lwt
cmdliner
csv
dream
fsnotify
logs.fmt
pcre))
(libraries cmdliner csv dream fsnotify logs.fmt pcre sqlite3))
Loading

0 comments on commit 40981a0

Please sign in to comment.