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