From: Ash Berlin Date: Fri, 27 Mar 2009 09:26:11 +0000 (+0000) Subject: Handle { foo => [ 1, 'bar' ] } and { foo => { '!=' => 'bar' } } X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c6039348d36f099cc680891120ba53fcc793ad6c;p=dbsrgits%2FSQL-Abstract-2.0-ish.git Handle { foo => [ 1, 'bar' ] } and { foo => { '!=' => 'bar' } } --- diff --git a/lib/SQL/Abstract/AST/Compat.pm b/lib/SQL/Abstract/AST/Compat.pm index 97e2fd3..9159048 100644 --- a/lib/SQL/Abstract/AST/Compat.pm +++ b/lib/SQL/Abstract/AST/Compat.pm @@ -95,13 +95,48 @@ class SQL::Abstract::AST::Compat { ], }; - 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); + } + } + }; diff --git a/t/compat/ast/01.t b/t/compat/ast/01.t index 16397b9..ff21dc0 100644 --- a/t/compat/ast/01.t +++ b/t/compat/ast/01.t @@ -3,16 +3,19 @@ use warnings; 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 } ] }; @@ -27,7 +30,7 @@ my $bar_eq_str = { -type => 'expr', op => '==', args => [ - { -type => 'name', args => [qw/bar/] }, + $bar_id, { -type => 'value', value => 'some str' } ] }; @@ -82,4 +85,33 @@ eq_or_diff $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' ]"; +