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) = @_;
15 return sub { local $self->{sqla} = shift; $self->$method(@_) };
19 my ($self, $sqla) = @_;
20 $self = $self->new unless ref($self);
21 my @clauses = $sqla->clauses_of('select');
23 CLAUSE: foreach my $idx (0..$#clauses) {
24 if ($clauses[$idx] eq 'order_by') {
25 @before_setop = @clauses[0..$idx-1];
26 splice(@clauses, $idx, 0, qw(setop group_by having));
30 die "Huh?" unless @before_setop;
31 $sqla->clauses_of(select => 'with', @clauses);
32 $sqla->clause_expanders(
33 'select.group_by', $self->cb(sub {
34 $_[0]->expand_maybe_list_expr($_[2], -ident)
36 'select.having', $self->cb(sub { $_[0]->expand_expr($_[2]) }),
38 foreach my $thing (qw(join from_list)) {
39 $sqla->expander($thing => $self->cb("_expand_${thing}"))
40 ->renderer($thing => $self->cb("_render_${thing}"))
42 $sqla->op_expander(as => $self->cb('_expand_op_as'));
43 $sqla->expander(as => $self->cb('_expand_op_as'));
44 $sqla->renderer(as => $self->cb('_render_as'));
45 $sqla->expander(alias => $self->cb('_expand_alias'));
46 $sqla->renderer(alias => $self->cb('_render_alias'));
48 $sqla->clauses_of(update => sub {
49 my ($self, @clauses) = @_;
50 splice(@clauses, 2, 0, 'from');
54 $sqla->clauses_of(delete => sub {
55 my ($self, @clauses) = @_;
56 splice(@clauses, 1, 0, 'using');
60 $sqla->clause_expanders(
61 'update.from' => $self->cb('_expand_select_clause_from'),
62 'delete.using' => $self->cb(sub {
63 +(using => $_[0]->_expand_from_list(undef, $_[2]));
65 'insert.rowvalues' => $self->cb(sub {
66 +(from => $_[0]->expand_expr({ -values => $_[2] }));
68 'insert.select' => $self->cb(sub {
69 +(from => $_[0]->expand_expr({ -select => $_[2] }));
74 $sqla->wrap_expander(select => sub {
78 my $exp = $self->sqla->$orig(@_);
79 return $exp unless my $setop = (my $sel = $exp->{-select})->{setop};
80 if (my @keys = grep $sel->{$_}, @before_setop) {
81 my %inner; @inner{@keys} = delete @{$sel}{@keys};
82 unshift @{(values(%$setop))[0]{queries}},
83 { -select => \%inner };
89 $sqla->clause_renderer('select.setop' => $self->cb(sub {
90 my ($self, undef, $setop) = @_;
91 $self->render_aqt($setop);
94 foreach my $setop (qw(union intersect except)) {
95 $sqla->expander($setop => $self->cb('_expand_setop'));
96 $sqla->renderer($setop => $self->cb('_render_setop'));
99 my $setop_expander = $self->cb('_expand_clause_setop');
101 $sqla->clause_expanders(
102 map +($_ => $setop_expander),
104 map +($_, "${_}_all", "${_}_distinct"),
105 qw(union intersect except)
108 my $w_exp = $self->cb('_expand_with');
109 my $w_rdr = $self->cb('_render_with');
110 $sqla->clause_expander('select.with' => $w_exp);
111 $sqla->clause_expander('select.with_recursive' => $w_exp);
112 $sqla->clause_renderer('select.with' => $w_rdr);
114 foreach my $stmt (qw(insert update delete)) {
115 $sqla->clauses_of($stmt => 'with', $sqla->clauses_of($stmt));
116 $sqla->clause_expander("${stmt}.$_" => $w_exp)
117 for qw(with with_recursive);
118 $sqla->clause_renderer("${stmt}.with" => $w_rdr);
121 $sqla->expander(cast => $self->cb('_expand_cast'));
123 $sqla->clause_expanders(
124 "select.from", $self->cb('_expand_select_clause_from'),
125 "update.target", $self->cb('_expand_update_clause_target'),
126 "update.update", $self->cb('_expand_update_clause_target'),
132 sub _expand_select_clause_from {
133 my ($self, undef, $from) = @_;
134 +(from => $self->_expand_from_list(undef, $from));
137 sub _expand_from_list {
138 my ($self, undef, $args) = @_;
139 if (ref($args) eq 'HASH') {
140 return $args if $args->{-from_list};
141 return { -from_list => [ $self->expand_expr($args) ] };
144 my @args = ref($args) eq 'ARRAY' ? @$args : ($args);
145 while (my $entry = shift @args) {
146 if (!ref($entry) and $entry =~ /^-(.*)/) {
148 $list[-1] = $self->expand_expr({ -as => [
149 $list[-1], map +(ref($_) eq 'ARRAY' ? @$_ : $_), shift(@args)
153 $entry = { $entry => shift @args };
155 my $aqt = $self->expand_expr($entry, -ident);
156 if ($aqt->{-join} and not $aqt->{-join}{from}) {
157 $aqt->{-join}{from} = pop @list;
161 return { -from_list => \@list };
165 my ($self, undef, $args) = @_;
169 : (to => $args->[0], @{$args}[1..$#$args])
171 if (my $as = delete $proto{as}) {
172 $proto{to} = $self->expand_expr({ -as => [ $proto{to}, $as ] });
174 if (defined($proto{using}) and ref(my $using = $proto{using}) ne 'HASH') {
176 map [ $self->expand_expr($_, -ident) ],
177 ref($using) eq 'ARRAY' ? @$using: $using
180 my %ret = map +($_ => $self->expand_expr($proto{$_}, -ident)),
182 return +{ -join => \%ret };
185 sub _render_from_list {
186 my ($self, undef, $list) = @_;
187 return $self->join_query_parts(', ', @$list);
191 my ($self, undef, $args) = @_;
195 $self->format_keyword(join '_', ($args->{type}||()), 'join'),
196 (map +($_->{-ident} || $_->{-as} ? $_ : ('(', $_, ')')), $args->{to}),
198 $self->format_keyword('on') ,
202 $self->format_keyword('using'),
203 '(', $args->{using}, ')',
206 return $self->join_query_parts(' ', @parts);
210 my ($self, undef, $vv, $k) = @_;
211 my @vv = (ref($vv) eq 'ARRAY' ? @$vv : $vv);
213 my $ik = $self->expand_expr($k, -ident);
214 return +{ -as => [ $ik, $self->expand_expr($vv[0], -alias) ] }
215 if @vv == 1 and ref($vv[0]) eq 'HASH';
217 my @as = map $self->expand_expr($_, -ident), @vv;
218 return { -as => [ $ik, { -alias => \@as } ] };
222 my ($self, undef, $args) = @_;
223 my ($thing, $alias) = @$args;
224 return $self->join_query_parts(
227 $self->format_keyword('as'),
233 my ($self, undef, $args) = @_;
234 my ($as, @cols) = @$args;
236 ? $self->join_query_parts('',
239 $self->join_query_parts(
245 : $self->render_aqt($as)
249 sub _expand_update_clause_target {
250 my ($self, undef, $target) = @_;
251 +(target => $self->_expand_from_list(undef, $target));
255 my ($self, undef, $thing) = @_;
256 return { -func => [ cast => $thing ] } if ref($thing) eq 'HASH';
257 my ($cast, $to) = @{$thing};
258 +{ -func => [ cast => { -as => [
259 $self->expand_expr($cast),
260 $self->expand_expr($to, -ident),
265 my ($self, undef, $args) = @_;
266 if (ref($args) eq 'HASH' and my $alias = $args->{-alias}) {
270 map $self->expand_expr($_, -ident),
271 ref($args) eq 'ARRAY' ? @{$args} : $args
277 my ($self, $name, $with) = @_;
278 my (undef, $type) = split '_', $name;
279 if (ref($with) eq 'HASH') {
284 $self->expand_expr({ -alias => $_->[0] }, -ident),
285 $self->expand_expr($_->[1]),
286 ], @{$with->{queries}}
292 while (my ($alias, $query) = splice @with, 0, 2) {
294 $self->expand_expr({ -alias => $alias }, -ident),
295 $self->expand_expr($query)
298 return +(with => { ($type ? (type => $type) : ()), queries => \@exp });
302 my ($self, undef, $with) = @_;
303 my $q_part = $self->join_query_parts(', ',
305 my ($alias, $query) = @$_;
306 $self->join_query_parts(' ',
308 $self->format_keyword('as'),
311 } @{$with->{queries}}
313 return $self->join_query_parts(' ',
314 $self->format_keyword(join '_', 'with', ($with->{type}||'')),
320 my ($self, $setop, $args) = @_;
323 queries => [ map $self->expand_expr($_), @{$args->{queries}} ],
328 my ($self, $setop, $args) = @_;
329 $self->join_query_parts(
330 ' '.$self->format_keyword(join '_', $setop, ($args->{type}||())).' ',
335 sub _expand_clause_setop {
336 my ($self, $setop, $args) = @_;
337 my ($op, $type) = split '_', $setop;
338 +(setop => $self->expand_expr({
340 ($type ? (type => $type) : ()),
341 queries => (ref($args) eq 'ARRAY' ? $args : [ $args ])