1 package SQL::Abstract::ExtraClauses;
6 is => 'ro', init_arg => undef,
8 expand_expr expand_maybe_list_expr render_aqt
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]->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('_expand_alias'));
48 $sqla->renderer(alias => $self->cb('_render_alias'));
50 $sqla->clauses_of(update => sub {
51 my ($self, @clauses) = @_;
52 splice(@clauses, 2, 0, 'from');
56 $sqla->clauses_of(delete => sub {
57 my ($self, @clauses) = @_;
58 splice(@clauses, 1, 0, 'using');
62 $sqla->clause_expanders(
63 'update.from' => $self->cb('_expand_select_clause_from'),
64 'delete.using' => $self->cb(sub {
65 +(using => $_[0]->_expand_from_list(undef, $_[2]));
67 'insert.rowvalues' => $self->cb(sub {
68 +(from => $_[0]->expand_expr({ -values => $_[2] }));
70 'insert.select' => $self->cb(sub {
71 +(from => $_[0]->expand_expr({ -select => $_[2] }));
76 $sqla->wrap_expander(select => sub {
80 my $exp = $self->sqla->$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 };
91 $sqla->clause_renderer('select.setop' => $self->cb(sub {
92 my ($self, undef, $setop) = @_;
93 $self->render_aqt($setop);
96 foreach my $setop (qw(union intersect except)) {
97 $sqla->expander($setop => $self->cb('_expand_setop'));
98 $sqla->renderer($setop => $self->cb('_render_setop'));
101 my $setop_expander = $self->cb('_expand_clause_setop');
103 $sqla->clause_expanders(
104 map +($_ => $setop_expander),
106 map +($_, "${_}_all", "${_}_distinct"),
107 qw(union intersect except)
110 my $w_exp = $self->cb('_expand_with');
111 my $w_rdr = $self->cb('_render_with');
112 $sqla->clause_expander('select.with' => $w_exp);
113 $sqla->clause_expander('select.with_recursive' => $w_exp);
114 $sqla->clause_renderer('select.with' => $w_rdr);
116 foreach my $stmt (qw(insert update delete)) {
117 $sqla->clauses_of($stmt => 'with', $sqla->clauses_of($stmt));
118 $sqla->clause_expander("${stmt}.$_" => $w_exp)
119 for qw(with with_recursive);
120 $sqla->clause_renderer("${stmt}.with" => $w_rdr);
123 $sqla->expander(cast => $self->cb('_expand_cast'));
125 $sqla->clause_expanders(
126 "select.from", $self->cb('_expand_select_clause_from'),
127 "update.target", $self->cb('_expand_update_clause_target'),
128 "update.update", $self->cb('_expand_update_clause_target'),
134 sub _expand_select_clause_from {
135 my ($self, undef, $from) = @_;
136 +(from => $self->_expand_from_list(undef, $from));
139 sub _expand_from_list {
140 my ($self, undef, $args) = @_;
141 if (ref($args) eq 'HASH') {
142 return $args if $args->{-from_list};
143 return { -from_list => [ $self->expand_expr($args) ] };
146 my @args = ref($args) eq 'ARRAY' ? @$args : ($args);
147 while (my $entry = shift @args) {
148 if (!ref($entry) and $entry =~ /^-(.*)/) {
150 $list[-1] = $self->expand_expr({ -as => [
151 $list[-1], map +(ref($_) eq 'ARRAY' ? @$_ : $_), shift(@args)
155 $entry = { $entry => shift @args };
157 my $aqt = $self->expand_expr($entry, -ident);
158 if ($aqt->{-join} and not $aqt->{-join}{from}) {
159 $aqt->{-join}{from} = pop @list;
163 return { -from_list => \@list };
167 my ($self, undef, $args) = @_;
171 : (to => $args->[0], @{$args}[1..$#$args])
173 if (my $as = delete $proto{as}) {
174 $proto{to} = $self->expand_expr({ -as => [ $proto{to}, $as ] });
176 if (defined($proto{using}) and ref(my $using = $proto{using}) ne 'HASH') {
178 map [ $self->expand_expr($_, -ident) ],
179 ref($using) eq 'ARRAY' ? @$using: $using
182 my %ret = map +($_ => $self->expand_expr($proto{$_}, -ident)),
184 return +{ -join => \%ret };
187 sub _render_from_list {
188 my ($self, undef, $list) = @_;
189 return $self->join_query_parts(', ', @$list);
193 my ($self, undef, $args) = @_;
197 $self->format_keyword(join '_', ($args->{type}||()), 'join'),
198 (map +($_->{-ident} || $_->{-as} ? $_ : ('(', $_, ')')), $args->{to}),
200 $self->format_keyword('on') ,
204 $self->format_keyword('using'),
205 '(', $args->{using}, ')',
208 return $self->join_query_parts(' ', @parts);
212 my ($self, undef, $vv, $k) = @_;
213 my @vv = (ref($vv) eq 'ARRAY' ? @$vv : $vv);
215 my $ik = $self->expand_expr($k, -ident);
216 return +{ -as => [ $ik, $self->expand_expr($vv[0], -alias) ] }
217 if @vv == 1 and ref($vv[0]) eq 'HASH';
219 my @as = map $self->expand_expr($_, -ident), @vv;
220 return { -as => [ $ik, { -alias => \@as } ] };
224 my ($self, undef, $args) = @_;
225 my ($thing, $alias) = @$args;
226 return $self->join_query_parts(
229 $self->format_keyword('as'),
235 my ($self, undef, $args) = @_;
236 my ($as, @cols) = @$args;
238 ? $self->join_query_parts('',
241 $self->join_query_parts(
247 : $self->render_aqt($as)
251 sub _expand_update_clause_target {
252 my ($self, undef, $target) = @_;
253 +(target => $self->_expand_from_list(undef, $target));
257 my ($self, undef, $thing) = @_;
258 return { -func => [ cast => $thing ] } if ref($thing) eq 'HASH';
259 my ($cast, $to) = @{$thing};
260 +{ -func => [ cast => { -as => [
261 $self->expand_expr($cast),
262 $self->expand_expr($to, -ident),
267 my ($self, undef, $args) = @_;
268 if (ref($args) eq 'HASH' and my $alias = $args->{-alias}) {
272 map $self->expand_expr($_, -ident),
273 ref($args) eq 'ARRAY' ? @{$args} : $args
279 my ($self, $name, $with) = @_;
280 my (undef, $type) = split '_', $name;
281 if (ref($with) eq 'HASH') {
286 $self->expand_expr({ -alias => $_->[0] }, -ident),
287 $self->expand_expr($_->[1]),
288 ], @{$with->{queries}}
294 while (my ($alias, $query) = splice @with, 0, 2) {
296 $self->expand_expr({ -alias => $alias }, -ident),
297 $self->expand_expr($query)
300 return +(with => { ($type ? (type => $type) : ()), queries => \@exp });
304 my ($self, undef, $with) = @_;
305 my $q_part = $self->join_query_parts(', ',
307 my ($alias, $query) = @$_;
308 $self->join_query_parts(' ',
310 $self->format_keyword('as'),
313 } @{$with->{queries}}
315 return $self->join_query_parts(' ',
316 $self->format_keyword(join '_', 'with', ($with->{type}||'')),
322 my ($self, $setop, $args) = @_;
325 queries => [ map $self->expand_expr($_), @{$args->{queries}} ],
330 my ($self, $setop, $args) = @_;
331 $self->join_query_parts(
332 ' '.$self->format_keyword(join '_', $setop, ($args->{type}||())).' ',
337 sub _expand_clause_setop {
338 my ($self, $setop, $args) = @_;
339 my ($op, $type) = split '_', $setop;
340 +(setop => $self->expand_expr({
342 ($type ? (type => $type) : ()),
343 queries => (ref($args) eq 'ARRAY' ? $args : [ $args ])