Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- type (_,_) query =
- | Exec : ('req, unit, [`Zero]) Caqti_request.t -> ('req, unit) query
- | Find : ('req, 'resp, [`One | `Zero]) Caqti_request.t -> ('req, 'resp option) query
- | List : ('req, 'resp, [`Many | `One | `Zero]) Caqti_request.t -> ('req, 'resp list) query
- | Trans : ('req, 'resp) query * 'acc * ('acc -> 'resp -> 'acc) ->
- ('req list, 'acc) query
- let rec request (type req resp) (module Db : Caqti_lwt.CONNECTION) (q : (req, resp) query) (args : req) : resp Lwt.t =
- match q with
- | Exec q -> Db.exec q args >>= fail_if
- | Find q -> Db.find_opt q args >>= fail_if
- | List q -> Db.rev_collect_list q args >>= fail_if
- | Trans (ql, acc, f) ->
- Db.start () >>= fail_if >>= fun () ->
- List.fold_left (fun acc arg ->
- acc >>= function
- | Error _ as e -> Lwt.return e
- | Ok v -> request (module Db : Caqti_lwt.CONNECTION) ql arg >>= (fun r -> Lwt.return_ok (f v r)))
- (Lwt.return_ok acc) args
- >>= fail_if >>= Db.commit >>= fail_if
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement