From: Ash Berlin Date: Sat, 14 Mar 2009 13:22:54 +0000 (+0000) Subject: Make join tests behave X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=f7dc453655519b10686efe72191a65fbccef8914;p=dbsrgits%2FSQL-Abstract-2.0-ish.git Make join tests behave --- diff --git a/Makefile.PL b/Makefile.PL index 82f6194..bb8e6fc 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -7,6 +7,7 @@ name 'SQL-Abstract'; requires 'Moose' => '0.71'; requires 'MooseX::Method::Signatures' => '0.13_804d1448'; requires 'MooseX::Declare' => '0.09'; +requires 'MooseX::Types::Structured' => '0.9'; test_requires 'Test::More'; test_requires 'Test::Differences'; diff --git a/lib/SQL/Abstract.pm b/lib/SQL/Abstract.pm index d3b517c..e04c863 100644 --- a/lib/SQL/Abstract.pm +++ b/lib/SQL/Abstract.pm @@ -1,13 +1,11 @@ use MooseX::Declare; - class SQL::Abstract { use Carp qw/croak/; use Data::Dump qw/pp/; use Moose::Util::TypeConstraints; - use MooseX::Types -declare => [qw/NameSeparator/]; use MooseX::Types::Moose qw/ArrayRef Str Int HashRef CodeRef/; use MooseX::AttributeHelpers; use SQL::Abstract::Types qw/NameSeparator QuoteChars AST HashAST ArrayAST/; @@ -138,6 +136,8 @@ class SQL::Abstract { } method dispatch (AST $ast) { + + # I want multi methods! my $tag; if (is_ArrayAST($ast)) { diff --git a/lib/SQL/Abstract/AST/v1.pm b/lib/SQL/Abstract/AST/v1.pm index 0ce6e9f..a8860b5 100644 --- a/lib/SQL/Abstract/AST/v1.pm +++ b/lib/SQL/Abstract/AST/v1.pm @@ -69,18 +69,14 @@ 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); + my @output = $self->dispatch($ast->{lhs}); push @output, uc $type if $type; - push @output, "JOIN", $self->dispatch($to); + push @output, "JOIN", $self->dispatch($ast->{rhs}); push @output, exists $ast->{on} diff --git a/t/200_join.t b/t/200_join.t index 7cf9be8..9ba74b6 100644 --- a/t/200_join.t +++ b/t/200_join.t @@ -8,31 +8,34 @@ use_ok('SQL::Abstract') or BAIL_OUT( "$@" ); my $sqla = SQL::Abstract->create(1); +my $foo = {-type => name => args => [qw/foo/]}; +my $bar = {-type => name => args => [qw/bar/]}, +my $fnord = {-type => name => args => [qw/fnord/]}; + +my $foo_id = { -type => 'name', args => [qw/foo id/] }; +my $me_foo_id = { -type => 'name', args => [qw/me foo_id/] }; + is $sqla->dispatch( { -type => 'join', - args => [ - {-type => name => args => [qw/bar/]}, - {-type => name => args => [qw/foo/]}, - ], + lhs => $bar, + rhs => $foo, on => { -type => 'expr', op => '==', - args => [ - { -type => 'name', args => [qw/foo id/] }, - { -type => 'name', args => [qw/me foo_id/] }, - ] + args => [ $foo_id, $me_foo_id ] } } ), "bar JOIN foo ON (foo.id = me.foo_id)", "simple join clause"; + +$foo_id = { -type => 'name', args => [qw/foo_id/] }; + is $sqla->dispatch( { -type => 'join', - args => [ - {-type => name => args => [qw/fnord/]}, - {-type => 'alias', ident => {-type => name => args => [qw/foo/]}, as => 'bar' } - ], - using => { -type => 'name', args => [qw/foo_id/] }, + lhs => $fnord, + rhs => {-type => 'alias', ident => $foo, as => 'bar' }, + using => $foo_id } ), "fnord JOIN foo AS bar USING (foo_id)", "using join clause"; @@ -41,11 +44,9 @@ is $sqla->dispatch( 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/] }, + lhs => $fnord, + rhs => {-type => 'alias', ident => $foo, as => 'bar' }, + using => $foo_id } ), "fnord LEFT JOIN foo AS bar USING (foo_id)", "using left join clause"; diff --git a/t/201_select.t b/t/201_select.t index e3b3aab..861e262 100644 --- a/t/201_select.t +++ b/t/201_select.t @@ -28,10 +28,8 @@ is $sqla->dispatch( ], tablespec => { -type => 'join', - args => [ - {-type => 'alias', ident => {-type => 'name', args => [qw/foo/]}, as => 'me' }, - {-type => 'name', args => [qw/bar/] }, - ], + lhs => {-type => 'alias', ident => {-type => 'name', args => [qw/foo/]}, as => 'me' }, + rhs => {-type => 'name', args => [qw/bar/] }, on => { -type => 'expr', op => '==',