From: Ash Berlin Date: Sat, 14 Mar 2009 00:34:29 +0000 (+0000) Subject: LEFT JOIN + rework join tests X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=d0ad3a92f4b801c12ea2a25dfeb112ab154cc8cf;p=dbsrgits%2FSQL-Abstract-2.0-ish.git LEFT JOIN + rework join tests --- diff --git a/lib/SQL/Abstract/AST/v1.pm b/lib/SQL/Abstract/AST/v1.pm index 2596c63..0ce6e9f 100644 --- a/lib/SQL/Abstract/AST/v1.pm +++ b/lib/SQL/Abstract/AST/v1.pm @@ -69,18 +69,26 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract { } method _join(HashRef $ast) { + confess "'args' option to join should be an array ref, not " . dump($ast->{args}) + unless is_ArrayRef($ast->{args}); + my ($from, $to) = @{ $ast->{args} }; + + # TODO: Validate join type + my $type = $ast->{join_type} || ""; - my $output = $self->dispatch($from) - . ' JOIN ' - . $self->dispatch($to); + my @output = $self->dispatch($from); + + push @output, uc $type if $type; + push @output, "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"); + push @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; + return join(" ", @output); } diff --git a/t/200_join.t b/t/200_join.t index 7095518..7cf9be8 100644 --- a/t/200_join.t +++ b/t/200_join.t @@ -1,7 +1,7 @@ use strict; use warnings; -use Test::More tests => 3; +use Test::More tests => 4; use Test::Differences; use_ok('SQL::Abstract') or BAIL_OUT( "$@" ); @@ -10,7 +10,10 @@ my $sqla = SQL::Abstract->create(1); is $sqla->dispatch( { -type => 'join', - tablespec => {-type => name => args => [qw/foo/]}, + args => [ + {-type => name => args => [qw/bar/]}, + {-type => name => args => [qw/foo/]}, + ], on => { -type => 'expr', op => '==', @@ -20,13 +23,29 @@ is $sqla->dispatch( ] } } -), "JOIN foo ON (foo.id = me.foo_id)", +), "bar JOIN foo ON (foo.id = me.foo_id)", "simple join clause"; is $sqla->dispatch( { -type => 'join', - tablespec => {-type => 'alias', ident => {-type => name => args => [qw/foo/]}, as => 'bar' }, + args => [ + {-type => name => args => [qw/fnord/]}, + {-type => 'alias', ident => {-type => name => args => [qw/foo/]}, as => 'bar' } + ], using => { -type => 'name', args => [qw/foo_id/] }, } -), "JOIN foo AS bar USING (foo_id)", +), "fnord JOIN foo AS bar USING (foo_id)", "using join clause"; + + +is $sqla->dispatch( + { -type => 'join', + join_type => 'LEFT', + args => [ + {-type => name => args => [qw/fnord/]}, + {-type => 'alias', ident => {-type => name => args => [qw/foo/]}, as => 'bar' } + ], + using => { -type => 'name', args => [qw/foo_id/] }, + } +), "fnord LEFT JOIN foo AS bar USING (foo_id)", + "using left join clause";