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
14 my ($self, $method, @args) = @_;
16 local $self->{sqla} = shift;
17 $self->$method(@args, @_)
22 my ($self, $sqla) = @_;
23 $self = $self->new unless ref($self);
24 my @clauses = $sqla->clauses_of('select');
26 CLAUSE: foreach my $idx (0..$#clauses) {
27 if ($clauses[$idx] eq 'order_by') {
28 @before_setop = @clauses[0..$idx-1];
29 splice(@clauses, $idx, 0, qw(setop group_by having));
33 die "Huh?" unless @before_setop;
34 $sqla->clauses_of(select => 'with', @clauses);
35 $sqla->clause_expanders(
36 'select.group_by', $self->cb(sub {
37 $_[0]->expand_maybe_list_expr($_[2], -ident)
39 'select.having', $self->cb(sub { $_[0]->expand_expr($_[2]) }),
41 foreach my $thing (qw(join from_list)) {
42 $sqla->expander($thing => $self->cb("_expand_${thing}"))
43 ->renderer($thing => $self->cb("_render_${thing}"))
45 $sqla->op_expander(as => $self->cb('_expand_op_as'));
46 $sqla->expander(as => $self->cb('_expand_op_as'));
47 $sqla->renderer(as => $self->cb('_render_as'));
48 $sqla->expander(alias => $self->cb('_expand_alias'));
49 $sqla->renderer(alias => $self->cb('_render_alias'));
51 $sqla->clauses_of(update => sub {
52 my ($self, @clauses) = @_;
53 splice(@clauses, 2, 0, 'from');
57 $sqla->clauses_of(delete => sub {
58 my ($self, @clauses) = @_;
59 splice(@clauses, 1, 0, 'using');
63 $sqla->clause_expanders(
64 'update.from' => $self->cb('_expand_from_list'),
65 'delete.using' => $self->cb('_expand_from_list'),
66 'insert.rowvalues' => $self->cb(sub {
67 +(from => $_[0]->expand_expr({ -values => $_[2] }));
69 'insert.select' => $self->cb(sub {
70 +(from => $_[0]->expand_expr({ -select => $_[2] }));
75 $sqla->wrap_expander(select => sub {
76 $self->cb('_expand_select', $_[0], \@before_setop);
79 $sqla->clause_renderer(
80 'select.setop' => $self->cb(sub { $_[0]->render_aqt($_[2]); })
83 foreach my $setop (qw(union intersect except)) {
84 $sqla->expander($setop => $self->cb('_expand_setop'));
85 $sqla->renderer($setop => $self->cb('_render_setop'));
88 my $setop_expander = $self->cb('_expand_clause_setop');
90 $sqla->clause_expanders(
91 map +($_ => $setop_expander),
93 map +($_, "${_}_all", "${_}_distinct"),
94 qw(union intersect except)
97 my $w_exp = $self->cb('_expand_with');
98 my $w_rdr = $self->cb('_render_with');
99 $sqla->clause_expander('select.with' => $w_exp);
100 $sqla->clause_expander('select.with_recursive' => $w_exp);
101 $sqla->clause_renderer('select.with' => $w_rdr);
103 foreach my $stmt (qw(insert update delete)) {
104 $sqla->clauses_of($stmt => 'with', $sqla->clauses_of($stmt));
105 $sqla->clause_expander("${stmt}.$_" => $w_exp)
106 for qw(with with_recursive);
107 $sqla->clause_renderer("${stmt}.with" => $w_rdr);
110 $sqla->expander(cast => $self->cb('_expand_cast'));
112 $sqla->clause_expanders(
113 "select.from", $self->cb('_expand_from_list'),
114 "update.target", $self->cb('_expand_update_clause_target'),
115 "update.update", $self->cb('_expand_update_clause_target'),
122 my ($self, $orig, $before_setop, @args) = @_;
123 my $exp = $self->sqla->$orig(@args);
124 return $exp unless my $setop = (my $sel = $exp->{-select})->{setop};
125 if (my @keys = grep $sel->{$_}, @$before_setop) {
126 my %inner; @inner{@keys} = delete @{$sel}{@keys};
127 unshift @{(values(%$setop))[0]{queries}},
128 { -select => \%inner };
133 sub _expand_from_list {
134 my ($self, undef, $args) = @_;
135 if (ref($args) eq 'HASH') {
136 return $args if $args->{-from_list};
137 return { -from_list => [ $self->expand_expr($args) ] };
140 my @args = ref($args) eq 'ARRAY' ? @$args : ($args);
141 while (my $entry = shift @args) {
142 if (!ref($entry) and $entry =~ /^-(.*)/) {
144 $list[-1] = $self->expand_expr({ -as => [
145 $list[-1], map +(ref($_) eq 'ARRAY' ? @$_ : $_), shift(@args)
149 $entry = { $entry => shift @args };
151 my $aqt = $self->expand_expr($entry, -ident);
152 if ($aqt->{-join} and not $aqt->{-join}{from}) {
153 $aqt->{-join}{from} = pop @list;
157 return { -from_list => \@list };
161 my ($self, undef, $args) = @_;
165 : (to => $args->[0], @{$args}[1..$#$args])
167 if (my $as = delete $proto{as}) {
168 $proto{to} = $self->expand_expr({ -as => [ $proto{to}, $as ] });
170 if (defined($proto{using}) and ref(my $using = $proto{using}) ne 'HASH') {
172 map [ $self->expand_expr($_, -ident) ],
173 ref($using) eq 'ARRAY' ? @$using: $using
176 my %ret = map +($_ => $self->expand_expr($proto{$_}, -ident)),
178 return +{ -join => \%ret };
181 sub _render_from_list {
182 my ($self, undef, $list) = @_;
183 return $self->join_query_parts(', ', @$list);
187 my ($self, undef, $args) = @_;
191 $self->format_keyword(join '_', ($args->{type}||()), 'join'),
192 (map +($_->{-ident} || $_->{-as} ? $_ : ('(', $_, ')')), $args->{to}),
194 $self->format_keyword('on') ,
198 $self->format_keyword('using'),
199 '(', $args->{using}, ')',
202 return $self->join_query_parts(' ', @parts);
206 my ($self, undef, $vv, $k) = @_;
207 my @vv = (ref($vv) eq 'ARRAY' ? @$vv : $vv);
209 my $ik = $self->expand_expr($k, -ident);
210 return +{ -as => [ $ik, $self->expand_expr($vv[0], -alias) ] }
211 if @vv == 1 and ref($vv[0]) eq 'HASH';
213 my @as = map $self->expand_expr($_, -ident), @vv;
214 return { -as => [ $ik, { -alias => \@as } ] };
218 my ($self, undef, $args) = @_;
219 my ($thing, $alias) = @$args;
220 return $self->join_query_parts(
223 $self->format_keyword('as'),
229 my ($self, undef, $args) = @_;
230 my ($as, @cols) = @$args;
232 ? $self->join_query_parts('',
235 $self->join_query_parts(
241 : $self->render_aqt($as)
245 sub _expand_update_clause_target {
246 my ($self, undef, $target) = @_;
247 +(target => $self->_expand_from_list(undef, $target));
251 my ($self, undef, $thing) = @_;
252 return { -func => [ cast => $thing ] } if ref($thing) eq 'HASH';
253 my ($cast, $to) = @{$thing};
254 +{ -func => [ cast => { -as => [
255 $self->expand_expr($cast),
256 $self->expand_expr($to, -ident),
261 my ($self, undef, $args) = @_;
262 if (ref($args) eq 'HASH' and my $alias = $args->{-alias}) {
266 map $self->expand_expr($_, -ident),
267 ref($args) eq 'ARRAY' ? @{$args} : $args
273 my ($self, $name, $with) = @_;
274 my (undef, $type) = split '_', $name;
275 if (ref($with) eq 'HASH') {
280 $self->expand_expr({ -alias => $_->[0] }, -ident),
281 $self->expand_expr($_->[1]),
282 ], @{$with->{queries}}
288 while (my ($alias, $query) = splice @with, 0, 2) {
290 $self->expand_expr({ -alias => $alias }, -ident),
291 $self->expand_expr($query)
294 return +(with => { ($type ? (type => $type) : ()), queries => \@exp });
298 my ($self, undef, $with) = @_;
299 my $q_part = $self->join_query_parts(', ',
301 my ($alias, $query) = @$_;
302 $self->join_query_parts(' ',
304 $self->format_keyword('as'),
307 } @{$with->{queries}}
309 return $self->join_query_parts(' ',
310 $self->format_keyword(join '_', 'with', ($with->{type}||'')),
316 my ($self, $setop, $args) = @_;
319 queries => [ map $self->expand_expr($_), @{$args->{queries}} ],
324 my ($self, $setop, $args) = @_;
325 $self->join_query_parts(
326 ' '.$self->format_keyword(join '_', $setop, ($args->{type}||())).' ',
331 sub _expand_clause_setop {
332 my ($self, $setop, $args) = @_;
333 my ($op, $type) = split '_', $setop;
334 +(setop => $self->expand_expr({
336 ($type ? (type => $type) : ()),
337 queries => (ref($args) eq 'ARRAY' ? $args : [ $args ])