From: Ash Berlin Date: Wed, 11 Mar 2009 23:48:34 +0000 (+0000) Subject: Mid-refactor: Convert from array refs to hashrefs for storing the AST in X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=7a56723e6b43b3f92f63060a018b42ab25037c10;p=dbsrgits%2FSQL-Abstract-2.0-ish.git Mid-refactor: Convert from array refs to hashrefs for storing the AST in --- diff --git a/lib/SQL/Abstract/AST/v1.pm b/lib/SQL/Abstract/AST/v1.pm index 10c3ac3..526092b 100644 --- a/lib/SQL/Abstract/AST/v1.pm +++ b/lib/SQL/Abstract/AST/v1.pm @@ -66,13 +66,13 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract { return 'WHERE ' . $self->_recurse_where(\@clauses); } - method _order_by(ArrayAST $ast) { - my (undef, @clauses) = @$ast; - + method _order_by(AST $ast) { + my @clauses = @{$ast->{order_by}}; + my @output; for (@clauses) { - if ($_->[0] =~ /^-(asc|desc)$/) { + if (is_ArrayRef($_) && $_->[0] =~ /^-(asc|desc)$/) { my $o = $1; push @output, $self->dispatch($_->[1]) . " " . uc($o); next; @@ -83,8 +83,8 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract { return "ORDER BY " . join(", ", @output); } - method _name(ArrayAST $ast) { - my (undef, @names) = @$ast; + method _name(AST $ast) { + my @names = @{$ast->{args}}; my $sep = $self->name_separator; my $quote = $self->is_quoting @@ -121,22 +121,18 @@ class SQL::Abstract::AST::v1 extends SQL::Abstract { } - method _list(ArrayAST $ast) { - my (undef, @items) = @$ast; + method _list(AST $ast) { + my @items = @{$ast->{args}}; return join( $self->list_separator, map { $self->dispatch($_) } @items); } - method _alias(ArrayAST $ast) { - my (undef, $alias, $as) = @$ast; - - confess "Not enough paremeters to -alias" - unless defined $as; - + method _alias(AST $ast) { + # TODO: Maybe we want qq{ AS "$as"} here - return $self->dispatch($alias) . " AS $as"; + return $self->dispatch($ast->{ident}) . " AS " . $ast->{as}; } diff --git a/t/001_basic.t b/t/001_basic.t index 95b45e2..728d948 100644 --- a/t/001_basic.t +++ b/t/001_basic.t @@ -1,7 +1,7 @@ use strict; use warnings; -use Test::More tests => 11; +use Test::More tests => 8; use Test::Differences; use_ok('SQL::Abstract') or BAIL_OUT( "$@" ); @@ -9,59 +9,43 @@ use_ok('SQL::Abstract') or BAIL_OUT( "$@" ); my $sqla = SQL::Abstract->create(1); # TODO: once MXMS supports %args, use that here -is $sqla->dispatch( [ -name => qw/me id/]), "me.id", +is $sqla->dispatch( { -type => 'name', args => [qw/me id/] }), "me.id", "Simple name generator"; -is $sqla->dispatch( [ -name => qw/me */]), +is $sqla->dispatch( { -type => 'name', args => [qw/me */]}), "me.*", "Simple name generator"; $sqla->quote_chars(['`']); -is $sqla->dispatch( [ -name => qw/me */]), +is $sqla->dispatch( { -type => 'name', args => [qw/me */]}), "`me`.*", "Simple name generator"; $sqla->disable_quoting; is $sqla->dispatch( - [ '-false' ] + { -type => 'false' } ), "0 = 1", "false value"; is $sqla->dispatch( - [ '-true' ] + { -type => 'true' } ), "1 = 1", "true value"; is $sqla->dispatch( - [ -list => - [ -name => qw/me id/], - [ -name => qw/me foo bar/], - [ -name => qw/bar/] - ] + { -type => 'list', + args => [ + { -type => name => args => [qw/me id/] }, + { -type => name => args => [qw/me foo bar/] }, + { -type => name => args => [qw/bar/] } + ] + } ), "me.id, me.foo.bar, bar", "List generator"; is $sqla->dispatch( - [ -alias => [ -name => qw/me id/], "foobar", ] + { -type => 'alias', ident => { -type => name => args => [qw/me id/]}, as => "foobar" } ), "me.id AS foobar", "Alias generator"; -is $sqla->dispatch( - [ -order_by => [ -name => qw/me date/ ] ] -), "ORDER BY me.date", - "order by"; - -is $sqla->dispatch( - [ -order_by => - [ -name => qw/me date/ ], - [ -name => qw/me foobar/ ], - ] -), "ORDER BY me.date, me.foobar", - "order by"; - -is $sqla->dispatch( - [ -order_by => [ -desc => [ -name => qw/me date/ ] ] ] -), "ORDER BY me.date DESC", - "order by desc"; - diff --git a/t/300_order_by.t b/t/300_order_by.t new file mode 100644 index 0000000..ee12886 --- /dev/null +++ b/t/300_order_by.t @@ -0,0 +1,35 @@ +use strict; +use warnings; + +use Test::More tests => 4; +use Test::Differences; + +use_ok('SQL::Abstract') or BAIL_OUT( "$@" ); + +my $sqla = SQL::Abstract->create(1); + + +is $sqla->dispatch( + { -type => 'order_by', order_by => [ { -type => name => args => [qw/me date/ ] } ] } +), "ORDER BY me.date", + "order by"; + +is $sqla->dispatch( + { -type => 'order_by', + order_by => [ + { -type => name => args => [qw/me date/] }, + { -type => name => args => [qw/me foobar/] }, + ] + } +), "ORDER BY me.date, me.foobar", + "order by"; + +# Hrmmm how to mark this up. +is $sqla->dispatch( + { -type => 'order_by', + order_by => [ + [ -desc => { -type => name => args => [qw/me date/ ] } ] + ] + } +), "ORDER BY me.date DESC", + "order by desc";