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/
33 method _select(HashAST $ast) {
34 # Default to requiring columns and from
35 # Once TCs give better errors, make this a SelectAST type
36 for (qw/columns from/) {
37 confess "$_ key is required (and must be an AST) to select"
38 unless is_ArrayAST($ast->{$_});
41 # Check that columns is a -list
42 confess "columns key should be a -list AST, not " . $ast->{columns}[0]
43 unless $ast->{columns}[0] eq '-list';
47 $self->dispatch($ast->{columns}),
49 $self->dispatch($ast->{from})
53 if (exists $ast->{$_}) {
54 my $sub_ast = $ast->{$_};
55 $sub_ast->{-type} = "$_" if is_HashRef($sub_ast);
56 confess "$_ option is not an AST"
57 unless is_AST($sub_ast);
59 push @output, $self->dispatch($sub_ast);
63 return join(' ', @output);
66 method _where(ArrayAST $ast) {
67 my (undef, @clauses) = @$ast;
69 return 'WHERE ' . $self->_recurse_where(\@clauses);
72 method _order_by(AST $ast) {
73 my @clauses = @{$ast->{order_by}};
78 if (is_ArrayRef($_) && $_->[0] =~ /^-(asc|desc)$/) {
80 push @output, $self->dispatch($_->[1]) . " " . uc($o);
83 push @output, $self->dispatch($_);
86 return "ORDER BY " . join(", ", @output);
89 method _name(AST $ast) {
90 my @names = @{$ast->{args}};
92 my $sep = $self->name_separator;
93 my $quote = $self->is_quoting
97 my $join = $quote->[-1] . $sep . $quote->[0];
99 # We dont want to quote * in [qw/me */]: `me`.* is the desired output there
100 # This means you can't have a field called `*`. I am willing to accept this
101 # situation, cos thats a really stupid thing to want.
103 $post = pop @names if $names[-1] eq '*';
107 join( $join, @names ) .
110 $ret .= $sep . $post if defined $post;
114 method _join(HashRef $ast) {
116 my $output = 'JOIN ' . $self->dispatch($ast->{tablespec});
118 $output .= exists $ast->{on}
119 ? ' ON (' . $self->_recurse_where( $ast->{on} )
120 : ' USING (' .$self->dispatch($ast->{using} || croak "No 'on' or 'join' clause passed to -join");
127 method _list(AST $ast) {
128 my @items = @{$ast->{args}};
131 $self->list_separator,
132 map { $self->dispatch($_) } @items);
135 method _alias(AST $ast) {
137 # TODO: Maybe we want qq{ AS "$as"} here
138 return $self->dispatch($ast->{ident}) . " AS " . $ast->{as};
142 method _value(HashAST $ast) {
144 $self->add_bind($ast->{value});
148 method _recurse_where(HashAST $ast) {
153 my $prio = $SQL::Abstract::PRIO{$op};
155 my $dispatch_table = $self->where_dispatch_table;
158 foreach ( @{$ast->{args}} ) {
159 croak "invalid component in where clause: $_" unless is_HashAST($_);
161 if ($_->{-type} eq 'expr' && $_->{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 use Devel::PartialDump qw/dump/;
189 croak "'$op' is not a valid operator in " . dump($ast);
193 method _expr(HashAST $ast) {
195 my $meth = $self->lookup_where_dispatch($op) || confess "Invalid operator '$op'";
197 $meth->($self, $ast);
200 method _binop(HashAST $ast) {
201 my ($lhs, $rhs) = @{$ast->{args}};
204 join (' ', $self->_where_component($lhs),
205 $self->binop_mapping($op) || croak("Unknown binary operator $op"),
206 $self->_where_component($rhs)
210 method _in(HashAST $ast) {
212 my ($field,$values) = @{$ast->{args}};
214 my $not = ($ast->{op} =~ /^-not/) ? " NOT" : "";
216 return $self->_false if !defined $values || @$values == 0;
218 return $self->_where_component($field) .
221 join(", ", map { $self->dispatch($_) } @$values ) .
225 method _generic_func(ArrayRef $ast) {
228 # 'constants' that are portable across DBs
229 method _false($ast?) { "0 = 1" }
230 method _true($ast?) { "1 = 1" }