Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ushitora-anqou committed Aug 24, 2023
1 parent ffb6e32 commit 5a4eb8b
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions lib/sql_writer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,22 @@ ORDER BY t.created_at DESC, t.id DESC, p.id DESC
Db.find Q.insert_transaction (string_of_date date, narration)
|> raise_if_error
let insert_transactions (module Db : Caqti_lwt.CONNECTION)
(txs : Model.transaction list) =
(* FIXME: SQL injection *)
let query =
txs
|> List.map (fun (tx : Model.transaction) ->
Printf.sprintf "('%s', '%s')" (string_of_date tx.date) tx.narration)
|> String.concat " "
|> Printf.sprintf
{|INSERT INTO transactions (created_at, narration) VALUES %s RETURNING id|}
in
let open Caqti_request.Infix in
let open Caqti_type.Std in
Db.fold_s ((unit ->* int) query) (fun x xs -> Lwt.return_ok (x :: xs)) () []
|> raise_if_error
let insert_posting (module Db : Caqti_lwt.CONNECTION) ~account_id
~transaction_id ~amount ~narration =
Db.exec Q.insert_posting (account_id, transaction_id, amount, narration)
Expand Down Expand Up @@ -266,6 +282,23 @@ let dump uri (model : Model.t) =
Store.create_full_accounts_view con;%lwt
let%lwt tx_ids = Store.insert_transactions con model.transactions in
let tags =
model.transactions
|> List.map (fun (tx : Model.transaction) -> tx.tags)
|> List.flatten |> List.sort_uniq compare
in
let%lwt tag_ids = Store.insert_tags con tags in
let tag_name_to_id =
List.compose tags tag_ids |> List.to_seq |> Hashtbl.of_seq
in
List.compose model.transactions tx_ids
|> List.map (fun ((tx : Model.transaction), tx_id) ->
tx.tags
|> List.map (fun name -> (tx_id, Hashtbl.find name tag_name_to_id)))
|> List.flatten |> Store.insert_transaction_tags;%lwt
(model.transactions
|> Lwt_list.iter_s @@ fun (tx : Model.transaction) ->
let%lwt tx_id =
Expand Down

0 comments on commit 5a4eb8b

Please sign in to comment.