1 package SQL::Abstract::ExtraClauses;
5 use if $] < '5.010', 'MRO::Compat';
7 use base qw(SQL::Abstract::Clauses);
9 BEGIN { *puke = \&SQL::Abstract::puke }
11 sub register_defaults {
13 $self->next::method(@_);
14 my @clauses = $self->clauses_of('select');
16 CLAUSE: foreach my $idx (0..$#clauses) {
17 if ($clauses[$idx] eq 'order_by') {
18 @before_setop = @clauses[0..$idx-1];
19 splice(@clauses, $idx, 0, qw(setop group_by having));
23 die "Huh?" unless @before_setop;
24 $self->clauses_of(select => 'with', @clauses);
25 $self->clause_expanders(
26 'select.group_by', sub {
27 $_[0]->_expand_maybe_list_expr($_[2], -ident)
29 'select.having', sub { $_[0]->expand_expr($_[2]) },
31 foreach my $thing (qw(join from_list)) {
32 $self->expander($thing => "_expand_${thing}")
33 ->renderer($thing => "_render_${thing}")
35 $self->op_expander(as => '_expand_op_as');
36 $self->expander(as => '_expand_op_as');
37 $self->renderer(as => '_render_as');
39 $self->clauses_of(update => sub {
40 my ($self, @clauses) = @_;
41 splice(@clauses, 2, 0, 'from');
45 $self->clauses_of(delete => sub {
46 my ($self, @clauses) = @_;
47 splice(@clauses, 1, 0, 'using');
51 $self->clause_expanders(
52 'update.from' => '_expand_select_clause_from',
53 'delete.using' => sub {
54 +(using => $_[0]->_expand_from_list(undef, $_[2]));
56 'insert.rowvalues' => sub {
57 +(from => $_[0]->expand_expr({ -values => $_[2] }));
59 'insert.select' => sub {
60 +(from => $_[0]->expand_expr({ -select => $_[2] }));
65 $self->wrap_expander(select => sub {
69 my $exp = $self->$orig(@_);
70 return $exp unless my $setop = (my $sel = $exp->{-select})->{setop};
71 if (my @keys = grep $sel->{$_}, @before_setop) {
72 my %inner; @inner{@keys} = delete @{$sel}{@keys};
73 unshift @{(values(%$setop))[0]{queries}},
74 { -select => \%inner };
79 my $expand_setop = sub {
80 my ($self, $setop, $args) = @_;
83 queries => [ map $self->expand_expr($_), @{$args->{queries}} ],
86 $self->expanders(map +($_ => $expand_setop), qw(union intersect except));
88 $self->clause_renderer('select.setop' => sub {
89 my ($self, undef, $setop) = @_;
90 $self->render_aqt($setop);
93 $self->renderer($_ => sub {
94 my ($self, $setop, $args) = @_;
95 $self->join_query_parts(
96 ' '.$self->format_keyword(join '_', $setop, ($args->{type}||())).' ',
99 }) for qw(union intersect except);
101 my $setop_expander = sub {
102 my ($self, $setop, $args) = @_;
103 my ($op, $type) = split '_', $setop;
104 +(setop => $self->expand_expr({
106 ($type ? (type => $type) : ()),
107 queries => (ref($args) eq 'ARRAY' ? $args : [ $args ])
112 $self->clause_expanders(
113 map +($_ => $setop_expander),
115 map +($_, "${_}_all", "${_}_distinct"),
116 qw(union intersect except)
119 $self->clause_expander('select.with' => my $with_expander = sub {
120 my ($self, $name, $with) = @_;
121 my (undef, $type) = split '_', $name;
122 if (ref($with) eq 'HASH') {
125 queries => [ map $self->expand_expr($_), @{$with->{queries}} ]
130 while (my ($name, $query) = splice @with, 0, 2) {
131 my @n = map $self->expand_expr($_, -ident),
132 ref($name) eq 'ARRAY' ? @$name : $name;
135 $self->expand_expr($query)
138 return +(with => { ($type ? (type => $type) : ()), queries => \@exp });
140 $self->clause_expander('select.with_recursive', $with_expander);
141 $self->clause_renderer('select.with' => sub {
142 my ($self, undef, $with) = @_;
143 my $q_part = $self->join_query_parts(', ',
145 my ($alias, $query) = @$_;
146 $self->join_query_parts(' ',
147 $self->_render_alias($alias),
148 $self->format_keyword('as'),
151 } @{$with->{queries}}
153 return $self->join_query_parts(' ',
154 $self->format_keyword(join '_', 'with', ($with->{type}||'')),
162 sub _expand_select_clause_from {
163 my ($self, undef, $from) = @_;
164 +(from => $self->_expand_from_list(undef, $from));
167 sub _expand_from_list {
168 my ($self, undef, $args) = @_;
169 if (ref($args) eq 'HASH') {
170 return { -from_list => [ $self->expand_expr($args) ] };
173 my @args = ref($args) eq 'ARRAY' ? @$args : ($args);
174 while (my $entry = shift @args) {
175 if (!ref($entry) and $entry =~ /^-(.*)/) {
177 $list[-1] = $self->expand_expr({ -as => [
178 $list[-1], map +(ref($_) eq 'ARRAY' ? @$_ : $_), shift(@args)
182 $entry = { $entry => shift @args };
184 my $aqt = $self->expand_expr($entry, -ident);
185 if ($aqt->{-join} and not $aqt->{-join}{from}) {
186 $aqt->{-join}{from} = pop @list;
190 return { -from_list => \@list };
194 my ($self, undef, $args) = @_;
198 : (to => $args->[0], @{$args}[1..$#$args])
200 if (my $as = delete $proto{as}) {
201 $proto{to} = { -as => [ $proto{to}, ref($as) eq 'ARRAY' ? @$as : $as ] };
203 if (defined($proto{using}) and ref(my $using = $proto{using}) ne 'HASH') {
204 $proto{using} = { -row => [
205 map [ $self->expand_expr($_, -ident) ],
206 ref($using) eq 'ARRAY' ? @$using: $using
209 my %ret = map +($_ => $self->expand_expr($proto{$_}, -ident)),
211 return +{ -join => \%ret };
214 sub _render_from_list {
215 my ($self, undef, $list) = @_;
216 return $self->join_query_parts(', ', @$list);
220 my ($self, undef, $args) = @_;
224 $self->format_keyword(join '_', ($args->{type}||()), 'join'),
225 (map +($_->{-ident} || $_->{-as} ? $_ : { -row => [ $_ ] }), $args->{to}),
227 $self->format_keyword('on') ,
231 $self->format_keyword('using'),
235 return $self->join_query_parts(' ', @parts);
239 my ($self, undef, $vv, $k) = @_;
240 my @as = map $self->expand_expr($_, -ident),
241 (defined($k) ? ($k) : ()), ref($vv) eq 'ARRAY' ? @$vv : $vv;
242 return { -as => \@as };
246 my ($self, undef, $args) = @_;
247 my ($thing, @alias) = @$args;
248 return $self->join_query_parts(
250 $self->render_aqt($thing),
251 $self->format_keyword('as'),
252 $self->_render_alias(\@alias),
257 my ($self, $args) = @_;
258 my ($as, @cols) = @$args;
260 ? $self->join_query_parts('',
263 $self->join_query_parts(
269 : $self->render_aqt($as)
273 sub _expand_update_clause_target {
274 my ($self, undef, $target) = @_;
275 +(target => $self->_expand_from_list(undef, $target));