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 # DB specific ones (i.e. mysql/Pg) can not require the FROM part with a bit
39 for (qw/columns tablespec/) {
40 confess "'$_' is required in select AST with " . dump ($ast)
41 unless exists $ast->{$_};
44 # Check that columns is a -list
45 confess "'columns' should be an array ref, not " . dump($ast->{columns})
46 unless is_ArrayRef($ast->{columns});
48 my $cols = $self->_list({-type => 'list', args => $ast->{columns} });
54 push @output, FROM => $self->dispatch($ast->{tablespec})
55 if exists $ast->{tablespec};
57 for (qw/where having group_by/) {
58 if (exists $ast->{$_}) {
59 my $sub_ast = $ast->{$_};
61 confess "$_ option is not an AST: " . dump($sub_ast)
62 unless is_AST($sub_ast);
65 push @output, $self->$meth($sub_ast);
69 return join(' ', @output);
72 method _join(HashRef $ast) {
74 # TODO: Validate join type
75 my $type = $ast->{join_type} || "";
77 my @output = $self->dispatch($ast->{lhs});
79 push @output, uc $type if $type;
80 push @output, "JOIN", $self->dispatch($ast->{rhs});
84 ? ('ON', '(' . $self->_expr( $ast->{on} ) . ')' )
85 : ('USING', '(' .$self->dispatch($ast->{using} || croak "No 'on' or 'join' clause passed to -join").
88 return join(" ", @output);
92 method _order_by(AST $ast) {
93 my @clauses = @{$ast->{order_by}};
98 if (is_ArrayRef($_) && $_->[0] =~ /^-(asc|desc)$/) {
100 push @output, $self->dispatch($_->[1]) . " " . uc($o);
103 push @output, $self->dispatch($_);
106 return "ORDER BY " . join(", ", @output);
109 method _name(HashAST $ast) {
110 my @names = @{$ast->{args}};
112 my $sep = $self->name_separator;
113 my $quote = $self->is_quoting
117 my $join = $quote->[-1] . $sep . $quote->[0];
119 # We dont want to quote * in [qw/me */]: `me`.* is the desired output there
120 # This means you can't have a field called `*`. I am willing to accept this
121 # situation, cos thats a really stupid thing to want.
123 $post = pop @names if $names[-1] eq '*';
127 join( $join, @names ) .
130 $ret .= $sep . $post if defined $post;
135 method _list(AST $ast) {
136 my @items = @{$ast->{args}};
139 $self->list_separator,
140 map { $self->dispatch($_) } @items);
143 # TODO: I think i want to parameterized AST type to get better validation
144 method _alias(AST $ast) {
146 # TODO: Maybe we want qq{ AS "$as"} here
147 return $self->dispatch($ast->{ident}) . " AS " . $ast->{as};
151 method _value(HashAST $ast) {
153 $self->add_bind($ast->{value});
157 # Not dispatchable to.
158 method __where(HashAST $ast) {
159 return "WHERE " . $self->_expr($ast);
162 # Perhaps badly named. handles 'and' and 'or' clauses
163 method _recurse_where(HashAST $ast) {
168 my $prio = $SQL::Abstract::PRIO{$op};
170 my $dispatch_table = $self->expr_dispatch_table;
173 foreach ( @{$ast->{args}} ) {
174 croak "invalid component in where clause: $_" unless is_HashAST($_);
176 if ($_->{-type} eq 'expr' && $_->{op} =~ /^(and|or)$/) {
177 my $sub_prio = $SQL::Abstract::PRIO{$1};
179 if ($sub_prio <= $prio) {
180 push @output, $self->_recurse_where($_);
182 push @output, '(' . $self->_recurse_where($_) . ')';
185 push @output, $self->_expr($_);
189 return join(" $OP ", @output);
192 method _expr(HashAST $ast) {
193 my $op = $ast->{-type};
195 $op = $ast->{op} if $op eq 'expr';
197 if (my $code = $self->lookup_expr_dispatch($op)) {
199 return $code->($self, $ast);
202 croak "'$op' is not a valid AST type in an expression with " . dump($ast)
203 if $ast->{-type} ne 'expr';
205 croak "'$op' is not a valid operator in an expression with " . dump($ast);
209 method _binop(HashAST $ast) {
210 my ($lhs, $rhs) = @{$ast->{args}};
213 join (' ', $self->_expr($lhs),
214 $self->binop_mapping($op) || croak("Unknown binary operator $op"),
219 method _in(HashAST $ast) {
221 my ($field,@values) = @{$ast->{args}};
223 my $not = ($ast->{op} =~ /^not_/) ? " NOT" : "";
225 return $self->_false unless @values;
227 return $self->_expr($field) .
230 join(", ", map { $self->dispatch($_) } @values ) .
234 method _generic_func(ArrayRef $ast) {
237 # 'constants' that are portable across DBs
238 method _false($ast?) { "0 = 1" }
239 method _true($ast?) { "1 = 1" }