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