1 package SQL::Abstract::ExtraClauses;
6 is => 'ro', init_arg => undef,
8 expand_expr render_aqt join_query_parts
13 my ($self, $method, @args) = @_;
15 local $self->{sqla} = shift;
16 $self->$method(@args, @_)
21 my ($self, @pairs) = @_;
22 my $sqla = $self->sqla;
23 while (my ($method, $cases) = splice(@pairs, 0, 2)) {
25 while (my ($name, $case) = splice(@cases, 0, 2)) {
26 $sqla->$method($name, $self->cb($case));
33 my ($self, $sqla) = @_;
34 $self = $self->new unless ref($self);
35 local $self->{sqla} = $sqla;
36 $self->register_extensions($sqla);
39 sub register_extensions {
40 my ($self, $sqla) = @_;
42 my @clauses = $sqla->clauses_of('select');
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));
52 die "Huh?" unless @before_setop;
53 $sqla->clauses_of(select => @clauses);
55 $sqla->clauses_of(update => sub {
56 my ($self, @clauses) = @_;
57 splice(@clauses, 2, 0, 'from');
61 $sqla->clauses_of(delete => sub {
62 my ($self, @clauses) = @_;
63 splice(@clauses, 1, 0, 'using');
72 (map +($_ => "_${x}_${_}"), qw(join from_list alias))
75 ), qw(expand render)),
76 binop_expander => [ as => '_expand_op_as' ],
77 renderer => [ as => '_render_as' ],
78 expander => [ cast => '_expand_cast' ],
80 "select.from", '_expand_from_list',
82 => sub { $_[0]->expand_expr({ -list => $_[2] }, -ident) },
84 => sub { $_[0]->expand_expr($_[2]) },
85 'update.from' => '_expand_from_list',
86 "update.target", '_expand_update_clause_target',
87 "update.update", '_expand_update_clause_target',
88 'delete.using' => '_expand_from_list',
89 'insert.rowvalues' => sub {
90 +(from => $_[0]->expand_expr({ -values => $_[2] }));
92 'insert.select' => sub {
93 +(from => $_[0]->expand_expr({ -select => $_[2] }));
99 $sqla->wrap_expander(select => sub {
100 $self->cb('_expand_select', $_[0], \@before_setop);
105 'select.setop' => sub { $_[0]->render_aqt($_[2]) }
107 expander => [ map +($_ => '_expand_setop'), qw(union intersect except) ],
108 renderer => [ map +($_ => '_render_setop'), qw(union intersect except) ],
111 my $setop_expander = $self->cb('_expand_clause_setop');
113 $sqla->clause_expanders(
114 map +($_ => $setop_expander),
116 map +($_, "${_}_all", "${_}_distinct"),
117 qw(union intersect except)
120 foreach my $stmt (qw(select insert update delete)) {
121 $sqla->clauses_of($stmt => 'with', $sqla->clauses_of($stmt));
123 clause_expanders => [
124 "${stmt}.with" => '_expand_with',
125 "${stmt}.with_recursive" => '_expand_with',
127 clause_renderer => [ "${stmt}.with" => '_render_with' ],
135 my ($self, $orig, $before_setop, @args) = @_;
136 my $exp = $self->sqla->$orig(@args);
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 };
146 sub _expand_from_list {
147 my ($self, undef, $args) = @_;
148 if (ref($args) eq 'HASH') {
149 return $args if $args->{-from_list};
150 return { -from_list => [ $self->expand_expr($args) ] };
153 my @args = ref($args) eq 'ARRAY' ? @$args : ($args);
154 while (my $entry = shift @args) {
155 if (!ref($entry) and $entry =~ /^-(.*)/) {
157 $list[-1] = $self->expand_expr({ -as => [
158 $list[-1], map +(ref($_) eq 'ARRAY' ? @$_ : $_), shift(@args)
162 $entry = { $entry => shift @args };
164 my $aqt = $self->expand_expr($entry, -ident);
165 if ($aqt->{-join} and not $aqt->{-join}{from}) {
166 $aqt->{-join}{from} = pop @list;
170 return $list[0] if @list == 1;
171 return { -from_list => \@list };
175 my ($self, undef, $args) = @_;
181 if (my $as = delete $proto{as}) {
182 $proto{to} = $self->expand_expr(
183 { -as => [ { -from_list => $proto{to} }, $as ] }
186 if (defined($proto{using}) and ref(my $using = $proto{using}) ne 'HASH') {
188 map [ $self->expand_expr($_, -ident) ],
189 ref($using) eq 'ARRAY' ? @$using: $using
193 type => delete $proto{type},
194 to => $self->expand_expr({ -from_list => delete $proto{to} }, -ident)
197 map +($_ => $self->expand_expr($proto{$_}, -ident)),
200 return +{ -join => \%ret };
203 sub _render_from_list {
204 my ($self, undef, $list) = @_;
205 return $self->join_query_parts(', ', @$list);
209 my ($self, undef, $args) = @_;
213 { -keyword => join '_', ($args->{type}||()), 'join' },
214 (map +($_->{-ident} || $_->{-as}
216 : ('(', $self->render_aqt($_, 1), ')')),
217 map +(@{$_->{-from_list}||[]} == 1 ? $_->{-from_list}[0] : $_),
221 { -keyword => 'on' },
225 { -keyword => 'using' },
226 '(', $args->{using}, ')',
229 return $self->join_query_parts(' ', @parts);
233 my ($self, undef, $vv, $k) = @_;
234 my @vv = (ref($vv) eq 'ARRAY' ? @$vv : $vv);
235 my $ik = $self->expand_expr($k, -ident);
236 return +{ -as => [ $ik, $self->expand_expr($vv[0], -ident) ] }
237 if @vv == 1 and ref($vv[0]) eq 'HASH';
239 my @as = map $self->expand_expr($_, -ident), @vv;
240 return { -as => [ $ik, $self->expand_expr({ -alias => \@as }) ] };
244 my ($self, undef, $args) = @_;
245 my ($thing, $alias) = @$args;
246 return $self->join_query_parts(
249 { -keyword => 'as' },
255 my ($self, undef, $args) = @_;
256 my ($as, @cols) = @$args;
258 ? $self->join_query_parts('',
261 $self->join_query_parts(
267 : $self->render_aqt($as)
271 sub _expand_update_clause_target {
272 my ($self, undef, $target) = @_;
273 +(target => $self->_expand_from_list(undef, $target));
277 my ($self, undef, $thing) = @_;
278 return { -func => [ cast => $thing ] } if ref($thing) eq 'HASH';
279 my ($cast, $to) = @{$thing};
280 +{ -func => [ cast => { -as => [
281 $self->expand_expr($cast),
282 $self->expand_expr($to, -ident),
287 my ($self, undef, $args) = @_;
288 if (ref($args) eq 'HASH' and my $alias = $args->{-alias}) {
291 my @parts = map $self->expand_expr($_, -ident),
292 ref($args) eq 'ARRAY' ? @{$args} : $args;
293 return $parts[0] if @parts == 1;
294 return { -alias => \@parts };
298 my ($self, $name, $with) = @_;
299 my (undef, $type) = split '_', $name;
300 if (ref($with) eq 'HASH') {
305 $self->expand_expr({ -alias => $_->[0] }, -ident),
306 $self->expand_expr($_->[1]),
307 ], @{$with->{queries}}
313 while (my ($alias, $query) = splice @with, 0, 2) {
315 $self->expand_expr({ -alias => $alias }, -ident),
316 $self->expand_expr($query)
319 return +(with => { ($type ? (type => $type) : ()), queries => \@exp });
323 my ($self, undef, $with) = @_;
324 my $q_part = $self->join_query_parts(', ',
326 my ($alias, $query) = @$_;
327 $self->join_query_parts(' ',
329 { -keyword => 'as' },
332 } @{$with->{queries}}
334 return $self->join_query_parts(' ',
335 { -keyword => join '_', 'with', ($with->{type}||'') },
341 my ($self, $setop, $args) = @_;
344 queries => [ map $self->expand_expr($_), @{$args->{queries}} ],
349 my ($self, $setop, $args) = @_;
350 $self->join_query_parts(
351 { -keyword => ' '.join('_', $setop, ($args->{type}||())).' ' },
356 sub _expand_clause_setop {
357 my ($self, $setop, $args) = @_;
358 my ($op, $type) = split '_', $setop;
359 +(setop => $self->expand_expr({
361 ($type ? (type => $type) : ()),
362 queries => (ref($args) eq 'ARRAY' ? $args : [ $args ])
373 SQL::Abstract::ExtraClauses - new/experimental additions to L<SQL::Abstract>
377 my $sqla = SQL::Abstract->new;
378 SQL::Abstract::ExtraClauses->apply_to($sqla);
382 This module is basically a nursery for things that seem like a good idea
383 to live in until we figure out if we were right about that.
389 Applies the plugin to an L<SQL::Abstract> object.
391 =head2 register_extensions
393 Registers the extensions described below
397 For plugin authors, creates a callback to call a method on the plugin.
401 For plugin authors, registers callbacks more easily.
405 Available only during plugin callback executions, contains the currently
406 active L<SQL::Abstract> object.
412 Represents a table alias. Expands name and column names with ident as default.
415 { -alias => [ 't', 'x', 'y', 'z' ] }
419 { -ident => [ 't' ] }, { -ident => [ 'x' ] },
420 { -ident => [ 'y' ] }, { -ident => [ 'z' ] },
429 Represents an sql AS. LHS is expanded with ident as default, RHS is treated
430 as a list of arguments for the alias node.
433 { foo => { -as => 'bar' } }
436 { -as => [ { -ident => [ 'foo' ] }, { -ident => [ 'bar' ] } ] }
443 { -as => [ { -select => { _ => 'blah' } }, 't', 'blah' ] }
448 { select => { -op => [ ',', { -ident => [ 'blah' ] } ] } }
450 { -alias => [ { -ident => [ 't' ] }, { -ident => [ 'blah' ] } ] },
454 (SELECT blah) AS t(blah)
460 { -cast => [ { -ident => 'birthday' }, 'date' ] }
465 -as => [ { -ident => [ 'birthday' ] }, { -ident => [ 'date' ] } ]
470 CAST(birthday AS date)
475 If given an arrayref, pretends it was given a hashref with the first
476 element of the arrayref as the value for 'to' and the remaining pairs copied.
478 Given a hashref, the 'as' key is if presented expanded to wrap the 'to'.
480 If present the 'using' key is expanded as a list of idents.
482 Known keys are: 'from' (the left hand side), 'type' ('left', 'right', or
483 nothing), 'to' (the right hand side), 'on' and 'using'.
488 on => { 'lft.bloo' => { '>' => 'rgt.blee' } },
495 from => { -ident => [ 'lft' ] },
497 '>', { -ident => [ 'lft', 'bloo' ] },
498 { -ident => [ 'rgt', 'blee' ] },
500 to => { -ident => [ 'rgt' ] },
505 lft LEFT JOIN rgt ON lft.bloo > rgt.blee
510 List of components of the FROM clause; -foo type elements indicate a pair
511 with the next element; this is easiest if I show you:
515 't1', -as => 'table_one', -join =>
516 [ 't2', 'on', { 'table_one.x' => 't2.x' } ],
523 -as => [ { -ident => [ 't1' ] }, { -ident => [ 'table_one' ] } ]
526 '=', { -ident => [ 'table_one', 'x' ] },
527 { -ident => [ 't2', 'x' ] },
529 to => { -ident => [ 't2' ] },
534 t1 AS table_one JOIN t2 ON table_one.x = t2.x
541 [ 't1', -as => 'table_one', -join => [ 't2', 'using', [ 'x' ] ] ]
548 -as => [ { -ident => [ 't1' ] }, { -ident => [ 'table_one' ] } ]
550 to => { -ident => [ 't2' ] },
553 { -op => [ 'or', { -op => [ 'or', { -ident => [ 'x' ] } ] } ] },
557 t1 AS table_one JOIN t2 USING ( x )
565 [ [ 'y', -join => [ 'z', 'type', 'left' ] ], 'type', 'left' ],
570 from => { -ident => [ 'x' ] },
572 from => { -ident => [ 'y' ] },
573 to => { -ident => [ 'z' ] },
580 x LEFT JOIN ( y LEFT JOIN z )
583 =head1 STATEMENT EXTENSIONS
585 =head2 group by clause for select
587 Expanded as a list with an ident default:
590 { -select => { group_by => [ 'foo', 'bar' ] } }
593 { -select => { group_by =>
595 -op => [ ',', { -ident => [ 'foo' ] }, { -ident => [ 'bar' ] } ]
603 =head2 having clause for select
605 Basic expr, just like where, given having is pretty much post-group-by
610 { having => { '>' => [ { -count => { -ident => 'foo' } }, 3 ] } }
614 { -select => { having => { -op => [
615 '>', { -func => [ 'count', { -ident => [ 'foo' ] } ] },
616 { -bind => [ undef, 3 ] },
620 HAVING COUNT(foo) > ?