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 = ( |
189bb68b |
312 | { |
01a01e57 |
313 | -and => [a => 10, b => 11], |
314 | -and2 => [ c => 20, d => 21 ], |
403e50d1 |
315 | -nest => [ x => 1 ], |
316 | -nest2 => [ y => 2 ], |
01a01e57 |
317 | -or => { m => 7, n => 8 }, |
318 | -or2 => { m => 17, n => 18 }, |
403e50d1 |
319 | }, |
189bb68b |
320 | { |
01a01e57 |
321 | -and2 => [a => 10, b => 11], |
322 | -and_3 => [ c => 20, d => 21 ], |
403e50d1 |
323 | -nest2 => [ x => 1 ], |
324 | -nest_3 => [ y => 2 ], |
325 | -or2 => { m => 7, n => 8 }, |
326 | -or_3 => { m => 17, n => 18 }, |
327 | }, |
403e50d1 |
328 | ); |
a8b90cb7 |
329 | |
01a01e57 |
330 | my @nest_tests = ( |
2f641e1f |
331 | { |
01a01e57 |
332 | where => {a => 1, -nest => [b => 2, c => 3]}, |
2f641e1f |
333 | stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )', |
334 | bind => [qw/2 3 1/], |
335 | }, |
336 | { |
01a01e57 |
337 | where => {a => 1, -nest => {b => 2, c => 3}}, |
37490959 |
338 | stmt => 'WHERE ( ( b = ? AND c = ? AND a = ? ) )', |
2f641e1f |
339 | bind => [qw/2 3 1/], |
340 | }, |
341 | { |
01a01e57 |
342 | where => {a => 1, -or => {-nest => {b => 2, c => 3}}}, |
37490959 |
343 | stmt => 'WHERE ( ( b = ? AND c = ? AND a = ? ) )', |
2f641e1f |
344 | bind => [qw/2 3 1/], |
345 | }, |
346 | { |
01a01e57 |
347 | where => {a => 1, -or => {-nest => [b => 2, c => 3]}}, |
2f641e1f |
348 | stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )', |
349 | bind => [qw/2 3 1/], |
350 | }, |
351 | { |
01a01e57 |
352 | where => {a => 1, -nest => {-or => {b => 2, c => 3}}}, |
106c861e |
353 | stmt => 'WHERE ( ( (b = ? OR c = ?) AND a = ? ) )', |
354 | bind => [qw/2 3 1/], |
2f641e1f |
355 | }, |
3a8b7db0 |
356 | { |
01a01e57 |
357 | where => [a => 1, -nest => {b => 2, c => 3}, -nest => [d => 4, e => 5]], |
37490959 |
358 | stmt => 'WHERE ( ( a = ? OR ( b = ? AND c = ? ) OR d = ? OR e = ? ) )', |
3a8b7db0 |
359 | bind => [qw/1 2 3 4 5/], |
360 | }, |
2f641e1f |
361 | ); |
362 | |
189bb68b |
363 | plan tests => @and_or_tests*4 + @numbered_mods + @nest_tests*2; |
a8b90cb7 |
364 | |
365 | for my $case (@and_or_tests) { |
ef8c0c94 |
366 | TODO: { |
367 | local $TODO = $case->{todo} if $case->{todo}; |
368 | |
e5360be4 |
369 | local $Data::Dumper::Terse = 1; |
a8b90cb7 |
370 | |
371 | my @w; |
372 | local $SIG{__WARN__} = sub { push @w, @_ }; |
ce261791 |
373 | |
a8b90cb7 |
374 | my $sql = SQL::Abstract->new ($case->{args} || {}); |
fdfbbc65 |
375 | my $where_copy = dclone($case->{where}); |
ce261791 |
376 | |
e5360be4 |
377 | lives_ok (sub { |
a8b90cb7 |
378 | my ($stmt, @bind) = $sql->where($case->{where}); |
403e50d1 |
379 | is_same_sql_bind( |
380 | $stmt, |
381 | \@bind, |
382 | $case->{stmt}, |
383 | $case->{bind}, |
384 | ) |
e5360be4 |
385 | || diag "Search term:\n" . Dumper $case->{where}; |
386 | }); |
a8b90cb7 |
387 | is (@w, 0, 'No warnings within and-or tests') |
388 | || diag join "\n", 'Emitted warnings:', @w; |
ce261791 |
389 | |
390 | is_deeply ($case->{where}, $where_copy, 'Where conditions unchanged'); |
ef8c0c94 |
391 | } |
e5360be4 |
392 | } |
403e50d1 |
393 | |
01a01e57 |
394 | for my $case (@nest_tests) { |
2f641e1f |
395 | TODO: { |
396 | local $TODO = $case->{todo} if $case->{todo}; |
397 | |
398 | local $SQL::Abstract::Test::parenthesis_significant = 1; |
399 | local $Data::Dumper::Terse = 1; |
400 | |
401 | my $sql = SQL::Abstract->new ($case->{args} || {}); |
402 | lives_ok (sub { |
403 | my ($stmt, @bind) = $sql->where($case->{where}); |
404 | is_same_sql_bind( |
405 | $stmt, |
406 | \@bind, |
407 | $case->{stmt}, |
408 | $case->{bind}, |
409 | ) |
410 | || diag "Search term:\n" . Dumper $case->{where}; |
411 | }); |
412 | } |
413 | } |
414 | |
01a01e57 |
415 | |
416 | |
417 | my $w_str = "\QUse of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0\E"; |
418 | for my $case (@numbered_mods) { |
2f641e1f |
419 | TODO: { |
403e50d1 |
420 | local $Data::Dumper::Terse = 1; |
421 | |
01a01e57 |
422 | my @w; |
403e50d1 |
423 | my $sql = SQL::Abstract->new ($case->{args} || {}); |
189bb68b |
424 | throws_ok (sub { |
425 | $sql->where($case); |
426 | }, qr/\QUse of [and|or|nest]_N modifiers is no longer supported/, 'Exception thrown on bogus syntax'); |
2f641e1f |
427 | } |
403e50d1 |
428 | } |
429 | |