6 use SQL::Abstract::Test import => [qw(is_same_sql_bind diag_where dumper)];
10 my @in_between_tests = (
12 where => { x => { -between => [1, 2] } },
13 stmt => 'WHERE (x BETWEEN ? AND ?)',
15 test => '-between with two placeholders',
18 where => { x => { -between => [\"1", 2] } },
19 stmt => 'WHERE (x BETWEEN 1 AND ?)',
21 test => '-between with one literal sql arg and one placeholder',
24 where => { x => { -between => [1, \"2"] } },
25 stmt => 'WHERE (x BETWEEN ? AND 2)',
27 test => '-between with one placeholder and one literal sql arg',
30 where => { x => { -between => [\'current_date - 1', \'current_date - 0'] } },
31 stmt => 'WHERE (x BETWEEN current_date - 1 AND current_date - 0)',
33 test => '-between with two literal sql arguments',
36 where => { x => { -between => [ \['current_date - ?', 1], \['current_date - ?', 0] ] } },
37 stmt => 'WHERE (x BETWEEN current_date - ? AND current_date - ?)',
39 test => '-between with two literal sql arguments with bind',
42 where => { x => { -between => \['? AND ?', 1, 2] } },
43 stmt => 'WHERE (x BETWEEN ? AND ?)',
45 test => '-between with literal sql with placeholders (\["? AND ?", scalar, scalar])',
48 where => { x => { -between => \["'something' AND ?", 2] } },
49 stmt => "WHERE (x BETWEEN 'something' AND ?)",
51 test => '-between with literal sql with one literal arg and one placeholder (\["\'something\' AND ?", scalar])',
54 where => { x => { -between => \["? AND 'something'", 1] } },
55 stmt => "WHERE (x BETWEEN ? AND 'something')",
57 test => '-between with literal sql with one placeholder and one literal arg (\["? AND \'something\'", scalar])',
60 where => { x => { -between => \"'this' AND 'that'" } },
61 stmt => "WHERE (x BETWEEN 'this' AND 'that')",
63 test => '-between with literal sql with a literal (\"\'this\' AND \'that\'")',
66 # generate a set of invalid -between tests
68 where => { x => { -between => $_ } },
69 test => 'invalid -between args',
70 throws => qr|Operator 'BETWEEN' requires either an arrayref with two defined values or expressions, or a single literal scalarref/arrayref-ref|,
87 start0 => { -between => [ 1, { -upper => 2 } ] },
88 start1 => { -between => \["? AND ?", 1, 2] },
89 start2 => { -between => \"lower(x) AND upper(y)" },
90 start3 => { -between => [
92 \["upper(?)", 'stuff' ],
96 ( start0 BETWEEN ? AND UPPER(?) )
97 AND ( start1 BETWEEN ? AND ? )
98 AND ( start2 BETWEEN lower(x) AND upper(y) )
99 AND ( start3 BETWEEN lower(x) AND upper(?) )
101 bind => [1, 2, 1, 2, 'stuff'],
102 test => '-between POD test',
105 args => { restore_old_unop_handling => 1 },
107 start0 => { -between => [ 1, { -upper => 2 } ] },
108 start1 => { -between => \["? AND ?", 1, 2] },
109 start2 => { -between => \"lower(x) AND upper(y)" },
110 start3 => { -between => [
112 \["upper(?)", 'stuff' ],
116 ( start0 BETWEEN ? AND UPPER ? )
117 AND ( start1 BETWEEN ? AND ? )
118 AND ( start2 BETWEEN lower(x) AND upper(y) )
119 AND ( start3 BETWEEN lower(x) AND upper(?) )
121 bind => [1, 2, 1, 2, 'stuff'],
122 test => '-between POD test',
125 args => { bindtype => 'columns' },
127 start0 => { -between => [ 1, { -upper => 2 } ] },
128 start1 => { -between => \["? AND ?", [ start1 => 1], [start1 => 2] ] },
129 start2 => { -between => \"lower(x) AND upper(y)" },
130 start3 => { -between => [
132 \["upper(?)", [ start3 => 'stuff'] ],
136 ( start0 BETWEEN ? AND UPPER(?) )
137 AND ( start1 BETWEEN ? AND ? )
138 AND ( start2 BETWEEN lower(x) AND upper(y) )
139 AND ( start3 BETWEEN lower(x) AND upper(?) )
146 [ start3 => 'stuff' ],
148 test => '-between POD test',
151 args => { restore_old_unop_handling => 1, bindtype => 'columns' },
153 start0 => { -between => [ 1, { -upper => 2 } ] },
154 start1 => { -between => \["? AND ?", [ start1 => 1], [start1 => 2] ] },
155 start2 => { -between => \"lower(x) AND upper(y)" },
156 start3 => { -between => [
158 \["upper(?)", [ start3 => 'stuff'] ],
162 ( start0 BETWEEN ? AND UPPER ? )
163 AND ( start1 BETWEEN ? AND ? )
164 AND ( start2 BETWEEN lower(x) AND upper(y) )
165 AND ( start3 BETWEEN lower(x) AND upper(?) )
172 [ start3 => 'stuff' ],
174 test => '-between POD test',
177 where => { 'test1.a' => { 'In', ['boom', 'bang'] } },
178 stmt => ' WHERE ( test1.a IN ( ?, ? ) )',
179 bind => ['boom', 'bang'],
180 test => 'In (no dash, initial cap) with qualified column',
183 where => { a => { 'between', ['boom', 'bang'] } },
184 stmt => ' WHERE ( a BETWEEN ? AND ? )',
185 bind => ['boom', 'bang'],
186 test => 'between (no dash) with two placeholders',
190 where => { x => { -in => [ 1 .. 3] } },
191 stmt => "WHERE x IN (?, ?, ?)",
193 test => '-in with an array of scalars',
196 where => { x => { -in => [] } },
199 test => '-in with an empty array',
202 where => { x => { -in => \'( 1,2,lower(y) )' } },
203 stmt => "WHERE x IN ( 1,2,lower(y) )",
205 test => '-in with a literal scalarref',
208 # note that outer parens are opened even though literal was requested below
210 where => { x => { -in => \['( ( ?,?,lower(y) ) )', 1, 2] } },
211 stmt => "WHERE x IN ( ?,?,lower(y) )",
213 test => '-in with a literal arrayrefref',
217 status => { -in => \"(SELECT status_codes\nFROM states)" },
219 stmt => " WHERE status IN ( SELECT status_codes FROM states )",
221 test => '-in multi-line subquery test',
224 # check that the outer paren opener is not too agressive
225 # note: this syntax *is not legal* on SQLite (maybe others)
226 # see end of https://rt.cpan.org/Ticket/Display.html?id=99503
228 where => { foo => { -in => \ '(SELECT 1) UNION (SELECT 2)' } },
229 stmt => 'WHERE foo IN ( (SELECT 1) UNION (SELECT 2) )',
231 test => '-in paren-opening works on balanced pairs only',
236 customer => { -in => \[
237 'SELECT cust_id FROM cust WHERE balance > ?',
240 status => { -in => \'SELECT status_codes FROM states' },
244 customer IN ( SELECT cust_id FROM cust WHERE balance > ? )
245 AND status IN ( SELECT status_codes FROM states )
248 test => '-in POD test',
252 where => { x => { -in => [ \['LOWER(?)', 'A' ], \'LOWER(b)', { -lower => 'c' } ] } },
253 stmt => " WHERE ( x IN ( LOWER(?), LOWER(b), LOWER(?) ) )",
255 test => '-in with an array of function array refs with args',
258 args => { restore_old_unop_handling => 1 },
259 where => { x => { -in => [ \['LOWER(?)', 'A' ], \'LOWER(b)', { -lower => 'c' } ] } },
260 stmt => " WHERE ( x IN ( LOWER(?), LOWER(b), LOWER ? ) )",
262 test => '-in with an array of function array refs with args',
266 \QSQL::Abstract before v1.75 used to generate incorrect SQL \E
267 \Qwhen the -IN operator was given an undef-containing list: \E
268 \Q!!!AUDIT YOUR CODE AND DATA!!! (the upcoming Data::Query-based \E
269 \Qversion of SQL::Abstract will emit the logically correct SQL \E
270 \Qinstead of raising this exception)\E
272 where => { x => { -in => [ 1, undef ] } },
273 stmt => " WHERE ( x IN ( ? ) OR x IS NULL )",
275 test => '-in with undef as an element',
279 \QSQL::Abstract before v1.75 used to generate incorrect SQL \E
280 \Qwhen the -IN operator was given an undef-containing list: \E
281 \Q!!!AUDIT YOUR CODE AND DATA!!! (the upcoming Data::Query-based \E
282 \Qversion of SQL::Abstract will emit the logically correct SQL \E
283 \Qinstead of raising this exception)\E
285 where => { x => { -in => [ 1, undef, 2, 3, undef ] } },
286 stmt => " WHERE ( x IN ( ?, ?, ? ) OR x IS NULL )",
288 test => '-in with multiple undef elements',
291 where => { a => { -in => 42 }, b => { -not_in => 42 } },
292 stmt => ' WHERE a IN ( ? ) AND b NOT IN ( ? )',
294 test => '-in, -not_in with scalar',
297 where => { a => { -in => [] }, b => { -not_in => [] } },
298 stmt => ' WHERE ( 0=1 AND 1=1 )',
300 test => '-in, -not_in with empty arrays',
304 \QSQL::Abstract before v1.75 used to generate incorrect SQL \E
305 \Qwhen the -IN operator was given an undef-containing list: \E
306 \Q!!!AUDIT YOUR CODE AND DATA!!! (the upcoming Data::Query-based \E
307 \Qversion of SQL::Abstract will emit the logically correct SQL \E
308 \Qinstead of raising this exception)\E
310 where => { a => { -in => [42, undef] }, b => { -not_in => [42, undef] } },
311 stmt => ' WHERE ( ( a IN ( ? ) OR a IS NULL ) AND b NOT IN ( ? ) AND b IS NOT NULL )',
313 test => '-in, -not_in with undef among elements',
317 \QSQL::Abstract before v1.75 used to generate incorrect SQL \E
318 \Qwhen the -IN operator was given an undef-containing list: \E
319 \Q!!!AUDIT YOUR CODE AND DATA!!! (the upcoming Data::Query-based \E
320 \Qversion of SQL::Abstract will emit the logically correct SQL \E
321 \Qinstead of raising this exception)\E
323 where => { a => { -in => [undef] }, b => { -not_in => [undef] } },
324 stmt => ' WHERE ( a IS NULL AND b IS NOT NULL )',
326 test => '-in, -not_in with just undef element',
329 where => { a => { -in => undef } },
330 throws => qr/Argument passed to the 'IN' operator can not be undefined/,
331 test => '-in with undef argument',
335 where => { -in => [ 'bob', 4, 2 ] },
336 stmt => ' WHERE (bob IN (?, ?))',
338 test => 'Top level -in',
340 # This works but then SQL::Abstract::Tree breaks - something for a later commit
342 # where => { -in => [ { -list => [ qw(x y) ] }, { -list => [ 1, 3 ] }, { -list => [ 2, 4 ] } ] },
343 # stmt => ' WHERE ((x, y) IN ((?, ?), (?, ?))',
344 # bind => [ 1, 3, 2, 4 ],
345 # test => 'Top level -in with list args',
348 where => { -between => [42, 69] },
349 throws => qr/Fatal: Operator 'BETWEEN' requires/,
350 test => 'Top level -between with broken args',
355 { -op => [ '+', { -ident => 'foo' }, 2 ] },
359 stmt => ' WHERE (foo + ? BETWEEN ? AND ?)',
361 test => 'Top level -between with useful LHS',
366 { -row => [ 'x', 'y' ] },
367 { -row => [ 1, 2 ] },
368 { -row => [ 3, 4 ] },
371 stmt => ' WHERE (x, y) IN ((?, ?), (?, ?))',
373 test => 'Complex top-level -in',
376 where => { -is => [ 'bob', undef ] },
377 stmt => ' WHERE bob IS NULL',
379 test => 'Top level -is ok',
382 where => { -op => [ in => x => 1, 2, 3 ] },
383 stmt => ' WHERE x IN (?, ?, ?)',
385 test => 'Raw -op passes through correctly'
390 for my $case (@in_between_tests) {
392 local $TODO = $case->{todo} if $case->{todo};
393 local $SQL::Abstract::Test::parenthesis_significant = $case->{parenthesis_significant};
394 my $label = $case->{test} || 'in-between test';
396 my $sql = SQL::Abstract->new($case->{args} || {});
398 if (my $e = $case->{throws}) {
400 throws_ok { ($stmt) = $sql->where($case->{where}) } $e, "$label throws correctly"
401 or diag dumper ({ where => $case->{where}, result => $stmt });
407 ($stmt, @bind) = $sql->where($case->{where});
408 } [], "$label gives no warnings";
415 "$label generates correct SQL and bind",
416 ) || diag dumper ({ where => $case->{where}, exp => $sql->_expand_expr($case->{where}) });
417 } || diag dumper ({ where => $case->{where}, exp => $sql->_expand_expr($case->{where}) });