Commit | Line | Data |
f8392f56 |
1 | package SQL::Abstract::ExtraClauses; |
2 | |
b40d30dc |
3 | use Moo; |
4 | |
5 | has sqla => ( |
6 | is => 'ro', init_arg => undef, |
7 | handles => [ qw( |
312abf42 |
8 | expand_expr expand_maybe_list_expr render_aqt join_query_parts |
b40d30dc |
9 | ) ], |
10 | ); |
f8392f56 |
11 | |
b40d30dc |
12 | sub cb { |
22f5ed9a |
13 | my ($self, $method, @args) = @_; |
14 | return sub { |
15 | local $self->{sqla} = shift; |
16 | $self->$method(@args, @_) |
17 | }; |
b40d30dc |
18 | } |
19 | |
20 | sub apply_to { |
21 | my ($self, $sqla) = @_; |
22 | $self = $self->new unless ref($self); |
ac3616e8 |
23 | my @clauses = $sqla->clauses_of('select'); |
f7a20100 |
24 | my @before_setop; |
25 | CLAUSE: foreach my $idx (0..$#clauses) { |
26 | if ($clauses[$idx] eq 'order_by') { |
27 | @before_setop = @clauses[0..$idx-1]; |
28 | splice(@clauses, $idx, 0, qw(setop group_by having)); |
29 | last CLAUSE; |
30 | } |
31 | } |
32 | die "Huh?" unless @before_setop; |
ac3616e8 |
33 | $sqla->clauses_of(select => 'with', @clauses); |
34 | $sqla->clause_expanders( |
b40d30dc |
35 | 'select.group_by', $self->cb(sub { |
035449c2 |
36 | $_[0]->expand_maybe_list_expr($_[2], -ident) |
b40d30dc |
37 | }), |
38 | 'select.having', $self->cb(sub { $_[0]->expand_expr($_[2]) }), |
71c1b4d5 |
39 | ); |
01dfea60 |
40 | foreach my $thing (qw(join from_list)) { |
ac3616e8 |
41 | $sqla->expander($thing => $self->cb("_expand_${thing}")) |
b40d30dc |
42 | ->renderer($thing => $self->cb("_render_${thing}")) |
01dfea60 |
43 | } |
ac3616e8 |
44 | $sqla->op_expander(as => $self->cb('_expand_op_as')); |
45 | $sqla->expander(as => $self->cb('_expand_op_as')); |
46 | $sqla->renderer(as => $self->cb('_render_as')); |
6d42f0b9 |
47 | $sqla->expander(alias => $self->cb('_expand_alias')); |
ac3616e8 |
48 | $sqla->renderer(alias => $self->cb('_render_alias')); |
71c1b4d5 |
49 | |
ac3616e8 |
50 | $sqla->clauses_of(update => sub { |
e6b86bee |
51 | my ($self, @clauses) = @_; |
52 | splice(@clauses, 2, 0, 'from'); |
53 | @clauses; |
54 | }); |
55 | |
ac3616e8 |
56 | $sqla->clauses_of(delete => sub { |
e6b86bee |
57 | my ($self, @clauses) = @_; |
58 | splice(@clauses, 1, 0, 'using'); |
59 | @clauses; |
60 | }); |
61 | |
ac3616e8 |
62 | $sqla->clause_expanders( |
c319683e |
63 | 'update.from' => $self->cb('_expand_from_list'), |
adda3bd8 |
64 | 'delete.using' => $self->cb('_expand_from_list'), |
b40d30dc |
65 | 'insert.rowvalues' => $self->cb(sub { |
1107714b |
66 | +(from => $_[0]->expand_expr({ -values => $_[2] })); |
b40d30dc |
67 | }), |
68 | 'insert.select' => $self->cb(sub { |
1107714b |
69 | +(from => $_[0]->expand_expr({ -select => $_[2] })); |
b40d30dc |
70 | }), |
71c1b4d5 |
71 | ); |
37b399a8 |
72 | |
f7a20100 |
73 | # set ops |
ac3616e8 |
74 | $sqla->wrap_expander(select => sub { |
22f5ed9a |
75 | $self->cb('_expand_select', $_[0], \@before_setop); |
95ab9342 |
76 | }); |
f7a20100 |
77 | |
5824607d |
78 | $sqla->clause_renderer( |
79 | 'select.setop' => $self->cb(sub { $_[0]->render_aqt($_[2]); }) |
80 | ); |
f7a20100 |
81 | |
4b5f7259 |
82 | foreach my $setop (qw(union intersect except)) { |
83 | $sqla->expander($setop => $self->cb('_expand_setop')); |
84 | $sqla->renderer($setop => $self->cb('_render_setop')); |
85 | } |
b42692a6 |
86 | |
0f4de029 |
87 | my $setop_expander = $self->cb('_expand_clause_setop'); |
bb36c26d |
88 | |
ac3616e8 |
89 | $sqla->clause_expanders( |
bb36c26d |
90 | map +($_ => $setop_expander), |
91 | map "select.${_}", |
92 | map +($_, "${_}_all", "${_}_distinct"), |
93 | qw(union intersect except) |
94 | ); |
f7fd09f7 |
95 | |
6d42f0b9 |
96 | my $w_exp = $self->cb('_expand_with'); |
97 | my $w_rdr = $self->cb('_render_with'); |
98 | $sqla->clause_expander('select.with' => $w_exp); |
99 | $sqla->clause_expander('select.with_recursive' => $w_exp); |
100 | $sqla->clause_renderer('select.with' => $w_rdr); |
101 | |
44a6affb |
102 | foreach my $stmt (qw(insert update delete)) { |
ac3616e8 |
103 | $sqla->clauses_of($stmt => 'with', $sqla->clauses_of($stmt)); |
6d42f0b9 |
104 | $sqla->clause_expander("${stmt}.$_" => $w_exp) |
44a6affb |
105 | for qw(with with_recursive); |
6d42f0b9 |
106 | $sqla->clause_renderer("${stmt}.with" => $w_rdr); |
44a6affb |
107 | } |
6d42f0b9 |
108 | |
109 | $sqla->expander(cast => $self->cb('_expand_cast')); |
b40d30dc |
110 | |
ac3616e8 |
111 | $sqla->clause_expanders( |
c319683e |
112 | "select.from", $self->cb('_expand_from_list'), |
b40d30dc |
113 | "update.target", $self->cb('_expand_update_clause_target'), |
114 | "update.update", $self->cb('_expand_update_clause_target'), |
115 | ); |
f7a20100 |
116 | |
ac3616e8 |
117 | return $sqla; |
f8392f56 |
118 | } |
119 | |
22f5ed9a |
120 | sub _expand_select { |
b5c13e0a |
121 | my ($self, $orig, $before_setop, @args) = @_; |
122 | my $exp = $self->sqla->$orig(@args); |
22f5ed9a |
123 | return $exp unless my $setop = (my $sel = $exp->{-select})->{setop}; |
124 | if (my @keys = grep $sel->{$_}, @$before_setop) { |
125 | my %inner; @inner{@keys} = delete @{$sel}{@keys}; |
126 | unshift @{(values(%$setop))[0]{queries}}, |
127 | { -select => \%inner }; |
128 | } |
129 | return $exp; |
130 | } |
131 | |
f79455dc |
132 | sub _expand_from_list { |
133 | my ($self, undef, $args) = @_; |
134 | if (ref($args) eq 'HASH') { |
09ceda11 |
135 | return $args if $args->{-from_list}; |
f79455dc |
136 | return { -from_list => [ $self->expand_expr($args) ] }; |
137 | } |
138 | my @list; |
b9e35873 |
139 | my @args = ref($args) eq 'ARRAY' ? @$args : ($args); |
f79455dc |
140 | while (my $entry = shift @args) { |
6990b2aa |
141 | if (!ref($entry) and $entry =~ /^-(.*)/) { |
142 | if ($1 eq 'as') { |
143 | $list[-1] = $self->expand_expr({ -as => [ |
144 | $list[-1], map +(ref($_) eq 'ARRAY' ? @$_ : $_), shift(@args) |
145 | ]}); |
146 | next; |
147 | } |
f79455dc |
148 | $entry = { $entry => shift @args }; |
149 | } |
150 | my $aqt = $self->expand_expr($entry, -ident); |
151 | if ($aqt->{-join} and not $aqt->{-join}{from}) { |
152 | $aqt->{-join}{from} = pop @list; |
153 | } |
154 | push @list, $aqt; |
155 | } |
156 | return { -from_list => \@list }; |
157 | } |
158 | |
159 | sub _expand_join { |
160 | my ($self, undef, $args) = @_; |
161 | my %proto = ( |
162 | ref($args) eq 'HASH' |
163 | ? %$args |
164 | : (to => $args->[0], @{$args}[1..$#$args]) |
165 | ); |
6990b2aa |
166 | if (my $as = delete $proto{as}) { |
01e9b916 |
167 | $proto{to} = $self->expand_expr({ -as => [ $proto{to}, $as ] }); |
6990b2aa |
168 | } |
0891ae97 |
169 | if (defined($proto{using}) and ref(my $using = $proto{using}) ne 'HASH') { |
60757815 |
170 | $proto{using} = [ |
0891ae97 |
171 | map [ $self->expand_expr($_, -ident) ], |
172 | ref($using) eq 'ARRAY' ? @$using: $using |
60757815 |
173 | ]; |
0891ae97 |
174 | } |
f79455dc |
175 | my %ret = map +($_ => $self->expand_expr($proto{$_}, -ident)), |
176 | sort keys %proto; |
177 | return +{ -join => \%ret }; |
178 | } |
179 | |
180 | sub _render_from_list { |
6c39a2f7 |
181 | my ($self, undef, $list) = @_; |
3e230b71 |
182 | return $self->join_query_parts(', ', @$list); |
f79455dc |
183 | } |
184 | |
185 | sub _render_join { |
6c39a2f7 |
186 | my ($self, undef, $args) = @_; |
f79455dc |
187 | |
188 | my @parts = ( |
e48c4b9a |
189 | $args->{from}, |
312abf42 |
190 | { -keyword => join '_', ($args->{type}||()), 'join' }, |
cbe3b5a9 |
191 | (map +($_->{-ident} || $_->{-as} ? $_ : ('(', $_, ')')), $args->{to}), |
f79455dc |
192 | ($args->{on} ? ( |
312abf42 |
193 | { -keyword => 'on' }, |
e48c4b9a |
194 | $args->{on}, |
f79455dc |
195 | ) : ()), |
196 | ($args->{using} ? ( |
312abf42 |
197 | { -keyword => 'using' }, |
60757815 |
198 | '(', $args->{using}, ')', |
f79455dc |
199 | ) : ()), |
200 | ); |
59c7f80e |
201 | return $self->join_query_parts(' ', @parts); |
f79455dc |
202 | } |
203 | |
6990b2aa |
204 | sub _expand_op_as { |
205 | my ($self, undef, $vv, $k) = @_; |
01e9b916 |
206 | my @vv = (ref($vv) eq 'ARRAY' ? @$vv : $vv); |
207 | $k ||= shift @vv; |
208 | my $ik = $self->expand_expr($k, -ident); |
209 | return +{ -as => [ $ik, $self->expand_expr($vv[0], -alias) ] } |
210 | if @vv == 1 and ref($vv[0]) eq 'HASH'; |
211 | |
212 | my @as = map $self->expand_expr($_, -ident), @vv; |
213 | return { -as => [ $ik, { -alias => \@as } ] }; |
6990b2aa |
214 | } |
215 | |
216 | sub _render_as { |
6c39a2f7 |
217 | my ($self, undef, $args) = @_; |
01e9b916 |
218 | my ($thing, $alias) = @$args; |
59c7f80e |
219 | return $self->join_query_parts( |
8d1295c3 |
220 | ' ', |
01e9b916 |
221 | $thing, |
312abf42 |
222 | { -keyword => 'as' }, |
01e9b916 |
223 | $alias, |
369e7844 |
224 | ); |
225 | } |
226 | |
227 | sub _render_alias { |
01e9b916 |
228 | my ($self, undef, $args) = @_; |
369e7844 |
229 | my ($as, @cols) = @$args; |
230 | return (@cols |
59c7f80e |
231 | ? $self->join_query_parts('', |
3e230b71 |
232 | $as, |
68a92d22 |
233 | '(', |
234 | $self->join_query_parts( |
235 | ', ', |
236 | @cols |
237 | ), |
238 | ')', |
369e7844 |
239 | ) |
240 | : $self->render_aqt($as) |
6990b2aa |
241 | ); |
242 | } |
243 | |
af407e9a |
244 | sub _expand_update_clause_target { |
1107714b |
245 | my ($self, undef, $target) = @_; |
af407e9a |
246 | +(target => $self->_expand_from_list(undef, $target)); |
247 | } |
248 | |
6d42f0b9 |
249 | sub _expand_cast { |
250 | my ($self, undef, $thing) = @_; |
251 | return { -func => [ cast => $thing ] } if ref($thing) eq 'HASH'; |
252 | my ($cast, $to) = @{$thing}; |
253 | +{ -func => [ cast => { -as => [ |
254 | $self->expand_expr($cast), |
255 | $self->expand_expr($to, -ident), |
256 | ] } ] }; |
257 | } |
258 | |
259 | sub _expand_alias { |
260 | my ($self, undef, $args) = @_; |
261 | if (ref($args) eq 'HASH' and my $alias = $args->{-alias}) { |
262 | $args = $alias; |
263 | } |
264 | +{ -alias => [ |
265 | map $self->expand_expr($_, -ident), |
266 | ref($args) eq 'ARRAY' ? @{$args} : $args |
267 | ] |
268 | } |
269 | } |
270 | |
271 | sub _expand_with { |
272 | my ($self, $name, $with) = @_; |
273 | my (undef, $type) = split '_', $name; |
274 | if (ref($with) eq 'HASH') { |
275 | return +{ |
276 | %$with, |
277 | queries => [ |
278 | map +[ |
279 | $self->expand_expr({ -alias => $_->[0] }, -ident), |
280 | $self->expand_expr($_->[1]), |
281 | ], @{$with->{queries}} |
282 | ] |
283 | } |
284 | } |
285 | my @with = @$with; |
286 | my @exp; |
287 | while (my ($alias, $query) = splice @with, 0, 2) { |
288 | push @exp, [ |
289 | $self->expand_expr({ -alias => $alias }, -ident), |
290 | $self->expand_expr($query) |
291 | ]; |
292 | } |
293 | return +(with => { ($type ? (type => $type) : ()), queries => \@exp }); |
294 | } |
295 | |
296 | sub _render_with { |
297 | my ($self, undef, $with) = @_; |
298 | my $q_part = $self->join_query_parts(', ', |
299 | map { |
300 | my ($alias, $query) = @$_; |
301 | $self->join_query_parts(' ', |
302 | $alias, |
312abf42 |
303 | { -keyword => 'as' }, |
6d42f0b9 |
304 | $query, |
305 | ) |
306 | } @{$with->{queries}} |
307 | ); |
308 | return $self->join_query_parts(' ', |
312abf42 |
309 | { -keyword => join '_', 'with', ($with->{type}||'') }, |
6d42f0b9 |
310 | $q_part, |
311 | ); |
312 | } |
313 | |
4b5f7259 |
314 | sub _expand_setop { |
315 | my ($self, $setop, $args) = @_; |
316 | +{ "-${setop}" => { |
317 | %$args, |
318 | queries => [ map $self->expand_expr($_), @{$args->{queries}} ], |
319 | } }; |
320 | } |
321 | |
acfbc601 |
322 | sub _render_setop { |
323 | my ($self, $setop, $args) = @_; |
324 | $self->join_query_parts( |
312abf42 |
325 | { -keyword => ' '.join('_', $setop, ($args->{type}||())).' ' }, |
acfbc601 |
326 | @{$args->{queries}} |
327 | ); |
328 | } |
329 | |
0f4de029 |
330 | sub _expand_clause_setop { |
331 | my ($self, $setop, $args) = @_; |
332 | my ($op, $type) = split '_', $setop; |
333 | +(setop => $self->expand_expr({ |
334 | "-${op}" => { |
335 | ($type ? (type => $type) : ()), |
336 | queries => (ref($args) eq 'ARRAY' ? $args : [ $args ]) |
337 | } |
338 | })); |
339 | } |
340 | |
f8392f56 |
341 | 1; |