3 class SQL::Abstract::AST::v1 extends SQL::Abstract {
8 use Moose::Util::TypeConstraints;
9 use MooseX::Types -declare => [qw/NameSeparator/];
10 use MooseX::Types::Moose qw/ArrayRef Str Int/;
11 use MooseX::AttributeHelpers;
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 map { +"-$_" => $self->can("_$_") } qw/
30 method _select(ArrayRef $ast) {
34 method _where(ArrayRef $ast) {
35 my (undef, @clauses) = @$ast;
37 return 'WHERE ' . $self->_recurse_where(\@clauses);
40 method _order_by(ArrayRef $ast) {
41 my (undef, @clauses) = @$ast;
46 if ($_->[0] =~ /^-(asc|desc)$/) {
48 push @output, $self->dispatch($_->[1]) . " " . uc($o);
51 push @output, $self->dispatch($_);
54 return "ORDER BY " . join(", ", @output);
57 method _name(ArrayRef $ast) {
58 my (undef, @names) = @$ast;
60 my $sep = $self->name_separator;
63 join( $sep->[1] . $sep->[0], @names ) .
67 return join($sep->[0], @names);
70 method _join(ArrayRef $ast) {
71 my (undef, @items) = @$ast;
73 croak "invalid component in JOIN: $_" unless ArrayRef->check($items[0]);
76 # TODO: Validation of inputs
77 return 'JOIN '. $self->dispatch(shift @items) .
79 $self->_recurse_where( \@items ) . ')';
83 method _list(ArrayRef $ast) {
84 my (undef, @items) = @$ast;
87 $self->list_separator,
88 map { $self->dispatch($_) } @items);
91 method _alias(ArrayRef $ast) {
92 my (undef, $alias, $as) = @$ast;
94 return $self->dispatch($alias) . " AS $as";
98 method _value(ArrayRef $ast) {
99 my ($undef, $value) = @$ast;
101 $self->add_bind($value);
105 method _recurse_where($clauses) {
108 my $prio = $SQL::Abstract::PRIO{and};
109 my $first = $clauses->[0];
111 if (!ref $first && $first =~ /^-(and|or)$/) {
113 $prio = $SQL::Abstract::PRIO{$1};
117 my $dispatch_table = $self->where_dispatch_table;
120 foreach (@$clauses) {
121 croak "invalid component in where clause: $_" unless ArrayRef->check($_);
124 if ($op =~ /^-(and|or)$/) {
125 my $sub_prio = $SQL::Abstract::PRIO{$1};
127 if ($sub_prio <= $prio) {
128 push @output, $self->_recurse_where($_);
130 push @output, '(' . $self->_recurse_where($_) . ')';
133 push @output, $self->_where_component($_);
137 return join(" $OP ", @output);
140 method _where_component($ast) {
143 if (my $code = $self->lookup_where_dispatch($op)) {
145 return $code->($self, $ast);
148 croak "'$op' is not a valid clause in a where AST"
151 croak "'$op' is not a valid operator";
156 method _binop($ast) {
157 my ($op, $lhs, $rhs) = @$ast;
159 join (' ', $self->_where_component($lhs),
160 $self->binop_mapping($op) || croak("Unknown binary operator $op"),
161 $self->_where_component($rhs)
166 my ($tag, $field, @values) = @$ast;
168 my $not = $tag =~ /^-not/ ? " NOT" : "";
170 return $self->_false if @values == 0;
171 return $self->_where_component($field) .
174 join(", ", map { $self->dispatch($_) } @values ) .
179 my ($tag, $field, @values) = @$ast;
181 my $not = $tag =~ /^-not/ ? " NOT" : "";
183 return $self->_false if @values == 0;
184 return $self->_where_component($field) .
187 join(", ", map { $self->_where_component($_) } @values ) .
191 method _generic_func(ArrayRef $ast) {
194 # 'constants' that are portable across DBs
195 method _false($ast?) { "0 = 1" }
196 method _true($ast?) { "1 = 1" }