7 use SQL::Abstract::Test import => ['is_same_sql_bind'];
12 my @in_between_tests = (
14 where => { x => { -between => [1, 2] } },
15 stmt => 'WHERE (x BETWEEN ? AND ?)',
17 test => '-between with two placeholders',
20 where => { x => { -between => [\"1", 2] } },
21 stmt => 'WHERE (x BETWEEN 1 AND ?)',
23 test => '-between with one literal sql arg and one placeholder',
26 where => { x => { -between => [1, \"2"] } },
27 stmt => 'WHERE (x BETWEEN ? AND 2)',
29 test => '-between with one placeholder and one literal sql arg',
32 where => { x => { -between => [\'current_date - 1', \'current_date - 0'] } },
33 stmt => 'WHERE (x BETWEEN current_date - 1 AND current_date - 0)',
35 test => '-between with two literal sql arguments',
38 where => { x => { -between => [ \['current_date - ?', 1], \['current_date - ?', 0] ] } },
39 stmt => 'WHERE (x BETWEEN current_date - ? AND current_date - ?)',
41 test => '-between with two literal sql arguments with bind',
44 where => { x => { -between => \['? AND ?', 1, 2] } },
45 stmt => 'WHERE (x BETWEEN ? AND ?)',
47 test => '-between with literal sql with placeholders (\["? AND ?", scalar, scalar])',
50 where => { x => { -between => \["'something' AND ?", 2] } },
51 stmt => "WHERE (x BETWEEN 'something' AND ?)",
53 test => '-between with literal sql with one literal arg and one placeholder (\["\'something\' AND ?", scalar])',
56 where => { x => { -between => \["? AND 'something'", 1] } },
57 stmt => "WHERE (x BETWEEN ? AND 'something')",
59 test => '-between with literal sql with one placeholder and one literal arg (\["? AND \'something\'", scalar])',
62 where => { x => { -between => \"'this' AND 'that'" } },
63 stmt => "WHERE (x BETWEEN 'this' AND 'that')",
65 test => '-between with literal sql with a literal (\"\'this\' AND \'that\'")',
68 # generate a set of invalid -between tests
70 where => { x => { -between => $_ } },
71 test => 'invalid -between args',
72 exception => qr|Operator 'BETWEEN' requires either an arrayref with two defined values or expressions, or a single literal scalarref/arrayref-ref|,
89 start0 => { -between => [ 1, { -upper => 2 } ] },
90 start1 => { -between => \["? AND ?", 1, 2] },
91 start2 => { -between => \"lower(x) AND upper(y)" },
92 start3 => { -between => [
94 \["upper(?)", 'stuff' ],
98 ( start0 BETWEEN ? AND UPPER ? )
99 AND ( start1 BETWEEN ? AND ? )
100 AND ( start2 BETWEEN lower(x) AND upper(y) )
101 AND ( start3 BETWEEN lower(x) AND upper(?) )
103 bind => [1, 2, 1, 2, 'stuff'],
104 test => '-between POD test',
107 args => { bindtype => 'columns' },
109 start0 => { -between => [ 1, { -upper => 2 } ] },
110 start1 => { -between => \["? AND ?", [ start1 => 1], [start1 => 2] ] },
111 start2 => { -between => \"lower(x) AND upper(y)" },
112 start3 => { -between => [
114 \["upper(?)", [ start3 => 'stuff'] ],
118 ( start0 BETWEEN ? AND UPPER ? )
119 AND ( start1 BETWEEN ? AND ? )
120 AND ( start2 BETWEEN lower(x) AND upper(y) )
121 AND ( start3 BETWEEN lower(x) AND upper(?) )
128 [ start3 => 'stuff' ],
130 test => '-between POD test',
134 parenthesis_significant => 1,
135 where => { x => { -in => [ 1 .. 3] } },
136 stmt => "WHERE ( x IN (?, ?, ?) )",
138 test => '-in with an array of scalars',
141 parenthesis_significant => 1,
142 where => { x => { -in => [] } },
143 stmt => "WHERE ( 0=1 )",
145 test => '-in with an empty array',
148 parenthesis_significant => 1,
149 where => { x => { -in => \'( 1,2,lower(y) )' } },
150 stmt => "WHERE ( x IN ( 1,2,lower(y) ) )",
152 test => '-in with a literal scalarref',
155 parenthesis_significant => 1,
156 where => { x => { -in => \['( ( ?,?,lower(y) ) )', 1, 2] } },
157 stmt => "WHERE ( x IN ( ?,?,lower(y) ) )", # note that outer parens are opened even though literal was requested (RIBASUSHI)
159 test => '-in with a literal arrayrefref',
162 parenthesis_significant => 1,
164 status => { -in => \"(SELECT status_codes\nFROM states)" },
166 # failed to open outer parens on a multi-line query in 1.61 (semifor)
167 stmt => " WHERE ( status IN ( SELECT status_codes FROM states )) ",
169 test => '-in multi-line subquery test',
172 parenthesis_significant => 1,
174 customer => { -in => \[
175 'SELECT cust_id FROM cust WHERE balance > ?',
178 status => { -in => \'SELECT status_codes FROM states' },
182 customer IN ( SELECT cust_id FROM cust WHERE balance > ? )
183 AND status IN ( SELECT status_codes FROM states )
187 test => '-in POD test',
190 where => { x => { -in => [ \['LOWER(?)', 'A' ], \'LOWER(b)', { -lower => 'c' } ] } },
191 stmt => " WHERE ( x IN ( LOWER(?), LOWER(b), LOWER ? ) )",
193 test => '-in with an array of function array refs with args',
197 \QSQL::Abstract before v1.75 used to generate incorrect SQL \E
198 \Qwhen the -IN operator was given an undef-containing list: \E
199 \Q!!!AUDIT YOUR CODE AND DATA!!! (the upcoming Data::Query-based \E
200 \Qversion of SQL::Abstract will emit the logically correct SQL \E
201 \Qinstead of raising this exception)\E
203 where => { x => { -in => [ 1, undef ] } },
204 stmt => " WHERE ( x IN ( ? ) OR x IS NULL )",
206 test => '-in with undef as an element',
210 \QSQL::Abstract before v1.75 used to generate incorrect SQL \E
211 \Qwhen the -IN operator was given an undef-containing list: \E
212 \Q!!!AUDIT YOUR CODE AND DATA!!! (the upcoming Data::Query-based \E
213 \Qversion of SQL::Abstract will emit the logically correct SQL \E
214 \Qinstead of raising this exception)\E
216 where => { x => { -in => [ 1, undef, 2, 3, undef ] } },
217 stmt => " WHERE ( x IN ( ?, ?, ? ) OR x IS NULL )",
219 test => '-in with multiple undef elements',
223 for my $case (@in_between_tests) {
225 local $TODO = $case->{todo} if $case->{todo};
226 local $SQL::Abstract::Test::parenthesis_significant = $case->{parenthesis_significant};
228 local $Data::Dumper::Terse = 1;
231 local $SIG{__WARN__} = sub { push @w, @_ };
232 my $sql = SQL::Abstract->new ($case->{args} || {});
234 if ($case->{exception}) {
235 throws_ok { $sql->where($case->{where}) } $case->{exception};
239 my ($stmt, @bind) = $sql->where($case->{where});
245 ) || diag "Search term:\n" . Dumper $case->{where};
246 } "$case->{test} doesn't die";
249 is (@w, 0, $case->{test} || 'No warnings within in-between tests')
250 || diag join "\n", 'Emitted warnings:', @w;