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