From: Ash Berlin Date: Thu, 12 Mar 2009 23:18:45 +0000 (+0000) Subject: Make more stuff work with HashAST X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e7996b3ab83f90865b6cf01a29981ecd254ad9f0;p=dbsrgits%2FSQL-Abstract-2.0-ish.git Make more stuff work with HashAST --- diff --git a/lib/SQL/Abstract/AST/v1.pm b/lib/SQL/Abstract/AST/v1.pm index 3135086..8f85c06 100644 --- a/lib/SQL/Abstract/AST/v1.pm +++ b/lib/SQL/Abstract/AST/v1.pm @@ -25,6 +25,7 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract { name true false + expr / }; } @@ -155,10 +156,9 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract { my @output; foreach ( @{$ast->{args}} ) { - croak "invalid component in where clause: $_" unless is_ArrayRef($_); - my $op = $_->[0]; + croak "invalid component in where clause: $_" unless is_HashAST($_); - if ($op =~ /^-(and|or)$/) { + if ($_->{-type} eq 'expr' && $_->{op} =~ /^-(and|or)$/) { my $sub_prio = $SQL::Abstract::PRIO{$1}; if ($sub_prio <= $prio) { @@ -185,7 +185,8 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract { croak "'$op' is not a valid clause in a where AST" if $op =~ /^-/; - croak "'$op' is not a valid operator"; + use Devel::PartialDump qw/dump/; + croak "'$op' is not a valid operator in " . dump($ast); } diff --git a/t/100_where_basic.t b/t/100_where_basic.t index e175912..d8f6783 100644 --- a/t/100_where_basic.t +++ b/t/100_where_basic.t @@ -41,26 +41,28 @@ is $sqla->dispatch( ), "me.id > ?", "simple expr clause"; +my $cols = [ + { -type => 'expr', + op => '>', + args => [ + {-type => 'name', args => [qw/me id/]}, + {-type => 'value', value => 500 } + ] + }, + { -type => 'expr', + op => '==', + args => [ + {-type => 'name', args => [qw/me name/]}, + {-type => 'value', value => '200' } + ] + }, +]; + eq_or_diff( [ SQL::Abstract->generate( { -ast_version => 1, -type => 'expr', op => 'and', - args => [ - { -type => 'expr', - op => '>', - args => [ - {-type => 'name', args => [qw/me id/]}, - {-type => 'value', value => 500 } - ] - }, - { -type => 'expr', - op => '==', - args => [ - {-type => 'name', args => [qw/me name/]}, - {-type => 'value', value => '200' } - ] - }, - ] + args => $cols, } ) ], [ "me.id > ? AND me.name = ?", @@ -70,40 +72,39 @@ eq_or_diff( [ SQL::Abstract->generate( ], "Where with binds" ); -__END__ is $sqla->dispatch( - [ -where => -or => - [ '>', [-name => qw/me id/], [-value => 500 ] ], - [ '==', [-name => qw/me name/], [-value => '200' ] ], - ] -), "WHERE me.id > ? OR me.name = ?", + { -type => 'expr', op => 'or', args => $cols } +), "me.id > ? OR me.name = ?", "where clause (simple or)"; is $sqla->dispatch( - [ -where => -or => - [ '>', [-name => qw/me id/], [-value => 500 ] ], - [ -or => - [ '==', [-name => qw/me name/], [-value => '200' ] ], - [ '==', [-name => qw/me name/], [-value => '100' ] ] - ] - ] -), "WHERE me.id > ? OR me.name = ? OR me.name = ?", + { -type => 'expr', op => 'or', + args => [ + { -type => 'expr', op => '==', + args => [ {-type => 'name', args => [qw/me name/] }, {-type => 'value', value => 500 } ] + }, + { -type => 'expr', op => 'or', args => $cols } + ] + } +), "me.name = ? OR me.id > ? OR me.name = ?", "where clause (nested or)"; is $sqla->dispatch( - [ -where => -or => - [ '==', [-name => qw/me id/], [-value => 500 ] ], - [ -and => - [ '>', [-name => qw/me name/], [-value => '200' ] ], - [ '<', [-name => qw/me name/], [-value => '100' ] ] - ] - ] -), "WHERE me.id = ? OR me.name > ? AND me.name < ?", + { -type => 'expr', op => 'or', + args => [ + { -type => 'expr', op => '==', + args => [ {-type => 'name', args => [qw/me name/] }, {-type => 'value', value => 500 } ] + }, + { -type => 'expr', op => 'and', args => $cols } + ] + } +), "me.name = ? OR me.id > ? AND me.name = ?", "where clause (inner and)"; +__END__ is $sqla->dispatch( [ -where => -and => [ '==', [-name => qw/me id/], [-value => 500 ] ],