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, $sqla) = @_;
22 $self = $self->new unless ref($self);
23 my @clauses = $sqla->clauses_of('select');
25 CLAUSE: foreach my $idx (0..$#clauses) {
26 if ($clauses[$idx] eq 'order_by') {
27 @before_setop = @clauses[0..$idx-1];
28 splice(@clauses, $idx, 0, qw(setop group_by having));
32 die "Huh?" unless @before_setop;
33 $sqla->clauses_of(select => 'with', @clauses);
34 $sqla->clause_expanders(
35 'select.group_by', $self->cb(sub {
36 $_[0]->expand_maybe_list_expr($_[2], -ident)
38 'select.having', $self->cb(sub { $_[0]->expand_expr($_[2]) }),
40 foreach my $thing (qw(join from_list)) {
41 $sqla->expander($thing => $self->cb("_expand_${thing}"))
42 ->renderer($thing => $self->cb("_render_${thing}"))
44 $sqla->binop_expander(as => $self->cb('_expand_op_as'));
45 $sqla->renderer(as => $self->cb('_render_as'));
46 $sqla->expander(alias => $self->cb('_expand_alias'));
47 $sqla->renderer(alias => $self->cb('_render_alias'));
49 $sqla->clauses_of(update => sub {
50 my ($self, @clauses) = @_;
51 splice(@clauses, 2, 0, 'from');
55 $sqla->clauses_of(delete => sub {
56 my ($self, @clauses) = @_;
57 splice(@clauses, 1, 0, 'using');
61 $sqla->clause_expanders(
62 'update.from' => $self->cb('_expand_from_list'),
63 'delete.using' => $self->cb('_expand_from_list'),
64 'insert.rowvalues' => $self->cb(sub {
65 +(from => $_[0]->expand_expr({ -values => $_[2] }));
67 'insert.select' => $self->cb(sub {
68 +(from => $_[0]->expand_expr({ -select => $_[2] }));
73 $sqla->wrap_expander(select => sub {
74 $self->cb('_expand_select', $_[0], \@before_setop);
77 $sqla->clause_renderer(
78 'select.setop' => $self->cb(sub { $_[0]->render_aqt($_[2]); })
81 foreach my $setop (qw(union intersect except)) {
82 $sqla->expander($setop => $self->cb('_expand_setop'));
83 $sqla->renderer($setop => $self->cb('_render_setop'));
86 my $setop_expander = $self->cb('_expand_clause_setop');
88 $sqla->clause_expanders(
89 map +($_ => $setop_expander),
91 map +($_, "${_}_all", "${_}_distinct"),
92 qw(union intersect except)
95 my $w_exp = $self->cb('_expand_with');
96 my $w_rdr = $self->cb('_render_with');
97 $sqla->clause_expander('select.with' => $w_exp);
98 $sqla->clause_expander('select.with_recursive' => $w_exp);
99 $sqla->clause_renderer('select.with' => $w_rdr);
101 foreach my $stmt (qw(insert update delete)) {
102 $sqla->clauses_of($stmt => 'with', $sqla->clauses_of($stmt));
103 $sqla->clause_expander("${stmt}.$_" => $w_exp)
104 for qw(with with_recursive);
105 $sqla->clause_renderer("${stmt}.with" => $w_rdr);
108 $sqla->expander(cast => $self->cb('_expand_cast'));
110 $sqla->clause_expanders(
111 "select.from", $self->cb('_expand_from_list'),
112 "update.target", $self->cb('_expand_update_clause_target'),
113 "update.update", $self->cb('_expand_update_clause_target'),
120 my ($self, $orig, $before_setop, @args) = @_;
121 my $exp = $self->sqla->$orig(@args);
122 return $exp unless my $setop = (my $sel = $exp->{-select})->{setop};
123 if (my @keys = grep $sel->{$_}, @$before_setop) {
124 my %inner; @inner{@keys} = delete @{$sel}{@keys};
125 unshift @{(values(%$setop))[0]{queries}},
126 { -select => \%inner };
131 sub _expand_from_list {
132 my ($self, undef, $args) = @_;
133 if (ref($args) eq 'HASH') {
134 return $args if $args->{-from_list};
135 return { -from_list => [ $self->expand_expr($args) ] };
138 my @args = ref($args) eq 'ARRAY' ? @$args : ($args);
139 while (my $entry = shift @args) {
140 if (!ref($entry) and $entry =~ /^-(.*)/) {
142 $list[-1] = $self->expand_expr({ -as => [
143 $list[-1], map +(ref($_) eq 'ARRAY' ? @$_ : $_), shift(@args)
147 $entry = { $entry => shift @args };
149 my $aqt = $self->expand_expr($entry, -ident);
150 if ($aqt->{-join} and not $aqt->{-join}{from}) {
151 $aqt->{-join}{from} = pop @list;
155 return { -from_list => \@list };
159 my ($self, undef, $args) = @_;
163 : (to => $args->[0], @{$args}[1..$#$args])
165 if (my $as = delete $proto{as}) {
166 $proto{to} = $self->expand_expr({ -as => [ $proto{to}, $as ] });
168 if (defined($proto{using}) and ref(my $using = $proto{using}) ne 'HASH') {
170 map [ $self->expand_expr($_, -ident) ],
171 ref($using) eq 'ARRAY' ? @$using: $using
174 my %ret = map +($_ => $self->expand_expr($proto{$_}, -ident)),
176 return +{ -join => \%ret };
179 sub _render_from_list {
180 my ($self, undef, $list) = @_;
181 return $self->join_query_parts(', ', @$list);
185 my ($self, undef, $args) = @_;
189 { -keyword => join '_', ($args->{type}||()), 'join' },
190 (map +($_->{-ident} || $_->{-as} ? $_ : ('(', $_, ')')), $args->{to}),
192 { -keyword => 'on' },
196 { -keyword => 'using' },
197 '(', $args->{using}, ')',
200 return $self->join_query_parts(' ', @parts);
204 my ($self, undef, $vv, $k) = @_;
205 my @vv = (ref($vv) eq 'ARRAY' ? @$vv : $vv);
206 my $ik = $self->expand_expr($k, -ident);
207 return +{ -as => [ $ik, $self->expand_expr($vv[0], -alias) ] }
208 if @vv == 1 and ref($vv[0]) eq 'HASH';
210 my @as = map $self->expand_expr($_, -ident), @vv;
211 return { -as => [ $ik, { -alias => \@as } ] };
215 my ($self, undef, $args) = @_;
216 my ($thing, $alias) = @$args;
217 return $self->join_query_parts(
220 { -keyword => 'as' },
226 my ($self, undef, $args) = @_;
227 my ($as, @cols) = @$args;
229 ? $self->join_query_parts('',
232 $self->join_query_parts(
238 : $self->render_aqt($as)
242 sub _expand_update_clause_target {
243 my ($self, undef, $target) = @_;
244 +(target => $self->_expand_from_list(undef, $target));
248 my ($self, undef, $thing) = @_;
249 return { -func => [ cast => $thing ] } if ref($thing) eq 'HASH';
250 my ($cast, $to) = @{$thing};
251 +{ -func => [ cast => { -as => [
252 $self->expand_expr($cast),
253 $self->expand_expr($to, -ident),
258 my ($self, undef, $args) = @_;
259 if (ref($args) eq 'HASH' and my $alias = $args->{-alias}) {
263 map $self->expand_expr($_, -ident),
264 ref($args) eq 'ARRAY' ? @{$args} : $args
270 my ($self, $name, $with) = @_;
271 my (undef, $type) = split '_', $name;
272 if (ref($with) eq 'HASH') {
277 $self->expand_expr({ -alias => $_->[0] }, -ident),
278 $self->expand_expr($_->[1]),
279 ], @{$with->{queries}}
285 while (my ($alias, $query) = splice @with, 0, 2) {
287 $self->expand_expr({ -alias => $alias }, -ident),
288 $self->expand_expr($query)
291 return +(with => { ($type ? (type => $type) : ()), queries => \@exp });
295 my ($self, undef, $with) = @_;
296 my $q_part = $self->join_query_parts(', ',
298 my ($alias, $query) = @$_;
299 $self->join_query_parts(' ',
301 { -keyword => 'as' },
304 } @{$with->{queries}}
306 return $self->join_query_parts(' ',
307 { -keyword => join '_', 'with', ($with->{type}||'') },
313 my ($self, $setop, $args) = @_;
316 queries => [ map $self->expand_expr($_), @{$args->{queries}} ],
321 my ($self, $setop, $args) = @_;
322 $self->join_query_parts(
323 { -keyword => ' '.join('_', $setop, ($args->{type}||())).' ' },
328 sub _expand_clause_setop {
329 my ($self, $setop, $args) = @_;
330 my ($op, $type) = split '_', $setop;
331 +(setop => $self->expand_expr({
333 ($type ? (type => $type) : ()),
334 queries => (ref($args) eq 'ARRAY' ? $args : [ $args ])
343 SQL::Abstract::ExtraClauses - new/experimental additions to L<SQL::Abstract>
347 my $sqla = SQL::Abstract->new;
348 SQL::Abstract::ExtraClauses->apply_to($sqla);
354 Applies the plugin to an L<SQL::Abstract> object.
358 For plugin authors, creates a callback to call a method on the plugin.
362 Available only during plugin callback executions, contains the currently
363 active L<SQL::Abstract> object.