1 package SQL::Abstract::ExtraClauses;
6 is => 'ro', init_arg => undef,
8 expand_expr expand_maybe_list_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) = @_;
41 my @clauses = $sqla->clauses_of('select');
43 CLAUSE: foreach my $idx (0..$#clauses) {
44 if ($clauses[$idx] eq 'order_by') {
45 @before_setop = @clauses[0..$idx-1];
46 splice(@clauses, $idx, 0, qw(setop group_by having));
50 die "Huh?" unless @before_setop;
51 $sqla->clauses_of(select => @clauses);
55 => sub { $_[0]->expand_maybe_list_expr($_[2], -ident) },
57 => sub { $_[0]->expand_expr($_[2]) },
63 (map +($_ => "_${x}_${_}"), qw(join from_list alias))
66 ), qw(expand render)),
67 binop_expander => [ as => '_expand_op_as' ],
68 renderer => [ as => '_render_as' ],
69 expander => [ cast => '_expand_cast' ],
72 $sqla->clauses_of(update => sub {
73 my ($self, @clauses) = @_;
74 splice(@clauses, 2, 0, 'from');
78 $sqla->clauses_of(delete => sub {
79 my ($self, @clauses) = @_;
80 splice(@clauses, 1, 0, 'using');
86 'update.from' => '_expand_from_list',
87 'delete.using' => '_expand_from_list',
88 'insert.rowvalues' => sub {
89 +(from => $_[0]->expand_expr({ -values => $_[2] }));
91 'insert.select' => sub {
92 +(from => $_[0]->expand_expr({ -select => $_[2] }));
98 $sqla->wrap_expander(select => sub {
99 $self->cb('_expand_select', $_[0], \@before_setop);
104 'select.setop' => sub { $_[0]->render_aqt($_[2]) }
106 expander => [ map +($_ => '_expand_setop'), qw(union intersect except) ],
107 renderer => [ map +($_ => '_render_setop'), qw(union intersect except) ],
110 my $setop_expander = $self->cb('_expand_clause_setop');
112 $sqla->clause_expanders(
113 map +($_ => $setop_expander),
115 map +($_, "${_}_all", "${_}_distinct"),
116 qw(union intersect except)
119 foreach my $stmt (qw(select insert update delete)) {
120 $sqla->clauses_of($stmt => 'with', $sqla->clauses_of($stmt));
122 clause_expanders => [
123 "${stmt}.with" => '_expand_with',
124 "${stmt}.with_recursive" => '_expand_with',
126 clause_renderer => [ "${stmt}.with" => '_render_with' ],
131 clause_expanders => [
132 "select.from", '_expand_from_list',
133 "update.target", '_expand_update_clause_target',
134 "update.update", '_expand_update_clause_target',
142 my ($self, $orig, $before_setop, @args) = @_;
143 my $exp = $self->sqla->$orig(@args);
144 return $exp unless my $setop = (my $sel = $exp->{-select})->{setop};
145 if (my @keys = grep $sel->{$_}, @$before_setop) {
146 my %inner; @inner{@keys} = delete @{$sel}{@keys};
147 unshift @{(values(%$setop))[0]{queries}},
148 { -select => \%inner };
153 sub _expand_from_list {
154 my ($self, undef, $args) = @_;
155 if (ref($args) eq 'HASH') {
156 return $args if $args->{-from_list};
157 return { -from_list => [ $self->expand_expr($args) ] };
160 my @args = ref($args) eq 'ARRAY' ? @$args : ($args);
161 while (my $entry = shift @args) {
162 if (!ref($entry) and $entry =~ /^-(.*)/) {
164 $list[-1] = $self->expand_expr({ -as => [
165 $list[-1], map +(ref($_) eq 'ARRAY' ? @$_ : $_), shift(@args)
169 $entry = { $entry => shift @args };
171 my $aqt = $self->expand_expr($entry, -ident);
172 if ($aqt->{-join} and not $aqt->{-join}{from}) {
173 $aqt->{-join}{from} = pop @list;
177 return { -from_list => \@list };
181 my ($self, undef, $args) = @_;
185 : (to => $args->[0], @{$args}[1..$#$args])
187 if (my $as = delete $proto{as}) {
188 $proto{to} = $self->expand_expr({ -as => [ $proto{to}, $as ] });
190 if (defined($proto{using}) and ref(my $using = $proto{using}) ne 'HASH') {
192 map [ $self->expand_expr($_, -ident) ],
193 ref($using) eq 'ARRAY' ? @$using: $using
196 my %ret = map +($_ => $self->expand_expr($proto{$_}, -ident)),
198 return +{ -join => \%ret };
201 sub _render_from_list {
202 my ($self, undef, $list) = @_;
203 return $self->join_query_parts(', ', @$list);
207 my ($self, undef, $args) = @_;
211 { -keyword => join '_', ($args->{type}||()), 'join' },
212 (map +($_->{-ident} || $_->{-as} ? $_ : ('(', $_, ')')), $args->{to}),
214 { -keyword => 'on' },
218 { -keyword => 'using' },
219 '(', $args->{using}, ')',
222 return $self->join_query_parts(' ', @parts);
226 my ($self, undef, $vv, $k) = @_;
227 my @vv = (ref($vv) eq 'ARRAY' ? @$vv : $vv);
228 my $ik = $self->expand_expr($k, -ident);
229 return +{ -as => [ $ik, $self->expand_expr($vv[0], -alias) ] }
230 if @vv == 1 and ref($vv[0]) eq 'HASH';
232 my @as = map $self->expand_expr($_, -ident), @vv;
233 return { -as => [ $ik, { -alias => \@as } ] };
237 my ($self, undef, $args) = @_;
238 my ($thing, $alias) = @$args;
239 return $self->join_query_parts(
242 { -keyword => 'as' },
248 my ($self, undef, $args) = @_;
249 my ($as, @cols) = @$args;
251 ? $self->join_query_parts('',
254 $self->join_query_parts(
260 : $self->render_aqt($as)
264 sub _expand_update_clause_target {
265 my ($self, undef, $target) = @_;
266 +(target => $self->_expand_from_list(undef, $target));
270 my ($self, undef, $thing) = @_;
271 return { -func => [ cast => $thing ] } if ref($thing) eq 'HASH';
272 my ($cast, $to) = @{$thing};
273 +{ -func => [ cast => { -as => [
274 $self->expand_expr($cast),
275 $self->expand_expr($to, -ident),
280 my ($self, undef, $args) = @_;
281 if (ref($args) eq 'HASH' and my $alias = $args->{-alias}) {
285 map $self->expand_expr($_, -ident),
286 ref($args) eq 'ARRAY' ? @{$args} : $args
292 my ($self, $name, $with) = @_;
293 my (undef, $type) = split '_', $name;
294 if (ref($with) eq 'HASH') {
299 $self->expand_expr({ -alias => $_->[0] }, -ident),
300 $self->expand_expr($_->[1]),
301 ], @{$with->{queries}}
307 while (my ($alias, $query) = splice @with, 0, 2) {
309 $self->expand_expr({ -alias => $alias }, -ident),
310 $self->expand_expr($query)
313 return +(with => { ($type ? (type => $type) : ()), queries => \@exp });
317 my ($self, undef, $with) = @_;
318 my $q_part = $self->join_query_parts(', ',
320 my ($alias, $query) = @$_;
321 $self->join_query_parts(' ',
323 { -keyword => 'as' },
326 } @{$with->{queries}}
328 return $self->join_query_parts(' ',
329 { -keyword => join '_', 'with', ($with->{type}||'') },
335 my ($self, $setop, $args) = @_;
338 queries => [ map $self->expand_expr($_), @{$args->{queries}} ],
343 my ($self, $setop, $args) = @_;
344 $self->join_query_parts(
345 { -keyword => ' '.join('_', $setop, ($args->{type}||())).' ' },
350 sub _expand_clause_setop {
351 my ($self, $setop, $args) = @_;
352 my ($op, $type) = split '_', $setop;
353 +(setop => $self->expand_expr({
355 ($type ? (type => $type) : ()),
356 queries => (ref($args) eq 'ARRAY' ? $args : [ $args ])
367 SQL::Abstract::ExtraClauses - new/experimental additions to L<SQL::Abstract>
371 my $sqla = SQL::Abstract->new;
372 SQL::Abstract::ExtraClauses->apply_to($sqla);
378 Applies the plugin to an L<SQL::Abstract> object.
382 For plugin authors, creates a callback to call a method on the plugin.
386 Available only during plugin callback executions, contains the currently
387 active L<SQL::Abstract> object.
393 Represents a table alias. Expands name and column names with ident as default.
396 { -alias => [ 't', 'x', 'y', 'z' ] }
400 { -ident => [ 't' ] }, { -ident => [ 'x' ] },
401 { -ident => [ 'y' ] }, { -ident => [ 'z' ] },
410 Represents an sql AS. LHS is expanded with ident as default, RHS is treated
411 as a list of arguments for the alias node.
414 { foo => { -as => 'bar' } }
419 { -ident => [ 'foo' ] },
420 { -alias => [ { -ident => [ 'bar' ] } ] },
429 { -as => [ { -select => { _ => 'blah' } }, 't', 'blah' ] }
434 { select => { -op => [ ',', { -ident => [ 'blah' ] } ] } }
436 { -alias => [ { -ident => [ 't' ] }, { -ident => [ 'blah' ] } ] },
440 (SELECT blah) AS t(blah)
446 { -cast => [ { -ident => 'birthday' }, 'date' ] }
451 -as => [ { -ident => [ 'birthday' ] }, { -ident => [ 'date' ] } ]
456 CAST(birthday AS date)