confess "Don't know how to handle " . dump($value) . " (too many keys)"
if @rest;
- $ret->{op} = $op;
+ # TODO: Validate the op?
+ if ($op =~ /^-([a-z_]+)$/i) {
+ $ret->{op} = lc $1;
+
+ if (is_ArrayRef($value->{$op})) {
+ push @{$ret->{args}}, $self->value($_)
+ for @{$value->{$op}};
+ return $ret;
+ }
+ }
+ else {
+ $ret->{op} = $op;
+ }
+
push @{$ret->{args}}, $self->value($value->{$op});
}
}
method value($value) returns (AST) {
- if (is_Str($value)) {
- return { -type => 'value', value => $value };
- }
- else {
- confess "Don't know how to handle " . dump($value);
- }
+ return { -type => 'value', value => $value }
+ if is_Str($value);
+
+ confess "Don't know how to handle terminal value " . dump($value);
}
use SQL::Abstract::AST::Compat;
-use Test::More tests => 8;
+use Test::More tests => 11;
use Test::Differences;
ok(my $visitor = SQL::Abstract::AST::Compat->new);
},
"foo => [ 1, 'bar' ]";
+eq_or_diff
+ $visitor->generate({ foo => { -in => [ 1, 'bar' ] } }),
+ { -type => 'expr',
+ op => 'in',
+ args => [
+ $foo_id,
+ { -type => 'value', value => 1 },
+ { -type => 'value', value => 'bar' },
+ ]
+ },
+ "foo => { -in => [ 1, 'bar' ] }";
+
+eq_or_diff
+ $visitor->generate({ foo => { -not_in => [ 1, 'bar' ] } }),
+ { -type => 'expr',
+ op => 'not_in',
+ args => [
+ $foo_id,
+ { -type => 'value', value => 1 },
+ { -type => 'value', value => 'bar' },
+ ]
+ },
+ "foo => { -not_in => [ 1, 'bar' ] }";
+
+eq_or_diff
+ $visitor->generate({ foo => { -in => [ ] } }),
+ { -type => 'expr',
+ op => 'in',
+ args => [
+ $foo_id,
+ ]
+ },
+ "foo => { -in => [ ] }";
+