Advertisement
Guest User

Untitled

a guest
Apr 18th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
OCaml 1.02 KB | None | 0 0
  1. type (_,_) query =
  2.   | Exec  : ('req, unit, [`Zero]) Caqti_request.t -> ('req, unit) query
  3.   | Find  : ('req, 'resp, [`One | `Zero]) Caqti_request.t -> ('req, 'resp option) query
  4.   | List  : ('req, 'resp, [`Many | `One | `Zero]) Caqti_request.t -> ('req, 'resp list) query
  5.   | Trans : ('req, 'resp) query * 'acc * ('acc -> 'resp -> 'acc) ->
  6.             ('req list, 'acc) query
  7.  
  8. let rec request (type req resp) (module Db : Caqti_lwt.CONNECTION) (q : (req, resp) query) (args : req) : resp Lwt.t =
  9.     match q with
  10.     | Exec q -> Db.exec q args >>= fail_if
  11.     | Find q -> Db.find_opt q args >>= fail_if
  12.     | List q -> Db.rev_collect_list q args >>= fail_if
  13.     | Trans (ql, acc, f) ->
  14.        Db.start () >>= fail_if >>= fun () ->
  15.        List.fold_left (fun acc arg ->
  16.            acc >>= function
  17.            | Error _ as e -> Lwt.return e
  18.            | Ok v -> request (module Db : Caqti_lwt.CONNECTION) ql arg >>= (fun r -> Lwt.return_ok (f v r)))
  19.          (Lwt.return_ok acc) args
  20.        >>= fail_if >>= Db.commit >>= fail_if
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement