From: Ash Berlin Date: Thu, 12 Mar 2009 00:00:20 +0000 (+0000) Subject: Port more stuff to use HashAST X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=1b85673a42cc17159f0ec4cc339d01f4d3231f38;p=dbsrgits%2FSQL-Abstract-2.0-ish.git Port more stuff to use HashAST --- diff --git a/lib/SQL/Abstract.pm b/lib/SQL/Abstract.pm index ad9b32e..d689e58 100644 --- a/lib/SQL/Abstract.pm +++ b/lib/SQL/Abstract.pm @@ -30,7 +30,7 @@ class SQL::Abstract { '==' => '=', '!=' => '!=', # LIKE is always "field LIKE " - '-like' => 'IN', + '-like' => 'LIKE', '-not_like' => 'NOT LIKE', ); diff --git a/lib/SQL/Abstract/AST/v1.pm b/lib/SQL/Abstract/AST/v1.pm index 526092b..7aa1711 100644 --- a/lib/SQL/Abstract/AST/v1.pm +++ b/lib/SQL/Abstract/AST/v1.pm @@ -16,9 +16,9 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract { override _build_where_dispatch_table { return { %{super()}, - -in => $self->can('_in'), - -not_in => $self->can('_in'), - map { +"-$_" => $self->can("_$_") } qw/ + in => $self->can('_in'), + not_in => $self->can('_in'), + map { +"$_" => $self->can("_$_") } qw/ value name true @@ -136,10 +136,9 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract { } - method _value(ArrayAST $ast) { - my ($undef, $value) = @$ast; + method _value(HashAST $ast) { - $self->add_bind($value); + $self->add_bind($ast->{value}); return "?"; } @@ -184,8 +183,8 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract { return join(" $OP ", @output); } - method _where_component(ArrayRef $ast) { - my $op = $ast->[0]; + method _where_component(HashAST $ast) { + my $op = $ast->{-type}; if (my $code = $self->lookup_where_dispatch($op)) { @@ -199,9 +198,16 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract { } + method _expr(HashAST $ast) { + my $op = $ast->{op}; + my $meth = $self->lookup_where_dispatch($op) || confess "Invalid operator '$op'"; + + $meth->($self, $ast); + } - method _binop(ArrayRef $ast) { - my ($op, $lhs, $rhs) = @$ast; + method _binop(HashAST $ast) { + my ($lhs, $rhs) = @{$ast->{args}}; + my $op = $ast->{op}; join (' ', $self->_where_component($lhs), $self->binop_mapping($op) || croak("Unknown binary operator $op"), diff --git a/t/100_where_basic.t b/t/100_where_basic.t index d508ada..2012ed8 100644 --- a/t/100_where_basic.t +++ b/t/100_where_basic.t @@ -9,12 +9,16 @@ use_ok('SQL::Abstract') or BAIL_OUT( "$@" ); my $sqla = SQL::Abstract->create(1); is $sqla->dispatch( - [ -where => - [ '>', [-name => qw/me id/], [-value => 500 ] ] - ] -), "WHERE me.id > ?", + { -type => 'expr', + op => '>', + args => [ + {-type => name => args => [qw/me id/] }, + { -type => 'value', value => 500 } + ] + } +), "me.id > ?", "simple where clause"; - +__END__ is $sqla->dispatch( [ -in => [ ] ] ), "0 = 1", "emtpy -in";