5 use SQL::Abstract::Test import => [qw(is_same_sql_bind diag_where)];
12 # -nest has been undocumented on purpose, but is still supported for the
13 # foreseable future. Do not rip out the -nest tests before speaking to
14 # someone on the DBIC mailing list or in irc.perl.org#dbix-class
19 Test -and -or and -nest modifiers, assuming the following:
21 * Modifiers are respected in both hashrefs and arrayrefs (with the obvious
22 limitation of one modifier type per hahsref)
23 * When in condition context i.e. where => { -or { a = 1 } }, each modifier
24 affects only the immediate element following it.
25 * When in column multi-condition context i.e.
26 where => { x => { '!=', [-and, [qw/1 2 3/]] } }, a modifier affects the
27 OUTER ARRAYREF if and only if it is the first element of said ARRAYREF
31 # no warnings (the -or/-and => { } warning is silly, there is nothing wrong with such usage)
33 and => { stmt => 'WHERE a = ? AND b = ?', bind => [qw/1 2/] },
34 or => { stmt => 'WHERE a = ? OR b = ?', bind => [qw/1 2/] },
35 or_and => { stmt => 'WHERE ( foo = ? OR bar = ? ) AND baz = ? ', bind => [qw/1 2 3/] },
36 or_or => { stmt => 'WHERE foo = ? OR bar = ? OR baz = ?', bind => [qw/1 2 3/] },
37 and_or => { stmt => 'WHERE ( foo = ? AND bar = ? ) OR baz = ?', bind => [qw/1 2 3/] },
43 where => { -and => [a => 1, b => 2] },
44 %{$and_or_args->{and}},
47 where => [ -and => [a => 1, b => 2] ],
48 %{$and_or_args->{and}},
51 where => { -or => [a => 1, b => 2] },
52 %{$and_or_args->{or}},
55 where => [ -or => [a => 1, b => 2] ],
56 %{$and_or_args->{or}},
60 where => { -and => {a => 1, b => 2} },
61 %{$and_or_args->{and}},
64 where => [ -and => {a => 1, b => 2} ],
65 %{$and_or_args->{and}},
68 where => { -or => {a => 1, b => 2} },
69 %{$and_or_args->{or}},
72 where => [ -or => {a => 1, b => 2} ],
73 %{$and_or_args->{or}},
76 # test modifiers within hashrefs
79 [ foo => 1, bar => 2 ],
82 %{$and_or_args->{or_or}},
86 [ foo => 1, bar => 2 ],
89 %{$and_or_args->{or_and}},
92 # test modifiers within arrayrefs
95 [ foo => 1, bar => 2 ],
98 %{$and_or_args->{or_or}},
102 [ foo => 1, bar => 2 ],
105 %{$and_or_args->{or_and}},
108 # test ambiguous modifiers within hashrefs (op extends to to immediate RHS only)
110 where => { -and => [ -or =>
111 [ foo => 1, bar => 2 ],
114 %{$and_or_args->{or_and}},
117 where => { -or => [ -and =>
118 [ foo => 1, bar => 2 ],
121 %{$and_or_args->{and_or}},
124 # test ambiguous modifiers within arrayrefs (op extends to to immediate RHS only)
126 where => [ -and => [ -or =>
127 [ foo => 1, bar => 2 ],
130 %{$and_or_args->{or_and}},
133 where => [ -or => [ -and =>
134 [ foo => 1, bar => 2 ],
137 %{$and_or_args->{and_or}},
140 # test column multi-cond in arrayref (useless example)
142 where => { x => [ -and => (1 .. 3) ] },
143 stmt => 'WHERE x = ? AND x = ? AND x = ?',
146 # test column multi-cond in arrayref (more useful)
148 where => { x => [ -and => {'!=' => 1}, {'!=' => 2}, {'!=' => 3} ] },
149 stmt => 'WHERE x != ? AND x != ? AND x != ?',
152 # test column multi-cond in arrayref (even more useful)
154 where => { x => { '!=' => [ -and => (1 .. 3) ] } },
155 stmt => 'WHERE x != ? AND x != ? AND x != ?',
159 # the -or should affect only the inner hashref, as we are not in an outer arrayref
162 -or => { '!=', 1, '>=', 2 }, -like => 'x%'
164 stmt => 'WHERE x LIKE ? AND ( x != ? OR x >= ? )',
165 bind => [qw/x% 1 2/],
168 # the -and should affect the OUTER arrayref, while the internal structures remain intact
171 -and => [ 1, 2 ], { -like => 'x%' }
173 stmt => 'WHERE (x = ? OR x = ?) AND x LIKE ?',
174 bind => [qw/1 2 x%/],
178 where => { -and => [a => 1, b => 2], x => 9, -or => { c => 3, d => 4 } },
179 stmt => 'WHERE a = ? AND b = ? AND ( c = ? OR d = ? ) AND x = ?',
180 bind => [qw/1 2 3 4 9/],
184 where => { -and => [a => 1, b => 2, k => [11, 12] ], x => 9, -or => { c => 3, d => 4, l => { '=' => [21, 22] } } },
185 stmt => 'WHERE a = ? AND b = ? AND (k = ? OR k = ?) AND (c = ? OR d = ? OR (l = ? OR l = ?) ) AND x = ?',
186 bind => [qw/1 2 11 12 3 4 21 22 9/],
190 where => { -or => [a => 1, b => 2, k => [11, 12] ], x => 9, -and => { c => 3, d => 4, l => { '=' => [21, 22] } } },
191 stmt => 'WHERE c = ? AND d = ? AND ( l = ? OR l = ?) AND (a = ? OR b = ? OR k = ? OR k = ?) AND x = ?',
192 bind => [qw/3 4 21 22 1 2 11 12 9/],
196 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 }],
197 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 = ?)',
202 # explicit OR logic in arrays should leave everything intact
203 args => { logic => 'or' },
204 where => { -and => [a => 1, b => 2, k => [11, 12] ], x => 9, -or => { c => 3, d => 4, l => { '=' => [21, 22] } } },
205 stmt => 'WHERE a = ? AND b = ? AND (k = ? OR k = ?) AND ( c = ? OR d = ? OR l = ? OR l = ? ) AND x = ? ',
206 bind => [qw/1 2 11 12 3 4 21 22 9/],
210 # flip logic in arrays except where excplicitly requested otherwise
211 args => { logic => 'and' },
212 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 }],
213 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 = ?',
217 # 1st -and is in column mode, thus flips the entire array, whereas the
218 # 2nd one is just a condition modifier
221 col => [ -and => {'<' => 123}, {'>' => 456 }, {'!=' => 789} ],
223 col2 => [ -or => { -like => 'crap' }, { -like => 'crop' } ],
224 col3 => [ -and => { -like => 'chap' }, { -like => 'chop' } ],
228 (col < ? AND col > ? AND col != ?)
231 ( col2 LIKE ? OR col2 LIKE ? )
233 ( col3 LIKE ? AND col3 LIKE ? )
236 bind => [qw/123 456 789 crap crop chap chop/],
240 # some corner cases by ldami (some produce useless SQL, just for clarification on 1.5 direction)
245 -and => [ { -like => 'foo%'}, {'>' => 'moo'} ],
246 { -like => '%bar', '<' => 'baz'},
247 [ {-like => '%alpha'}, {-like => '%beta'} ],
248 [ {'!=' => 'toto', '=' => 'koko'} ],
250 stmt => 'WHERE (foo LIKE ? OR foo > ?) AND (foo LIKE ? AND foo < ?) AND (foo LIKE ? OR foo LIKE ?) AND (foo != ? AND foo = ?)',
251 bind => [qw/foo% moo %bar baz %alpha %beta toto koko/],
256 -and => [a => 1, b => 2],
257 -or => [c => 3, d => 4],
258 e => [-and => {-like => 'foo%'}, {-like => '%bar'} ],
260 stmt => 'WHERE (a = ? AND b = ?) OR c = ? OR d = ? OR (e LIKE ? AND e LIKE ?)',
261 bind => [qw/1 2 3 4 foo% %bar/],
264 # -or has nothing to flip
266 where => [-and => [{foo => 1}, {bar => 2}, -or => {baz => 3}] ],
267 stmt => 'WHERE foo = ? AND bar = ? AND baz = ?',
271 where => [-and => [{foo => 1}, {bar => 2}, -or => {baz => 3, woz => 4} ] ],
272 stmt => 'WHERE foo = ? AND bar = ? AND (baz = ? OR woz = ?)',
276 # -and has only 1 following element, thus all still ORed
278 where => { col => [ -and => [{'<' => 123}, {'>' => 456 }, {'!=' => 789}] ] },
279 stmt => 'WHERE col < ? OR col > ? OR col != ?',
280 bind => [qw/123 456 789/],
283 # flipping array logic affects both column value and condition arrays
285 args => { logic => 'and' },
286 where => [ col => [ {'<' => 123}, {'>' => 456 }, {'!=' => 789} ], col2 => 0 ],
287 stmt => 'WHERE col < ? AND col > ? AND col != ? AND col2 = ?',
288 bind => [qw/123 456 789 0/],
291 # flipping array logic with explicit -and works
293 args => { logic => 'and' },
294 where => [ col => [ -and => {'<' => 123}, {'>' => 456 }, {'!=' => 789} ], col2 => 0 ],
295 stmt => 'WHERE col < ? AND col > ? AND col != ? AND col2 = ?',
296 bind => [qw/123 456 789 0/],
298 # flipping array logic with explicit -or flipping it back
300 args => { logic => 'and' },
301 where => [ col => [ -or => {'<' => 123}, {'>' => 456 }, {'!=' => 789} ], col2 => 0 ],
302 stmt => 'WHERE (col < ? OR col > ? OR col != ?) AND col2 = ?',
303 bind => [qw/123 456 789 0/],
307 # modN and mod_N were a bad design decision - they go away in SQLA2, warn now
308 my @numbered_mods = (
311 -and => [a => 10, b => 11],
312 -and2 => [ c => 20, d => 21 ],
314 -nest2 => [ y => 2 ],
315 -or => { m => 7, n => 8 },
316 -or2 => { m => 17, n => 18 },
318 correct => { -and => [
319 -and => [a => 10, b => 11],
320 -and => [ c => 20, d => 21 ],
323 -or => { m => 7, n => 8 },
324 -or => { m => 17, n => 18 },
329 -and2 => [a => 10, b => 11],
330 -and_3 => [ c => 20, d => 21 ],
331 -nest2 => [ x => 1 ],
332 -nest_3 => [ y => 2 ],
333 -or2 => { m => 7, n => 8 },
334 -or_3 => { m => 17, n => 18 },
336 correct => [ -and => [
337 -and => [a => 10, b => 11],
338 -and => [ c => 20, d => 21 ],
341 -or => { m => 7, n => 8 },
342 -or => { m => 17, n => 18 },
349 where => {a => 1, -nest => [b => 2, c => 3]},
350 stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
354 where => {a => 1, -nest => {b => 2, c => 3}},
355 stmt => 'WHERE ( ( (b = ? AND c = ?) AND a = ? ) )',
359 where => {a => 1, -or => {-nest => {b => 2, c => 3}}},
360 stmt => 'WHERE ( ( (b = ? AND c = ?) AND a = ? ) )',
364 where => {a => 1, -or => {-nest => [b => 2, c => 3]}},
365 stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
369 where => {a => 1, -nest => {-or => {b => 2, c => 3}}},
370 stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
374 where => [a => 1, -nest => {b => 2, c => 3}, -nest => [d => 4, e => 5]],
375 stmt => 'WHERE ( ( a = ? OR ( b = ? AND c = ? ) OR ( d = ? OR e = ? ) ) )',
376 bind => [qw/1 2 3 4 5/],
380 for my $case (@and_or_tests) {
382 local $TODO = $case->{todo} if $case->{todo};
385 local $SIG{__WARN__} = sub { push @w, @_ };
387 my $sql = SQL::Abstract->new ($case->{args} || {});
389 my $where_copy = dclone($case->{where});
392 my ($stmt, @bind) = $sql->where($case->{where});
398 ) || diag_where( $case->{where} );
400 is (@w, 0, 'No warnings within and-or tests')
401 || diag join "\n", 'Emitted warnings:', @w;
403 is_deeply ($case->{where}, $where_copy, 'Where conditions unchanged');
407 for my $case (@nest_tests) {
409 local $TODO = $case->{todo} if $case->{todo};
411 local $SQL::Abstract::Test::parenthesis_significant = 1;
413 my $sql = SQL::Abstract->new ($case->{args} || {});
415 my ($stmt, @bind) = $sql->where($case->{where});
421 ) || diag_where ( $case->{where} );
428 my $w_str = "\QUse of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0\E";
429 for my $case (@numbered_mods) {
431 local $TODO = $case->{todo} if $case->{todo};
434 local $SIG{__WARN__} = sub { push @w, @_ };
435 my $sql = SQL::Abstract->new ($case->{args} || {});
437 my ($old_s, @old_b) = $sql->where($case->{backcompat});
438 my ($new_s, @new_b) = $sql->where($case->{correct});
442 'Backcompat and the correct(tm) syntax result in identical statements',
444 backcompat => $case->{backcompat},
445 correct => $case->{correct},
449 ok (@w, 'Warnings were emitted about a mod_N construct');
453 push @non_match, $_ if ($_ !~ /$w_str/);
456 is (@non_match, 0, 'All warnings match the deprecation message')
457 || diag join "\n", 'Rogue warnings:', @non_match;