move multop rendering to join_query_parts
[scpubgit/Q-Branch.git] / lib / SQL / Abstract / Clauses.pm
CommitLineData
bbe638af 1package SQL::Abstract::Clauses;
2
3use strict;
4use warnings;
ccb7f3d6 5use if $] < '5.010', 'MRO::Compat';
bbe638af 6use mro 'c3';
7use base 'SQL::Abstract';
8
1bcd361f 9BEGIN { *puke = \&SQL::Abstract::puke }
10
bbe638af 11sub new {
12 shift->next::method(@_)->register_defaults
13}
14
15sub register_defaults {
16 my ($self) = @_;
17 $self->{clauses_of}{select} = [ qw(select from where order_by) ];
18 $self->{expand}{select} = sub { shift->_expand_statement(@_) };
a01911a2 19 $self->{render}{select} = sub { shift->_render_statement(@_) };
df0f5af9 20 $self->{expand_clause}{"select.$_"} = "_expand_select_clause_$_"
21 for @{$self->{clauses_of}{select}};
46158aa8 22 $self->{clauses_of}{update} = [ qw(target set where returning) ];
1bcd361f 23 $self->{expand}{update} = sub { shift->_expand_statement(@_) };
a01911a2 24 $self->{render}{update} = sub { shift->_render_statement(@_) };
889fdd38 25 $self->{expand_clause}{"update.$_"} = "_expand_update_clause_$_"
26 for @{$self->{clauses_of}{update}};
46158aa8 27 $self->{expand_clause}{'update.update'} = '_expand_update_clause_target';
28 $self->{render_clause}{'update.target'} = sub {
fe8b493f 29 my ($self, undef, $target) = @_;
e7f248c4 30 $self->join_query_parts(' ', $self->format_keyword('update'), $target);
46158aa8 31 };
32 $self->{clauses_of}{delete} = [ qw(target where returning) ];
77e8c03d 33 $self->{expand}{delete} = sub { shift->_expand_statement(@_) };
a01911a2 34 $self->{render}{delete} = sub { shift->_render_statement(@_) };
44296b4e 35 $self->{expand_clause}{"delete.$_"} = "_expand_delete_clause_$_"
36 for @{$self->{clauses_of}{delete}};
46158aa8 37 $self->{expand_clause}{"delete.from"} = '_expand_delete_clause_target';
38 $self->{render_clause}{'delete.target'} = sub {
fe8b493f 39 my ($self, undef, $from) = @_;
e7f248c4 40 $self->join_query_parts(' ', $self->format_keyword('delete from'), $from);
46158aa8 41 };
2b057b28 42 $self->{clauses_of}{insert} = [
93253a11 43 'target', 'fields', 'from', 'returning'
2b057b28 44 ];
39544e0e 45 $self->{expand}{insert} = sub { shift->_expand_statement(@_) };
a01911a2 46 $self->{render}{insert} = sub { shift->_render_statement(@_) };
ddbe2793 47 $self->{expand_clause}{"insert.$_"} = "_expand_insert_clause_$_"
48 for @{$self->{clauses_of}{insert}};
46158aa8 49 $self->{expand_clause}{'insert.into'} = '_expand_insert_clause_target';
ddbe2793 50 $self->{expand_clause}{'insert.values'} = '_expand_insert_clause_from';
2b057b28 51 $self->{render_clause}{'insert.fields'} = sub {
fe8b493f 52 return $_[0]->render_aqt($_[2]);
2b057b28 53 };
46158aa8 54 $self->{render_clause}{'insert.target'} = sub {
fe8b493f 55 my ($self, undef, $from) = @_;
e7f248c4 56 $self->join_query_parts(' ', $self->format_keyword('insert into'), $from);
46158aa8 57 };
93253a11 58 $self->{render_clause}{'insert.from'} = sub {
fe8b493f 59 return $_[0]->render_aqt($_[2], 1);
f8d50921 60 };
01222ad2 61 $self->{expand}{values} = '_expand_values';
62 $self->{render}{values} = '_render_values';
7250aa13 63 $self->{expand}{exists} = sub {
64 $_[0]->_expand_op(undef, [ exists => $_[2] ]);
65 };
38c5397d 66 $self->{render}{convert_where} = sub {
67 my $self = shift;
68 local $self->{convert_where} = $self->{convert};
e7f248c4 69 $self->render_aqt($_[1]);
38c5397d 70 };
bbe638af 71 return $self;
72}
73
df0f5af9 74sub _expand_select_clause_select {
fe8b493f 75 my ($self, undef, $select) = @_;
df0f5af9 76 +(select => $self->_expand_maybe_list_expr($select, -ident));
77}
78
79sub _expand_select_clause_from {
fe8b493f 80 my ($self, undef, $from) = @_;
df0f5af9 81 +(from => $self->_expand_maybe_list_expr($from, -ident));
82}
83
84sub _expand_select_clause_where {
fe8b493f 85 my ($self, undef, $where) = @_;
38c5397d 86 my $exp = $self->expand_expr($where);
e7f248c4 87 +(where => ($self->{convert} ? +{ -convert_where => $exp } : $exp));
df0f5af9 88}
89
90sub _expand_select_clause_order_by {
fe8b493f 91 my ($self, undef, $order_by) = @_;
df0f5af9 92 +(order_by => $self->_expand_order_by($order_by));
93}
94
46158aa8 95sub _expand_update_clause_target {
fe8b493f 96 my ($self, undef, $target) = @_;
e0eb8d26 97 +(target => $self->_expand_maybe_list_expr($target, -ident));
889fdd38 98}
99
100sub _expand_update_clause_set {
fe8b493f 101 return $_[2] if ref($_[2]) eq 'HASH' and ($_[2]->{-op}||[''])->[0] eq ',';
102 +(set => $_[0]->_expand_update_set_values($_[1], $_[2]));
889fdd38 103}
104
105sub _expand_update_clause_where {
fe8b493f 106 +(where => $_[0]->expand_expr($_[2]));
889fdd38 107}
108
109sub _expand_update_clause_returning {
fe8b493f 110 +(returning => $_[0]->_expand_maybe_list_expr($_[2], -ident));
889fdd38 111}
112
46158aa8 113sub _expand_delete_clause_target {
fe8b493f 114 +(target => $_[0]->_expand_maybe_list_expr($_[2], -ident));
44296b4e 115}
116
fe8b493f 117sub _expand_delete_clause_where { +(where => $_[0]->expand_expr($_[2])); }
44296b4e 118
119sub _expand_delete_clause_returning {
fe8b493f 120 +(returning => $_[0]->_expand_maybe_list_expr($_[2], -ident));
44296b4e 121}
122
bbe638af 123sub _expand_statement {
124 my ($self, $type, $args) = @_;
125 my $ec = $self->{expand_clause};
f9f1fdcd 126 if ($args->{_}) {
127 $args = { %$args };
128 $args->{$type} = delete $args->{_}
129 }
27d61b67 130 return +{ "-${type}" => +{
c79c67d7 131 map {
bbe638af 132 my $val = $args->{$_};
133 if (defined($val) and my $exp = $ec->{"${type}.$_"}) {
fe8b493f 134 if ((my (@exp) = $self->$exp($_ => $val)) == 1) {
c79c67d7 135 ($_ => $exp[0])
136 } else {
137 @exp
138 }
bbe638af 139 } else {
93253a11 140 ($_ => $self->expand_expr($val))
bbe638af 141 }
c79c67d7 142 } sort keys %$args
27d61b67 143 } };
bbe638af 144}
145
146sub _render_statement {
147 my ($self, $type, $args) = @_;
148 my @parts;
149 foreach my $clause (@{$self->{clauses_of}{$type}}) {
150 next unless my $clause_expr = $args->{$clause};
5e4aaea3 151 my @part = do {
2b057b28 152 if (my $rdr = $self->{render_clause}{"${type}.${clause}"}) {
fe8b493f 153 $self->$rdr($clause, $clause_expr);
2b057b28 154 } else {
01222ad2 155 my ($clause_sql, @bind) = $self->render_aqt($clause_expr, 1);
2b057b28 156 my $sql = join ' ',
157 $self->_sqlcase(join ' ', split '_', $clause),
158 $clause_sql;
159 ($sql, @bind);
160 }
161 };
2b057b28 162 push @parts, \@part;
bbe638af 163 }
0236f122 164 my ($sql, @bind) = $self->join_query_parts(' ', @parts);
01222ad2 165 return (
166 (our $Render_Top_Level ? $sql : '('.$sql.')'),
167 @bind
168 );
169}
170
171sub render_aqt {
172 my ($self, $aqt, $top_level) = @_;
173 local our $Render_Top_Level = $top_level;
174 return $self->next::method($aqt);
175}
176
177sub render_statement {
178 my ($self, $expr, $default_scalar_to) = @_;
179 my ($sql, @bind) = $self->render_aqt(
180 $self->expand_expr($expr, $default_scalar_to), 1
181 );
182 return (wantarray ? ($sql, @bind) : $sql);
bbe638af 183}
184
81b270e7 185sub select {
186 my ($self, @args) = @_;
7958f5c6 187
01222ad2 188 return $self->render_statement({ -select => $_[1] }) if ref($_[1]) eq 'HASH';
7958f5c6 189
81b270e7 190 my %clauses;
191 @clauses{qw(from select where order_by)} = @args;
8e6988ce 192
81b270e7 193 # This oddity is to literalify since historically SQLA doesn't quote
8e6988ce 194 # a single identifier argument, so we convert it into a literal
195
4ee74413 196 $clauses{select} = { -literal => [ $clauses{select}||'*' ] }
197 unless ref($clauses{select});
198
01222ad2 199 return $self->render_statement({ -select => \%clauses });
81b270e7 200}
201
1bcd361f 202sub update {
203 my ($self, $table, $set, $where, $options) = @_;
7958f5c6 204
01222ad2 205 return $self->render_statement({ -update => $_[1] }) if ref($_[1]) eq 'HASH';
7958f5c6 206
1bcd361f 207 my %clauses;
46158aa8 208 @clauses{qw(target set where)} = ($table, $set, $where);
1bcd361f 209 puke "Unsupported data type specified to \$sql->update"
210 unless ref($clauses{set}) eq 'HASH';
211 @clauses{keys %$options} = values %$options;
01222ad2 212 return $self->render_statement({ -update => \%clauses });
1bcd361f 213}
214
77e8c03d 215sub delete {
216 my ($self, $table, $where, $options) = @_;
7958f5c6 217
01222ad2 218 return $self->render_statement({ -delete => $_[1] }) if ref($_[1]) eq 'HASH';
7958f5c6 219
46158aa8 220 my %clauses = (target => $table, where => $where, %{$options||{}});
01222ad2 221 return $self->render_statement({ -delete => \%clauses });
77e8c03d 222}
223
39544e0e 224sub insert {
225 my ($self, $table, $data, $options) = @_;
7958f5c6 226
01222ad2 227 return $self->render_statement({ -insert => $_[1] }) if ref($_[1]) eq 'HASH';
7958f5c6 228
46158aa8 229 my %clauses = (target => $table, values => $data, %{$options||{}});
01222ad2 230 return $self->render_statement({ -insert => \%clauses });
39544e0e 231}
232
46158aa8 233sub _expand_insert_clause_target {
fe8b493f 234 +(target => $_[0]->_expand_maybe_list_expr($_[2], -ident));
46158aa8 235}
236
ddbe2793 237sub _expand_insert_clause_fields {
238 return +{ -row => [
239 $_[0]->_expand_maybe_list_expr($_[2], -ident)
240 ] } if ref($_[2]) eq 'ARRAY';
241 return $_[2]; # should maybe still expand somewhat?
242}
243
244sub _expand_insert_clause_from {
fe8b493f 245 my ($self, undef, $data) = @_;
f8d50921 246 if (ref($data) eq 'HASH' and (keys(%$data))[0] =~ /^-/) {
247 return $self->expand_expr($data);
248 }
e5feec9f 249 return $data if ref($data) eq 'HASH' and $data->{-row};
f33d93b5 250 my ($f_aqt, $v_aqt) = $self->_expand_insert_values($data);
93253a11 251 return (from => { -values => $v_aqt }, ($f_aqt ? (fields => $f_aqt) : ()));
f33d93b5 252}
253
ddbe2793 254sub _expand_insert_clause_returning {
255 +(returning => $_[0]->_expand_maybe_list_expr($_[2], -ident));
256}
257
01222ad2 258sub _expand_values {
259 my ($self, undef, $values) = @_;
260 return { -values => [
261 map +(
262 ref($_) eq 'HASH'
263 ? $self->expand_expr($_)
264 : +{ -row => [ map $self->expand_expr($_), @$_ ] }
265 ), ref($values) eq 'ARRAY' ? @$values : $values
266 ] };
267}
268
269sub _render_values {
a01911a2 270 my ($self, undef, $values) = @_;
e7f248c4 271 my $inner = [
272 $self->join_query_parts(' ',
273 $self->format_keyword('values'),
274 [ $self->join_query_parts(', ',
275 ref($values) eq 'ARRAY' ? @$values : $values
276 ) ],
277 ),
278 ];
279 return $self->join_query_parts('',
280 (our $Render_Top_Level ? $inner : ('(', $inner, ')'))
01222ad2 281 );
282}
283
34570093 284sub _ext_rw {
285 my ($self, $name, $key, $value) = @_;
2b0b3d43 286 return $self->{$name}{$key} unless @_ > 3;
34570093 287 $self->{$name}{$key} = $value;
288 return $self;
289}
290
d390df44 291BEGIN {
292 foreach my $type (qw(
2b0b3d43 293 expand op_expand render op_render clause_expand clause_render
d390df44 294 )) {
3f9899e5 295 my $name = join '_', reverse split '_', $type;
58ebc7fa 296 my $singular = "${type}er";
3f9899e5 297 eval qq{sub ${singular} { shift->_ext_rw($name => \@_) }; 1 }
298 or die "Method builder failed for ${singular}: $@";
299 eval qq{sub wrap_${singular} {
300 my (\$self, \$key, \$builder) = \@_;
301 my \$orig = \$self->_ext_rw('${name}', \$key);
302 \$self->_ext_rw(
303 '${name}', \$key,
304 \$builder->(\$orig, '${name}', \$key)
305 );
306 }; 1 } or die "Method builder failed for wrap_${singular}: $@";
58ebc7fa 307 eval qq{sub ${singular}s {
308 my (\$self, \@args) = \@_;
309 while (my (\$this_key, \$this_value) = splice(\@args, 0, 2)) {
3f9899e5 310 \$self->{${name}}{\$this_key} = \$this_value;
58ebc7fa 311 }
312 return \$self;
313 }; 1 } or die "Method builder failed for ${singular}s: $@";
3f9899e5 314 eval qq{sub ${singular}_list { sort keys %{\$_[0]->{\$name}} }; 1; }
315 or die "Method builder failed for ${singular}_list: $@";
d390df44 316 }
317}
34570093 318
a17df223 319sub statement_list { sort keys %{$_[0]->{clauses_of}} }
54a2a1e5 320
34570093 321sub clauses_of {
322 my ($self, $of, @clauses) = @_;
323 unless (@clauses) {
324 return @{$self->{clauses_of}{$of}||[]};
325 }
ee706e89 326 if (ref($clauses[0]) eq 'CODE') {
327 @clauses = $self->${\($clauses[0])}(@{$self->{clauses_of}{$of}||[]});
328 }
34570093 329 $self->{clauses_of}{$of} = \@clauses;
330 return $self;
331}
332
333sub clone {
334 my ($self) = @_;
335 bless(
336 {
337 (map +($_ => (
338 ref($self->{$_}) eq 'HASH'
339 ? { %{$self->{$_}} }
340 : $self->{$_}
341 )), keys %$self),
342 },
343 ref($self)
344 );
345}
346
bbe638af 3471;