Commit | Line | Data |
f8392f56 |
1 | package SQL::Abstract::ExtraClauses; |
2 | |
b40d30dc |
3 | use Moo; |
4 | |
5 | has sqla => ( |
6 | is => 'ro', init_arg => undef, |
7 | handles => [ qw( |
8 | expand_expr render_aqt |
b40d30dc |
9 | format_keyword join_query_parts |
10 | ) ], |
11 | ); |
f8392f56 |
12 | |
13 | BEGIN { *puke = \&SQL::Abstract::puke } |
14 | |
b40d30dc |
15 | sub cb { |
16 | my ($self, $method) = @_; |
17 | return sub { local $self->{sqla} = shift; $self->$method(@_) }; |
18 | } |
19 | |
20 | sub apply_to { |
21 | my ($self, $sqla) = @_; |
22 | $self = $self->new unless ref($self); |
ac3616e8 |
23 | my @clauses = $sqla->clauses_of('select'); |
f7a20100 |
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; |
ac3616e8 |
33 | $sqla->clauses_of(select => 'with', @clauses); |
34 | $sqla->clause_expanders( |
b40d30dc |
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]) }), |
71c1b4d5 |
39 | ); |
01dfea60 |
40 | foreach my $thing (qw(join from_list)) { |
ac3616e8 |
41 | $sqla->expander($thing => $self->cb("_expand_${thing}")) |
b40d30dc |
42 | ->renderer($thing => $self->cb("_render_${thing}")) |
01dfea60 |
43 | } |
ac3616e8 |
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 { |
01e9b916 |
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 | } |
b40d30dc |
57 | })); |
ac3616e8 |
58 | $sqla->renderer(alias => $self->cb('_render_alias')); |
71c1b4d5 |
59 | |
ac3616e8 |
60 | $sqla->clauses_of(update => sub { |
e6b86bee |
61 | my ($self, @clauses) = @_; |
62 | splice(@clauses, 2, 0, 'from'); |
63 | @clauses; |
64 | }); |
65 | |
ac3616e8 |
66 | $sqla->clauses_of(delete => sub { |
e6b86bee |
67 | my ($self, @clauses) = @_; |
68 | splice(@clauses, 1, 0, 'using'); |
69 | @clauses; |
70 | }); |
71 | |
ac3616e8 |
72 | $sqla->clause_expanders( |
b40d30dc |
73 | 'update.from' => $self->cb('_expand_select_clause_from'), |
74 | 'delete.using' => $self->cb(sub { |
1107714b |
75 | +(using => $_[0]->_expand_from_list(undef, $_[2])); |
b40d30dc |
76 | }), |
77 | 'insert.rowvalues' => $self->cb(sub { |
1107714b |
78 | +(from => $_[0]->expand_expr({ -values => $_[2] })); |
b40d30dc |
79 | }), |
80 | 'insert.select' => $self->cb(sub { |
1107714b |
81 | +(from => $_[0]->expand_expr({ -select => $_[2] })); |
b40d30dc |
82 | }), |
71c1b4d5 |
83 | ); |
37b399a8 |
84 | |
f7a20100 |
85 | # set ops |
ac3616e8 |
86 | $sqla->wrap_expander(select => sub { |
95ab9342 |
87 | my $orig = shift; |
b40d30dc |
88 | $self->cb(sub { |
f7a20100 |
89 | my $self = shift; |
b40d30dc |
90 | my $exp = $self->sqla->$orig(@_); |
f7a20100 |
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; |
b40d30dc |
98 | }); |
95ab9342 |
99 | }); |
b40d30dc |
100 | my $expand_setop = $self->cb(sub { |
f7fd09f7 |
101 | my ($self, $setop, $args) = @_; |
102 | +{ "-${setop}" => { |
f7a20100 |
103 | %$args, |
104 | queries => [ map $self->expand_expr($_), @{$args->{queries}} ], |
105 | } }; |
b40d30dc |
106 | }); |
ac3616e8 |
107 | $sqla->expanders(map +($_ => $expand_setop), qw(union intersect except)); |
f7a20100 |
108 | |
ac3616e8 |
109 | $sqla->clause_renderer('select.setop' => $self->cb(sub { |
1107714b |
110 | my ($self, undef, $setop) = @_; |
68a92d22 |
111 | $self->render_aqt($setop); |
b40d30dc |
112 | })); |
f7a20100 |
113 | |
ac3616e8 |
114 | $sqla->renderer($_ => $self->cb(sub { |
b42692a6 |
115 | my ($self, $setop, $args) = @_; |
59c7f80e |
116 | $self->join_query_parts( |
b42692a6 |
117 | ' '.$self->format_keyword(join '_', $setop, ($args->{type}||())).' ', |
3e230b71 |
118 | @{$args->{queries}} |
b42692a6 |
119 | ); |
b40d30dc |
120 | })) for qw(union intersect except); |
b42692a6 |
121 | |
b40d30dc |
122 | my $setop_expander = $self->cb(sub { |
bb36c26d |
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 | })); |
b40d30dc |
131 | }); |
bb36c26d |
132 | |
ac3616e8 |
133 | $sqla->clause_expanders( |
bb36c26d |
134 | map +($_ => $setop_expander), |
135 | map "select.${_}", |
136 | map +($_, "${_}_all", "${_}_distinct"), |
137 | qw(union intersect except) |
138 | ); |
f7fd09f7 |
139 | |
ac3616e8 |
140 | $sqla->clause_expander('select.with' => my $with_expander = $self->cb(sub { |
631135cd |
141 | my ($self, $name, $with) = @_; |
142 | my (undef, $type) = split '_', $name; |
369e7844 |
143 | if (ref($with) eq 'HASH') { |
144 | return +{ |
145 | %$with, |
09ceda11 |
146 | queries => [ |
147 | map +[ |
01e9b916 |
148 | $self->expand_expr({ -alias => $_->[0] }, -ident), |
09ceda11 |
149 | $self->expand_expr($_->[1]), |
150 | ], @{$with->{queries}} |
151 | ] |
369e7844 |
152 | } |
153 | } |
154 | my @with = @$with; |
155 | my @exp; |
09ceda11 |
156 | while (my ($alias, $query) = splice @with, 0, 2) { |
369e7844 |
157 | push @exp, [ |
01e9b916 |
158 | $self->expand_expr({ -alias => $alias }, -ident), |
369e7844 |
159 | $self->expand_expr($query) |
160 | ]; |
161 | } |
631135cd |
162 | return +(with => { ($type ? (type => $type) : ()), queries => \@exp }); |
b40d30dc |
163 | })); |
ac3616e8 |
164 | $sqla->clause_expander('select.with_recursive', $with_expander); |
165 | $sqla->clause_renderer('select.with' => my $with_renderer = $self->cb(sub { |
1107714b |
166 | my ($self, undef, $with) = @_; |
68a92d22 |
167 | my $q_part = $self->join_query_parts(', ', |
369e7844 |
168 | map { |
169 | my ($alias, $query) = @$_; |
68a92d22 |
170 | $self->join_query_parts(' ', |
01e9b916 |
171 | $alias, |
02345985 |
172 | $self->format_keyword('as'), |
3e230b71 |
173 | $query, |
68a92d22 |
174 | ) |
369e7844 |
175 | } @{$with->{queries}} |
68a92d22 |
176 | ); |
59c7f80e |
177 | return $self->join_query_parts(' ', |
02345985 |
178 | $self->format_keyword(join '_', 'with', ($with->{type}||'')), |
369e7844 |
179 | $q_part, |
369e7844 |
180 | ); |
b40d30dc |
181 | })); |
44a6affb |
182 | foreach my $stmt (qw(insert update delete)) { |
ac3616e8 |
183 | $sqla->clauses_of($stmt => 'with', $sqla->clauses_of($stmt)); |
184 | $sqla->clause_expander("${stmt}.$_", $with_expander) |
44a6affb |
185 | for qw(with with_recursive); |
ac3616e8 |
186 | $sqla->clause_renderer("${stmt}.with", $with_renderer); |
44a6affb |
187 | } |
ac3616e8 |
188 | $sqla->expander(cast => $self->cb(sub { |
61bcff30 |
189 | return { -func => [ cast => $_[2] ] } if ref($_[2]) eq 'HASH'; |
7b560753 |
190 | my ($cast, $to) = @{$_[2]}; |
191 | +{ -func => [ cast => { -as => [ |
192 | $self->expand_expr($cast), |
193 | $self->expand_expr($to, -ident), |
194 | ] } ] }; |
b40d30dc |
195 | })); |
196 | |
ac3616e8 |
197 | $sqla->clause_expanders( |
b40d30dc |
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 | ); |
f7a20100 |
202 | |
ac3616e8 |
203 | return $sqla; |
f8392f56 |
204 | } |
205 | |
f79455dc |
206 | sub _expand_select_clause_from { |
1107714b |
207 | my ($self, undef, $from) = @_; |
f79455dc |
208 | +(from => $self->_expand_from_list(undef, $from)); |
209 | } |
210 | |
211 | sub _expand_from_list { |
212 | my ($self, undef, $args) = @_; |
213 | if (ref($args) eq 'HASH') { |
09ceda11 |
214 | return $args if $args->{-from_list}; |
f79455dc |
215 | return { -from_list => [ $self->expand_expr($args) ] }; |
216 | } |
217 | my @list; |
b9e35873 |
218 | my @args = ref($args) eq 'ARRAY' ? @$args : ($args); |
f79455dc |
219 | while (my $entry = shift @args) { |
6990b2aa |
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 | } |
f79455dc |
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 | |
238 | sub _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 | ); |
6990b2aa |
245 | if (my $as = delete $proto{as}) { |
01e9b916 |
246 | $proto{to} = $self->expand_expr({ -as => [ $proto{to}, $as ] }); |
6990b2aa |
247 | } |
0891ae97 |
248 | if (defined($proto{using}) and ref(my $using = $proto{using}) ne 'HASH') { |
60757815 |
249 | $proto{using} = [ |
0891ae97 |
250 | map [ $self->expand_expr($_, -ident) ], |
251 | ref($using) eq 'ARRAY' ? @$using: $using |
60757815 |
252 | ]; |
0891ae97 |
253 | } |
f79455dc |
254 | my %ret = map +($_ => $self->expand_expr($proto{$_}, -ident)), |
255 | sort keys %proto; |
256 | return +{ -join => \%ret }; |
257 | } |
258 | |
259 | sub _render_from_list { |
6c39a2f7 |
260 | my ($self, undef, $list) = @_; |
3e230b71 |
261 | return $self->join_query_parts(', ', @$list); |
f79455dc |
262 | } |
263 | |
264 | sub _render_join { |
6c39a2f7 |
265 | my ($self, undef, $args) = @_; |
f79455dc |
266 | |
267 | my @parts = ( |
e48c4b9a |
268 | $args->{from}, |
269 | $self->format_keyword(join '_', ($args->{type}||()), 'join'), |
cbe3b5a9 |
270 | (map +($_->{-ident} || $_->{-as} ? $_ : ('(', $_, ')')), $args->{to}), |
f79455dc |
271 | ($args->{on} ? ( |
e48c4b9a |
272 | $self->format_keyword('on') , |
273 | $args->{on}, |
f79455dc |
274 | ) : ()), |
275 | ($args->{using} ? ( |
e48c4b9a |
276 | $self->format_keyword('using'), |
60757815 |
277 | '(', $args->{using}, ')', |
f79455dc |
278 | ) : ()), |
279 | ); |
59c7f80e |
280 | return $self->join_query_parts(' ', @parts); |
f79455dc |
281 | } |
282 | |
6990b2aa |
283 | sub _expand_op_as { |
284 | my ($self, undef, $vv, $k) = @_; |
01e9b916 |
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 } ] }; |
6990b2aa |
293 | } |
294 | |
295 | sub _render_as { |
6c39a2f7 |
296 | my ($self, undef, $args) = @_; |
01e9b916 |
297 | my ($thing, $alias) = @$args; |
59c7f80e |
298 | return $self->join_query_parts( |
8d1295c3 |
299 | ' ', |
01e9b916 |
300 | $thing, |
8d1295c3 |
301 | $self->format_keyword('as'), |
01e9b916 |
302 | $alias, |
369e7844 |
303 | ); |
304 | } |
305 | |
306 | sub _render_alias { |
01e9b916 |
307 | my ($self, undef, $args) = @_; |
369e7844 |
308 | my ($as, @cols) = @$args; |
309 | return (@cols |
59c7f80e |
310 | ? $self->join_query_parts('', |
3e230b71 |
311 | $as, |
68a92d22 |
312 | '(', |
313 | $self->join_query_parts( |
314 | ', ', |
315 | @cols |
316 | ), |
317 | ')', |
369e7844 |
318 | ) |
319 | : $self->render_aqt($as) |
6990b2aa |
320 | ); |
321 | } |
322 | |
af407e9a |
323 | sub _expand_update_clause_target { |
1107714b |
324 | my ($self, undef, $target) = @_; |
af407e9a |
325 | +(target => $self->_expand_from_list(undef, $target)); |
326 | } |
327 | |
f8392f56 |
328 | 1; |