7 use SQL::Abstract::Test import => [qw(is_same_sql_bind diag_where)];
11 my @in_between_tests = (
13 where => { x => { -between => [1, 2] } },
14 stmt => 'WHERE (x BETWEEN ? AND ?)',
16 test => '-between with two placeholders',
19 where => { x => { -between => [\"1", 2] } },
20 stmt => 'WHERE (x BETWEEN 1 AND ?)',
22 test => '-between with one literal sql arg and one placeholder',
25 where => { x => { -between => [1, \"2"] } },
26 stmt => 'WHERE (x BETWEEN ? AND 2)',
28 test => '-between with one placeholder and one literal sql arg',
31 where => { x => { -between => [\'current_date - 1', \'current_date - 0'] } },
32 stmt => 'WHERE (x BETWEEN current_date - 1 AND current_date - 0)',
34 test => '-between with two literal sql arguments',
37 where => { x => { -between => [ \['current_date - ?', 1], \['current_date - ?', 0] ] } },
38 stmt => 'WHERE (x BETWEEN current_date - ? AND current_date - ?)',
40 test => '-between with two literal sql arguments with bind',
43 where => { x => { -between => \['? AND ?', 1, 2] } },
44 stmt => 'WHERE (x BETWEEN ? AND ?)',
46 test => '-between with literal sql with placeholders (\["? AND ?", scalar, scalar])',
49 where => { x => { -between => \["'something' AND ?", 2] } },
50 stmt => "WHERE (x BETWEEN 'something' AND ?)",
52 test => '-between with literal sql with one literal arg and one placeholder (\["\'something\' AND ?", scalar])',
55 where => { x => { -between => \["? AND 'something'", 1] } },
56 stmt => "WHERE (x BETWEEN ? AND 'something')",
58 test => '-between with literal sql with one placeholder and one literal arg (\["? AND \'something\'", scalar])',
61 where => { x => { -between => \"'this' AND 'that'" } },
62 stmt => "WHERE (x BETWEEN 'this' AND 'that')",
64 test => '-between with literal sql with a literal (\"\'this\' AND \'that\'")',
67 # generate a set of invalid -between tests
69 where => { x => { -between => $_ } },
70 test => 'invalid -between args',
71 exception => qr|Operator 'BETWEEN' requires either an arrayref with two defined values or expressions, or a single literal scalarref/arrayref-ref|,
88 start0 => { -between => [ 1, { -upper => 2 } ] },
89 start1 => { -between => \["? AND ?", 1, 2] },
90 start2 => { -between => \"lower(x) AND upper(y)" },
91 start3 => { -between => [
93 \["upper(?)", 'stuff' ],
97 ( start0 BETWEEN ? AND UPPER ? )
98 AND ( start1 BETWEEN ? AND ? )
99 AND ( start2 BETWEEN lower(x) AND upper(y) )
100 AND ( start3 BETWEEN lower(x) AND upper(?) )
102 bind => [1, 2, 1, 2, 'stuff'],
103 test => '-between POD test',
106 args => { bindtype => 'columns' },
108 start0 => { -between => [ 1, { -upper => 2 } ] },
109 start1 => { -between => \["? AND ?", [ start1 => 1], [start1 => 2] ] },
110 start2 => { -between => \"lower(x) AND upper(y)" },
111 start3 => { -between => [
113 \["upper(?)", [ start3 => 'stuff'] ],
117 ( start0 BETWEEN ? AND UPPER ? )
118 AND ( start1 BETWEEN ? AND ? )
119 AND ( start2 BETWEEN lower(x) AND upper(y) )
120 AND ( start3 BETWEEN lower(x) AND upper(?) )
127 [ start3 => 'stuff' ],
129 test => '-between POD test',
133 parenthesis_significant => 1,
134 where => { x => { -in => [ 1 .. 3] } },
135 stmt => "WHERE ( x IN (?, ?, ?) )",
137 test => '-in with an array of scalars',
140 parenthesis_significant => 1,
141 where => { x => { -in => [] } },
142 stmt => "WHERE ( 0=1 )",
144 test => '-in with an empty array',
147 parenthesis_significant => 1,
148 where => { x => { -in => \'( 1,2,lower(y) )' } },
149 stmt => "WHERE ( x IN ( 1,2,lower(y) ) )",
151 test => '-in with a literal scalarref',
154 parenthesis_significant => 1,
155 where => { x => { -in => \['( ( ?,?,lower(y) ) )', 1, 2] } },
156 stmt => "WHERE ( x IN ( ?,?,lower(y) ) )", # note that outer parens are opened even though literal was requested (RIBASUSHI)
158 test => '-in with a literal arrayrefref',
161 parenthesis_significant => 1,
163 status => { -in => \"(SELECT status_codes\nFROM states)" },
165 # failed to open outer parens on a multi-line query in 1.61 (semifor)
166 stmt => " WHERE ( status IN ( SELECT status_codes FROM states )) ",
168 test => '-in multi-line subquery test',
171 parenthesis_significant => 1,
173 customer => { -in => \[
174 'SELECT cust_id FROM cust WHERE balance > ?',
177 status => { -in => \'SELECT status_codes FROM states' },
181 customer IN ( SELECT cust_id FROM cust WHERE balance > ? )
182 AND status IN ( SELECT status_codes FROM states )
186 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;