more formatting cleanup
[dbsrgits/SQL-Abstract.git] / lib / SQL / Abstract / ExtraClauses.pm
CommitLineData
f8392f56 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
c53ad93e 11sub register_defaults {
12 my $self = shift;
13 $self->next::method(@_);
f7a20100 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;
369e7844 24 $self->clauses_of(select => 'with', @clauses);
71c1b4d5 25 $self->clause_expanders(
26 'select.group_by', sub {
1107714b 27 $_[0]->_expand_maybe_list_expr($_[2], -ident)
71c1b4d5 28 },
1107714b 29 'select.having', sub { $_[0]->expand_expr($_[2]) },
71c1b4d5 30 );
01dfea60 31 foreach my $thing (qw(join from_list)) {
32 $self->expander($thing => "_expand_${thing}")
33 ->renderer($thing => "_render_${thing}")
34 }
5d6fafbe 35 $self->op_expander(as => '_expand_op_as');
36 $self->expander(as => '_expand_op_as');
37 $self->renderer(as => '_render_as');
01e9b916 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');
71c1b4d5 50
e6b86bee 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
71c1b4d5 63 $self->clause_expanders(
64 'update.from' => '_expand_select_clause_from',
65 'delete.using' => sub {
1107714b 66 +(using => $_[0]->_expand_from_list(undef, $_[2]));
71c1b4d5 67 },
68 'insert.rowvalues' => sub {
1107714b 69 +(from => $_[0]->expand_expr({ -values => $_[2] }));
71c1b4d5 70 },
71 'insert.select' => sub {
1107714b 72 +(from => $_[0]->expand_expr({ -select => $_[2] }));
71c1b4d5 73 },
74 );
37b399a8 75
f7a20100 76 # set ops
95ab9342 77 $self->wrap_expander(select => sub {
78 my $orig = shift;
79 sub {
f7a20100 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;
95ab9342 89 }
90 });
f7fd09f7 91 my $expand_setop = sub {
92 my ($self, $setop, $args) = @_;
93 +{ "-${setop}" => {
f7a20100 94 %$args,
95 queries => [ map $self->expand_expr($_), @{$args->{queries}} ],
96 } };
f7fd09f7 97 };
98 $self->expanders(map +($_ => $expand_setop), qw(union intersect except));
f7a20100 99
100 $self->clause_renderer('select.setop' => sub {
1107714b 101 my ($self, undef, $setop) = @_;
68a92d22 102 $self->render_aqt($setop);
f7a20100 103 });
104
b42692a6 105 $self->renderer($_ => sub {
106 my ($self, $setop, $args) = @_;
59c7f80e 107 $self->join_query_parts(
b42692a6 108 ' '.$self->format_keyword(join '_', $setop, ($args->{type}||())).' ',
3e230b71 109 @{$args->{queries}}
b42692a6 110 );
111 }) for qw(union intersect except);
112
bb36c26d 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 );
f7fd09f7 130
369e7844 131 $self->clause_expander('select.with' => my $with_expander = sub {
631135cd 132 my ($self, $name, $with) = @_;
133 my (undef, $type) = split '_', $name;
369e7844 134 if (ref($with) eq 'HASH') {
135 return +{
136 %$with,
09ceda11 137 queries => [
138 map +[
01e9b916 139 $self->expand_expr({ -alias => $_->[0] }, -ident),
09ceda11 140 $self->expand_expr($_->[1]),
141 ], @{$with->{queries}}
142 ]
369e7844 143 }
144 }
145 my @with = @$with;
146 my @exp;
09ceda11 147 while (my ($alias, $query) = splice @with, 0, 2) {
369e7844 148 push @exp, [
01e9b916 149 $self->expand_expr({ -alias => $alias }, -ident),
369e7844 150 $self->expand_expr($query)
151 ];
152 }
631135cd 153 return +(with => { ($type ? (type => $type) : ()), queries => \@exp });
369e7844 154 });
631135cd 155 $self->clause_expander('select.with_recursive', $with_expander);
44a6affb 156 $self->clause_renderer('select.with' => my $with_renderer = sub {
1107714b 157 my ($self, undef, $with) = @_;
68a92d22 158 my $q_part = $self->join_query_parts(', ',
369e7844 159 map {
160 my ($alias, $query) = @$_;
68a92d22 161 $self->join_query_parts(' ',
01e9b916 162 $alias,
02345985 163 $self->format_keyword('as'),
3e230b71 164 $query,
68a92d22 165 )
369e7844 166 } @{$with->{queries}}
68a92d22 167 );
59c7f80e 168 return $self->join_query_parts(' ',
02345985 169 $self->format_keyword(join '_', 'with', ($with->{type}||'')),
369e7844 170 $q_part,
369e7844 171 );
172 });
44a6affb 173 foreach my $stmt (qw(insert update delete)) {
174 $self->clauses_of($stmt => 'with', $self->clauses_of($stmt));
175 $self->clause_expander("${stmt}.$_", $with_expander)
176 for qw(with with_recursive);
177 $self->clause_renderer("${stmt}.with", $with_renderer);
178 }
7b560753 179 $self->expander(cast => sub {
61bcff30 180 return { -func => [ cast => $_[2] ] } if ref($_[2]) eq 'HASH';
7b560753 181 my ($cast, $to) = @{$_[2]};
182 +{ -func => [ cast => { -as => [
183 $self->expand_expr($cast),
184 $self->expand_expr($to, -ident),
185 ] } ] };
186 });
f7a20100 187
c53ad93e 188 return $self;
f8392f56 189}
190
f79455dc 191sub _expand_select_clause_from {
1107714b 192 my ($self, undef, $from) = @_;
f79455dc 193 +(from => $self->_expand_from_list(undef, $from));
194}
195
196sub _expand_from_list {
197 my ($self, undef, $args) = @_;
198 if (ref($args) eq 'HASH') {
09ceda11 199 return $args if $args->{-from_list};
f79455dc 200 return { -from_list => [ $self->expand_expr($args) ] };
201 }
202 my @list;
b9e35873 203 my @args = ref($args) eq 'ARRAY' ? @$args : ($args);
f79455dc 204 while (my $entry = shift @args) {
6990b2aa 205 if (!ref($entry) and $entry =~ /^-(.*)/) {
206 if ($1 eq 'as') {
207 $list[-1] = $self->expand_expr({ -as => [
208 $list[-1], map +(ref($_) eq 'ARRAY' ? @$_ : $_), shift(@args)
209 ]});
210 next;
211 }
f79455dc 212 $entry = { $entry => shift @args };
213 }
214 my $aqt = $self->expand_expr($entry, -ident);
215 if ($aqt->{-join} and not $aqt->{-join}{from}) {
216 $aqt->{-join}{from} = pop @list;
217 }
218 push @list, $aqt;
219 }
220 return { -from_list => \@list };
221}
222
223sub _expand_join {
224 my ($self, undef, $args) = @_;
225 my %proto = (
226 ref($args) eq 'HASH'
227 ? %$args
228 : (to => $args->[0], @{$args}[1..$#$args])
229 );
6990b2aa 230 if (my $as = delete $proto{as}) {
01e9b916 231 $proto{to} = $self->expand_expr({ -as => [ $proto{to}, $as ] });
6990b2aa 232 }
0891ae97 233 if (defined($proto{using}) and ref(my $using = $proto{using}) ne 'HASH') {
60757815 234 $proto{using} = [
0891ae97 235 map [ $self->expand_expr($_, -ident) ],
236 ref($using) eq 'ARRAY' ? @$using: $using
60757815 237 ];
0891ae97 238 }
f79455dc 239 my %ret = map +($_ => $self->expand_expr($proto{$_}, -ident)),
240 sort keys %proto;
241 return +{ -join => \%ret };
242}
243
244sub _render_from_list {
6c39a2f7 245 my ($self, undef, $list) = @_;
3e230b71 246 return $self->join_query_parts(', ', @$list);
f79455dc 247}
248
249sub _render_join {
6c39a2f7 250 my ($self, undef, $args) = @_;
f79455dc 251
252 my @parts = (
e48c4b9a 253 $args->{from},
254 $self->format_keyword(join '_', ($args->{type}||()), 'join'),
cbe3b5a9 255 (map +($_->{-ident} || $_->{-as} ? $_ : ('(', $_, ')')), $args->{to}),
f79455dc 256 ($args->{on} ? (
e48c4b9a 257 $self->format_keyword('on') ,
258 $args->{on},
f79455dc 259 ) : ()),
260 ($args->{using} ? (
e48c4b9a 261 $self->format_keyword('using'),
60757815 262 '(', $args->{using}, ')',
f79455dc 263 ) : ()),
264 );
59c7f80e 265 return $self->join_query_parts(' ', @parts);
f79455dc 266}
267
6990b2aa 268sub _expand_op_as {
269 my ($self, undef, $vv, $k) = @_;
01e9b916 270 my @vv = (ref($vv) eq 'ARRAY' ? @$vv : $vv);
271 $k ||= shift @vv;
272 my $ik = $self->expand_expr($k, -ident);
273 return +{ -as => [ $ik, $self->expand_expr($vv[0], -alias) ] }
274 if @vv == 1 and ref($vv[0]) eq 'HASH';
275
276 my @as = map $self->expand_expr($_, -ident), @vv;
277 return { -as => [ $ik, { -alias => \@as } ] };
6990b2aa 278}
279
280sub _render_as {
6c39a2f7 281 my ($self, undef, $args) = @_;
01e9b916 282 my ($thing, $alias) = @$args;
59c7f80e 283 return $self->join_query_parts(
8d1295c3 284 ' ',
01e9b916 285 $thing,
8d1295c3 286 $self->format_keyword('as'),
01e9b916 287 $alias,
369e7844 288 );
289}
290
291sub _render_alias {
01e9b916 292 my ($self, undef, $args) = @_;
369e7844 293 my ($as, @cols) = @$args;
294 return (@cols
59c7f80e 295 ? $self->join_query_parts('',
3e230b71 296 $as,
68a92d22 297 '(',
298 $self->join_query_parts(
299 ', ',
300 @cols
301 ),
302 ')',
369e7844 303 )
304 : $self->render_aqt($as)
6990b2aa 305 );
306}
307
af407e9a 308sub _expand_update_clause_target {
1107714b 309 my ($self, undef, $target) = @_;
af407e9a 310 +(target => $self->_expand_from_list(undef, $target));
311}
312
f8392f56 3131;