7 use SQL::Abstract::Test import => [qw(is_same_sql_bind diag_where)];
10 use Storable 'dclone';
14 # -nest has been undocumented on purpose, but is still supported for the
15 # foreseable future. Do not rip out the -nest tests before speaking to
16 # someone on the DBIC mailing list or in irc.perl.org#dbix-class
21 Test -and -or and -nest modifiers, assuming the following:
23 * Modifiers are respected in both hashrefs and arrayrefs (with the obvious
24 limitation of one modifier type per hahsref)
25 * When in condition context i.e. where => { -or { a = 1 } }, each modifier
26 affects only the immediate element following it.
27 * When in column multi-condition context i.e.
28 where => { x => { '!=', [-and, [qw/1 2 3/]] } }, a modifier affects the
29 OUTER ARRAYREF if and only if it is the first element of said ARRAYREF
33 # no warnings (the -or/-and => { } warning is silly, there is nothing wrong with such usage)
35 and => { stmt => 'WHERE a = ? AND b = ?', bind => [qw/1 2/] },
36 or => { stmt => 'WHERE a = ? OR b = ?', bind => [qw/1 2/] },
37 or_and => { stmt => 'WHERE ( foo = ? OR bar = ? ) AND baz = ? ', bind => [qw/1 2 3/] },
38 or_or => { stmt => 'WHERE foo = ? OR bar = ? OR baz = ?', bind => [qw/1 2 3/] },
39 and_or => { stmt => 'WHERE ( foo = ? AND bar = ? ) OR baz = ?', bind => [qw/1 2 3/] },
45 where => { -and => [a => 1, b => 2] },
46 %{$and_or_args->{and}},
49 where => [ -and => [a => 1, b => 2] ],
50 %{$and_or_args->{and}},
53 where => { -or => [a => 1, b => 2] },
54 %{$and_or_args->{or}},
57 where => [ -or => [a => 1, b => 2] ],
58 %{$and_or_args->{or}},
62 where => { -and => {a => 1, b => 2} },
63 %{$and_or_args->{and}},
66 where => [ -and => {a => 1, b => 2} ],
67 %{$and_or_args->{and}},
70 where => { -or => {a => 1, b => 2} },
71 %{$and_or_args->{or}},
74 where => [ -or => {a => 1, b => 2} ],
75 %{$and_or_args->{or}},
78 # test modifiers within hashrefs
81 [ foo => 1, bar => 2 ],
84 %{$and_or_args->{or_or}},
88 [ foo => 1, bar => 2 ],
91 %{$and_or_args->{or_and}},
94 # test modifiers within arrayrefs
97 [ foo => 1, bar => 2 ],
100 %{$and_or_args->{or_or}},
104 [ foo => 1, bar => 2 ],
107 %{$and_or_args->{or_and}},
110 # test ambiguous modifiers within hashrefs (op extends to to immediate RHS only)
112 where => { -and => [ -or =>
113 [ foo => 1, bar => 2 ],
116 %{$and_or_args->{or_and}},
119 where => { -or => [ -and =>
120 [ foo => 1, bar => 2 ],
123 %{$and_or_args->{and_or}},
126 # test ambiguous modifiers within arrayrefs (op extends to to immediate RHS only)
128 where => [ -and => [ -or =>
129 [ foo => 1, bar => 2 ],
132 %{$and_or_args->{or_and}},
135 where => [ -or => [ -and =>
136 [ foo => 1, bar => 2 ],
139 %{$and_or_args->{and_or}},
142 # test column multi-cond in arrayref (useless example)
144 where => { x => [ -and => (1 .. 3) ] },
145 stmt => 'WHERE x = ? AND x = ? AND x = ?',
148 # test column multi-cond in arrayref (more useful)
150 where => { x => [ -and => {'!=' => 1}, {'!=' => 2}, {'!=' => 3} ] },
151 stmt => 'WHERE x != ? AND x != ? AND x != ?',
154 # test column multi-cond in arrayref (even more useful)
156 where => { x => { '!=' => [ -and => (1 .. 3) ] } },
157 stmt => 'WHERE x != ? AND x != ? AND x != ?',
161 # the -or should affect only the inner hashref, as we are not in an outer arrayref
164 -or => { '!=', 1, '>=', 2 }, -like => 'x%'
166 stmt => 'WHERE x LIKE ? AND ( x != ? OR x >= ? )',
167 bind => [qw/x% 1 2/],
170 # the -and should affect the OUTER arrayref, while the internal structures remain intact
173 -and => [ 1, 2 ], { -like => 'x%' }
175 stmt => 'WHERE (x = ? OR x = ?) AND x LIKE ?',
176 bind => [qw/1 2 x%/],
180 where => { -and => [a => 1, b => 2], x => 9, -or => { c => 3, d => 4 } },
181 stmt => 'WHERE a = ? AND b = ? AND ( c = ? OR d = ? ) AND x = ?',
182 bind => [qw/1 2 3 4 9/],
186 where => { -and => [a => 1, b => 2, k => [11, 12] ], x => 9, -or => { c => 3, d => 4, l => { '=' => [21, 22] } } },
187 stmt => 'WHERE a = ? AND b = ? AND (k = ? OR k = ?) AND (c = ? OR d = ? OR (l = ? OR l = ?) ) AND x = ?',
188 bind => [qw/1 2 11 12 3 4 21 22 9/],
192 where => { -or => [a => 1, b => 2, k => [11, 12] ], x => 9, -and => { c => 3, d => 4, l => { '=' => [21, 22] } } },
193 stmt => 'WHERE c = ? AND d = ? AND ( l = ? OR l = ?) AND (a = ? OR b = ? OR k = ? OR k = ?) AND x = ?',
194 bind => [qw/3 4 21 22 1 2 11 12 9/],
198 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 }],
199 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 = ?)',
204 # explicit OR logic in arrays should leave everything intact
205 args => { logic => 'or' },
206 where => { -and => [a => 1, b => 2, k => [11, 12] ], x => 9, -or => { c => 3, d => 4, l => { '=' => [21, 22] } } },
207 stmt => 'WHERE a = ? AND b = ? AND (k = ? OR k = ?) AND ( c = ? OR d = ? OR l = ? OR l = ? ) AND x = ? ',
208 bind => [qw/1 2 11 12 3 4 21 22 9/],
212 # flip logic in arrays except where excplicitly requested otherwise
213 args => { logic => 'and' },
214 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 }],
215 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 = ?',
219 # 1st -and is in column mode, thus flips the entire array, whereas the
220 # 2nd one is just a condition modifier
223 col => [ -and => {'<' => 123}, {'>' => 456 }, {'!=' => 789} ],
225 col2 => [ -or => { -like => 'crap' }, { -like => 'crop' } ],
226 col3 => [ -and => { -like => 'chap' }, { -like => 'chop' } ],
230 (col < ? AND col > ? AND col != ?)
233 ( col2 LIKE ? OR col2 LIKE ? )
235 ( col3 LIKE ? AND col3 LIKE ? )
238 bind => [qw/123 456 789 crap crop chap chop/],
242 # some corner cases by ldami (some produce useless SQL, just for clarification on 1.5 direction)
247 -and => [ { -like => 'foo%'}, {'>' => 'moo'} ],
248 { -like => '%bar', '<' => 'baz'},
249 [ {-like => '%alpha'}, {-like => '%beta'} ],
250 [ {'!=' => 'toto', '=' => 'koko'} ],
252 stmt => 'WHERE (foo LIKE ? OR foo > ?) AND (foo LIKE ? AND foo < ?) AND (foo LIKE ? OR foo LIKE ?) AND (foo != ? AND foo = ?)',
253 bind => [qw/foo% moo %bar baz %alpha %beta toto koko/],
258 -and => [a => 1, b => 2],
259 -or => [c => 3, d => 4],
260 e => [-and => {-like => 'foo%'}, {-like => '%bar'} ],
262 stmt => 'WHERE (a = ? AND b = ?) OR c = ? OR d = ? OR (e LIKE ? AND e LIKE ?)',
263 bind => [qw/1 2 3 4 foo% %bar/],
266 # -or has nothing to flip
268 where => [-and => [{foo => 1}, {bar => 2}, -or => {baz => 3}] ],
269 stmt => 'WHERE foo = ? AND bar = ? AND baz = ?',
273 where => [-and => [{foo => 1}, {bar => 2}, -or => {baz => 3, woz => 4} ] ],
274 stmt => 'WHERE foo = ? AND bar = ? AND (baz = ? OR woz = ?)',
278 # -and has only 1 following element, thus all still ORed
280 where => { col => [ -and => [{'<' => 123}, {'>' => 456 }, {'!=' => 789}] ] },
281 stmt => 'WHERE col < ? OR col > ? OR col != ?',
282 bind => [qw/123 456 789/],
285 # flipping array logic affects both column value and condition arrays
287 args => { logic => 'and' },
288 where => [ col => [ {'<' => 123}, {'>' => 456 }, {'!=' => 789} ], col2 => 0 ],
289 stmt => 'WHERE col < ? AND col > ? AND col != ? AND col2 = ?',
290 bind => [qw/123 456 789 0/],
293 # flipping array logic with explicit -and works
295 args => { logic => 'and' },
296 where => [ col => [ -and => {'<' => 123}, {'>' => 456 }, {'!=' => 789} ], col2 => 0 ],
297 stmt => 'WHERE col < ? AND col > ? AND col != ? AND col2 = ?',
298 bind => [qw/123 456 789 0/],
300 # flipping array logic with explicit -or flipping it back
302 args => { logic => 'and' },
303 where => [ col => [ -or => {'<' => 123}, {'>' => 456 }, {'!=' => 789} ], col2 => 0 ],
304 stmt => 'WHERE (col < ? OR col > ? OR col != ?) AND col2 = ?',
305 bind => [qw/123 456 789 0/],
309 # modN and mod_N were a bad design decision - they go away in SQLA2, warn now
310 my @numbered_mods = (
313 -and => [a => 10, b => 11],
314 -and2 => [ c => 20, d => 21 ],
316 -nest2 => [ y => 2 ],
317 -or => { m => 7, n => 8 },
318 -or2 => { m => 17, n => 18 },
320 correct => { -and => [
321 -and => [a => 10, b => 11],
322 -and => [ c => 20, d => 21 ],
325 -or => { m => 7, n => 8 },
326 -or => { m => 17, n => 18 },
331 -and2 => [a => 10, b => 11],
332 -and_3 => [ c => 20, d => 21 ],
333 -nest2 => [ x => 1 ],
334 -nest_3 => [ y => 2 ],
335 -or2 => { m => 7, n => 8 },
336 -or_3 => { m => 17, n => 18 },
338 correct => [ -and => [
339 -and => [a => 10, b => 11],
340 -and => [ c => 20, d => 21 ],
343 -or => { m => 7, n => 8 },
344 -or => { m => 17, n => 18 },
351 where => {a => 1, -nest => [b => 2, c => 3]},
352 stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
356 where => {a => 1, -nest => {b => 2, c => 3}},
357 stmt => 'WHERE ( ( (b = ? AND c = ?) AND a = ? ) )',
361 where => {a => 1, -or => {-nest => {b => 2, c => 3}}},
362 stmt => 'WHERE ( ( (b = ? AND c = ?) AND a = ? ) )',
366 where => {a => 1, -or => {-nest => [b => 2, c => 3]}},
367 stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
371 where => {a => 1, -nest => {-or => {b => 2, c => 3}}},
372 stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
376 where => [a => 1, -nest => {b => 2, c => 3}, -nest => [d => 4, e => 5]],
377 stmt => 'WHERE ( ( a = ? OR ( b = ? AND c = ? ) OR ( d = ? OR e = ? ) ) )',
378 bind => [qw/1 2 3 4 5/],
382 for my $case (@and_or_tests) {
384 local $TODO = $case->{todo} if $case->{todo};
387 local $SIG{__WARN__} = sub { push @w, @_ };
389 my $sql = SQL::Abstract->new ($case->{args} || {});
391 my $where_copy = dclone($case->{where});
394 my ($stmt, @bind) = $sql->where($case->{where});
400 ) || diag_where( $case->{where} );
402 is (@w, 0, 'No warnings within and-or tests')
403 || diag join "\n", 'Emitted warnings:', @w;
405 is_deeply ($case->{where}, $where_copy, 'Where conditions unchanged');
409 for my $case (@nest_tests) {
411 local $TODO = $case->{todo} if $case->{todo};
413 local $SQL::Abstract::Test::parenthesis_significant = 1;
415 my $sql = SQL::Abstract->new ($case->{args} || {});
417 my ($stmt, @bind) = $sql->where($case->{where});
423 ) || diag_where ( $case->{where} );
430 my $w_str = "\QUse of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0\E";
431 for my $case (@numbered_mods) {
433 local $TODO = $case->{todo} if $case->{todo};
436 local $SIG{__WARN__} = sub { push @w, @_ };
437 my $sql = SQL::Abstract->new ($case->{args} || {});
439 my ($old_s, @old_b) = $sql->where($case->{backcompat});
440 my ($new_s, @new_b) = $sql->where($case->{correct});
444 'Backcompat and the correct(tm) syntax result in identical statements',
446 backcompat => $case->{backcompat},
447 correct => $case->{correct},
451 ok (@w, 'Warnings were emitted about a mod_N construct');
455 push @non_match, $_ if ($_ !~ /$w_str/);
458 is (@non_match, 0, 'All warnings match the deprecation message')
459 || diag join "\n", 'Rogue warnings:', @non_match;