diff --git a/lib/runtime/ppx_pgsql_runtime.ml b/lib/runtime/ppx_pgsql_runtime.ml index 52e2931..d8a1714 100644 --- a/lib/runtime/ppx_pgsql_runtime.ml +++ b/lib/runtime/ppx_pgsql_runtime.ml @@ -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 diff --git a/tests/test_ppx.ml b/tests/test_ppx.ml index 7443978..d2e7bb7 100644 --- a/tests/test_ppx.ml +++ b/tests/test_ppx.ml @@ -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