Commit | Line | Data |
e5360be4 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | use Test::More; |
6 | use Test::Exception; |
7 | use SQL::Abstract::Test import => ['is_same_sql_bind']; |
8 | |
9 | use Data::Dumper; |
fdfbbc65 |
10 | use Storable qw/dclone/; |
e5360be4 |
11 | use SQL::Abstract; |
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 |
22 | Test -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. |
28 | * When in column multi-condition context i.e. |
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) |
35 | my $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 |
43 | my @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 | |
05a05fd1 |
79 | # test modifiers within hashrefs |
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 | |
95 | # test modifiers within arrayrefs |
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 | { |
173 | where => { x => [ |
174 | -and => [ 1, 2 ], { -like => 'x%' } |
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 | |
6c8ac65d |
220 | # 1st -and is in column mode, thus flips the entire array, whereas the |
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 |
311 | my @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 |
350 | my @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 | |
ce261791 |
383 | plan tests => @and_or_tests*4 + @numbered_mods*4 + @nest_tests*2; |
a8b90cb7 |
384 | |
385 | for my $case (@and_or_tests) { |
ef8c0c94 |
386 | TODO: { |
387 | local $TODO = $case->{todo} if $case->{todo}; |
388 | |
e5360be4 |
389 | local $Data::Dumper::Terse = 1; |
a8b90cb7 |
390 | |
391 | my @w; |
392 | local $SIG{__WARN__} = sub { push @w, @_ }; |
ce261791 |
393 | |
a8b90cb7 |
394 | my $sql = SQL::Abstract->new ($case->{args} || {}); |
fdfbbc65 |
395 | my $where_copy = dclone($case->{where}); |
ce261791 |
396 | |
e5360be4 |
397 | lives_ok (sub { |
a8b90cb7 |
398 | my ($stmt, @bind) = $sql->where($case->{where}); |
403e50d1 |
399 | is_same_sql_bind( |
400 | $stmt, |
401 | \@bind, |
402 | $case->{stmt}, |
403 | $case->{bind}, |
404 | ) |
e5360be4 |
405 | || diag "Search term:\n" . Dumper $case->{where}; |
406 | }); |
a8b90cb7 |
407 | is (@w, 0, 'No warnings within and-or tests') |
408 | || diag join "\n", 'Emitted warnings:', @w; |
ce261791 |
409 | |
410 | is_deeply ($case->{where}, $where_copy, 'Where conditions unchanged'); |
ef8c0c94 |
411 | } |
e5360be4 |
412 | } |
403e50d1 |
413 | |
01a01e57 |
414 | for my $case (@nest_tests) { |
2f641e1f |
415 | TODO: { |
416 | local $TODO = $case->{todo} if $case->{todo}; |
417 | |
418 | local $SQL::Abstract::Test::parenthesis_significant = 1; |
419 | local $Data::Dumper::Terse = 1; |
420 | |
421 | my $sql = SQL::Abstract->new ($case->{args} || {}); |
422 | lives_ok (sub { |
423 | my ($stmt, @bind) = $sql->where($case->{where}); |
424 | is_same_sql_bind( |
425 | $stmt, |
426 | \@bind, |
427 | $case->{stmt}, |
428 | $case->{bind}, |
429 | ) |
430 | || diag "Search term:\n" . Dumper $case->{where}; |
431 | }); |
432 | } |
433 | } |
434 | |
01a01e57 |
435 | |
436 | |
437 | my $w_str = "\QUse of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0\E"; |
438 | for my $case (@numbered_mods) { |
2f641e1f |
439 | TODO: { |
440 | local $TODO = $case->{todo} if $case->{todo}; |
441 | |
403e50d1 |
442 | local $Data::Dumper::Terse = 1; |
443 | |
01a01e57 |
444 | my @w; |
445 | local $SIG{__WARN__} = sub { push @w, @_ }; |
403e50d1 |
446 | my $sql = SQL::Abstract->new ($case->{args} || {}); |
447 | lives_ok (sub { |
448 | my ($old_s, @old_b) = $sql->where($case->{backcompat}); |
449 | my ($new_s, @new_b) = $sql->where($case->{correct}); |
450 | is_same_sql_bind( |
451 | $old_s, \@old_b, |
452 | $new_s, \@new_b, |
453 | 'Backcompat and the correct(tm) syntax result in identical statements', |
454 | ) || diag "Search terms:\n" . Dumper { |
455 | backcompat => $case->{backcompat}, |
456 | correct => $case->{correct}, |
457 | }; |
458 | }); |
459 | |
01a01e57 |
460 | ok (@w, 'Warnings were emitted about a mod_N construct'); |
461 | |
462 | my @non_match; |
463 | for (@w) { |
464 | push @non_match, $_ if ($_ !~ /$w_str/); |
465 | } |
466 | |
467 | is (@non_match, 0, 'All warnings match the deprecation message') |
468 | || diag join "\n", 'Rogue warnings:', @non_match; |
2f641e1f |
469 | } |
403e50d1 |
470 | } |
471 | |