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