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