Source file ppx_pgsql.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
open PGOCaml_aux
open Printf
open Migrate_parsetree
open Migrate_parsetree.Ast_408.Ast_mapper
open Migrate_parsetree.Ast_408.Ast_helper
open Migrate_parsetree.Ast_408.Asttypes
open Migrate_parsetree.Ast_408.Parsetree
open Migrate_parsetree.Ast_408.Longident
let nullable_name = "nullable"
let unravel_name = "unravel"
let typname_name = "typname"
type key = PGOCaml.connection_desc
let connections : (key, unit PGOCaml.t) Hashtbl.t = Hashtbl.create 16
[%%if ocaml_version < (4, 08, 0)]
let exp_of_string ~loc:_ x =
let lexer = Lexing.from_string x in
(Migrate_parsetree.Parse.expression
Migrate_parsetree.Versions.ocaml_408
lexer)
[%%else]
let exp_of_string ~loc x =
let lexer =
let acc = Lexing.from_string ~with_positions:false x in
acc.Lexing.lex_start_p <- loc.Warnings.loc_start;
acc.Lexing.lex_curr_p <- loc.Warnings.loc_end;
acc
in
(Migrate_parsetree.Parse.expression
Migrate_parsetree.Versions.ocaml_408
lexer)
[%%endif]
(** [get_connection key] Find the database connection specified by [key],
* otherwise attempt to create a new one from [key] and return that (or an
* error).
*)
let get_connection ~loc key =
match Hashtbl.find_opt connections key with
| Some connection ->
let open Rresult in
Ok connection
| None ->
try
let dbh = PGOCaml.connect ~desc:key () in
let nullable_query = "select attnotnull from pg_attribute where attrelid = $1 and attnum = $2" in
PGOCaml.prepare dbh ~query:nullable_query ~name:nullable_name ();
let unravel_query = "select typname, typtype, typbasetype from pg_type where oid = $1" in
PGOCaml.prepare dbh ~query:unravel_query ~name:unravel_name ();
let typname_query = "select typname from pg_type where oid = $1" in
PGOCaml.prepare dbh ~query:typname_query ~name:typname_name ();
Hashtbl.add connections key dbh;
Rresult.Ok dbh
with
| err ->
Error ("Could not make the connection "
^ PGOCaml.connection_desc_to_string key
^ ", error: "
^ Printexc.to_string err
, loc)
let name_of_type_wrapper dbh oid =
try
Some (PGOCaml.name_of_type oid)
with PGOCaml.Error _ ->
let params = [ Some (PGOCaml.string_of_oid oid) ] in
let rows = PGOCaml.execute dbh ~name:typname_name ~params () in
match rows with
| [ [ Some "citext" ] ] -> Some "string"
| [ [ Some "hstore" ] ] -> Some "hstore"
| _ -> None
let unravel_type dbh ?load_custom_from ?colnam ?argnam ?typnam orig_type =
let get_custom typnam =
match PGOCaml.find_custom_typconvs ?typnam ?lookin:load_custom_from ?colnam ?argnam () with
| Ok convs ->
Option.default "FIXME" typnam, convs
| Error exc -> failwith exc
in
let rec unravel_type_aux ft =
let rv =
let rv =
match typnam with
| Some x ->
Some x, None
| None ->
name_of_type_wrapper dbh ft, None
in
match PGOCaml.find_custom_typconvs ?typnam:(fst rv) ?lookin:load_custom_from ?colnam ?argnam () with
| Ok (x) ->
(fst rv), x
| Error err -> failwith err
in
match rv with
| None, _ ->
let params = [ Some (PGOCaml.string_of_oid ft) ] in
let rows = PGOCaml.execute dbh ~name:unravel_name ~params () in
begin match rows with
| [ typnam :: _ ] when Rresult.R.is_ok (PGOCaml.find_custom_typconvs ?typnam ?lookin:load_custom_from ?colnam ?argnam ()) ->
get_custom typnam
| [ [ Some _; Some typtype ; _ ] ] when typtype = "e" -> "string", None
| [ [ Some _; Some typtype ; Some typbasetype ] ] when typtype = "d" ->
unravel_type_aux (PGOCaml.oid_of_string typbasetype)
| _ ->
failwith "Impossible"
end
| typnam, coders ->
Option.default "FIXME" typnam, coders
in unravel_type_aux orig_type
let rec range a b =
if a < b then a :: range (a+1) b else []
let rex =
let open Re in
[
char '$';
opt (group (char '@'));
opt (group (char '?'));
group (
alt [
seq [
alt [char '_'; rg 'a' 'z'];
rep (alt [char '_'; char '\''; rg 'a' 'z'; rg 'A' 'Z'; rg '0' '9'])
];
seq [
char '{';
rep (diff any (char '}'));
char '}'
]
]
)
] |> seq |> compile
let loc_raise _loc exn =
raise exn
let const_string ~loc str =
{ pexp_desc = Pexp_constant (Pconst_string (str, None));
pexp_loc = loc;
pexp_attributes = [];
pexp_loc_stack = []
}
let parse_flags _config flags loc =
let f_execute = ref false in
let f_nullable_results = ref false in
let host = ref None in
let port = ref None in
let user = ref None in
let password = ref None in
let database = ref None in
let unix_domain_socket_dir = ref None in
let comment_src_loc = ref (PGOCaml.comment_src_loc ()) in
let show = ref None in
let load_custom_from = ref None in
List.iter (
function
| "execute" -> f_execute := true
| "nullable-results" -> f_nullable_results := true
| "show" -> show := Some "show"
| str when String.starts_with str "host=" ->
let host' = String.sub str 5 (String.length str - 5) in
host := Some host'
| str when String.starts_with str "port=" ->
let port' = int_of_string (String.sub str 5 (String.length str - 5)) in
port := Some port'
| str when String.starts_with str "user=" ->
let user' = String.sub str 5 (String.length str - 5) in
user := Some user'
| str when String.starts_with str "password=" ->
let password' = String.sub str 9 (String.length str - 9) in
password := Some password'
| str when String.starts_with str "database=" ->
let database' = String.sub str 9 (String.length str - 9) in
database := Some database'
| str when String.starts_with str "unix_domain_socket_dir=" ->
let socket = String.sub str 23 (String.length str - 23) in
unix_domain_socket_dir := Some socket
| str when String.starts_with str "comment_src_loc=" ->
let comment_src_loc' = String.sub str 19 (String.length str - 19) in
begin match comment_src_loc' with
| "yes" | "1" | "on" -> comment_src_loc := true
| "no" | "0" | "off" -> comment_src_loc := false
| _ -> loc_raise loc (Failure "Unrecognized value for option 'comment_src_loc'")
end
| str when String.starts_with str "show=" ->
let shownam = String.sub str 5 (String.length str - 5) in
show := Some shownam
| str when String.starts_with str "load_custom_from=" ->
let txt = String.sub str 17 (String.length str - 17) in
load_custom_from := Some ((Unix.getcwd ()) ^ "/" ^ txt)
| str ->
loc_raise loc (
Failure ("Unknown flag: " ^ str)
)
) flags;
let f_execute = !f_execute in
let f_nullable_results = !f_nullable_results in
let host = !host in
let user = !user in
let password = !password in
let database = !database in
let port = !port in
let unix_domain_socket_dir = !unix_domain_socket_dir in
let key = PGOCaml.describe_connection ?host ?user ?password ?database ?port ?unix_domain_socket_dir () in
key, f_execute, f_nullable_results, !comment_src_loc, !show, !load_custom_from
let mk_conversions ?load_custom_from ~loc ~dbh results =
List.mapi (
fun i (result, nullable) ->
let field_type = result.PGOCaml.field_type in
let fn =
match unravel_type dbh ?load_custom_from ~colnam:result.PGOCaml.name field_type with
| nam, None ->
let fn = nam ^ "_of_string" in
[%expr PGOCaml.(
[%e
exp_of_string ~loc fn
]
)
][@metaloc loc]
| _nam, Some (_, deserialize) ->
exp_of_string ~loc deserialize
in
let col =
let cname = "c" ^ string_of_int i in
Exp.ident { txt = Lident cname; loc }
in
let sconv = [%expr match [%e col] with Some x -> x | None -> "-"][@metaloc loc] in
if nullable then
([%expr
PGOCaml_aux.Option.map
[%e fn]
[%e col]
]
[@metaloc loc])
, sconv
else
([%expr [%e fn]
(try PGOCaml_aux.Option.get [%e col] with
| _ -> failwith "ppx_pgsql's nullability heuristic has failed - use \"nullable-results\""
)
][@metaloc loc])
, sconv
)
results
let coretype_of_type ~loc ~dbh oid =
let typ =
match unravel_type dbh oid with
| "timestamp", _ -> Longident.Ldot(Ldot(Lident "CalendarLib", "Calendar"), "t")
| nam, _ -> Lident nam
in
{ ptyp_desc = Ptyp_constr({txt = typ; loc}, []);
ptyp_loc = loc;
ptyp_attributes = [];
ptyp_loc_stack = []
}
(** produce a list pattern to match the result of a query *)
let mk_listpat ~loc results =
List.fold_right
(fun i tail ->
let var = Pat.var @@ { txt = "c"^string_of_int i; loc } in
([%pat? [%p var]::[%p tail]][@metaloc loc])
)
(range 0 (List.length results))
([%pat? []][@metaloc loc])
let pgsql_expand ~genobject ?(flags = []) ~config loc dbh query =
let open Rresult in
let (key, f_execute, f_nullable_results, comment_src_loc, show, load_custom_from) = parse_flags config flags loc in
let query =
if comment_src_loc
then
let start = loc.Location.loc_start in
let open Lexing in
(Printf.sprintf
"-- '%s' L%d\n"
start.pos_fname
start.pos_lnum)
^ query
else
query
in
get_connection ~loc key
>>= fun my_dbh ->
let split =
let f = function
| `Text text -> `Text text
| `Delim subs -> `Var Re.Group.(get subs 3, test subs 1, test subs 2)
in List.map f (Re.split_full rex query) in
let (params, results), varmap =
let next = let i = ref 0 in fun () -> incr i; !i in
let varmap = Hashtbl.create 8 in
let query = String.concat "" (
List.map (
function
| `Text text -> text
| `Var (varname, false, option) ->
let i = next () in
Hashtbl.add varmap i (varname, false, option);
sprintf "$%d" i
| `Var (varname, true, option) ->
let i = next () in
Hashtbl.add varmap i (varname, true, option);
sprintf "($%d)" i
) split
) in
let varmap = Hashtbl.fold (
fun i var vars -> (i, var) :: vars
) varmap [] in
try
PGOCaml.prepare my_dbh ~query ();
PGOCaml.describe_statement my_dbh (), varmap
with
exn -> loc_raise loc exn in
if f_execute then ignore (PGOCaml.execute my_dbh ~params:[] ());
if List.length varmap <> List.length params then
loc_raise loc (
Failure ("Mismatch in number of parameters found by database. " ^
"Most likely your statement contains bare $, $number, etc.")
);
let params =
List.fold_right
(fun (i, { PGOCaml.param_type = param_type }) tail ->
let varname, list, option = List.assoc i varmap in
let argnam =
if String.starts_with varname "{"
then None
else Some varname
in
let varname =
if String.starts_with varname "{"
then String.sub varname 1 (String.length varname - 2)
else varname
in
let varname, typnam =
match String.index_opt varname ':' with
| None -> varname, None
| Some _ ->
let[@warning "-8"] [varname; typnam] = String.split_on_char ':' varname in
varname, Some (String.trim typnam)
in
let varname = exp_of_string ~loc varname in
let varname = {varname with pexp_loc = loc} in
let fn =
match unravel_type ?load_custom_from ?argnam ?typnam my_dbh param_type with
| nam, None ->
let fn = exp_of_string ~loc ("string_of_" ^ nam) in
[%expr PGOCaml.([%e fn])][@metaloc loc]
| _, Some (serialize, _) -> exp_of_string ~loc serialize
in
let head =
match list, option with
| false, false -> [%expr [Some ([%e fn] [%e varname])]][@metaloc loc]
| false, true -> [%expr [PGOCaml_aux.Option.map [%e fn] [%e varname]]][@metaloc loc]
| true, false -> [%expr List.map (fun x -> Some ([%e fn] x)) [%e varname]][@metaloc loc]
| true, true -> [%expr List.map (fun x -> PGOCaml_aux.Option.map [%e fn]) [%e varname]][@metaloc loc]
in
([%expr [%e head]::[%e tail]][@metaloc loc])
)
(List.combine (range 1 (1 + List.length varmap)) params)
([%expr []][@metaloc loc])
in
let expr =
let split = List.fold_right (
fun s tail ->
let head = match s with
| `Text text -> ([%expr `Text [%e const_string ~loc text]][@metaloc loc])
| `Var (varname, list, option) ->
let list =
if list then ([%expr true][@metaloc loc]) else ([%expr false][@metaloc loc]) in
let option =
if option then ([%expr true][@metaloc loc]) else ([%expr false][@metaloc loc]) in
([%expr `Var ([%e const_string ~loc varname],
[%e list],
[%e option])][@metaloc loc]) in
([%expr [%e head] :: [%e tail]][@metaloc loc])
) split ([%expr []][@metaloc loc]) in
[%expr
let dbh = [%e dbh] in
let params : string option list list = [%e params] in
let split = [%e split] in
let i = ref 0 in
let j = ref 0 in
let query = String.concat "" (
List.map (
function
| `Text text -> text
| `Var (_varname, false, _) ->
let () = incr i in
let () = incr j in
"$" ^ string_of_int j.contents
| `Var (_varname, true, _) ->
let param = List.nth params i.contents in
let () = incr i in
"(" ^
String.concat "," (
List.map (
fun _ ->
let () = incr j in
"$" ^ string_of_int j.contents
) param
) ^
")"
) split) in
let params = List.flatten params in
let name = "ppx_pgsql." ^ Digest.to_hex (Digest.string query) in
let hash =
try PGOCaml.private_data dbh
with
| Not_found ->
let hash = Hashtbl.create 17 in
PGOCaml.set_private_data dbh hash;
hash
in
let is_prepared = Hashtbl.mem hash name in
PGOCaml.bind
(if not is_prepared then
PGOCaml.bind (PGOCaml.prepare dbh ~name ~query ()) (fun () ->
Hashtbl.add hash name true;
PGOCaml.return ()
)
else
PGOCaml.return ()) (fun () ->
PGOCaml.execute_rev dbh ~name ~params ())
][@metaloc loc] in
(** decorate the results with the nullability heuristic *)
let results' =
match results with
| Some results ->
Some (
List.map
(fun result ->
match (result.PGOCaml.table, result.PGOCaml.column) with
| Some table, Some column ->
let params =
[ Some (PGOCaml.string_of_oid table);
Some (PGOCaml.string_of_int column) ] in
let _rows =
PGOCaml.execute my_dbh ~name:nullable_name ~params () in
let not_nullable =
match _rows with
| [ [ Some b ] ] -> PGOCaml.bool_of_string b
| _ -> false in
result, f_nullable_results || not not_nullable
| _ -> result, f_nullable_results || true )
results
)
| None ->
None
in
let mkexpr ~convert ~list = [%expr
PGOCaml.bind [%e expr] (fun _rows ->
PGOCaml.return
(let original_query = [%e const_string ~loc query] in
List.rev_map (
fun row ->
match row with
| [%p list] -> [%e convert]
| _ ->
let msg = "ppx_pgsql: internal error: " ^
"Incorrect number of columns returned from query: " ^
original_query ^
". Columns are: " ^
String.concat "; " (
List.map (
function
| Some str -> Printf.sprintf "%S" str
| None -> "NULL"
) row
) in
raise (PGOCaml.Error msg)
) _rows))
][@metaloc loc]
in
match (genobject, results') with
| true, Some results ->
let list = mk_listpat ~loc results in
let fields =
List.map
(fun ({PGOCaml.name; field_type; _}, nullable) ->
name, coretype_of_type ~loc ~dbh:my_dbh field_type, nullable)
results
in
let convert =
List.fold_left2
(fun (lsacc, showacc) (name, _, _) (conv, sconv) ->
let hd =
{ pcf_desc = Pcf_method(
{txt = name; loc}
, Public
, Cfk_concrete(Fresh, conv)
)
; pcf_loc = loc
; pcf_attributes = []
}
in
let ename = const_string ~loc name in
let showacc =
[%expr
let fields = ([%e ename], [%e sconv]) :: fields in
[%e showacc]
][@metaloc loc]
in
(hd :: lsacc, showacc)
)
( []
, [%expr
List.fold_left
(fun buffer (name, value) ->
let () = Buffer.add_string buffer name in
let () = Buffer.add_char buffer ':' in
let () = Buffer.add_char buffer ' ' in
let () = Buffer.add_string buffer value in
let () = Buffer.add_char buffer '\n' in
buffer
)
(Buffer.create 16)
fields
|> Buffer.contents
][@metaloc loc]
)
fields
(mk_conversions ?load_custom_from ~loc ~dbh:my_dbh results)
|> fun (fields, fshow) ->
let fshow =
[%expr let fields = [] in [%e fshow]][@metaloc loc]
in
let fields =
match show with
| Some txt ->
{ pcf_desc = Pcf_method(
{txt; loc}
, Public
, Cfk_concrete(Fresh, fshow)
)
; pcf_loc = loc
; pcf_attributes = []
}
:: fields
| None ->
fields
in
Exp.mk (
Pexp_object({
pcstr_self = Pat.any ~loc ()
; pcstr_fields = fields
})
)
in
let expr = mkexpr ~convert ~list in
Ok expr
| true, None ->
Error("It doesn't make sense to make an object to encapsulate results that aren't coming", loc)
| false, Some results ->
let list = mk_listpat ~loc results in
let convert =
let conversions =
mk_conversions ?load_custom_from ~loc ~dbh:my_dbh results
|> List.map fst
in
match conversions with
| [] -> [%expr ()][@metaloc loc]
| [a] -> a
| conversions -> Exp.tuple conversions
in
Ok(mkexpr ~convert ~list)
| false, None ->
Ok ([%expr PGOCaml.bind [%e expr] (fun _rows -> PGOCaml.return ())][@metaloc loc])
let expand_sql ~genobject ~config loc dbh extras =
let query, flags =
match List.rev extras with
| [] -> assert false
| query :: flags -> query, flags in
try pgsql_expand ~config ~genobject ~flags loc dbh query
with
| Failure s -> Error(s, loc)
| PGOCaml.Error s -> Error(s, loc)
| PGOCaml.PostgreSQL_Error (s, fields) ->
let fields' = List.map (fun (c, s) -> Printf.sprintf "(%c: %s)" c s) fields in
Error ("Postgres backend error: " ^ s ^ ": " ^ s ^ String.concat "," fields', loc)
| exn -> Error("Unexpected PG'OCaml PPX error: " ^ Printexc.to_string exn, loc)
let list_of_string_args mapper args =
let maybe_strs =
List.map
(function
| (Nolabel, {pexp_desc = Pexp_constant (Pconst_string (str, None)); _})
-> Some str
| (_, other) ->
match mapper other with
| {pexp_desc = Pexp_constant (Pconst_string (str, None)); _}
-> Some str
| _ -> None
)
args
in
if List.mem None maybe_strs then
[]
else
List.map (function Some x -> x | None -> assert false) maybe_strs
let pgocaml_rewriter config _cookies =
{ default_mapper with
expr = fun mapper expr ->
let unsupported loc =
{ expr with
pexp_desc = Pexp_extension (
extension_of_error @@
Location.error ~loc (Printf.sprintf "Something unsupported")
)
}
in
match expr with
| { pexp_desc =
Pexp_extension (
{ txt ; loc }
, PStr [{ pstr_desc = Pstr_eval ({pexp_desc = Pexp_apply (dbh, args); pexp_loc = qloc; _}, _); _}]
)
; _
} when String.starts_with txt "pgsql" ->
let open Rresult in
let genobject = txt = "pgsql.object" in
( match list_of_string_args (default_mapper.expr mapper) args with
| [] -> unsupported loc
| args ->
let x = expand_sql ~config ~genobject loc dbh args in
( match x with
| Rresult.Ok pexp -> {pexp with pexp_loc = qloc}
| Error(s, loc) ->
{ expr with
pexp_desc = Pexp_extension (
extension_of_error @@
Location.error ~loc ("PG'OCaml PPX error: " ^ s))
; pexp_loc = loc
}
)
)
| { pexp_desc =
Pexp_extension (
{ txt = "pgsql"; loc }
, _)
; _
} ->
unsupported loc
| other ->
default_mapper.expr mapper other
}
let () =
Driver.register ~name:"pgocaml" ~reset_args:(fun _ -> ()) ~args:[]
Versions.ocaml_408 pgocaml_rewriter