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 method dispatch (ArrayRef $ast) {
18 s/^-/_/g or croak "Unknown type tag '$_'";
19 my $meth = $self->can($_) || \&_generic_func;
20 return $meth->($self, $ast);
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};
102 foreach (@$clauses) {
103 croak "invalid component in where clause" unless ArrayRef->check($_);
106 unless (substr($op, 0, 1) eq '-') {
107 # A simple comparison op (==, >, etc.)
109 push @output, $self->_binop(@$_);
111 } elsif ($op =~ /^-(and|or)$/) {
112 my $sub_prio = $SQL::Abstract::PRIO{$1};
114 if ($sub_prio <= $prio) {
115 push @output, $self->_recurse_where($_);
117 push @output, '(' . $self->_recurse_where($_) . ')';
120 push @output, $self->dispatch($_);
124 return join(" $OP ", @output);
127 method _binop($op, $lhs, $rhs) {
128 join (' ', $self->dispatch($lhs),
129 $SQL::Abstract::OP_MAP{$op} || croak("Unknown binary operator $op"),
130 $self->dispatch($rhs)
135 my (undef, $field, @values) = @$ast;
137 return $self->dispatch($field) .
139 join(", ", map { $self->dispatch($_) } @values ) .
143 method _generic_func(ArrayRef $ast) {