7 use SQL::Abstract::Test import => ['is_same_sql_bind'];
10 use Storable qw/dclone/;
15 # -nest has been undocumented on purpose, but is still supported for the
16 # foreseable future. Do not rip out the -nest tests before speaking to
17 # someone on the DBIC mailing list or in irc.perl.org#dbix-class
22 Test -and -or and -nest modifiers, assuming the following:
24 * Modifiers are respected in both hashrefs and arrayrefs (with the obvious
25 limitation of one modifier type per hahsref)
26 * When in condition context i.e. where => { -or { a = 1 } }, each modifier
27 affects only the immediate element following it.
28 * When in column multi-condition context i.e.
29 where => { x => { '!=', [-and, [qw/1 2 3/]] } }, a modifier affects the
30 OUTER ARRAYREF if and only if it is the first element of said ARRAYREF
34 # no warnings (the -or/-and => { } warning is silly, there is nothing wrong with such usage)
36 and => { stmt => 'WHERE a = ? AND b = ?', bind => [qw/1 2/] },
37 or => { stmt => 'WHERE a = ? OR b = ?', bind => [qw/1 2/] },
38 or_and => { stmt => 'WHERE ( foo = ? OR bar = ? ) AND baz = ? ', bind => [qw/1 2 3/] },
39 or_or => { stmt => 'WHERE foo = ? OR bar = ? OR baz = ?', bind => [qw/1 2 3/] },
40 and_or => { stmt => 'WHERE ( foo = ? AND bar = ? ) OR baz = ?', bind => [qw/1 2 3/] },
46 where => { -and => [a => 1, b => 2] },
47 %{$and_or_args->{and}},
50 where => [ -and => [a => 1, b => 2] ],
51 %{$and_or_args->{and}},
54 where => { -or => [a => 1, b => 2] },
55 %{$and_or_args->{or}},
58 where => [ -or => [a => 1, b => 2] ],
59 %{$and_or_args->{or}},
63 where => { -and => {a => 1, b => 2} },
64 %{$and_or_args->{and}},
67 where => [ -and => {a => 1, b => 2} ],
68 %{$and_or_args->{and}},
71 where => { -or => {a => 1, b => 2} },
72 %{$and_or_args->{or}},
75 where => [ -or => {a => 1, b => 2} ],
76 %{$and_or_args->{or}},
79 # test modifiers within hashrefs
82 [ foo => 1, bar => 2 ],
85 %{$and_or_args->{or_or}},
89 [ foo => 1, bar => 2 ],
92 %{$and_or_args->{or_and}},
95 # test modifiers within arrayrefs
98 [ foo => 1, bar => 2 ],
101 %{$and_or_args->{or_or}},
105 [ foo => 1, bar => 2 ],
108 %{$and_or_args->{or_and}},
111 # test ambiguous modifiers within hashrefs (op extends to to immediate RHS only)
113 where => { -and => [ -or =>
114 [ foo => 1, bar => 2 ],
117 %{$and_or_args->{or_and}},
120 where => { -or => [ -and =>
121 [ foo => 1, bar => 2 ],
124 %{$and_or_args->{and_or}},
127 # test ambiguous modifiers within arrayrefs (op extends to to immediate RHS only)
129 where => [ -and => [ -or =>
130 [ foo => 1, bar => 2 ],
133 %{$and_or_args->{or_and}},
136 where => [ -or => [ -and =>
137 [ foo => 1, bar => 2 ],
140 %{$and_or_args->{and_or}},
143 # test column multi-cond in arrayref (useless example)
145 where => { x => [ -and => (1 .. 3) ] },
146 stmt => 'WHERE x = ? AND x = ? AND x = ?',
149 # test column multi-cond in arrayref (more useful)
151 where => { x => [ -and => {'!=' => 1}, {'!=' => 2}, {'!=' => 3} ] },
152 stmt => 'WHERE x != ? AND x != ? AND x != ?',
155 # test column multi-cond in arrayref (even more useful)
157 where => { x => { '!=' => [ -and => (1 .. 3) ] } },
158 stmt => 'WHERE x != ? AND x != ? AND x != ?',
162 # the -or should affect only the inner hashref, as we are not in an outer arrayref
165 -or => { '!=', 1, '>=', 2 }, -like => 'x%'
167 stmt => 'WHERE x LIKE ? AND ( x != ? OR x >= ? )',
168 bind => [qw/x% 1 2/],
171 # the -and should affect the OUTER arrayref, while the internal structures remain intact
174 -and => [ 1, 2 ], { -like => 'x%' }
176 stmt => 'WHERE (x = ? OR x = ?) AND x LIKE ?',
177 bind => [qw/1 2 x%/],
181 where => { -and => [a => 1, b => 2], x => 9, -or => { c => 3, d => 4 } },
182 stmt => 'WHERE a = ? AND b = ? AND ( c = ? OR d = ? ) AND x = ?',
183 bind => [qw/1 2 3 4 9/],
187 where => { -and => [a => 1, b => 2, k => [11, 12] ], x => 9, -or => { c => 3, d => 4, l => { '=' => [21, 22] } } },
188 stmt => 'WHERE a = ? AND b = ? AND (k = ? OR k = ?) AND (c = ? OR d = ? OR (l = ? OR l = ?) ) AND x = ?',
189 bind => [qw/1 2 11 12 3 4 21 22 9/],
193 where => { -or => [a => 1, b => 2, k => [11, 12] ], x => 9, -and => { c => 3, d => 4, l => { '=' => [21, 22] } } },
194 stmt => 'WHERE c = ? AND d = ? AND ( l = ? OR l = ?) AND (a = ? OR b = ? OR k = ? OR k = ?) AND x = ?',
195 bind => [qw/3 4 21 22 1 2 11 12 9/],
199 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 }],
200 stmt => 'WHERE a = ? OR b = ? OR c = ? OR d = ? OR e = ? OR ( f = ? AND g = ?) OR h = ? OR i = ? OR ( k = ? AND l = ? ) OR (m = ? AND n = ?)',
205 # explicit OR logic in arrays should leave everything intact
206 args => { logic => 'or' },
207 where => { -and => [a => 1, b => 2, k => [11, 12] ], x => 9, -or => { c => 3, d => 4, l => { '=' => [21, 22] } } },
208 stmt => 'WHERE a = ? AND b = ? AND (k = ? OR k = ?) AND ( c = ? OR d = ? OR l = ? OR l = ? ) AND x = ? ',
209 bind => [qw/1 2 11 12 3 4 21 22 9/],
213 # flip logic in arrays except where excplicitly requested otherwise
214 args => { logic => 'and' },
215 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 }],
216 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 = ?',
220 # 1st -and is in column mode, thus flips the entire array, whereas the
221 # 2nd one is just a condition modifier
224 col => [ -and => {'<' => 123}, {'>' => 456 }, {'!=' => 789} ],
226 col2 => [ -or => { -like => 'crap' }, { -like => 'crop' } ],
227 col3 => [ -and => { -like => 'chap' }, { -like => 'chop' } ],
231 (col < ? AND col > ? AND col != ?)
234 ( col2 LIKE ? OR col2 LIKE ? )
236 ( col3 LIKE ? AND col3 LIKE ? )
239 bind => [qw/123 456 789 crap crop chap chop/],
243 # some corner cases by ldami (some produce useless SQL, just for clarification on 1.5 direction)
248 -and => [ { -like => 'foo%'}, {'>' => 'moo'} ],
249 { -like => '%bar', '<' => 'baz'},
250 [ {-like => '%alpha'}, {-like => '%beta'} ],
251 [ {'!=' => 'toto', '=' => 'koko'} ],
253 stmt => 'WHERE (foo LIKE ? OR foo > ?) AND (foo LIKE ? AND foo < ?) AND (foo LIKE ? OR foo LIKE ?) AND (foo != ? AND foo = ?)',
254 bind => [qw/foo% moo %bar baz %alpha %beta toto koko/],
259 -and => [a => 1, b => 2],
260 -or => [c => 3, d => 4],
261 e => [-and => {-like => 'foo%'}, {-like => '%bar'} ],
263 stmt => 'WHERE (a = ? AND b = ?) OR c = ? OR d = ? OR (e LIKE ? AND e LIKE ?)',
264 bind => [qw/1 2 3 4 foo% %bar/],
267 # -or has nothing to flip
269 where => [-and => [{foo => 1}, {bar => 2}, -or => {baz => 3}] ],
270 stmt => 'WHERE foo = ? AND bar = ? AND baz = ?',
274 where => [-and => [{foo => 1}, {bar => 2}, -or => {baz => 3, woz => 4} ] ],
275 stmt => 'WHERE foo = ? AND bar = ? AND (baz = ? OR woz = ?)',
279 # -and has only 1 following element, thus all still ORed
281 where => { col => [ -and => [{'<' => 123}, {'>' => 456 }, {'!=' => 789}] ] },
282 stmt => 'WHERE col < ? OR col > ? OR col != ?',
283 bind => [qw/123 456 789/],
286 # flipping array logic affects both column value and condition arrays
288 args => { logic => 'and' },
289 where => [ col => [ {'<' => 123}, {'>' => 456 }, {'!=' => 789} ], col2 => 0 ],
290 stmt => 'WHERE col < ? AND col > ? AND col != ? AND col2 = ?',
291 bind => [qw/123 456 789 0/],
294 # flipping array logic with explicit -and works
296 args => { logic => 'and' },
297 where => [ col => [ -and => {'<' => 123}, {'>' => 456 }, {'!=' => 789} ], col2 => 0 ],
298 stmt => 'WHERE col < ? AND col > ? AND col != ? AND col2 = ?',
299 bind => [qw/123 456 789 0/],
301 # flipping array logic with explicit -or flipping it back
303 args => { logic => 'and' },
304 where => [ col => [ -or => {'<' => 123}, {'>' => 456 }, {'!=' => 789} ], col2 => 0 ],
305 stmt => 'WHERE (col < ? OR col > ? OR col != ?) AND col2 = ?',
306 bind => [qw/123 456 789 0/],
310 # modN and mod_N were a bad design decision - they go away in SQLA2, warn now
311 my @numbered_mods = (
314 -and => [a => 10, b => 11],
315 -and2 => [ c => 20, d => 21 ],
317 -nest2 => [ y => 2 ],
318 -or => { m => 7, n => 8 },
319 -or2 => { m => 17, n => 18 },
321 correct => { -and => [
322 -and => [a => 10, b => 11],
323 -and => [ c => 20, d => 21 ],
326 -or => { m => 7, n => 8 },
327 -or => { m => 17, n => 18 },
332 -and2 => [a => 10, b => 11],
333 -and_3 => [ c => 20, d => 21 ],
334 -nest2 => [ x => 1 ],
335 -nest_3 => [ y => 2 ],
336 -or2 => { m => 7, n => 8 },
337 -or_3 => { m => 17, n => 18 },
339 correct => [ -and => [
340 -and => [a => 10, b => 11],
341 -and => [ c => 20, d => 21 ],
344 -or => { m => 7, n => 8 },
345 -or => { m => 17, n => 18 },
352 where => {a => 1, -nest => [b => 2, c => 3]},
353 stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
357 where => {a => 1, -nest => {b => 2, c => 3}},
358 stmt => 'WHERE ( ( (b = ? AND c = ?) AND a = ? ) )',
362 where => {a => 1, -or => {-nest => {b => 2, c => 3}}},
363 stmt => 'WHERE ( ( (b = ? AND c = ?) AND a = ? ) )',
367 where => {a => 1, -or => {-nest => [b => 2, c => 3]}},
368 stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
372 where => {a => 1, -nest => {-or => {b => 2, c => 3}}},
373 stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
377 where => [a => 1, -nest => {b => 2, c => 3}, -nest => [d => 4, e => 5]],
378 stmt => 'WHERE ( ( a = ? OR ( b = ? AND c = ? ) OR ( d = ? OR e = ? ) ) )',
379 bind => [qw/1 2 3 4 5/],
383 plan tests => @and_or_tests*4 + @numbered_mods*4 + @nest_tests*2;
385 for my $case (@and_or_tests) {
387 local $TODO = $case->{todo} if $case->{todo};
389 local $Data::Dumper::Terse = 1;
392 local $SIG{__WARN__} = sub { push @w, @_ };
394 my $sql = SQL::Abstract->new ($case->{args} || {});
395 my $where_copy = dclone($case->{where});
398 my ($stmt, @bind) = $sql->where($case->{where});
405 || diag "Search term:\n" . Dumper $case->{where};
407 is (@w, 0, 'No warnings within and-or tests')
408 || diag join "\n", 'Emitted warnings:', @w;
410 is_deeply ($case->{where}, $where_copy, 'Where conditions unchanged');
414 for my $case (@nest_tests) {
416 local $TODO = $case->{todo} if $case->{todo};
418 local $SQL::Abstract::Test::parenthesis_significant = 1;
419 local $Data::Dumper::Terse = 1;
421 my $sql = SQL::Abstract->new ($case->{args} || {});
423 my ($stmt, @bind) = $sql->where($case->{where});
430 || diag "Search term:\n" . Dumper $case->{where};
437 my $w_str = "\QUse of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0\E";
438 for my $case (@numbered_mods) {
440 local $TODO = $case->{todo} if $case->{todo};
442 local $Data::Dumper::Terse = 1;
445 local $SIG{__WARN__} = sub { push @w, @_ };
446 my $sql = SQL::Abstract->new ($case->{args} || {});
448 my ($old_s, @old_b) = $sql->where($case->{backcompat});
449 my ($new_s, @new_b) = $sql->where($case->{correct});
453 'Backcompat and the correct(tm) syntax result in identical statements',
454 ) || diag "Search terms:\n" . Dumper {
455 backcompat => $case->{backcompat},
456 correct => $case->{correct},
460 ok (@w, 'Warnings were emitted about a mod_N construct');
464 push @non_match, $_ if ($_ !~ /$w_str/);
467 is (@non_match, 0, 'All warnings match the deprecation message')
468 || diag join "\n", 'Rogue warnings:', @non_match;