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/;
12 use Devel::PartialDump qw/dump/;
16 # set things that are valid in where clauses
17 override _build_expr_dispatch_table {
20 in => $self->can('_in'),
21 not_in => $self->can('_in'),
22 and => $self->can('_recurse_where'),
23 or => $self->can('_recurse_where'),
24 map { +"$_" => $self->can("_$_") } qw/
34 method _select(HashAST $ast) {
35 # Default to requiring columns and from
36 # Once TCs give better errors, make this a SelectAST type
37 for (qw/columns from/) {
38 confess "$_ key is required (and must be an AST) to select"
39 unless is_ArrayAST($ast->{$_});
42 # Check that columns is a -list
43 confess "columns key should be a -list AST, not " . $ast->{columns}[0]
44 unless $ast->{columns}[0] eq '-list';
48 $self->dispatch($ast->{columns}),
50 $self->dispatch($ast->{from})
54 if (exists $ast->{$_}) {
55 my $sub_ast = $ast->{$_};
56 $sub_ast->{-type} = "$_" if is_HashRef($sub_ast);
57 confess "$_ option is not an AST"
58 unless is_AST($sub_ast);
60 push @output, $self->dispatch($sub_ast);
64 return join(' ', @output);
67 method _where(ArrayAST $ast) {
68 my (undef, @clauses) = @$ast;
70 return 'WHERE ' . $self->_recurse_where(\@clauses);
73 method _order_by(AST $ast) {
74 my @clauses = @{$ast->{order_by}};
79 if (is_ArrayRef($_) && $_->[0] =~ /^-(asc|desc)$/) {
81 push @output, $self->dispatch($_->[1]) . " " . uc($o);
84 push @output, $self->dispatch($_);
87 return "ORDER BY " . join(", ", @output);
90 method _name(AST $ast) {
91 my @names = @{$ast->{args}};
93 my $sep = $self->name_separator;
94 my $quote = $self->is_quoting
98 my $join = $quote->[-1] . $sep . $quote->[0];
100 # We dont want to quote * in [qw/me */]: `me`.* is the desired output there
101 # This means you can't have a field called `*`. I am willing to accept this
102 # situation, cos thats a really stupid thing to want.
104 $post = pop @names if $names[-1] eq '*';
108 join( $join, @names ) .
111 $ret .= $sep . $post if defined $post;
115 method _join(HashRef $ast) {
117 my $output = 'JOIN ' . $self->dispatch($ast->{tablespec});
119 $output .= exists $ast->{on}
120 ? ' ON (' . $self->_recurse_where( $ast->{on} )
121 : ' USING (' .$self->dispatch($ast->{using} || croak "No 'on' or 'join' clause passed to -join");
128 method _list(AST $ast) {
129 my @items = @{$ast->{args}};
132 $self->list_separator,
133 map { $self->dispatch($_) } @items);
136 method _alias(AST $ast) {
138 # TODO: Maybe we want qq{ AS "$as"} here
139 return $self->dispatch($ast->{ident}) . " AS " . $ast->{as};
143 method _value(HashAST $ast) {
145 $self->add_bind($ast->{value});
149 # Perhaps badly named. handles 'and' and 'or' clauses
150 method _recurse_where(HashAST $ast) {
155 my $prio = $SQL::Abstract::PRIO{$op};
157 my $dispatch_table = $self->expr_dispatch_table;
160 foreach ( @{$ast->{args}} ) {
161 croak "invalid component in where clause: $_" unless is_HashAST($_);
163 if ($_->{-type} eq 'expr' && $_->{op} =~ /^(and|or)$/) {
164 my $sub_prio = $SQL::Abstract::PRIO{$1};
166 if ($sub_prio <= $prio) {
167 push @output, $self->_recurse_where($_);
169 push @output, '(' . $self->_recurse_where($_) . ')';
172 push @output, $self->_expr($_);
176 return join(" $OP ", @output);
179 method _expr(HashAST $ast) {
180 my $op = $ast->{-type};
182 $op = $ast->{op} if $op eq 'expr';
184 if (my $code = $self->lookup_expr_dispatch($op)) {
186 return $code->($self, $ast);
189 croak "'$op' is not a valid AST type in an expression with " . dump($ast)
190 if $ast->{-type} ne 'expr';
192 croak "'$op' is not a valid operator in an expression with " . dump($ast);
196 method _binop(HashAST $ast) {
197 my ($lhs, $rhs) = @{$ast->{args}};
200 join (' ', $self->_expr($lhs),
201 $self->binop_mapping($op) || croak("Unknown binary operator $op"),
206 method _in(HashAST $ast) {
208 my ($field,@values) = @{$ast->{args}};
210 my $not = ($ast->{op} =~ /^not_/) ? " NOT" : "";
212 return $self->_false unless @values;
214 return $self->_expr($field) .
217 join(", ", map { $self->dispatch($_) } @values ) .
221 method _generic_func(ArrayRef $ast) {
224 # 'constants' that are portable across DBs
225 method _false($ast?) { "0 = 1" }
226 method _true($ast?) { "1 = 1" }