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],
205 where => { -and => [{}, { 'me.id' => '1'}] },
206 stmt => " WHERE ( ( me.id = ? ) )",
211 where => { foo => $not_stringifiable, },
212 stmt => " WHERE ( foo = ? )",
213 bind => [ $not_stringifiable ],
217 where => \[ 'foo = ?','bar' ],
218 stmt => " WHERE (foo = ?)",
223 where => [ \[ 'foo = ?','bar' ] ],
224 stmt => " WHERE (foo = ?)",
229 where => { -bool => \'function(x)' },
230 stmt => " WHERE function(x)",
235 where => { -bool => 'foo' },
236 stmt => " WHERE foo",
241 where => { -and => [-bool => 'foo', -bool => 'bar'] },
242 stmt => " WHERE foo AND bar",
247 where => { -or => [-bool => 'foo', -bool => 'bar'] },
248 stmt => " WHERE foo OR bar",
253 where => { -not_bool => \'function(x)' },
254 stmt => " WHERE NOT function(x)",
259 where => { -not_bool => 'foo' },
260 stmt => " WHERE NOT foo",
265 where => { -and => [-not_bool => 'foo', -not_bool => 'bar'] },
266 stmt => " WHERE (NOT foo) AND (NOT bar)",
271 where => { -or => [-not_bool => 'foo', -not_bool => 'bar'] },
272 stmt => " WHERE (NOT foo) OR (NOT bar)",
277 where => { -bool => \['function(?)', 20] },
278 stmt => " WHERE function(?)",
283 where => { -not_bool => \['function(?)', 20] },
284 stmt => " WHERE NOT function(?)",
289 where => { -bool => { a => 1, b => 2} },
290 stmt => " WHERE a = ? AND b = ?",
295 where => { -bool => [ a => 1, b => 2] },
296 stmt => " WHERE a = ? OR b = ?",
301 where => { -not_bool => { a => 1, b => 2} },
302 stmt => " WHERE NOT (a = ? AND b = ?)",
307 where => { -not_bool => [ a => 1, b => 2] },
308 stmt => " WHERE NOT ( a = ? OR b = ? )",
312 # Op against internal function
314 where => { bool1 => { '=' => { -not_bool => 'bool2' } } },
315 stmt => " WHERE ( bool1 = (NOT bool2) )",
319 where => { -not_bool => { -not_bool => { -not_bool => 'bool2' } } },
320 stmt => " WHERE ( NOT ( NOT ( NOT bool2 ) ) )",
324 # Op against random functions (these two are oracle-specific)
326 where => { timestamp => { '!=' => { -trunc => { -year => \'sysdate' } } } },
327 stmt => " WHERE ( timestamp != TRUNC (YEAR sysdate) )",
331 where => { timestamp => { '>=' => { -TO_DATE => '2009-12-21 00:00:00' } } },
332 stmt => " WHERE ( timestamp >= TO DATE ? )",
333 bind => ['2009-12-21 00:00:00'],
336 # Legacy function specs
338 where => { ip => {'<<=' => '127.0.0.1/32' } },
339 stmt => "WHERE ( ip <<= ? )",
340 bind => ['127.0.0.1/32'],
343 where => { foo => { 'GLOB' => '*str*' } },
344 stmt => " WHERE foo GLOB ? ",
348 where => { foo => { 'REGEXP' => 'bar|baz' } },
349 stmt => " WHERE foo REGEXP ? ",
350 bind => [ 'bar|baz' ],
356 where => { -not => { a => 1 } },
357 stmt => " WHERE ( (NOT a = ?) ) ",
361 where => { a => 1, -not => { b => 2 } },
362 stmt => " WHERE ( ( (NOT b = ?) AND a = ? ) ) ",
366 where => { -not => { a => 1, b => 2, c => 3 } },
367 stmt => " WHERE ( (NOT ( a = ? AND b = ? AND c = ? )) ) ",
371 where => { -not => [ a => 1, b => 2, c => 3 ] },
372 stmt => " WHERE ( (NOT ( a = ? OR b = ? OR c = ? )) ) ",
376 where => { -not => { c => 3, -not => { b => 2, -not => { a => 1 } } } },
377 stmt => " WHERE ( (NOT ( (NOT ( (NOT a = ?) AND b = ? )) AND c = ? )) ) ",
381 where => { -not => { -bool => 'c', -not => { -not_bool => 'b', -not => { a => 1 } } } },
382 stmt => " WHERE ( (NOT ( c AND (NOT ( (NOT a = ?) AND (NOT b) )) )) ) ",
387 plan tests => ( @handle_tests * 2 ) + 1;
389 for my $case (@handle_tests) {
390 local $Data::Dumper::Terse = 1;
391 my $sql = SQL::Abstract->new;
394 ($stmt, @bind) = $sql->where($case->{where}, $case->{order});
395 is_same_sql_bind($stmt, \@bind, $case->{stmt}, $case->{bind})
396 || diag "Search term:\n" . Dumper $case->{where};
401 my $sql = SQL::Abstract->new;
402 $sql->where({ foo => { '>=' => [] }},);