From: Ash Berlin Date: Fri, 27 Mar 2009 09:35:37 +0000 (+0000) Subject: -in and -not_in support X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2e828b0b33834edb4e84acd0d61ee762da20d1b4;p=dbsrgits%2FSQL-Abstract-2.0-ish.git -in and -not_in support --- diff --git a/lib/SQL/Abstract/AST/Compat.pm b/lib/SQL/Abstract/AST/Compat.pm index 9159048..6ca78fd 100644 --- a/lib/SQL/Abstract/AST/Compat.pm +++ b/lib/SQL/Abstract/AST/Compat.pm @@ -100,7 +100,20 @@ class SQL::Abstract::AST::Compat { 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}); } @@ -129,12 +142,10 @@ class SQL::Abstract::AST::Compat { } 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); } diff --git a/t/compat/ast/01.t b/t/compat/ast/01.t index ff21dc0..830bad3 100644 --- a/t/compat/ast/01.t +++ b/t/compat/ast/01.t @@ -3,7 +3,7 @@ use warnings; 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); @@ -115,3 +115,37 @@ eq_or_diff }, "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 => [ ] }"; +