3 class SQL::Abstract::AST::v1 extends SQL::Abstract {
8 use Moose::Util::TypeConstraints;
9 use MooseX::Types::Moose qw/ArrayRef Str Int Ref HashRef/;
10 use MooseX::AttributeHelpers;
11 use SQL::Abstract::Types qw/AST ArrayAST HashAST/;
15 # set things that are valid in where clauses
16 override _build_where_dispatch_table {
19 in => $self->can('_in'),
20 not_in => $self->can('_in'),
21 and => $self->can('_recurse_where'),
22 or => $self->can('_recurse_where'),
23 map { +"$_" => $self->can("_$_") } qw/
32 method _select(HashAST $ast) {
33 # Default to requiring columns and from
34 # Once TCs give better errors, make this a SelectAST type
35 for (qw/columns from/) {
36 confess "$_ key is required (and must be an AST) to select"
37 unless is_ArrayAST($ast->{$_});
40 # Check that columns is a -list
41 confess "columns key should be a -list AST, not " . $ast->{columns}[0]
42 unless $ast->{columns}[0] eq '-list';
46 $self->dispatch($ast->{columns}),
48 $self->dispatch($ast->{from})
52 if (exists $ast->{$_}) {
53 my $sub_ast = $ast->{$_};
54 $sub_ast->{-type} = "$_" if is_HashRef($sub_ast);
55 confess "$_ option is not an AST"
56 unless is_AST($sub_ast);
58 push @output, $self->dispatch($sub_ast);
62 return join(' ', @output);
65 method _where(ArrayAST $ast) {
66 my (undef, @clauses) = @$ast;
68 return 'WHERE ' . $self->_recurse_where(\@clauses);
71 method _order_by(AST $ast) {
72 my @clauses = @{$ast->{order_by}};
77 if (is_ArrayRef($_) && $_->[0] =~ /^-(asc|desc)$/) {
79 push @output, $self->dispatch($_->[1]) . " " . uc($o);
82 push @output, $self->dispatch($_);
85 return "ORDER BY " . join(", ", @output);
88 method _name(AST $ast) {
89 my @names = @{$ast->{args}};
91 my $sep = $self->name_separator;
92 my $quote = $self->is_quoting
96 my $join = $quote->[-1] . $sep . $quote->[0];
98 # We dont want to quote * in [qw/me */]: `me`.* is the desired output there
99 # This means you can't have a field called `*`. I am willing to accept this
100 # situation, cos thats a really stupid thing to want.
102 $post = pop @names if $names[-1] eq '*';
106 join( $join, @names ) .
109 $ret .= $sep . $post if defined $post;
113 method _join(HashRef $ast) {
115 my $output = 'JOIN ' . $self->dispatch($ast->{tablespec});
117 $output .= exists $ast->{on}
118 ? ' ON (' . $self->_recurse_where( $ast->{on} )
119 : ' USING (' .$self->dispatch($ast->{using} || croak "No 'on' or 'join' clause passed to -join");
126 method _list(AST $ast) {
127 my @items = @{$ast->{args}};
130 $self->list_separator,
131 map { $self->dispatch($_) } @items);
134 method _alias(AST $ast) {
136 # TODO: Maybe we want qq{ AS "$as"} here
137 return $self->dispatch($ast->{ident}) . " AS " . $ast->{as};
141 method _value(HashAST $ast) {
143 $self->add_bind($ast->{value});
147 method _recurse_where(HashAST $ast) {
152 my $prio = $SQL::Abstract::PRIO{$op};
154 my $dispatch_table = $self->where_dispatch_table;
157 foreach ( @{$ast->{args}} ) {
158 croak "invalid component in where clause: $_" unless is_ArrayRef($_);
161 if ($op =~ /^-(and|or)$/) {
162 my $sub_prio = $SQL::Abstract::PRIO{$1};
164 if ($sub_prio <= $prio) {
165 push @output, $self->_recurse_where($_);
167 push @output, '(' . $self->_recurse_where($_) . ')';
170 push @output, $self->_where_component($_);
174 return join(" $OP ", @output);
177 method _where_component(HashAST $ast) {
178 my $op = $ast->{-type};
180 if (my $code = $self->lookup_where_dispatch($op)) {
182 return $code->($self, $ast);
185 croak "'$op' is not a valid clause in a where AST"
188 croak "'$op' is not a valid operator";
192 method _expr(HashAST $ast) {
194 my $meth = $self->lookup_where_dispatch($op) || confess "Invalid operator '$op'";
196 $meth->($self, $ast);
199 method _binop(HashAST $ast) {
200 my ($lhs, $rhs) = @{$ast->{args}};
203 join (' ', $self->_where_component($lhs),
204 $self->binop_mapping($op) || croak("Unknown binary operator $op"),
205 $self->_where_component($rhs)
209 method _in(HashAST $ast) {
211 my ($field,$values) = @{$ast->{args}};
213 my $not = ($ast->{op} =~ /^-not/) ? " NOT" : "";
215 return $self->_false if !defined $values || @$values == 0;
217 return $self->_where_component($field) .
220 join(", ", map { $self->dispatch($_) } @$values ) .
224 method _generic_func(ArrayRef $ast) {
227 # 'constants' that are portable across DBs
228 method _false($ast?) { "0 = 1" }
229 method _true($ast?) { "1 = 1" }