unop expander wrapper conversion
[scpubgit/Q-Branch.git] / lib / SQL / Abstract / ExtraClauses.pm
CommitLineData
1ff9018c 1package SQL::Abstract::ExtraClauses;
2
df5d0507 3use Moo;
4
5has sqla => (
6 is => 'ro', init_arg => undef,
7 handles => [ qw(
b99e393b 8 expand_expr expand_maybe_list_expr render_aqt
df5d0507 9 format_keyword join_query_parts
10 ) ],
11);
1ff9018c 12
df5d0507 13sub cb {
c671eba6 14 my ($self, $method, @args) = @_;
15 return sub {
16 local $self->{sqla} = shift;
17 $self->$method(@args, @_)
18 };
df5d0507 19}
20
21sub apply_to {
22 my ($self, $sqla) = @_;
23 $self = $self->new unless ref($self);
ad078c71 24 my @clauses = $sqla->clauses_of('select');
2b0b3d43 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;
ad078c71 34 $sqla->clauses_of(select => 'with', @clauses);
35 $sqla->clause_expanders(
df5d0507 36 'select.group_by', $self->cb(sub {
b99e393b 37 $_[0]->expand_maybe_list_expr($_[2], -ident)
df5d0507 38 }),
39 'select.having', $self->cb(sub { $_[0]->expand_expr($_[2]) }),
58ebc7fa 40 );
a6227174 41 foreach my $thing (qw(join from_list)) {
ad078c71 42 $sqla->expander($thing => $self->cb("_expand_${thing}"))
df5d0507 43 ->renderer($thing => $self->cb("_render_${thing}"))
a6227174 44 }
ad078c71 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'));
2f4717ad 48 $sqla->expander(alias => $self->cb('_expand_alias'));
ad078c71 49 $sqla->renderer(alias => $self->cb('_render_alias'));
58ebc7fa 50
ad078c71 51 $sqla->clauses_of(update => sub {
ee706e89 52 my ($self, @clauses) = @_;
53 splice(@clauses, 2, 0, 'from');
54 @clauses;
55 });
56
ad078c71 57 $sqla->clauses_of(delete => sub {
ee706e89 58 my ($self, @clauses) = @_;
59 splice(@clauses, 1, 0, 'using');
60 @clauses;
61 });
62
ad078c71 63 $sqla->clause_expanders(
805c64f1 64 'update.from' => $self->cb('_expand_from_list'),
9a20a32d 65 'delete.using' => $self->cb('_expand_from_list'),
df5d0507 66 'insert.rowvalues' => $self->cb(sub {
fe8b493f 67 +(from => $_[0]->expand_expr({ -values => $_[2] }));
df5d0507 68 }),
69 'insert.select' => $self->cb(sub {
fe8b493f 70 +(from => $_[0]->expand_expr({ -select => $_[2] }));
df5d0507 71 }),
58ebc7fa 72 );
26994fdd 73
2b0b3d43 74 # set ops
ad078c71 75 $sqla->wrap_expander(select => sub {
c671eba6 76 $self->cb('_expand_select', $_[0], \@before_setop);
3f9899e5 77 });
2b0b3d43 78
e2db8228 79 $sqla->clause_renderer(
80 'select.setop' => $self->cb(sub { $_[0]->render_aqt($_[2]); })
81 );
2b0b3d43 82
51046d0e 83 foreach my $setop (qw(union intersect except)) {
84 $sqla->expander($setop => $self->cb('_expand_setop'));
85 $sqla->renderer($setop => $self->cb('_render_setop'));
86 }
260e0f29 87
41086177 88 my $setop_expander = $self->cb('_expand_clause_setop');
f61bfd7b 89
ad078c71 90 $sqla->clause_expanders(
f61bfd7b 91 map +($_ => $setop_expander),
92 map "select.${_}",
93 map +($_, "${_}_all", "${_}_distinct"),
94 qw(union intersect except)
95 );
d175037f 96
2f4717ad 97 my $w_exp = $self->cb('_expand_with');
98 my $w_rdr = $self->cb('_render_with');
99 $sqla->clause_expander('select.with' => $w_exp);
100 $sqla->clause_expander('select.with_recursive' => $w_exp);
101 $sqla->clause_renderer('select.with' => $w_rdr);
102
a97ecd95 103 foreach my $stmt (qw(insert update delete)) {
ad078c71 104 $sqla->clauses_of($stmt => 'with', $sqla->clauses_of($stmt));
2f4717ad 105 $sqla->clause_expander("${stmt}.$_" => $w_exp)
a97ecd95 106 for qw(with with_recursive);
2f4717ad 107 $sqla->clause_renderer("${stmt}.with" => $w_rdr);
a97ecd95 108 }
2f4717ad 109
110 $sqla->expander(cast => $self->cb('_expand_cast'));
df5d0507 111
ad078c71 112 $sqla->clause_expanders(
805c64f1 113 "select.from", $self->cb('_expand_from_list'),
df5d0507 114 "update.target", $self->cb('_expand_update_clause_target'),
115 "update.update", $self->cb('_expand_update_clause_target'),
116 );
2b0b3d43 117
ad078c71 118 return $sqla;
1ff9018c 119}
120
c671eba6 121sub _expand_select {
093442c8 122 my ($self, $orig, $before_setop, @args) = @_;
123 my $exp = $self->sqla->$orig(@args);
c671eba6 124 return $exp unless my $setop = (my $sel = $exp->{-select})->{setop};
125 if (my @keys = grep $sel->{$_}, @$before_setop) {
126 my %inner; @inner{@keys} = delete @{$sel}{@keys};
127 unshift @{(values(%$setop))[0]{queries}},
128 { -select => \%inner };
129 }
130 return $exp;
131}
132
7741b7ad 133sub _expand_from_list {
134 my ($self, undef, $args) = @_;
135 if (ref($args) eq 'HASH') {
38e67490 136 return $args if $args->{-from_list};
7741b7ad 137 return { -from_list => [ $self->expand_expr($args) ] };
138 }
139 my @list;
86a6ebf4 140 my @args = ref($args) eq 'ARRAY' ? @$args : ($args);
7741b7ad 141 while (my $entry = shift @args) {
b99e9a14 142 if (!ref($entry) and $entry =~ /^-(.*)/) {
143 if ($1 eq 'as') {
144 $list[-1] = $self->expand_expr({ -as => [
145 $list[-1], map +(ref($_) eq 'ARRAY' ? @$_ : $_), shift(@args)
146 ]});
147 next;
148 }
7741b7ad 149 $entry = { $entry => shift @args };
150 }
151 my $aqt = $self->expand_expr($entry, -ident);
152 if ($aqt->{-join} and not $aqt->{-join}{from}) {
153 $aqt->{-join}{from} = pop @list;
154 }
155 push @list, $aqt;
156 }
157 return { -from_list => \@list };
158}
159
160sub _expand_join {
161 my ($self, undef, $args) = @_;
162 my %proto = (
163 ref($args) eq 'HASH'
164 ? %$args
165 : (to => $args->[0], @{$args}[1..$#$args])
166 );
b99e9a14 167 if (my $as = delete $proto{as}) {
984db0d7 168 $proto{to} = $self->expand_expr({ -as => [ $proto{to}, $as ] });
b99e9a14 169 }
e0eb8d26 170 if (defined($proto{using}) and ref(my $using = $proto{using}) ne 'HASH') {
13c99dad 171 $proto{using} = [
e0eb8d26 172 map [ $self->expand_expr($_, -ident) ],
173 ref($using) eq 'ARRAY' ? @$using: $using
13c99dad 174 ];
e0eb8d26 175 }
7741b7ad 176 my %ret = map +($_ => $self->expand_expr($proto{$_}, -ident)),
177 sort keys %proto;
178 return +{ -join => \%ret };
179}
180
181sub _render_from_list {
a01911a2 182 my ($self, undef, $list) = @_;
a1f8b6ef 183 return $self->join_query_parts(', ', @$list);
7741b7ad 184}
185
186sub _render_join {
a01911a2 187 my ($self, undef, $args) = @_;
7741b7ad 188
189 my @parts = (
412f9efe 190 $args->{from},
191 $self->format_keyword(join '_', ($args->{type}||()), 'join'),
51ccda04 192 (map +($_->{-ident} || $_->{-as} ? $_ : ('(', $_, ')')), $args->{to}),
7741b7ad 193 ($args->{on} ? (
412f9efe 194 $self->format_keyword('on') ,
195 $args->{on},
7741b7ad 196 ) : ()),
197 ($args->{using} ? (
412f9efe 198 $self->format_keyword('using'),
13c99dad 199 '(', $args->{using}, ')',
7741b7ad 200 ) : ()),
201 );
0236f122 202 return $self->join_query_parts(' ', @parts);
7741b7ad 203}
204
b99e9a14 205sub _expand_op_as {
206 my ($self, undef, $vv, $k) = @_;
984db0d7 207 my @vv = (ref($vv) eq 'ARRAY' ? @$vv : $vv);
208 $k ||= shift @vv;
209 my $ik = $self->expand_expr($k, -ident);
210 return +{ -as => [ $ik, $self->expand_expr($vv[0], -alias) ] }
211 if @vv == 1 and ref($vv[0]) eq 'HASH';
212
213 my @as = map $self->expand_expr($_, -ident), @vv;
214 return { -as => [ $ik, { -alias => \@as } ] };
b99e9a14 215}
216
217sub _render_as {
a01911a2 218 my ($self, undef, $args) = @_;
984db0d7 219 my ($thing, $alias) = @$args;
0236f122 220 return $self->join_query_parts(
ac7992be 221 ' ',
984db0d7 222 $thing,
ac7992be 223 $self->format_keyword('as'),
984db0d7 224 $alias,
1ba47f38 225 );
226}
227
228sub _render_alias {
984db0d7 229 my ($self, undef, $args) = @_;
1ba47f38 230 my ($as, @cols) = @$args;
231 return (@cols
0236f122 232 ? $self->join_query_parts('',
a1f8b6ef 233 $as,
3f312d2e 234 '(',
235 $self->join_query_parts(
236 ', ',
237 @cols
238 ),
239 ')',
1ba47f38 240 )
241 : $self->render_aqt($as)
b99e9a14 242 );
243}
244
f9f1fdcd 245sub _expand_update_clause_target {
fe8b493f 246 my ($self, undef, $target) = @_;
f9f1fdcd 247 +(target => $self->_expand_from_list(undef, $target));
248}
249
2f4717ad 250sub _expand_cast {
251 my ($self, undef, $thing) = @_;
252 return { -func => [ cast => $thing ] } if ref($thing) eq 'HASH';
253 my ($cast, $to) = @{$thing};
254 +{ -func => [ cast => { -as => [
255 $self->expand_expr($cast),
256 $self->expand_expr($to, -ident),
257 ] } ] };
258}
259
260sub _expand_alias {
261 my ($self, undef, $args) = @_;
262 if (ref($args) eq 'HASH' and my $alias = $args->{-alias}) {
263 $args = $alias;
264 }
265 +{ -alias => [
266 map $self->expand_expr($_, -ident),
267 ref($args) eq 'ARRAY' ? @{$args} : $args
268 ]
269 }
270}
271
272sub _expand_with {
273 my ($self, $name, $with) = @_;
274 my (undef, $type) = split '_', $name;
275 if (ref($with) eq 'HASH') {
276 return +{
277 %$with,
278 queries => [
279 map +[
280 $self->expand_expr({ -alias => $_->[0] }, -ident),
281 $self->expand_expr($_->[1]),
282 ], @{$with->{queries}}
283 ]
284 }
285 }
286 my @with = @$with;
287 my @exp;
288 while (my ($alias, $query) = splice @with, 0, 2) {
289 push @exp, [
290 $self->expand_expr({ -alias => $alias }, -ident),
291 $self->expand_expr($query)
292 ];
293 }
294 return +(with => { ($type ? (type => $type) : ()), queries => \@exp });
295}
296
297sub _render_with {
298 my ($self, undef, $with) = @_;
299 my $q_part = $self->join_query_parts(', ',
300 map {
301 my ($alias, $query) = @$_;
302 $self->join_query_parts(' ',
303 $alias,
304 $self->format_keyword('as'),
305 $query,
306 )
307 } @{$with->{queries}}
308 );
309 return $self->join_query_parts(' ',
310 $self->format_keyword(join '_', 'with', ($with->{type}||'')),
311 $q_part,
312 );
313}
314
51046d0e 315sub _expand_setop {
316 my ($self, $setop, $args) = @_;
317 +{ "-${setop}" => {
318 %$args,
319 queries => [ map $self->expand_expr($_), @{$args->{queries}} ],
320 } };
321}
322
b5f4a869 323sub _render_setop {
324 my ($self, $setop, $args) = @_;
325 $self->join_query_parts(
326 ' '.$self->format_keyword(join '_', $setop, ($args->{type}||())).' ',
327 @{$args->{queries}}
328 );
329}
330
41086177 331sub _expand_clause_setop {
332 my ($self, $setop, $args) = @_;
333 my ($op, $type) = split '_', $setop;
334 +(setop => $self->expand_expr({
335 "-${op}" => {
336 ($type ? (type => $type) : ()),
337 queries => (ref($args) eq 'ARRAY' ? $args : [ $args ])
338 }
339 }));
340}
341
1ff9018c 3421;