Skip to content

Commit

Permalink
refactor sql code into mina_caqti
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang Jiawei committed Jan 5, 2024
1 parent 49874ea commit 6e5f053
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 60 deletions.
64 changes: 14 additions & 50 deletions src/app/archive/lib/processor.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1417,9 +1417,11 @@ module Zkapp_events = struct

let table_name = "zkapp_events"

let sep_by_comma ?(parenthesis = false) xs =
List.map xs ~f:(if parenthesis then sprintf "('%s')" else sprintf "'%s'")
|> String.concat ~sep:", "
module Field_array_map = Map.Make (struct
type t = int array [@@deriving sexp]

let compare = Array.compare Int.compare
end)

(* Account_update.Body.Events'.t is defined as `field array list`,
which is ismorphic to a list of list of fields.
Expand Down Expand Up @@ -1450,28 +1452,11 @@ module Zkapp_events = struct
let fields = field_list_list |> List.concat in
let%bind field_id_list_list =
if not @@ List.is_empty fields then
let field_insert =
sprintf
{sql| INSERT INTO zkapp_field (field) VALUES %s
ON CONFLICT (field)
DO NOTHING |sql}
(sep_by_comma ~parenthesis:true fields)
in
let%bind () =
Conn.exec (Caqti_request.exec Caqti_type.unit field_insert) ()
in
let field_search =
sprintf
{sql| SELECT field, id FROM zkapp_field
WHERE field in (%s) |sql}
@@ sep_by_comma fields
in
let%map field_map =
Conn.collect_list
(Caqti_request.collect Caqti_type.unit
Caqti_type.(tup2 string int)
field_search )
()
Mina_caqti.insert_multi_into_col ~table_name:"zkapp_field"
~col:("field", Caqti_type.string)
(module Conn)
fields
>>| String.Map.of_alist_exn
in
let field_id_list_list =
Expand All @@ -1482,38 +1467,17 @@ module Zkapp_events = struct
(* if there's no fields, then we must have some list of empty lists *)
return @@ List.map field_list_list ~f:(fun _ -> [])
in
(* this conversion should be done by caqti using `typ`, FIX this in the future *)
let field_array_list =
List.map field_id_list_list ~f:(fun id_list ->
List.map id_list ~f:Int.to_string
|> String.concat ~sep:", " |> sprintf "{%s}" )
in
let field_array_insert =
sprintf
{sql| INSERT INTO zkapp_field_array (element_ids) VALUES %s
ON CONFLICT (element_ids)
DO NOTHING |sql}
@@ sep_by_comma ~parenthesis:true field_array_list
in
let%bind () =
Conn.exec (Caqti_request.exec Caqti_type.unit field_array_insert) ()
in
let field_array_search =
sprintf
{sql| SELECT element_ids, id FROM zkapp_field_array
WHERE element_ids in (%s) |sql}
@@ sep_by_comma field_array_list
in
let module Field_array_map = Map.Make (struct
type t = int array [@@deriving sexp]

let compare = Array.compare Int.compare
end) in
let%map field_array_map =
Conn.collect_list
(Caqti_request.collect Caqti_type.unit
Caqti_type.(tup2 Mina_caqti.array_int_typ int)
field_array_search )
()
Mina_caqti.insert_multi_into_col ~table_name:"zkapp_field_array"
~col:("element_ids", Mina_caqti.array_int_typ)
(module Conn)
field_array_list
>>| Field_array_map.of_alist_exn
in
let field_array_id_list =
Expand Down
46 changes: 36 additions & 10 deletions src/lib/mina_caqti/mina_caqti.ml
Original file line number Diff line number Diff line change
Expand Up @@ -287,19 +287,45 @@ let select_insert_into_cols ~(select : string * 'select Caqti_type.t)
@@ select_cols ~select:(fst select) ~table_name ?tannot ~cols:(fst cols) ()
)
value
>>= fun res ->
match res with
>>= function
| Some id ->
return id
| None ->
let%map res =
Conn.find
( Caqti_request.find (snd cols) (snd select)
@@ insert_into_cols ~returning:(fst select) ~table_name ?tannot
~cols:(fst cols) () )
value
in
res
Conn.find
( Caqti_request.find (snd cols) (snd select)
@@ insert_into_cols ~returning:(fst select) ~table_name ?tannot
~cols:(fst cols) () )
value

let sep_by_comma ?(parenthesis = false) xs =
List.map xs ~f:(if parenthesis then sprintf "('%s')" else sprintf "'%s'")
|> String.concat ~sep:", "

let insert_multi_into_col ~(table_name : string)
~(col : string * 'col Caqti_type.t) (module Conn : CONNECTION)
(values : string list) =
let open Deferred.Result.Let_syntax in
let insert =
sprintf
{sql| INSERT INTO %s (%s) VALUES %s
ON CONFLICT (%s)
DO NOTHING |sql}
table_name (fst col)
(sep_by_comma ~parenthesis:true values)
(fst col)
in
let%bind () = Conn.exec (Caqti_request.exec Caqti_type.unit insert) () in
let search =
sprintf
{sql| SELECT %s, id FROM %s
WHERE %s in (%s) |sql}
(fst col) table_name (fst col) (sep_by_comma values)
in
Conn.collect_list
(Caqti_request.collect Caqti_type.unit
Caqti_type.(tup2 (snd col) int)
search )
()

let query ~f pool =
match%bind Caqti_async.Pool.use f pool with
Expand Down

0 comments on commit 6e5f053

Please sign in to comment.