$self = $self->new unless blessed($self);
local $_ = $ast->[0];
- s/^-/_/ or croak "Unknown type tag '$_'";
+ s/^-/_/g or croak "Unknown type tag '$_'";
my $meth = $self->can($_) || \&_generic_func;
return $meth->($self, $ast);
}
}
+ method _where(ArrayRef $ast) {
+ my (undef, @clauses) = @$ast;
+
+ return 'WHERE ' . $self->_recurse_where(\@clauses);
+ }
+
+ method _order_by(ArrayRef $ast) {
+ my (undef, @clauses) = @$ast;
+
+ my @output;
+
+ for (@clauses) {
+ if ($_->[0] =~ /^-(asc|desc)$/) {
+ my $o = $1;
+ push @output, $self->generate($_->[1]) . " " . uc($o);
+ next;
+ }
+ push @output, $self->generate($_);
+ }
+
+ return "ORDER BY " . join(", ", @output);
+ }
+
method _name(ArrayRef $ast) {
my (undef, @names) = @$ast;
return "?";
}
- method _where(ArrayRef $ast) {
- my (undef, @clauses) = @$ast;
-
- return 'WHERE ' . $self->_recurse_where(\@clauses);
- }
-
method _recurse_where($clauses) {
my $OP = 'AND';
use strict;
use warnings;
-use Test::More tests => 11;
+use Test::More tests => 14;
use_ok('SQL::Abstract') or BAIL_OUT( "$@" );
"Alias generator";
is SQL::Abstract->generate(
+ [ -order_by => [ -name => qw/me date/ ] ]
+), "ORDER BY me.date";
+
+is SQL::Abstract->generate(
+ [ -order_by =>
+ [ -name => qw/me date/ ],
+ [ -name => qw/me foobar/ ],
+ ]
+), "ORDER BY me.date, me.foobar";
+
+is SQL::Abstract->generate(
+ [ -order_by => [ -desc => [ -name => qw/me date/ ] ] ]
+), "ORDER BY me.date DESC";
+
+
+is SQL::Abstract->generate(
[ -where =>
[ '>', [-name => qw/me id/], [-value => 500 ] ]
]