6 use SQL::Abstract::Test import => [qw(is_same_sql_bind diag_where)];
13 # -nest has been undocumented on purpose, but is still supported for the
14 # foreseable future. Do not rip out the -nest tests before speaking to
15 # someone on the DBIC mailing list or in irc.perl.org#dbix-class
20 Test -and -or and -nest modifiers, assuming the following:
22 * Modifiers are respected in both hashrefs and arrayrefs (with the obvious
23 limitation of one modifier type per hahsref)
24 * When in condition context i.e. where => { -or => { a = 1 } }, each modifier
25 affects only the immediate element following it.
26 * When in column multi-condition context i.e.
27 where => { x => { '!=', [-and => [qw/1 2 3/]] } }, a modifier affects the
28 OUTER ARRAYREF if and only if it is the first element of said ARRAYREF
32 # no warnings (the -or/-and => { } warning is silly, there is nothing wrong with such usage)
34 and => { stmt => 'WHERE a = ? AND b = ?', bind => [qw/1 2/] },
35 or => { stmt => 'WHERE a = ? OR b = ?', bind => [qw/1 2/] },
36 or_and => { stmt => 'WHERE ( foo = ? OR bar = ? ) AND baz = ? ', bind => [qw/1 2 3/] },
37 or_or => { stmt => 'WHERE foo = ? OR bar = ? OR baz = ?', bind => [qw/1 2 3/] },
38 and_or => { stmt => 'WHERE ( foo = ? AND bar = ? ) OR baz = ?', bind => [qw/1 2 3/] },
44 where => { -and => [a => 1, b => 2] },
45 %{$and_or_args->{and}},
48 where => [ -and => [a => 1, b => 2] ],
49 %{$and_or_args->{and}},
52 where => { -or => [a => 1, b => 2] },
53 %{$and_or_args->{or}},
56 where => [ -or => [a => 1, b => 2] ],
57 %{$and_or_args->{or}},
61 where => { -and => {a => 1, b => 2} },
62 %{$and_or_args->{and}},
65 where => [ -and => {a => 1, b => 2} ],
66 %{$and_or_args->{and}},
69 where => { -or => {a => 1, b => 2} },
70 %{$and_or_args->{or}},
73 where => [ -or => {a => 1, b => 2} ],
74 %{$and_or_args->{or}},
77 # test modifiers within hashrefs
80 [ foo => 1, bar => 2 ],
83 %{$and_or_args->{or_or}},
87 [ foo => 1, bar => 2 ],
90 %{$and_or_args->{or_and}},
93 # test modifiers within arrayrefs
96 [ foo => 1, bar => 2 ],
99 %{$and_or_args->{or_or}},
103 [ foo => 1, bar => 2 ],
106 %{$and_or_args->{or_and}},
109 # test ambiguous modifiers within hashrefs (op extends to to immediate RHS only)
111 where => { -and => [ -or =>
112 [ foo => 1, bar => 2 ],
115 %{$and_or_args->{or_and}},
118 where => { -or => [ -and =>
119 [ foo => 1, bar => 2 ],
122 %{$and_or_args->{and_or}},
125 # test ambiguous modifiers within arrayrefs (op extends to to immediate RHS only)
127 where => [ -and => [ -or =>
128 [ foo => 1, bar => 2 ],
131 %{$and_or_args->{or_and}},
134 where => [ -or => [ -and =>
135 [ foo => 1, bar => 2 ],
138 %{$and_or_args->{and_or}},
141 # test column multi-cond in arrayref (useless example)
143 where => { x => [ -and => (1 .. 3) ] },
144 stmt => 'WHERE x = ? AND x = ? AND x = ?',
147 # test column multi-cond in arrayref (more useful)
149 where => { x => [ -and => {'!=' => 1}, {'!=' => 2}, {'!=' => 3} ] },
150 stmt => 'WHERE x != ? AND x != ? AND x != ?',
153 # test column multi-cond in arrayref (even more useful)
155 where => { x => { '!=' => [ -and => (1 .. 3) ] } },
156 stmt => 'WHERE x != ? AND x != ? AND x != ?',
160 # the -or should affect only the inner hashref, as we are not in an outer arrayref
163 -or => { '!=', 1, '>=', 2 }, -like => 'x%'
165 stmt => 'WHERE x LIKE ? AND ( x != ? OR x >= ? )',
166 bind => [qw/x% 1 2/],
169 # the -and should affect the OUTER arrayref, while the internal structures remain intact
172 -and => [ 1, 2 ], { -like => 'x%' }
174 stmt => 'WHERE (x = ? OR x = ?) AND x LIKE ?',
175 bind => [qw/1 2 x%/],
179 where => { -and => [a => 1, b => 2], x => 9, -or => { c => 3, d => 4 } },
180 stmt => 'WHERE a = ? AND b = ? AND ( c = ? OR d = ? ) AND x = ?',
181 bind => [qw/1 2 3 4 9/],
185 where => { -and => [a => 1, b => 2, k => [11, 12] ], x => 9, -or => { c => 3, d => 4, l => { '=' => [21, 22] } } },
186 stmt => 'WHERE a = ? AND b = ? AND (k = ? OR k = ?) AND (c = ? OR d = ? OR (l = ? OR l = ?) ) AND x = ?',
187 bind => [qw/1 2 11 12 3 4 21 22 9/],
191 where => { -or => [a => 1, b => 2, k => [11, 12] ], x => 9, -and => { c => 3, d => 4, l => { '=' => [21, 22] } } },
192 stmt => 'WHERE c = ? AND d = ? AND ( l = ? OR l = ?) AND (a = ? OR b = ? OR k = ? OR k = ?) AND x = ?',
193 bind => [qw/3 4 21 22 1 2 11 12 9/],
197 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 }],
198 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 = ?)',
203 # explicit OR logic in arrays should leave everything intact
204 args => { logic => 'or' },
205 where => { -and => [a => 1, b => 2, k => [11, 12] ], x => 9, -or => { c => 3, d => 4, l => { '=' => [21, 22] } } },
206 stmt => 'WHERE a = ? AND b = ? AND (k = ? OR k = ?) AND ( c = ? OR d = ? OR l = ? OR l = ? ) AND x = ? ',
207 bind => [qw/1 2 11 12 3 4 21 22 9/],
211 # flip logic in arrays except where excplicitly requested otherwise
212 args => { logic => 'and' },
213 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 }],
214 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 = ?',
218 # 1st -and is in column mode, thus flips the entire array, whereas the
219 # 2nd one is just a condition modifier
222 col => [ -and => {'<' => 123}, {'>' => 456 }, {'!=' => 789} ],
224 col2 => [ -or => { -like => 'crap' }, { -like => 'crop' } ],
225 col3 => [ -and => { -like => 'chap' }, { -like => 'chop' } ],
229 (col < ? AND col > ? AND col != ?)
232 ( col2 LIKE ? OR col2 LIKE ? )
234 ( col3 LIKE ? AND col3 LIKE ? )
237 bind => [qw/123 456 789 crap crop chap chop/],
241 # some corner cases by ldami (some produce useless SQL, just for clarification on 1.5 direction)
246 -and => [ { -like => 'foo%'}, {'>' => 'moo'} ],
247 { -like => '%bar', '<' => 'baz'},
248 [ {-like => '%alpha'}, {-like => '%beta'} ],
249 [ {'!=' => 'toto', '=' => 'koko'} ],
251 stmt => 'WHERE (foo LIKE ? OR foo > ?) AND (foo LIKE ? AND foo < ?) AND (foo LIKE ? OR foo LIKE ?) AND (foo != ? AND foo = ?)',
252 bind => [qw/foo% moo %bar baz %alpha %beta toto koko/],
257 -and => [a => 1, b => 2],
258 -or => [c => 3, d => 4],
259 e => [-and => {-like => 'foo%'}, {-like => '%bar'} ],
261 stmt => 'WHERE (a = ? AND b = ?) OR c = ? OR d = ? OR (e LIKE ? AND e LIKE ?)',
262 bind => [qw/1 2 3 4 foo% %bar/],
265 # -or has nothing to flip
267 where => [-and => [{foo => 1}, {bar => 2}, -or => {baz => 3}] ],
268 stmt => 'WHERE foo = ? AND bar = ? AND baz = ?',
272 where => [-and => [{foo => 1}, {bar => 2}, -or => {baz => 3, woz => 4} ] ],
273 stmt => 'WHERE foo = ? AND bar = ? AND (baz = ? OR woz = ?)',
277 # -and has only 1 following element, thus all still ORed
279 where => { col => [ -and => [{'<' => 123}, {'>' => 456 }, {'!=' => 789}] ] },
280 stmt => 'WHERE col < ? OR col > ? OR col != ?',
281 bind => [qw/123 456 789/],
284 # flipping array logic affects both column value and condition arrays
286 args => { logic => 'and' },
287 where => [ col => [ {'<' => 123}, {'>' => 456 }, {'!=' => 789} ], col2 => 0 ],
288 stmt => 'WHERE col < ? AND col > ? AND col != ? AND col2 = ?',
289 bind => [qw/123 456 789 0/],
292 # flipping array logic with explicit -and works
294 args => { logic => 'and' },
295 where => [ col => [ -and => {'<' => 123}, {'>' => 456 }, {'!=' => 789} ], col2 => 0 ],
296 stmt => 'WHERE col < ? AND col > ? AND col != ? AND col2 = ?',
297 bind => [qw/123 456 789 0/],
299 # flipping array logic with explicit -or flipping it back
301 args => { logic => 'and' },
302 where => [ col => [ -or => {'<' => 123}, {'>' => 456 }, {'!=' => 789} ], col2 => 0 ],
303 stmt => 'WHERE (col < ? OR col > ? OR col != ?) AND col2 = ?',
304 bind => [qw/123 456 789 0/],
308 # modN and mod_N were a bad design decision - they go away in SQLA2, warn now
309 my @numbered_mods = (
312 -and => [a => 10, b => 11],
313 -and2 => [ c => 20, d => 21 ],
315 -nest2 => [ y => 2 ],
316 -or => { m => 7, n => 8 },
317 -or2 => { m => 17, n => 18 },
319 correct => { -and => [
320 -and => [a => 10, b => 11],
321 -and => [ c => 20, d => 21 ],
324 -or => { m => 7, n => 8 },
325 -or => { m => 17, n => 18 },
330 -and2 => [a => 10, b => 11],
331 -and_3 => [ c => 20, d => 21 ],
332 -nest2 => [ x => 1 ],
333 -nest_3 => [ y => 2 ],
334 -or2 => { m => 7, n => 8 },
335 -or_3 => { m => 17, n => 18 },
337 correct => [ -and => [
338 -and => [a => 10, b => 11],
339 -and => [ c => 20, d => 21 ],
342 -or => { m => 7, n => 8 },
343 -or => { m => 17, n => 18 },
350 where => {a => 1, -nest => [b => 2, c => 3]},
351 stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
355 where => {a => 1, -nest => {b => 2, c => 3}},
356 stmt => 'WHERE ( ( (b = ? AND c = ?) AND a = ? ) )',
360 where => {a => 1, -or => {-nest => {b => 2, c => 3}}},
361 stmt => 'WHERE ( ( (b = ? AND c = ?) AND a = ? ) )',
365 where => {a => 1, -or => {-nest => [b => 2, c => 3]}},
366 stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
370 where => {a => 1, -nest => {-or => {b => 2, c => 3}}},
371 stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
375 where => [a => 1, -nest => {b => 2, c => 3}, -nest => [d => 4, e => 5]],
376 stmt => 'WHERE ( ( a = ? OR ( b = ? AND c = ? ) OR ( d = ? OR e = ? ) ) )',
377 bind => [qw/1 2 3 4 5/],
381 for my $case (@and_or_tests) {
383 local $TODO = $case->{todo} if $case->{todo};
385 my $sql = SQL::Abstract->new ($case->{args} || {});
387 my $where_copy = dclone($case->{where});
390 my ($stmt, @bind) = $sql->where($case->{where});
396 ) || diag_where( $case->{where} );
397 } [], 'No warnings within and-or tests';
399 is_deeply ($case->{where}, $where_copy, 'Where conditions unchanged');
403 for my $case (@nest_tests) {
405 local $TODO = $case->{todo} if $case->{todo};
407 local $SQL::Abstract::Test::parenthesis_significant = 1;
409 my $sql = SQL::Abstract->new ($case->{args} || {});
411 my ($stmt, @bind) = $sql->where($case->{where});
417 ) || diag_where ( $case->{where} );
422 for my $case (@numbered_mods) {
424 local $TODO = $case->{todo} if $case->{todo};
426 # not using Test::Warn here - variable amount of warnings
428 local $SIG{__WARN__} = sub { push @w, @_ };
430 my $sql = SQL::Abstract->new ($case->{args} || {});
432 my ($old_s, @old_b) = $sql->where($case->{backcompat});
433 my ($new_s, @new_b) = $sql->where($case->{correct});
437 'Backcompat and the correct(tm) syntax result in identical statements',
439 backcompat => $case->{backcompat},
440 correct => $case->{correct},
445 { $_ =~ qr/\QUse of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0/ }
447 ), 'Warnings were emitted about a mod_N construct');