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 |
a8b90cb7 |
13 | Test -and -or and -nest 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) |
147 | { |
222c9505 |
148 | # todo => 'Clarify semantics in 1.52', |
6c8ac65d |
149 | where => { x => { '!=' => [ -and => (1 .. 3) ] } }, |
150 | stmt => 'WHERE x != ? AND x != ? AND x != ?', |
151 | bind => [1..3], |
152 | }, |
153 | |
154 | # the -or should affect only the inner hashref, as we are not in an outer arrayref |
05a05fd1 |
155 | { |
222c9505 |
156 | # todo => 'Clarify semantics in 1.52', |
05a05fd1 |
157 | where => { x => { |
158 | -or => { '!=', 1, '>=', 2 }, -like => 'x%' |
159 | }}, |
160 | stmt => 'WHERE (x != ? OR x >= ?) AND x LIKE ?', |
161 | bind => [qw/1 2 x%/], |
162 | }, |
6c8ac65d |
163 | |
164 | # the -and should affect the OUTER arrayref, while the internal structures remain intact |
05a05fd1 |
165 | { |
166 | where => { x => [ |
167 | -and => [ 1, 2 ], { -like => 'x%' } |
168 | ]}, |
6c8ac65d |
169 | stmt => 'WHERE (x = ? OR x = ?) AND x LIKE ?', |
05a05fd1 |
170 | bind => [qw/1 2 x%/], |
a8b90cb7 |
171 | }, |
6c8ac65d |
172 | |
a8b90cb7 |
173 | { |
174 | where => { -and => [a => 1, b => 2], x => 9, -or => { c => 3, d => 4 } }, |
175 | stmt => 'WHERE a = ? AND b = ? AND ( c = ? OR d = ? ) AND x = ?', |
176 | bind => [qw/1 2 3 4 9/], |
177 | }, |
6c8ac65d |
178 | |
a8b90cb7 |
179 | { |
180 | where => { -and => [a => 1, b => 2, k => [11, 12] ], x => 9, -or => { c => 3, d => 4, l => { '=' => [21, 22] } } }, |
6c8ac65d |
181 | stmt => 'WHERE a = ? AND b = ? AND (k = ? OR k = ?) AND ((l = ? OR l = ?) OR c = ? OR d = ? ) AND x = ?', |
a8b90cb7 |
182 | bind => [qw/1 2 11 12 21 22 3 4 9/], |
183 | }, |
e5360be4 |
184 | |
a8b90cb7 |
185 | { |
a8b90cb7 |
186 | where => { -or => [a => 1, b => 2, k => [11, 12] ], x => 9, -and => { c => 3, d => 4, l => { '=' => [21, 22] } } }, |
187 | stmt => 'WHERE c = ? AND d = ? AND ( l = ? OR l = ?) AND (a = ? OR b = ? OR k = ? OR k = ?) AND x = ?', |
188 | bind => [qw/3 4 21 22 1 2 11 12 9/], |
189 | }, |
e5360be4 |
190 | |
a8b90cb7 |
191 | { |
192 | 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 }], |
193 | 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 = ?)', |
194 | bind => [1 .. 13], |
195 | }, |
6c8ac65d |
196 | |
197 | { |
198 | # explicit OR logic in arrays should leave everything intact |
199 | args => { logic => 'or' }, |
200 | where => { -and => [a => 1, b => 2, k => [11, 12] ], x => 9, -or => { c => 3, d => 4, l => { '=' => [21, 22] } } }, |
201 | stmt => 'WHERE a = ? AND b = ? AND (k = ? OR k = ?) AND ( l = ? OR l = ? OR c = ? OR d = ? ) AND x = ? ', |
202 | bind => [qw/1 2 11 12 21 22 3 4 9/], |
203 | }, |
204 | |
a8b90cb7 |
205 | { |
6c8ac65d |
206 | # flip logic in arrays except where excplicitly requested otherwise |
a8b90cb7 |
207 | args => { logic => 'and' }, |
208 | 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 }], |
209 | 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 = ?', |
210 | bind => [1 .. 13], |
211 | }, |
05a05fd1 |
212 | |
6c8ac65d |
213 | # 1st -and is in column mode, thus flips the entire array, whereas the |
214 | # 2nd one is just a condition modifier |
215 | { |
216 | where => [ |
217 | col => [ -and => {'<' => 123}, {'>' => 456 }, {'!=' => 789} ], |
218 | -and => [ |
219 | col2 => [ -or => { -like => 'crap' }, { -like => 'crop' } ], |
220 | col3 => [ -and => { -like => 'chap' }, { -like => 'chop' } ], |
221 | ], |
222 | ], |
223 | stmt => 'WHERE |
224 | (col < ? AND col > ? AND col != ?) |
225 | OR |
226 | ( |
227 | ( col2 LIKE ? OR col2 LIKE ? ) |
228 | AND |
229 | ( col3 LIKE ? AND col3 LIKE ? ) |
230 | ) |
231 | ', |
232 | bind => [qw/123 456 789 crap crop chap chop/], |
233 | }, |
234 | |
05a05fd1 |
235 | ########## |
236 | # some corner cases by ldami (some produce useless SQL, just for clarification on 1.5 direction) |
237 | # |
238 | |
239 | { |
240 | where => { foo => [ |
241 | -and => [ { -like => 'foo%'}, {'>' => 'moo'} ], |
242 | { -like => '%bar', '<' => 'baz'}, |
243 | [ {-like => '%alpha'}, {-like => '%beta'} ], |
6c8ac65d |
244 | [ {'!=' => 'toto', '=' => 'koko'} ], |
05a05fd1 |
245 | ] }, |
6c8ac65d |
246 | stmt => 'WHERE (foo LIKE ? OR foo > ?) AND (foo LIKE ? AND foo < ?) AND (foo LIKE ? OR foo LIKE ?) AND (foo != ? AND foo = ?)', |
05a05fd1 |
247 | bind => [qw/foo% moo %bar baz %alpha %beta toto koko/], |
248 | }, |
6c8ac65d |
249 | |
05a05fd1 |
250 | { |
6c8ac65d |
251 | where => [ |
252 | -and => [a => 1, b => 2], |
253 | -or => [c => 3, d => 4], |
254 | e => [-and => {-like => 'foo%'}, {-like => '%bar'} ], |
255 | ], |
256 | stmt => 'WHERE (a = ? AND b = ?) OR c = ? OR d = ? OR (e LIKE ? AND e LIKE ?)', |
257 | bind => [qw/1 2 3 4 foo% %bar/], |
258 | }, |
259 | |
260 | # -or has nothing to flip |
261 | { |
262 | where => [-and => [{foo => 1}, {bar => 2}, -or => {baz => 3}] ], |
05a05fd1 |
263 | stmt => 'WHERE foo = ? AND bar = ? AND baz = ?', |
6c8ac65d |
264 | bind => [1 .. 3], |
05a05fd1 |
265 | }, |
266 | { |
6c8ac65d |
267 | where => [-and => [{foo => 1}, {bar => 2}, -or => {baz => 3, woz => 4} ] ], |
268 | stmt => 'WHERE foo = ? AND bar = ? AND (baz = ? OR woz = ?)', |
05a05fd1 |
269 | bind => [1 .. 4], |
270 | }, |
271 | |
6c8ac65d |
272 | # -and has only 1 following element, thus all still ORed |
05a05fd1 |
273 | { |
6c8ac65d |
274 | where => { col => [ -and => [{'<' => 123}, {'>' => 456 }, {'!=' => 789}] ] }, |
05a05fd1 |
275 | stmt => 'WHERE col < ? OR col > ? OR col != ?', |
276 | bind => [qw/123 456 789/], |
277 | }, |
278 | |
6c8ac65d |
279 | # flipping array logic affects both column value and condition arrays |
05a05fd1 |
280 | { |
6c8ac65d |
281 | args => { logic => 'and' }, |
282 | where => [ col => [ {'<' => 123}, {'>' => 456 }, {'!=' => 789} ], col2 => 0 ], |
283 | stmt => 'WHERE col < ? AND col > ? AND col != ? AND col2 = ?', |
284 | bind => [qw/123 456 789 0/], |
285 | }, |
286 | |
287 | # flipping array logic with explicit -and works |
288 | { |
289 | args => { logic => 'and' }, |
290 | where => [ col => [ -and => {'<' => 123}, {'>' => 456 }, {'!=' => 789} ], col2 => 0 ], |
291 | stmt => 'WHERE col < ? AND col > ? AND col != ? AND col2 = ?', |
292 | bind => [qw/123 456 789 0/], |
293 | }, |
294 | # flipping array logic with explicit -or flipping it back |
295 | { |
296 | args => { logic => 'and' }, |
297 | where => [ col => [ -or => {'<' => 123}, {'>' => 456 }, {'!=' => 789} ], col2 => 0 ], |
298 | stmt => 'WHERE (col < ? OR col > ? OR col != ?) AND col2 = ?', |
299 | bind => [qw/123 456 789 0/], |
05a05fd1 |
300 | }, |
a8b90cb7 |
301 | ); |
e5360be4 |
302 | |
403e50d1 |
303 | # modN and mod_N were a bad design decision - they go away in SQLA2, warn now |
304 | my @numbered_mods = ( |
305 | { |
306 | backcompat => { |
307 | -and => [a => 10, b => 11], |
308 | -and2 => [ c => 20, d => 21 ], |
309 | -nest => [ x => 1 ], |
310 | -nest2 => [ y => 2 ], |
311 | -or => { m => 7, n => 8 }, |
312 | -or2 => { m => 17, n => 18 }, |
313 | }, |
314 | correct => { -and => [ |
315 | -and => [a => 10, b => 11], |
316 | -and => [ c => 20, d => 21 ], |
317 | -nest => [ x => 1 ], |
318 | -nest => [ y => 2 ], |
319 | -or => { m => 7, n => 8 }, |
320 | -or => { m => 17, n => 18 }, |
321 | ] }, |
322 | }, |
323 | { |
324 | backcompat => { |
325 | -and2 => [a => 10, b => 11], |
326 | -and_3 => [ c => 20, d => 21 ], |
327 | -nest2 => [ x => 1 ], |
328 | -nest_3 => [ y => 2 ], |
329 | -or2 => { m => 7, n => 8 }, |
330 | -or_3 => { m => 17, n => 18 }, |
331 | }, |
332 | correct => [ -and => [ |
333 | -and => [a => 10, b => 11], |
334 | -and => [ c => 20, d => 21 ], |
335 | -nest => [ x => 1 ], |
336 | -nest => [ y => 2 ], |
337 | -or => { m => 7, n => 8 }, |
338 | -or => { m => 17, n => 18 }, |
339 | ] ], |
340 | }, |
341 | ); |
a8b90cb7 |
342 | |
403e50d1 |
343 | plan tests => @and_or_tests*3 + @numbered_mods*4; |
a8b90cb7 |
344 | |
345 | for my $case (@and_or_tests) { |
ef8c0c94 |
346 | TODO: { |
347 | local $TODO = $case->{todo} if $case->{todo}; |
348 | |
e5360be4 |
349 | local $Data::Dumper::Terse = 1; |
a8b90cb7 |
350 | |
351 | my @w; |
352 | local $SIG{__WARN__} = sub { push @w, @_ }; |
353 | my $sql = SQL::Abstract->new ($case->{args} || {}); |
e5360be4 |
354 | lives_ok (sub { |
a8b90cb7 |
355 | my ($stmt, @bind) = $sql->where($case->{where}); |
403e50d1 |
356 | is_same_sql_bind( |
357 | $stmt, |
358 | \@bind, |
359 | $case->{stmt}, |
360 | $case->{bind}, |
361 | ) |
e5360be4 |
362 | || diag "Search term:\n" . Dumper $case->{where}; |
363 | }); |
a8b90cb7 |
364 | is (@w, 0, 'No warnings within and-or tests') |
365 | || diag join "\n", 'Emitted warnings:', @w; |
ef8c0c94 |
366 | } |
e5360be4 |
367 | } |
403e50d1 |
368 | |
6c8ac65d |
369 | my $w_str = "\QUse of [and|or|nest]_N modifiers is deprecated and will be removed in SQLA v2.0\E"; |
403e50d1 |
370 | for my $case (@numbered_mods) { |
371 | local $Data::Dumper::Terse = 1; |
372 | |
373 | my @w; |
374 | local $SIG{__WARN__} = sub { push @w, @_ }; |
375 | my $sql = SQL::Abstract->new ($case->{args} || {}); |
376 | lives_ok (sub { |
377 | my ($old_s, @old_b) = $sql->where($case->{backcompat}); |
378 | my ($new_s, @new_b) = $sql->where($case->{correct}); |
379 | is_same_sql_bind( |
380 | $old_s, \@old_b, |
381 | $new_s, \@new_b, |
382 | 'Backcompat and the correct(tm) syntax result in identical statements', |
383 | ) || diag "Search terms:\n" . Dumper { |
384 | backcompat => $case->{backcompat}, |
385 | correct => $case->{correct}, |
386 | }; |
387 | }); |
388 | |
389 | ok (@w, 'Warnings were emitted about a mod_N construct'); |
390 | |
391 | my @non_match; |
392 | for (@w) { |
6c8ac65d |
393 | push @non_match, $_ if ($_ !~ /$w_str/); |
403e50d1 |
394 | } |
395 | |
396 | is (@non_match, 0, 'All warnings match the deprecation message') |
397 | || diag join "\n", 'Rogue warnings:', @non_match; |
398 | } |
399 | |