Commit | Line | Data |
1ff9018c |
1 | package SQL::Abstract::ExtraClauses; |
2 | |
df5d0507 |
3 | use Moo; |
4 | |
5 | has sqla => ( |
6 | is => 'ro', init_arg => undef, |
7 | handles => [ qw( |
1601bb47 |
8 | expand_expr expand_maybe_list_expr render_aqt join_query_parts |
df5d0507 |
9 | ) ], |
10 | ); |
1ff9018c |
11 | |
df5d0507 |
12 | sub cb { |
c671eba6 |
13 | my ($self, $method, @args) = @_; |
14 | return sub { |
15 | local $self->{sqla} = shift; |
16 | $self->$method(@args, @_) |
17 | }; |
df5d0507 |
18 | } |
19 | |
ec49a2e1 |
20 | sub register { |
d37f2f17 |
21 | my ($self, @pairs) = @_; |
ec49a2e1 |
22 | my $sqla = $self->sqla; |
d37f2f17 |
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 | } |
ec49a2e1 |
28 | } |
29 | return $self; |
30 | } |
31 | |
df5d0507 |
32 | sub apply_to { |
33 | my ($self, $sqla) = @_; |
34 | $self = $self->new unless ref($self); |
ec49a2e1 |
35 | local $self->{sqla} = $sqla; |
36 | $self->register_extensions($sqla); |
37 | } |
38 | |
39 | sub register_extensions { |
40 | my ($self, $sqla) = @_; |
f1c52c0c |
41 | |
ad078c71 |
42 | my @clauses = $sqla->clauses_of('select'); |
2b0b3d43 |
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 | } |
f1c52c0c |
51 | |
2b0b3d43 |
52 | die "Huh?" unless @before_setop; |
4be5ded3 |
53 | $sqla->clauses_of(select => @clauses); |
58ebc7fa |
54 | |
ad078c71 |
55 | $sqla->clauses_of(update => sub { |
ee706e89 |
56 | my ($self, @clauses) = @_; |
57 | splice(@clauses, 2, 0, 'from'); |
58 | @clauses; |
59 | }); |
60 | |
ad078c71 |
61 | $sqla->clauses_of(delete => sub { |
ee706e89 |
62 | my ($self, @clauses) = @_; |
63 | splice(@clauses, 1, 0, 'using'); |
64 | @clauses; |
65 | }); |
66 | |
4be5ded3 |
67 | $self->register( |
f1c52c0c |
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' ], |
4be5ded3 |
79 | clause_expanders => [ |
f1c52c0c |
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]) }, |
4be5ded3 |
85 | 'update.from' => '_expand_from_list', |
f1c52c0c |
86 | "update.target", '_expand_update_clause_target', |
87 | "update.update", '_expand_update_clause_target', |
4be5ded3 |
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 | ], |
58ebc7fa |
96 | ); |
26994fdd |
97 | |
2b0b3d43 |
98 | # set ops |
ad078c71 |
99 | $sqla->wrap_expander(select => sub { |
c671eba6 |
100 | $self->cb('_expand_select', $_[0], \@before_setop); |
3f9899e5 |
101 | }); |
2b0b3d43 |
102 | |
4be5ded3 |
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) ], |
e2db8228 |
109 | ); |
2b0b3d43 |
110 | |
41086177 |
111 | my $setop_expander = $self->cb('_expand_clause_setop'); |
f61bfd7b |
112 | |
ad078c71 |
113 | $sqla->clause_expanders( |
f61bfd7b |
114 | map +($_ => $setop_expander), |
115 | map "select.${_}", |
116 | map +($_, "${_}_all", "${_}_distinct"), |
117 | qw(union intersect except) |
118 | ); |
d175037f |
119 | |
4be5ded3 |
120 | foreach my $stmt (qw(select insert update delete)) { |
ad078c71 |
121 | $sqla->clauses_of($stmt => 'with', $sqla->clauses_of($stmt)); |
4be5ded3 |
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 | ); |
a97ecd95 |
129 | } |
2f4717ad |
130 | |
ad078c71 |
131 | return $sqla; |
1ff9018c |
132 | } |
133 | |
c671eba6 |
134 | sub _expand_select { |
093442c8 |
135 | my ($self, $orig, $before_setop, @args) = @_; |
136 | my $exp = $self->sqla->$orig(@args); |
c671eba6 |
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 | |
7741b7ad |
146 | sub _expand_from_list { |
147 | my ($self, undef, $args) = @_; |
148 | if (ref($args) eq 'HASH') { |
38e67490 |
149 | return $args if $args->{-from_list}; |
7741b7ad |
150 | return { -from_list => [ $self->expand_expr($args) ] }; |
151 | } |
152 | my @list; |
86a6ebf4 |
153 | my @args = ref($args) eq 'ARRAY' ? @$args : ($args); |
7741b7ad |
154 | while (my $entry = shift @args) { |
b99e9a14 |
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 | } |
7741b7ad |
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 |
d36cb439 |
178 | : (to => @$args) |
7741b7ad |
179 | ); |
b99e9a14 |
180 | if (my $as = delete $proto{as}) { |
984db0d7 |
181 | $proto{to} = $self->expand_expr({ -as => [ $proto{to}, $as ] }); |
b99e9a14 |
182 | } |
e0eb8d26 |
183 | if (defined($proto{using}) and ref(my $using = $proto{using}) ne 'HASH') { |
13c99dad |
184 | $proto{using} = [ |
e0eb8d26 |
185 | map [ $self->expand_expr($_, -ident) ], |
186 | ref($using) eq 'ARRAY' ? @$using: $using |
13c99dad |
187 | ]; |
e0eb8d26 |
188 | } |
7741b7ad |
189 | my %ret = map +($_ => $self->expand_expr($proto{$_}, -ident)), |
190 | sort keys %proto; |
d36cb439 |
191 | $ret{type} = $proto{type}; |
7741b7ad |
192 | return +{ -join => \%ret }; |
193 | } |
194 | |
195 | sub _render_from_list { |
a01911a2 |
196 | my ($self, undef, $list) = @_; |
a1f8b6ef |
197 | return $self->join_query_parts(', ', @$list); |
7741b7ad |
198 | } |
199 | |
200 | sub _render_join { |
a01911a2 |
201 | my ($self, undef, $args) = @_; |
7741b7ad |
202 | |
203 | my @parts = ( |
412f9efe |
204 | $args->{from}, |
1601bb47 |
205 | { -keyword => join '_', ($args->{type}||()), 'join' }, |
51ccda04 |
206 | (map +($_->{-ident} || $_->{-as} ? $_ : ('(', $_, ')')), $args->{to}), |
7741b7ad |
207 | ($args->{on} ? ( |
1601bb47 |
208 | { -keyword => 'on' }, |
412f9efe |
209 | $args->{on}, |
7741b7ad |
210 | ) : ()), |
211 | ($args->{using} ? ( |
1601bb47 |
212 | { -keyword => 'using' }, |
13c99dad |
213 | '(', $args->{using}, ')', |
7741b7ad |
214 | ) : ()), |
215 | ); |
0236f122 |
216 | return $self->join_query_parts(' ', @parts); |
7741b7ad |
217 | } |
218 | |
b99e9a14 |
219 | sub _expand_op_as { |
220 | my ($self, undef, $vv, $k) = @_; |
984db0d7 |
221 | my @vv = (ref($vv) eq 'ARRAY' ? @$vv : $vv); |
984db0d7 |
222 | my $ik = $self->expand_expr($k, -ident); |
223 | return +{ -as => [ $ik, $self->expand_expr($vv[0], -alias) ] } |
224 | if @vv == 1 and ref($vv[0]) eq 'HASH'; |
225 | |
226 | my @as = map $self->expand_expr($_, -ident), @vv; |
227 | return { -as => [ $ik, { -alias => \@as } ] }; |
b99e9a14 |
228 | } |
229 | |
230 | sub _render_as { |
a01911a2 |
231 | my ($self, undef, $args) = @_; |
984db0d7 |
232 | my ($thing, $alias) = @$args; |
0236f122 |
233 | return $self->join_query_parts( |
ac7992be |
234 | ' ', |
984db0d7 |
235 | $thing, |
1601bb47 |
236 | { -keyword => 'as' }, |
984db0d7 |
237 | $alias, |
1ba47f38 |
238 | ); |
239 | } |
240 | |
241 | sub _render_alias { |
984db0d7 |
242 | my ($self, undef, $args) = @_; |
1ba47f38 |
243 | my ($as, @cols) = @$args; |
244 | return (@cols |
0236f122 |
245 | ? $self->join_query_parts('', |
a1f8b6ef |
246 | $as, |
3f312d2e |
247 | '(', |
248 | $self->join_query_parts( |
249 | ', ', |
250 | @cols |
251 | ), |
252 | ')', |
1ba47f38 |
253 | ) |
254 | : $self->render_aqt($as) |
b99e9a14 |
255 | ); |
256 | } |
257 | |
f9f1fdcd |
258 | sub _expand_update_clause_target { |
fe8b493f |
259 | my ($self, undef, $target) = @_; |
f9f1fdcd |
260 | +(target => $self->_expand_from_list(undef, $target)); |
261 | } |
262 | |
2f4717ad |
263 | sub _expand_cast { |
264 | my ($self, undef, $thing) = @_; |
265 | return { -func => [ cast => $thing ] } if ref($thing) eq 'HASH'; |
266 | my ($cast, $to) = @{$thing}; |
267 | +{ -func => [ cast => { -as => [ |
268 | $self->expand_expr($cast), |
269 | $self->expand_expr($to, -ident), |
270 | ] } ] }; |
271 | } |
272 | |
273 | sub _expand_alias { |
274 | my ($self, undef, $args) = @_; |
275 | if (ref($args) eq 'HASH' and my $alias = $args->{-alias}) { |
276 | $args = $alias; |
277 | } |
278 | +{ -alias => [ |
279 | map $self->expand_expr($_, -ident), |
280 | ref($args) eq 'ARRAY' ? @{$args} : $args |
281 | ] |
282 | } |
283 | } |
284 | |
285 | sub _expand_with { |
286 | my ($self, $name, $with) = @_; |
287 | my (undef, $type) = split '_', $name; |
288 | if (ref($with) eq 'HASH') { |
289 | return +{ |
290 | %$with, |
291 | queries => [ |
292 | map +[ |
293 | $self->expand_expr({ -alias => $_->[0] }, -ident), |
294 | $self->expand_expr($_->[1]), |
295 | ], @{$with->{queries}} |
296 | ] |
297 | } |
298 | } |
299 | my @with = @$with; |
300 | my @exp; |
301 | while (my ($alias, $query) = splice @with, 0, 2) { |
302 | push @exp, [ |
303 | $self->expand_expr({ -alias => $alias }, -ident), |
304 | $self->expand_expr($query) |
305 | ]; |
306 | } |
307 | return +(with => { ($type ? (type => $type) : ()), queries => \@exp }); |
308 | } |
309 | |
310 | sub _render_with { |
311 | my ($self, undef, $with) = @_; |
312 | my $q_part = $self->join_query_parts(', ', |
313 | map { |
314 | my ($alias, $query) = @$_; |
315 | $self->join_query_parts(' ', |
316 | $alias, |
1601bb47 |
317 | { -keyword => 'as' }, |
2f4717ad |
318 | $query, |
319 | ) |
320 | } @{$with->{queries}} |
321 | ); |
322 | return $self->join_query_parts(' ', |
1601bb47 |
323 | { -keyword => join '_', 'with', ($with->{type}||'') }, |
2f4717ad |
324 | $q_part, |
325 | ); |
326 | } |
327 | |
51046d0e |
328 | sub _expand_setop { |
329 | my ($self, $setop, $args) = @_; |
330 | +{ "-${setop}" => { |
331 | %$args, |
332 | queries => [ map $self->expand_expr($_), @{$args->{queries}} ], |
333 | } }; |
334 | } |
335 | |
b5f4a869 |
336 | sub _render_setop { |
337 | my ($self, $setop, $args) = @_; |
338 | $self->join_query_parts( |
1601bb47 |
339 | { -keyword => ' '.join('_', $setop, ($args->{type}||())).' ' }, |
b5f4a869 |
340 | @{$args->{queries}} |
341 | ); |
342 | } |
343 | |
41086177 |
344 | sub _expand_clause_setop { |
345 | my ($self, $setop, $args) = @_; |
346 | my ($op, $type) = split '_', $setop; |
347 | +(setop => $self->expand_expr({ |
348 | "-${op}" => { |
349 | ($type ? (type => $type) : ()), |
350 | queries => (ref($args) eq 'ARRAY' ? $args : [ $args ]) |
351 | } |
352 | })); |
353 | } |
354 | |
1ff9018c |
355 | 1; |
3001f097 |
356 | |
583c7957 |
357 | __END__ |
358 | |
3001f097 |
359 | =head1 NAME |
360 | |
361 | SQL::Abstract::ExtraClauses - new/experimental additions to L<SQL::Abstract> |
362 | |
363 | =head1 SYNOPSIS |
364 | |
365 | my $sqla = SQL::Abstract->new; |
366 | SQL::Abstract::ExtraClauses->apply_to($sqla); |
367 | |
368 | =head1 METHODS |
369 | |
370 | =head2 apply_to |
371 | |
372 | Applies the plugin to an L<SQL::Abstract> object. |
373 | |
f1c52c0c |
374 | =head2 register_extensions |
375 | |
376 | Registers the extensions described below |
377 | |
3001f097 |
378 | =head2 cb |
379 | |
380 | For plugin authors, creates a callback to call a method on the plugin. |
381 | |
f1c52c0c |
382 | =head2 register |
383 | |
384 | For plugin authors, registers callbacks more easily. |
385 | |
3001f097 |
386 | =head2 sqla |
387 | |
388 | Available only during plugin callback executions, contains the currently |
389 | active L<SQL::Abstract> object. |
390 | |
583c7957 |
391 | =head1 NODE TYPES |
392 | |
393 | =head2 alias |
394 | |
395 | Represents a table alias. Expands name and column names with ident as default. |
396 | |
397 | # expr |
398 | { -alias => [ 't', 'x', 'y', 'z' ] } |
399 | |
400 | # aqt |
401 | { -alias => [ |
402 | { -ident => [ 't' ] }, { -ident => [ 'x' ] }, |
403 | { -ident => [ 'y' ] }, { -ident => [ 'z' ] }, |
404 | ] } |
405 | |
406 | # query |
407 | t(x, y, z) |
408 | [] |
409 | |
410 | =head2 as |
411 | |
412 | Represents an sql AS. LHS is expanded with ident as default, RHS is treated |
413 | as a list of arguments for the alias node. |
414 | |
415 | # expr |
416 | { foo => { -as => 'bar' } } |
417 | |
418 | # aqt |
419 | { -as => |
420 | [ |
421 | { -ident => [ 'foo' ] }, |
422 | { -alias => [ { -ident => [ 'bar' ] } ] }, |
423 | ] |
424 | } |
425 | |
426 | # query |
427 | foo AS bar |
428 | [] |
429 | |
430 | # expr |
431 | { -as => [ { -select => { _ => 'blah' } }, 't', 'blah' ] } |
432 | |
433 | # aqt |
434 | { -as => [ |
435 | { -select => |
436 | { select => { -op => [ ',', { -ident => [ 'blah' ] } ] } } |
437 | }, |
438 | { -alias => [ { -ident => [ 't' ] }, { -ident => [ 'blah' ] } ] }, |
439 | ] } |
440 | |
441 | # query |
442 | (SELECT blah) AS t(blah) |
443 | [] |
444 | |
445 | =head2 cast |
446 | |
447 | # expr |
448 | { -cast => [ { -ident => 'birthday' }, 'date' ] } |
449 | |
450 | # aqt |
451 | { -func => [ |
452 | 'cast', { |
453 | -as => [ { -ident => [ 'birthday' ] }, { -ident => [ 'date' ] } ] |
454 | }, |
455 | ] } |
456 | |
457 | # query |
458 | CAST(birthday AS date) |
459 | [] |
460 | |
d36cb439 |
461 | =head2 join |
462 | |
463 | If given an arrayref, pretends it was given a hashref with the first |
464 | element of the arrayref as the value for 'to' and the remaining pairs copied. |
465 | |
466 | Given a hashref, the 'as' key is if presented expanded to wrap the 'to'. |
467 | |
468 | If present the 'using' key is expanded as a list of idents. |
469 | |
470 | Known keys are: 'from' (the left hand side), 'type' ('left', 'right', or |
471 | nothing), 'to' (the right hand side), 'on' and 'using'. |
472 | |
473 | # expr |
474 | { -join => { |
475 | from => 'lft', |
476 | on => { 'lft.bloo' => { '>' => 'rgt.blee' } }, |
477 | to => 'rgt', |
478 | type => 'left', |
479 | } } |
480 | |
481 | # aqt |
482 | { -join => { |
483 | from => { -ident => [ 'lft' ] }, |
484 | on => { -op => [ |
485 | '>', { -ident => [ 'lft', 'bloo' ] }, |
486 | { -ident => [ 'rgt', 'blee' ] }, |
487 | ] }, |
488 | to => { -ident => [ 'rgt' ] }, |
489 | type => 'left', |
490 | } } |
491 | |
492 | # query |
493 | lft LEFT JOIN rgt ON lft.bloo > rgt.blee |
494 | [] |
495 | |
496 | =head2 from_list |
497 | |
498 | List of components of the FROM clause; -foo type elements indicate a pair |
499 | with the next element; this is easiest if I show you: |
500 | |
501 | # expr |
502 | { -from_list => [ |
503 | 't1', -as => 'table_one', -join => |
504 | [ 't2', 'on', { 'table_one.x' => 't2.x' } ], |
505 | ] } |
506 | |
507 | # aqt |
508 | { -from_list => [ { -join => { |
509 | from => { -as => [ |
510 | { -ident => [ 't1' ] }, |
511 | { -alias => [ { -ident => [ 'table_one' ] } ] }, |
512 | ] }, |
513 | on => { -op => [ |
514 | '=', { -ident => [ 'table_one', 'x' ] }, |
515 | { -ident => [ 't2', 'x' ] }, |
516 | ] }, |
517 | to => { -ident => [ 't2' ] }, |
518 | type => undef, |
519 | } } ] } |
520 | |
521 | # query |
522 | t1 AS table_one JOIN t2 ON table_one.x = t2.x |
523 | [] |
524 | |
525 | Or with using: |
526 | |
527 | # expr |
528 | { -from_list => |
529 | [ 't1', -as => 'table_one', -join => [ 't2', 'using', [ 'x' ] ] ] |
530 | } |
531 | |
532 | # aqt |
533 | { -from_list => [ { -join => { |
534 | from => { -as => [ |
535 | { -ident => [ 't1' ] }, |
536 | { -alias => [ { -ident => [ 'table_one' ] } ] }, |
537 | ] }, |
538 | to => { -ident => [ 't2' ] }, |
539 | type => undef, |
540 | using => |
541 | { |
542 | -op => [ 'or', { -op => [ 'or', { -ident => [ 'x' ] } ] } ] |
543 | }, |
544 | } } ] } |
545 | |
546 | # query |
547 | t1 AS table_one JOIN t2 USING ( x ) |
548 | [] |
549 | |
550 | With oddities: |
551 | |
552 | # expr |
553 | { -from_list => [ |
554 | 'x', -join => [ |
555 | { -join => { from => 'y', to => 'z', type => 'left' } }, 'type', |
556 | 'left', |
557 | ], |
558 | ] } |
559 | |
560 | # aqt |
561 | { -from_list => [ { -join => { |
562 | from => { -ident => [ 'x' ] }, |
563 | to => { -join => { |
564 | from => { -ident => [ 'y' ] }, |
565 | to => { -ident => [ 'z' ] }, |
566 | type => 'left', |
567 | } }, |
568 | type => 'left', |
569 | } } ] } |
570 | |
571 | # query |
572 | x LEFT JOIN ( y LEFT JOIN z ) |
573 | [] |
574 | |
3001f097 |
575 | =cut |