extract remaining insert clause expansion handling to methods
[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');
58ebc7fa 38
ee706e89 39 $self->clauses_of(update => sub {
40 my ($self, @clauses) = @_;
41 splice(@clauses, 2, 0, 'from');
42 @clauses;
43 });
44
45 $self->clauses_of(delete => sub {
46 my ($self, @clauses) = @_;
47 splice(@clauses, 1, 0, 'using');
48 @clauses;
49 });
50
58ebc7fa 51 $self->clause_expanders(
52 'update.from' => '_expand_select_clause_from',
53 'delete.using' => sub {
fe8b493f 54 +(using => $_[0]->_expand_from_list(undef, $_[2]));
58ebc7fa 55 },
56 'insert.rowvalues' => sub {
fe8b493f 57 +(from => $_[0]->expand_expr({ -values => $_[2] }));
58ebc7fa 58 },
59 'insert.select' => sub {
fe8b493f 60 +(from => $_[0]->expand_expr({ -select => $_[2] }));
58ebc7fa 61 },
62 );
26994fdd 63
2b0b3d43 64 # set ops
3f9899e5 65 $self->wrap_expander(select => sub {
66 my $orig = shift;
67 sub {
2b0b3d43 68 my $self = shift;
69 my $exp = $self->$orig(@_);
70 return $exp unless my $setop = (my $sel = $exp->{-select})->{setop};
71 if (my @keys = grep $sel->{$_}, @before_setop) {
72 my %inner; @inner{@keys} = delete @{$sel}{@keys};
73 unshift @{(values(%$setop))[0]{queries}},
74 { -select => \%inner };
75 }
76 return $exp;
3f9899e5 77 }
78 });
d175037f 79 my $expand_setop = sub {
80 my ($self, $setop, $args) = @_;
81 +{ "-${setop}" => {
2b0b3d43 82 %$args,
83 queries => [ map $self->expand_expr($_), @{$args->{queries}} ],
84 } };
d175037f 85 };
86 $self->expanders(map +($_ => $expand_setop), qw(union intersect except));
2b0b3d43 87
88 $self->clause_renderer('select.setop' => sub {
fe8b493f 89 my ($self, undef, $setop) = @_;
2b0b3d43 90 $self->render_aqt($setop);
91 });
92
260e0f29 93 $self->renderer($_ => sub {
94 my ($self, $setop, $args) = @_;
0236f122 95 $self->join_query_parts(
260e0f29 96 ' '.$self->format_keyword(join '_', $setop, ($args->{type}||())).' ',
a1f8b6ef 97 @{$args->{queries}}
260e0f29 98 );
99 }) for qw(union intersect except);
100
f61bfd7b 101 my $setop_expander = sub {
102 my ($self, $setop, $args) = @_;
103 my ($op, $type) = split '_', $setop;
104 +(setop => $self->expand_expr({
105 "-${op}" => {
106 ($type ? (type => $type) : ()),
107 queries => (ref($args) eq 'ARRAY' ? $args : [ $args ])
108 }
109 }));
110 };
111
112 $self->clause_expanders(
113 map +($_ => $setop_expander),
114 map "select.${_}",
115 map +($_, "${_}_all", "${_}_distinct"),
116 qw(union intersect except)
117 );
d175037f 118
1ba47f38 119 $self->clause_expander('select.with' => my $with_expander = sub {
7237eb3c 120 my ($self, $name, $with) = @_;
121 my (undef, $type) = split '_', $name;
1ba47f38 122 if (ref($with) eq 'HASH') {
123 return +{
124 %$with,
125 queries => [ map $self->expand_expr($_), @{$with->{queries}} ]
126 }
127 }
128 my @with = @$with;
129 my @exp;
130 while (my ($name, $query) = splice @with, 0, 2) {
131 my @n = map $self->expand_expr($_, -ident),
132 ref($name) eq 'ARRAY' ? @$name : $name;
133 push @exp, [
134 \@n,
135 $self->expand_expr($query)
136 ];
137 }
7237eb3c 138 return +(with => { ($type ? (type => $type) : ()), queries => \@exp });
1ba47f38 139 });
7237eb3c 140 $self->clause_expander('select.with_recursive', $with_expander);
1ba47f38 141 $self->clause_renderer('select.with' => sub {
fe8b493f 142 my ($self, undef, $with) = @_;
0236f122 143 my $q_part = [ $self->join_query_parts(', ',
1ba47f38 144 map {
145 my ($alias, $query) = @$_;
0236f122 146 [ $self->join_query_parts(' ',
1ba47f38 147 [ $self->_render_alias($alias) ],
148 [ $self->format_keyword('as') ],
a1f8b6ef 149 $query,
1ba47f38 150 ) ]
151 } @{$with->{queries}}
152 ) ];
0236f122 153 return $self->join_query_parts(' ',
541af914 154 [ $self->format_keyword(join '_', 'with', ($with->{type}||'')) ],
1ba47f38 155 $q_part,
1ba47f38 156 );
157 });
2b0b3d43 158
b4546197 159 return $self;
1ff9018c 160}
161
7741b7ad 162sub _expand_select_clause_from {
fe8b493f 163 my ($self, undef, $from) = @_;
7741b7ad 164 +(from => $self->_expand_from_list(undef, $from));
165}
166
167sub _expand_from_list {
168 my ($self, undef, $args) = @_;
169 if (ref($args) eq 'HASH') {
170 return { -from_list => [ $self->expand_expr($args) ] };
171 }
172 my @list;
86a6ebf4 173 my @args = ref($args) eq 'ARRAY' ? @$args : ($args);
7741b7ad 174 while (my $entry = shift @args) {
b99e9a14 175 if (!ref($entry) and $entry =~ /^-(.*)/) {
176 if ($1 eq 'as') {
177 $list[-1] = $self->expand_expr({ -as => [
178 $list[-1], map +(ref($_) eq 'ARRAY' ? @$_ : $_), shift(@args)
179 ]});
180 next;
181 }
7741b7ad 182 $entry = { $entry => shift @args };
183 }
184 my $aqt = $self->expand_expr($entry, -ident);
185 if ($aqt->{-join} and not $aqt->{-join}{from}) {
186 $aqt->{-join}{from} = pop @list;
187 }
188 push @list, $aqt;
189 }
190 return { -from_list => \@list };
191}
192
193sub _expand_join {
194 my ($self, undef, $args) = @_;
195 my %proto = (
196 ref($args) eq 'HASH'
197 ? %$args
198 : (to => $args->[0], @{$args}[1..$#$args])
199 );
b99e9a14 200 if (my $as = delete $proto{as}) {
201 $proto{to} = { -as => [ $proto{to}, ref($as) eq 'ARRAY' ? @$as : $as ] };
202 }
e0eb8d26 203 if (defined($proto{using}) and ref(my $using = $proto{using}) ne 'HASH') {
204 $proto{using} = { -row => [
205 map [ $self->expand_expr($_, -ident) ],
206 ref($using) eq 'ARRAY' ? @$using: $using
207 ] };
208 }
7741b7ad 209 my %ret = map +($_ => $self->expand_expr($proto{$_}, -ident)),
210 sort keys %proto;
211 return +{ -join => \%ret };
212}
213
214sub _render_from_list {
a01911a2 215 my ($self, undef, $list) = @_;
a1f8b6ef 216 return $self->join_query_parts(', ', @$list);
7741b7ad 217}
218
219sub _render_join {
a01911a2 220 my ($self, undef, $args) = @_;
7741b7ad 221
222 my @parts = (
223 [ $self->render_aqt($args->{from}) ],
2b0b3d43 224 [ $self->format_keyword(join '_', ($args->{type}||()), 'join') ],
4a2c263b 225 [ $self->render_aqt(
e0eb8d26 226 map +($_->{-ident} || $_->{-as} ? $_ : { -row => [ $_ ] }), $args->{to}
4a2c263b 227 ) ],
7741b7ad 228 ($args->{on} ? (
2b0b3d43 229 [ $self->format_keyword('on') ],
7741b7ad 230 [ $self->render_aqt($args->{on}) ],
231 ) : ()),
232 ($args->{using} ? (
2b0b3d43 233 [ $self->format_keyword('using') ],
7741b7ad 234 [ $self->render_aqt($args->{using}) ],
235 ) : ()),
236 );
0236f122 237 return $self->join_query_parts(' ', @parts);
7741b7ad 238}
239
b99e9a14 240sub _expand_op_as {
241 my ($self, undef, $vv, $k) = @_;
242 my @as = map $self->expand_expr($_, -ident),
243 (defined($k) ? ($k) : ()), ref($vv) eq 'ARRAY' ? @$vv : $vv;
244 return { -as => \@as };
245}
246
247sub _render_as {
a01911a2 248 my ($self, undef, $args) = @_;
1ba47f38 249 my ($thing, @alias) = @$args;
0236f122 250 return $self->join_query_parts(
b99e9a14 251 ' ',
01222ad2 252 [ $self->render_aqt($thing) ],
2b0b3d43 253 [ $self->format_keyword('as') ],
1ba47f38 254 [ $self->_render_alias(\@alias) ],
255 );
256}
257
258sub _render_alias {
259 my ($self, $args) = @_;
260 my ($as, @cols) = @$args;
261 return (@cols
0236f122 262 ? $self->join_query_parts('',
a1f8b6ef 263 $as,
1ba47f38 264 [ '(' ],
0236f122 265 [ $self->join_query_parts(
1ba47f38 266 ', ',
a1f8b6ef 267 @cols
1ba47f38 268 ) ],
269 [ ')' ],
270 )
271 : $self->render_aqt($as)
b99e9a14 272 );
273}
274
f9f1fdcd 275sub _expand_update_clause_target {
fe8b493f 276 my ($self, undef, $target) = @_;
f9f1fdcd 277 +(target => $self->_expand_from_list(undef, $target));
278}
279
1ff9018c 2801;