Commit | Line | Data |
cf02fc47 |
1 | use strict; |
2 | use warnings; |
3 | use Test::More; |
97084113 |
4 | use Test::Warn; |
cf02fc47 |
5 | use Test::Exception; |
db8e4588 |
6 | use SQL::Abstract::Test import => [qw(is_same_sql_bind diag_where dumper)]; |
cf02fc47 |
7 | |
cf02fc47 |
8 | use SQL::Abstract; |
9 | |
cf02fc47 |
10 | my @in_between_tests = ( |
11 | { |
12 | where => { x => { -between => [1, 2] } }, |
13 | stmt => 'WHERE (x BETWEEN ? AND ?)', |
14 | bind => [qw/1 2/], |
15 | test => '-between with two placeholders', |
16 | }, |
17 | { |
18 | where => { x => { -between => [\"1", 2] } }, |
19 | stmt => 'WHERE (x BETWEEN 1 AND ?)', |
20 | bind => [qw/2/], |
21 | test => '-between with one literal sql arg and one placeholder', |
22 | }, |
23 | { |
24 | where => { x => { -between => [1, \"2"] } }, |
25 | stmt => 'WHERE (x BETWEEN ? AND 2)', |
26 | bind => [qw/1/], |
27 | test => '-between with one placeholder and one literal sql arg', |
28 | }, |
29 | { |
30 | where => { x => { -between => [\'current_date - 1', \'current_date - 0'] } }, |
31 | stmt => 'WHERE (x BETWEEN current_date - 1 AND current_date - 0)', |
32 | bind => [], |
33 | test => '-between with two literal sql arguments', |
34 | }, |
35 | { |
4d8b3dc4 |
36 | where => { x => { -between => [ \['current_date - ?', 1], \['current_date - ?', 0] ] } }, |
37 | stmt => 'WHERE (x BETWEEN current_date - ? AND current_date - ?)', |
38 | bind => [1, 0], |
39 | test => '-between with two literal sql arguments with bind', |
40 | }, |
41 | { |
cf02fc47 |
42 | where => { x => { -between => \['? AND ?', 1, 2] } }, |
43 | stmt => 'WHERE (x BETWEEN ? AND ?)', |
44 | bind => [1,2], |
45 | test => '-between with literal sql with placeholders (\["? AND ?", scalar, scalar])', |
46 | }, |
47 | { |
48 | where => { x => { -between => \["'something' AND ?", 2] } }, |
49 | stmt => "WHERE (x BETWEEN 'something' AND ?)", |
50 | bind => [2], |
51 | test => '-between with literal sql with one literal arg and one placeholder (\["\'something\' AND ?", scalar])', |
52 | }, |
53 | { |
54 | where => { x => { -between => \["? AND 'something'", 1] } }, |
55 | stmt => "WHERE (x BETWEEN ? AND 'something')", |
56 | bind => [1], |
57 | test => '-between with literal sql with one placeholder and one literal arg (\["? AND \'something\'", scalar])', |
58 | }, |
59 | { |
4d8b3dc4 |
60 | where => { x => { -between => \"'this' AND 'that'" } }, |
cf02fc47 |
61 | stmt => "WHERE (x BETWEEN 'this' AND 'that')", |
62 | bind => [], |
4d8b3dc4 |
63 | test => '-between with literal sql with a literal (\"\'this\' AND \'that\'")', |
cf02fc47 |
64 | }, |
7f54040f |
65 | |
66 | # generate a set of invalid -between tests |
67 | ( map { { |
68 | where => { x => { -between => $_ } }, |
69 | test => 'invalid -between args', |
df2ddf77 |
70 | throws => qr{ |
71 | \QOperator 'BETWEEN' requires either an arrayref with two defined values or expressions, or a single literal scalarref/arrayref-ref\E |
72 | | |
73 | \QArgument passed to the 'BETWEEN' operator can not be undefined\E |
74 | }x,, |
7f54040f |
75 | } } ( |
76 | [ 1, 2, 3 ], |
77 | [ 1, undef, 3 ], |
78 | [ undef, 2, 3 ], |
79 | [ 1, 2, undef ], |
80 | [ 1, undef ], |
81 | [ undef, 2 ], |
82 | [ undef, undef ], |
83 | [ 1 ], |
84 | [ undef ], |
85 | [], |
86 | 1, |
87 | undef, |
88 | )), |
e41c3bdd |
89 | { |
90 | where => { |
0336eddb |
91 | start0 => { -between => [ 1, { -upper => 2 } ] }, |
e41c3bdd |
92 | start1 => { -between => \["? AND ?", 1, 2] }, |
93 | start2 => { -between => \"lower(x) AND upper(y)" }, |
94 | start3 => { -between => [ |
95 | \"lower(x)", |
96 | \["upper(?)", 'stuff' ], |
97 | ] }, |
98 | }, |
99 | stmt => "WHERE ( |
a420b11f |
100 | ( start0 BETWEEN ? AND UPPER(?) ) |
e41c3bdd |
101 | AND ( start1 BETWEEN ? AND ? ) |
102 | AND ( start2 BETWEEN lower(x) AND upper(y) ) |
103 | AND ( start3 BETWEEN lower(x) AND upper(?) ) |
104 | )", |
105 | bind => [1, 2, 1, 2, 'stuff'], |
106 | test => '-between POD test', |
107 | }, |
5e5cbf51 |
108 | { |
109 | args => { bindtype => 'columns' }, |
110 | where => { |
111 | start0 => { -between => [ 1, { -upper => 2 } ] }, |
112 | start1 => { -between => \["? AND ?", [ start1 => 1], [start1 => 2] ] }, |
113 | start2 => { -between => \"lower(x) AND upper(y)" }, |
114 | start3 => { -between => [ |
115 | \"lower(x)", |
116 | \["upper(?)", [ start3 => 'stuff'] ], |
117 | ] }, |
118 | }, |
119 | stmt => "WHERE ( |
a420b11f |
120 | ( start0 BETWEEN ? AND UPPER(?) ) |
5e5cbf51 |
121 | AND ( start1 BETWEEN ? AND ? ) |
122 | AND ( start2 BETWEEN lower(x) AND upper(y) ) |
123 | AND ( start3 BETWEEN lower(x) AND upper(?) ) |
124 | )", |
125 | bind => [ |
126 | [ start0 => 1 ], |
127 | [ start0 => 2 ], |
128 | [ start1 => 1 ], |
129 | [ start1 => 2 ], |
130 | [ start3 => 'stuff' ], |
131 | ], |
132 | test => '-between POD test', |
133 | }, |
904c3621 |
134 | { |
135 | where => { 'test1.a' => { 'In', ['boom', 'bang'] } }, |
136 | stmt => ' WHERE ( test1.a IN ( ?, ? ) )', |
137 | bind => ['boom', 'bang'], |
138 | test => 'In (no dash, initial cap) with qualified column', |
139 | }, |
140 | { |
141 | where => { a => { 'between', ['boom', 'bang'] } }, |
142 | stmt => ' WHERE ( a BETWEEN ? AND ? )', |
143 | bind => ['boom', 'bang'], |
144 | test => 'between (no dash) with two placeholders', |
145 | }, |
4a1f01a3 |
146 | |
147 | { |
4a1f01a3 |
148 | where => { x => { -in => [ 1 .. 3] } }, |
7d273452 |
149 | stmt => "WHERE x IN (?, ?, ?)", |
150 | bind => [ 1 .. 3 ], |
4a1f01a3 |
151 | test => '-in with an array of scalars', |
152 | }, |
153 | { |
e41c3bdd |
154 | where => { x => { -in => [] } }, |
7d273452 |
155 | stmt => "WHERE 0=1", |
e41c3bdd |
156 | bind => [], |
157 | test => '-in with an empty array', |
158 | }, |
159 | { |
4a1f01a3 |
160 | where => { x => { -in => \'( 1,2,lower(y) )' } }, |
7d273452 |
161 | stmt => "WHERE x IN ( 1,2,lower(y) )", |
4a1f01a3 |
162 | bind => [], |
163 | test => '-in with a literal scalarref', |
164 | }, |
46dc2f3e |
165 | |
166 | # note that outer parens are opened even though literal was requested below |
4a1f01a3 |
167 | { |
4a1f01a3 |
168 | where => { x => { -in => \['( ( ?,?,lower(y) ) )', 1, 2] } }, |
7d273452 |
169 | stmt => "WHERE x IN ( ?,?,lower(y) )", |
4a1f01a3 |
170 | bind => [1, 2], |
171 | test => '-in with a literal arrayrefref', |
172 | }, |
e41c3bdd |
173 | { |
e41c3bdd |
174 | where => { |
171a709f |
175 | status => { -in => \"(SELECT status_codes\nFROM states)" }, |
176 | }, |
7d273452 |
177 | stmt => " WHERE status IN ( SELECT status_codes FROM states )", |
171a709f |
178 | bind => [], |
179 | test => '-in multi-line subquery test', |
180 | }, |
181 | { |
171a709f |
182 | where => { |
e41c3bdd |
183 | customer => { -in => \[ |
184 | 'SELECT cust_id FROM cust WHERE balance > ?', |
185 | 2000, |
186 | ]}, |
187 | status => { -in => \'SELECT status_codes FROM states' }, |
188 | }, |
189 | stmt => " |
7d273452 |
190 | WHERE |
e41c3bdd |
191 | customer IN ( SELECT cust_id FROM cust WHERE balance > ? ) |
192 | AND status IN ( SELECT status_codes FROM states ) |
e41c3bdd |
193 | ", |
194 | bind => [2000], |
195 | test => '-in POD test', |
196 | }, |
46dc2f3e |
197 | |
0336eddb |
198 | { |
199 | where => { x => { -in => [ \['LOWER(?)', 'A' ], \'LOWER(b)', { -lower => 'c' } ] } }, |
a420b11f |
200 | stmt => " WHERE ( x IN ( LOWER(?), LOWER(b), LOWER(?) ) )", |
0336eddb |
201 | bind => [qw/A c/], |
202 | test => '-in with an array of function array refs with args', |
203 | }, |
279eb282 |
204 | { |
205 | where => { x => { -in => [ 1, undef ] } }, |
038b0a7f |
206 | stmt => " WHERE ( x IN ( ? ) OR x IS NULL )", |
207 | bind => [ 1 ], |
f2a0d52b |
208 | test => '-in with undef as an element', |
279eb282 |
209 | }, |
210 | { |
211 | where => { x => { -in => [ 1, undef, 2, 3, undef ] } }, |
038b0a7f |
212 | stmt => " WHERE ( x IN ( ?, ?, ? ) OR x IS NULL )", |
213 | bind => [ 1, 2, 3 ], |
032dfe20 |
214 | test => '-in with multiple undef elements', |
279eb282 |
215 | }, |
904c3621 |
216 | { |
217 | where => { a => { -in => 42 }, b => { -not_in => 42 } }, |
7d273452 |
218 | stmt => ' WHERE a IN ( ? ) AND b NOT IN ( ? )', |
904c3621 |
219 | bind => [ 42, 42 ], |
220 | test => '-in, -not_in with scalar', |
221 | }, |
222 | { |
223 | where => { a => { -in => [] }, b => { -not_in => [] } }, |
224 | stmt => ' WHERE ( 0=1 AND 1=1 )', |
225 | bind => [], |
226 | test => '-in, -not_in with empty arrays', |
227 | }, |
228 | { |
904c3621 |
229 | where => { a => { -in => [42, undef] }, b => { -not_in => [42, undef] } }, |
230 | stmt => ' WHERE ( ( a IN ( ? ) OR a IS NULL ) AND b NOT IN ( ? ) AND b IS NOT NULL )', |
231 | bind => [ 42, 42 ], |
232 | test => '-in, -not_in with undef among elements', |
233 | }, |
234 | { |
904c3621 |
235 | where => { a => { -in => [undef] }, b => { -not_in => [undef] } }, |
236 | stmt => ' WHERE ( a IS NULL AND b IS NOT NULL )', |
237 | bind => [], |
238 | test => '-in, -not_in with just undef element', |
239 | }, |
240 | { |
241 | where => { a => { -in => undef } }, |
242 | throws => qr/Argument passed to the 'IN' operator can not be undefined/, |
243 | test => '-in with undef argument', |
244 | }, |
cf02fc47 |
245 | ); |
246 | |
cf02fc47 |
247 | for my $case (@in_between_tests) { |
248 | TODO: { |
249 | local $TODO = $case->{todo} if $case->{todo}; |
4a1f01a3 |
250 | local $SQL::Abstract::Test::parenthesis_significant = $case->{parenthesis_significant}; |
df7b1db3 |
251 | my $label = $case->{test} || 'in-between test'; |
cf02fc47 |
252 | |
032dfe20 |
253 | my $sql = SQL::Abstract->new ($case->{args} || {}); |
4d8b3dc4 |
254 | |
97084113 |
255 | if (my $e = $case->{throws}) { |
db8e4588 |
256 | my $stmt; |
257 | throws_ok { ($stmt) = $sql->where($case->{where}) } $e, "$label throws correctly" |
258 | or diag dumper ({ where => $case->{where}, result => $stmt }); |
032dfe20 |
259 | } |
260 | else { |
97084113 |
261 | my ($stmt, @bind); |
262 | warnings_are { |
263 | ($stmt, @bind) = $sql->where($case->{where}); |
df7b1db3 |
264 | } [], "$label gives no warnings"; |
032dfe20 |
265 | |
2fadf08e |
266 | is_same_sql_bind( |
267 | $stmt, |
268 | \@bind, |
269 | $case->{stmt}, |
270 | $case->{bind}, |
df7b1db3 |
271 | "$label generates correct SQL and bind", |
2fadf08e |
272 | ) || diag_where ( $case->{where} ); |
032dfe20 |
273 | } |
cf02fc47 |
274 | } |
275 | } |
10e6c946 |
276 | |
277 | done_testing; |