Optimise out AND/OR clause in { -(not_)in => [undef] }
[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;
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
c771453a 311# modN and mod_N were a bad design decision - they went away
312my @invalid_numbered_mods = (
189bb68b 313 {
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 },
189bb68b 321 {
01a01e57 322 -and2 => [a => 10, b => 11],
323 -and_3 => [ c => 20, d => 21 ],
403e50d1 324 -nest2 => [ x => 1 ],
325 -nest_3 => [ y => 2 ],
326 -or2 => { m => 7, n => 8 },
327 -or_3 => { m => 17, n => 18 },
328 },
403e50d1 329);
a8b90cb7 330
01a01e57 331my @nest_tests = (
2f641e1f 332 {
01a01e57 333 where => {a => 1, -nest => [b => 2, c => 3]},
2f641e1f 334 stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
335 bind => [qw/2 3 1/],
336 },
337 {
01a01e57 338 where => {a => 1, -nest => {b => 2, c => 3}},
37490959 339 stmt => 'WHERE ( ( b = ? AND c = ? AND a = ? ) )',
2f641e1f 340 bind => [qw/2 3 1/],
341 },
342 {
01a01e57 343 where => {a => 1, -or => {-nest => {b => 2, c => 3}}},
37490959 344 stmt => 'WHERE ( ( b = ? AND c = ? AND a = ? ) )',
2f641e1f 345 bind => [qw/2 3 1/],
346 },
347 {
01a01e57 348 where => {a => 1, -or => {-nest => [b => 2, c => 3]}},
2f641e1f 349 stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
350 bind => [qw/2 3 1/],
351 },
352 {
01a01e57 353 where => {a => 1, -nest => {-or => {b => 2, c => 3}}},
106c861e 354 stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )',
355 bind => [qw/2 3 1/],
2f641e1f 356 },
3a8b7db0 357 {
01a01e57 358 where => [a => 1, -nest => {b => 2, c => 3}, -nest => [d => 4, e => 5]],
37490959 359 stmt => 'WHERE ( ( a = ? OR ( b = ? AND c = ? ) OR d = ? OR e = ? ) )',
3a8b7db0 360 bind => [qw/1 2 3 4 5/],
361 },
c771453a 362 {
363 where => { -and => [
364 -and => [a => 10, b => 11],
365 -and => [ c => 20, d => 21 ],
366 -nest => [ x => 1 ],
367 -nest => [ y => 2 ],
368 -or => { m => 7, n => 8 },
369 -or => { m => 17, n => 18 },
370 ] },
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 ],
373 },
374 {
375 where => [ -and => [
376 -and => [a => 10, b => 11],
377 -and => [ c => 20, d => 21 ],
378 -nest => [ x => 1 ],
379 -nest => [ y => 2 ],
380 -or => { m => 7, n => 8 },
381 -or => { m => 17, n => 18 },
382 ] ],
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 ],
385 },
2f641e1f 386);
387
a8b90cb7 388for my $case (@and_or_tests) {
ef8c0c94 389 TODO: {
390 local $TODO = $case->{todo} if $case->{todo};
391
e5360be4 392 local $Data::Dumper::Terse = 1;
a8b90cb7 393
394 my @w;
395 local $SIG{__WARN__} = sub { push @w, @_ };
ce261791 396
a8b90cb7 397 my $sql = SQL::Abstract->new ($case->{args} || {});
8fec3fd9 398
399 my $where_copy = $dclone->($case->{where})
400 if $dclone;;
ce261791 401
e5360be4 402 lives_ok (sub {
a8b90cb7 403 my ($stmt, @bind) = $sql->where($case->{where});
403e50d1 404 is_same_sql_bind(
405 $stmt,
406 \@bind,
407 $case->{stmt},
408 $case->{bind},
409 )
e5360be4 410 || diag "Search term:\n" . Dumper $case->{where};
411 });
a8b90cb7 412 is (@w, 0, 'No warnings within and-or tests')
413 || diag join "\n", 'Emitted warnings:', @w;
ce261791 414
8fec3fd9 415 is_deeply ($case->{where}, $where_copy, 'Where conditions unchanged')
416 if $dclone;
ef8c0c94 417 }
e5360be4 418}
403e50d1 419
01a01e57 420for my $case (@nest_tests) {
2f641e1f 421 TODO: {
422 local $TODO = $case->{todo} if $case->{todo};
423
424 local $SQL::Abstract::Test::parenthesis_significant = 1;
425 local $Data::Dumper::Terse = 1;
426
427 my $sql = SQL::Abstract->new ($case->{args} || {});
428 lives_ok (sub {
429 my ($stmt, @bind) = $sql->where($case->{where});
430 is_same_sql_bind(
431 $stmt,
432 \@bind,
433 $case->{stmt},
434 $case->{bind},
435 )
436 || diag "Search term:\n" . Dumper $case->{where};
437 });
438 }
439}
440
c771453a 441for my $case (@invalid_numbered_mods) {
403e50d1 442 my $sql = SQL::Abstract->new ($case->{args} || {});
189bb68b 443 throws_ok (sub {
444 $sql->where($case);
445 }, qr/\QUse of [and|or|nest]_N modifiers is no longer supported/, 'Exception thrown on bogus syntax');
403e50d1 446}
447
8fec3fd9 448done_testing;