And yet more improvements in the parsing engine
[scpubgit/Q-Branch.git] / t / 04modifiers.t
CommitLineData
e5360be4 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5use Test::More;
6use Test::Exception;
7use SQL::Abstract::Test import => ['is_same_sql_bind'];
8
9use Data::Dumper;
10use SQL::Abstract;
11
8fec3fd9 12my $dclone = eval { require Storable; \&Storable::dclone };
13
48d9f5f8 14#### WARNING ####
15#
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
19#
20#################
21
e5360be4 22=begin
01a01e57 23Test -and -or and -nest modifiers, assuming the following:
e5360be4 24
6c8ac65d 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
e5360be4 32
33=cut
34
05a05fd1 35# no warnings (the -or/-and => { } warning is silly, there is nothing wrong with such usage)
36my $and_or_args = {
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/] },
42};
43
a8b90cb7 44my @and_or_tests = (
05a05fd1 45 # basic tests
a8b90cb7 46 {
47 where => { -and => [a => 1, b => 2] },
05a05fd1 48 %{$and_or_args->{and}},
a8b90cb7 49 },
50 {
51 where => [ -and => [a => 1, b => 2] ],
05a05fd1 52 %{$and_or_args->{and}},
a8b90cb7 53 },
54 {
55 where => { -or => [a => 1, b => 2] },
05a05fd1 56 %{$and_or_args->{or}},
a8b90cb7 57 },
58 {
59 where => [ -or => [a => 1, b => 2] ],
05a05fd1 60 %{$and_or_args->{or}},
61 },
62
6c8ac65d 63 {
64 where => { -and => {a => 1, b => 2} },
65 %{$and_or_args->{and}},
66 },
67 {
68 where => [ -and => {a => 1, b => 2} ],
69 %{$and_or_args->{and}},
70 },
71 {
72 where => { -or => {a => 1, b => 2} },
73 %{$and_or_args->{or}},
74 },
75 {
76 where => [ -or => {a => 1, b => 2} ],
77 %{$and_or_args->{or}},
78 },
79
05a05fd1 80 # test modifiers within hashrefs
81 {
82 where => { -or => [
83 [ foo => 1, bar => 2 ],
84 baz => 3,
85 ]},
86 %{$and_or_args->{or_or}},
87 },
88 {
89 where => { -and => [
90 [ foo => 1, bar => 2 ],
91 baz => 3,
92 ]},
93 %{$and_or_args->{or_and}},
94 },
95
96 # test modifiers within arrayrefs
97 {
98 where => [ -or => [
99 [ foo => 1, bar => 2 ],
100 baz => 3,
101 ]],
102 %{$and_or_args->{or_or}},
103 },
104 {
105 where => [ -and => [
106 [ foo => 1, bar => 2 ],
107 baz => 3,
108 ]],
109 %{$and_or_args->{or_and}},
110 },
111
112 # test ambiguous modifiers within hashrefs (op extends to to immediate RHS only)
113 {
114 where => { -and => [ -or =>
115 [ foo => 1, bar => 2 ],
116 baz => 3,
117 ]},
118 %{$and_or_args->{or_and}},
119 },
120 {
121 where => { -or => [ -and =>
122 [ foo => 1, bar => 2 ],
123 baz => 3,
124 ]},
125 %{$and_or_args->{and_or}},
126 },
127
128 # test ambiguous modifiers within arrayrefs (op extends to to immediate RHS only)
129 {
130 where => [ -and => [ -or =>
131 [ foo => 1, bar => 2 ],
132 baz => 3,
133 ]],
134 %{$and_or_args->{or_and}},
135 },
136 {
137 where => [ -or => [ -and =>
138 [ foo => 1, bar => 2 ],
139 baz => 3
140 ]],
141 %{$and_or_args->{and_or}},
142 },
143
6c8ac65d 144 # test column multi-cond in arrayref (useless example)
145 {
146 where => { x => [ -and => (1 .. 3) ] },
147 stmt => 'WHERE x = ? AND x = ? AND x = ?',
148 bind => [1..3],
149 },
150 # test column multi-cond in arrayref (more useful)
151 {
152 where => { x => [ -and => {'!=' => 1}, {'!=' => 2}, {'!=' => 3} ] },
153 stmt => 'WHERE x != ? AND x != ? AND x != ?',
154 bind => [1..3],
155 },
156 # test column multi-cond in arrayref (even more useful)
64385831 157 {
158 where => { x => { '!=' => [ -and => (1 .. 3) ] } },
159 stmt => 'WHERE x != ? AND x != ? AND x != ?',
160 bind => [1..3],
161 },
6c8ac65d 162
163 # the -or should affect only the inner hashref, as we are not in an outer arrayref
05a05fd1 164 {
165 where => { x => {
166 -or => { '!=', 1, '>=', 2 }, -like => 'x%'
167 }},
25e4c693 168 stmt => 'WHERE x LIKE ? AND ( x != ? OR x >= ? )',
eb49170d 169 bind => [qw/x% 1 2/],
05a05fd1 170 },
6c8ac65d 171
172 # the -and should affect the OUTER arrayref, while the internal structures remain intact
05a05fd1 173 {
174 where => { x => [
175 -and => [ 1, 2 ], { -like => 'x%' }
176 ]},
6c8ac65d 177 stmt => 'WHERE (x = ? OR x = ?) AND x LIKE ?',
05a05fd1 178 bind => [qw/1 2 x%/],
a8b90cb7 179 },
6c8ac65d 180
a8b90cb7 181 {
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/],
185 },
6c8ac65d 186
a8b90cb7 187 {
188 where => { -and => [a => 1, b => 2, k => [11, 12] ], x => 9, -or => { c => 3, d => 4, l => { '=' => [21, 22] } } },
106c861e 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/],
a8b90cb7 191 },
e5360be4 192
a8b90cb7 193 {
a8b90cb7 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/],
197 },
e5360be4 198
a8b90cb7 199 {
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 = ?)',
202 bind => [1 .. 13],
203 },
6c8ac65d 204
205 {
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] } } },
106c861e 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/],
6c8ac65d 211 },
212
a8b90cb7 213 {
6c8ac65d 214 # flip logic in arrays except where excplicitly requested otherwise
a8b90cb7 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 = ?',
218 bind => [1 .. 13],
219 },
05a05fd1 220
6c8ac65d 221 # 1st -and is in column mode, thus flips the entire array, whereas the
222 # 2nd one is just a condition modifier
223 {
224 where => [
225 col => [ -and => {'<' => 123}, {'>' => 456 }, {'!=' => 789} ],
226 -and => [
227 col2 => [ -or => { -like => 'crap' }, { -like => 'crop' } ],
228 col3 => [ -and => { -like => 'chap' }, { -like => 'chop' } ],
229 ],
230 ],
231 stmt => 'WHERE
232 (col < ? AND col > ? AND col != ?)
233 OR
234 (
235 ( col2 LIKE ? OR col2 LIKE ? )
236 AND
237 ( col3 LIKE ? AND col3 LIKE ? )
238 )
239 ',
240 bind => [qw/123 456 789 crap crop chap chop/],
241 },
242
05a05fd1 243 ##########
244 # some corner cases by ldami (some produce useless SQL, just for clarification on 1.5 direction)
245 #
246
247 {
248 where => { foo => [
249 -and => [ { -like => 'foo%'}, {'>' => 'moo'} ],
250 { -like => '%bar', '<' => 'baz'},
251 [ {-like => '%alpha'}, {-like => '%beta'} ],
6c8ac65d 252 [ {'!=' => 'toto', '=' => 'koko'} ],
05a05fd1 253 ] },
6c8ac65d 254 stmt => 'WHERE (foo LIKE ? OR foo > ?) AND (foo LIKE ? AND foo < ?) AND (foo LIKE ? OR foo LIKE ?) AND (foo != ? AND foo = ?)',
05a05fd1 255 bind => [qw/foo% moo %bar baz %alpha %beta toto koko/],
256 },
6c8ac65d 257
05a05fd1 258 {
6c8ac65d 259 where => [
260 -and => [a => 1, b => 2],
261 -or => [c => 3, d => 4],
262 e => [-and => {-like => 'foo%'}, {-like => '%bar'} ],
263 ],
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/],
266 },
267
268 # -or has nothing to flip
269 {
270 where => [-and => [{foo => 1}, {bar => 2}, -or => {baz => 3}] ],
05a05fd1 271 stmt => 'WHERE foo = ? AND bar = ? AND baz = ?',
6c8ac65d 272 bind => [1 .. 3],
05a05fd1 273 },
274 {
6c8ac65d 275 where => [-and => [{foo => 1}, {bar => 2}, -or => {baz => 3, woz => 4} ] ],
276 stmt => 'WHERE foo = ? AND bar = ? AND (baz = ? OR woz = ?)',
05a05fd1 277 bind => [1 .. 4],
278 },
279
6c8ac65d 280 # -and has only 1 following element, thus all still ORed
05a05fd1 281 {
6c8ac65d 282 where => { col => [ -and => [{'<' => 123}, {'>' => 456 }, {'!=' => 789}] ] },
05a05fd1 283 stmt => 'WHERE col < ? OR col > ? OR col != ?',
284 bind => [qw/123 456 789/],
285 },
286
6c8ac65d 287 # flipping array logic affects both column value and condition arrays
05a05fd1 288 {
6c8ac65d 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/],
293 },
294
295 # flipping array logic with explicit -and works
296 {
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/],
301 },
302 # flipping array logic with explicit -or flipping it back
303 {
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/],
05a05fd1 308 },
a8b90cb7 309);
e5360be4 310
403e50d1 311# modN and mod_N were a bad design decision - they go away in SQLA2, warn now
01a01e57 312my @numbered_mods = (
403e50d1 313 {
314 backcompat => {
01a01e57 315 -and => [a => 10, b => 11],
316 -and2 => [ c => 20, d => 21 ],
403e50d1 317 -nest => [ x => 1 ],
318 -nest2 => [ y => 2 ],
01a01e57 319 -or => { m => 7, n => 8 },
320 -or2 => { m => 17, n => 18 },
403e50d1 321 },
322 correct => { -and => [
323 -and => [a => 10, b => 11],
324 -and => [ c => 20, d => 21 ],
01a01e57 325 -nest => [ x => 1 ],
326 -nest => [ y => 2 ],
403e50d1 327 -or => { m => 7, n => 8 },
328 -or => { m => 17, n => 18 },
329 ] },
330 },
331 {
332 backcompat => {
01a01e57 333 -and2 => [a => 10, b => 11],
334 -and_3 => [ c => 20, d => 21 ],
403e50d1 335 -nest2 => [ x => 1 ],
336 -nest_3 => [ y => 2 ],
337 -or2 => { m => 7, n => 8 },
338 -or_3 => { m => 17, n => 18 },
339 },
340 correct => [ -and => [
341 -and => [a => 10, b => 11],
342 -and => [ c => 20, d => 21 ],
01a01e57 343 -nest => [ x => 1 ],
344 -nest => [ y => 2 ],
403e50d1 345 -or => { m => 7, n => 8 },
346 -or => { m => 17, n => 18 },
347 ] ],
348 },
349);
a8b90cb7 350
01a01e57 351my @nest_tests = (
2f641e1f 352 {
01a01e57 353 where => {a => 1, -nest => [b => 2, c => 3]},
2f641e1f 354 stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
355 bind => [qw/2 3 1/],
356 },
357 {
01a01e57 358 where => {a => 1, -nest => {b => 2, c => 3}},
2f641e1f 359 stmt => 'WHERE ( ( (b = ? AND c = ?) AND a = ? ) )',
360 bind => [qw/2 3 1/],
361 },
362 {
01a01e57 363 where => {a => 1, -or => {-nest => {b => 2, c => 3}}},
2f641e1f 364 stmt => 'WHERE ( ( (b = ? AND c = ?) AND a = ? ) )',
365 bind => [qw/2 3 1/],
366 },
367 {
01a01e57 368 where => {a => 1, -or => {-nest => [b => 2, c => 3]}},
2f641e1f 369 stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
370 bind => [qw/2 3 1/],
371 },
372 {
01a01e57 373 where => {a => 1, -nest => {-or => {b => 2, c => 3}}},
106c861e 374 stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
375 bind => [qw/2 3 1/],
2f641e1f 376 },
3a8b7db0 377 {
01a01e57 378 where => [a => 1, -nest => {b => 2, c => 3}, -nest => [d => 4, e => 5]],
c592e251 379 stmt => 'WHERE ( ( a = ? OR ( b = ? AND c = ? ) OR ( d = ? OR e = ? ) ) )',
3a8b7db0 380 bind => [qw/1 2 3 4 5/],
381 },
2f641e1f 382);
383
a8b90cb7 384for my $case (@and_or_tests) {
ef8c0c94 385 TODO: {
386 local $TODO = $case->{todo} if $case->{todo};
387
e5360be4 388 local $Data::Dumper::Terse = 1;
a8b90cb7 389
390 my @w;
391 local $SIG{__WARN__} = sub { push @w, @_ };
ce261791 392
a8b90cb7 393 my $sql = SQL::Abstract->new ($case->{args} || {});
8fec3fd9 394
395 my $where_copy = $dclone->($case->{where})
396 if $dclone;;
ce261791 397
e5360be4 398 lives_ok (sub {
a8b90cb7 399 my ($stmt, @bind) = $sql->where($case->{where});
403e50d1 400 is_same_sql_bind(
401 $stmt,
402 \@bind,
403 $case->{stmt},
404 $case->{bind},
405 )
e5360be4 406 || diag "Search term:\n" . Dumper $case->{where};
407 });
a8b90cb7 408 is (@w, 0, 'No warnings within and-or tests')
409 || diag join "\n", 'Emitted warnings:', @w;
ce261791 410
8fec3fd9 411 is_deeply ($case->{where}, $where_copy, 'Where conditions unchanged')
412 if $dclone;
ef8c0c94 413 }
e5360be4 414}
403e50d1 415
01a01e57 416for my $case (@nest_tests) {
2f641e1f 417 TODO: {
418 local $TODO = $case->{todo} if $case->{todo};
419
420 local $SQL::Abstract::Test::parenthesis_significant = 1;
421 local $Data::Dumper::Terse = 1;
422
423 my $sql = SQL::Abstract->new ($case->{args} || {});
424 lives_ok (sub {
425 my ($stmt, @bind) = $sql->where($case->{where});
426 is_same_sql_bind(
427 $stmt,
428 \@bind,
429 $case->{stmt},
430 $case->{bind},
431 )
432 || diag "Search term:\n" . Dumper $case->{where};
433 });
434 }
435}
436
01a01e57 437
438
439my $w_str = "\QUse of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0\E";
440for my $case (@numbered_mods) {
2f641e1f 441 TODO: {
442 local $TODO = $case->{todo} if $case->{todo};
443
403e50d1 444 local $Data::Dumper::Terse = 1;
445
01a01e57 446 my @w;
447 local $SIG{__WARN__} = sub { push @w, @_ };
403e50d1 448 my $sql = SQL::Abstract->new ($case->{args} || {});
449 lives_ok (sub {
450 my ($old_s, @old_b) = $sql->where($case->{backcompat});
451 my ($new_s, @new_b) = $sql->where($case->{correct});
452 is_same_sql_bind(
453 $old_s, \@old_b,
454 $new_s, \@new_b,
455 'Backcompat and the correct(tm) syntax result in identical statements',
456 ) || diag "Search terms:\n" . Dumper {
457 backcompat => $case->{backcompat},
458 correct => $case->{correct},
459 };
460 });
461
01a01e57 462 ok (@w, 'Warnings were emitted about a mod_N construct');
463
464 my @non_match;
465 for (@w) {
466 push @non_match, $_ if ($_ !~ /$w_str/);
467 }
468
469 is (@non_match, 0, 'All warnings match the deprecation message')
470 || diag join "\n", 'Rogue warnings:', @non_match;
2f641e1f 471 }
403e50d1 472}
473
8fec3fd9 474done_testing;