push @output, FROM => $self->dispatch($ast->{tablespec})
if exists $ast->{tablespec};
- for (qw//) {
+ for (qw/where having group_by/) {
if (exists $ast->{$_}) {
my $sub_ast = $ast->{$_};
- $sub_ast->{-type} = "$_" if is_HashRef($sub_ast);
- confess "$_ option is not an AST"
+
+ confess "$_ option is not an AST: " . dump($sub_ast)
unless is_AST($sub_ast);
- push @output, $self->dispatch($sub_ast);
+ my $meth = "__$_";
+ push @output, $self->$meth($sub_ast);
}
}
return "?";
}
+ # Not dispatchable to.
+ method __where(HashAST $ast) {
+ return "WHERE " . $self->_expr($ast);
+ }
+
# Perhaps badly named. handles 'and' and 'or' clauses
method _recurse_where(HashAST $ast) {
use strict;
use warnings;
-use Test::More tests => 3;
+use Test::More tests => 4;
use Test::Differences;
use_ok('SQL::Abstract') or BAIL_OUT( "$@" );
my $sqla = SQL::Abstract->create(1);
+my $foo_as_me = {
+ -type => 'alias',
+ ident => {-type => 'name', args => [qw/foo/]},
+ as => 'me'
+};
+my $me_foo_id = { -type => 'name', args => [qw/me foo_id/] };
+
is $sqla->dispatch(
{ -type => 'select',
- tablespec => {-type => 'alias', ident => {-type => 'name', args => [qw/foo/]}, as => 'me' },
+ tablespec => $foo_as_me,
columns => [
{ -type => 'name', args => [qw/me id/] },
- { -type => 'alias', ident => { -type => 'name', args => [qw/me foo_id/] }, as => 'foo' },
+ { -type => 'alias', ident => $me_foo_id, as => 'foo' },
]
}
), "SELECT me.id, me.foo_id AS foo FROM foo AS me",
{ -type => 'select',
columns => [
{ -type => 'name', args => [qw/me id/] },
- { -type => 'alias', ident => { -type => 'name', args => [qw/me foo_id/] }, as => 'foo' },
+ { -type => 'alias', ident => $me_foo_id, as => 'foo' },
{ -type => 'name', args => [qw/bar name/] },
],
tablespec => {
-type => 'join',
- lhs => {-type => 'alias', ident => {-type => 'name', args => [qw/foo/]}, as => 'me' },
+ lhs => $foo_as_me,
rhs => {-type => 'name', args => [qw/bar/] },
on => {
-type => 'expr',
), "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__
+
+is $sqla->dispatch(
+ { -type => 'select',
+ columns => [
+ { -type => 'name', args => [qw/me */] },
+ ],
+ tablespec => $foo_as_me,
+ where => {
+ -type => 'expr',
+ op => '==',
+ args => [
+ {-type => 'name', args => [qw/me id/]},
+ {-type => 'value', value => 1 },
+ ]
+ }
+ }
+
+
+), "SELECT me.* FROM foo AS me WHERE me.id = ?",
+ "select with where";