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