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",
147 stmt => " WHERE ( id = ? AND ( num <= ? AND num > ? ) )",
148 bind => [qw/1 20 10/],
152 where => { foo => {-not_like => [7,8,9]},
153 fum => {'like' => [qw/a b/]},
154 nix => {'between' => [100,200] },
155 nox => {'not between' => [150,160] },
156 wix => {'in' => [qw/zz yy/]},
157 wux => {'not_in' => [qw/30 40/]}
159 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 ( ?, ? ) )",
160 bind => [7,8,9,'a','b',100,200,150,160,'zz','yy','30','40'],
167 stmt => " WHERE ( 1=1 )",
175 stmt => " WHERE ( 0=1 )",
182 foo => \["IN (?, ?)", 22, 33],
183 bar => [-and => \["> ?", 44], \["< ?", 55] ],
185 stmt => " WHERE ( (bar > ? AND bar < ?) AND foo IN (?, ?) )",
186 bind => [44, 55, 22, 33],
194 -and => [ workhrs => {'>', 20}, geo => 'ASIA' ],
195 -or => { workhrs => {'<', 50}, geo => 'EURO' },
199 stmt => "WHERE ( user = ? AND (
200 ( workhrs > ? AND geo = ? )
201 OR ( geo = ? OR workhrs < ? )
203 bind => [qw/nwiger 20 ASIA EURO 50/],
207 where => { -and => [{}, { 'me.id' => '1'}] },
208 stmt => " WHERE ( ( me.id = ? ) )",
213 where => { foo => $not_stringifiable, },
214 stmt => " WHERE ( foo = ? )",
215 bind => [ $not_stringifiable ],
219 where => \[ 'foo = ?','bar' ],
220 stmt => " WHERE (foo = ?)",
225 where => [ \[ 'foo = ?','bar' ] ],
226 stmt => " WHERE (foo = ?)",
231 where => { -bool => \'function(x)' },
232 stmt => " WHERE function(x)",
237 where => { -bool => 'foo' },
238 stmt => " WHERE foo",
243 where => { -and => [-bool => 'foo', -bool => 'bar'] },
244 stmt => " WHERE foo AND bar",
249 where => { -or => [-bool => 'foo', -bool => 'bar'] },
250 stmt => " WHERE foo OR bar",
255 where => { -not_bool => \'function(x)' },
256 stmt => " WHERE NOT function(x)",
261 where => { -not_bool => 'foo' },
262 stmt => " WHERE NOT foo",
267 where => { -and => [-not_bool => 'foo', -not_bool => 'bar'] },
268 stmt => " WHERE (NOT foo) AND (NOT bar)",
273 where => { -or => [-not_bool => 'foo', -not_bool => 'bar'] },
274 stmt => " WHERE (NOT foo) OR (NOT bar)",
279 where => { -bool => \['function(?)', 20] },
280 stmt => " WHERE function(?)",
285 where => { -not_bool => \['function(?)', 20] },
286 stmt => " WHERE NOT function(?)",
291 where => { -bool => { a => 1, b => 2} },
292 stmt => " WHERE a = ? AND b = ?",
297 where => { -bool => [ a => 1, b => 2] },
298 stmt => " WHERE a = ? OR b = ?",
303 where => { -not_bool => { a => 1, b => 2} },
304 stmt => " WHERE NOT (a = ? AND b = ?)",
309 where => { -not_bool => [ a => 1, b => 2] },
310 stmt => " WHERE NOT ( a = ? OR b = ? )",
314 # Op against internal function
316 where => { bool1 => { '=' => { -not_bool => 'bool2' } } },
317 stmt => " WHERE ( bool1 = (NOT bool2) )",
321 where => { -not_bool => { -not_bool => { -not_bool => 'bool2' } } },
322 stmt => " WHERE ( NOT ( NOT ( NOT bool2 ) ) )",
326 # Op against random functions (these two are oracle-specific)
328 where => { timestamp => { '!=' => { -trunc => { -year => \'sysdate' } } } },
329 stmt => " WHERE ( timestamp != TRUNC (YEAR sysdate) )",
333 where => { timestamp => { '>=' => { -to_date => '2009-12-21 00:00:00' } } },
334 stmt => " WHERE ( timestamp >= TO_DATE ? )",
335 bind => ['2009-12-21 00:00:00'],
338 # Legacy function specs
340 where => { ip => {'<<=' => '127.0.0.1/32' } },
341 stmt => "WHERE ( ip <<= ? )",
342 bind => ['127.0.0.1/32'],
345 where => { foo => { 'GLOB' => '*str*' } },
346 stmt => " WHERE foo GLOB ? ",
350 where => { foo => { 'REGEXP' => 'bar|baz' } },
351 stmt => " WHERE foo REGEXP ? ",
352 bind => [ 'bar|baz' ],
358 where => { -not => { a => 1 } },
359 stmt => " WHERE ( (NOT a = ?) ) ",
363 where => { a => 1, -not => { b => 2 } },
364 stmt => " WHERE ( ( (NOT b = ?) AND a = ? ) ) ",
368 where => { -not => { a => 1, b => 2, c => 3 } },
369 stmt => " WHERE ( (NOT ( a = ? AND b = ? AND c = ? )) ) ",
373 where => { -not => [ a => 1, b => 2, c => 3 ] },
374 stmt => " WHERE ( (NOT ( a = ? OR b = ? OR c = ? )) ) ",
378 where => { -not => { c => 3, -not => { b => 2, -not => { a => 1 } } } },
379 stmt => " WHERE ( (NOT ( (NOT ( (NOT a = ?) AND b = ? )) AND c = ? )) ) ",
383 where => { -not => { -bool => 'c', -not => { -not_bool => 'b', -not => { a => 1 } } } },
384 stmt => " WHERE ( (NOT ( c AND (NOT ( (NOT a = ?) AND (NOT b) )) )) ) ",
389 for my $case (@handle_tests) {
390 my $sql = SQL::Abstract->new;
391 my ($stmt, @bind) = $sql->where($case->{where}, $case->{order});
392 is_same_sql_bind($stmt, \@bind, $case->{stmt}, $case->{bind})
393 || diag_where ( $case->{where} );
397 my $sql = SQL::Abstract->new;
398 $sql->where({ foo => { '>=' => [] }},);