Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix empty list parameters. #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/runtime/ppx_pgsql_runtime.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,11 @@ let rebuild_query_with_placeholders query_fragments params =
| `Variable (_name, true (* is_list *), _) ->
let list_param = List.nth params !param_idx in
let list_placeholders = List.map mkplaceholder list_param in
let expr = match list_placeholders with
| [] -> "NULL"
| _ -> String.concat "," list_placeholders in
incr param_idx;
"(" ^ String.concat "," list_placeholders ^ ")"
"(" ^ expr ^ ")"
) query_fragments in
String.concat "" fragments

Expand Down
42 changes: 42 additions & 0 deletions tests/test_ppx.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,45 @@
let test1 = [%sqlf "SELECT 1"]

let test2 = [%sqlf "SELECT hasindexes FROM pg_catalog.pg_tables WHERE hasrules = $hasrules"]

let test_list = [%sqlf {|
SELECT true WHERE $?needle::integer IN $@haystack
|}]

let check msg f =
print_string @@ msg ^ "... " ;
try
assert (f ());
print_endline "OK"
with e ->
print_endline "FAIL";
raise e

let () =
let dbh = PGOCaml.connect () in
check "test 1" (fun () ->
let res = test1 dbh in
res = [Some 1l]);

check "test 2" (fun () ->
let _res = test2 ~hasrules:true dbh in
(* TODO: fixme *)
true);

check "test list param positive" (fun () ->
let res = test_list ~needle:1l ~haystack:[1l;2l;3l] dbh in
res = [Some true]);

check "test list param negative" (fun () ->
let res = test_list ~needle:4l ~haystack:[1l;2l;3l] dbh in
res = []);

check "test list param with NULL" (fun () ->
let res = test_list ?needle:None ~haystack:[1l;2l;3l] dbh in
res = []);

check "test list param with empty list" (fun () ->
let res = test_list ~needle:1l ~haystack:[] dbh in
res = []);

PGOCaml.close dbh