diff --git a/.gitignore b/.gitignore index fcb4d5a..d536a33 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .eunit *.beam .rebar/* +ebin/ diff --git a/ebin/sqerl.app b/ebin/sqerl.app deleted file mode 100644 index af1163c..0000000 --- a/ebin/sqerl.app +++ /dev/null @@ -1,5 +0,0 @@ -{application,sqerl, - [{description,"An SQL generating DSL"}, - {vsn,"0.1.0"}, - {applications,[kernel,stdlib]}, - {modules,[sqerl]}]}. diff --git a/src/sqerl.erl b/src/sqerl.erl index eaf074d..e104ec9 100644 --- a/src/sqerl.erl +++ b/src/sqerl.erl @@ -146,11 +146,11 @@ sql2({Select1, union, Select2, {where, _} = Where, Extras}, Safe) -> sql2({Select1, union_all, Select2}, Safe) -> [$(, sql2(Select1, Safe), <<") UNION ALL (">>, sql2(Select2, Safe), $)]; sql2({Select1, union_all, Select2, {where, WhereExpr}}, Safe) -> - [sql2({Select1, union, Select2}, Safe), where(WhereExpr, Safe)]; + [sql2({Select1, union_all, Select2}, Safe), where(WhereExpr, Safe)]; sql2({Select1, union_all, Select2, Extras}, Safe) -> - [sql2({Select1, union, Select2}, Safe), extra_clause(Extras, Safe)]; + [sql2({Select1, union_all, Select2}, Safe), extra_clause(Extras, Safe)]; sql2({Select1, union_all, Select2, {where, _} = Where, Extras}, Safe) -> - [sql2({Select1, union, Select2, Where}, Safe), extra_clause(Extras, Safe)]; + [sql2({Select1, union_all, Select2, Where}, Safe), extra_clause(Extras, Safe)]; sql2({insert, Table, Params}, _Safe) -> insert(Table, Params); @@ -424,6 +424,12 @@ expr({Val, Op, {_, union, _, _} = Subquery}, Safe) -> subquery(Val, Op, Subquery, Safe); expr({Val, Op, {_, union, _, _, _} = Subquery}, Safe) -> subquery(Val, Op, Subquery, Safe); +expr({Val, Op, {_, union_all, _} = Subquery}, Safe) -> + subquery(Val, Op, Subquery, Safe); +expr({Val, Op, {_, union_all, _, _} = Subquery}, Safe) -> + subquery(Val, Op, Subquery, Safe); +expr({Val, Op, {_, union_all, _, _, _} = Subquery}, Safe) -> + subquery(Val, Op, Subquery, Safe); expr({_, in, []}, _Safe) -> <<"0">>; expr({Val, Op, Values}, Safe) when (Op =:= in orelse Op =:= any orelse diff --git a/test/sqerl_tests.erl b/test/sqerl_tests.erl index 19832ca..49c0c28 100644 --- a/test/sqerl_tests.erl +++ b/test/sqerl_tests.erl @@ -127,6 +127,29 @@ safe_test_() -> {select,name,{from,project}}}) }, + {<<"(SELECT name FROM person) UNION ALL (SELECT name FROM project)">>, + ?_safe_test({{select,name,{from,person}}, + union_all, + {select,name,{from,project}}}) + }, + + {<<"SELECT id FROM person WHERE id IN ((SELECT 1) UNION ALL (SELECT 1))">>, + ?_safe_test({select, id, + {from, person}, + {where, {id, in, {{select, 1}, + union_all, + {select, 1}}}}}) + }, + + {<<"SELECT id FROM person WHERE id IN ((SELECT 1) UNION ALL (SELECT 1) WHERE (2 = 1))">>, + ?_safe_test({select, id, + {from, person}, + {where, {id, in, {{select, 1}, + union_all, + {select, 1}, + {where, {2, '=', 1}}}}}}) + }, + {<<"SELECT DISTINCT name FROM person LIMIT 5">>, ?_safe_test({select,distinct,name,{from,person},{limit,5}}) },