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('select.setop' => $self->cb(sub {
80 my ($self, undef, $setop) = @_;
81 $self->render_aqt($setop);
84 foreach my $setop (qw(union intersect except)) {
85 $sqla->expander($setop => $self->cb('_expand_setop'));
86 $sqla->renderer($setop => $self->cb('_render_setop'));
89 my $setop_expander = $self->cb('_expand_clause_setop');
91 $sqla->clause_expanders(
92 map +($_ => $setop_expander),
94 map +($_, "${_}_all", "${_}_distinct"),
95 qw(union intersect except)
98 my $w_exp = $self->cb('_expand_with');
99 my $w_rdr = $self->cb('_render_with');
100 $sqla->clause_expander('select.with' => $w_exp);
101 $sqla->clause_expander('select.with_recursive' => $w_exp);
102 $sqla->clause_renderer('select.with' => $w_rdr);
104 foreach my $stmt (qw(insert update delete)) {
105 $sqla->clauses_of($stmt => 'with', $sqla->clauses_of($stmt));
106 $sqla->clause_expander("${stmt}.$_" => $w_exp)
107 for qw(with with_recursive);
108 $sqla->clause_renderer("${stmt}.with" => $w_rdr);
111 $sqla->expander(cast => $self->cb('_expand_cast'));
113 $sqla->clause_expanders(
114 "select.from", $self->cb('_expand_from_list'),
115 "update.target", $self->cb('_expand_update_clause_target'),
116 "update.update", $self->cb('_expand_update_clause_target'),
123 my ($self, $orig, $before_setop, @args) = @_;
124 my $exp = $self->sqla->$orig(@args);
125 return $exp unless my $setop = (my $sel = $exp->{-select})->{setop};
126 if (my @keys = grep $sel->{$_}, @$before_setop) {
127 my %inner; @inner{@keys} = delete @{$sel}{@keys};
128 unshift @{(values(%$setop))[0]{queries}},
129 { -select => \%inner };
134 sub _expand_from_list {
135 my ($self, undef, $args) = @_;
136 if (ref($args) eq 'HASH') {
137 return $args if $args->{-from_list};
138 return { -from_list => [ $self->expand_expr($args) ] };
141 my @args = ref($args) eq 'ARRAY' ? @$args : ($args);
142 while (my $entry = shift @args) {
143 if (!ref($entry) and $entry =~ /^-(.*)/) {
145 $list[-1] = $self->expand_expr({ -as => [
146 $list[-1], map +(ref($_) eq 'ARRAY' ? @$_ : $_), shift(@args)
150 $entry = { $entry => shift @args };
152 my $aqt = $self->expand_expr($entry, -ident);
153 if ($aqt->{-join} and not $aqt->{-join}{from}) {
154 $aqt->{-join}{from} = pop @list;
158 return { -from_list => \@list };
162 my ($self, undef, $args) = @_;
166 : (to => $args->[0], @{$args}[1..$#$args])
168 if (my $as = delete $proto{as}) {
169 $proto{to} = $self->expand_expr({ -as => [ $proto{to}, $as ] });
171 if (defined($proto{using}) and ref(my $using = $proto{using}) ne 'HASH') {
173 map [ $self->expand_expr($_, -ident) ],
174 ref($using) eq 'ARRAY' ? @$using: $using
177 my %ret = map +($_ => $self->expand_expr($proto{$_}, -ident)),
179 return +{ -join => \%ret };
182 sub _render_from_list {
183 my ($self, undef, $list) = @_;
184 return $self->join_query_parts(', ', @$list);
188 my ($self, undef, $args) = @_;
192 $self->format_keyword(join '_', ($args->{type}||()), 'join'),
193 (map +($_->{-ident} || $_->{-as} ? $_ : ('(', $_, ')')), $args->{to}),
195 $self->format_keyword('on') ,
199 $self->format_keyword('using'),
200 '(', $args->{using}, ')',
203 return $self->join_query_parts(' ', @parts);
207 my ($self, undef, $vv, $k) = @_;
208 my @vv = (ref($vv) eq 'ARRAY' ? @$vv : $vv);
210 my $ik = $self->expand_expr($k, -ident);
211 return +{ -as => [ $ik, $self->expand_expr($vv[0], -alias) ] }
212 if @vv == 1 and ref($vv[0]) eq 'HASH';
214 my @as = map $self->expand_expr($_, -ident), @vv;
215 return { -as => [ $ik, { -alias => \@as } ] };
219 my ($self, undef, $args) = @_;
220 my ($thing, $alias) = @$args;
221 return $self->join_query_parts(
224 $self->format_keyword('as'),
230 my ($self, undef, $args) = @_;
231 my ($as, @cols) = @$args;
233 ? $self->join_query_parts('',
236 $self->join_query_parts(
242 : $self->render_aqt($as)
246 sub _expand_update_clause_target {
247 my ($self, undef, $target) = @_;
248 +(target => $self->_expand_from_list(undef, $target));
252 my ($self, undef, $thing) = @_;
253 return { -func => [ cast => $thing ] } if ref($thing) eq 'HASH';
254 my ($cast, $to) = @{$thing};
255 +{ -func => [ cast => { -as => [
256 $self->expand_expr($cast),
257 $self->expand_expr($to, -ident),
262 my ($self, undef, $args) = @_;
263 if (ref($args) eq 'HASH' and my $alias = $args->{-alias}) {
267 map $self->expand_expr($_, -ident),
268 ref($args) eq 'ARRAY' ? @{$args} : $args
274 my ($self, $name, $with) = @_;
275 my (undef, $type) = split '_', $name;
276 if (ref($with) eq 'HASH') {
281 $self->expand_expr({ -alias => $_->[0] }, -ident),
282 $self->expand_expr($_->[1]),
283 ], @{$with->{queries}}
289 while (my ($alias, $query) = splice @with, 0, 2) {
291 $self->expand_expr({ -alias => $alias }, -ident),
292 $self->expand_expr($query)
295 return +(with => { ($type ? (type => $type) : ()), queries => \@exp });
299 my ($self, undef, $with) = @_;
300 my $q_part = $self->join_query_parts(', ',
302 my ($alias, $query) = @$_;
303 $self->join_query_parts(' ',
305 $self->format_keyword('as'),
308 } @{$with->{queries}}
310 return $self->join_query_parts(' ',
311 $self->format_keyword(join '_', 'with', ($with->{type}||'')),
317 my ($self, $setop, $args) = @_;
320 queries => [ map $self->expand_expr($_), @{$args->{queries}} ],
325 my ($self, $setop, $args) = @_;
326 $self->join_query_parts(
327 ' '.$self->format_keyword(join '_', $setop, ($args->{type}||())).' ',
332 sub _expand_clause_setop {
333 my ($self, $setop, $args) = @_;
334 my ($op, $type) = split '_', $setop;
335 +(setop => $self->expand_expr({
337 ($type ? (type => $type) : ()),
338 queries => (ref($args) eq 'ARRAY' ? $args : [ $args ])