Start working on update clause
[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;
bad761ba 11 use SQL::Abstract::Types qw/AST/;
ef0d6124 12 use Devel::PartialDump qw/dump/;
14774be0 13
14 clean;
15
0c371882 16 # set things that are valid in where clauses
ef0d6124 17 override _build_expr_dispatch_table {
0bf8a8c4 18 return {
19 %{super()},
1b85673a 20 in => $self->can('_in'),
21 not_in => $self->can('_in'),
1f4bd99c 22 between => $self->can('_between'),
23 not_between => $self->can('_between'),
a464be15 24 and => $self->can('_recurse_where'),
25 or => $self->can('_recurse_where'),
1b85673a 26 map { +"$_" => $self->can("_$_") } qw/
0c371882 27 value
627dcb62 28 identifier
0c371882 29 true
30 false
e7996b3a 31 expr
0c371882 32 /
0bf8a8c4 33 };
14774be0 34 }
35
bad761ba 36 method _select(AST $ast) {
747f7c21 37 # Default to requiring columns and from.
38 # DB specific ones (i.e. mysql/Pg) can not require the FROM part with a bit
39 # of refactoring
40
41 for (qw/columns tablespec/) {
42 confess "'$_' is required in select AST with " . dump ($ast)
43 unless exists $ast->{$_};
4ee32f41 44 }
45
46 # Check that columns is a -list
747f7c21 47 confess "'columns' should be an array ref, not " . dump($ast->{columns})
48 unless is_ArrayRef($ast->{columns});
49
64c32031 50 my $cols = $self->_list({-type => 'list', args => $ast->{columns} });
4ee32f41 51
52 my @output = (
747f7c21 53 SELECT => $cols
4ee32f41 54 );
55
747f7c21 56 push @output, FROM => $self->dispatch($ast->{tablespec})
57 if exists $ast->{tablespec};
58
924d940e 59 if (exists $ast->{where}) {
60 my $sub_ast = $ast->{where};
61
62 confess "$_ option is not an AST: " . dump($sub_ast)
63 unless is_AST($sub_ast);
64
65 push @output, "WHERE", $self->_expr($sub_ast);
66 }
67
68 for (qw/group_by having order_by/) {
4ee32f41 69 if (exists $ast->{$_}) {
70 my $sub_ast = $ast->{$_};
e68f980b 71
924d940e 72 confess "$_ option is not an AST or an ArrayRef: " . dump($sub_ast)
73 unless is_AST($sub_ast) || is_ArrayRef($sub_ast);;
4ee32f41 74
e68f980b 75 my $meth = "__$_";
76 push @output, $self->$meth($sub_ast);
4ee32f41 77 }
78 }
79
80 return join(' ', @output);
14774be0 81 }
82
d4656fcf 83 method _update(AST $ast) {
84
85 for (qw/columns values tablespec/) {
86 confess "'$_' is required in update AST with " . dump ($ast)
87 unless exists $ast->{$_};
88 }
89
90 }
91
64c32031 92 method _join(HashRef $ast) {
d0ad3a92 93
94 # TODO: Validate join type
95 my $type = $ast->{join_type} || "";
14774be0 96
f7dc4536 97 my @output = $self->dispatch($ast->{lhs});
d0ad3a92 98
99 push @output, uc $type if $type;
f7dc4536 100 push @output, "JOIN", $self->dispatch($ast->{rhs});
64c32031 101
d0ad3a92 102 push @output,
103 exists $ast->{on}
104 ? ('ON', '(' . $self->_expr( $ast->{on} ) . ')' )
924d940e 105 : ('USING', '(' .$self->dispatch($ast->{using}
106 || croak "No 'on' or 'uinsg' clause passed to join cluase: " .
107 dump($ast)
108 ) .
d0ad3a92 109 ')' );
64c32031 110
d0ad3a92 111 return join(" ", @output);
64c32031 112
14774be0 113 }
114
924d940e 115 method _ordering(AST $ast) {
116
117 my $output = $self->_expr($ast->{expr});
118
119 $output .= " " . uc $1
120 if $ast->{direction} &&
121 ( $ast->{direction} =~ /^(asc|desc)$/i
122 || confess "Unknown ordering direction " . dump($ast)
123 );
14774be0 124
924d940e 125 return $output;
14774be0 126 }
127
627dcb62 128 method _identifier(AST $ast) {
129 my @names = @{$ast->{elements}};
14774be0 130
627dcb62 131 my $sep = $self->ident_separator;
4ee32f41 132 my $quote = $self->is_quoting
133 ? $self->quote_chars
134 : [ '' ];
135
136 my $join = $quote->[-1] . $sep . $quote->[0];
14774be0 137
4ee32f41 138 # We dont want to quote * in [qw/me */]: `me`.* is the desired output there
139 # This means you can't have a field called `*`. I am willing to accept this
140 # situation, cos thats a really stupid thing to want.
141 my $post;
142 $post = pop @names if $names[-1] eq '*';
14774be0 143
8b398780 144 my $ret;
145 $ret = $quote->[0] .
146 join( $join, @names ) .
147 $quote->[-1]
148 if @names;
149
150 $ret = $ret
151 ? $ret . $sep . $post
152 : $post
153 if defined $post;
154
4ee32f41 155
4ee32f41 156 return $ret;
14774be0 157 }
158
14774be0 159
7a56723e 160 method _list(AST $ast) {
e6a33ce3 161 return "" unless $ast->{args};
162
924d940e 163 my @items = is_ArrayRef($ast->{args})
164 ? @{$ast->{args}}
165 : $ast->{args};
14774be0 166
167 return join(
168 $self->list_separator,
169 map { $self->dispatch($_) } @items);
170 }
171
747f7c21 172 # TODO: I think i want to parameterized AST type to get better validation
7a56723e 173 method _alias(AST $ast) {
174
4ee32f41 175 # TODO: Maybe we want qq{ AS "$as"} here
7a56723e 176 return $self->dispatch($ast->{ident}) . " AS " . $ast->{as};
14774be0 177
178 }
179
bad761ba 180 method _value(AST $ast) {
14774be0 181
1b85673a 182 $self->add_bind($ast->{value});
14774be0 183 return "?";
184 }
185
e68f980b 186 # Not dispatchable to.
924d940e 187 method __having($args) {
188 return "HAVING " . $self->_list({-type => 'list', args => $args});
189 }
190
191 method __group_by($args) {
192 return "GROUP BY " . $self->_list({-type => 'list', args => $args});
e68f980b 193 }
194
924d940e 195 method __order_by($args) {
196 return "ORDER BY " . $self->_list({-type => 'list', args => $args});
197 }
198
199
ef0d6124 200 # Perhaps badly named. handles 'and' and 'or' clauses
bad761ba 201 method _recurse_where(AST $ast) {
14774be0 202
a464be15 203 my $op = $ast->{op};
14774be0 204
a464be15 205 my $OP = uc $op;
206 my $prio = $SQL::Abstract::PRIO{$op};
14774be0 207
ef0d6124 208 my $dispatch_table = $self->expr_dispatch_table;
0bf8a8c4 209
14774be0 210 my @output;
a464be15 211 foreach ( @{$ast->{args}} ) {
bad761ba 212 croak "invalid component in where clause: $_" unless is_AST($_);
14774be0 213
9d7d0694 214 if ($_->{-type} eq 'expr' && $_->{op} =~ /^(and|or)$/) {
14774be0 215 my $sub_prio = $SQL::Abstract::PRIO{$1};
216
39f7dc30 217 if ($sub_prio == $prio) {
218 # When the element below has same priority, i.e. 'or' as a child of
219 # 'or', dont produce extra brackets
14774be0 220 push @output, $self->_recurse_where($_);
221 } else {
222 push @output, '(' . $self->_recurse_where($_) . ')';
223 }
224 } else {
ef0d6124 225 push @output, $self->_expr($_);
14774be0 226 }
227 }
228
229 return join(" $OP ", @output);
230 }
231
bad761ba 232 method _expr(AST $ast) {
1b85673a 233 my $op = $ast->{-type};
0c371882 234
ef0d6124 235 $op = $ast->{op} if $op eq 'expr';
236
237 if (my $code = $self->lookup_expr_dispatch($op)) {
0c371882 238
239 return $code->($self, $ast);
240
241 }
ef0d6124 242 croak "'$op' is not a valid AST type in an expression with " . dump($ast)
243 if $ast->{-type} ne 'expr';
0c371882 244
e6a33ce3 245 # This is an attempt to do some form of validation on function names. This
246 # might end up being a bad thing.
247 croak "'$op' is not a valid operator in an expression with " . dump($ast)
248 if $op =~ /\W/;
249
250 return $self->_generic_function_op($ast);
1b85673a 251
1b85673a 252 }
0c371882 253
bad761ba 254 method _binop(AST $ast) {
1b85673a 255 my ($lhs, $rhs) = @{$ast->{args}};
256 my $op = $ast->{op};
0bf8a8c4 257
ec376489 258 # IS NOT? NULL
259 if ($rhs->{-type} eq 'value' && !defined $rhs->{value} &&
260 ($op eq '==' || $op eq '!='))
261 {
262 return $self->_expr($lhs) .
263 ($op eq '==' ? " IS " : " IS NOT ") .
264 "NULL";
265 }
266
ef0d6124 267 join (' ', $self->_expr($lhs),
0bf8a8c4 268 $self->binop_mapping($op) || croak("Unknown binary operator $op"),
ef0d6124 269 $self->_expr($rhs)
14774be0 270 );
271 }
272
e6a33ce3 273 method _generic_function_op(AST $ast) {
274 my $op = $ast->{op};
275
276 return "$op(" . $self->_list($ast) . ")";
277 }
278
bad761ba 279 method _in(AST $ast) {
a464be15 280
9d7d0694 281 my ($field,@values) = @{$ast->{args}};
a464be15 282
9d7d0694 283 my $not = ($ast->{op} =~ /^not_/) ? " NOT" : "";
0bf8a8c4 284
9d7d0694 285 return $self->_false unless @values;
14774be0 286
ef0d6124 287 return $self->_expr($field) .
9d7d0694 288 $not .
14774be0 289 " IN (" .
9d7d0694 290 join(", ", map { $self->dispatch($_) } @values ) .
14774be0 291 ")";
292 }
293
1f4bd99c 294 method _between(AST $ast) {
295
296 my ($field,@values) = @{$ast->{args}};
297
298 my $not = ($ast->{op} =~ /^not_/) ? " NOT" : "";
299 croak "between requires 3 arguments: " . dump($ast)
300 unless @values == 2;
301
39f7dc30 302 # The brackets are to work round an issue with SQL::A::Test
303 return "(" .
304 $self->_expr($field) .
1f4bd99c 305 $not .
306 " BETWEEN " .
39f7dc30 307 join(" AND ", map { $self->dispatch($_) } @values ) .
308 ")";
1f4bd99c 309 }
310
44cfd1f6 311 # 'constants' that are portable across DBs
312 method _false($ast?) { "0 = 1" }
313 method _true($ast?) { "1 = 1" }
314
14774be0 315}