Port more stuff to use HashAST
[dbsrgits/SQL-Abstract-2.0-ish.git] / lib / SQL / Abstract / AST / v1.pm
CommitLineData
14774be0 1use MooseX::Declare;
2
3class SQL::Abstract::AST::v1 extends SQL::Abstract {
4
5 use Carp qw/croak/;
6 use Data::Dump qw/pp/;
7
8 use Moose::Util::TypeConstraints;
cbcfedc1 9 use MooseX::Types::Moose qw/ArrayRef Str Int Ref HashRef/;
14774be0 10 use MooseX::AttributeHelpers;
cbcfedc1 11 use SQL::Abstract::Types qw/AST ArrayAST HashAST/;
14774be0 12
13 clean;
14
0c371882 15 # set things that are valid in where clauses
0bf8a8c4 16 override _build_where_dispatch_table {
17 return {
18 %{super()},
1b85673a 19 in => $self->can('_in'),
20 not_in => $self->can('_in'),
21 map { +"$_" => $self->can("_$_") } qw/
0c371882 22 value
23 name
24 true
25 false
26 /
0bf8a8c4 27 };
14774be0 28 }
29
cbcfedc1 30 method _select(HashAST $ast) {
4ee32f41 31 # Default to requiring columns and from
32 # Once TCs give better errors, make this a SelectAST type
33 for (qw/columns from/) {
34 confess "$_ key is required (and must be an AST) to select"
35 unless is_ArrayAST($ast->{$_});
36 }
37
38 # Check that columns is a -list
39 confess "columns key should be a -list AST, not " . $ast->{columns}[0]
40 unless $ast->{columns}[0] eq '-list';
41
42 my @output = (
43 "SELECT",
44 $self->dispatch($ast->{columns}),
45 "FROM",
46 $self->dispatch($ast->{from})
47 );
48
49 for (qw/join/) {
50 if (exists $ast->{$_}) {
51 my $sub_ast = $ast->{$_};
52 $sub_ast->{-type} = "$_" if is_HashRef($sub_ast);
53 confess "$_ option is not an AST"
54 unless is_AST($sub_ast);
55
56 push @output, $self->dispatch($sub_ast);
57 }
58 }
59
60 return join(' ', @output);
14774be0 61 }
62
cbcfedc1 63 method _where(ArrayAST $ast) {
14774be0 64 my (undef, @clauses) = @$ast;
65
66 return 'WHERE ' . $self->_recurse_where(\@clauses);
67 }
68
7a56723e 69 method _order_by(AST $ast) {
70 my @clauses = @{$ast->{order_by}};
71
14774be0 72 my @output;
73
74 for (@clauses) {
7a56723e 75 if (is_ArrayRef($_) && $_->[0] =~ /^-(asc|desc)$/) {
14774be0 76 my $o = $1;
77 push @output, $self->dispatch($_->[1]) . " " . uc($o);
78 next;
79 }
80 push @output, $self->dispatch($_);
81 }
82
83 return "ORDER BY " . join(", ", @output);
84 }
85
7a56723e 86 method _name(AST $ast) {
87 my @names = @{$ast->{args}};
14774be0 88
89 my $sep = $self->name_separator;
4ee32f41 90 my $quote = $self->is_quoting
91 ? $self->quote_chars
92 : [ '' ];
93
94 my $join = $quote->[-1] . $sep . $quote->[0];
14774be0 95
4ee32f41 96 # We dont want to quote * in [qw/me */]: `me`.* is the desired output there
97 # This means you can't have a field called `*`. I am willing to accept this
98 # situation, cos thats a really stupid thing to want.
99 my $post;
100 $post = pop @names if $names[-1] eq '*';
14774be0 101
4ee32f41 102 my $ret =
103 $quote->[0] .
104 join( $join, @names ) .
105 $quote->[-1];
106
107 $ret .= $sep . $post if defined $post;
108 return $ret;
14774be0 109 }
110
4ee32f41 111 method _join(HashRef $ast) {
704c5138 112
cbcfedc1 113 my $output = 'JOIN ' . $self->dispatch($ast->{tablespec});
114
115 $output .= exists $ast->{on}
116 ? ' ON (' . $self->_recurse_where( $ast->{on} )
117 : ' USING (' .$self->dispatch($ast->{using} || croak "No 'on' or 'join' clause passed to -join");
704c5138 118
cbcfedc1 119 $output .= ")";
120 return $output;
704c5138 121
14774be0 122 }
123
7a56723e 124 method _list(AST $ast) {
125 my @items = @{$ast->{args}};
14774be0 126
127 return join(
128 $self->list_separator,
129 map { $self->dispatch($_) } @items);
130 }
131
7a56723e 132 method _alias(AST $ast) {
133
4ee32f41 134 # TODO: Maybe we want qq{ AS "$as"} here
7a56723e 135 return $self->dispatch($ast->{ident}) . " AS " . $ast->{as};
14774be0 136
137 }
138
1b85673a 139 method _value(HashAST $ast) {
14774be0 140
1b85673a 141 $self->add_bind($ast->{value});
14774be0 142 return "?";
143 }
144
cbcfedc1 145 method _recurse_where(ArrayRef $clauses) {
14774be0 146
147 my $OP = 'AND';
148 my $prio = $SQL::Abstract::PRIO{and};
149 my $first = $clauses->[0];
150
cbcfedc1 151 if (!ref $first) {
152 if ($first =~ /^-(and|or)$/) {
153 $OP = uc($1);
154 $prio = $SQL::Abstract::PRIO{$1};
155 shift @$clauses;
156 } else {
0406569b 157 # If first is not a ref, and its not -and or -or, then $clauses
158 # contains just a single clause
cbcfedc1 159 $clauses = [ $clauses ];
160 }
14774be0 161 }
162
0bf8a8c4 163 my $dispatch_table = $self->where_dispatch_table;
164
14774be0 165 my @output;
166 foreach (@$clauses) {
cbcfedc1 167 croak "invalid component in where clause: $_" unless is_ArrayRef($_);
14774be0 168 my $op = $_->[0];
169
0c371882 170 if ($op =~ /^-(and|or)$/) {
14774be0 171 my $sub_prio = $SQL::Abstract::PRIO{$1};
172
173 if ($sub_prio <= $prio) {
174 push @output, $self->_recurse_where($_);
175 } else {
176 push @output, '(' . $self->_recurse_where($_) . ')';
177 }
178 } else {
0c371882 179 push @output, $self->_where_component($_);
14774be0 180 }
181 }
182
183 return join(" $OP ", @output);
184 }
185
1b85673a 186 method _where_component(HashAST $ast) {
187 my $op = $ast->{-type};
0c371882 188
189 if (my $code = $self->lookup_where_dispatch($op)) {
190
191 return $code->($self, $ast);
192
193 }
194 croak "'$op' is not a valid clause in a where AST"
195 if $op =~ /^-/;
196
197 croak "'$op' is not a valid operator";
198
199 }
200
1b85673a 201 method _expr(HashAST $ast) {
202 my $op = $ast->{op};
203 my $meth = $self->lookup_where_dispatch($op) || confess "Invalid operator '$op'";
204
205 $meth->($self, $ast);
206 }
0c371882 207
1b85673a 208 method _binop(HashAST $ast) {
209 my ($lhs, $rhs) = @{$ast->{args}};
210 my $op = $ast->{op};
0bf8a8c4 211
0c371882 212 join (' ', $self->_where_component($lhs),
0bf8a8c4 213 $self->binop_mapping($op) || croak("Unknown binary operator $op"),
0c371882 214 $self->_where_component($rhs)
14774be0 215 );
216 }
217
cbcfedc1 218 method _in(ArrayAST $ast) {
0bf8a8c4 219 my ($tag, $field, @values) = @$ast;
220
221 my $not = $tag =~ /^-not/ ? " NOT" : "";
14774be0 222
44cfd1f6 223 return $self->_false if @values == 0;
0c371882 224 return $self->_where_component($field) .
0bf8a8c4 225 $not.
14774be0 226 " IN (" .
227 join(", ", map { $self->dispatch($_) } @values ) .
228 ")";
229 }
230
231 method _generic_func(ArrayRef $ast) {
232 }
233
44cfd1f6 234 # 'constants' that are portable across DBs
235 method _false($ast?) { "0 = 1" }
236 method _true($ast?) { "1 = 1" }
237
14774be0 238}