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 | |
57 | for (qw//) { |
4ee32f41 |
58 | if (exists $ast->{$_}) { |
59 | my $sub_ast = $ast->{$_}; |
60 | $sub_ast->{-type} = "$_" if is_HashRef($sub_ast); |
61 | confess "$_ option is not an AST" |
62 | unless is_AST($sub_ast); |
63 | |
64 | push @output, $self->dispatch($sub_ast); |
65 | } |
66 | } |
67 | |
68 | return join(' ', @output); |
14774be0 |
69 | } |
70 | |
64c32031 |
71 | method _join(HashRef $ast) { |
d0ad3a92 |
72 | |
73 | # TODO: Validate join type |
74 | my $type = $ast->{join_type} || ""; |
14774be0 |
75 | |
f7dc4536 |
76 | my @output = $self->dispatch($ast->{lhs}); |
d0ad3a92 |
77 | |
78 | push @output, uc $type if $type; |
f7dc4536 |
79 | push @output, "JOIN", $self->dispatch($ast->{rhs}); |
64c32031 |
80 | |
d0ad3a92 |
81 | push @output, |
82 | exists $ast->{on} |
83 | ? ('ON', '(' . $self->_expr( $ast->{on} ) . ')' ) |
84 | : ('USING', '(' .$self->dispatch($ast->{using} || croak "No 'on' or 'join' clause passed to -join"). |
85 | ')' ); |
64c32031 |
86 | |
d0ad3a92 |
87 | return join(" ", @output); |
64c32031 |
88 | |
14774be0 |
89 | } |
90 | |
7a56723e |
91 | method _order_by(AST $ast) { |
92 | my @clauses = @{$ast->{order_by}}; |
93 | |
14774be0 |
94 | my @output; |
95 | |
96 | for (@clauses) { |
7a56723e |
97 | if (is_ArrayRef($_) && $_->[0] =~ /^-(asc|desc)$/) { |
14774be0 |
98 | my $o = $1; |
99 | push @output, $self->dispatch($_->[1]) . " " . uc($o); |
100 | next; |
101 | } |
102 | push @output, $self->dispatch($_); |
103 | } |
104 | |
105 | return "ORDER BY " . join(", ", @output); |
106 | } |
107 | |
747f7c21 |
108 | method _name(HashAST $ast) { |
7a56723e |
109 | my @names = @{$ast->{args}}; |
14774be0 |
110 | |
111 | my $sep = $self->name_separator; |
4ee32f41 |
112 | my $quote = $self->is_quoting |
113 | ? $self->quote_chars |
114 | : [ '' ]; |
115 | |
116 | my $join = $quote->[-1] . $sep . $quote->[0]; |
14774be0 |
117 | |
4ee32f41 |
118 | # We dont want to quote * in [qw/me */]: `me`.* is the desired output there |
119 | # This means you can't have a field called `*`. I am willing to accept this |
120 | # situation, cos thats a really stupid thing to want. |
121 | my $post; |
122 | $post = pop @names if $names[-1] eq '*'; |
14774be0 |
123 | |
4ee32f41 |
124 | my $ret = |
125 | $quote->[0] . |
126 | join( $join, @names ) . |
127 | $quote->[-1]; |
128 | |
129 | $ret .= $sep . $post if defined $post; |
130 | return $ret; |
14774be0 |
131 | } |
132 | |
14774be0 |
133 | |
7a56723e |
134 | method _list(AST $ast) { |
135 | my @items = @{$ast->{args}}; |
14774be0 |
136 | |
137 | return join( |
138 | $self->list_separator, |
139 | map { $self->dispatch($_) } @items); |
140 | } |
141 | |
747f7c21 |
142 | # TODO: I think i want to parameterized AST type to get better validation |
7a56723e |
143 | method _alias(AST $ast) { |
144 | |
4ee32f41 |
145 | # TODO: Maybe we want qq{ AS "$as"} here |
7a56723e |
146 | return $self->dispatch($ast->{ident}) . " AS " . $ast->{as}; |
14774be0 |
147 | |
148 | } |
149 | |
1b85673a |
150 | method _value(HashAST $ast) { |
14774be0 |
151 | |
1b85673a |
152 | $self->add_bind($ast->{value}); |
14774be0 |
153 | return "?"; |
154 | } |
155 | |
ef0d6124 |
156 | # Perhaps badly named. handles 'and' and 'or' clauses |
a464be15 |
157 | method _recurse_where(HashAST $ast) { |
14774be0 |
158 | |
a464be15 |
159 | my $op = $ast->{op}; |
14774be0 |
160 | |
a464be15 |
161 | my $OP = uc $op; |
162 | my $prio = $SQL::Abstract::PRIO{$op}; |
14774be0 |
163 | |
ef0d6124 |
164 | my $dispatch_table = $self->expr_dispatch_table; |
0bf8a8c4 |
165 | |
14774be0 |
166 | my @output; |
a464be15 |
167 | foreach ( @{$ast->{args}} ) { |
e7996b3a |
168 | croak "invalid component in where clause: $_" unless is_HashAST($_); |
14774be0 |
169 | |
9d7d0694 |
170 | if ($_->{-type} eq 'expr' && $_->{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 { |
ef0d6124 |
179 | push @output, $self->_expr($_); |
14774be0 |
180 | } |
181 | } |
182 | |
183 | return join(" $OP ", @output); |
184 | } |
185 | |
ef0d6124 |
186 | method _expr(HashAST $ast) { |
1b85673a |
187 | my $op = $ast->{-type}; |
0c371882 |
188 | |
ef0d6124 |
189 | $op = $ast->{op} if $op eq 'expr'; |
190 | |
191 | if (my $code = $self->lookup_expr_dispatch($op)) { |
0c371882 |
192 | |
193 | return $code->($self, $ast); |
194 | |
195 | } |
ef0d6124 |
196 | croak "'$op' is not a valid AST type in an expression with " . dump($ast) |
197 | if $ast->{-type} ne 'expr'; |
0c371882 |
198 | |
ef0d6124 |
199 | croak "'$op' is not a valid operator in an expression with " . dump($ast); |
1b85673a |
200 | |
1b85673a |
201 | } |
0c371882 |
202 | |
1b85673a |
203 | method _binop(HashAST $ast) { |
204 | my ($lhs, $rhs) = @{$ast->{args}}; |
205 | my $op = $ast->{op}; |
0bf8a8c4 |
206 | |
ef0d6124 |
207 | join (' ', $self->_expr($lhs), |
0bf8a8c4 |
208 | $self->binop_mapping($op) || croak("Unknown binary operator $op"), |
ef0d6124 |
209 | $self->_expr($rhs) |
14774be0 |
210 | ); |
211 | } |
212 | |
a464be15 |
213 | method _in(HashAST $ast) { |
214 | |
9d7d0694 |
215 | my ($field,@values) = @{$ast->{args}}; |
a464be15 |
216 | |
9d7d0694 |
217 | my $not = ($ast->{op} =~ /^not_/) ? " NOT" : ""; |
0bf8a8c4 |
218 | |
9d7d0694 |
219 | return $self->_false unless @values; |
14774be0 |
220 | |
ef0d6124 |
221 | return $self->_expr($field) . |
9d7d0694 |
222 | $not . |
14774be0 |
223 | " IN (" . |
9d7d0694 |
224 | join(", ", map { $self->dispatch($_) } @values ) . |
14774be0 |
225 | ")"; |
226 | } |
227 | |
228 | method _generic_func(ArrayRef $ast) { |
229 | } |
230 | |
44cfd1f6 |
231 | # 'constants' that are portable across DBs |
232 | method _false($ast?) { "0 = 1" } |
233 | method _true($ast?) { "1 = 1" } |
234 | |
14774be0 |
235 | } |