6 use SQL::Abstract::Test import => [qw(is_same_sql_bind diag_where dumper) ];
10 my $not_stringifiable = bless {}, 'SQLA::NotStringifiable';
16 worker => ['nwiger', 'rcwe', 'sfz'],
17 status => { '!=', 'completed' }
20 stmt => " WHERE ( requestor = ? AND status != ? AND ( ( worker = ? ) OR"
21 . " ( worker = ? ) OR ( worker = ? ) ) )",
22 bind => [qw/inna completed nwiger rcwe sfz/],
27 status => 'completed',
30 stmt => " WHERE ( status = ? OR user = ? )",
31 bind => [qw/completed nwiger/],
39 order => [qw/ticket/],
40 stmt => " WHERE ( status = ? AND user = ? ) ORDER BY ticket",
41 bind => [qw/completed nwiger/],
47 status => { '!=', 'completed' }
49 order => [qw/ticket/],
50 stmt => " WHERE ( status != ? AND user = ? ) ORDER BY ticket",
51 bind => [qw/completed nwiger/],
56 status => 'completed',
57 reportid => { 'in', [567, 2335, 2] }
60 stmt => " WHERE ( reportid IN ( ?, ?, ? ) AND status = ? )",
61 bind => [qw/567 2335 2 completed/],
66 status => 'completed',
67 reportid => { 'not in', [567, 2335, 2] }
70 stmt => " WHERE ( reportid NOT IN ( ?, ?, ? ) AND status = ? )",
71 bind => [qw/567 2335 2 completed/],
76 status => 'completed',
77 completion_date => { 'between', ['2002-10-01', '2003-02-06'] },
79 order => \'ticket, requestor',
80 stmt => "WHERE ( ( completion_date BETWEEN ? AND ? ) AND status = ? ) ORDER BY ticket, requestor",
81 bind => [qw/2002-10-01 2003-02-06 completed/],
88 status => { 'in', ['pending', 'dispatched'] },
92 status => 'unassigned',
96 stmt => " WHERE ( ( status IN ( ?, ? ) AND user = ? ) OR ( status = ? AND user = ? ) )",
97 bind => [qw/pending dispatched nwiger unassigned robot/],
102 priority => [ {'>', 3}, {'<', 1} ],
103 requestor => \'is not null',
106 stmt => " WHERE ( ( ( priority > ? ) OR ( priority < ? ) ) AND requestor is not null ) ORDER BY priority",
112 requestor => { '!=', ['-and', undef, ''] },
114 stmt => " WHERE ( requestor IS NOT NULL AND requestor != ? )",
120 priority => [ {'>', 3}, {'<', 1} ],
121 requestor => { '!=', undef },
123 order => [qw/a b c d e f g/],
124 stmt => " WHERE ( ( ( priority > ? ) OR ( priority < ? ) ) AND requestor IS NOT NULL )"
125 . " ORDER BY a, b, c, d, e, f, g",
131 priority => { 'between', [1, 3] },
132 requestor => { 'like', undef },
134 order => \'requestor, ticket',
135 stmt => " WHERE ( ( priority BETWEEN ? AND ? ) AND requestor IS NULL ) ORDER BY requestor, ticket",
137 warns => qr/Supplying an undefined argument to 'LIKE' is deprecated/,
149 stmt => " WHERE ( id = ? AND ( num <= ? AND num > ? ) )",
150 bind => [qw/1 20 10/],
154 where => { foo => {-not_like => [7,8,9]},
155 fum => {'like' => [qw/a b/]},
156 nix => {'between' => [100,200] },
157 nox => {'not between' => [150,160] },
158 wix => {'in' => [qw/zz yy/]},
159 wux => {'not_in' => [qw/30 40/]}
161 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 ( ?, ? ) )",
162 bind => [7,8,9,'a','b',100,200,150,160,'zz','yy','30','40'],
163 warns => qr/\QA multi-element arrayref as an argument to the inequality op 'NOT LIKE' is technically equivalent to an always-true 1=1/,
170 stmt => " WHERE ( 1=1 )",
178 stmt => " WHERE ( 0=1 )",
185 foo => \["IN (?, ?)", 22, 33],
186 bar => [-and => \["> ?", 44], \["< ?", 55] ],
188 stmt => " WHERE ( (bar > ? AND bar < ?) AND foo IN (?, ?) )",
189 bind => [44, 55, 22, 33],
197 -and => [ workhrs => {'>', 20}, geo => 'ASIA' ],
198 -or => { workhrs => {'<', 50}, geo => 'EURO' },
202 stmt => "WHERE ( user = ? AND (
203 ( workhrs > ? AND geo = ? )
204 OR ( geo = ? OR workhrs < ? )
206 bind => [qw/nwiger 20 ASIA EURO 50/],
210 where => { -and => [{}, { 'me.id' => '1'}] },
211 stmt => " WHERE ( ( me.id = ? ) )",
216 where => { foo => $not_stringifiable, },
217 stmt => " WHERE ( foo = ? )",
218 bind => [ $not_stringifiable ],
222 where => \[ 'foo = ?','bar' ],
223 stmt => " WHERE (foo = ?)",
228 where => [ \[ 'foo = ?','bar' ] ],
229 stmt => " WHERE (foo = ?)",
234 where => { -bool => \'function(x)' },
235 stmt => " WHERE function(x)",
240 where => { -bool => 'foo' },
241 stmt => " WHERE foo",
246 where => { -and => [-bool => 'foo', -bool => 'bar'] },
247 stmt => " WHERE foo AND bar",
252 where => { -or => [-bool => 'foo', -bool => 'bar'] },
253 stmt => " WHERE foo OR bar",
258 where => { -not_bool => \'function(x)' },
259 stmt => " WHERE NOT function(x)",
264 where => { -not_bool => 'foo' },
265 stmt => " WHERE NOT foo",
270 where => { -and => [-not_bool => 'foo', -not_bool => 'bar'] },
271 stmt => " WHERE (NOT foo) AND (NOT bar)",
276 where => { -or => [-not_bool => 'foo', -not_bool => 'bar'] },
277 stmt => " WHERE (NOT foo) OR (NOT bar)",
282 where => { -bool => \['function(?)', 20] },
283 stmt => " WHERE function(?)",
288 where => { -not_bool => \['function(?)', 20] },
289 stmt => " WHERE NOT function(?)",
294 where => { -bool => { a => 1, b => 2} },
295 stmt => " WHERE a = ? AND b = ?",
300 where => { -bool => [ a => 1, b => 2] },
301 stmt => " WHERE a = ? OR b = ?",
306 where => { -not_bool => { a => 1, b => 2} },
307 stmt => " WHERE NOT (a = ? AND b = ?)",
312 where => { -not_bool => [ a => 1, b => 2] },
313 stmt => " WHERE NOT ( a = ? OR b = ? )",
317 # Op against internal function
319 where => { bool1 => { '=' => { -not_bool => 'bool2' } } },
320 stmt => " WHERE ( bool1 = (NOT bool2) )",
324 where => { -not_bool => { -not_bool => { -not_bool => 'bool2' } } },
325 stmt => " WHERE ( NOT ( NOT ( NOT bool2 ) ) )",
329 # Op against random functions (these two are oracle-specific)
331 where => { timestamp => { '!=' => { -trunc => { -year => \'sysdate' } } } },
332 stmt => " WHERE ( timestamp != TRUNC (YEAR sysdate) )",
336 where => { timestamp => { '>=' => { -to_date => '2009-12-21 00:00:00' } } },
337 stmt => " WHERE ( timestamp >= TO_DATE ? )",
338 bind => ['2009-12-21 00:00:00'],
341 # Legacy function specs
343 where => { ip => {'<<=' => '127.0.0.1/32' } },
344 stmt => "WHERE ( ip <<= ? )",
345 bind => ['127.0.0.1/32'],
348 where => { foo => { 'GLOB' => '*str*' } },
349 stmt => " WHERE foo GLOB ? ",
353 where => { foo => { 'REGEXP' => 'bar|baz' } },
354 stmt => " WHERE foo REGEXP ? ",
355 bind => [ 'bar|baz' ],
361 where => { -not => { a => 1 } },
362 stmt => " WHERE ( (NOT a = ?) ) ",
366 where => { a => 1, -not => { b => 2 } },
367 stmt => " WHERE ( ( (NOT b = ?) AND a = ? ) ) ",
371 where => { -not => { a => 1, b => 2, c => 3 } },
372 stmt => " WHERE ( (NOT ( a = ? AND b = ? AND c = ? )) ) ",
376 where => { -not => [ a => 1, b => 2, c => 3 ] },
377 stmt => " WHERE ( (NOT ( a = ? OR b = ? OR c = ? )) ) ",
381 where => { -not => { c => 3, -not => { b => 2, -not => { a => 1 } } } },
382 stmt => " WHERE ( (NOT ( (NOT ( (NOT a = ?) AND b = ? )) AND c = ? )) ) ",
386 where => { -not => { -bool => 'c', -not => { -not_bool => 'b', -not => { a => 1 } } } },
387 stmt => " WHERE ( (NOT ( c AND (NOT ( (NOT a = ?) AND (NOT b) )) )) ) ",
392 stmt => " WHERE ( 0 ) ",
397 for my $case (@handle_tests) {
398 my $sql = SQL::Abstract->new;
402 ($stmt, @bind) = $sql->where($case->{where}, $case->{order});
403 } $case->{warns} || [];
406 is_same_sql_bind($stmt, \@bind, $case->{stmt}, $case->{bind})
407 || do { diag_where ( $case->{where} ); diag dumper($sql->_expand_expr($case->{where})) };