Skip to content

Commit ea76b51

Browse files
committed
Fix empty list parameters.
1 parent 7051058 commit ea76b51

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

lib/runtime/ppx_pgsql_runtime.ml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,11 @@ let rebuild_query_with_placeholders query_fragments params =
4545
| `Variable (_name, true (* is_list *), _) ->
4646
let list_param = List.nth params !param_idx in
4747
let list_placeholders = List.map mkplaceholder list_param in
48+
let expr = match list_placeholders with
49+
| [] -> "NULL"
50+
| _ -> String.concat "," list_placeholders in
4851
incr param_idx;
49-
"(" ^ String.concat "," list_placeholders ^ ")"
52+
"(" ^ expr ^ ")"
5053
) query_fragments in
5154
String.concat "" fragments
5255

tests/test_ppx.ml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,45 @@
11
let test1 = [%sqlf "SELECT 1"]
22

33
let test2 = [%sqlf "SELECT hasindexes FROM pg_catalog.pg_tables WHERE hasrules = $hasrules"]
4+
5+
let test_list = [%sqlf {|
6+
SELECT true WHERE $?needle::integer in $@haystack
7+
|}]
8+
9+
let check msg f =
10+
print_string @@ msg ^ "... " ;
11+
try
12+
assert (f ());
13+
print_endline "OK"
14+
with e ->
15+
print_endline "FAIL";
16+
raise e
17+
18+
let () =
19+
let dbh = PGOCaml.connect () in
20+
check "test 1" (fun () ->
21+
let res = test1 dbh in
22+
res = [Some 1l]);
23+
24+
check "test 2" (fun () ->
25+
let _res = test2 ~hasrules:true dbh in
26+
(* TODO: fixme *)
27+
true);
28+
29+
check "test list param positive" (fun () ->
30+
let res = test_list ~needle:1l ~haystack:[1l;2l;3l] dbh in
31+
res = [Some true]);
32+
33+
check "test list param negative" (fun () ->
34+
let res = test_list ~needle:4l ~haystack:[1l;2l;3l] dbh in
35+
res = []);
36+
37+
check "test list param with NULL" (fun () ->
38+
let res = test_list ?needle:None ~haystack:[1l;2l;3l] dbh in
39+
res = []);
40+
41+
check "test list param with empty list" (fun () ->
42+
let res = test_list ~needle:1l ~haystack:[] dbh in
43+
res = []);
44+
45+
PGOCaml.close dbh

0 commit comments

Comments
 (0)