],
};
- if (is_Str($value)) {
- push @{$ret->{args}}, { -type => 'value', value => $value };
+ if (is_HashRef($value)) {
+ my ($op, @rest) = keys %$value;
+ confess "Don't know how to handle " . dump($value) . " (too many keys)"
+ if @rest;
+
+ $ret->{op} = $op;
+ push @{$ret->{args}}, $self->value($value->{$op});
+
+ }
+ elsif (is_ArrayRef($value)) {
+ # Return an or clause, sort of.
+ return {
+ -type => 'expr',
+ op => 'or',
+ args => [ map {
+ {
+ -type => 'expr',
+ op => '==',
+ args => [
+ { -type => 'name', args => [$key] },
+ $self->value($_)
+ ],
+ }
+ } @$value ]
+ };
+ }
+ else {
+ push @{$ret->{args}}, $self->value($value);
}
return $ret;
}
+ method value($value) returns (AST) {
+ if (is_Str($value)) {
+ return { -type => 'value', value => $value };
+ }
+ else {
+ confess "Don't know how to handle " . dump($value);
+ }
+ }
+
};
use SQL::Abstract::AST::Compat;
-use Test::More tests => 6;
+use Test::More tests => 8;
use Test::Differences;
ok(my $visitor = SQL::Abstract::AST::Compat->new);
+my $foo_id = { -type => 'name', args => [qw/foo/] };
+my $bar_id = { -type => 'name', args => [qw/bar/] };
+
my $foo_eq_1 = {
-type => 'expr',
op => '==',
args => [
- { -type => 'name', args => [qw/foo/] },
+ $foo_id,
{ -type => 'value', value => 1 }
]
};
-type => 'expr',
op => '==',
args => [
- { -type => 'name', args => [qw/bar/] },
+ $bar_id,
{ -type => 'value', value => 'some str' }
]
};
$foo_eq_1,
]
},
- "-and as first element of array";
+ "-and as first element of array + hash";
+
+eq_or_diff
+ $visitor->generate({ foo => { '!=' => 'bar' } }),
+ { -type => 'expr',
+ op => '!=',
+ args => [
+ $foo_id,
+ { -type => 'value', value => 'bar' },
+ ]
+ },
+ "foo => { '!=' => 'bar' }";
+
+eq_or_diff
+ $visitor->generate({ foo => [ 1, 'bar' ] }),
+ { -type => 'expr',
+ op => 'or',
+ args => [
+ $foo_eq_1,
+ { -type => 'expr',
+ op => '==',
+ args => [
+ $foo_id,
+ { -type => 'value', value => 'bar' },
+ ]
+ },
+ ],
+ },
+ "foo => [ 1, 'bar' ]";
+