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 between => $self->can('_between'),
23 not_between => $self->can('_between'),
24 and => $self->can('_recurse_where'),
25 or => $self->can('_recurse_where'),
26 map { +"$_" => $self->can("_$_") } qw/
36 method _select(AST $ast) {
37 # Default to requiring columns and from.
38 # DB specific ones (i.e. mysql/Pg) can not require the FROM part with a bit
41 for (qw/columns tablespec/) {
42 confess "'$_' is required in select AST with " . dump ($ast)
43 unless exists $ast->{$_};
46 # Check that columns is a -list
47 confess "'columns' should be an array ref, not " . dump($ast->{columns})
48 unless is_ArrayRef($ast->{columns});
50 my $cols = $self->_list({-type => 'list', args => $ast->{columns} });
56 push @output, FROM => $self->dispatch($ast->{tablespec})
57 if exists $ast->{tablespec};
59 if (exists $ast->{where}) {
60 my $sub_ast = $ast->{where};
62 confess "$_ option is not an AST: " . dump($sub_ast)
63 unless is_AST($sub_ast);
65 push @output, "WHERE", $self->_expr($sub_ast);
68 for (qw/group_by having order_by/) {
69 if (exists $ast->{$_}) {
70 my $sub_ast = $ast->{$_};
72 confess "$_ option is not an AST or an ArrayRef: " . dump($sub_ast)
73 unless is_AST($sub_ast) || is_ArrayRef($sub_ast);;
76 push @output, $self->$meth($sub_ast);
80 return join(' ', @output);
83 method _update(AST $ast) {
85 for (qw/columns values tablespec/) {
86 confess "'$_' is required in update AST with " . dump ($ast)
87 unless exists $ast->{$_};
90 my $table = $ast->{tablespec};
91 confess 'update: tablespec must be an ident or an alias in ' . dump($ast)
92 unless $table->{-type} =~ /^identifier|alias$/;
96 $self->dispatch($table),
100 confess 'update: number of columns doesn\'t match values: ' . dump($ast)
101 if @{$ast->{columns}} != @{$ast->{values}};
108 op => '==', # This should really be '=' but hmmmmmmmm
114 } 0..$#{$ast->{columns}} ]
117 push @output, $self->dispatch($list);
119 return join(' ', @output);
123 method _join(HashRef $ast) {
125 # TODO: Validate join type
126 my $type = $ast->{join_type} || "";
128 my @output = $self->dispatch($ast->{lhs});
130 push @output, uc $type if $type;
131 push @output, "JOIN", $self->dispatch($ast->{rhs});
135 ? ('ON', '(' . $self->_expr( $ast->{on} ) . ')' )
136 : ('USING', '(' .$self->dispatch($ast->{using}
137 || croak "No 'on' or 'uinsg' clause passed to join cluase: " .
142 return join(" ", @output);
146 method _ordering(AST $ast) {
148 my $output = $self->_expr($ast->{expr});
150 $output .= " " . uc $1
151 if $ast->{direction} &&
152 ( $ast->{direction} =~ /^(asc|desc)$/i
153 || confess "Unknown ordering direction " . dump($ast)
159 method _identifier(AST $ast) {
160 my @names = @{$ast->{elements}};
162 my $sep = $self->ident_separator;
163 my $quote = $self->is_quoting
167 my $join = $quote->[-1] . $sep . $quote->[0];
169 # We dont want to quote * in [qw/me */]: `me`.* is the desired output there
170 # This means you can't have a field called `*`. I am willing to accept this
171 # situation, cos thats a really stupid thing to want.
173 $post = pop @names if $names[-1] eq '*';
177 join( $join, @names ) .
182 ? $ret . $sep . $post
191 method _list(AST $ast) {
192 return "" unless $ast->{args};
194 my @items = is_ArrayRef($ast->{args})
199 $self->list_separator,
200 map { $self->dispatch($_) } @items);
203 # TODO: I think i want to parameterized AST type to get better validation
204 method _alias(AST $ast) {
206 # TODO: Maybe we want qq{ AS "$as"} here
207 return $self->dispatch($ast->{ident}) . " AS " . $ast->{as};
211 method _value(AST $ast) {
213 $self->add_bind($ast->{value});
217 # Not dispatchable to.
218 method __having($args) {
219 return "HAVING " . $self->_list({-type => 'list', args => $args});
222 method __group_by($args) {
223 return "GROUP BY " . $self->_list({-type => 'list', args => $args});
226 method __order_by($args) {
227 return "ORDER BY " . $self->_list({-type => 'list', args => $args});
231 # Perhaps badly named. handles 'and' and 'or' clauses
232 method _recurse_where(AST $ast) {
237 my $prio = $SQL::Abstract::PRIO{$op};
239 my $dispatch_table = $self->expr_dispatch_table;
242 foreach ( @{$ast->{args}} ) {
243 croak "invalid component in where clause: $_" unless is_AST($_);
245 if ($_->{-type} eq 'expr' && $_->{op} =~ /^(and|or)$/) {
246 my $sub_prio = $SQL::Abstract::PRIO{$1};
248 if ($sub_prio == $prio) {
249 # When the element below has same priority, i.e. 'or' as a child of
250 # 'or', dont produce extra brackets
251 push @output, $self->_recurse_where($_);
253 push @output, '(' . $self->_recurse_where($_) . ')';
256 push @output, $self->_expr($_);
260 return join(" $OP ", @output);
263 method _expr(AST $ast) {
264 my $op = $ast->{-type};
266 $op = $ast->{op} if $op eq 'expr';
268 if (my $code = $self->lookup_expr_dispatch($op)) {
270 return $code->($self, $ast);
273 croak "'$op' is not a valid AST type in an expression with " . dump($ast)
274 if $ast->{-type} ne 'expr';
276 # This is an attempt to do some form of validation on function names. This
277 # might end up being a bad thing.
278 croak "'$op' is not a valid operator in an expression with " . dump($ast)
281 return $self->_generic_function_op($ast);
285 method _binop(AST $ast) {
286 my ($lhs, $rhs) = @{$ast->{args}};
290 if ($rhs->{-type} eq 'value' && !defined $rhs->{value} &&
291 ($op eq '==' || $op eq '!='))
293 return $self->_expr($lhs) .
294 ($op eq '==' ? " IS " : " IS NOT ") .
298 join (' ', $self->_expr($lhs),
299 $self->binop_mapping($op) || croak("Unknown binary operator $op"),
304 method _generic_function_op(AST $ast) {
307 return "$op(" . $self->_list($ast) . ")";
310 method _in(AST $ast) {
312 my ($field,@values) = @{$ast->{args}};
314 my $not = ($ast->{op} =~ /^not_/) ? " NOT" : "";
316 return $self->_false unless @values;
318 return $self->_expr($field) .
321 join(", ", map { $self->dispatch($_) } @values ) .
325 method _between(AST $ast) {
327 my ($field,@values) = @{$ast->{args}};
329 my $not = ($ast->{op} =~ /^not_/) ? " NOT" : "";
330 croak "between requires 3 arguments: " . dump($ast)
333 # The brackets are to work round an issue with SQL::A::Test
335 $self->_expr($field) .
338 join(" AND ", map { $self->dispatch($_) } @values ) .
342 # 'constants' that are portable across DBs
343 method _false($ast?) { "0 = 1" }
344 method _true($ast?) { "1 = 1" }