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/;
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(AST $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 if (exists $ast->{where}) {
58 my $sub_ast = $ast->{where};
60 confess "$_ option is not an AST: " . dump($sub_ast)
61 unless is_AST($sub_ast);
63 push @output, "WHERE", $self->_expr($sub_ast);
66 for (qw/group_by having order_by/) {
67 if (exists $ast->{$_}) {
68 my $sub_ast = $ast->{$_};
70 confess "$_ option is not an AST or an ArrayRef: " . dump($sub_ast)
71 unless is_AST($sub_ast) || is_ArrayRef($sub_ast);;
74 push @output, $self->$meth($sub_ast);
78 return join(' ', @output);
81 method _join(HashRef $ast) {
83 # TODO: Validate join type
84 my $type = $ast->{join_type} || "";
86 my @output = $self->dispatch($ast->{lhs});
88 push @output, uc $type if $type;
89 push @output, "JOIN", $self->dispatch($ast->{rhs});
93 ? ('ON', '(' . $self->_expr( $ast->{on} ) . ')' )
94 : ('USING', '(' .$self->dispatch($ast->{using}
95 || croak "No 'on' or 'uinsg' clause passed to join cluase: " .
100 return join(" ", @output);
104 method _ordering(AST $ast) {
106 my $output = $self->_expr($ast->{expr});
108 $output .= " " . uc $1
109 if $ast->{direction} &&
110 ( $ast->{direction} =~ /^(asc|desc)$/i
111 || confess "Unknown ordering direction " . dump($ast)
117 method _name(AST $ast) {
118 my @names = @{$ast->{args}};
120 my $sep = $self->name_separator;
121 my $quote = $self->is_quoting
125 my $join = $quote->[-1] . $sep . $quote->[0];
127 # We dont want to quote * in [qw/me */]: `me`.* is the desired output there
128 # This means you can't have a field called `*`. I am willing to accept this
129 # situation, cos thats a really stupid thing to want.
131 $post = pop @names if $names[-1] eq '*';
135 join( $join, @names ) .
140 ? $ret . $sep . $post
149 method _list(AST $ast) {
150 return "" unless $ast->{args};
152 my @items = is_ArrayRef($ast->{args})
157 $self->list_separator,
158 map { $self->dispatch($_) } @items);
161 # TODO: I think i want to parameterized AST type to get better validation
162 method _alias(AST $ast) {
164 # TODO: Maybe we want qq{ AS "$as"} here
165 return $self->dispatch($ast->{ident}) . " AS " . $ast->{as};
169 method _value(AST $ast) {
171 $self->add_bind($ast->{value});
175 # Not dispatchable to.
176 method __having($args) {
177 return "HAVING " . $self->_list({-type => 'list', args => $args});
180 method __group_by($args) {
181 return "GROUP BY " . $self->_list({-type => 'list', args => $args});
184 method __order_by($args) {
185 return "ORDER BY " . $self->_list({-type => 'list', args => $args});
189 # Perhaps badly named. handles 'and' and 'or' clauses
190 method _recurse_where(AST $ast) {
195 my $prio = $SQL::Abstract::PRIO{$op};
197 my $dispatch_table = $self->expr_dispatch_table;
200 foreach ( @{$ast->{args}} ) {
201 croak "invalid component in where clause: $_" unless is_AST($_);
203 if ($_->{-type} eq 'expr' && $_->{op} =~ /^(and|or)$/) {
204 my $sub_prio = $SQL::Abstract::PRIO{$1};
206 if ($sub_prio <= $prio) {
207 push @output, $self->_recurse_where($_);
209 push @output, '(' . $self->_recurse_where($_) . ')';
212 push @output, $self->_expr($_);
216 return join(" $OP ", @output);
219 method _expr(AST $ast) {
220 my $op = $ast->{-type};
222 $op = $ast->{op} if $op eq 'expr';
224 if (my $code = $self->lookup_expr_dispatch($op)) {
226 return $code->($self, $ast);
229 croak "'$op' is not a valid AST type in an expression with " . dump($ast)
230 if $ast->{-type} ne 'expr';
232 # This is an attempt to do some form of validation on function names. This
233 # might end up being a bad thing.
234 croak "'$op' is not a valid operator in an expression with " . dump($ast)
237 return $self->_generic_function_op($ast);
241 method _binop(AST $ast) {
242 my ($lhs, $rhs) = @{$ast->{args}};
245 join (' ', $self->_expr($lhs),
246 $self->binop_mapping($op) || croak("Unknown binary operator $op"),
251 method _generic_function_op(AST $ast) {
254 return "$op(" . $self->_list($ast) . ")";
257 method _in(AST $ast) {
259 my ($field,@values) = @{$ast->{args}};
261 my $not = ($ast->{op} =~ /^not_/) ? " NOT" : "";
263 return $self->_false unless @values;
265 return $self->_expr($field) .
268 join(", ", map { $self->dispatch($_) } @values ) .
272 # 'constants' that are portable across DBs
273 method _false($ast?) { "0 = 1" }
274 method _true($ast?) { "1 = 1" }