5 use SQL::Abstract::Test import => [qw(is_same_sql_bind diag_where) ];
9 my $not_stringifiable = bless {}, 'SQLA::NotStringifiable';
15 worker => ['nwiger', 'rcwe', 'sfz'],
16 status => { '!=', 'completed' }
19 stmt => " WHERE ( requestor = ? AND status != ? AND ( ( worker = ? ) OR"
20 . " ( worker = ? ) OR ( worker = ? ) ) )",
21 bind => [qw/inna completed nwiger rcwe sfz/],
26 status => 'completed',
29 stmt => " WHERE ( status = ? OR user = ? )",
30 bind => [qw/completed nwiger/],
38 order => [qw/ticket/],
39 stmt => " WHERE ( status = ? AND user = ? ) ORDER BY ticket",
40 bind => [qw/completed nwiger/],
46 status => { '!=', 'completed' }
48 order => [qw/ticket/],
49 stmt => " WHERE ( status != ? AND user = ? ) ORDER BY ticket",
50 bind => [qw/completed nwiger/],
55 status => 'completed',
56 reportid => { 'in', [567, 2335, 2] }
59 stmt => " WHERE ( reportid IN ( ?, ?, ? ) AND status = ? )",
60 bind => [qw/567 2335 2 completed/],
65 status => 'completed',
66 reportid => { 'not in', [567, 2335, 2] }
69 stmt => " WHERE ( reportid NOT IN ( ?, ?, ? ) AND status = ? )",
70 bind => [qw/567 2335 2 completed/],
75 status => 'completed',
76 completion_date => { 'between', ['2002-10-01', '2003-02-06'] },
78 order => \'ticket, requestor',
79 stmt => "WHERE ( ( completion_date BETWEEN ? AND ? ) AND status = ? ) ORDER BY ticket, requestor",
80 bind => [qw/2002-10-01 2003-02-06 completed/],
87 status => { 'in', ['pending', 'dispatched'] },
91 status => 'unassigned',
95 stmt => " WHERE ( ( status IN ( ?, ? ) AND user = ? ) OR ( status = ? AND user = ? ) )",
96 bind => [qw/pending dispatched nwiger unassigned robot/],
101 priority => [ {'>', 3}, {'<', 1} ],
102 requestor => \'is not null',
105 stmt => " WHERE ( ( ( priority > ? ) OR ( priority < ? ) ) AND requestor is not null ) ORDER BY priority",
111 requestor => { '!=', ['-and', undef, ''] },
113 stmt => " WHERE ( requestor IS NOT NULL AND requestor != ? )",
119 priority => [ {'>', 3}, {'<', 1} ],
120 requestor => { '!=', undef },
122 order => [qw/a b c d e f g/],
123 stmt => " WHERE ( ( ( priority > ? ) OR ( priority < ? ) ) AND requestor IS NOT NULL )"
124 . " ORDER BY a, b, c, d, e, f, g",
130 priority => { 'between', [1, 3] },
131 requestor => { 'like', undef },
133 order => \'requestor, ticket',
134 stmt => " WHERE ( ( priority BETWEEN ? AND ? ) AND requestor IS NULL ) ORDER BY requestor, ticket",
136 warns => qr/Supplying an undefined argument to 'LIKE' is deprecated/,
148 stmt => " WHERE ( id = ? AND ( num <= ? AND num > ? ) )",
149 bind => [qw/1 20 10/],
153 where => { foo => {-not_like => [7,8,9]},
154 fum => {'like' => [qw/a b/]},
155 nix => {'between' => [100,200] },
156 nox => {'not between' => [150,160] },
157 wix => {'in' => [qw/zz yy/]},
158 wux => {'not_in' => [qw/30 40/]}
160 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 ( ?, ? ) )",
161 bind => [7,8,9,'a','b',100,200,150,160,'zz','yy','30','40'],
162 warns => qr/\QA multi-element arrayref as an argument to the inequality op 'NOT LIKE' is technically equivalent to an always-true 1=1/,
169 stmt => " WHERE ( 1=1 )",
177 stmt => " WHERE ( 0=1 )",
184 foo => \["IN (?, ?)", 22, 33],
185 bar => [-and => \["> ?", 44], \["< ?", 55] ],
187 stmt => " WHERE ( (bar > ? AND bar < ?) AND foo IN (?, ?) )",
188 bind => [44, 55, 22, 33],
196 -and => [ workhrs => {'>', 20}, geo => 'ASIA' ],
197 -or => { workhrs => {'<', 50}, geo => 'EURO' },
201 stmt => "WHERE ( user = ? AND (
202 ( workhrs > ? AND geo = ? )
203 OR ( geo = ? OR workhrs < ? )
205 bind => [qw/nwiger 20 ASIA EURO 50/],
209 where => { -and => [{}, { 'me.id' => '1'}] },
210 stmt => " WHERE ( ( me.id = ? ) )",
215 where => { foo => $not_stringifiable, },
216 stmt => " WHERE ( foo = ? )",
217 bind => [ $not_stringifiable ],
221 where => \[ 'foo = ?','bar' ],
222 stmt => " WHERE (foo = ?)",
227 where => [ \[ 'foo = ?','bar' ] ],
228 stmt => " WHERE (foo = ?)",
233 where => { -bool => \'function(x)' },
234 stmt => " WHERE function(x)",
239 where => { -bool => 'foo' },
240 stmt => " WHERE foo",
245 where => { -and => [-bool => 'foo', -bool => 'bar'] },
246 stmt => " WHERE foo AND bar",
251 where => { -or => [-bool => 'foo', -bool => 'bar'] },
252 stmt => " WHERE foo OR bar",
257 where => { -not_bool => \'function(x)' },
258 stmt => " WHERE NOT function(x)",
263 where => { -not_bool => 'foo' },
264 stmt => " WHERE NOT foo",
269 where => { -and => [-not_bool => 'foo', -not_bool => 'bar'] },
270 stmt => " WHERE (NOT foo) AND (NOT bar)",
275 where => { -or => [-not_bool => 'foo', -not_bool => 'bar'] },
276 stmt => " WHERE (NOT foo) OR (NOT bar)",
281 where => { -bool => \['function(?)', 20] },
282 stmt => " WHERE function(?)",
287 where => { -not_bool => \['function(?)', 20] },
288 stmt => " WHERE NOT function(?)",
293 where => { -bool => { a => 1, b => 2} },
294 stmt => " WHERE a = ? AND b = ?",
299 where => { -bool => [ a => 1, b => 2] },
300 stmt => " WHERE a = ? OR b = ?",
305 where => { -not_bool => { a => 1, b => 2} },
306 stmt => " WHERE NOT (a = ? AND b = ?)",
311 where => { -not_bool => [ a => 1, b => 2] },
312 stmt => " WHERE NOT ( a = ? OR b = ? )",
316 # Op against internal function
318 where => { bool1 => { '=' => { -not_bool => 'bool2' } } },
319 stmt => " WHERE ( bool1 = (NOT bool2) )",
323 where => { -not_bool => { -not_bool => { -not_bool => 'bool2' } } },
324 stmt => " WHERE ( NOT ( NOT ( NOT bool2 ) ) )",
328 # Op against random functions (these two are oracle-specific)
330 where => { timestamp => { '!=' => { -trunc => { -year => \'sysdate' } } } },
331 stmt => " WHERE ( timestamp != TRUNC (YEAR sysdate) )",
335 where => { timestamp => { '>=' => { -to_date => '2009-12-21 00:00:00' } } },
336 stmt => " WHERE ( timestamp >= TO_DATE ? )",
337 bind => ['2009-12-21 00:00:00'],
340 # Legacy function specs
342 where => { ip => {'<<=' => '127.0.0.1/32' } },
343 stmt => "WHERE ( ip <<= ? )",
344 bind => ['127.0.0.1/32'],
347 where => { foo => { 'GLOB' => '*str*' } },
348 stmt => " WHERE foo GLOB ? ",
352 where => { foo => { 'REGEXP' => 'bar|baz' } },
353 stmt => " WHERE foo REGEXP ? ",
354 bind => [ 'bar|baz' ],
360 where => { -not => { a => 1 } },
361 stmt => " WHERE ( (NOT a = ?) ) ",
365 where => { a => 1, -not => { b => 2 } },
366 stmt => " WHERE ( ( (NOT b = ?) AND a = ? ) ) ",
370 where => { -not => { a => 1, b => 2, c => 3 } },
371 stmt => " WHERE ( (NOT ( a = ? AND b = ? AND c = ? )) ) ",
375 where => { -not => [ a => 1, b => 2, c => 3 ] },
376 stmt => " WHERE ( (NOT ( a = ? OR b = ? OR c = ? )) ) ",
380 where => { -not => { c => 3, -not => { b => 2, -not => { a => 1 } } } },
381 stmt => " WHERE ( (NOT ( (NOT ( (NOT a = ?) AND b = ? )) AND c = ? )) ) ",
385 where => { -not => { -bool => 'c', -not => { -not_bool => 'b', -not => { a => 1 } } } },
386 stmt => " WHERE ( (NOT ( c AND (NOT ( (NOT a = ?) AND (NOT b) )) )) ) ",
391 for my $case (@handle_tests) {
392 my $sql = SQL::Abstract->new;
395 ($stmt, @bind) = $sql->where($case->{where}, $case->{order});
396 } $case->{warns} || [];
398 is_same_sql_bind($stmt, \@bind, $case->{stmt}, $case->{bind})
399 || diag_where ( $case->{where} );