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