6 use SQL::Abstract::Test import => [qw(is_same_sql_bind diag_where dumper) ];
10 my $not_stringifiable = bless {}, 'SQLA::NotStringifiable';
22 worker => ['nwiger', 'rcwe', 'sfz'],
23 status => { '!=', 'completed' }
26 stmt => " WHERE ( requestor = ? AND status != ? AND ( ( worker = ? ) OR"
27 . " ( worker = ? ) OR ( worker = ? ) ) )",
28 bind => [qw/inna completed nwiger rcwe sfz/],
33 status => 'completed',
36 stmt => " WHERE ( status = ? OR user = ? )",
37 bind => [qw/completed nwiger/],
45 order => [qw/ticket/],
46 stmt => " WHERE ( status = ? AND user = ? ) ORDER BY ticket",
47 bind => [qw/completed nwiger/],
53 status => { '!=', 'completed' }
55 order => [qw/ticket/],
56 stmt => " WHERE ( status != ? AND user = ? ) ORDER BY ticket",
57 bind => [qw/completed nwiger/],
62 status => 'completed',
63 reportid => { 'in', [567, 2335, 2] }
66 stmt => " WHERE ( reportid IN ( ?, ?, ? ) AND status = ? )",
67 bind => [qw/567 2335 2 completed/],
72 status => 'completed',
73 reportid => { 'not in', [567, 2335, 2] }
76 stmt => " WHERE ( reportid NOT IN ( ?, ?, ? ) AND status = ? )",
77 bind => [qw/567 2335 2 completed/],
82 status => 'completed',
83 completion_date => { 'between', ['2002-10-01', '2003-02-06'] },
85 order => \'ticket, requestor',
86 stmt => "WHERE ( ( completion_date BETWEEN ? AND ? ) AND status = ? ) ORDER BY ticket, requestor",
87 bind => [qw/2002-10-01 2003-02-06 completed/],
94 status => { 'in', ['pending', 'dispatched'] },
98 status => 'unassigned',
102 stmt => " WHERE ( ( status IN ( ?, ? ) AND user = ? ) OR ( status = ? AND user = ? ) )",
103 bind => [qw/pending dispatched nwiger unassigned robot/],
108 priority => [ {'>', 3}, {'<', 1} ],
109 requestor => \'is not null',
112 stmt => " WHERE ( ( ( priority > ? ) OR ( priority < ? ) ) AND requestor is not null ) ORDER BY priority",
118 requestor => { '!=', ['-and', undef, ''] },
120 stmt => " WHERE ( requestor IS NOT NULL AND requestor != ? )",
126 requestor => [undef, ''],
128 stmt => " WHERE ( requestor IS NULL OR requestor = ? )",
134 priority => [ {'>', 3}, {'<', 1} ],
135 requestor => { '!=', undef },
137 order => [qw/a b c d e f g/],
138 stmt => " WHERE ( ( ( priority > ? ) OR ( priority < ? ) ) AND requestor IS NOT NULL )"
139 . " ORDER BY a, b, c, d, e, f, g",
145 priority => { 'between', [1, 3] },
146 requestor => { 'like', undef },
148 order => \'requestor, ticket',
149 stmt => " WHERE ( ( priority BETWEEN ? AND ? ) AND requestor IS NULL ) ORDER BY requestor, ticket",
151 warns => qr/Supplying an undefined argument to 'LIKE' is deprecated/,
163 stmt => " WHERE ( id = ? AND ( num <= ? AND num > ? ) )",
164 bind => [qw/1 20 10/],
168 where => { foo => {-not_like => [7,8,9]},
169 fum => {'like' => [qw/a b/]},
170 nix => {'between' => [100,200] },
171 nox => {'not between' => [150,160] },
172 wix => {'in' => [qw/zz yy/]},
173 wux => {'not_in' => [qw/30 40/]}
175 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 ( ?, ? ) )",
176 bind => [7,8,9,'a','b',100,200,150,160,'zz','yy','30','40'],
177 warns => qr/\QA multi-element arrayref as an argument to the inequality op 'NOT LIKE' is technically equivalent to an always-true 1=1/,
184 stmt => " WHERE ( 1=1 )",
192 stmt => " WHERE ( 0=1 )",
199 foo => \["IN (?, ?)", 22, 33],
200 bar => [-and => \["> ?", 44], \["< ?", 55] ],
202 stmt => " WHERE ( (bar > ? AND bar < ?) AND foo IN (?, ?) )",
203 bind => [44, 55, 22, 33],
211 -and => [ workhrs => {'>', 20}, geo => 'ASIA' ],
212 -or => { workhrs => {'<', 50}, geo => 'EURO' },
216 stmt => "WHERE ( user = ? AND (
217 ( workhrs > ? AND geo = ? )
218 OR ( geo = ? OR workhrs < ? )
220 bind => [qw/nwiger 20 ASIA EURO 50/],
224 where => { -and => [{}, { 'me.id' => '1'}] },
225 stmt => " WHERE ( ( me.id = ? ) )",
230 where => { foo => $not_stringifiable, },
231 stmt => " WHERE ( foo = ? )",
232 bind => [ $not_stringifiable ],
236 where => \[ 'foo = ?','bar' ],
237 stmt => " WHERE (foo = ?)",
242 where => [ \[ 'foo = ?','bar' ] ],
243 stmt => " WHERE (foo = ?)",
248 where => { -bool => \'function(x)' },
249 stmt => " WHERE function(x)",
254 where => { -bool => 'foo' },
255 stmt => " WHERE foo",
260 where => { -and => [-bool => 'foo', -bool => 'bar'] },
261 stmt => " WHERE foo AND bar",
266 where => { -or => [-bool => 'foo', -bool => 'bar'] },
267 stmt => " WHERE foo OR bar",
272 where => { -not_bool => \'function(x)' },
273 stmt => " WHERE NOT function(x)",
278 where => { -not_bool => 'foo' },
279 stmt => " WHERE NOT foo",
284 where => { -and => [-not_bool => 'foo', -not_bool => 'bar'] },
285 stmt => " WHERE (NOT foo) AND (NOT bar)",
290 where => { -or => [-not_bool => 'foo', -not_bool => 'bar'] },
291 stmt => " WHERE (NOT foo) OR (NOT bar)",
296 where => { -bool => \['function(?)', 20] },
297 stmt => " WHERE function(?)",
302 where => { -not_bool => \['function(?)', 20] },
303 stmt => " WHERE NOT function(?)",
308 where => { -bool => { a => 1, b => 2} },
309 stmt => " WHERE a = ? AND b = ?",
314 where => { -bool => [ a => 1, b => 2] },
315 stmt => " WHERE a = ? OR b = ?",
320 where => { -not_bool => { a => 1, b => 2} },
321 stmt => " WHERE NOT (a = ? AND b = ?)",
326 where => { -not_bool => [ a => 1, b => 2] },
327 stmt => " WHERE NOT ( a = ? OR b = ? )",
331 # Op against internal function
333 where => { bool1 => { '=' => { -not_bool => 'bool2' } } },
334 stmt => " WHERE ( bool1 = (NOT bool2) )",
338 where => { -not_bool => { -not_bool => { -not_bool => 'bool2' } } },
339 stmt => " WHERE ( NOT ( NOT ( NOT bool2 ) ) )",
343 # Op against random functions (these two are oracle-specific)
345 where => { timestamp => { '!=' => { -trunc => { -year => \'sysdate' } } } },
346 stmt => " WHERE ( timestamp != TRUNC(YEAR(sysdate)) )",
350 where => { timestamp => { '>=' => { -to_date => '2009-12-21 00:00:00' } } },
351 stmt => " WHERE ( timestamp >= TO_DATE(?) )",
352 bind => ['2009-12-21 00:00:00'],
355 # Legacy function specs
357 where => { ip => {'<<=' => '127.0.0.1/32' } },
358 stmt => "WHERE ( ip <<= ? )",
359 bind => ['127.0.0.1/32'],
362 where => { foo => { 'GLOB' => '*str*' } },
363 stmt => " WHERE foo GLOB ? ",
367 where => { foo => { 'REGEXP' => 'bar|baz' } },
368 stmt => " WHERE foo REGEXP ? ",
369 bind => [ 'bar|baz' ],
375 where => { -not => { a => 1 } },
376 stmt => " WHERE ( (NOT a = ?) ) ",
380 where => { a => 1, -not => { b => 2 } },
381 stmt => " WHERE ( ( (NOT b = ?) AND a = ? ) ) ",
385 where => { -not => { a => 1, b => 2, c => 3 } },
386 stmt => " WHERE ( (NOT ( a = ? AND b = ? AND c = ? )) ) ",
390 where => { -not => [ a => 1, b => 2, c => 3 ] },
391 stmt => " WHERE ( (NOT ( a = ? OR b = ? OR c = ? )) ) ",
395 where => { -not => { c => 3, -not => { b => 2, -not => { a => 1 } } } },
396 stmt => " WHERE ( (NOT ( (NOT ( (NOT a = ?) AND b = ? )) AND c = ? )) ) ",
400 where => { -not => { -bool => 'c', -not => { -not_bool => 'b', -not => { a => 1 } } } },
401 stmt => " WHERE ( (NOT ( c AND (NOT ( (NOT a = ?) AND (NOT b) )) )) ) ",
406 stmt => " WHERE ( 0 ) ",
410 where => { artistid => {} },
415 where => [ -and => [ {}, [] ], -or => [ {}, [] ] ],
420 where => { '=' => \'bozz' },
421 stmt => 'WHERE = bozz',
426 for my $case (@handle_tests) {
427 my $sql = SQL::Abstract->new;
431 ($stmt, @bind) = $sql->where($case->{where}, $case->{order});
432 } $case->{warns} || [];
435 is_same_sql_bind($stmt, \@bind, $case->{stmt}, $case->{bind})
436 || do { diag_where ( $case->{where} ); diag dumper($sql->_expand_expr($case->{where})) };