Source file caqti_request.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
let (%) f g x = f (g x)
module Log = (val Logs.src_log (Logs.Src.create "caqti"))
type query = Caqti_query.t =
| L of string
| Q of string
| P of int
| E of string
| S of query list
type ('a, 'b, +'m) t = {
id: int option;
query: Caqti_driver_info.t -> query;
param_type: 'a Caqti_type.t;
row_type: 'b Caqti_type.t;
row_mult: 'm Caqti_mult.t;
} constraint 'm = [< `Zero | `One | `Many]
let last_id = ref (-1)
let create ?(oneshot = false) param_type row_type row_mult query =
let id = if oneshot then None else (incr last_id; Some !last_id) in
{id; query; param_type; row_type; row_mult}
let param_type request = request.param_type
let row_type request = request.row_type
let row_mult request = request.row_mult
let query_id request = request.id
let query request = request.query
module Infix = struct
let (-->.) t u ?oneshot f = create ?oneshot t u Caqti_mult.zero f
let (-->!) t u ?oneshot f = create ?oneshot t u Caqti_mult.one f
let (-->?) t u ?oneshot f = create ?oneshot t u Caqti_mult.zero_or_one f
let (-->*) t u ?oneshot f = create ?oneshot t u Caqti_mult.zero_or_more f
let (@:-) f s =
let q = Caqti_query.of_string_exn s in
f (fun _ -> q)
let (@@:-) f g =
f (Caqti_query.of_string_exn % g % Caqti_driver_info.dialect_tag)
let (->.) t u ?oneshot s = create ?oneshot t u Caqti_mult.zero @:- s
let (->!) t u ?oneshot s = create ?oneshot t u Caqti_mult.one @:- s
let (->?) t u ?oneshot s = create ?oneshot t u Caqti_mult.zero_or_one @:- s
let (->*) t u ?oneshot s = create ?oneshot t u Caqti_mult.zero_or_more @:- s
end
let no_env _ _ = raise Not_found
let make_pp ?(env = no_env) ?(driver_info = Caqti_driver_info.dummy) ()
ppf req =
let query = Caqti_query.expand (env driver_info) (req.query driver_info) in
Format.fprintf ppf "(%a -->%s %a) {|%a|}"
Caqti_type.pp req.param_type
(match Caqti_mult.expose req.row_mult with
| `Zero -> "."
| `One -> ""
| `Zero_or_one -> "?"
| `Zero_or_more -> "*")
Caqti_type.pp req.row_type
Caqti_query.pp query
let pp ppf = make_pp () ppf
let pp_with_param_enabled =
(match Sys.getenv "CAQTI_DEBUG_PARAM" with
| "true" -> true
| "false" -> false
| s ->
Log.err (fun f ->
f "Invalid value %s for CAQTI_DEBUG_PARAM, assuming false." s);
false
| exception Not_found -> false)
let make_pp_with_param ?env ?driver_info () ppf (req, param) =
let pp = make_pp ?env ?driver_info () in
pp ppf req;
if pp_with_param_enabled then
Format.fprintf ppf " %a" Caqti_type.pp_value (req.param_type, param)