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 override _build_where_dispatch_table {
18 -in => $self->can('_in'),
19 -not_in => $self->can('_in')
23 method _select(ArrayRef $ast) {
27 method _where(ArrayRef $ast) {
28 my (undef, @clauses) = @$ast;
30 return 'WHERE ' . $self->_recurse_where(\@clauses);
33 method _order_by(ArrayRef $ast) {
34 my (undef, @clauses) = @$ast;
39 if ($_->[0] =~ /^-(asc|desc)$/) {
41 push @output, $self->dispatch($_->[1]) . " " . uc($o);
44 push @output, $self->dispatch($_);
47 return "ORDER BY " . join(", ", @output);
50 method _name(ArrayRef $ast) {
51 my (undef, @names) = @$ast;
53 my $sep = $self->name_separator;
56 join( $sep->[1] . $sep->[0], @names ) .
60 return join($sep->[0], @names);
63 method _join(ArrayRef $ast) {
67 method _list(ArrayRef $ast) {
68 my (undef, @items) = @$ast;
71 $self->list_separator,
72 map { $self->dispatch($_) } @items);
75 method _alias(ArrayRef $ast) {
76 my (undef, $alias, $as) = @$ast;
78 return $self->dispatch($alias) . " AS $as";
82 method _value(ArrayRef $ast) {
83 my ($undef, $value) = @$ast;
85 $self->add_bind($value);
89 method _recurse_where($clauses) {
92 my $prio = $SQL::Abstract::PRIO{and};
93 my $first = $clauses->[0];
95 if (!ref $first && $first =~ /^-(and|or)$/) {
97 $prio = $SQL::Abstract::PRIO{$1};
101 my $dispatch_table = $self->where_dispatch_table;
104 foreach (@$clauses) {
105 croak "invalid component in where clause" unless ArrayRef->check($_);
108 if (my $code = $dispatch_table->{$op}) {
110 push @output, $code->($self, $_);
112 } elsif ($op =~ /^-(and|or)$/) {
113 my $sub_prio = $SQL::Abstract::PRIO{$1};
115 if ($sub_prio <= $prio) {
116 push @output, $self->_recurse_where($_);
118 push @output, '(' . $self->_recurse_where($_) . ')';
121 croak "Unknown where clause '$op'";
125 return join(" $OP ", @output);
128 method _binop($ast) {
129 my ($op, $lhs, $rhs) = @$ast;
131 join (' ', $self->dispatch($lhs),
132 $self->binop_mapping($op) || croak("Unknown binary operator $op"),
133 $self->dispatch($rhs)
138 my ($tag, $field, @values) = @$ast;
140 my $not = $tag =~ /^-not/ ? " NOT" : "";
142 return $self->_false if @values == 0;
143 return $self->dispatch($field) .
146 join(", ", map { $self->dispatch($_) } @values ) .
151 my ($tag, $field, @values) = @$ast;
153 my $not = $tag =~ /^-not/ ? " NOT" : "";
155 return $self->_false if @values == 0;
156 return $self->dispatch($field) .
159 join(", ", map { $self->dispatch($_) } @values ) .
163 method _generic_func(ArrayRef $ast) {
166 # 'constants' that are portable across DBs
167 method _false($ast?) { "0 = 1" }
168 method _true($ast?) { "1 = 1" }