1 package SQL::Abstract::ExtraClauses;
6 is => 'ro', init_arg => undef,
8 expand_expr expand_maybe_list_expr render_aqt join_query_parts
13 my ($self, $method, @args) = @_;
15 local $self->{sqla} = shift;
16 $self->$method(@args, @_)
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_from_list'),
64 'delete.using' => $self->cb('_expand_from_list'),
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 {
75 $self->cb('_expand_select', $_[0], \@before_setop);
78 $sqla->clause_renderer(
79 'select.setop' => $self->cb(sub { $_[0]->render_aqt($_[2]); })
82 foreach my $setop (qw(union intersect except)) {
83 $sqla->expander($setop => $self->cb('_expand_setop'));
84 $sqla->renderer($setop => $self->cb('_render_setop'));
87 my $setop_expander = $self->cb('_expand_clause_setop');
89 $sqla->clause_expanders(
90 map +($_ => $setop_expander),
92 map +($_, "${_}_all", "${_}_distinct"),
93 qw(union intersect except)
96 my $w_exp = $self->cb('_expand_with');
97 my $w_rdr = $self->cb('_render_with');
98 $sqla->clause_expander('select.with' => $w_exp);
99 $sqla->clause_expander('select.with_recursive' => $w_exp);
100 $sqla->clause_renderer('select.with' => $w_rdr);
102 foreach my $stmt (qw(insert update delete)) {
103 $sqla->clauses_of($stmt => 'with', $sqla->clauses_of($stmt));
104 $sqla->clause_expander("${stmt}.$_" => $w_exp)
105 for qw(with with_recursive);
106 $sqla->clause_renderer("${stmt}.with" => $w_rdr);
109 $sqla->expander(cast => $self->cb('_expand_cast'));
111 $sqla->clause_expanders(
112 "select.from", $self->cb('_expand_from_list'),
113 "update.target", $self->cb('_expand_update_clause_target'),
114 "update.update", $self->cb('_expand_update_clause_target'),
121 my ($self, $orig, $before_setop, @args) = @_;
122 my $exp = $self->sqla->$orig(@args);
123 return $exp unless my $setop = (my $sel = $exp->{-select})->{setop};
124 if (my @keys = grep $sel->{$_}, @$before_setop) {
125 my %inner; @inner{@keys} = delete @{$sel}{@keys};
126 unshift @{(values(%$setop))[0]{queries}},
127 { -select => \%inner };
132 sub _expand_from_list {
133 my ($self, undef, $args) = @_;
134 if (ref($args) eq 'HASH') {
135 return $args if $args->{-from_list};
136 return { -from_list => [ $self->expand_expr($args) ] };
139 my @args = ref($args) eq 'ARRAY' ? @$args : ($args);
140 while (my $entry = shift @args) {
141 if (!ref($entry) and $entry =~ /^-(.*)/) {
143 $list[-1] = $self->expand_expr({ -as => [
144 $list[-1], map +(ref($_) eq 'ARRAY' ? @$_ : $_), shift(@args)
148 $entry = { $entry => shift @args };
150 my $aqt = $self->expand_expr($entry, -ident);
151 if ($aqt->{-join} and not $aqt->{-join}{from}) {
152 $aqt->{-join}{from} = pop @list;
156 return { -from_list => \@list };
160 my ($self, undef, $args) = @_;
164 : (to => $args->[0], @{$args}[1..$#$args])
166 if (my $as = delete $proto{as}) {
167 $proto{to} = $self->expand_expr({ -as => [ $proto{to}, $as ] });
169 if (defined($proto{using}) and ref(my $using = $proto{using}) ne 'HASH') {
171 map [ $self->expand_expr($_, -ident) ],
172 ref($using) eq 'ARRAY' ? @$using: $using
175 my %ret = map +($_ => $self->expand_expr($proto{$_}, -ident)),
177 return +{ -join => \%ret };
180 sub _render_from_list {
181 my ($self, undef, $list) = @_;
182 return $self->join_query_parts(', ', @$list);
186 my ($self, undef, $args) = @_;
190 { -keyword => join '_', ($args->{type}||()), 'join' },
191 (map +($_->{-ident} || $_->{-as} ? $_ : ('(', $_, ')')), $args->{to}),
193 { -keyword => 'on' },
197 { -keyword => 'using' },
198 '(', $args->{using}, ')',
201 return $self->join_query_parts(' ', @parts);
205 my ($self, undef, $vv, $k) = @_;
206 my @vv = (ref($vv) eq 'ARRAY' ? @$vv : $vv);
208 my $ik = $self->expand_expr($k, -ident);
209 return +{ -as => [ $ik, $self->expand_expr($vv[0], -alias) ] }
210 if @vv == 1 and ref($vv[0]) eq 'HASH';
212 my @as = map $self->expand_expr($_, -ident), @vv;
213 return { -as => [ $ik, { -alias => \@as } ] };
217 my ($self, undef, $args) = @_;
218 my ($thing, $alias) = @$args;
219 return $self->join_query_parts(
222 { -keyword => 'as' },
228 my ($self, undef, $args) = @_;
229 my ($as, @cols) = @$args;
231 ? $self->join_query_parts('',
234 $self->join_query_parts(
240 : $self->render_aqt($as)
244 sub _expand_update_clause_target {
245 my ($self, undef, $target) = @_;
246 +(target => $self->_expand_from_list(undef, $target));
250 my ($self, undef, $thing) = @_;
251 return { -func => [ cast => $thing ] } if ref($thing) eq 'HASH';
252 my ($cast, $to) = @{$thing};
253 +{ -func => [ cast => { -as => [
254 $self->expand_expr($cast),
255 $self->expand_expr($to, -ident),
260 my ($self, undef, $args) = @_;
261 if (ref($args) eq 'HASH' and my $alias = $args->{-alias}) {
265 map $self->expand_expr($_, -ident),
266 ref($args) eq 'ARRAY' ? @{$args} : $args
272 my ($self, $name, $with) = @_;
273 my (undef, $type) = split '_', $name;
274 if (ref($with) eq 'HASH') {
279 $self->expand_expr({ -alias => $_->[0] }, -ident),
280 $self->expand_expr($_->[1]),
281 ], @{$with->{queries}}
287 while (my ($alias, $query) = splice @with, 0, 2) {
289 $self->expand_expr({ -alias => $alias }, -ident),
290 $self->expand_expr($query)
293 return +(with => { ($type ? (type => $type) : ()), queries => \@exp });
297 my ($self, undef, $with) = @_;
298 my $q_part = $self->join_query_parts(', ',
300 my ($alias, $query) = @$_;
301 $self->join_query_parts(' ',
303 { -keyword => 'as' },
306 } @{$with->{queries}}
308 return $self->join_query_parts(' ',
309 { -keyword => join '_', 'with', ($with->{type}||'') },
315 my ($self, $setop, $args) = @_;
318 queries => [ map $self->expand_expr($_), @{$args->{queries}} ],
323 my ($self, $setop, $args) = @_;
324 $self->join_query_parts(
325 { -keyword => ' '.join('_', $setop, ($args->{type}||())).' ' },
330 sub _expand_clause_setop {
331 my ($self, $setop, $args) = @_;
332 my ($op, $type) = split '_', $setop;
333 +(setop => $self->expand_expr({
335 ($type ? (type => $type) : ()),
336 queries => (ref($args) eq 'ARRAY' ? $args : [ $args ])