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