Commit | Line | Data |
d70ca130 |
1 | use strict; |
2 | use warnings; |
3 | |
03f6671a |
4 | use FindBin; |
5 | use lib "$FindBin::Bin/../../lib"; |
96bbece0 |
6 | use Test::SQL::Abstract::Util qw/ |
7 | mk_name |
8 | mk_value |
9 | field_op_value |
10 | :dumper_sort |
11 | /; |
03f6671a |
12 | |
1c51edc4 |
13 | use SQL::Abstract::Compat; |
d70ca130 |
14 | |
0073ca43 |
15 | use Test::More tests => 17; |
d70ca130 |
16 | use Test::Differences; |
17 | |
1c51edc4 |
18 | ok(my $visitor = SQL::Abstract::Compat->new); |
d70ca130 |
19 | |
00f4d23e |
20 | |
627dcb62 |
21 | my $foo_id = { -type => 'identifier', elements => [qw/foo/] }; |
22 | my $bar_id = { -type => 'identifier', elements => [qw/bar/] }; |
c6039348 |
23 | |
00f4d23e |
24 | my $foo_eq_1 = field_op_value($foo_id, '==', 1); |
25 | my $bar_eq_str = field_op_value($bar_id, '==', 'some str'); |
d70ca130 |
26 | |
27 | eq_or_diff |
37716782 |
28 | $visitor->recurse_where({ foo => 1 }), |
d70ca130 |
29 | $foo_eq_1, |
30 | "Single value hash"; |
31 | |
32 | |
d70ca130 |
33 | |
34 | eq_or_diff |
37716782 |
35 | $visitor->recurse_where({ foo => 1, bar => 'some str' }), |
d70ca130 |
36 | { -type => 'expr', |
37 | op => 'and', |
38 | args => [ |
39 | $bar_eq_str, |
40 | $foo_eq_1, |
41 | ] |
42 | }, |
43 | "two keys in hash"; |
44 | |
45 | eq_or_diff |
37716782 |
46 | $visitor->recurse_where({ -or => { foo => 1, bar => 'some str' } }), |
d70ca130 |
47 | { -type => 'expr', |
48 | op => 'or', |
49 | args => [ |
50 | $bar_eq_str, |
51 | $foo_eq_1, |
52 | ] |
53 | }, |
54 | "-or key in hash"; |
55 | |
56 | |
57 | eq_or_diff |
37716782 |
58 | $visitor->recurse_where([ -and => { foo => 1, bar => 'some str' } ]), |
d70ca130 |
59 | { -type => 'expr', |
60 | op => 'and', |
61 | args => [ |
62 | $bar_eq_str, |
63 | $foo_eq_1, |
64 | ] |
65 | }, |
66 | "-and as first element of array"; |
67 | |
68 | |
69 | eq_or_diff |
37716782 |
70 | $visitor->recurse_where([ -and => { foo => 1, bar => 'some str' }, { foo => 1} ]), |
d70ca130 |
71 | { -type => 'expr', |
72 | op => 'or', |
73 | args => [ |
74 | { -type => 'expr', |
75 | op => 'and', |
76 | args => [ |
77 | $bar_eq_str, |
78 | $foo_eq_1, |
79 | ] |
80 | }, |
81 | $foo_eq_1, |
82 | ] |
83 | }, |
c6039348 |
84 | "-and as first element of array + hash"; |
85 | |
86 | eq_or_diff |
37716782 |
87 | $visitor->recurse_where({ foo => { '!=' => 'bar' } }), |
00f4d23e |
88 | field_op_value($foo_id, '!=', 'bar'), |
c6039348 |
89 | "foo => { '!=' => 'bar' }"; |
90 | |
91 | eq_or_diff |
37716782 |
92 | $visitor->recurse_where({ foo => [ 1, 'bar' ] }), |
c6039348 |
93 | { -type => 'expr', |
94 | op => 'or', |
95 | args => [ |
96 | $foo_eq_1, |
00f4d23e |
97 | field_op_value($foo_id, '==', 'bar'), |
c6039348 |
98 | ], |
99 | }, |
100 | "foo => [ 1, 'bar' ]"; |
101 | |
2e828b0b |
102 | eq_or_diff |
37716782 |
103 | $visitor->recurse_where({ foo => { -in => [ 1, 'bar' ] } }), |
2e828b0b |
104 | { -type => 'expr', |
105 | op => 'in', |
106 | args => [ |
107 | $foo_id, |
108 | { -type => 'value', value => 1 }, |
109 | { -type => 'value', value => 'bar' }, |
110 | ] |
111 | }, |
112 | "foo => { -in => [ 1, 'bar' ] }"; |
113 | |
114 | eq_or_diff |
37716782 |
115 | $visitor->recurse_where({ foo => { -not_in => [ 1, 'bar' ] } }), |
2e828b0b |
116 | { -type => 'expr', |
117 | op => 'not_in', |
118 | args => [ |
119 | $foo_id, |
120 | { -type => 'value', value => 1 }, |
121 | { -type => 'value', value => 'bar' }, |
122 | ] |
123 | }, |
124 | "foo => { -not_in => [ 1, 'bar' ] }"; |
125 | |
126 | eq_or_diff |
37716782 |
127 | $visitor->recurse_where({ foo => { -in => [ ] } }), |
2e828b0b |
128 | { -type => 'expr', |
129 | op => 'in', |
130 | args => [ |
131 | $foo_id, |
132 | ] |
133 | }, |
134 | "foo => { -in => [ ] }"; |
135 | |
00f4d23e |
136 | my $worker_eq = sub { |
137 | return { |
138 | -type => 'expr', |
139 | op => '==', |
140 | args => [ |
627dcb62 |
141 | { -type => 'identifier', elements => ['worker'] }, |
00f4d23e |
142 | { -type => 'value', value => $_[0] }, |
143 | ], |
144 | } |
145 | }; |
03f6671a |
146 | |
00f4d23e |
147 | eq_or_diff |
37716782 |
148 | $visitor->recurse_where( { |
00f4d23e |
149 | requestor => 'inna', |
150 | worker => ['nwiger', 'rcwe', 'sfz'], |
151 | status => { '!=', 'completed' } |
152 | } ), |
153 | { -type => 'expr', |
154 | op => 'and', |
155 | args => [ |
03f6671a |
156 | field_op_value(qw/requestor == inna/), |
00f4d23e |
157 | field_op_value(qw/status != completed/), |
158 | { -type => 'expr', |
159 | op => 'or', |
160 | args => [ |
161 | field_op_value(qw/worker == nwiger/), |
162 | field_op_value(qw/worker == rcwe/), |
163 | field_op_value(qw/worker == sfz/), |
164 | ] |
165 | }, |
00f4d23e |
166 | ] |
167 | }, |
03f6671a |
168 | "complex expr 1"; |
00f4d23e |
169 | |
170 | |
03f6671a |
171 | $visitor->convert('UPPER'); |
172 | |
0073ca43 |
173 | my $ticket_or_eq = { |
174 | -type => 'expr', |
175 | op => 'or', |
176 | args => [ |
177 | field_op_value( upper(mk_name('ticket')), '==', upper(mk_value(11))), |
178 | field_op_value( upper(mk_name('ticket')), '==', upper(mk_value(12))), |
179 | field_op_value( upper(mk_name('ticket')), '==', upper(mk_value(13))), |
180 | ] |
181 | }; |
182 | |
03f6671a |
183 | eq_or_diff |
184 | $visitor->select_ast( |
185 | 'test', '*', [ { ticket => [11, 12, 13] } ] |
186 | ), |
187 | { -type => 'select', |
627dcb62 |
188 | columns => [ { -type => 'identifier', elements => ['*'] } ], |
189 | tablespec => { -type => 'identifier', elements => ['test'] }, |
0073ca43 |
190 | where => $ticket_or_eq |
03f6671a |
191 | }, |
192 | "Complex AST with convert('UPPER')"; |
193 | |
0073ca43 |
194 | my $hostname_and_ticket = { |
195 | -type => 'expr', |
196 | op => 'and', |
197 | args => [ |
198 | field_op_value( upper(mk_name('hostname')), |
199 | in => [ map { |
200 | upper(mk_value($_)) |
201 | } qw/ntf avd bvd 123/ ] |
202 | ), |
203 | $ticket_or_eq, |
204 | ] |
205 | }; |
206 | |
207 | eq_or_diff |
208 | $visitor->select_ast( |
209 | 'test', '*', [ { ticket => [11, 12, 13], |
210 | hostname => { in => ['ntf', 'avd', 'bvd', '123'] } |
211 | } |
212 | ] |
213 | ), |
214 | { -type => 'select', |
627dcb62 |
215 | columns => [ { -type => 'identifier', elements => ['*'] } ], |
216 | tablespec => { -type => 'identifier', elements => ['test'] }, |
0073ca43 |
217 | where => $hostname_and_ticket |
218 | }, |
219 | "Complex AST mixing arrays+hashes with convert('UPPER')"; |
220 | |
221 | my $tack_between = { |
222 | -type => 'expr', |
223 | op => 'between', |
224 | args => [ |
225 | upper(mk_name('tack')), |
226 | upper(mk_value('tick')), |
227 | upper(mk_value('tock')), |
228 | ] |
229 | }; |
230 | |
231 | eq_or_diff |
232 | $visitor->select_ast( |
233 | 'test', '*', [ { ticket => [11, 12, 13], |
234 | hostname => { in => ['ntf', 'avd', 'bvd', '123'] } |
235 | }, |
236 | { tack => { between => [qw/tick tock/] } } |
237 | ] |
238 | ), |
239 | { -type => 'select', |
627dcb62 |
240 | columns => [ { -type => 'identifier', elements => ['*'] } ], |
241 | tablespec => { -type => 'identifier', elements => ['test'] }, |
0073ca43 |
242 | where => { |
243 | -type => 'expr', |
244 | op => 'or', |
245 | args => [ |
246 | $hostname_and_ticket, |
247 | $tack_between, |
248 | ] |
249 | } |
250 | }, |
251 | "Complex AST mixing [ {a => [1,2],b => 3}, { c => 4 }]"; |
252 | |
253 | my $a_or_eq = { |
254 | -type => 'expr', |
255 | op => 'or', |
256 | args => [ map { |
257 | { -type => 'expr', op => '==', args => [ upper(mk_name('a')), upper(mk_value($_)) ] } |
258 | } qw/b c d/ ] |
259 | }; |
260 | |
261 | my $e_ne = { |
262 | -type => 'expr', |
263 | op => 'or', |
264 | args => [ map { |
265 | { -type => 'expr', op => '!=', args => [ upper(mk_name('e')), upper(mk_value($_)) ] } |
266 | } qw/f g/ ] |
267 | }; |
268 | |
269 | eq_or_diff |
270 | $visitor->select_ast( |
271 | 'test', '*', [ { ticket => [11, 12, 13], |
272 | hostname => { in => ['ntf', 'avd', 'bvd', '123'] } |
273 | }, |
274 | { tack => { between => [qw/tick tock/] } }, |
275 | { a => [qw/b c d/], |
276 | e => { '!=', [qw(f g)] }, |
277 | } |
278 | ] |
279 | ), |
280 | { -type => 'select', |
627dcb62 |
281 | columns => [ { -type => 'identifier', elements => ['*'] } ], |
282 | tablespec => { -type => 'identifier', elements => ['test'] }, |
0073ca43 |
283 | where => { |
284 | -type => 'expr', |
285 | op => 'or', |
286 | args => [ |
287 | $hostname_and_ticket, |
288 | $tack_between, |
289 | { -type => 'expr', op => 'and', args => [ $a_or_eq, $e_ne ] } |
290 | ] |
291 | } |
292 | }, |
293 | "Complex AST mixing [ {a => [1,2],b => 3}, { c => 4 }, { d => [5,6,7], e => { '!=' => [8,9] } } ]"; |
294 | |
295 | |
03f6671a |
296 | eq_or_diff |
297 | $visitor->select_ast( |
298 | 'test', '*', [ { ticket => [11, 12, 13], |
299 | hostname => { in => ['ntf', 'avd', 'bvd', '123'] } }, |
0073ca43 |
300 | { tack => { between => [qw/tick tock/] } }, |
301 | { a => [qw/b c d/], |
302 | e => { '!=', [qw(f g)] }, |
303 | q => { 'not in', [14..20] } |
304 | } |
03f6671a |
305 | ] |
306 | ), |
307 | { -type => 'select', |
627dcb62 |
308 | columns => [ { -type => 'identifier', elements => ['*'] } ], |
309 | tablespec => { -type => 'identifier', elements => ['test'] }, |
0073ca43 |
310 | where => { |
311 | -type => 'expr', |
312 | op => 'or', |
313 | args => [ |
314 | $hostname_and_ticket, |
315 | $tack_between, |
316 | { -type => 'expr', op => 'and', args => [ |
317 | $a_or_eq, |
318 | $e_ne, |
319 | { -type => 'expr', |
320 | op => 'not_in', |
321 | args => [ |
322 | upper(mk_name('q')), |
323 | map { upper(mk_value($_)) } 14..20 |
324 | ] |
325 | } |
03f6671a |
326 | ] } |
0073ca43 |
327 | ] |
328 | } |
03f6671a |
329 | }, |
0073ca43 |
330 | "Complex AST [ {a => [1,2],b => 3}, { c => 4 }, { d => [5,6,7], e => { '!=' => [8,9] }, q => {'not in' => [10,11] } } ]"; |
00f4d23e |
331 | |
03f6671a |
332 | |
333 | sub upper { expr(UPPER => @_) } |
334 | |
335 | sub expr { |
336 | my ($op, @args) = @_; |
337 | |
338 | return { |
339 | -type => 'expr', |
340 | op => $op, |
341 | args => [@args] |
342 | }; |
343 | } |
344 | |