1 package SQL::Abstract::ExtraClauses;
5 use if $] < '5.010', 'MRO::Compat';
7 use base qw(SQL::Abstract);
9 BEGIN { *puke = \&SQL::Abstract::puke }
12 my $self = shift->next::method(@_);
13 my @clauses = $self->clauses_of('select');
15 CLAUSE: foreach my $idx (0..$#clauses) {
16 if ($clauses[$idx] eq 'order_by') {
17 @before_setop = @clauses[0..$idx-1];
18 splice(@clauses, $idx, 0, qw(setop group_by having));
22 die "Huh?" unless @before_setop;
23 $self->clauses_of(select => 'with', @clauses);
24 $self->clause_expanders(
25 'select.group_by', sub {
26 $_[0]->_expand_maybe_list_expr($_[2], -ident)
28 'select.having', sub { $_[0]->expand_expr($_[2]) },
30 foreach my $thing (qw(join from_list)) {
31 $self->expander($thing => "_expand_${thing}")
32 ->renderer($thing => "_render_${thing}")
34 $self->op_expander(as => '_expand_op_as');
35 $self->expander(as => '_expand_op_as');
36 $self->renderer(as => '_render_as');
37 $self->expander(alias => sub {
38 my ($self, undef, $args) = @_;
39 if (ref($args) eq 'HASH' and my $alias = $args->{-alias}) {
43 map $self->expand_expr($_, -ident),
44 ref($args) eq 'ARRAY' ? @{$args} : $args
48 $self->renderer(alias => '_render_alias');
50 $self->clauses_of(update => sub {
51 my ($self, @clauses) = @_;
52 splice(@clauses, 2, 0, 'from');
56 $self->clauses_of(delete => sub {
57 my ($self, @clauses) = @_;
58 splice(@clauses, 1, 0, 'using');
62 $self->clause_expanders(
63 'update.from' => '_expand_select_clause_from',
64 'delete.using' => sub {
65 +(using => $_[0]->_expand_from_list(undef, $_[2]));
67 'insert.rowvalues' => sub {
68 +(from => $_[0]->expand_expr({ -values => $_[2] }));
70 'insert.select' => sub {
71 +(from => $_[0]->expand_expr({ -select => $_[2] }));
76 $self->wrap_expander(select => sub {
80 my $exp = $self->$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 };
90 my $expand_setop = sub {
91 my ($self, $setop, $args) = @_;
94 queries => [ map $self->expand_expr($_), @{$args->{queries}} ],
97 $self->expanders(map +($_ => $expand_setop), qw(union intersect except));
99 $self->clause_renderer('select.setop' => sub {
100 my ($self, undef, $setop) = @_;
101 $self->render_aqt($setop);
104 $self->renderer($_ => sub {
105 my ($self, $setop, $args) = @_;
106 $self->join_query_parts(
107 ' '.$self->format_keyword(join '_', $setop, ($args->{type}||())).' ',
110 }) for qw(union intersect except);
112 my $setop_expander = sub {
113 my ($self, $setop, $args) = @_;
114 my ($op, $type) = split '_', $setop;
115 +(setop => $self->expand_expr({
117 ($type ? (type => $type) : ()),
118 queries => (ref($args) eq 'ARRAY' ? $args : [ $args ])
123 $self->clause_expanders(
124 map +($_ => $setop_expander),
126 map +($_, "${_}_all", "${_}_distinct"),
127 qw(union intersect except)
130 $self->clause_expander('select.with' => my $with_expander = sub {
131 my ($self, $name, $with) = @_;
132 my (undef, $type) = split '_', $name;
133 if (ref($with) eq 'HASH') {
138 $self->expand_expr({ -alias => $_->[0] }, -ident),
139 $self->expand_expr($_->[1]),
140 ], @{$with->{queries}}
146 while (my ($alias, $query) = splice @with, 0, 2) {
148 $self->expand_expr({ -alias => $alias }, -ident),
149 $self->expand_expr($query)
152 return +(with => { ($type ? (type => $type) : ()), queries => \@exp });
154 $self->clause_expander('select.with_recursive', $with_expander);
155 $self->clause_renderer('select.with' => my $with_renderer = sub {
156 my ($self, undef, $with) = @_;
157 my $q_part = $self->join_query_parts(', ',
159 my ($alias, $query) = @$_;
160 $self->join_query_parts(' ',
162 $self->format_keyword('as'),
165 } @{$with->{queries}}
167 return $self->join_query_parts(' ',
168 $self->format_keyword(join '_', 'with', ($with->{type}||'')),
172 foreach my $stmt (qw(insert update delete)) {
173 $self->clauses_of($stmt => 'with', $self->clauses_of($stmt));
174 $self->clause_expander("${stmt}.$_", $with_expander)
175 for qw(with with_recursive);
176 $self->clause_renderer("${stmt}.with", $with_renderer);
178 $self->expander(cast => sub {
179 return { -func => [ cast => $_[2] ] } if ref($_[2]) eq 'HASH';
180 my ($cast, $to) = @{$_[2]};
181 +{ -func => [ cast => { -as => [
182 $self->expand_expr($cast),
183 $self->expand_expr($to, -ident),
190 sub _expand_select_clause_from {
191 my ($self, undef, $from) = @_;
192 +(from => $self->_expand_from_list(undef, $from));
195 sub _expand_from_list {
196 my ($self, undef, $args) = @_;
197 if (ref($args) eq 'HASH') {
198 return $args if $args->{-from_list};
199 return { -from_list => [ $self->expand_expr($args) ] };
202 my @args = ref($args) eq 'ARRAY' ? @$args : ($args);
203 while (my $entry = shift @args) {
204 if (!ref($entry) and $entry =~ /^-(.*)/) {
206 $list[-1] = $self->expand_expr({ -as => [
207 $list[-1], map +(ref($_) eq 'ARRAY' ? @$_ : $_), shift(@args)
211 $entry = { $entry => shift @args };
213 my $aqt = $self->expand_expr($entry, -ident);
214 if ($aqt->{-join} and not $aqt->{-join}{from}) {
215 $aqt->{-join}{from} = pop @list;
219 return { -from_list => \@list };
223 my ($self, undef, $args) = @_;
227 : (to => $args->[0], @{$args}[1..$#$args])
229 if (my $as = delete $proto{as}) {
230 $proto{to} = $self->expand_expr({ -as => [ $proto{to}, $as ] });
232 if (defined($proto{using}) and ref(my $using = $proto{using}) ne 'HASH') {
234 map [ $self->expand_expr($_, -ident) ],
235 ref($using) eq 'ARRAY' ? @$using: $using
238 my %ret = map +($_ => $self->expand_expr($proto{$_}, -ident)),
240 return +{ -join => \%ret };
243 sub _render_from_list {
244 my ($self, undef, $list) = @_;
245 return $self->join_query_parts(', ', @$list);
249 my ($self, undef, $args) = @_;
253 $self->format_keyword(join '_', ($args->{type}||()), 'join'),
254 (map +($_->{-ident} || $_->{-as} ? $_ : ('(', $_, ')')), $args->{to}),
256 $self->format_keyword('on') ,
260 $self->format_keyword('using'),
261 '(', $args->{using}, ')',
264 return $self->join_query_parts(' ', @parts);
268 my ($self, undef, $vv, $k) = @_;
269 my @vv = (ref($vv) eq 'ARRAY' ? @$vv : $vv);
271 my $ik = $self->expand_expr($k, -ident);
272 return +{ -as => [ $ik, $self->expand_expr($vv[0], -alias) ] }
273 if @vv == 1 and ref($vv[0]) eq 'HASH';
275 my @as = map $self->expand_expr($_, -ident), @vv;
276 return { -as => [ $ik, { -alias => \@as } ] };
280 my ($self, undef, $args) = @_;
281 my ($thing, $alias) = @$args;
282 return $self->join_query_parts(
285 $self->format_keyword('as'),
291 my ($self, undef, $args) = @_;
292 my ($as, @cols) = @$args;
294 ? $self->join_query_parts('',
297 $self->join_query_parts(
303 : $self->render_aqt($as)
307 sub _expand_update_clause_target {
308 my ($self, undef, $target) = @_;
309 +(target => $self->_expand_from_list(undef, $target));