From: Ash Berlin Date: Sat, 14 Mar 2009 13:50:57 +0000 (+0000) Subject: Select with WHERE X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e68f980b07ce769cf555ae62880281173fca251e;p=dbsrgits%2FSQL-Abstract-2.0-ish.git Select with WHERE --- diff --git a/lib/SQL/Abstract/AST/v1.pm b/lib/SQL/Abstract/AST/v1.pm index a8860b5..d0f4522 100644 --- a/lib/SQL/Abstract/AST/v1.pm +++ b/lib/SQL/Abstract/AST/v1.pm @@ -54,14 +54,15 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract { push @output, FROM => $self->dispatch($ast->{tablespec}) if exists $ast->{tablespec}; - for (qw//) { + for (qw/where having group_by/) { if (exists $ast->{$_}) { my $sub_ast = $ast->{$_}; - $sub_ast->{-type} = "$_" if is_HashRef($sub_ast); - confess "$_ option is not an AST" + + confess "$_ option is not an AST: " . dump($sub_ast) unless is_AST($sub_ast); - push @output, $self->dispatch($sub_ast); + my $meth = "__$_"; + push @output, $self->$meth($sub_ast); } } @@ -153,6 +154,11 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract { return "?"; } + # Not dispatchable to. + method __where(HashAST $ast) { + return "WHERE " . $self->_expr($ast); + } + # Perhaps badly named. handles 'and' and 'or' clauses method _recurse_where(HashAST $ast) { diff --git a/t/201_select.t b/t/201_select.t index 861e262..d27a327 100644 --- a/t/201_select.t +++ b/t/201_select.t @@ -1,19 +1,26 @@ use strict; use warnings; -use Test::More tests => 3; +use Test::More tests => 4; use Test::Differences; use_ok('SQL::Abstract') or BAIL_OUT( "$@" ); my $sqla = SQL::Abstract->create(1); +my $foo_as_me = { + -type => 'alias', + ident => {-type => 'name', args => [qw/foo/]}, + as => 'me' +}; +my $me_foo_id = { -type => 'name', args => [qw/me foo_id/] }; + is $sqla->dispatch( { -type => 'select', - tablespec => {-type => 'alias', ident => {-type => 'name', args => [qw/foo/]}, as => 'me' }, + tablespec => $foo_as_me, columns => [ { -type => 'name', args => [qw/me id/] }, - { -type => 'alias', ident => { -type => 'name', args => [qw/me foo_id/] }, as => 'foo' }, + { -type => 'alias', ident => $me_foo_id, as => 'foo' }, ] } ), "SELECT me.id, me.foo_id AS foo FROM foo AS me", @@ -23,12 +30,12 @@ is $sqla->dispatch( { -type => 'select', columns => [ { -type => 'name', args => [qw/me id/] }, - { -type => 'alias', ident => { -type => 'name', args => [qw/me foo_id/] }, as => 'foo' }, + { -type => 'alias', ident => $me_foo_id, as => 'foo' }, { -type => 'name', args => [qw/bar name/] }, ], tablespec => { -type => 'join', - lhs => {-type => 'alias', ident => {-type => 'name', args => [qw/foo/]}, as => 'me' }, + lhs => $foo_as_me, rhs => {-type => 'name', args => [qw/bar/] }, on => { -type => 'expr', @@ -45,4 +52,23 @@ is $sqla->dispatch( ), "SELECT me.id, me.foo_id AS foo, bar.name FROM foo AS me JOIN bar ON (bar.id = me.bar_id)", "select with join clause"; -__END__ + +is $sqla->dispatch( + { -type => 'select', + columns => [ + { -type => 'name', args => [qw/me */] }, + ], + tablespec => $foo_as_me, + where => { + -type => 'expr', + op => '==', + args => [ + {-type => 'name', args => [qw/me id/]}, + {-type => 'value', value => 1 }, + ] + } + } + + +), "SELECT me.* FROM foo AS me WHERE me.id = ?", + "select with where";