1 package SQL::Abstract::ExtraClauses;
6 is => 'ro', init_arg => undef,
9 format_keyword join_query_parts
13 BEGIN { *puke = \&SQL::Abstract::puke }
16 my ($self, $method) = @_;
17 return sub { local $self->{sqla} = shift; $self->$method(@_) };
21 my ($self, $sqla) = @_;
22 $self = $self->new unless ref($self);
23 my @clauses = $sqla->clauses_of('select');
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));
32 die "Huh?" unless @before_setop;
33 $sqla->clauses_of(select => 'with', @clauses);
34 $sqla->clause_expanders(
35 'select.group_by', $self->cb(sub {
36 $_[0]->sqla->_expand_maybe_list_expr($_[2], -ident)
38 'select.having', $self->cb(sub { $_[0]->expand_expr($_[2]) }),
40 foreach my $thing (qw(join from_list)) {
41 $sqla->expander($thing => $self->cb("_expand_${thing}"))
42 ->renderer($thing => $self->cb("_render_${thing}"))
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 {
48 my ($self, undef, $args) = @_;
49 if (ref($args) eq 'HASH' and my $alias = $args->{-alias}) {
53 map $self->expand_expr($_, -ident),
54 ref($args) eq 'ARRAY' ? @{$args} : $args
58 $sqla->renderer(alias => $self->cb('_render_alias'));
60 $sqla->clauses_of(update => sub {
61 my ($self, @clauses) = @_;
62 splice(@clauses, 2, 0, 'from');
66 $sqla->clauses_of(delete => sub {
67 my ($self, @clauses) = @_;
68 splice(@clauses, 1, 0, 'using');
72 $sqla->clause_expanders(
73 'update.from' => $self->cb('_expand_select_clause_from'),
74 'delete.using' => $self->cb(sub {
75 +(using => $_[0]->_expand_from_list(undef, $_[2]));
77 'insert.rowvalues' => $self->cb(sub {
78 +(from => $_[0]->expand_expr({ -values => $_[2] }));
80 'insert.select' => $self->cb(sub {
81 +(from => $_[0]->expand_expr({ -select => $_[2] }));
86 $sqla->wrap_expander(select => sub {
90 my $exp = $self->sqla->$orig(@_);
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 };
100 my $expand_setop = $self->cb(sub {
101 my ($self, $setop, $args) = @_;
104 queries => [ map $self->expand_expr($_), @{$args->{queries}} ],
107 $sqla->expanders(map +($_ => $expand_setop), qw(union intersect except));
109 $sqla->clause_renderer('select.setop' => $self->cb(sub {
110 my ($self, undef, $setop) = @_;
111 $self->render_aqt($setop);
114 $sqla->renderer($_ => $self->cb(sub {
115 my ($self, $setop, $args) = @_;
116 $self->join_query_parts(
117 ' '.$self->format_keyword(join '_', $setop, ($args->{type}||())).' ',
120 })) for qw(union intersect except);
122 my $setop_expander = $self->cb(sub {
123 my ($self, $setop, $args) = @_;
124 my ($op, $type) = split '_', $setop;
125 +(setop => $self->expand_expr({
127 ($type ? (type => $type) : ()),
128 queries => (ref($args) eq 'ARRAY' ? $args : [ $args ])
133 $sqla->clause_expanders(
134 map +($_ => $setop_expander),
136 map +($_, "${_}_all", "${_}_distinct"),
137 qw(union intersect except)
140 $sqla->clause_expander('select.with' => my $with_expander = $self->cb(sub {
141 my ($self, $name, $with) = @_;
142 my (undef, $type) = split '_', $name;
143 if (ref($with) eq 'HASH') {
148 $self->expand_expr({ -alias => $_->[0] }, -ident),
149 $self->expand_expr($_->[1]),
150 ], @{$with->{queries}}
156 while (my ($alias, $query) = splice @with, 0, 2) {
158 $self->expand_expr({ -alias => $alias }, -ident),
159 $self->expand_expr($query)
162 return +(with => { ($type ? (type => $type) : ()), queries => \@exp });
164 $sqla->clause_expander('select.with_recursive', $with_expander);
165 $sqla->clause_renderer('select.with' => my $with_renderer = $self->cb(sub {
166 my ($self, undef, $with) = @_;
167 my $q_part = $self->join_query_parts(', ',
169 my ($alias, $query) = @$_;
170 $self->join_query_parts(' ',
172 $self->format_keyword('as'),
175 } @{$with->{queries}}
177 return $self->join_query_parts(' ',
178 $self->format_keyword(join '_', 'with', ($with->{type}||'')),
182 foreach my $stmt (qw(insert update delete)) {
183 $sqla->clauses_of($stmt => 'with', $sqla->clauses_of($stmt));
184 $sqla->clause_expander("${stmt}.$_", $with_expander)
185 for qw(with with_recursive);
186 $sqla->clause_renderer("${stmt}.with", $with_renderer);
188 $sqla->expander(cast => $self->cb(sub {
189 return { -func => [ cast => $_[2] ] } if ref($_[2]) eq 'HASH';
190 my ($cast, $to) = @{$_[2]};
191 +{ -func => [ cast => { -as => [
192 $self->expand_expr($cast),
193 $self->expand_expr($to, -ident),
197 $sqla->clause_expanders(
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'),
206 sub _expand_select_clause_from {
207 my ($self, undef, $from) = @_;
208 +(from => $self->_expand_from_list(undef, $from));
211 sub _expand_from_list {
212 my ($self, undef, $args) = @_;
213 if (ref($args) eq 'HASH') {
214 return $args if $args->{-from_list};
215 return { -from_list => [ $self->expand_expr($args) ] };
218 my @args = ref($args) eq 'ARRAY' ? @$args : ($args);
219 while (my $entry = shift @args) {
220 if (!ref($entry) and $entry =~ /^-(.*)/) {
222 $list[-1] = $self->expand_expr({ -as => [
223 $list[-1], map +(ref($_) eq 'ARRAY' ? @$_ : $_), shift(@args)
227 $entry = { $entry => shift @args };
229 my $aqt = $self->expand_expr($entry, -ident);
230 if ($aqt->{-join} and not $aqt->{-join}{from}) {
231 $aqt->{-join}{from} = pop @list;
235 return { -from_list => \@list };
239 my ($self, undef, $args) = @_;
243 : (to => $args->[0], @{$args}[1..$#$args])
245 if (my $as = delete $proto{as}) {
246 $proto{to} = $self->expand_expr({ -as => [ $proto{to}, $as ] });
248 if (defined($proto{using}) and ref(my $using = $proto{using}) ne 'HASH') {
250 map [ $self->expand_expr($_, -ident) ],
251 ref($using) eq 'ARRAY' ? @$using: $using
254 my %ret = map +($_ => $self->expand_expr($proto{$_}, -ident)),
256 return +{ -join => \%ret };
259 sub _render_from_list {
260 my ($self, undef, $list) = @_;
261 return $self->join_query_parts(', ', @$list);
265 my ($self, undef, $args) = @_;
269 $self->format_keyword(join '_', ($args->{type}||()), 'join'),
270 (map +($_->{-ident} || $_->{-as} ? $_ : ('(', $_, ')')), $args->{to}),
272 $self->format_keyword('on') ,
276 $self->format_keyword('using'),
277 '(', $args->{using}, ')',
280 return $self->join_query_parts(' ', @parts);
284 my ($self, undef, $vv, $k) = @_;
285 my @vv = (ref($vv) eq 'ARRAY' ? @$vv : $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';
291 my @as = map $self->expand_expr($_, -ident), @vv;
292 return { -as => [ $ik, { -alias => \@as } ] };
296 my ($self, undef, $args) = @_;
297 my ($thing, $alias) = @$args;
298 return $self->join_query_parts(
301 $self->format_keyword('as'),
307 my ($self, undef, $args) = @_;
308 my ($as, @cols) = @$args;
310 ? $self->join_query_parts('',
313 $self->join_query_parts(
319 : $self->render_aqt($as)
323 sub _expand_update_clause_target {
324 my ($self, undef, $target) = @_;
325 +(target => $self->_expand_from_list(undef, $target));