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, $method, @pairs) = @_;
22 my $sqla = $self->sqla;
23 while (my ($car, $cdr) = splice(@pairs, 0, 2)) {
24 my $cb = $self->cb($cdr);
25 $sqla->$method($car, $cb);
31 my ($self, $sqla) = @_;
32 $self = $self->new unless ref($self);
33 local $self->{sqla} = $sqla;
34 $self->register_extensions($sqla);
37 sub register_extensions {
38 my ($self, $sqla) = @_;
39 my @clauses = $sqla->clauses_of('select');
41 CLAUSE: foreach my $idx (0..$#clauses) {
42 if ($clauses[$idx] eq 'order_by') {
43 @before_setop = @clauses[0..$idx-1];
44 splice(@clauses, $idx, 0, qw(setop group_by having));
48 die "Huh?" unless @before_setop;
49 $sqla->clauses_of(select => 'with', @clauses);
53 => sub { $_[0]->expand_maybe_list_expr($_[2], -ident) },
55 => sub { $_[0]->expand_expr($_[2]) },
57 foreach my $thing (qw(join from_list)) {
58 $sqla->expander($thing => $self->cb("_expand_${thing}"))
59 ->renderer($thing => $self->cb("_render_${thing}"))
61 $sqla->binop_expander(as => $self->cb('_expand_op_as'));
62 $sqla->renderer(as => $self->cb('_render_as'));
63 $sqla->expander(alias => $self->cb('_expand_alias'));
64 $sqla->renderer(alias => $self->cb('_render_alias'));
66 $sqla->clauses_of(update => sub {
67 my ($self, @clauses) = @_;
68 splice(@clauses, 2, 0, 'from');
72 $sqla->clauses_of(delete => sub {
73 my ($self, @clauses) = @_;
74 splice(@clauses, 1, 0, 'using');
78 $sqla->clause_expanders(
79 'update.from' => $self->cb('_expand_from_list'),
80 'delete.using' => $self->cb('_expand_from_list'),
81 'insert.rowvalues' => $self->cb(sub {
82 +(from => $_[0]->expand_expr({ -values => $_[2] }));
84 'insert.select' => $self->cb(sub {
85 +(from => $_[0]->expand_expr({ -select => $_[2] }));
90 $sqla->wrap_expander(select => sub {
91 $self->cb('_expand_select', $_[0], \@before_setop);
94 $sqla->clause_renderer(
95 'select.setop' => $self->cb(sub { $_[0]->render_aqt($_[2]); })
98 foreach my $setop (qw(union intersect except)) {
99 $sqla->expander($setop => $self->cb('_expand_setop'));
100 $sqla->renderer($setop => $self->cb('_render_setop'));
103 my $setop_expander = $self->cb('_expand_clause_setop');
105 $sqla->clause_expanders(
106 map +($_ => $setop_expander),
108 map +($_, "${_}_all", "${_}_distinct"),
109 qw(union intersect except)
112 my $w_exp = $self->cb('_expand_with');
113 my $w_rdr = $self->cb('_render_with');
114 $sqla->clause_expander('select.with' => $w_exp);
115 $sqla->clause_expander('select.with_recursive' => $w_exp);
116 $sqla->clause_renderer('select.with' => $w_rdr);
118 foreach my $stmt (qw(insert update delete)) {
119 $sqla->clauses_of($stmt => 'with', $sqla->clauses_of($stmt));
120 $sqla->clause_expander("${stmt}.$_" => $w_exp)
121 for qw(with with_recursive);
122 $sqla->clause_renderer("${stmt}.with" => $w_rdr);
125 $sqla->expander(cast => $self->cb('_expand_cast'));
127 $sqla->clause_expanders(
128 "select.from", $self->cb('_expand_from_list'),
129 "update.target", $self->cb('_expand_update_clause_target'),
130 "update.update", $self->cb('_expand_update_clause_target'),
137 my ($self, $orig, $before_setop, @args) = @_;
138 my $exp = $self->sqla->$orig(@args);
139 return $exp unless my $setop = (my $sel = $exp->{-select})->{setop};
140 if (my @keys = grep $sel->{$_}, @$before_setop) {
141 my %inner; @inner{@keys} = delete @{$sel}{@keys};
142 unshift @{(values(%$setop))[0]{queries}},
143 { -select => \%inner };
148 sub _expand_from_list {
149 my ($self, undef, $args) = @_;
150 if (ref($args) eq 'HASH') {
151 return $args if $args->{-from_list};
152 return { -from_list => [ $self->expand_expr($args) ] };
155 my @args = ref($args) eq 'ARRAY' ? @$args : ($args);
156 while (my $entry = shift @args) {
157 if (!ref($entry) and $entry =~ /^-(.*)/) {
159 $list[-1] = $self->expand_expr({ -as => [
160 $list[-1], map +(ref($_) eq 'ARRAY' ? @$_ : $_), shift(@args)
164 $entry = { $entry => shift @args };
166 my $aqt = $self->expand_expr($entry, -ident);
167 if ($aqt->{-join} and not $aqt->{-join}{from}) {
168 $aqt->{-join}{from} = pop @list;
172 return { -from_list => \@list };
176 my ($self, undef, $args) = @_;
180 : (to => $args->[0], @{$args}[1..$#$args])
182 if (my $as = delete $proto{as}) {
183 $proto{to} = $self->expand_expr({ -as => [ $proto{to}, $as ] });
185 if (defined($proto{using}) and ref(my $using = $proto{using}) ne 'HASH') {
187 map [ $self->expand_expr($_, -ident) ],
188 ref($using) eq 'ARRAY' ? @$using: $using
191 my %ret = map +($_ => $self->expand_expr($proto{$_}, -ident)),
193 return +{ -join => \%ret };
196 sub _render_from_list {
197 my ($self, undef, $list) = @_;
198 return $self->join_query_parts(', ', @$list);
202 my ($self, undef, $args) = @_;
206 { -keyword => join '_', ($args->{type}||()), 'join' },
207 (map +($_->{-ident} || $_->{-as} ? $_ : ('(', $_, ')')), $args->{to}),
209 { -keyword => 'on' },
213 { -keyword => 'using' },
214 '(', $args->{using}, ')',
217 return $self->join_query_parts(' ', @parts);
221 my ($self, undef, $vv, $k) = @_;
222 my @vv = (ref($vv) eq 'ARRAY' ? @$vv : $vv);
223 my $ik = $self->expand_expr($k, -ident);
224 return +{ -as => [ $ik, $self->expand_expr($vv[0], -alias) ] }
225 if @vv == 1 and ref($vv[0]) eq 'HASH';
227 my @as = map $self->expand_expr($_, -ident), @vv;
228 return { -as => [ $ik, { -alias => \@as } ] };
232 my ($self, undef, $args) = @_;
233 my ($thing, $alias) = @$args;
234 return $self->join_query_parts(
237 { -keyword => 'as' },
243 my ($self, undef, $args) = @_;
244 my ($as, @cols) = @$args;
246 ? $self->join_query_parts('',
249 $self->join_query_parts(
255 : $self->render_aqt($as)
259 sub _expand_update_clause_target {
260 my ($self, undef, $target) = @_;
261 +(target => $self->_expand_from_list(undef, $target));
265 my ($self, undef, $thing) = @_;
266 return { -func => [ cast => $thing ] } if ref($thing) eq 'HASH';
267 my ($cast, $to) = @{$thing};
268 +{ -func => [ cast => { -as => [
269 $self->expand_expr($cast),
270 $self->expand_expr($to, -ident),
275 my ($self, undef, $args) = @_;
276 if (ref($args) eq 'HASH' and my $alias = $args->{-alias}) {
280 map $self->expand_expr($_, -ident),
281 ref($args) eq 'ARRAY' ? @{$args} : $args
287 my ($self, $name, $with) = @_;
288 my (undef, $type) = split '_', $name;
289 if (ref($with) eq 'HASH') {
294 $self->expand_expr({ -alias => $_->[0] }, -ident),
295 $self->expand_expr($_->[1]),
296 ], @{$with->{queries}}
302 while (my ($alias, $query) = splice @with, 0, 2) {
304 $self->expand_expr({ -alias => $alias }, -ident),
305 $self->expand_expr($query)
308 return +(with => { ($type ? (type => $type) : ()), queries => \@exp });
312 my ($self, undef, $with) = @_;
313 my $q_part = $self->join_query_parts(', ',
315 my ($alias, $query) = @$_;
316 $self->join_query_parts(' ',
318 { -keyword => 'as' },
321 } @{$with->{queries}}
323 return $self->join_query_parts(' ',
324 { -keyword => join '_', 'with', ($with->{type}||'') },
330 my ($self, $setop, $args) = @_;
333 queries => [ map $self->expand_expr($_), @{$args->{queries}} ],
338 my ($self, $setop, $args) = @_;
339 $self->join_query_parts(
340 { -keyword => ' '.join('_', $setop, ($args->{type}||())).' ' },
345 sub _expand_clause_setop {
346 my ($self, $setop, $args) = @_;
347 my ($op, $type) = split '_', $setop;
348 +(setop => $self->expand_expr({
350 ($type ? (type => $type) : ()),
351 queries => (ref($args) eq 'ARRAY' ? $args : [ $args ])
362 SQL::Abstract::ExtraClauses - new/experimental additions to L<SQL::Abstract>
366 my $sqla = SQL::Abstract->new;
367 SQL::Abstract::ExtraClauses->apply_to($sqla);
373 Applies the plugin to an L<SQL::Abstract> object.
377 For plugin authors, creates a callback to call a method on the plugin.
381 Available only during plugin callback executions, contains the currently
382 active L<SQL::Abstract> object.
388 Represents a table alias. Expands name and column names with ident as default.
391 { -alias => [ 't', 'x', 'y', 'z' ] }
395 { -ident => [ 't' ] }, { -ident => [ 'x' ] },
396 { -ident => [ 'y' ] }, { -ident => [ 'z' ] },
405 Represents an sql AS. LHS is expanded with ident as default, RHS is treated
406 as a list of arguments for the alias node.
409 { foo => { -as => 'bar' } }
414 { -ident => [ 'foo' ] },
415 { -alias => [ { -ident => [ 'bar' ] } ] },
424 { -as => [ { -select => { _ => 'blah' } }, 't', 'blah' ] }
429 { select => { -op => [ ',', { -ident => [ 'blah' ] } ] } }
431 { -alias => [ { -ident => [ 't' ] }, { -ident => [ 'blah' ] } ] },
435 (SELECT blah) AS t(blah)
441 { -cast => [ { -ident => 'birthday' }, 'date' ] }
446 -as => [ { -ident => [ 'birthday' ] }, { -ident => [ 'date' ] } ]
451 CAST(birthday AS date)