refactor to add the rest of the setops
[scpubgit/Q-Branch.git] / lib / SQL / Abstract / ExtraClauses.pm
CommitLineData
1ff9018c 1package SQL::Abstract::ExtraClauses;
2
3use strict;
4use warnings;
5use if $] < '5.010', 'MRO::Compat';
6use mro 'c3';
7use base qw(SQL::Abstract::Clauses);
8
9BEGIN { *puke = \&SQL::Abstract::puke }
10
b4546197 11sub register_defaults {
12 my $self = shift;
13 $self->next::method(@_);
2b0b3d43 14 my @clauses = $self->clauses_of('select');
15 my @before_setop;
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));
20 last CLAUSE;
21 }
22 }
23 die "Huh?" unless @before_setop;
24 $self->clauses_of(select => @clauses);
58ebc7fa 25 $self->clause_expanders(
26 'select.group_by', sub {
27 $_[0]->_expand_maybe_list_expr($_[1], -ident)
28 },
29 'select.having', 'expand_expr',
30 );
a6227174 31 foreach my $thing (qw(join from_list)) {
32 $self->expander($thing => "_expand_${thing}")
33 ->renderer($thing => "_render_${thing}")
34 }
555ad787 35 $self->op_expander(as => '_expand_op_as');
36 $self->expander(as => '_expand_op_as');
37 $self->renderer(as => '_render_as');
58ebc7fa 38
ee706e89 39 $self->clauses_of(update => sub {
40 my ($self, @clauses) = @_;
41 splice(@clauses, 2, 0, 'from');
42 @clauses;
43 });
44
45 $self->clauses_of(delete => sub {
46 my ($self, @clauses) = @_;
47 splice(@clauses, 1, 0, 'using');
48 @clauses;
49 });
50
58ebc7fa 51 $self->clause_expanders(
52 'update.from' => '_expand_select_clause_from',
53 'delete.using' => sub {
54 +(using => $_[0]->_expand_from_list(undef, $_[1]));
55 },
56 'insert.rowvalues' => sub {
57 +(from => $_[0]->expand_expr({ -values => $_[1] }));
58 },
59 'insert.select' => sub {
60 +(from => $_[0]->expand_expr({ -select => $_[1] }));
61 },
62 );
26994fdd 63
2b0b3d43 64 # set ops
65 {
66 my $orig = $self->expander('select');
67 $self->expander(select => sub {
68 my $self = shift;
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 };
75 }
76 return $exp;
77 });
78 }
d175037f 79 my $expand_setop = sub {
80 my ($self, $setop, $args) = @_;
81 +{ "-${setop}" => {
2b0b3d43 82 %$args,
83 queries => [ map $self->expand_expr($_), @{$args->{queries}} ],
84 } };
d175037f 85 };
86 $self->expanders(map +($_ => $expand_setop), qw(union intersect except));
2b0b3d43 87
88 $self->clause_renderer('select.setop' => sub {
89 my ($self, $setop) = @_;
90 $self->render_aqt($setop);
91 });
92
d175037f 93 foreach my $setop (qw(union intersect except)) {
94 $self->renderer($setop => sub {
95 my ($self, $args) = @_;
96 $self->join_clauses(
97 ' '.$self->format_keyword(join '_', $setop, ($args->{type}||())).' ',
98 map [ $self->render_aqt($_) ], @{$args->{queries}}
99 );
100 });
101
102 $self->clause_expander("select.${setop}" => sub {
103 +(setop => $_[0]->expand_expr({
104 "-${setop}" => {
105 queries => (ref($_[1]) eq 'ARRAY' ? $_[1] : [ $_[1] ]),
106 }
107 }));
108 });
109 $self->clause_expander("select.${setop}_all" => sub {
110 +(setop => $_[0]->expand_expr({
111 "-${setop}" => {
112 type => 'all',
113 queries => (ref($_[1]) eq 'ARRAY' ? $_[1] : [ $_[1] ]),
114 }
115 }));
116 });
117 }
2b0b3d43 118
b4546197 119 return $self;
1ff9018c 120}
121
2b0b3d43 122sub format_keyword { $_[0]->_sqlcase(join ' ', split '_', $_[1]) }
123
7741b7ad 124sub _expand_select_clause_from {
125 my ($self, $from) = @_;
126 +(from => $self->_expand_from_list(undef, $from));
127}
128
129sub _expand_from_list {
130 my ($self, undef, $args) = @_;
131 if (ref($args) eq 'HASH') {
132 return { -from_list => [ $self->expand_expr($args) ] };
133 }
134 my @list;
86a6ebf4 135 my @args = ref($args) eq 'ARRAY' ? @$args : ($args);
7741b7ad 136 while (my $entry = shift @args) {
b99e9a14 137 if (!ref($entry) and $entry =~ /^-(.*)/) {
138 if ($1 eq 'as') {
139 $list[-1] = $self->expand_expr({ -as => [
140 $list[-1], map +(ref($_) eq 'ARRAY' ? @$_ : $_), shift(@args)
141 ]});
142 next;
143 }
7741b7ad 144 $entry = { $entry => shift @args };
145 }
146 my $aqt = $self->expand_expr($entry, -ident);
147 if ($aqt->{-join} and not $aqt->{-join}{from}) {
148 $aqt->{-join}{from} = pop @list;
149 }
150 push @list, $aqt;
151 }
152 return { -from_list => \@list };
153}
154
155sub _expand_join {
156 my ($self, undef, $args) = @_;
157 my %proto = (
158 ref($args) eq 'HASH'
159 ? %$args
160 : (to => $args->[0], @{$args}[1..$#$args])
161 );
b99e9a14 162 if (my $as = delete $proto{as}) {
163 $proto{to} = { -as => [ $proto{to}, ref($as) eq 'ARRAY' ? @$as : $as ] };
164 }
e0eb8d26 165 if (defined($proto{using}) and ref(my $using = $proto{using}) ne 'HASH') {
166 $proto{using} = { -row => [
167 map [ $self->expand_expr($_, -ident) ],
168 ref($using) eq 'ARRAY' ? @$using: $using
169 ] };
170 }
7741b7ad 171 my %ret = map +($_ => $self->expand_expr($proto{$_}, -ident)),
172 sort keys %proto;
173 return +{ -join => \%ret };
174}
175
176sub _render_from_list {
177 my ($self, $list) = @_;
e5dc19f6 178 return $self->join_clauses(', ', map [ $self->render_aqt($_) ], @$list);
7741b7ad 179}
180
181sub _render_join {
182 my ($self, $args) = @_;
183
184 my @parts = (
185 [ $self->render_aqt($args->{from}) ],
2b0b3d43 186 [ $self->format_keyword(join '_', ($args->{type}||()), 'join') ],
4a2c263b 187 [ $self->render_aqt(
e0eb8d26 188 map +($_->{-ident} || $_->{-as} ? $_ : { -row => [ $_ ] }), $args->{to}
4a2c263b 189 ) ],
7741b7ad 190 ($args->{on} ? (
2b0b3d43 191 [ $self->format_keyword('on') ],
7741b7ad 192 [ $self->render_aqt($args->{on}) ],
193 ) : ()),
194 ($args->{using} ? (
2b0b3d43 195 [ $self->format_keyword('using') ],
7741b7ad 196 [ $self->render_aqt($args->{using}) ],
197 ) : ()),
198 );
e5dc19f6 199 return $self->join_clauses(' ', @parts);
7741b7ad 200}
201
b99e9a14 202sub _expand_op_as {
203 my ($self, undef, $vv, $k) = @_;
204 my @as = map $self->expand_expr($_, -ident),
205 (defined($k) ? ($k) : ()), ref($vv) eq 'ARRAY' ? @$vv : $vv;
206 return { -as => \@as };
207}
208
209sub _render_as {
210 my ($self, $args) = @_;
211 my ($thing, $as, @cols) = @$args;
e5dc19f6 212 return $self->join_clauses(
b99e9a14 213 ' ',
01222ad2 214 [ $self->render_aqt($thing) ],
2b0b3d43 215 [ $self->format_keyword('as') ],
4a2c263b 216 (@cols
e5dc19f6 217 ? [ $self->join_clauses('',
4a2c263b 218 [ $self->render_aqt($as) ],
219 [ '(' ],
e5dc19f6 220 [ $self->join_clauses(
4a2c263b 221 ', ',
222 map [ $self->render_aqt($_) ], @cols
223 ) ],
224 [ ')' ],
225 ) ]
226 : [ $self->render_aqt($as) ]
227 ),
b99e9a14 228 );
229}
230
1ff9018c 2311;