From: Ash Berlin Date: Fri, 13 Mar 2009 19:59:58 +0000 (+0000) Subject: Make more of the expression (formerly basic where) tests pass X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9d7d069492444a5a00f7a43771c37f4752ff4144;p=dbsrgits%2FSQL-Abstract-2.0-ish.git Make more of the expression (formerly basic where) tests pass --- diff --git a/lib/SQL/Abstract/AST/v1.pm b/lib/SQL/Abstract/AST/v1.pm index 8f85c06..95a7e80 100644 --- a/lib/SQL/Abstract/AST/v1.pm +++ b/lib/SQL/Abstract/AST/v1.pm @@ -158,7 +158,7 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract { foreach ( @{$ast->{args}} ) { croak "invalid component in where clause: $_" unless is_HashAST($_); - if ($_->{-type} eq 'expr' && $_->{op} =~ /^-(and|or)$/) { + if ($_->{-type} eq 'expr' && $_->{op} =~ /^(and|or)$/) { my $sub_prio = $SQL::Abstract::PRIO{$1}; if ($sub_prio <= $prio) { @@ -182,11 +182,11 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract { return $code->($self, $ast); } - croak "'$op' is not a valid clause in a where AST" + croak "'$op' is not a valid AST type in an expression" if $op =~ /^-/; use Devel::PartialDump qw/dump/; - croak "'$op' is not a valid operator in " . dump($ast); + croak "'$op' is not a valid AST type in " . dump($ast); } @@ -209,16 +209,16 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract { method _in(HashAST $ast) { - my ($field,$values) = @{$ast->{args}}; + my ($field,@values) = @{$ast->{args}}; - my $not = ($ast->{op} =~ /^-not/) ? " NOT" : ""; + my $not = ($ast->{op} =~ /^not_/) ? " NOT" : ""; - return $self->_false if !defined $values || @$values == 0; + return $self->_false unless @values; return $self->_where_component($field) . - $not. + $not . " IN (" . - join(", ", map { $self->dispatch($_) } @$values ) . + join(", ", map { $self->dispatch($_) } @values ) . ")"; } diff --git a/t/100_where_basic.t b/t/100_expr_basic.t similarity index 57% rename from t/100_where_basic.t rename to t/100_expr_basic.t index d8f6783..f44d884 100644 --- a/t/100_where_basic.t +++ b/t/100_expr_basic.t @@ -1,7 +1,7 @@ use strict; use warnings; -use Test::More tests => 12; +use Test::More tests => 13; use Test::Differences; use_ok('SQL::Abstract') or BAIL_OUT( "$@" ); @@ -17,18 +17,7 @@ is $sqla->dispatch( ] } ), "me.id > ?", - "simple where clause"; - -is $sqla->dispatch( - { -type => 'expr', op => 'in', args => [ ] } -), "0 = 1", "emtpy -in"; - -is $sqla->dispatch( - { -type => 'expr', - op => 'in', - args => [ { -type => 'name', args => ['foo'] } ], - } -), "0 = 1", "emtpy -in"; + "simple expr clause"; is $sqla->dispatch( { -type => 'expr', @@ -77,7 +66,7 @@ eq_or_diff( [ SQL::Abstract->generate( is $sqla->dispatch( { -type => 'expr', op => 'or', args => $cols } ), "me.id > ? OR me.name = ?", - "where clause (simple or)"; + "expr clause (simple or)"; is $sqla->dispatch( @@ -90,7 +79,7 @@ is $sqla->dispatch( ] } ), "me.name = ? OR me.id > ? OR me.name = ?", - "where clause (nested or)"; + "expr clause (nested or)"; is $sqla->dispatch( { -type => 'expr', op => 'or', @@ -102,67 +91,90 @@ is $sqla->dispatch( ] } ), "me.name = ? OR me.id > ? AND me.name = ?", - "where clause (inner and)"; + "expr clause (inner and)"; -__END__ is $sqla->dispatch( - [ -where => -and => - [ '==', [-name => qw/me id/], [-value => 500 ] ], - [ -and => - [ '>', [-name => qw/me name/], [-value => '200' ] ], - [ '<', [-name => qw/me name/], [-value => '100' ] ] - ] - ] -), "WHERE me.id = ? AND me.name > ? AND me.name < ?", - "where clause (nested and)"; + { -type => 'expr', op => 'and', args => [ + { -type => 'expr', op => '==', args => [ + {-type => 'name', args => [qw/me id/] }, {-type => 'value', value => 200 } + ], + }, + { -type => 'expr', op => 'and', args => $cols } + ] + } +), "me.id = ? AND me.id > ? AND me.name = ?", + "expr clause (nested and)"; is $sqla->dispatch( - [ -where => -and => - [ '==', [-name => qw/me id/], [-value => 500 ] ], - [ -or => - [ '>', [-name => qw/me name/], [-value => '200' ] ], - [ '<', [-name => qw/me name/], [-value => '100' ] ] - ] - ] -), "WHERE me.id = ? AND (me.name > ? OR me.name < ?)", - "where clause (inner or)"; + { -type => 'expr', op => 'and', args => [ + { -type => 'expr', op => '==', args => [ + {-type => 'name', args => [qw/me id/] }, {-type => 'value', value => 200 } + ], + }, + { -type => 'expr', op => 'or', args => $cols } + ] + } +), "me.id = ? AND (me.id > ? OR me.name = ?)", + "expr clause (inner or)"; + +is $sqla->dispatch( + { -type => 'expr', op => 'in', args => [ ] } +), "0 = 1", "emtpy -in"; + +is $sqla->dispatch( + { -type => 'expr', + op => 'in', + args => [ { -type => 'name', args => ['foo'] } ], + } +), "0 = 1", "emtpy -in"; eq_or_diff( [SQL::Abstract->generate( - [ -ast_version => 1, - -where => - [ -in => - [-name => qw/me id/], - [-value => '100' ], - [-value => '200' ], - [-value => '300' ], + { -ast_version => 1, + -type => 'expr', + op => 'and', + args => [ + { -type => 'expr', + op => 'in', + args => [ + {-type => 'name', args => [qw/me id/] }, + {-type => 'value', value => 100 }, + {-type => 'value', value => 200 }, + {-type => 'value', value => 300 }, + ] + } ] - ] + } ) ], - [ "WHERE me.id IN (?, ?, ?)", - [ qw/100 200 300/] + [ "me.id IN (?, ?, ?)", + [ 100, 200, 300 ] ], - "where IN clause"); - + "IN expression"); eq_or_diff( [SQL::Abstract->generate( - [ -ast_version => 1, - -where => - [ -not_in => - [-name => qw/me id/], - [-value => '100' ], - [-value => '200' ], - [-value => '300' ], + { -ast_version => 1, + -type => 'expr', + op => 'and', + args => [ + { -type => 'expr', + op => 'not_in', + args => [ + {-type => 'name', args => [qw/me id/] }, + {-type => 'value', value => 100 }, + {-type => 'value', value => 200 }, + {-type => 'value', value => 300 }, + ] + } ] - ] + } ) ], - [ "WHERE me.id NOT IN (?, ?, ?)", - [ qw/100 200 300/] + [ "me.id NOT IN (?, ?, ?)", + [ 100, 200, 300 ] ], - "where NOT IN clause"); + "NOT IN clause");