From: Ash Berlin Date: Sat, 14 Mar 2009 00:26:43 +0000 (+0000) Subject: Re-work SELECT X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=64c3203158006b6e4c1be93052fbcbae1ac8e9da;p=dbsrgits%2FSQL-Abstract-2.0-ish.git Re-work SELECT --- diff --git a/lib/SQL/Abstract/AST/v1.pm b/lib/SQL/Abstract/AST/v1.pm index 3914388..2596c63 100644 --- a/lib/SQL/Abstract/AST/v1.pm +++ b/lib/SQL/Abstract/AST/v1.pm @@ -45,7 +45,7 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract { confess "'columns' should be an array ref, not " . dump($ast->{columns}) unless is_ArrayRef($ast->{columns}); - my $cols = join ($self->list_separator, map { $self->dispatch($_) } @{ $ast->{columns}}); + my $cols = $self->_list({-type => 'list', args => $ast->{columns} }); my @output = ( SELECT => $cols @@ -68,10 +68,20 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract { return join(' ', @output); } - method _where(ArrayAST $ast) { - my (undef, @clauses) = @$ast; + method _join(HashRef $ast) { + my ($from, $to) = @{ $ast->{args} }; - return 'WHERE ' . $self->_recurse_where(\@clauses); + my $output = $self->dispatch($from) + . ' JOIN ' + . $self->dispatch($to); + + $output .= exists $ast->{on} + ? ' ON (' . $self->_expr( $ast->{on} ) + : ' USING (' .$self->dispatch($ast->{using} || croak "No 'on' or 'join' clause passed to -join"); + + $output .= ")"; + return $output; + } method _order_by(AST $ast) { @@ -116,18 +126,6 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract { return $ret; } - method _join(HashRef $ast) { - - my $output = 'JOIN ' . $self->dispatch($ast->{tablespec}); - - $output .= exists $ast->{on} - ? ' ON (' . $self->_expr( $ast->{on} ) - : ' USING (' .$self->dispatch($ast->{using} || croak "No 'on' or 'join' clause passed to -join"); - - $output .= ")"; - return $output; - - } method _list(AST $ast) { my @items = @{$ast->{args}}; diff --git a/t/201_select.t b/t/201_select.t index 61b57d0..e3b3aab 100644 --- a/t/201_select.t +++ b/t/201_select.t @@ -19,31 +19,32 @@ is $sqla->dispatch( ), "SELECT me.id, me.foo_id AS foo FROM foo AS me", "simple select clause"; -__END__ is $sqla->dispatch( { -type => 'select', - tablespec => {-type => 'alias', ident => {-type => 'name', args => [qw/foo/]}, as => 'me' }, columns => [ { -type => 'name', args => [qw/me id/] }, { -type => 'alias', ident => { -type => 'name', args => [qw/me foo_id/] }, as => 'foo' }, { -type => 'name', args => [qw/bar name/] }, - ] + ], + tablespec => { + -type => 'join', + args => [ + {-type => 'alias', ident => {-type => 'name', args => [qw/foo/]}, as => 'me' }, + {-type => 'name', args => [qw/bar/] }, + ], + on => { + -type => 'expr', + op => '==', + args => [ + {-type => 'name', args => [qw/bar id/]}, + {-type => 'name', args => [qw/me bar_id/]} + ], + } + }, } - { -type => 'select', - from => [-alias => [-name => 'foo'] => 'me' ], - columns => [ -list => - [ -name => qw/me id/ ], - [ -alias => [ -name => qw/me foo_id/ ], 'foo' ], - [ -name => qw/bar name/ ], - ], - join => { - tablespec => [-name => qw/bar/], - on => [ '==', [-name => qw/bar id/], [ -name => qw/me bar_id/ ] ], - } - } ), "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__