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 generation of UNION ALL statements #9

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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.eunit
*.beam
.rebar/*
ebin/
5 changes: 0 additions & 5 deletions ebin/sqerl.app

This file was deleted.

12 changes: 9 additions & 3 deletions src/sqerl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions test/sqerl_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -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}})
},
Expand Down