use Data::Dump qw/pp/;
use Moose::Util::TypeConstraints;
- use MooseX::Types -declare => [qw/NameSeparator/];
- use MooseX::Types::Moose qw/ArrayRef Str Int/;
+ use MooseX::Types::Moose qw/ArrayRef Str Int Ref HashRef/;
use MooseX::AttributeHelpers;
+ use SQL::Abstract::Types qw/AST ArrayAST HashAST/;
+ use Devel::PartialDump qw/dump/;
clean;
- method dispatch (ArrayRef $ast) {
-
- local $_ = $ast->[0];
- s/^-/_/g or croak "Unknown type tag '$_'";
- my $meth = $self->can($_) || \&_generic_func;
- return $meth->($self, $ast);
+ # set things that are valid in where clauses
+ override _build_expr_dispatch_table {
+ return {
+ %{super()},
+ in => $self->can('_in'),
+ not_in => $self->can('_in'),
+ and => $self->can('_recurse_where'),
+ or => $self->can('_recurse_where'),
+ map { +"$_" => $self->can("_$_") } qw/
+ value
+ name
+ true
+ false
+ expr
+ /
+ };
}
- method _select(ArrayRef $ast) {
+ method _select(HashAST $ast) {
+ # Default to requiring columns and from.
+ # DB specific ones (i.e. mysql/Pg) can not require the FROM part with a bit
+ # of refactoring
+ for (qw/columns tablespec/) {
+ confess "'$_' is required in select AST with " . dump ($ast)
+ unless exists $ast->{$_};
+ }
+
+ # Check that columns is a -list
+ confess "'columns' should be an array ref, not " . dump($ast->{columns})
+ unless is_ArrayRef($ast->{columns});
+
+ my $cols = join ($self->list_separator, map { $self->dispatch($_) } @{ $ast->{columns}});
+
+ my @output = (
+ SELECT => $cols
+ );
+
+ push @output, FROM => $self->dispatch($ast->{tablespec})
+ if exists $ast->{tablespec};
+
+ for (qw//) {
+ if (exists $ast->{$_}) {
+ my $sub_ast = $ast->{$_};
+ $sub_ast->{-type} = "$_" if is_HashRef($sub_ast);
+ confess "$_ option is not an AST"
+ unless is_AST($sub_ast);
+
+ push @output, $self->dispatch($sub_ast);
+ }
+ }
+
+ return join(' ', @output);
}
- method _where(ArrayRef $ast) {
+ method _where(ArrayAST $ast) {
my (undef, @clauses) = @$ast;
return 'WHERE ' . $self->_recurse_where(\@clauses);
}
- method _order_by(ArrayRef $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;
return "ORDER BY " . join(", ", @output);
}
- method _name(ArrayRef $ast) {
- my (undef, @names) = @$ast;
+ method _name(HashAST $ast) {
+ my @names = @{$ast->{args}};
my $sep = $self->name_separator;
+ my $quote = $self->is_quoting
+ ? $self->quote_chars
+ : [ '' ];
- return $sep->[0] .
- join( $sep->[1] . $sep->[0], @names ) .
- $sep->[1]
- if (@$sep > 1);
+ my $join = $quote->[-1] . $sep . $quote->[0];
- return join($sep->[0], @names);
+ # We dont want to quote * in [qw/me */]: `me`.* is the desired output there
+ # This means you can't have a field called `*`. I am willing to accept this
+ # situation, cos thats a really stupid thing to want.
+ my $post;
+ $post = pop @names if $names[-1] eq '*';
+
+ my $ret =
+ $quote->[0] .
+ join( $join, @names ) .
+ $quote->[-1];
+
+ $ret .= $sep . $post if defined $post;
+ return $ret;
}
- method _join(ArrayRef $ast) {
-
+ method _join(HashRef $ast) {
+
+ my $output = 'JOIN ' . $self->dispatch($ast->{tablespec});
+
+ $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;
+
}
- method _list(ArrayRef $ast) {
- my (undef, @items) = @$ast;
+ method _list(AST $ast) {
+ my @items = @{$ast->{args}};
return join(
$self->list_separator,
map { $self->dispatch($_) } @items);
}
- method _alias(ArrayRef $ast) {
- my (undef, $alias, $as) = @$ast;
-
- return $self->dispatch($alias) . " AS $as";
+ # TODO: I think i want to parameterized AST type to get better validation
+ method _alias(AST $ast) {
+
+ # TODO: Maybe we want qq{ AS "$as"} here
+ return $self->dispatch($ast->{ident}) . " AS " . $ast->{as};
}
- method _value(ArrayRef $ast) {
- my ($undef, $value) = @$ast;
+ method _value(HashAST $ast) {
- $self->add_bind($value);
+ $self->add_bind($ast->{value});
return "?";
}
- method _recurse_where($clauses) {
+ # Perhaps badly named. handles 'and' and 'or' clauses
+ method _recurse_where(HashAST $ast) {
- my $OP = 'AND';
- my $prio = $SQL::Abstract::PRIO{and};
- my $first = $clauses->[0];
+ my $op = $ast->{op};
- if (!ref $first && $first =~ /^-(and|or)$/) {
- $OP = uc($1);
- $prio = $SQL::Abstract::PRIO{$1};
- shift @$clauses;
- }
+ my $OP = uc $op;
+ my $prio = $SQL::Abstract::PRIO{$op};
+
+ my $dispatch_table = $self->expr_dispatch_table;
my @output;
- foreach (@$clauses) {
- croak "invalid component in where clause" unless ArrayRef->check($_);
- my $op = $_->[0];
-
- unless (substr($op, 0, 1) eq '-') {
- # A simple comparison op (==, >, etc.)
-
- push @output, $self->_binop(@$_);
-
- } elsif ($op =~ /^-(and|or)$/) {
+ foreach ( @{$ast->{args}} ) {
+ croak "invalid component in where clause: $_" unless is_HashAST($_);
+
+ if ($_->{-type} eq 'expr' && $_->{op} =~ /^(and|or)$/) {
my $sub_prio = $SQL::Abstract::PRIO{$1};
if ($sub_prio <= $prio) {
push @output, '(' . $self->_recurse_where($_) . ')';
}
} else {
- push @output, $self->dispatch($_);
+ push @output, $self->_expr($_);
}
}
return join(" $OP ", @output);
}
- method _binop($op, $lhs, $rhs) {
- join (' ', $self->dispatch($lhs),
- $SQL::Abstract::OP_MAP{$op} || croak("Unknown binary operator $op"),
- $self->dispatch($rhs)
+ method _expr(HashAST $ast) {
+ my $op = $ast->{-type};
+
+ $op = $ast->{op} if $op eq 'expr';
+
+ if (my $code = $self->lookup_expr_dispatch($op)) {
+
+ return $code->($self, $ast);
+
+ }
+ croak "'$op' is not a valid AST type in an expression with " . dump($ast)
+ if $ast->{-type} ne 'expr';
+
+ croak "'$op' is not a valid operator in an expression with " . dump($ast);
+
+ }
+
+ method _binop(HashAST $ast) {
+ my ($lhs, $rhs) = @{$ast->{args}};
+ my $op = $ast->{op};
+
+ join (' ', $self->_expr($lhs),
+ $self->binop_mapping($op) || croak("Unknown binary operator $op"),
+ $self->_expr($rhs)
);
}
- method _in($ast) {
- my (undef, $field, @values) = @$ast;
+ method _in(HashAST $ast) {
+
+ my ($field,@values) = @{$ast->{args}};
+
+ my $not = ($ast->{op} =~ /^not_/) ? " NOT" : "";
+
+ return $self->_false unless @values;
- return $self->_false if @values == 0;
- return $self->dispatch($field) .
+ return $self->_expr($field) .
+ $not .
" IN (" .
join(", ", map { $self->dispatch($_) } @values ) .
")";