Commit | Line | Data |
14774be0 |
1 | use MooseX::Declare; |
2 | |
3 | class 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/; |
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 | |
cbcfedc1 |
34 | method _select(HashAST $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 | |
747f7c21 |
117 | method _name(HashAST $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 | |
4ee32f41 |
133 | my $ret = |
134 | $quote->[0] . |
135 | join( $join, @names ) . |
136 | $quote->[-1]; |
137 | |
138 | $ret .= $sep . $post if defined $post; |
139 | return $ret; |
14774be0 |
140 | } |
141 | |
14774be0 |
142 | |
7a56723e |
143 | method _list(AST $ast) { |
924d940e |
144 | my @items = is_ArrayRef($ast->{args}) |
145 | ? @{$ast->{args}} |
146 | : $ast->{args}; |
14774be0 |
147 | |
148 | return join( |
149 | $self->list_separator, |
150 | map { $self->dispatch($_) } @items); |
151 | } |
152 | |
747f7c21 |
153 | # TODO: I think i want to parameterized AST type to get better validation |
7a56723e |
154 | method _alias(AST $ast) { |
155 | |
4ee32f41 |
156 | # TODO: Maybe we want qq{ AS "$as"} here |
7a56723e |
157 | return $self->dispatch($ast->{ident}) . " AS " . $ast->{as}; |
14774be0 |
158 | |
159 | } |
160 | |
1b85673a |
161 | method _value(HashAST $ast) { |
14774be0 |
162 | |
1b85673a |
163 | $self->add_bind($ast->{value}); |
14774be0 |
164 | return "?"; |
165 | } |
166 | |
e68f980b |
167 | # Not dispatchable to. |
924d940e |
168 | method __having($args) { |
169 | return "HAVING " . $self->_list({-type => 'list', args => $args}); |
170 | } |
171 | |
172 | method __group_by($args) { |
173 | return "GROUP BY " . $self->_list({-type => 'list', args => $args}); |
e68f980b |
174 | } |
175 | |
924d940e |
176 | method __order_by($args) { |
177 | return "ORDER BY " . $self->_list({-type => 'list', args => $args}); |
178 | } |
179 | |
180 | |
ef0d6124 |
181 | # Perhaps badly named. handles 'and' and 'or' clauses |
a464be15 |
182 | method _recurse_where(HashAST $ast) { |
14774be0 |
183 | |
a464be15 |
184 | my $op = $ast->{op}; |
14774be0 |
185 | |
a464be15 |
186 | my $OP = uc $op; |
187 | my $prio = $SQL::Abstract::PRIO{$op}; |
14774be0 |
188 | |
ef0d6124 |
189 | my $dispatch_table = $self->expr_dispatch_table; |
0bf8a8c4 |
190 | |
14774be0 |
191 | my @output; |
a464be15 |
192 | foreach ( @{$ast->{args}} ) { |
e7996b3a |
193 | croak "invalid component in where clause: $_" unless is_HashAST($_); |
14774be0 |
194 | |
9d7d0694 |
195 | if ($_->{-type} eq 'expr' && $_->{op} =~ /^(and|or)$/) { |
14774be0 |
196 | my $sub_prio = $SQL::Abstract::PRIO{$1}; |
197 | |
198 | if ($sub_prio <= $prio) { |
199 | push @output, $self->_recurse_where($_); |
200 | } else { |
201 | push @output, '(' . $self->_recurse_where($_) . ')'; |
202 | } |
203 | } else { |
ef0d6124 |
204 | push @output, $self->_expr($_); |
14774be0 |
205 | } |
206 | } |
207 | |
208 | return join(" $OP ", @output); |
209 | } |
210 | |
ef0d6124 |
211 | method _expr(HashAST $ast) { |
1b85673a |
212 | my $op = $ast->{-type}; |
0c371882 |
213 | |
ef0d6124 |
214 | $op = $ast->{op} if $op eq 'expr'; |
215 | |
216 | if (my $code = $self->lookup_expr_dispatch($op)) { |
0c371882 |
217 | |
218 | return $code->($self, $ast); |
219 | |
220 | } |
ef0d6124 |
221 | croak "'$op' is not a valid AST type in an expression with " . dump($ast) |
222 | if $ast->{-type} ne 'expr'; |
0c371882 |
223 | |
ef0d6124 |
224 | croak "'$op' is not a valid operator in an expression with " . dump($ast); |
1b85673a |
225 | |
1b85673a |
226 | } |
0c371882 |
227 | |
1b85673a |
228 | method _binop(HashAST $ast) { |
229 | my ($lhs, $rhs) = @{$ast->{args}}; |
230 | my $op = $ast->{op}; |
0bf8a8c4 |
231 | |
ef0d6124 |
232 | join (' ', $self->_expr($lhs), |
0bf8a8c4 |
233 | $self->binop_mapping($op) || croak("Unknown binary operator $op"), |
ef0d6124 |
234 | $self->_expr($rhs) |
14774be0 |
235 | ); |
236 | } |
237 | |
a464be15 |
238 | method _in(HashAST $ast) { |
239 | |
9d7d0694 |
240 | my ($field,@values) = @{$ast->{args}}; |
a464be15 |
241 | |
9d7d0694 |
242 | my $not = ($ast->{op} =~ /^not_/) ? " NOT" : ""; |
0bf8a8c4 |
243 | |
9d7d0694 |
244 | return $self->_false unless @values; |
14774be0 |
245 | |
ef0d6124 |
246 | return $self->_expr($field) . |
9d7d0694 |
247 | $not . |
14774be0 |
248 | " IN (" . |
9d7d0694 |
249 | join(", ", map { $self->dispatch($_) } @values ) . |
14774be0 |
250 | ")"; |
251 | } |
252 | |
253 | method _generic_func(ArrayRef $ast) { |
254 | } |
255 | |
44cfd1f6 |
256 | # 'constants' that are portable across DBs |
257 | method _false($ast?) { "0 = 1" } |
258 | method _true($ast?) { "1 = 1" } |
259 | |
14774be0 |
260 | } |