Commit | Line | Data |
32eab2da |
1 | use strict; |
41751122 |
2 | use warnings; |
3 | use Test::More; |
3cdadcbe |
4 | use Test::Warn; |
4c77e2d4 |
5 | use Test::Exception; |
6 | use SQL::Abstract::Test import => [qw(is_same_sql_bind diag_where dumper) ]; |
fffe6900 |
7 | |
32eab2da |
8 | use SQL::Abstract; |
9 | |
5ebfe5f2 |
10 | my $not_stringifiable = bless {}, 'SQLA::NotStringifiable'; |
ba566dc3 |
11 | |
c59982bb |
12 | my @handle_tests = ( |
32eab2da |
13 | { |
3abc59be |
14 | where => 'foo', |
15 | order => [], |
16 | stmt => ' WHERE foo', |
17 | bind => [], |
18 | }, |
19 | { |
32eab2da |
20 | where => { |
21 | requestor => 'inna', |
22 | worker => ['nwiger', 'rcwe', 'sfz'], |
23 | status => { '!=', 'completed' } |
24 | }, |
25 | order => [], |
26 | stmt => " WHERE ( requestor = ? AND status != ? AND ( ( worker = ? ) OR" |
27 | . " ( worker = ? ) OR ( worker = ? ) ) )", |
28 | bind => [qw/inna completed nwiger rcwe sfz/], |
29 | }, |
30 | |
31 | { |
73a67e2c |
32 | where => [ |
33 | status => 'completed', |
34 | user => 'nwiger', |
35 | ], |
36 | stmt => " WHERE ( status = ? OR user = ? )", |
37 | bind => [qw/completed nwiger/], |
38 | }, |
39 | |
40 | { |
32eab2da |
41 | where => { |
42 | user => 'nwiger', |
43 | status => 'completed' |
44 | }, |
45 | order => [qw/ticket/], |
46 | stmt => " WHERE ( status = ? AND user = ? ) ORDER BY ticket", |
47 | bind => [qw/completed nwiger/], |
48 | }, |
49 | |
50 | { |
51 | where => { |
52 | user => 'nwiger', |
53 | status => { '!=', 'completed' } |
54 | }, |
55 | order => [qw/ticket/], |
56 | stmt => " WHERE ( status != ? AND user = ? ) ORDER BY ticket", |
57 | bind => [qw/completed nwiger/], |
58 | }, |
59 | |
60 | { |
61 | where => { |
62 | status => 'completed', |
63 | reportid => { 'in', [567, 2335, 2] } |
64 | }, |
65 | order => [], |
66 | stmt => " WHERE ( reportid IN ( ?, ?, ? ) AND status = ? )", |
67 | bind => [qw/567 2335 2 completed/], |
68 | }, |
69 | |
70 | { |
71 | where => { |
72 | status => 'completed', |
73 | reportid => { 'not in', [567, 2335, 2] } |
74 | }, |
75 | order => [], |
76 | stmt => " WHERE ( reportid NOT IN ( ?, ?, ? ) AND status = ? )", |
77 | bind => [qw/567 2335 2 completed/], |
78 | }, |
79 | |
80 | { |
81 | where => { |
82 | status => 'completed', |
83 | completion_date => { 'between', ['2002-10-01', '2003-02-06'] }, |
84 | }, |
85 | order => \'ticket, requestor', |
e30faf88 |
86 | stmt => "WHERE ( ( completion_date BETWEEN ? AND ? ) AND status = ? ) ORDER BY ticket, requestor", |
32eab2da |
87 | bind => [qw/2002-10-01 2003-02-06 completed/], |
88 | }, |
89 | |
90 | { |
91 | where => [ |
92 | { |
93 | user => 'nwiger', |
94 | status => { 'in', ['pending', 'dispatched'] }, |
95 | }, |
96 | { |
97 | user => 'robot', |
98 | status => 'unassigned', |
99 | }, |
100 | ], |
101 | order => [], |
102 | stmt => " WHERE ( ( status IN ( ?, ? ) AND user = ? ) OR ( status = ? AND user = ? ) )", |
103 | bind => [qw/pending dispatched nwiger unassigned robot/], |
104 | }, |
105 | |
106 | { |
428975b0 |
107 | where => { |
32eab2da |
108 | priority => [ {'>', 3}, {'<', 1} ], |
109 | requestor => \'is not null', |
110 | }, |
111 | order => 'priority', |
112 | stmt => " WHERE ( ( ( priority > ? ) OR ( priority < ? ) ) AND requestor is not null ) ORDER BY priority", |
113 | bind => [qw/3 1/], |
114 | }, |
115 | |
116 | { |
428975b0 |
117 | where => { |
bd6a65ca |
118 | requestor => { '!=', ['-and', undef, ''] }, |
119 | }, |
120 | stmt => " WHERE ( requestor IS NOT NULL AND requestor != ? )", |
121 | bind => [''], |
122 | }, |
123 | |
124 | { |
428975b0 |
125 | where => { |
50aaa4e3 |
126 | requestor => [undef, ''], |
127 | }, |
128 | stmt => " WHERE ( requestor IS NULL OR requestor = ? )", |
129 | bind => [''], |
130 | }, |
131 | |
132 | { |
133 | where => { |
32eab2da |
134 | priority => [ {'>', 3}, {'<', 1} ], |
428975b0 |
135 | requestor => { '!=', undef }, |
32eab2da |
136 | }, |
137 | order => [qw/a b c d e f g/], |
138 | stmt => " WHERE ( ( ( priority > ? ) OR ( priority < ? ) ) AND requestor IS NOT NULL )" |
139 | . " ORDER BY a, b, c, d, e, f, g", |
140 | bind => [qw/3 1/], |
141 | }, |
142 | |
143 | { |
428975b0 |
144 | where => { |
32eab2da |
145 | priority => { 'between', [1, 3] }, |
428975b0 |
146 | requestor => { 'like', undef }, |
32eab2da |
147 | }, |
148 | order => \'requestor, ticket', |
96449e8e |
149 | stmt => " WHERE ( ( priority BETWEEN ? AND ? ) AND requestor IS NULL ) ORDER BY requestor, ticket", |
32eab2da |
150 | bind => [qw/1 3/], |
3cdadcbe |
151 | warns => qr/Supplying an undefined argument to 'LIKE' is deprecated/, |
32eab2da |
152 | }, |
153 | |
154 | |
155 | { |
428975b0 |
156 | where => { |
157 | id => 1, |
158 | num => { |
159 | '<=' => 20, |
160 | '>' => 10, |
161 | }, |
32eab2da |
162 | }, |
96449e8e |
163 | stmt => " WHERE ( id = ? AND ( num <= ? AND num > ? ) )", |
32eab2da |
164 | bind => [qw/1 20 10/], |
165 | }, |
166 | |
167 | { |
168 | where => { foo => {-not_like => [7,8,9]}, |
169 | fum => {'like' => [qw/a b/]}, |
170 | nix => {'between' => [100,200] }, |
171 | nox => {'not between' => [150,160] }, |
172 | wix => {'in' => [qw/zz yy/]}, |
173 | wux => {'not_in' => [qw/30 40/]} |
174 | }, |
f2d5020d |
175 | stmt => " WHERE ( ( ( foo NOT LIKE ? ) OR ( foo NOT LIKE ? ) OR ( foo NOT LIKE ? ) ) AND ( ( fum LIKE ? ) OR ( fum LIKE ? ) ) AND ( nix BETWEEN ? AND ? ) AND ( nox NOT BETWEEN ? AND ? ) AND wix IN ( ?, ? ) AND wux NOT IN ( ?, ? ) )", |
32eab2da |
176 | bind => [7,8,9,'a','b',100,200,150,160,'zz','yy','30','40'], |
3cdadcbe |
177 | warns => qr/\QA multi-element arrayref as an argument to the inequality op 'NOT LIKE' is technically equivalent to an always-true 1=1/, |
32eab2da |
178 | }, |
179 | |
8a68b5be |
180 | { |
181 | where => { |
8a68b5be |
182 | bar => {'!=' => []}, |
183 | }, |
385ded7f |
184 | stmt => " WHERE ( 1=1 )", |
185 | bind => [], |
186 | }, |
187 | |
188 | { |
189 | where => { |
190 | id => [], |
191 | }, |
192 | stmt => " WHERE ( 0=1 )", |
8a68b5be |
193 | bind => [], |
194 | }, |
195 | |
96449e8e |
196 | |
197 | { |
198 | where => { |
199 | foo => \["IN (?, ?)", 22, 33], |
200 | bar => [-and => \["> ?", 44], \["< ?", 55] ], |
201 | }, |
202 | stmt => " WHERE ( (bar > ? AND bar < ?) AND foo IN (?, ?) )", |
203 | bind => [44, 55, 22, 33], |
204 | }, |
48d9f5f8 |
205 | |
206 | { |
207 | where => { |
208 | -and => [ |
209 | user => 'nwiger', |
210 | [ |
211 | -and => [ workhrs => {'>', 20}, geo => 'ASIA' ], |
212 | -or => { workhrs => {'<', 50}, geo => 'EURO' }, |
213 | ], |
214 | ], |
215 | }, |
216 | stmt => "WHERE ( user = ? AND ( |
217 | ( workhrs > ? AND geo = ? ) |
218 | OR ( geo = ? OR workhrs < ? ) |
219 | ) )", |
220 | bind => [qw/nwiger 20 ASIA EURO 50/], |
221 | }, |
222 | |
4b7b6026 |
223 | { |
224 | where => { -and => [{}, { 'me.id' => '1'}] }, |
225 | stmt => " WHERE ( ( me.id = ? ) )", |
226 | bind => [ 1 ], |
227 | }, |
228 | |
fffe6900 |
229 | { |
ba566dc3 |
230 | where => { foo => $not_stringifiable, }, |
231 | stmt => " WHERE ( foo = ? )", |
232 | bind => [ $not_stringifiable ], |
233 | }, |
234 | |
73a67e2c |
235 | { |
474e3335 |
236 | where => \[ 'foo = ?','bar' ], |
c59982bb |
237 | stmt => " WHERE (foo = ?)", |
73a67e2c |
238 | bind => [ "bar" ], |
239 | }, |
240 | |
241 | { |
474e3335 |
242 | where => [ \[ 'foo = ?','bar' ] ], |
c59982bb |
243 | stmt => " WHERE (foo = ?)", |
73a67e2c |
244 | bind => [ "bar" ], |
245 | }, |
97a920ef |
246 | |
247 | { |
248 | where => { -bool => \'function(x)' }, |
249 | stmt => " WHERE function(x)", |
250 | bind => [], |
251 | }, |
252 | |
253 | { |
254 | where => { -bool => 'foo' }, |
255 | stmt => " WHERE foo", |
256 | bind => [], |
257 | }, |
258 | |
259 | { |
260 | where => { -and => [-bool => 'foo', -bool => 'bar'] }, |
261 | stmt => " WHERE foo AND bar", |
262 | bind => [], |
263 | }, |
264 | |
265 | { |
266 | where => { -or => [-bool => 'foo', -bool => 'bar'] }, |
267 | stmt => " WHERE foo OR bar", |
268 | bind => [], |
269 | }, |
270 | |
271 | { |
272 | where => { -not_bool => \'function(x)' }, |
273 | stmt => " WHERE NOT function(x)", |
274 | bind => [], |
275 | }, |
276 | |
277 | { |
278 | where => { -not_bool => 'foo' }, |
279 | stmt => " WHERE NOT foo", |
280 | bind => [], |
281 | }, |
282 | |
283 | { |
284 | where => { -and => [-not_bool => 'foo', -not_bool => 'bar'] }, |
ef03f1bc |
285 | stmt => " WHERE (NOT foo) AND (NOT bar)", |
97a920ef |
286 | bind => [], |
287 | }, |
288 | |
289 | { |
290 | where => { -or => [-not_bool => 'foo', -not_bool => 'bar'] }, |
ef03f1bc |
291 | stmt => " WHERE (NOT foo) OR (NOT bar)", |
97a920ef |
292 | bind => [], |
293 | }, |
294 | |
ef03f1bc |
295 | { |
296 | where => { -bool => \['function(?)', 20] }, |
297 | stmt => " WHERE function(?)", |
298 | bind => [20], |
299 | }, |
300 | |
301 | { |
302 | where => { -not_bool => \['function(?)', 20] }, |
303 | stmt => " WHERE NOT function(?)", |
304 | bind => [20], |
305 | }, |
306 | |
307 | { |
308 | where => { -bool => { a => 1, b => 2} }, |
309 | stmt => " WHERE a = ? AND b = ?", |
310 | bind => [1, 2], |
311 | }, |
312 | |
313 | { |
314 | where => { -bool => [ a => 1, b => 2] }, |
315 | stmt => " WHERE a = ? OR b = ?", |
316 | bind => [1, 2], |
317 | }, |
318 | |
319 | { |
320 | where => { -not_bool => { a => 1, b => 2} }, |
321 | stmt => " WHERE NOT (a = ? AND b = ?)", |
322 | bind => [1, 2], |
323 | }, |
324 | |
325 | { |
326 | where => { -not_bool => [ a => 1, b => 2] }, |
327 | stmt => " WHERE NOT ( a = ? OR b = ? )", |
328 | bind => [1, 2], |
329 | }, |
330 | |
63cb75ac |
331 | # Op against internal function |
332 | { |
333 | where => { bool1 => { '=' => { -not_bool => 'bool2' } } }, |
2281c758 |
334 | stmt => " WHERE ( bool1 = (NOT bool2) )", |
63cb75ac |
335 | bind => [], |
336 | }, |
337 | { |
338 | where => { -not_bool => { -not_bool => { -not_bool => 'bool2' } } }, |
339 | stmt => " WHERE ( NOT ( NOT ( NOT bool2 ) ) )", |
340 | bind => [], |
341 | }, |
342 | |
343 | # Op against random functions (these two are oracle-specific) |
344 | { |
953d164e |
345 | where => { timestamp => { '!=' => { -trunc => { -year => \'sysdate' } } } }, |
336bff32 |
346 | stmt => " WHERE ( timestamp != TRUNC(YEAR(sysdate)) )", |
63cb75ac |
347 | bind => [], |
348 | }, |
349 | { |
b8db59b8 |
350 | where => { timestamp => { '>=' => { -to_date => '2009-12-21 00:00:00' } } }, |
336bff32 |
351 | stmt => " WHERE ( timestamp >= TO_DATE(?) )", |
63cb75ac |
352 | bind => ['2009-12-21 00:00:00'], |
353 | }, |
354 | |
953d164e |
355 | # Legacy function specs |
356 | { |
357 | where => { ip => {'<<=' => '127.0.0.1/32' } }, |
358 | stmt => "WHERE ( ip <<= ? )", |
359 | bind => ['127.0.0.1/32'], |
360 | }, |
361 | { |
362 | where => { foo => { 'GLOB' => '*str*' } }, |
363 | stmt => " WHERE foo GLOB ? ", |
364 | bind => [ '*str*' ], |
365 | }, |
366 | { |
367 | where => { foo => { 'REGEXP' => 'bar|baz' } }, |
368 | stmt => " WHERE foo REGEXP ? ", |
369 | bind => [ 'bar|baz' ], |
370 | }, |
e24e3012 |
371 | |
372 | # Tests for -not |
373 | # Basic tests only |
374 | { |
375 | where => { -not => { a => 1 } }, |
376 | stmt => " WHERE ( (NOT a = ?) ) ", |
377 | bind => [ 1 ], |
378 | }, |
379 | { |
380 | where => { a => 1, -not => { b => 2 } }, |
381 | stmt => " WHERE ( ( (NOT b = ?) AND a = ? ) ) ", |
382 | bind => [ 2, 1 ], |
383 | }, |
384 | { |
385 | where => { -not => { a => 1, b => 2, c => 3 } }, |
386 | stmt => " WHERE ( (NOT ( a = ? AND b = ? AND c = ? )) ) ", |
387 | bind => [ 1, 2, 3 ], |
388 | }, |
389 | { |
390 | where => { -not => [ a => 1, b => 2, c => 3 ] }, |
391 | stmt => " WHERE ( (NOT ( a = ? OR b = ? OR c = ? )) ) ", |
392 | bind => [ 1, 2, 3 ], |
393 | }, |
394 | { |
395 | where => { -not => { c => 3, -not => { b => 2, -not => { a => 1 } } } }, |
396 | stmt => " WHERE ( (NOT ( (NOT ( (NOT a = ?) AND b = ? )) AND c = ? )) ) ", |
397 | bind => [ 1, 2, 3 ], |
398 | }, |
399 | { |
400 | where => { -not => { -bool => 'c', -not => { -not_bool => 'b', -not => { a => 1 } } } }, |
401 | stmt => " WHERE ( (NOT ( c AND (NOT ( (NOT a = ?) AND (NOT b) )) )) ) ", |
402 | bind => [ 1 ], |
403 | }, |
417dd15e |
404 | { |
405 | where => \"0", |
406 | stmt => " WHERE ( 0 ) ", |
407 | bind => [ ], |
408 | }, |
97a32cd3 |
409 | { |
410 | where => { artistid => {} }, |
411 | stmt => '', |
412 | bind => [ ], |
3df759f8 |
413 | }, |
414 | { |
415 | where => [ -and => [ {}, [] ], -or => [ {}, [] ] ], |
416 | stmt => '', |
417 | bind => [ ], |
418 | }, |
ab0877ff |
419 | { |
420 | where => { '=' => \'bozz' }, |
421 | stmt => 'WHERE = bozz', |
422 | bind => [ ], |
423 | }, |
32eab2da |
424 | ); |
425 | |
8a68b5be |
426 | for my $case (@handle_tests) { |
32eab2da |
427 | my $sql = SQL::Abstract->new; |
3cdadcbe |
428 | my ($stmt, @bind); |
4c77e2d4 |
429 | lives_ok { |
50aaa4e3 |
430 | warnings_like { |
4c77e2d4 |
431 | ($stmt, @bind) = $sql->where($case->{where}, $case->{order}); |
432 | } $case->{warns} || []; |
433 | }; |
3cdadcbe |
434 | |
2fadf08e |
435 | is_same_sql_bind($stmt, \@bind, $case->{stmt}, $case->{bind}) |
4c77e2d4 |
436 | || do { diag_where ( $case->{where} ); diag dumper($sql->_expand_expr($case->{where})) }; |
32eab2da |
437 | } |
438 | |
10e6c946 |
439 | done_testing; |