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 | |
8346c47f |
20 | sub register { |
942c16b1 |
21 | my ($self, @pairs) = @_; |
8346c47f |
22 | my $sqla = $self->sqla; |
942c16b1 |
23 | while (my ($method, $cases) = splice(@pairs, 0, 2)) { |
24 | my @cases = @$cases; |
25 | while (my ($name, $case) = splice(@cases, 0, 2)) { |
26 | $sqla->$method($name, $self->cb($case)); |
27 | } |
8346c47f |
28 | } |
29 | return $self; |
30 | } |
31 | |
b40d30dc |
32 | sub apply_to { |
33 | my ($self, $sqla) = @_; |
34 | $self = $self->new unless ref($self); |
8346c47f |
35 | local $self->{sqla} = $sqla; |
36 | $self->register_extensions($sqla); |
37 | } |
38 | |
39 | sub register_extensions { |
40 | my ($self, $sqla) = @_; |
ad993fa3 |
41 | |
ac3616e8 |
42 | my @clauses = $sqla->clauses_of('select'); |
f7a20100 |
43 | my @before_setop; |
44 | CLAUSE: foreach my $idx (0..$#clauses) { |
45 | if ($clauses[$idx] eq 'order_by') { |
46 | @before_setop = @clauses[0..$idx-1]; |
47 | splice(@clauses, $idx, 0, qw(setop group_by having)); |
48 | last CLAUSE; |
49 | } |
50 | } |
ad993fa3 |
51 | |
f7a20100 |
52 | die "Huh?" unless @before_setop; |
d689c054 |
53 | $sqla->clauses_of(select => @clauses); |
71c1b4d5 |
54 | |
ac3616e8 |
55 | $sqla->clauses_of(update => sub { |
e6b86bee |
56 | my ($self, @clauses) = @_; |
57 | splice(@clauses, 2, 0, 'from'); |
58 | @clauses; |
59 | }); |
60 | |
ac3616e8 |
61 | $sqla->clauses_of(delete => sub { |
e6b86bee |
62 | my ($self, @clauses) = @_; |
63 | splice(@clauses, 1, 0, 'using'); |
64 | @clauses; |
65 | }); |
66 | |
d689c054 |
67 | $self->register( |
ad993fa3 |
68 | (map +( |
69 | "${_}er" => [ |
70 | do { |
71 | my $x = $_; |
72 | (map +($_ => "_${x}_${_}"), qw(join from_list alias)) |
73 | } |
74 | ] |
75 | ), qw(expand render)), |
76 | binop_expander => [ as => '_expand_op_as' ], |
77 | renderer => [ as => '_render_as' ], |
78 | expander => [ cast => '_expand_cast' ], |
d689c054 |
79 | clause_expanders => [ |
ad993fa3 |
80 | "select.from", '_expand_from_list', |
81 | 'select.group_by' |
82 | => sub { $_[0]->expand_maybe_list_expr($_[2], -ident) }, |
83 | 'select.having' |
84 | => sub { $_[0]->expand_expr($_[2]) }, |
d689c054 |
85 | 'update.from' => '_expand_from_list', |
ad993fa3 |
86 | "update.target", '_expand_update_clause_target', |
87 | "update.update", '_expand_update_clause_target', |
d689c054 |
88 | 'delete.using' => '_expand_from_list', |
89 | 'insert.rowvalues' => sub { |
90 | +(from => $_[0]->expand_expr({ -values => $_[2] })); |
91 | }, |
92 | 'insert.select' => sub { |
93 | +(from => $_[0]->expand_expr({ -select => $_[2] })); |
94 | }, |
95 | ], |
71c1b4d5 |
96 | ); |
37b399a8 |
97 | |
f7a20100 |
98 | # set ops |
ac3616e8 |
99 | $sqla->wrap_expander(select => sub { |
22f5ed9a |
100 | $self->cb('_expand_select', $_[0], \@before_setop); |
95ab9342 |
101 | }); |
f7a20100 |
102 | |
d689c054 |
103 | $self->register( |
104 | clause_renderer => [ |
105 | 'select.setop' => sub { $_[0]->render_aqt($_[2]) } |
106 | ], |
107 | expander => [ map +($_ => '_expand_setop'), qw(union intersect except) ], |
108 | renderer => [ map +($_ => '_render_setop'), qw(union intersect except) ], |
5824607d |
109 | ); |
f7a20100 |
110 | |
0f4de029 |
111 | my $setop_expander = $self->cb('_expand_clause_setop'); |
bb36c26d |
112 | |
ac3616e8 |
113 | $sqla->clause_expanders( |
bb36c26d |
114 | map +($_ => $setop_expander), |
115 | map "select.${_}", |
116 | map +($_, "${_}_all", "${_}_distinct"), |
117 | qw(union intersect except) |
118 | ); |
f7fd09f7 |
119 | |
d689c054 |
120 | foreach my $stmt (qw(select insert update delete)) { |
ac3616e8 |
121 | $sqla->clauses_of($stmt => 'with', $sqla->clauses_of($stmt)); |
d689c054 |
122 | $self->register( |
123 | clause_expanders => [ |
124 | "${stmt}.with" => '_expand_with', |
125 | "${stmt}.with_recursive" => '_expand_with', |
126 | ], |
127 | clause_renderer => [ "${stmt}.with" => '_render_with' ], |
128 | ); |
44a6affb |
129 | } |
6d42f0b9 |
130 | |
ac3616e8 |
131 | return $sqla; |
f8392f56 |
132 | } |
133 | |
22f5ed9a |
134 | sub _expand_select { |
b5c13e0a |
135 | my ($self, $orig, $before_setop, @args) = @_; |
136 | my $exp = $self->sqla->$orig(@args); |
22f5ed9a |
137 | return $exp unless my $setop = (my $sel = $exp->{-select})->{setop}; |
138 | if (my @keys = grep $sel->{$_}, @$before_setop) { |
139 | my %inner; @inner{@keys} = delete @{$sel}{@keys}; |
140 | unshift @{(values(%$setop))[0]{queries}}, |
141 | { -select => \%inner }; |
142 | } |
143 | return $exp; |
144 | } |
145 | |
f79455dc |
146 | sub _expand_from_list { |
147 | my ($self, undef, $args) = @_; |
148 | if (ref($args) eq 'HASH') { |
09ceda11 |
149 | return $args if $args->{-from_list}; |
f79455dc |
150 | return { -from_list => [ $self->expand_expr($args) ] }; |
151 | } |
152 | my @list; |
b9e35873 |
153 | my @args = ref($args) eq 'ARRAY' ? @$args : ($args); |
f79455dc |
154 | while (my $entry = shift @args) { |
6990b2aa |
155 | if (!ref($entry) and $entry =~ /^-(.*)/) { |
156 | if ($1 eq 'as') { |
157 | $list[-1] = $self->expand_expr({ -as => [ |
158 | $list[-1], map +(ref($_) eq 'ARRAY' ? @$_ : $_), shift(@args) |
159 | ]}); |
160 | next; |
161 | } |
f79455dc |
162 | $entry = { $entry => shift @args }; |
163 | } |
164 | my $aqt = $self->expand_expr($entry, -ident); |
165 | if ($aqt->{-join} and not $aqt->{-join}{from}) { |
166 | $aqt->{-join}{from} = pop @list; |
167 | } |
168 | push @list, $aqt; |
169 | } |
170 | return { -from_list => \@list }; |
171 | } |
172 | |
173 | sub _expand_join { |
174 | my ($self, undef, $args) = @_; |
175 | my %proto = ( |
176 | ref($args) eq 'HASH' |
177 | ? %$args |
178 | : (to => $args->[0], @{$args}[1..$#$args]) |
179 | ); |
6990b2aa |
180 | if (my $as = delete $proto{as}) { |
01e9b916 |
181 | $proto{to} = $self->expand_expr({ -as => [ $proto{to}, $as ] }); |
6990b2aa |
182 | } |
0891ae97 |
183 | if (defined($proto{using}) and ref(my $using = $proto{using}) ne 'HASH') { |
60757815 |
184 | $proto{using} = [ |
0891ae97 |
185 | map [ $self->expand_expr($_, -ident) ], |
186 | ref($using) eq 'ARRAY' ? @$using: $using |
60757815 |
187 | ]; |
0891ae97 |
188 | } |
f79455dc |
189 | my %ret = map +($_ => $self->expand_expr($proto{$_}, -ident)), |
190 | sort keys %proto; |
191 | return +{ -join => \%ret }; |
192 | } |
193 | |
194 | sub _render_from_list { |
6c39a2f7 |
195 | my ($self, undef, $list) = @_; |
3e230b71 |
196 | return $self->join_query_parts(', ', @$list); |
f79455dc |
197 | } |
198 | |
199 | sub _render_join { |
6c39a2f7 |
200 | my ($self, undef, $args) = @_; |
f79455dc |
201 | |
202 | my @parts = ( |
e48c4b9a |
203 | $args->{from}, |
312abf42 |
204 | { -keyword => join '_', ($args->{type}||()), 'join' }, |
cbe3b5a9 |
205 | (map +($_->{-ident} || $_->{-as} ? $_ : ('(', $_, ')')), $args->{to}), |
f79455dc |
206 | ($args->{on} ? ( |
312abf42 |
207 | { -keyword => 'on' }, |
e48c4b9a |
208 | $args->{on}, |
f79455dc |
209 | ) : ()), |
210 | ($args->{using} ? ( |
312abf42 |
211 | { -keyword => 'using' }, |
60757815 |
212 | '(', $args->{using}, ')', |
f79455dc |
213 | ) : ()), |
214 | ); |
59c7f80e |
215 | return $self->join_query_parts(' ', @parts); |
f79455dc |
216 | } |
217 | |
6990b2aa |
218 | sub _expand_op_as { |
219 | my ($self, undef, $vv, $k) = @_; |
01e9b916 |
220 | my @vv = (ref($vv) eq 'ARRAY' ? @$vv : $vv); |
01e9b916 |
221 | my $ik = $self->expand_expr($k, -ident); |
222 | return +{ -as => [ $ik, $self->expand_expr($vv[0], -alias) ] } |
223 | if @vv == 1 and ref($vv[0]) eq 'HASH'; |
224 | |
225 | my @as = map $self->expand_expr($_, -ident), @vv; |
226 | return { -as => [ $ik, { -alias => \@as } ] }; |
6990b2aa |
227 | } |
228 | |
229 | sub _render_as { |
6c39a2f7 |
230 | my ($self, undef, $args) = @_; |
01e9b916 |
231 | my ($thing, $alias) = @$args; |
59c7f80e |
232 | return $self->join_query_parts( |
8d1295c3 |
233 | ' ', |
01e9b916 |
234 | $thing, |
312abf42 |
235 | { -keyword => 'as' }, |
01e9b916 |
236 | $alias, |
369e7844 |
237 | ); |
238 | } |
239 | |
240 | sub _render_alias { |
01e9b916 |
241 | my ($self, undef, $args) = @_; |
369e7844 |
242 | my ($as, @cols) = @$args; |
243 | return (@cols |
59c7f80e |
244 | ? $self->join_query_parts('', |
3e230b71 |
245 | $as, |
68a92d22 |
246 | '(', |
247 | $self->join_query_parts( |
248 | ', ', |
249 | @cols |
250 | ), |
251 | ')', |
369e7844 |
252 | ) |
253 | : $self->render_aqt($as) |
6990b2aa |
254 | ); |
255 | } |
256 | |
af407e9a |
257 | sub _expand_update_clause_target { |
1107714b |
258 | my ($self, undef, $target) = @_; |
af407e9a |
259 | +(target => $self->_expand_from_list(undef, $target)); |
260 | } |
261 | |
6d42f0b9 |
262 | sub _expand_cast { |
263 | my ($self, undef, $thing) = @_; |
264 | return { -func => [ cast => $thing ] } if ref($thing) eq 'HASH'; |
265 | my ($cast, $to) = @{$thing}; |
266 | +{ -func => [ cast => { -as => [ |
267 | $self->expand_expr($cast), |
268 | $self->expand_expr($to, -ident), |
269 | ] } ] }; |
270 | } |
271 | |
272 | sub _expand_alias { |
273 | my ($self, undef, $args) = @_; |
274 | if (ref($args) eq 'HASH' and my $alias = $args->{-alias}) { |
275 | $args = $alias; |
276 | } |
277 | +{ -alias => [ |
278 | map $self->expand_expr($_, -ident), |
279 | ref($args) eq 'ARRAY' ? @{$args} : $args |
280 | ] |
281 | } |
282 | } |
283 | |
284 | sub _expand_with { |
285 | my ($self, $name, $with) = @_; |
286 | my (undef, $type) = split '_', $name; |
287 | if (ref($with) eq 'HASH') { |
288 | return +{ |
289 | %$with, |
290 | queries => [ |
291 | map +[ |
292 | $self->expand_expr({ -alias => $_->[0] }, -ident), |
293 | $self->expand_expr($_->[1]), |
294 | ], @{$with->{queries}} |
295 | ] |
296 | } |
297 | } |
298 | my @with = @$with; |
299 | my @exp; |
300 | while (my ($alias, $query) = splice @with, 0, 2) { |
301 | push @exp, [ |
302 | $self->expand_expr({ -alias => $alias }, -ident), |
303 | $self->expand_expr($query) |
304 | ]; |
305 | } |
306 | return +(with => { ($type ? (type => $type) : ()), queries => \@exp }); |
307 | } |
308 | |
309 | sub _render_with { |
310 | my ($self, undef, $with) = @_; |
311 | my $q_part = $self->join_query_parts(', ', |
312 | map { |
313 | my ($alias, $query) = @$_; |
314 | $self->join_query_parts(' ', |
315 | $alias, |
312abf42 |
316 | { -keyword => 'as' }, |
6d42f0b9 |
317 | $query, |
318 | ) |
319 | } @{$with->{queries}} |
320 | ); |
321 | return $self->join_query_parts(' ', |
312abf42 |
322 | { -keyword => join '_', 'with', ($with->{type}||'') }, |
6d42f0b9 |
323 | $q_part, |
324 | ); |
325 | } |
326 | |
4b5f7259 |
327 | sub _expand_setop { |
328 | my ($self, $setop, $args) = @_; |
329 | +{ "-${setop}" => { |
330 | %$args, |
331 | queries => [ map $self->expand_expr($_), @{$args->{queries}} ], |
332 | } }; |
333 | } |
334 | |
acfbc601 |
335 | sub _render_setop { |
336 | my ($self, $setop, $args) = @_; |
337 | $self->join_query_parts( |
312abf42 |
338 | { -keyword => ' '.join('_', $setop, ($args->{type}||())).' ' }, |
acfbc601 |
339 | @{$args->{queries}} |
340 | ); |
341 | } |
342 | |
0f4de029 |
343 | sub _expand_clause_setop { |
344 | my ($self, $setop, $args) = @_; |
345 | my ($op, $type) = split '_', $setop; |
346 | +(setop => $self->expand_expr({ |
347 | "-${op}" => { |
348 | ($type ? (type => $type) : ()), |
349 | queries => (ref($args) eq 'ARRAY' ? $args : [ $args ]) |
350 | } |
351 | })); |
352 | } |
353 | |
f8392f56 |
354 | 1; |
99fe0bf3 |
355 | |
eba4f083 |
356 | __END__ |
357 | |
99fe0bf3 |
358 | =head1 NAME |
359 | |
360 | SQL::Abstract::ExtraClauses - new/experimental additions to L<SQL::Abstract> |
361 | |
362 | =head1 SYNOPSIS |
363 | |
364 | my $sqla = SQL::Abstract->new; |
365 | SQL::Abstract::ExtraClauses->apply_to($sqla); |
366 | |
367 | =head1 METHODS |
368 | |
369 | =head2 apply_to |
370 | |
371 | Applies the plugin to an L<SQL::Abstract> object. |
372 | |
ad993fa3 |
373 | =head2 register_extensions |
374 | |
375 | Registers the extensions described below |
376 | |
99fe0bf3 |
377 | =head2 cb |
378 | |
379 | For plugin authors, creates a callback to call a method on the plugin. |
380 | |
ad993fa3 |
381 | =head2 register |
382 | |
383 | For plugin authors, registers callbacks more easily. |
384 | |
99fe0bf3 |
385 | =head2 sqla |
386 | |
387 | Available only during plugin callback executions, contains the currently |
388 | active L<SQL::Abstract> object. |
389 | |
eba4f083 |
390 | =head1 NODE TYPES |
391 | |
392 | =head2 alias |
393 | |
394 | Represents a table alias. Expands name and column names with ident as default. |
395 | |
396 | # expr |
397 | { -alias => [ 't', 'x', 'y', 'z' ] } |
398 | |
399 | # aqt |
400 | { -alias => [ |
401 | { -ident => [ 't' ] }, { -ident => [ 'x' ] }, |
402 | { -ident => [ 'y' ] }, { -ident => [ 'z' ] }, |
403 | ] } |
404 | |
405 | # query |
406 | t(x, y, z) |
407 | [] |
408 | |
409 | =head2 as |
410 | |
411 | Represents an sql AS. LHS is expanded with ident as default, RHS is treated |
412 | as a list of arguments for the alias node. |
413 | |
414 | # expr |
415 | { foo => { -as => 'bar' } } |
416 | |
417 | # aqt |
418 | { -as => |
419 | [ |
420 | { -ident => [ 'foo' ] }, |
421 | { -alias => [ { -ident => [ 'bar' ] } ] }, |
422 | ] |
423 | } |
424 | |
425 | # query |
426 | foo AS bar |
427 | [] |
428 | |
429 | # expr |
430 | { -as => [ { -select => { _ => 'blah' } }, 't', 'blah' ] } |
431 | |
432 | # aqt |
433 | { -as => [ |
434 | { -select => |
435 | { select => { -op => [ ',', { -ident => [ 'blah' ] } ] } } |
436 | }, |
437 | { -alias => [ { -ident => [ 't' ] }, { -ident => [ 'blah' ] } ] }, |
438 | ] } |
439 | |
440 | # query |
441 | (SELECT blah) AS t(blah) |
442 | [] |
443 | |
444 | =head2 cast |
445 | |
446 | # expr |
447 | { -cast => [ { -ident => 'birthday' }, 'date' ] } |
448 | |
449 | # aqt |
450 | { -func => [ |
451 | 'cast', { |
452 | -as => [ { -ident => [ 'birthday' ] }, { -ident => [ 'date' ] } ] |
453 | }, |
454 | ] } |
455 | |
456 | # query |
457 | CAST(birthday AS date) |
458 | [] |
459 | |
99fe0bf3 |
460 | =cut |