7 use SQL::Abstract::Test import => ['is_same_sql_bind'];
13 Test -and -or and -nest modifiers, assuming the following:
15 * Modifiers are respected in both hashrefs and arrayrefs (with the obvious
16 limitation of one modifier type per hahsref)
17 * When in condition context i.e. where => { -or { a = 1 } }, each modifier
18 affects only the immediate element following it.
19 * When in column multi-condition context i.e.
20 where => { x => { '!=', [-and, [qw/1 2 3/]] } }, a modifier affects the
21 OUTER ARRAYREF if and only if it is the first element of said ARRAYREF
25 # no warnings (the -or/-and => { } warning is silly, there is nothing wrong with such usage)
27 and => { stmt => 'WHERE a = ? AND b = ?', bind => [qw/1 2/] },
28 or => { stmt => 'WHERE a = ? OR b = ?', bind => [qw/1 2/] },
29 or_and => { stmt => 'WHERE ( foo = ? OR bar = ? ) AND baz = ? ', bind => [qw/1 2 3/] },
30 or_or => { stmt => 'WHERE foo = ? OR bar = ? OR baz = ?', bind => [qw/1 2 3/] },
31 and_or => { stmt => 'WHERE ( foo = ? AND bar = ? ) OR baz = ?', bind => [qw/1 2 3/] },
37 where => { -and => [a => 1, b => 2] },
38 %{$and_or_args->{and}},
41 where => [ -and => [a => 1, b => 2] ],
42 %{$and_or_args->{and}},
45 where => { -or => [a => 1, b => 2] },
46 %{$and_or_args->{or}},
49 where => [ -or => [a => 1, b => 2] ],
50 %{$and_or_args->{or}},
54 where => { -and => {a => 1, b => 2} },
55 %{$and_or_args->{and}},
58 where => [ -and => {a => 1, b => 2} ],
59 %{$and_or_args->{and}},
62 where => { -or => {a => 1, b => 2} },
63 %{$and_or_args->{or}},
66 where => [ -or => {a => 1, b => 2} ],
67 %{$and_or_args->{or}},
70 # test modifiers within hashrefs
73 [ foo => 1, bar => 2 ],
76 %{$and_or_args->{or_or}},
80 [ foo => 1, bar => 2 ],
83 %{$and_or_args->{or_and}},
86 # test modifiers within arrayrefs
89 [ foo => 1, bar => 2 ],
92 %{$and_or_args->{or_or}},
96 [ foo => 1, bar => 2 ],
99 %{$and_or_args->{or_and}},
102 # test ambiguous modifiers within hashrefs (op extends to to immediate RHS only)
104 where => { -and => [ -or =>
105 [ foo => 1, bar => 2 ],
108 %{$and_or_args->{or_and}},
111 where => { -or => [ -and =>
112 [ foo => 1, bar => 2 ],
115 %{$and_or_args->{and_or}},
118 # test ambiguous modifiers within arrayrefs (op extends to to immediate RHS only)
120 where => [ -and => [ -or =>
121 [ foo => 1, bar => 2 ],
124 %{$and_or_args->{or_and}},
127 where => [ -or => [ -and =>
128 [ foo => 1, bar => 2 ],
131 %{$and_or_args->{and_or}},
134 # test column multi-cond in arrayref (useless example)
136 where => { x => [ -and => (1 .. 3) ] },
137 stmt => 'WHERE x = ? AND x = ? AND x = ?',
140 # test column multi-cond in arrayref (more useful)
142 where => { x => [ -and => {'!=' => 1}, {'!=' => 2}, {'!=' => 3} ] },
143 stmt => 'WHERE x != ? AND x != ? AND x != ?',
146 # test column multi-cond in arrayref (even more useful)
148 where => { x => { '!=' => [ -and => (1 .. 3) ] } },
149 stmt => 'WHERE x != ? AND x != ? AND x != ?',
153 # the -or should affect only the inner hashref, as we are not in an outer arrayref
156 -or => { '!=', 1, '>=', 2 }, -like => 'x%'
158 stmt => 'WHERE x LIKE ? AND ( x != ? OR x >= ? )',
159 bind => [qw/x% 1 2/],
162 # the -and should affect the OUTER arrayref, while the internal structures remain intact
165 -and => [ 1, 2 ], { -like => 'x%' }
167 stmt => 'WHERE (x = ? OR x = ?) AND x LIKE ?',
168 bind => [qw/1 2 x%/],
172 where => { -and => [a => 1, b => 2], x => 9, -or => { c => 3, d => 4 } },
173 stmt => 'WHERE a = ? AND b = ? AND ( c = ? OR d = ? ) AND x = ?',
174 bind => [qw/1 2 3 4 9/],
178 where => { -and => [a => 1, b => 2, k => [11, 12] ], x => 9, -or => { c => 3, d => 4, l => { '=' => [21, 22] } } },
179 stmt => 'WHERE a = ? AND b = ? AND (k = ? OR k = ?) AND ((l = ? OR l = ?) OR c = ? OR d = ? ) AND x = ?',
180 bind => [qw/1 2 11 12 21 22 3 4 9/],
184 where => { -or => [a => 1, b => 2, k => [11, 12] ], x => 9, -and => { c => 3, d => 4, l => { '=' => [21, 22] } } },
185 stmt => 'WHERE c = ? AND d = ? AND ( l = ? OR l = ?) AND (a = ? OR b = ? OR k = ? OR k = ?) AND x = ?',
186 bind => [qw/3 4 21 22 1 2 11 12 9/],
190 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 }],
191 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 = ?)',
196 # explicit OR logic in arrays should leave everything intact
197 args => { logic => 'or' },
198 where => { -and => [a => 1, b => 2, k => [11, 12] ], x => 9, -or => { c => 3, d => 4, l => { '=' => [21, 22] } } },
199 stmt => 'WHERE a = ? AND b = ? AND (k = ? OR k = ?) AND ( l = ? OR l = ? OR c = ? OR d = ? ) AND x = ? ',
200 bind => [qw/1 2 11 12 21 22 3 4 9/],
204 # flip logic in arrays except where excplicitly requested otherwise
205 args => { logic => 'and' },
206 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 }],
207 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 = ?',
211 # 1st -and is in column mode, thus flips the entire array, whereas the
212 # 2nd one is just a condition modifier
215 col => [ -and => {'<' => 123}, {'>' => 456 }, {'!=' => 789} ],
217 col2 => [ -or => { -like => 'crap' }, { -like => 'crop' } ],
218 col3 => [ -and => { -like => 'chap' }, { -like => 'chop' } ],
222 (col < ? AND col > ? AND col != ?)
225 ( col2 LIKE ? OR col2 LIKE ? )
227 ( col3 LIKE ? AND col3 LIKE ? )
230 bind => [qw/123 456 789 crap crop chap chop/],
234 # some corner cases by ldami (some produce useless SQL, just for clarification on 1.5 direction)
239 -and => [ { -like => 'foo%'}, {'>' => 'moo'} ],
240 { -like => '%bar', '<' => 'baz'},
241 [ {-like => '%alpha'}, {-like => '%beta'} ],
242 [ {'!=' => 'toto', '=' => 'koko'} ],
244 stmt => 'WHERE (foo LIKE ? OR foo > ?) AND (foo LIKE ? AND foo < ?) AND (foo LIKE ? OR foo LIKE ?) AND (foo != ? AND foo = ?)',
245 bind => [qw/foo% moo %bar baz %alpha %beta toto koko/],
250 -and => [a => 1, b => 2],
251 -or => [c => 3, d => 4],
252 e => [-and => {-like => 'foo%'}, {-like => '%bar'} ],
254 stmt => 'WHERE (a = ? AND b = ?) OR c = ? OR d = ? OR (e LIKE ? AND e LIKE ?)',
255 bind => [qw/1 2 3 4 foo% %bar/],
258 # -or has nothing to flip
260 where => [-and => [{foo => 1}, {bar => 2}, -or => {baz => 3}] ],
261 stmt => 'WHERE foo = ? AND bar = ? AND baz = ?',
265 where => [-and => [{foo => 1}, {bar => 2}, -or => {baz => 3, woz => 4} ] ],
266 stmt => 'WHERE foo = ? AND bar = ? AND (baz = ? OR woz = ?)',
270 # -and has only 1 following element, thus all still ORed
272 where => { col => [ -and => [{'<' => 123}, {'>' => 456 }, {'!=' => 789}] ] },
273 stmt => 'WHERE col < ? OR col > ? OR col != ?',
274 bind => [qw/123 456 789/],
277 # flipping array logic affects both column value and condition arrays
279 args => { logic => 'and' },
280 where => [ col => [ {'<' => 123}, {'>' => 456 }, {'!=' => 789} ], col2 => 0 ],
281 stmt => 'WHERE col < ? AND col > ? AND col != ? AND col2 = ?',
282 bind => [qw/123 456 789 0/],
285 # flipping array logic with explicit -and works
287 args => { logic => 'and' },
288 where => [ col => [ -and => {'<' => 123}, {'>' => 456 }, {'!=' => 789} ], col2 => 0 ],
289 stmt => 'WHERE col < ? AND col > ? AND col != ? AND col2 = ?',
290 bind => [qw/123 456 789 0/],
292 # flipping array logic with explicit -or flipping it back
294 args => { logic => 'and' },
295 where => [ col => [ -or => {'<' => 123}, {'>' => 456 }, {'!=' => 789} ], col2 => 0 ],
296 stmt => 'WHERE (col < ? OR col > ? OR col != ?) AND col2 = ?',
297 bind => [qw/123 456 789 0/],
301 # modN and mod_N were a bad design decision - they go away in SQLA2, warn now
302 my @numbered_mods = (
305 -and => [a => 10, b => 11],
306 -and2 => [ c => 20, d => 21 ],
308 -nest2 => [ y => 2 ],
309 -or => { m => 7, n => 8 },
310 -or2 => { m => 17, n => 18 },
312 correct => { -and => [
313 -and => [a => 10, b => 11],
314 -and => [ c => 20, d => 21 ],
317 -or => { m => 7, n => 8 },
318 -or => { m => 17, n => 18 },
323 -and2 => [a => 10, b => 11],
324 -and_3 => [ c => 20, d => 21 ],
325 -nest2 => [ x => 1 ],
326 -nest_3 => [ y => 2 ],
327 -or2 => { m => 7, n => 8 },
328 -or_3 => { m => 17, n => 18 },
330 correct => [ -and => [
331 -and => [a => 10, b => 11],
332 -and => [ c => 20, d => 21 ],
335 -or => { m => 7, n => 8 },
336 -or => { m => 17, n => 18 },
341 plan tests => @and_or_tests*3 + @numbered_mods*4;
343 for my $case (@and_or_tests) {
345 local $TODO = $case->{todo} if $case->{todo};
347 local $Data::Dumper::Terse = 1;
350 local $SIG{__WARN__} = sub { push @w, @_ };
351 my $sql = SQL::Abstract->new ($case->{args} || {});
353 my ($stmt, @bind) = $sql->where($case->{where});
360 || diag "Search term:\n" . Dumper $case->{where};
362 is (@w, 0, 'No warnings within and-or tests')
363 || diag join "\n", 'Emitted warnings:', @w;
367 my $w_str = "\QUse of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0\E";
368 for my $case (@numbered_mods) {
369 local $Data::Dumper::Terse = 1;
372 local $SIG{__WARN__} = sub { push @w, @_ };
373 my $sql = SQL::Abstract->new ($case->{args} || {});
375 my ($old_s, @old_b) = $sql->where($case->{backcompat});
376 my ($new_s, @new_b) = $sql->where($case->{correct});
380 'Backcompat and the correct(tm) syntax result in identical statements',
381 ) || diag "Search terms:\n" . Dumper {
382 backcompat => $case->{backcompat},
383 correct => $case->{correct},
387 ok (@w, 'Warnings were emitted about a mod_N construct');
391 push @non_match, $_ if ($_ !~ /$w_str/);
394 is (@non_match, 0, 'All warnings match the deprecation message')
395 || diag join "\n", 'Rogue warnings:', @non_match;