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 => 'with', @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');
84 $sqla->clause_expanders(
85 'update.from' => $self->cb('_expand_from_list'),
86 'delete.using' => $self->cb('_expand_from_list'),
87 'insert.rowvalues' => $self->cb(sub {
88 +(from => $_[0]->expand_expr({ -values => $_[2] }));
90 'insert.select' => $self->cb(sub {
91 +(from => $_[0]->expand_expr({ -select => $_[2] }));
96 $sqla->wrap_expander(select => sub {
97 $self->cb('_expand_select', $_[0], \@before_setop);
100 $sqla->clause_renderer(
101 'select.setop' => $self->cb(sub { $_[0]->render_aqt($_[2]); })
104 foreach my $setop (qw(union intersect except)) {
105 $sqla->expander($setop => $self->cb('_expand_setop'));
106 $sqla->renderer($setop => $self->cb('_render_setop'));
109 my $setop_expander = $self->cb('_expand_clause_setop');
111 $sqla->clause_expanders(
112 map +($_ => $setop_expander),
114 map +($_, "${_}_all", "${_}_distinct"),
115 qw(union intersect except)
118 my $w_exp = $self->cb('_expand_with');
119 my $w_rdr = $self->cb('_render_with');
120 $sqla->clause_expander('select.with' => $w_exp);
121 $sqla->clause_expander('select.with_recursive' => $w_exp);
122 $sqla->clause_renderer('select.with' => $w_rdr);
124 foreach my $stmt (qw(insert update delete)) {
125 $sqla->clauses_of($stmt => 'with', $sqla->clauses_of($stmt));
126 $sqla->clause_expander("${stmt}.$_" => $w_exp)
127 for qw(with with_recursive);
128 $sqla->clause_renderer("${stmt}.with" => $w_rdr);
131 $sqla->clause_expanders(
132 "select.from", $self->cb('_expand_from_list'),
133 "update.target", $self->cb('_expand_update_clause_target'),
134 "update.update", $self->cb('_expand_update_clause_target'),
141 my ($self, $orig, $before_setop, @args) = @_;
142 my $exp = $self->sqla->$orig(@args);
143 return $exp unless my $setop = (my $sel = $exp->{-select})->{setop};
144 if (my @keys = grep $sel->{$_}, @$before_setop) {
145 my %inner; @inner{@keys} = delete @{$sel}{@keys};
146 unshift @{(values(%$setop))[0]{queries}},
147 { -select => \%inner };
152 sub _expand_from_list {
153 my ($self, undef, $args) = @_;
154 if (ref($args) eq 'HASH') {
155 return $args if $args->{-from_list};
156 return { -from_list => [ $self->expand_expr($args) ] };
159 my @args = ref($args) eq 'ARRAY' ? @$args : ($args);
160 while (my $entry = shift @args) {
161 if (!ref($entry) and $entry =~ /^-(.*)/) {
163 $list[-1] = $self->expand_expr({ -as => [
164 $list[-1], map +(ref($_) eq 'ARRAY' ? @$_ : $_), shift(@args)
168 $entry = { $entry => shift @args };
170 my $aqt = $self->expand_expr($entry, -ident);
171 if ($aqt->{-join} and not $aqt->{-join}{from}) {
172 $aqt->{-join}{from} = pop @list;
176 return { -from_list => \@list };
180 my ($self, undef, $args) = @_;
184 : (to => $args->[0], @{$args}[1..$#$args])
186 if (my $as = delete $proto{as}) {
187 $proto{to} = $self->expand_expr({ -as => [ $proto{to}, $as ] });
189 if (defined($proto{using}) and ref(my $using = $proto{using}) ne 'HASH') {
191 map [ $self->expand_expr($_, -ident) ],
192 ref($using) eq 'ARRAY' ? @$using: $using
195 my %ret = map +($_ => $self->expand_expr($proto{$_}, -ident)),
197 return +{ -join => \%ret };
200 sub _render_from_list {
201 my ($self, undef, $list) = @_;
202 return $self->join_query_parts(', ', @$list);
206 my ($self, undef, $args) = @_;
210 { -keyword => join '_', ($args->{type}||()), 'join' },
211 (map +($_->{-ident} || $_->{-as} ? $_ : ('(', $_, ')')), $args->{to}),
213 { -keyword => 'on' },
217 { -keyword => 'using' },
218 '(', $args->{using}, ')',
221 return $self->join_query_parts(' ', @parts);
225 my ($self, undef, $vv, $k) = @_;
226 my @vv = (ref($vv) eq 'ARRAY' ? @$vv : $vv);
227 my $ik = $self->expand_expr($k, -ident);
228 return +{ -as => [ $ik, $self->expand_expr($vv[0], -alias) ] }
229 if @vv == 1 and ref($vv[0]) eq 'HASH';
231 my @as = map $self->expand_expr($_, -ident), @vv;
232 return { -as => [ $ik, { -alias => \@as } ] };
236 my ($self, undef, $args) = @_;
237 my ($thing, $alias) = @$args;
238 return $self->join_query_parts(
241 { -keyword => 'as' },
247 my ($self, undef, $args) = @_;
248 my ($as, @cols) = @$args;
250 ? $self->join_query_parts('',
253 $self->join_query_parts(
259 : $self->render_aqt($as)
263 sub _expand_update_clause_target {
264 my ($self, undef, $target) = @_;
265 +(target => $self->_expand_from_list(undef, $target));
269 my ($self, undef, $thing) = @_;
270 return { -func => [ cast => $thing ] } if ref($thing) eq 'HASH';
271 my ($cast, $to) = @{$thing};
272 +{ -func => [ cast => { -as => [
273 $self->expand_expr($cast),
274 $self->expand_expr($to, -ident),
279 my ($self, undef, $args) = @_;
280 if (ref($args) eq 'HASH' and my $alias = $args->{-alias}) {
284 map $self->expand_expr($_, -ident),
285 ref($args) eq 'ARRAY' ? @{$args} : $args
291 my ($self, $name, $with) = @_;
292 my (undef, $type) = split '_', $name;
293 if (ref($with) eq 'HASH') {
298 $self->expand_expr({ -alias => $_->[0] }, -ident),
299 $self->expand_expr($_->[1]),
300 ], @{$with->{queries}}
306 while (my ($alias, $query) = splice @with, 0, 2) {
308 $self->expand_expr({ -alias => $alias }, -ident),
309 $self->expand_expr($query)
312 return +(with => { ($type ? (type => $type) : ()), queries => \@exp });
316 my ($self, undef, $with) = @_;
317 my $q_part = $self->join_query_parts(', ',
319 my ($alias, $query) = @$_;
320 $self->join_query_parts(' ',
322 { -keyword => 'as' },
325 } @{$with->{queries}}
327 return $self->join_query_parts(' ',
328 { -keyword => join '_', 'with', ($with->{type}||'') },
334 my ($self, $setop, $args) = @_;
337 queries => [ map $self->expand_expr($_), @{$args->{queries}} ],
342 my ($self, $setop, $args) = @_;
343 $self->join_query_parts(
344 { -keyword => ' '.join('_', $setop, ($args->{type}||())).' ' },
349 sub _expand_clause_setop {
350 my ($self, $setop, $args) = @_;
351 my ($op, $type) = split '_', $setop;
352 +(setop => $self->expand_expr({
354 ($type ? (type => $type) : ()),
355 queries => (ref($args) eq 'ARRAY' ? $args : [ $args ])
366 SQL::Abstract::ExtraClauses - new/experimental additions to L<SQL::Abstract>
370 my $sqla = SQL::Abstract->new;
371 SQL::Abstract::ExtraClauses->apply_to($sqla);
377 Applies the plugin to an L<SQL::Abstract> object.
381 For plugin authors, creates a callback to call a method on the plugin.
385 Available only during plugin callback executions, contains the currently
386 active L<SQL::Abstract> object.
392 Represents a table alias. Expands name and column names with ident as default.
395 { -alias => [ 't', 'x', 'y', 'z' ] }
399 { -ident => [ 't' ] }, { -ident => [ 'x' ] },
400 { -ident => [ 'y' ] }, { -ident => [ 'z' ] },
409 Represents an sql AS. LHS is expanded with ident as default, RHS is treated
410 as a list of arguments for the alias node.
413 { foo => { -as => 'bar' } }
418 { -ident => [ 'foo' ] },
419 { -alias => [ { -ident => [ 'bar' ] } ] },
428 { -as => [ { -select => { _ => 'blah' } }, 't', 'blah' ] }
433 { select => { -op => [ ',', { -ident => [ 'blah' ] } ] } }
435 { -alias => [ { -ident => [ 't' ] }, { -ident => [ 'blah' ] } ] },
439 (SELECT blah) AS t(blah)
445 { -cast => [ { -ident => 'birthday' }, 'date' ] }
450 -as => [ { -ident => [ 'birthday' ] }, { -ident => [ 'date' ] } ]
455 CAST(birthday AS date)