4 use SQL::Abstract::Test import => [qw(is_same_sql_bind diag_where) ];
8 my $not_stringifiable = bless {}, 'SQLA::NotStringifiable';
14 worker => ['nwiger', 'rcwe', 'sfz'],
15 status => { '!=', 'completed' }
18 stmt => " WHERE ( requestor = ? AND status != ? AND ( ( worker = ? ) OR"
19 . " ( worker = ? ) OR ( worker = ? ) ) )",
20 bind => [qw/inna completed nwiger rcwe sfz/],
25 status => 'completed',
28 stmt => " WHERE ( status = ? OR user = ? )",
29 bind => [qw/completed nwiger/],
37 order => [qw/ticket/],
38 stmt => " WHERE ( status = ? AND user = ? ) ORDER BY ticket",
39 bind => [qw/completed nwiger/],
45 status => { '!=', 'completed' }
47 order => [qw/ticket/],
48 stmt => " WHERE ( status != ? AND user = ? ) ORDER BY ticket",
49 bind => [qw/completed nwiger/],
54 status => 'completed',
55 reportid => { 'in', [567, 2335, 2] }
58 stmt => " WHERE ( reportid IN ( ?, ?, ? ) AND status = ? )",
59 bind => [qw/567 2335 2 completed/],
64 status => 'completed',
65 reportid => { 'not in', [567, 2335, 2] }
68 stmt => " WHERE ( reportid NOT IN ( ?, ?, ? ) AND status = ? )",
69 bind => [qw/567 2335 2 completed/],
74 status => 'completed',
75 completion_date => { 'between', ['2002-10-01', '2003-02-06'] },
77 order => \'ticket, requestor',
78 stmt => "WHERE ( ( completion_date BETWEEN ? AND ? ) AND status = ? ) ORDER BY ticket, requestor",
79 bind => [qw/2002-10-01 2003-02-06 completed/],
86 status => { 'in', ['pending', 'dispatched'] },
90 status => 'unassigned',
94 stmt => " WHERE ( ( status IN ( ?, ? ) AND user = ? ) OR ( status = ? AND user = ? ) )",
95 bind => [qw/pending dispatched nwiger unassigned robot/],
100 priority => [ {'>', 3}, {'<', 1} ],
101 requestor => \'is not null',
104 stmt => " WHERE ( ( ( priority > ? ) OR ( priority < ? ) ) AND requestor is not null ) ORDER BY priority",
110 requestor => { '!=', ['-and', undef, ''] },
112 stmt => " WHERE ( requestor IS NOT NULL AND requestor != ? )",
118 priority => [ {'>', 3}, {'<', 1} ],
119 requestor => { '!=', undef },
121 order => [qw/a b c d e f g/],
122 stmt => " WHERE ( ( ( priority > ? ) OR ( priority < ? ) ) AND requestor IS NOT NULL )"
123 . " ORDER BY a, b, c, d, e, f, g",
129 priority => { 'between', [1, 3] },
130 requestor => { 'like', undef },
132 order => \'requestor, ticket',
133 stmt => " WHERE ( ( priority BETWEEN ? AND ? ) AND requestor IS NULL ) ORDER BY requestor, ticket",
146 stmt => " WHERE ( id = ? AND ( num <= ? AND num > ? ) )",
147 bind => [qw/1 20 10/],
151 where => { foo => {-not_like => [7,8,9]},
152 fum => {'like' => [qw/a b/]},
153 nix => {'between' => [100,200] },
154 nox => {'not between' => [150,160] },
155 wix => {'in' => [qw/zz yy/]},
156 wux => {'not_in' => [qw/30 40/]}
158 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 ( ?, ? ) )",
159 bind => [7,8,9,'a','b',100,200,150,160,'zz','yy','30','40'],
166 stmt => " WHERE ( 1=1 )",
174 stmt => " WHERE ( 0=1 )",
181 foo => \["IN (?, ?)", 22, 33],
182 bar => [-and => \["> ?", 44], \["< ?", 55] ],
184 stmt => " WHERE ( (bar > ? AND bar < ?) AND foo IN (?, ?) )",
185 bind => [44, 55, 22, 33],
193 -and => [ workhrs => {'>', 20}, geo => 'ASIA' ],
194 -or => { workhrs => {'<', 50}, geo => 'EURO' },
198 stmt => "WHERE ( user = ? AND (
199 ( workhrs > ? AND geo = ? )
200 OR ( geo = ? OR workhrs < ? )
202 bind => [qw/nwiger 20 ASIA EURO 50/],
206 where => { -and => [{}, { 'me.id' => '1'}] },
207 stmt => " WHERE ( ( me.id = ? ) )",
212 where => { foo => $not_stringifiable, },
213 stmt => " WHERE ( foo = ? )",
214 bind => [ $not_stringifiable ],
218 where => \[ 'foo = ?','bar' ],
219 stmt => " WHERE (foo = ?)",
224 where => [ \[ 'foo = ?','bar' ] ],
225 stmt => " WHERE (foo = ?)",
230 where => { -bool => \'function(x)' },
231 stmt => " WHERE function(x)",
236 where => { -bool => 'foo' },
237 stmt => " WHERE foo",
242 where => { -and => [-bool => 'foo', -bool => 'bar'] },
243 stmt => " WHERE foo AND bar",
248 where => { -or => [-bool => 'foo', -bool => 'bar'] },
249 stmt => " WHERE foo OR bar",
254 where => { -not_bool => \'function(x)' },
255 stmt => " WHERE NOT function(x)",
260 where => { -not_bool => 'foo' },
261 stmt => " WHERE NOT foo",
266 where => { -and => [-not_bool => 'foo', -not_bool => 'bar'] },
267 stmt => " WHERE (NOT foo) AND (NOT bar)",
272 where => { -or => [-not_bool => 'foo', -not_bool => 'bar'] },
273 stmt => " WHERE (NOT foo) OR (NOT bar)",
278 where => { -bool => \['function(?)', 20] },
279 stmt => " WHERE function(?)",
284 where => { -not_bool => \['function(?)', 20] },
285 stmt => " WHERE NOT function(?)",
290 where => { -bool => { a => 1, b => 2} },
291 stmt => " WHERE a = ? AND b = ?",
296 where => { -bool => [ a => 1, b => 2] },
297 stmt => " WHERE a = ? OR b = ?",
302 where => { -not_bool => { a => 1, b => 2} },
303 stmt => " WHERE NOT (a = ? AND b = ?)",
308 where => { -not_bool => [ a => 1, b => 2] },
309 stmt => " WHERE NOT ( a = ? OR b = ? )",
313 # Op against internal function
315 where => { bool1 => { '=' => { -not_bool => 'bool2' } } },
316 stmt => " WHERE ( bool1 = (NOT bool2) )",
320 where => { -not_bool => { -not_bool => { -not_bool => 'bool2' } } },
321 stmt => " WHERE ( NOT ( NOT ( NOT bool2 ) ) )",
325 # Op against random functions (these two are oracle-specific)
327 where => { timestamp => { '!=' => { -trunc => { -year => \'sysdate' } } } },
328 stmt => " WHERE ( timestamp != TRUNC (YEAR sysdate) )",
332 where => { timestamp => { '>=' => { -to_date => '2009-12-21 00:00:00' } } },
333 stmt => " WHERE ( timestamp >= TO_DATE ? )",
334 bind => ['2009-12-21 00:00:00'],
337 # Legacy function specs
339 where => { ip => {'<<=' => '127.0.0.1/32' } },
340 stmt => "WHERE ( ip <<= ? )",
341 bind => ['127.0.0.1/32'],
344 where => { foo => { 'GLOB' => '*str*' } },
345 stmt => " WHERE foo GLOB ? ",
349 where => { foo => { 'REGEXP' => 'bar|baz' } },
350 stmt => " WHERE foo REGEXP ? ",
351 bind => [ 'bar|baz' ],
357 where => { -not => { a => 1 } },
358 stmt => " WHERE ( (NOT a = ?) ) ",
362 where => { a => 1, -not => { b => 2 } },
363 stmt => " WHERE ( ( (NOT b = ?) AND a = ? ) ) ",
367 where => { -not => { a => 1, b => 2, c => 3 } },
368 stmt => " WHERE ( (NOT ( a = ? AND b = ? AND c = ? )) ) ",
372 where => { -not => [ a => 1, b => 2, c => 3 ] },
373 stmt => " WHERE ( (NOT ( a = ? OR b = ? OR c = ? )) ) ",
377 where => { -not => { c => 3, -not => { b => 2, -not => { a => 1 } } } },
378 stmt => " WHERE ( (NOT ( (NOT ( (NOT a = ?) AND b = ? )) AND c = ? )) ) ",
382 where => { -not => { -bool => 'c', -not => { -not_bool => 'b', -not => { a => 1 } } } },
383 stmt => " WHERE ( (NOT ( c AND (NOT ( (NOT a = ?) AND (NOT b) )) )) ) ",
388 for my $case (@handle_tests) {
389 my $sql = SQL::Abstract->new;
390 my ($stmt, @bind) = $sql->where($case->{where}, $case->{order});
391 is_same_sql_bind($stmt, \@bind, $case->{stmt}, $case->{bind})
392 || diag_where ( $case->{where} );