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