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