Commit | Line | Data |
f8392f56 |
1 | package SQL::Abstract::ExtraClauses; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | use if $] < '5.010', 'MRO::Compat'; |
6 | use mro 'c3'; |
7 | use base qw(SQL::Abstract::Clauses); |
8 | |
9 | BEGIN { *puke = \&SQL::Abstract::puke } |
10 | |
c53ad93e |
11 | sub 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'); |
71c1b4d5 |
38 | |
e6b86bee |
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 | |
71c1b4d5 |
51 | $self->clause_expanders( |
52 | 'update.from' => '_expand_select_clause_from', |
53 | 'delete.using' => sub { |
1107714b |
54 | +(using => $_[0]->_expand_from_list(undef, $_[2])); |
71c1b4d5 |
55 | }, |
56 | 'insert.rowvalues' => sub { |
1107714b |
57 | +(from => $_[0]->expand_expr({ -values => $_[2] })); |
71c1b4d5 |
58 | }, |
59 | 'insert.select' => sub { |
1107714b |
60 | +(from => $_[0]->expand_expr({ -select => $_[2] })); |
71c1b4d5 |
61 | }, |
62 | ); |
37b399a8 |
63 | |
f7a20100 |
64 | # set ops |
95ab9342 |
65 | $self->wrap_expander(select => sub { |
66 | my $orig = shift; |
67 | sub { |
f7a20100 |
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; |
95ab9342 |
77 | } |
78 | }); |
f7fd09f7 |
79 | my $expand_setop = sub { |
80 | my ($self, $setop, $args) = @_; |
81 | +{ "-${setop}" => { |
f7a20100 |
82 | %$args, |
83 | queries => [ map $self->expand_expr($_), @{$args->{queries}} ], |
84 | } }; |
f7fd09f7 |
85 | }; |
86 | $self->expanders(map +($_ => $expand_setop), qw(union intersect except)); |
f7a20100 |
87 | |
88 | $self->clause_renderer('select.setop' => sub { |
1107714b |
89 | my ($self, undef, $setop) = @_; |
f7a20100 |
90 | $self->render_aqt($setop); |
91 | }); |
92 | |
b42692a6 |
93 | $self->renderer($_ => sub { |
94 | my ($self, $setop, $args) = @_; |
59c7f80e |
95 | $self->join_query_parts( |
b42692a6 |
96 | ' '.$self->format_keyword(join '_', $setop, ($args->{type}||())).' ', |
3e230b71 |
97 | @{$args->{queries}} |
b42692a6 |
98 | ); |
99 | }) for qw(union intersect except); |
100 | |
bb36c26d |
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 | ); |
f7fd09f7 |
118 | |
369e7844 |
119 | $self->clause_expander('select.with' => my $with_expander = sub { |
631135cd |
120 | my ($self, $name, $with) = @_; |
121 | my (undef, $type) = split '_', $name; |
369e7844 |
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 | } |
631135cd |
138 | return +(with => { ($type ? (type => $type) : ()), queries => \@exp }); |
369e7844 |
139 | }); |
631135cd |
140 | $self->clause_expander('select.with_recursive', $with_expander); |
369e7844 |
141 | $self->clause_renderer('select.with' => sub { |
1107714b |
142 | my ($self, undef, $with) = @_; |
59c7f80e |
143 | my $q_part = [ $self->join_query_parts(', ', |
369e7844 |
144 | map { |
145 | my ($alias, $query) = @$_; |
59c7f80e |
146 | [ $self->join_query_parts(' ', |
369e7844 |
147 | [ $self->_render_alias($alias) ], |
148 | [ $self->format_keyword('as') ], |
3e230b71 |
149 | $query, |
369e7844 |
150 | ) ] |
151 | } @{$with->{queries}} |
152 | ) ]; |
59c7f80e |
153 | return $self->join_query_parts(' ', |
aed9ebda |
154 | [ $self->format_keyword(join '_', 'with', ($with->{type}||'')) ], |
369e7844 |
155 | $q_part, |
369e7844 |
156 | ); |
157 | }); |
f7a20100 |
158 | |
c53ad93e |
159 | return $self; |
f8392f56 |
160 | } |
161 | |
f7a20100 |
162 | sub format_keyword { $_[0]->_sqlcase(join ' ', split '_', $_[1]) } |
163 | |
f79455dc |
164 | sub _expand_select_clause_from { |
1107714b |
165 | my ($self, undef, $from) = @_; |
f79455dc |
166 | +(from => $self->_expand_from_list(undef, $from)); |
167 | } |
168 | |
169 | sub _expand_from_list { |
170 | my ($self, undef, $args) = @_; |
171 | if (ref($args) eq 'HASH') { |
172 | return { -from_list => [ $self->expand_expr($args) ] }; |
173 | } |
174 | my @list; |
b9e35873 |
175 | my @args = ref($args) eq 'ARRAY' ? @$args : ($args); |
f79455dc |
176 | while (my $entry = shift @args) { |
6990b2aa |
177 | if (!ref($entry) and $entry =~ /^-(.*)/) { |
178 | if ($1 eq 'as') { |
179 | $list[-1] = $self->expand_expr({ -as => [ |
180 | $list[-1], map +(ref($_) eq 'ARRAY' ? @$_ : $_), shift(@args) |
181 | ]}); |
182 | next; |
183 | } |
f79455dc |
184 | $entry = { $entry => shift @args }; |
185 | } |
186 | my $aqt = $self->expand_expr($entry, -ident); |
187 | if ($aqt->{-join} and not $aqt->{-join}{from}) { |
188 | $aqt->{-join}{from} = pop @list; |
189 | } |
190 | push @list, $aqt; |
191 | } |
192 | return { -from_list => \@list }; |
193 | } |
194 | |
195 | sub _expand_join { |
196 | my ($self, undef, $args) = @_; |
197 | my %proto = ( |
198 | ref($args) eq 'HASH' |
199 | ? %$args |
200 | : (to => $args->[0], @{$args}[1..$#$args]) |
201 | ); |
6990b2aa |
202 | if (my $as = delete $proto{as}) { |
203 | $proto{to} = { -as => [ $proto{to}, ref($as) eq 'ARRAY' ? @$as : $as ] }; |
204 | } |
0891ae97 |
205 | if (defined($proto{using}) and ref(my $using = $proto{using}) ne 'HASH') { |
206 | $proto{using} = { -row => [ |
207 | map [ $self->expand_expr($_, -ident) ], |
208 | ref($using) eq 'ARRAY' ? @$using: $using |
209 | ] }; |
210 | } |
f79455dc |
211 | my %ret = map +($_ => $self->expand_expr($proto{$_}, -ident)), |
212 | sort keys %proto; |
213 | return +{ -join => \%ret }; |
214 | } |
215 | |
216 | sub _render_from_list { |
6c39a2f7 |
217 | my ($self, undef, $list) = @_; |
3e230b71 |
218 | return $self->join_query_parts(', ', @$list); |
f79455dc |
219 | } |
220 | |
221 | sub _render_join { |
6c39a2f7 |
222 | my ($self, undef, $args) = @_; |
f79455dc |
223 | |
224 | my @parts = ( |
225 | [ $self->render_aqt($args->{from}) ], |
f7a20100 |
226 | [ $self->format_keyword(join '_', ($args->{type}||()), 'join') ], |
76aa8b0d |
227 | [ $self->render_aqt( |
0891ae97 |
228 | map +($_->{-ident} || $_->{-as} ? $_ : { -row => [ $_ ] }), $args->{to} |
76aa8b0d |
229 | ) ], |
f79455dc |
230 | ($args->{on} ? ( |
f7a20100 |
231 | [ $self->format_keyword('on') ], |
f79455dc |
232 | [ $self->render_aqt($args->{on}) ], |
233 | ) : ()), |
234 | ($args->{using} ? ( |
f7a20100 |
235 | [ $self->format_keyword('using') ], |
f79455dc |
236 | [ $self->render_aqt($args->{using}) ], |
237 | ) : ()), |
238 | ); |
59c7f80e |
239 | return $self->join_query_parts(' ', @parts); |
f79455dc |
240 | } |
241 | |
6990b2aa |
242 | sub _expand_op_as { |
243 | my ($self, undef, $vv, $k) = @_; |
244 | my @as = map $self->expand_expr($_, -ident), |
245 | (defined($k) ? ($k) : ()), ref($vv) eq 'ARRAY' ? @$vv : $vv; |
246 | return { -as => \@as }; |
247 | } |
248 | |
249 | sub _render_as { |
6c39a2f7 |
250 | my ($self, undef, $args) = @_; |
369e7844 |
251 | my ($thing, @alias) = @$args; |
59c7f80e |
252 | return $self->join_query_parts( |
6990b2aa |
253 | ' ', |
434c40f1 |
254 | [ $self->render_aqt($thing) ], |
f7a20100 |
255 | [ $self->format_keyword('as') ], |
369e7844 |
256 | [ $self->_render_alias(\@alias) ], |
257 | ); |
258 | } |
259 | |
260 | sub _render_alias { |
261 | my ($self, $args) = @_; |
262 | my ($as, @cols) = @$args; |
263 | return (@cols |
59c7f80e |
264 | ? $self->join_query_parts('', |
3e230b71 |
265 | $as, |
369e7844 |
266 | [ '(' ], |
59c7f80e |
267 | [ $self->join_query_parts( |
369e7844 |
268 | ', ', |
3e230b71 |
269 | @cols |
369e7844 |
270 | ) ], |
271 | [ ')' ], |
272 | ) |
273 | : $self->render_aqt($as) |
6990b2aa |
274 | ); |
275 | } |
276 | |
af407e9a |
277 | sub _expand_update_clause_target { |
1107714b |
278 | my ($self, undef, $target) = @_; |
af407e9a |
279 | +(target => $self->_expand_from_list(undef, $target)); |
280 | } |
281 | |
f8392f56 |
282 | 1; |