5 use SQL::Abstract::Test import => [qw(is_same_sql_bind diag_where)];
9 my @in_between_tests = (
11 where => { x => { -between => [1, 2] } },
12 stmt => 'WHERE (x BETWEEN ? AND ?)',
14 test => '-between with two placeholders',
17 where => { x => { -between => [\"1", 2] } },
18 stmt => 'WHERE (x BETWEEN 1 AND ?)',
20 test => '-between with one literal sql arg and one placeholder',
23 where => { x => { -between => [1, \"2"] } },
24 stmt => 'WHERE (x BETWEEN ? AND 2)',
26 test => '-between with one placeholder and one literal sql arg',
29 where => { x => { -between => [\'current_date - 1', \'current_date - 0'] } },
30 stmt => 'WHERE (x BETWEEN current_date - 1 AND current_date - 0)',
32 test => '-between with two literal sql arguments',
35 where => { x => { -between => [ \['current_date - ?', 1], \['current_date - ?', 0] ] } },
36 stmt => 'WHERE (x BETWEEN current_date - ? AND current_date - ?)',
38 test => '-between with two literal sql arguments with bind',
41 where => { x => { -between => \['? AND ?', 1, 2] } },
42 stmt => 'WHERE (x BETWEEN ? AND ?)',
44 test => '-between with literal sql with placeholders (\["? AND ?", scalar, scalar])',
47 where => { x => { -between => \["'something' AND ?", 2] } },
48 stmt => "WHERE (x BETWEEN 'something' AND ?)",
50 test => '-between with literal sql with one literal arg and one placeholder (\["\'something\' AND ?", scalar])',
53 where => { x => { -between => \["? AND 'something'", 1] } },
54 stmt => "WHERE (x BETWEEN ? AND 'something')",
56 test => '-between with literal sql with one placeholder and one literal arg (\["? AND \'something\'", scalar])',
59 where => { x => { -between => \"'this' AND 'that'" } },
60 stmt => "WHERE (x BETWEEN 'this' AND 'that')",
62 test => '-between with literal sql with a literal (\"\'this\' AND \'that\'")',
65 # generate a set of invalid -between tests
67 where => { x => { -between => $_ } },
68 test => 'invalid -between args',
69 exception => qr|Operator 'BETWEEN' requires either an arrayref with two defined values or expressions, or a single literal scalarref/arrayref-ref|,
86 start0 => { -between => [ 1, { -upper => 2 } ] },
87 start1 => { -between => \["? AND ?", 1, 2] },
88 start2 => { -between => \"lower(x) AND upper(y)" },
89 start3 => { -between => [
91 \["upper(?)", 'stuff' ],
95 ( start0 BETWEEN ? AND UPPER ? )
96 AND ( start1 BETWEEN ? AND ? )
97 AND ( start2 BETWEEN lower(x) AND upper(y) )
98 AND ( start3 BETWEEN lower(x) AND upper(?) )
100 bind => [1, 2, 1, 2, 'stuff'],
101 test => '-between POD test',
104 args => { bindtype => 'columns' },
106 start0 => { -between => [ 1, { -upper => 2 } ] },
107 start1 => { -between => \["? AND ?", [ start1 => 1], [start1 => 2] ] },
108 start2 => { -between => \"lower(x) AND upper(y)" },
109 start3 => { -between => [
111 \["upper(?)", [ start3 => 'stuff'] ],
115 ( start0 BETWEEN ? AND UPPER ? )
116 AND ( start1 BETWEEN ? AND ? )
117 AND ( start2 BETWEEN lower(x) AND upper(y) )
118 AND ( start3 BETWEEN lower(x) AND upper(?) )
125 [ start3 => 'stuff' ],
127 test => '-between POD test',
131 parenthesis_significant => 1,
132 where => { x => { -in => [ 1 .. 3] } },
133 stmt => "WHERE ( x IN (?, ?, ?) )",
135 test => '-in with an array of scalars',
138 parenthesis_significant => 1,
139 where => { x => { -in => [] } },
140 stmt => "WHERE ( 0=1 )",
142 test => '-in with an empty array',
145 parenthesis_significant => 1,
146 where => { x => { -in => \'( 1,2,lower(y) )' } },
147 stmt => "WHERE ( x IN ( 1,2,lower(y) ) )",
149 test => '-in with a literal scalarref',
152 # note that outer parens are opened even though literal was requested below
154 parenthesis_significant => 1,
155 where => { x => { -in => \['( ( ?,?,lower(y) ) )', 1, 2] } },
156 stmt => "WHERE ( x IN ( ?,?,lower(y) ) )",
158 test => '-in with a literal arrayrefref',
161 parenthesis_significant => 1,
163 status => { -in => \"(SELECT status_codes\nFROM states)" },
165 stmt => " WHERE ( status IN ( SELECT status_codes FROM states )) ",
167 test => '-in multi-line subquery test',
170 parenthesis_significant => 1,
172 customer => { -in => \[
173 'SELECT cust_id FROM cust WHERE balance > ?',
176 status => { -in => \'SELECT status_codes FROM states' },
180 customer IN ( SELECT cust_id FROM cust WHERE balance > ? )
181 AND status IN ( SELECT status_codes FROM states )
185 test => '-in POD test',
189 where => { x => { -in => [ \['LOWER(?)', 'A' ], \'LOWER(b)', { -lower => 'c' } ] } },
190 stmt => " WHERE ( x IN ( LOWER(?), LOWER(b), LOWER ? ) )",
192 test => '-in with an array of function array refs with args',
196 \QSQL::Abstract before v1.75 used to generate incorrect SQL \E
197 \Qwhen the -IN operator was given an undef-containing list: \E
198 \Q!!!AUDIT YOUR CODE AND DATA!!! (the upcoming Data::Query-based \E
199 \Qversion of SQL::Abstract will emit the logically correct SQL \E
200 \Qinstead of raising this exception)\E
202 where => { x => { -in => [ 1, undef ] } },
203 stmt => " WHERE ( x IN ( ? ) OR x IS NULL )",
205 test => '-in with undef as an element',
209 \QSQL::Abstract before v1.75 used to generate incorrect SQL \E
210 \Qwhen the -IN operator was given an undef-containing list: \E
211 \Q!!!AUDIT YOUR CODE AND DATA!!! (the upcoming Data::Query-based \E
212 \Qversion of SQL::Abstract will emit the logically correct SQL \E
213 \Qinstead of raising this exception)\E
215 where => { x => { -in => [ 1, undef, 2, 3, undef ] } },
216 stmt => " WHERE ( x IN ( ?, ?, ? ) OR x IS NULL )",
218 test => '-in with multiple undef elements',
222 for my $case (@in_between_tests) {
224 local $TODO = $case->{todo} if $case->{todo};
225 local $SQL::Abstract::Test::parenthesis_significant = $case->{parenthesis_significant};
229 local $SIG{__WARN__} = sub { push @w, @_ };
231 my $sql = SQL::Abstract->new ($case->{args} || {});
233 if ($case->{exception}) {
234 throws_ok { $sql->where($case->{where}) } $case->{exception};
237 my ($stmt, @bind) = $sql->where($case->{where});
243 ) || diag_where ( $case->{where} );
246 is (@w, 0, $case->{test} || 'No warnings within in-between tests')
247 || diag join "\n", 'Emitted warnings:', @w;