5 use lib "$FindBin::Bin/../../lib";
6 use Test::SQL::Abstract::Util qw/
14 use SQL::Abstract::Compat;
16 use Test::More tests => 17;
17 use Test::Differences;
19 ok(my $visitor = SQL::Abstract::Compat->new);
22 my $foo_id = { -type => 'identifier', elements => [qw/foo/] };
23 my $bar_id = { -type => 'identifier', elements => [qw/bar/] };
25 my $foo_eq_1 = field_op_value($foo_id, '==', 1);
26 my $bar_eq_str = field_op_value($bar_id, '==', 'some str');
29 $visitor->recurse_where({ foo => 1 }),
36 $visitor->recurse_where({ foo => 1, bar => 'some str' }),
47 $visitor->recurse_where({ -or => { foo => 1, bar => 'some str' } }),
59 $visitor->recurse_where([ -and => { foo => 1, bar => 'some str' } ]),
67 "-and as first element of array";
71 $visitor->recurse_where([ -and => { foo => 1, bar => 'some str' }, { foo => 1} ]),
85 "-and as first element of array + hash";
88 $visitor->recurse_where({ foo => { '!=' => 'bar' } }),
89 field_op_value($foo_id, '!=', 'bar'),
90 "foo => { '!=' => 'bar' }";
93 $visitor->recurse_where({ foo => [ 1, 'bar' ] }),
98 field_op_value($foo_id, '==', 'bar'),
101 "foo => [ 1, 'bar' ]";
104 $visitor->recurse_where({ foo => { -in => [ 1, 'bar' ] } }),
109 { -type => 'value', value => 1 },
110 { -type => 'value', value => 'bar' },
113 "foo => { -in => [ 1, 'bar' ] }";
116 $visitor->recurse_where({ foo => { -not_in => [ 1, 'bar' ] } }),
121 { -type => 'value', value => 1 },
122 { -type => 'value', value => 'bar' },
125 "foo => { -not_in => [ 1, 'bar' ] }";
128 $visitor->recurse_where({ foo => { -in => [ ] } }),
135 "foo => { -in => [ ] }";
137 my $worker_eq = sub {
142 { -type => 'identifier', elements => ['worker'] },
143 { -type => 'value', value => $_[0] },
149 $visitor->recurse_where( {
151 worker => ['nwiger', 'rcwe', 'sfz'],
152 status => { '!=', 'completed' }
157 field_op_value(qw/requestor == inna/),
158 field_op_value(qw/status != completed/),
162 field_op_value(qw/worker == nwiger/),
163 field_op_value(qw/worker == rcwe/),
164 field_op_value(qw/worker == sfz/),
172 $visitor->convert('UPPER');
178 field_op_value( upper(mk_name('ticket')), '==', upper(mk_value(11))),
179 field_op_value( upper(mk_name('ticket')), '==', upper(mk_value(12))),
180 field_op_value( upper(mk_name('ticket')), '==', upper(mk_value(13))),
185 $visitor->select_ast(
186 'test', '*', [ { ticket => [11, 12, 13] } ]
189 columns => [ { -type => 'identifier', elements => ['*'] } ],
190 tablespec => { -type => 'identifier', elements => ['test'] },
191 where => $ticket_or_eq
193 "Complex AST with convert('UPPER')";
195 my $hostname_and_ticket = {
199 field_op_value( upper(mk_name('hostname')),
202 } qw/ntf avd bvd 123/ ]
209 $visitor->select_ast(
210 'test', '*', [ { ticket => [11, 12, 13],
211 hostname => { in => ['ntf', 'avd', 'bvd', '123'] }
216 columns => [ { -type => 'identifier', elements => ['*'] } ],
217 tablespec => { -type => 'identifier', elements => ['test'] },
218 where => $hostname_and_ticket
220 "Complex AST mixing arrays+hashes with convert('UPPER')";
226 upper(mk_name('tack')),
227 upper(mk_value('tick')),
228 upper(mk_value('tock')),
233 $visitor->select_ast(
234 'test', '*', [ { ticket => [11, 12, 13],
235 hostname => { in => ['ntf', 'avd', 'bvd', '123'] }
237 { tack => { between => [qw/tick tock/] } }
241 columns => [ { -type => 'identifier', elements => ['*'] } ],
242 tablespec => { -type => 'identifier', elements => ['test'] },
247 $hostname_and_ticket,
252 "Complex AST mixing [ {a => [1,2],b => 3}, { c => 4 }]";
258 { -type => 'expr', op => '==', args => [ upper(mk_name('a')), upper(mk_value($_)) ] }
266 { -type => 'expr', op => '!=', args => [ upper(mk_name('e')), upper(mk_value($_)) ] }
271 $visitor->select_ast(
272 'test', '*', [ { ticket => [11, 12, 13],
273 hostname => { in => ['ntf', 'avd', 'bvd', '123'] }
275 { tack => { between => [qw/tick tock/] } },
277 e => { '!=', [qw(f g)] },
282 columns => [ { -type => 'identifier', elements => ['*'] } ],
283 tablespec => { -type => 'identifier', elements => ['test'] },
288 $hostname_and_ticket,
290 { -type => 'expr', op => 'and', args => [ $a_or_eq, $e_ne ] }
294 "Complex AST mixing [ {a => [1,2],b => 3}, { c => 4 }, { d => [5,6,7], e => { '!=' => [8,9] } } ]";
298 $visitor->select_ast(
299 'test', '*', [ { ticket => [11, 12, 13],
300 hostname => { in => ['ntf', 'avd', 'bvd', '123'] } },
301 { tack => { between => [qw/tick tock/] } },
303 e => { '!=', [qw(f g)] },
304 q => { 'not in', [14..20] }
309 columns => [ { -type => 'identifier', elements => ['*'] } ],
310 tablespec => { -type => 'identifier', elements => ['test'] },
315 $hostname_and_ticket,
317 { -type => 'expr', op => 'and', args => [
324 map { upper(mk_value($_)) } 14..20
331 "Complex AST [ {a => [1,2],b => 3}, { c => 4 }, { d => [5,6,7], e => { '!=' => [8,9] }, q => {'not in' => [10,11] } } ]";
334 sub upper { mk_expr(UPPER => @_) }