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