7 use SQL::Abstract::Test import => ['is_same_sql_bind'];
12 my $dclone = eval { require Storable; \&Storable::dclone };
16 # -nest has been undocumented on purpose, but is still supported for the
17 # foreseable future. Do not rip out the -nest tests before speaking to
18 # someone on the DBIC mailing list or in irc.perl.org#dbix-class
23 Test -and -or and -nest modifiers, assuming the following:
25 * Modifiers are respected in both hashrefs and arrayrefs (with the obvious
26 limitation of one modifier type per hahsref)
27 * When in condition context i.e. where => { -or { a = 1 } }, each modifier
28 affects only the immediate element following it.
29 * When in column multi-condition context i.e.
30 where => { x => { '!=', [-and, [qw/1 2 3/]] } }, a modifier affects the
31 OUTER ARRAYREF if and only if it is the first element of said ARRAYREF
35 # no warnings (the -or/-and => { } warning is silly, there is nothing wrong with such usage)
37 and => { stmt => 'WHERE a = ? AND b = ?', bind => [qw/1 2/] },
38 or => { stmt => 'WHERE a = ? OR b = ?', bind => [qw/1 2/] },
39 or_and => { stmt => 'WHERE ( foo = ? OR bar = ? ) AND baz = ? ', bind => [qw/1 2 3/] },
40 or_or => { stmt => 'WHERE foo = ? OR bar = ? OR baz = ?', bind => [qw/1 2 3/] },
41 and_or => { stmt => 'WHERE ( foo = ? AND bar = ? ) OR baz = ?', bind => [qw/1 2 3/] },
47 where => { -and => [a => 1, b => 2] },
48 %{$and_or_args->{and}},
51 where => [ -and => [a => 1, b => 2] ],
52 %{$and_or_args->{and}},
55 where => { -or => [a => 1, b => 2] },
56 %{$and_or_args->{or}},
59 where => [ -or => [a => 1, b => 2] ],
60 %{$and_or_args->{or}},
64 where => { -and => {a => 1, b => 2} },
65 %{$and_or_args->{and}},
68 where => [ -and => {a => 1, b => 2} ],
69 %{$and_or_args->{and}},
72 where => { -or => {a => 1, b => 2} },
73 %{$and_or_args->{or}},
76 where => [ -or => {a => 1, b => 2} ],
77 %{$and_or_args->{or}},
80 # test modifiers within hashrefs
83 [ foo => 1, bar => 2 ],
86 %{$and_or_args->{or_or}},
90 [ foo => 1, bar => 2 ],
93 %{$and_or_args->{or_and}},
96 # test modifiers within arrayrefs
99 [ foo => 1, bar => 2 ],
102 %{$and_or_args->{or_or}},
106 [ foo => 1, bar => 2 ],
109 %{$and_or_args->{or_and}},
112 # test ambiguous modifiers within hashrefs (op extends to to immediate RHS only)
114 where => { -and => [ -or =>
115 [ foo => 1, bar => 2 ],
118 %{$and_or_args->{or_and}},
121 where => { -or => [ -and =>
122 [ foo => 1, bar => 2 ],
125 %{$and_or_args->{and_or}},
128 # test ambiguous modifiers within arrayrefs (op extends to to immediate RHS only)
130 where => [ -and => [ -or =>
131 [ foo => 1, bar => 2 ],
134 %{$and_or_args->{or_and}},
137 where => [ -or => [ -and =>
138 [ foo => 1, bar => 2 ],
141 %{$and_or_args->{and_or}},
144 # test column multi-cond in arrayref (useless example)
146 where => { x => [ -and => (1 .. 3) ] },
147 stmt => 'WHERE x = ? AND x = ? AND x = ?',
150 # test column multi-cond in arrayref (more useful)
152 where => { x => [ -and => {'!=' => 1}, {'!=' => 2}, {'!=' => 3} ] },
153 stmt => 'WHERE x != ? AND x != ? AND x != ?',
156 # test column multi-cond in arrayref (even more useful)
158 where => { x => { '!=' => [ -and => (1 .. 3) ] } },
159 stmt => 'WHERE x != ? AND x != ? AND x != ?',
163 # the -or should affect only the inner hashref, as we are not in an outer arrayref
166 -or => { '!=', 1, '>=', 2 }, -like => 'x%'
168 stmt => 'WHERE x LIKE ? AND ( x != ? OR x >= ? )',
169 bind => [qw/x% 1 2/],
172 # the -and should affect the OUTER arrayref, while the internal structures remain intact
175 -and => [ 1, 2 ], { -like => 'x%' }
177 stmt => 'WHERE (x = ? OR x = ?) AND x LIKE ?',
178 bind => [qw/1 2 x%/],
182 where => { -and => [a => 1, b => 2], x => 9, -or => { c => 3, d => 4 } },
183 stmt => 'WHERE a = ? AND b = ? AND ( c = ? OR d = ? ) AND x = ?',
184 bind => [qw/1 2 3 4 9/],
188 where => { -and => [a => 1, b => 2, k => [11, 12] ], x => 9, -or => { c => 3, d => 4, l => { '=' => [21, 22] } } },
189 stmt => 'WHERE a = ? AND b = ? AND (k = ? OR k = ?) AND (c = ? OR d = ? OR (l = ? OR l = ?) ) AND x = ?',
190 bind => [qw/1 2 11 12 3 4 21 22 9/],
194 where => { -or => [a => 1, b => 2, k => [11, 12] ], x => 9, -and => { c => 3, d => 4, l => { '=' => [21, 22] } } },
195 stmt => 'WHERE c = ? AND d = ? AND ( l = ? OR l = ?) AND (a = ? OR b = ? OR k = ? OR k = ?) AND x = ?',
196 bind => [qw/3 4 21 22 1 2 11 12 9/],
200 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 }],
201 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 = ?)',
206 # explicit OR logic in arrays should leave everything intact
207 args => { logic => 'or' },
208 where => { -and => [a => 1, b => 2, k => [11, 12] ], x => 9, -or => { c => 3, d => 4, l => { '=' => [21, 22] } } },
209 stmt => 'WHERE a = ? AND b = ? AND (k = ? OR k = ?) AND ( c = ? OR d = ? OR l = ? OR l = ? ) AND x = ? ',
210 bind => [qw/1 2 11 12 3 4 21 22 9/],
214 # flip logic in arrays except where excplicitly requested otherwise
215 args => { logic => 'and' },
216 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 }],
217 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 = ?',
221 # 1st -and is in column mode, thus flips the entire array, whereas the
222 # 2nd one is just a condition modifier
225 col => [ -and => {'<' => 123}, {'>' => 456 }, {'!=' => 789} ],
227 col2 => [ -or => { -like => 'crap' }, { -like => 'crop' } ],
228 col3 => [ -and => { -like => 'chap' }, { -like => 'chop' } ],
232 (col < ? AND col > ? AND col != ?)
235 ( col2 LIKE ? OR col2 LIKE ? )
237 ( col3 LIKE ? AND col3 LIKE ? )
240 bind => [qw/123 456 789 crap crop chap chop/],
244 # some corner cases by ldami (some produce useless SQL, just for clarification on 1.5 direction)
249 -and => [ { -like => 'foo%'}, {'>' => 'moo'} ],
250 { -like => '%bar', '<' => 'baz'},
251 [ {-like => '%alpha'}, {-like => '%beta'} ],
252 [ {'!=' => 'toto', '=' => 'koko'} ],
254 stmt => 'WHERE (foo LIKE ? OR foo > ?) AND (foo LIKE ? AND foo < ?) AND (foo LIKE ? OR foo LIKE ?) AND (foo != ? AND foo = ?)',
255 bind => [qw/foo% moo %bar baz %alpha %beta toto koko/],
260 -and => [a => 1, b => 2],
261 -or => [c => 3, d => 4],
262 e => [-and => {-like => 'foo%'}, {-like => '%bar'} ],
264 stmt => 'WHERE (a = ? AND b = ?) OR c = ? OR d = ? OR (e LIKE ? AND e LIKE ?)',
265 bind => [qw/1 2 3 4 foo% %bar/],
268 # -or has nothing to flip
270 where => [-and => [{foo => 1}, {bar => 2}, -or => {baz => 3}] ],
271 stmt => 'WHERE foo = ? AND bar = ? AND baz = ?',
275 where => [-and => [{foo => 1}, {bar => 2}, -or => {baz => 3, woz => 4} ] ],
276 stmt => 'WHERE foo = ? AND bar = ? AND (baz = ? OR woz = ?)',
280 # -and has only 1 following element, thus all still ORed
282 where => { col => [ -and => [{'<' => 123}, {'>' => 456 }, {'!=' => 789}] ] },
283 stmt => 'WHERE col < ? OR col > ? OR col != ?',
284 bind => [qw/123 456 789/],
287 # flipping array logic affects both column value and condition arrays
289 args => { logic => 'and' },
290 where => [ col => [ {'<' => 123}, {'>' => 456 }, {'!=' => 789} ], col2 => 0 ],
291 stmt => 'WHERE col < ? AND col > ? AND col != ? AND col2 = ?',
292 bind => [qw/123 456 789 0/],
295 # flipping array logic with explicit -and works
297 args => { logic => 'and' },
298 where => [ col => [ -and => {'<' => 123}, {'>' => 456 }, {'!=' => 789} ], col2 => 0 ],
299 stmt => 'WHERE col < ? AND col > ? AND col != ? AND col2 = ?',
300 bind => [qw/123 456 789 0/],
302 # flipping array logic with explicit -or flipping it back
304 args => { logic => 'and' },
305 where => [ col => [ -or => {'<' => 123}, {'>' => 456 }, {'!=' => 789} ], col2 => 0 ],
306 stmt => 'WHERE (col < ? OR col > ? OR col != ?) AND col2 = ?',
307 bind => [qw/123 456 789 0/],
311 # modN and mod_N were a bad design decision - they went away
312 my @invalid_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 },
322 -and2 => [a => 10, b => 11],
323 -and_3 => [ c => 20, d => 21 ],
324 -nest2 => [ x => 1 ],
325 -nest_3 => [ y => 2 ],
326 -or2 => { m => 7, n => 8 },
327 -or_3 => { m => 17, n => 18 },
333 where => {a => 1, -nest => [b => 2, c => 3]},
334 stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
338 where => {a => 1, -nest => {b => 2, c => 3}},
339 stmt => 'WHERE ( ( b = ? AND c = ? AND a = ? ) )',
343 where => {a => 1, -or => {-nest => {b => 2, c => 3}}},
344 stmt => 'WHERE ( ( b = ? AND c = ? AND a = ? ) )',
348 where => {a => 1, -or => {-nest => [b => 2, c => 3]}},
349 stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
353 where => {a => 1, -nest => {-or => {b => 2, c => 3}}},
354 stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
358 where => [a => 1, -nest => {b => 2, c => 3}, -nest => [d => 4, e => 5]],
359 stmt => 'WHERE ( ( a = ? OR ( b = ? AND c = ? ) OR d = ? OR e = ? ) )',
360 bind => [qw/1 2 3 4 5/],
364 -and => [a => 10, b => 11],
365 -and => [ c => 20, d => 21 ],
368 -or => { m => 7, n => 8 },
369 -or => { m => 17, n => 18 },
371 stmt => 'WHERE ( ( a = ? AND b = ? AND c = ? AND d = ? AND ( x = ? ) AND ( y = ? ) AND ( m = ? OR n = ? ) AND ( m = ? OR n = ? ) ) )',
372 bind => [ 10, 11, 20, 21, 1, 2, 7, 8, 17, 18 ],
376 -and => [a => 10, b => 11],
377 -and => [ c => 20, d => 21 ],
380 -or => { m => 7, n => 8 },
381 -or => { m => 17, n => 18 },
383 stmt => 'WHERE ( ( ( a = ? AND b = ? AND c = ? AND d = ? AND ( x = ? ) AND ( y = ? ) AND ( m = ? OR n = ? ) AND ( m = ? OR n = ? ) ) ) )',
384 bind => [ 10, 11, 20, 21, 1, 2, 7, 8, 17, 18 ],
388 for my $case (@and_or_tests) {
390 local $TODO = $case->{todo} if $case->{todo};
392 local $Data::Dumper::Terse = 1;
395 local $SIG{__WARN__} = sub { push @w, @_ };
397 my $sql = SQL::Abstract->new ($case->{args} || {});
399 my $where_copy = $dclone->($case->{where})
403 my ($stmt, @bind) = $sql->where($case->{where});
410 || diag "Search term:\n" . Dumper $case->{where};
412 is (@w, 0, 'No warnings within and-or tests')
413 || diag join "\n", 'Emitted warnings:', @w;
415 is_deeply ($case->{where}, $where_copy, 'Where conditions unchanged')
420 for my $case (@nest_tests) {
422 local $TODO = $case->{todo} if $case->{todo};
424 local $SQL::Abstract::Test::parenthesis_significant = 1;
425 local $Data::Dumper::Terse = 1;
427 my $sql = SQL::Abstract->new ($case->{args} || {});
429 my ($stmt, @bind) = $sql->where($case->{where});
436 || diag "Search term:\n" . Dumper $case->{where};
441 for my $case (@invalid_numbered_mods) {
442 my $sql = SQL::Abstract->new ($case->{args} || {});
445 }, qr/\QUse of [and|or|nest]_N modifiers is no longer supported/, 'Exception thrown on bogus syntax');