clean up EC _expand_select
[dbsrgits/SQL-Abstract.git] / lib / SQL / Abstract / ExtraClauses.pm
CommitLineData
f8392f56 1package SQL::Abstract::ExtraClauses;
2
b40d30dc 3use Moo;
4
5has sqla => (
6 is => 'ro', init_arg => undef,
7 handles => [ qw(
035449c2 8 expand_expr expand_maybe_list_expr render_aqt
b40d30dc 9 format_keyword join_query_parts
10 ) ],
11);
f8392f56 12
b40d30dc 13sub cb {
22f5ed9a 14 my ($self, $method, @args) = @_;
15 return sub {
16 local $self->{sqla} = shift;
17 $self->$method(@args, @_)
18 };
b40d30dc 19}
20
21sub apply_to {
22 my ($self, $sqla) = @_;
23 $self = $self->new unless ref($self);
ac3616e8 24 my @clauses = $sqla->clauses_of('select');
f7a20100 25 my @before_setop;
26 CLAUSE: foreach my $idx (0..$#clauses) {
27 if ($clauses[$idx] eq 'order_by') {
28 @before_setop = @clauses[0..$idx-1];
29 splice(@clauses, $idx, 0, qw(setop group_by having));
30 last CLAUSE;
31 }
32 }
33 die "Huh?" unless @before_setop;
ac3616e8 34 $sqla->clauses_of(select => 'with', @clauses);
35 $sqla->clause_expanders(
b40d30dc 36 'select.group_by', $self->cb(sub {
035449c2 37 $_[0]->expand_maybe_list_expr($_[2], -ident)
b40d30dc 38 }),
39 'select.having', $self->cb(sub { $_[0]->expand_expr($_[2]) }),
71c1b4d5 40 );
01dfea60 41 foreach my $thing (qw(join from_list)) {
ac3616e8 42 $sqla->expander($thing => $self->cb("_expand_${thing}"))
b40d30dc 43 ->renderer($thing => $self->cb("_render_${thing}"))
01dfea60 44 }
ac3616e8 45 $sqla->op_expander(as => $self->cb('_expand_op_as'));
46 $sqla->expander(as => $self->cb('_expand_op_as'));
47 $sqla->renderer(as => $self->cb('_render_as'));
6d42f0b9 48 $sqla->expander(alias => $self->cb('_expand_alias'));
ac3616e8 49 $sqla->renderer(alias => $self->cb('_render_alias'));
71c1b4d5 50
ac3616e8 51 $sqla->clauses_of(update => sub {
e6b86bee 52 my ($self, @clauses) = @_;
53 splice(@clauses, 2, 0, 'from');
54 @clauses;
55 });
56
ac3616e8 57 $sqla->clauses_of(delete => sub {
e6b86bee 58 my ($self, @clauses) = @_;
59 splice(@clauses, 1, 0, 'using');
60 @clauses;
61 });
62
ac3616e8 63 $sqla->clause_expanders(
b40d30dc 64 'update.from' => $self->cb('_expand_select_clause_from'),
65 'delete.using' => $self->cb(sub {
1107714b 66 +(using => $_[0]->_expand_from_list(undef, $_[2]));
b40d30dc 67 }),
68 'insert.rowvalues' => $self->cb(sub {
1107714b 69 +(from => $_[0]->expand_expr({ -values => $_[2] }));
b40d30dc 70 }),
71 'insert.select' => $self->cb(sub {
1107714b 72 +(from => $_[0]->expand_expr({ -select => $_[2] }));
b40d30dc 73 }),
71c1b4d5 74 );
37b399a8 75
f7a20100 76 # set ops
ac3616e8 77 $sqla->wrap_expander(select => sub {
22f5ed9a 78 $self->cb('_expand_select', $_[0], \@before_setop);
95ab9342 79 });
f7a20100 80
ac3616e8 81 $sqla->clause_renderer('select.setop' => $self->cb(sub {
1107714b 82 my ($self, undef, $setop) = @_;
68a92d22 83 $self->render_aqt($setop);
b40d30dc 84 }));
f7a20100 85
4b5f7259 86 foreach my $setop (qw(union intersect except)) {
87 $sqla->expander($setop => $self->cb('_expand_setop'));
88 $sqla->renderer($setop => $self->cb('_render_setop'));
89 }
b42692a6 90
0f4de029 91 my $setop_expander = $self->cb('_expand_clause_setop');
bb36c26d 92
ac3616e8 93 $sqla->clause_expanders(
bb36c26d 94 map +($_ => $setop_expander),
95 map "select.${_}",
96 map +($_, "${_}_all", "${_}_distinct"),
97 qw(union intersect except)
98 );
f7fd09f7 99
6d42f0b9 100 my $w_exp = $self->cb('_expand_with');
101 my $w_rdr = $self->cb('_render_with');
102 $sqla->clause_expander('select.with' => $w_exp);
103 $sqla->clause_expander('select.with_recursive' => $w_exp);
104 $sqla->clause_renderer('select.with' => $w_rdr);
105
44a6affb 106 foreach my $stmt (qw(insert update delete)) {
ac3616e8 107 $sqla->clauses_of($stmt => 'with', $sqla->clauses_of($stmt));
6d42f0b9 108 $sqla->clause_expander("${stmt}.$_" => $w_exp)
44a6affb 109 for qw(with with_recursive);
6d42f0b9 110 $sqla->clause_renderer("${stmt}.with" => $w_rdr);
44a6affb 111 }
6d42f0b9 112
113 $sqla->expander(cast => $self->cb('_expand_cast'));
b40d30dc 114
ac3616e8 115 $sqla->clause_expanders(
b40d30dc 116 "select.from", $self->cb('_expand_select_clause_from'),
117 "update.target", $self->cb('_expand_update_clause_target'),
118 "update.update", $self->cb('_expand_update_clause_target'),
119 );
f7a20100 120
ac3616e8 121 return $sqla;
f8392f56 122}
123
22f5ed9a 124sub _expand_select {
b5c13e0a 125 my ($self, $orig, $before_setop, @args) = @_;
126 my $exp = $self->sqla->$orig(@args);
22f5ed9a 127 return $exp unless my $setop = (my $sel = $exp->{-select})->{setop};
128 if (my @keys = grep $sel->{$_}, @$before_setop) {
129 my %inner; @inner{@keys} = delete @{$sel}{@keys};
130 unshift @{(values(%$setop))[0]{queries}},
131 { -select => \%inner };
132 }
133 return $exp;
134}
135
f79455dc 136sub _expand_select_clause_from {
1107714b 137 my ($self, undef, $from) = @_;
f79455dc 138 +(from => $self->_expand_from_list(undef, $from));
139}
140
141sub _expand_from_list {
142 my ($self, undef, $args) = @_;
143 if (ref($args) eq 'HASH') {
09ceda11 144 return $args if $args->{-from_list};
f79455dc 145 return { -from_list => [ $self->expand_expr($args) ] };
146 }
147 my @list;
b9e35873 148 my @args = ref($args) eq 'ARRAY' ? @$args : ($args);
f79455dc 149 while (my $entry = shift @args) {
6990b2aa 150 if (!ref($entry) and $entry =~ /^-(.*)/) {
151 if ($1 eq 'as') {
152 $list[-1] = $self->expand_expr({ -as => [
153 $list[-1], map +(ref($_) eq 'ARRAY' ? @$_ : $_), shift(@args)
154 ]});
155 next;
156 }
f79455dc 157 $entry = { $entry => shift @args };
158 }
159 my $aqt = $self->expand_expr($entry, -ident);
160 if ($aqt->{-join} and not $aqt->{-join}{from}) {
161 $aqt->{-join}{from} = pop @list;
162 }
163 push @list, $aqt;
164 }
165 return { -from_list => \@list };
166}
167
168sub _expand_join {
169 my ($self, undef, $args) = @_;
170 my %proto = (
171 ref($args) eq 'HASH'
172 ? %$args
173 : (to => $args->[0], @{$args}[1..$#$args])
174 );
6990b2aa 175 if (my $as = delete $proto{as}) {
01e9b916 176 $proto{to} = $self->expand_expr({ -as => [ $proto{to}, $as ] });
6990b2aa 177 }
0891ae97 178 if (defined($proto{using}) and ref(my $using = $proto{using}) ne 'HASH') {
60757815 179 $proto{using} = [
0891ae97 180 map [ $self->expand_expr($_, -ident) ],
181 ref($using) eq 'ARRAY' ? @$using: $using
60757815 182 ];
0891ae97 183 }
f79455dc 184 my %ret = map +($_ => $self->expand_expr($proto{$_}, -ident)),
185 sort keys %proto;
186 return +{ -join => \%ret };
187}
188
189sub _render_from_list {
6c39a2f7 190 my ($self, undef, $list) = @_;
3e230b71 191 return $self->join_query_parts(', ', @$list);
f79455dc 192}
193
194sub _render_join {
6c39a2f7 195 my ($self, undef, $args) = @_;
f79455dc 196
197 my @parts = (
e48c4b9a 198 $args->{from},
199 $self->format_keyword(join '_', ($args->{type}||()), 'join'),
cbe3b5a9 200 (map +($_->{-ident} || $_->{-as} ? $_ : ('(', $_, ')')), $args->{to}),
f79455dc 201 ($args->{on} ? (
e48c4b9a 202 $self->format_keyword('on') ,
203 $args->{on},
f79455dc 204 ) : ()),
205 ($args->{using} ? (
e48c4b9a 206 $self->format_keyword('using'),
60757815 207 '(', $args->{using}, ')',
f79455dc 208 ) : ()),
209 );
59c7f80e 210 return $self->join_query_parts(' ', @parts);
f79455dc 211}
212
6990b2aa 213sub _expand_op_as {
214 my ($self, undef, $vv, $k) = @_;
01e9b916 215 my @vv = (ref($vv) eq 'ARRAY' ? @$vv : $vv);
216 $k ||= shift @vv;
217 my $ik = $self->expand_expr($k, -ident);
218 return +{ -as => [ $ik, $self->expand_expr($vv[0], -alias) ] }
219 if @vv == 1 and ref($vv[0]) eq 'HASH';
220
221 my @as = map $self->expand_expr($_, -ident), @vv;
222 return { -as => [ $ik, { -alias => \@as } ] };
6990b2aa 223}
224
225sub _render_as {
6c39a2f7 226 my ($self, undef, $args) = @_;
01e9b916 227 my ($thing, $alias) = @$args;
59c7f80e 228 return $self->join_query_parts(
8d1295c3 229 ' ',
01e9b916 230 $thing,
8d1295c3 231 $self->format_keyword('as'),
01e9b916 232 $alias,
369e7844 233 );
234}
235
236sub _render_alias {
01e9b916 237 my ($self, undef, $args) = @_;
369e7844 238 my ($as, @cols) = @$args;
239 return (@cols
59c7f80e 240 ? $self->join_query_parts('',
3e230b71 241 $as,
68a92d22 242 '(',
243 $self->join_query_parts(
244 ', ',
245 @cols
246 ),
247 ')',
369e7844 248 )
249 : $self->render_aqt($as)
6990b2aa 250 );
251}
252
af407e9a 253sub _expand_update_clause_target {
1107714b 254 my ($self, undef, $target) = @_;
af407e9a 255 +(target => $self->_expand_from_list(undef, $target));
256}
257
6d42f0b9 258sub _expand_cast {
259 my ($self, undef, $thing) = @_;
260 return { -func => [ cast => $thing ] } if ref($thing) eq 'HASH';
261 my ($cast, $to) = @{$thing};
262 +{ -func => [ cast => { -as => [
263 $self->expand_expr($cast),
264 $self->expand_expr($to, -ident),
265 ] } ] };
266}
267
268sub _expand_alias {
269 my ($self, undef, $args) = @_;
270 if (ref($args) eq 'HASH' and my $alias = $args->{-alias}) {
271 $args = $alias;
272 }
273 +{ -alias => [
274 map $self->expand_expr($_, -ident),
275 ref($args) eq 'ARRAY' ? @{$args} : $args
276 ]
277 }
278}
279
280sub _expand_with {
281 my ($self, $name, $with) = @_;
282 my (undef, $type) = split '_', $name;
283 if (ref($with) eq 'HASH') {
284 return +{
285 %$with,
286 queries => [
287 map +[
288 $self->expand_expr({ -alias => $_->[0] }, -ident),
289 $self->expand_expr($_->[1]),
290 ], @{$with->{queries}}
291 ]
292 }
293 }
294 my @with = @$with;
295 my @exp;
296 while (my ($alias, $query) = splice @with, 0, 2) {
297 push @exp, [
298 $self->expand_expr({ -alias => $alias }, -ident),
299 $self->expand_expr($query)
300 ];
301 }
302 return +(with => { ($type ? (type => $type) : ()), queries => \@exp });
303}
304
305sub _render_with {
306 my ($self, undef, $with) = @_;
307 my $q_part = $self->join_query_parts(', ',
308 map {
309 my ($alias, $query) = @$_;
310 $self->join_query_parts(' ',
311 $alias,
312 $self->format_keyword('as'),
313 $query,
314 )
315 } @{$with->{queries}}
316 );
317 return $self->join_query_parts(' ',
318 $self->format_keyword(join '_', 'with', ($with->{type}||'')),
319 $q_part,
320 );
321}
322
4b5f7259 323sub _expand_setop {
324 my ($self, $setop, $args) = @_;
325 +{ "-${setop}" => {
326 %$args,
327 queries => [ map $self->expand_expr($_), @{$args->{queries}} ],
328 } };
329}
330
acfbc601 331sub _render_setop {
332 my ($self, $setop, $args) = @_;
333 $self->join_query_parts(
334 ' '.$self->format_keyword(join '_', $setop, ($args->{type}||())).' ',
335 @{$args->{queries}}
336 );
337}
338
0f4de029 339sub _expand_clause_setop {
340 my ($self, $setop, $args) = @_;
341 my ($op, $type) = split '_', $setop;
342 +(setop => $self->expand_expr({
343 "-${op}" => {
344 ($type ? (type => $type) : ()),
345 queries => (ref($args) eq 'ARRAY' ? $args : [ $args ])
346 }
347 }));
348}
349
f8392f56 3501;