stmt => 'SELECT * FROM test WHERE ( a < to_date(?, \'MM/DD/YY\') AND b = ? )',
stmt_q => 'SELECT * FROM `test` WHERE ( `a` < to_date(?, \'MM/DD/YY\') AND `b` = ? )',
bind => [[{dummy => 1} => '02/02/02'], [b => 8]],
- },
+ },
#57
{
func => 'select',
stmt => 'SELECT * FROM test WHERE ( a = ? AND b = ? ) OR ( c = ? AND d = ? )',
stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? AND `b` = ? ) OR ( `c` = ? AND `d` = ? )',
bind => [[a => 'a'], [b => 'b'], [ c => 'c'],[ d => 'd']],
- },
-
+ },
+ #58
+ {
+ func => 'select',
+ new => {bindtype => 'columns'},
+ args => ['test', '*', [ { a => 1, b => 1}, [ a => 2, b => 2] ] ],
+ stmt => 'SELECT * FROM test WHERE ( a = ? AND b = ? ) OR ( a = ? OR b = ? )',
+ stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? AND `b` = ? ) OR ( `a` = ? OR `b` = ? )',
+ bind => [[a => 1], [b => 1], [ a => 2], [ b => 2]],
+ },
+ #59
+ {
+ func => 'select',
+ new => {bindtype => 'columns'},
+ args => ['test', '*', [ [ a => 1, b => 1], { a => 2, b => 2 } ] ],
+ stmt => 'SELECT * FROM test WHERE ( a = ? OR b = ? ) OR ( a = ? AND b = ? )',
+ stmt_q => 'SELECT * FROM `test` WHERE ( `a` = ? OR `b` = ? ) OR ( `a` = ? AND `b` = ? )',
+ bind => [[a => 1], [b => 1], [ a => 2], [ b => 2]],
+ },
);
},
);
-# add extra modifier tests, based on 2 outcomes
-my $mod_or_and = {
- stmt => 'WHERE ( foo = ? OR bar = ? ) AND baz = ? ',
- bind => [qw/1 2 3/],
-};
-my $mod_or_or = {
- stmt => 'WHERE ( foo = ? OR bar = ? ) OR baz = ?',
- bind => [qw/1 2 3/],
-};
-my $mod_and_or = {
- stmt => 'WHERE ( foo = ? AND bar = ? ) OR baz = ?',
- bind => [qw/1 2 3/],
-};
-
-push @handle_tests, (
- # test modifiers within hashrefs
- {
- where => { -or => [
- [ foo => 1, bar => 2 ],
- baz => 3,
- ]},
- %$mod_or_or,
- },
- {
- where => { -and => [
- [ foo => 1, bar => 2 ],
- baz => 3,
- ]},
- %$mod_or_and,
- },
-
- # test modifiers within arrayrefs
- {
- where => [ -or => [
- [ foo => 1, bar => 2 ],
- baz => 3,
- ]],
- %$mod_or_or,
- },
- {
- where => [ -and => [
- [ foo => 1, bar => 2 ],
- baz => 3,
- ]],
- %$mod_or_and,
- },
-
- # test ambiguous modifiers within hashrefs (op extends to to immediate RHS only)
- {
- where => { -and => [ -or =>
- [ foo => 1, bar => 2 ],
- baz => 3,
- ]},
- %$mod_or_and,
- },
- {
- where => { -or => [ -and =>
- [ foo => 1, bar => 2 ],
- baz => 3,
- ]},
- %$mod_and_or,
- },
-
- # test ambiguous modifiers within arrayrefs (op extends to to immediate RHS only)
- {
- where => [ -and => [ -or =>
- [ foo => 1, bar => 2 ],
- baz => 3,
- ]],
- %$mod_or_and,
- },
- {
- where => [ -or => [ -and =>
- [ foo => 1, bar => 2 ],
- baz => 3,
- ]],
- %$mod_and_or,
- },
-);
-
plan tests => ( @handle_tests * 2 ) + 1;
for my $case (@handle_tests) {
=cut
-# no warnings
+# no warnings (the -or/-and => { } warning is silly, there is nothing wrong with such usage)
+my $and_or_args = {
+ and => { stmt => 'WHERE a = ? AND b = ?', bind => [qw/1 2/] },
+ or => { stmt => 'WHERE a = ? OR b = ?', bind => [qw/1 2/] },
+ or_and => { stmt => 'WHERE ( foo = ? OR bar = ? ) AND baz = ? ', bind => [qw/1 2 3/] },
+ or_or => { stmt => 'WHERE foo = ? OR bar = ? OR baz = ?', bind => [qw/1 2 3/] },
+ and_or => { stmt => 'WHERE ( foo = ? AND bar = ? ) OR baz = ?', bind => [qw/1 2 3/] },
+};
+
my @and_or_tests = (
+ # basic tests
{
where => { -and => [a => 1, b => 2] },
- stmt => 'WHERE a = ? AND b = ?',
- bind => [qw/1 2/],
+ %{$and_or_args->{and}},
},
{
where => [ -and => [a => 1, b => 2] ],
- stmt => 'WHERE a = ? AND b = ?',
- bind => [qw/1 2/],
+ %{$and_or_args->{and}},
},
{
where => { -or => [a => 1, b => 2] },
- stmt => 'WHERE a = ? OR b = ?',
- bind => [qw/1 2/],
+ %{$and_or_args->{or}},
},
{
where => [ -or => [a => 1, b => 2] ],
- stmt => 'WHERE a = ? OR b = ?',
- bind => [qw/1 2/],
+ %{$and_or_args->{or}},
+ },
+
+ # test modifiers within hashrefs
+ {
+ where => { -or => [
+ [ foo => 1, bar => 2 ],
+ baz => 3,
+ ]},
+ %{$and_or_args->{or_or}},
+ },
+ {
+ where => { -and => [
+ [ foo => 1, bar => 2 ],
+ baz => 3,
+ ]},
+ %{$and_or_args->{or_and}},
+ },
+
+ # test modifiers within arrayrefs
+ {
+ where => [ -or => [
+ [ foo => 1, bar => 2 ],
+ baz => 3,
+ ]],
+ %{$and_or_args->{or_or}},
+ },
+ {
+ where => [ -and => [
+ [ foo => 1, bar => 2 ],
+ baz => 3,
+ ]],
+ %{$and_or_args->{or_and}},
+ },
+
+ # test ambiguous modifiers within hashrefs (op extends to to immediate RHS only)
+ {
+ where => { -and => [ -or =>
+ [ foo => 1, bar => 2 ],
+ baz => 3,
+ ]},
+ %{$and_or_args->{or_and}},
+ },
+ {
+ where => { -or => [ -and =>
+ [ foo => 1, bar => 2 ],
+ baz => 3,
+ ]},
+ %{$and_or_args->{and_or}},
+ },
+
+ # test ambiguous modifiers within arrayrefs (op extends to to immediate RHS only)
+ {
+ where => [ -and => [ -or =>
+ [ foo => 1, bar => 2 ],
+ baz => 3,
+ ]],
+ %{$and_or_args->{or_and}},
+ },
+ {
+ where => [ -or => [ -and =>
+ [ foo => 1, bar => 2 ],
+ baz => 3
+ ]],
+ %{$and_or_args->{and_or}},
+ },
+
+ # the -or should affect only the next element
+ {
+ where => { x => {
+ -or => { '!=', 1, '>=', 2 }, -like => 'x%'
+ }},
+ stmt => 'WHERE (x != ? OR x >= ?) AND x LIKE ?',
+ bind => [qw/1 2 x%/],
+ },
+ # the -and should affect only the next element
+ {
+ where => { x => [
+ -and => [ 1, 2 ], { -like => 'x%' }
+ ]},
+ stmt => 'WHERE (x = ? AND x = ?) OR x LIKE ?',
+ bind => [qw/1 2 x%/],
},
{
where => { -and => [a => 1, b => 2], x => 9, -or => { c => 3, d => 4 } },
},
{
- # things should remain the same as above, hashrefs not affected
+ # flip logic except where excplicitly requested otherwise
args => { logic => 'or' },
where => { -or => [a => 1, b => 2, k => [11, 12] ], x => 9, -and => { c => 3, d => 4, l => { '=' => [21, 22] } } },
stmt => 'WHERE c = ? AND d = ? AND ( l = ? OR l = ?) AND (a = ? OR b = ? OR k = ? OR k = ?) AND x = ?',
bind => [1 .. 13],
},
{
- # while the arrayref logic should flip, except when requested otherwise
+ # flip logic except where excplicitly requested otherwise
args => { logic => 'and' },
where => [ -or => [a => 1, b => 2], -or => { c => 3, d => 4}, e => 5, -and => [ f => 6, g => 7], [ h => 8, i => 9, -and => [ k => 10, l => 11] ], { m => 12, n => 13 }],
stmt => 'WHERE (a = ? OR b = ?) AND (c = ? OR d = ?) AND e = ? AND f = ? AND g = ? AND h = ? AND i = ? AND k = ? AND l = ? AND m = ? AND n = ?',
bind => [1 .. 13],
},
+
+ ##########
+ # some corner cases by ldami (some produce useless SQL, just for clarification on 1.5 direction)
+ #
+
+ {
+ where => { foo => [
+ -and => [ { -like => 'foo%'}, {'>' => 'moo'} ],
+ { -like => '%bar', '<' => 'baz'},
+ [ {-like => '%alpha'}, {-like => '%beta'} ],
+ -or => { '!=' => 'toto', '=' => 'koko' }
+ ] },
+ stmt => 'WHERE (foo LIKE ? AND foo > ?) OR (foo LIKE ? AND foo < ?) OR (foo LIKE ? OR foo LIKE ?) OR (foo != ? OR foo = ?)',
+ bind => [qw/foo% moo %bar baz %alpha %beta toto koko/],
+ },
+ {
+ where => [-and => [{foo => 1}, {bar => 2}, -or => {baz => 3 }] ],
+ stmt => 'WHERE foo = ? AND bar = ? AND baz = ?',
+ bind => [qw/1 2 3/],
+ },
+ {
+ where => [-and => [{foo => 1}, {bar => 2}, -or => {baz => 3, boz => 4} ] ],
+ stmt => 'WHERE foo = ? AND bar = ? AND (baz = ? OR boz = ?)',
+ bind => [1 .. 4],
+ },
+
+ # -and affects only the first {} thus a noop
+ {
+ where => { col => [ -and => {'<' => 123}, {'>' => 456 }, {'!=' => 789} ] },
+ stmt => 'WHERE col < ? OR col > ? OR col != ?',
+ bind => [qw/123 456 789/],
+ },
+
+ # -and affects the entire inner [], thus 3 ANDs
+ {
+ where => { col => [ -and => [{'<' => 123}, {'>' => 456 }, {'!=' => 789}] ] },
+ stmt => 'WHERE col < ? AND col > ? AND col != ?',
+ bind => [qw/123 456 789/],
+ },
);
my @nest_tests = (); #can not be verified via is_same_sql_bind - need exact matching (parenthesis and all)