7 use SQL::Abstract::Test import => ['is_same_sql_bind'];
12 # Make sure to test the examples, since having them break is somewhat
15 my $not_stringifiable = bless {}, 'SQLA::NotStringifiable';
21 worker => ['nwiger', 'rcwe', 'sfz'],
22 status => { '!=', 'completed' }
25 stmt => " WHERE ( requestor = ? AND status != ? AND ( ( worker = ? ) OR"
26 . " ( worker = ? ) OR ( worker = ? ) ) )",
27 bind => [qw/inna completed nwiger rcwe sfz/],
32 status => 'completed',
35 stmt => " WHERE ( status = ? OR user = ? )",
36 bind => [qw/completed nwiger/],
44 order => [qw/ticket/],
45 stmt => " WHERE ( status = ? AND user = ? ) ORDER BY ticket",
46 bind => [qw/completed nwiger/],
52 status => { '!=', 'completed' }
54 order => [qw/ticket/],
55 stmt => " WHERE ( status != ? AND user = ? ) ORDER BY ticket",
56 bind => [qw/completed nwiger/],
61 status => 'completed',
62 reportid => { 'in', [567, 2335, 2] }
65 stmt => " WHERE ( reportid IN ( ?, ?, ? ) AND status = ? )",
66 bind => [qw/567 2335 2 completed/],
71 status => 'completed',
72 reportid => { 'not in', [567, 2335, 2] }
75 stmt => " WHERE ( reportid NOT IN ( ?, ?, ? ) AND status = ? )",
76 bind => [qw/567 2335 2 completed/],
81 status => 'completed',
82 completion_date => { 'between', ['2002-10-01', '2003-02-06'] },
84 order => \'ticket, requestor',
85 #LDNOTE: modified parentheses
88 stmt => "WHERE ( ( completion_date BETWEEN ? AND ? ) AND status = ? ) ORDER BY ticket, requestor",
89 bind => [qw/2002-10-01 2003-02-06 completed/],
96 status => { 'in', ['pending', 'dispatched'] },
100 status => 'unassigned',
104 stmt => " WHERE ( ( status IN ( ?, ? ) AND user = ? ) OR ( status = ? AND user = ? ) )",
105 bind => [qw/pending dispatched nwiger unassigned robot/],
110 priority => [ {'>', 3}, {'<', 1} ],
111 requestor => \'is not null',
114 stmt => " WHERE ( ( ( priority > ? ) OR ( priority < ? ) ) AND requestor is not null ) ORDER BY priority",
120 requestor => { '!=', ['-and', undef, ''] },
122 stmt => " WHERE ( requestor IS NOT NULL AND requestor != ? )",
128 priority => [ {'>', 3}, {'<', 1} ],
129 requestor => { '!=', undef },
131 order => [qw/a b c d e f g/],
132 stmt => " WHERE ( ( ( priority > ? ) OR ( priority < ? ) ) AND requestor IS NOT NULL )"
133 . " ORDER BY a, b, c, d, e, f, g",
139 priority => { 'between', [1, 3] },
140 requestor => { 'like', undef },
142 order => \'requestor, ticket',
143 #LDNOTE: modified parentheses
146 stmt => " WHERE ( ( priority BETWEEN ? AND ? ) AND requestor IS NULL ) ORDER BY requestor, ticket",
159 # LDNOTE : modified test below, just parentheses differ
162 stmt => " WHERE ( id = ? AND ( num <= ? AND num > ? ) )",
163 bind => [qw/1 20 10/],
167 # LDNOTE 23.03.09 : modified test below, just parentheses differ
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'],
183 stmt => " WHERE ( 1=1 )",
191 stmt => " WHERE ( 0=1 )",
198 foo => \["IN (?, ?)", 22, 33],
199 bar => [-and => \["> ?", 44], \["< ?", 55] ],
201 stmt => " WHERE ( (bar > ? AND bar < ?) AND foo IN (?, ?) )",
202 bind => [44, 55, 22, 33],
210 -and => [ workhrs => {'>', 20}, geo => 'ASIA' ],
211 -or => { workhrs => {'<', 50}, geo => 'EURO' },
215 stmt => "WHERE ( user = ? AND (
216 ( workhrs > ? AND geo = ? )
217 OR ( geo = ? OR workhrs < ? )
219 bind => [qw/nwiger 20 ASIA EURO 50/],
223 where => { -and => [{}, { 'me.id' => '1'}] },
224 stmt => " WHERE ( ( me.id = ? ) )",
229 where => { foo => $not_stringifiable, },
230 stmt => " WHERE ( foo = ? )",
231 bind => [ $not_stringifiable ],
235 where => \[ 'foo = ?','bar' ],
236 stmt => " WHERE (foo = ?)",
241 where => [ \[ 'foo = ?','bar' ] ],
242 stmt => " WHERE (foo = ?)",
247 where => { -bool => \'function(x)' },
248 stmt => " WHERE function(x)",
253 where => { -bool => 'foo' },
254 stmt => " WHERE foo",
259 where => { -and => [-bool => 'foo', -bool => 'bar'] },
260 stmt => " WHERE foo AND bar",
265 where => { -or => [-bool => 'foo', -bool => 'bar'] },
266 stmt => " WHERE foo OR bar",
271 where => { -not_bool => \'function(x)' },
272 stmt => " WHERE NOT function(x)",
277 where => { -not_bool => 'foo' },
278 stmt => " WHERE NOT foo",
283 where => { -and => [-not_bool => 'foo', -not_bool => 'bar'] },
284 stmt => " WHERE (NOT foo) AND (NOT bar)",
289 where => { -or => [-not_bool => 'foo', -not_bool => 'bar'] },
290 stmt => " WHERE (NOT foo) OR (NOT bar)",
295 where => { -bool => \['function(?)', 20] },
296 stmt => " WHERE function(?)",
301 where => { -not_bool => \['function(?)', 20] },
302 stmt => " WHERE NOT function(?)",
307 where => { -bool => { a => 1, b => 2} },
308 stmt => " WHERE a = ? AND b = ?",
313 where => { -bool => [ a => 1, b => 2] },
314 stmt => " WHERE a = ? OR b = ?",
319 where => { -not_bool => { a => 1, b => 2} },
320 stmt => " WHERE NOT (a = ? AND b = ?)",
325 where => { -not_bool => [ a => 1, b => 2] },
326 stmt => " WHERE NOT ( a = ? OR b = ? )",
330 # Op against internal function
332 where => { bool1 => { '=' => { -not_bool => 'bool2' } } },
333 stmt => " WHERE ( bool1 = (NOT bool2) )",
337 where => { -not_bool => { -not_bool => { -not_bool => 'bool2' } } },
338 stmt => " WHERE ( NOT ( NOT ( NOT bool2 ) ) )",
342 # Op against random functions (these two are oracle-specific)
344 where => { timestamp => { '!=' => { -trunc => { -year => \'sysdate' } } } },
345 stmt => " WHERE ( timestamp != TRUNC (YEAR sysdate) )",
349 where => { timestamp => { '>=' => { -to_date => '2009-12-21 00:00:00' } } },
350 stmt => " WHERE ( timestamp >= TO_DATE(?) )",
351 bind => ['2009-12-21 00:00:00'],
354 # Legacy function specs
356 where => { ip => {'<<=' => '127.0.0.1/32' } },
357 stmt => "WHERE ( ip <<= ? )",
358 bind => ['127.0.0.1/32'],
361 where => { foo => { 'GLOB' => '*str*' } },
362 stmt => " WHERE foo GLOB ? ",
366 where => { foo => { 'REGEXP' => 'bar|baz' } },
367 stmt => " WHERE foo REGEXP ? ",
368 bind => [ 'bar|baz' ],
374 where => { -not => { a => 1 } },
375 stmt => " WHERE ( (NOT a = ?) ) ",
379 where => { a => 1, -not => { b => 2 } },
380 stmt => " WHERE ( ( (NOT b = ?) AND a = ? ) ) ",
384 where => { -not => { a => 1, b => 2, c => 3 } },
385 stmt => " WHERE ( (NOT ( a = ? AND b = ? AND c = ? )) ) ",
389 where => { -not => [ a => 1, b => 2, c => 3 ] },
390 stmt => " WHERE ( (NOT ( a = ? OR b = ? OR c = ? )) ) ",
394 where => { -not => { c => 3, -not => { b => 2, -not => { a => 1 } } } },
395 stmt => " WHERE ( (NOT ( (NOT ( (NOT a = ?) AND b = ? )) AND c = ? )) ) ",
399 where => { -not => { -bool => 'c', -not => { -not_bool => 'b', -not => { a => 1 } } } },
400 stmt => " WHERE ( (NOT ( c AND (NOT ( (NOT a = ?) AND (NOT b) )) )) ) ",
404 where => { foo => { '>=', [] } },
405 stmt => " WHERE 0=1",
410 for my $case (@handle_tests) {
411 local $Data::Dumper::Terse = 1;
412 my $sql = SQL::Abstract->new;
414 ok(!(my $e = exception {
415 ($stmt, @bind) = $sql->where($case->{where}, $case->{order});
416 is_same_sql_bind($stmt, \@bind, $case->{stmt}, $case->{bind})
417 || diag "Search term:\n" . Dumper $case->{where};
420 fail "Died: $e: Search term:\n" . Dumper $case->{where};