test is case insensitive, result should be lower cased
[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 {
7c532574 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 {
7c532574 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 };
5d4463f6 66
67 # check for overriden methods
68 if ($self->can('_table') ne SQL::Abstract->can('_table')) {
69 $self->{expand_clause}{'select.from'} = sub {
70 return +{ -literal => [ $_[0]->_table($_[2]) ] };
71 };
72 }
73 if ($self->can('_order_by') ne SQL::Abstract->can('_order_by')) {
74 $self->{expand_clause}{'select.order_by'} = sub {
75 my ($osql, @obind) = $_[0]->_order_by($_[2]);
76 $osql =~ s/^order by //i;
77 return undef unless length($osql);
78 return +{ -literal => [ $osql, @obind ] };
79 };
80 }
bbe638af 81 return $self;
82}
83
df0f5af9 84sub _expand_select_clause_select {
fe8b493f 85 my ($self, undef, $select) = @_;
df0f5af9 86 +(select => $self->_expand_maybe_list_expr($select, -ident));
87}
88
89sub _expand_select_clause_from {
fe8b493f 90 my ($self, undef, $from) = @_;
df0f5af9 91 +(from => $self->_expand_maybe_list_expr($from, -ident));
92}
93
94sub _expand_select_clause_where {
fe8b493f 95 my ($self, undef, $where) = @_;
67e082b8 96
91c490fa 97 my $sqla = do {
98 if (my $conv = $self->{convert}) {
99 my $_wrap = sub {
100 my $orig = shift;
101 sub {
102 my $self = shift;
103 +{ -func => [
104 $conv,
105 $self->$orig(@_)
106 ] };
107 };
108 };
109 $self->clone
110 ->wrap_expanders(map +($_ => $_wrap), qw(ident value bind))
111 ->wrap_op_expanders(map +($_ => $_wrap), qw(ident value bind))
112 ->wrap_expander(func => sub {
113 my $orig = shift;
114 sub {
115 my ($self, $type, $thing) = @_;
116 if (ref($thing) eq 'ARRAY' and $thing->[0] eq $conv
117 and @$thing == 2 and ref($thing->[1]) eq 'HASH'
118 and (
119 $thing->[1]{-ident}
120 or $thing->[1]{-value}
121 or $thing->[1]{-bind})
122 ) {
123 return { -func => $thing }; # already went through our expander
124 }
125 return $self->$orig($type, $thing);
126 }
127 });
128 } else {
129 $self;
67e082b8 130 }
91c490fa 131 };
67e082b8 132
91c490fa 133 return +(where => $sqla->expand_expr($where));
df0f5af9 134}
135
136sub _expand_select_clause_order_by {
fe8b493f 137 my ($self, undef, $order_by) = @_;
df0f5af9 138 +(order_by => $self->_expand_order_by($order_by));
139}
140
46158aa8 141sub _expand_update_clause_target {
fe8b493f 142 my ($self, undef, $target) = @_;
e0eb8d26 143 +(target => $self->_expand_maybe_list_expr($target, -ident));
889fdd38 144}
145
146sub _expand_update_clause_set {
fe8b493f 147 return $_[2] if ref($_[2]) eq 'HASH' and ($_[2]->{-op}||[''])->[0] eq ',';
148 +(set => $_[0]->_expand_update_set_values($_[1], $_[2]));
889fdd38 149}
150
151sub _expand_update_clause_where {
fe8b493f 152 +(where => $_[0]->expand_expr($_[2]));
889fdd38 153}
154
155sub _expand_update_clause_returning {
fe8b493f 156 +(returning => $_[0]->_expand_maybe_list_expr($_[2], -ident));
889fdd38 157}
158
46158aa8 159sub _expand_delete_clause_target {
fe8b493f 160 +(target => $_[0]->_expand_maybe_list_expr($_[2], -ident));
44296b4e 161}
162
fe8b493f 163sub _expand_delete_clause_where { +(where => $_[0]->expand_expr($_[2])); }
44296b4e 164
165sub _expand_delete_clause_returning {
fe8b493f 166 +(returning => $_[0]->_expand_maybe_list_expr($_[2], -ident));
44296b4e 167}
168
bbe638af 169sub _expand_statement {
170 my ($self, $type, $args) = @_;
171 my $ec = $self->{expand_clause};
f9f1fdcd 172 if ($args->{_}) {
173 $args = { %$args };
174 $args->{$type} = delete $args->{_}
175 }
27d61b67 176 return +{ "-${type}" => +{
c79c67d7 177 map {
bbe638af 178 my $val = $args->{$_};
179 if (defined($val) and my $exp = $ec->{"${type}.$_"}) {
fe8b493f 180 if ((my (@exp) = $self->$exp($_ => $val)) == 1) {
c79c67d7 181 ($_ => $exp[0])
182 } else {
183 @exp
184 }
bbe638af 185 } else {
93253a11 186 ($_ => $self->expand_expr($val))
bbe638af 187 }
c79c67d7 188 } sort keys %$args
27d61b67 189 } };
bbe638af 190}
191
192sub _render_statement {
193 my ($self, $type, $args) = @_;
194 my @parts;
195 foreach my $clause (@{$self->{clauses_of}{$type}}) {
196 next unless my $clause_expr = $args->{$clause};
7c532574 197 my $part = do {
2b057b28 198 if (my $rdr = $self->{render_clause}{"${type}.${clause}"}) {
fe8b493f 199 $self->$rdr($clause, $clause_expr);
2b057b28 200 } else {
7c532574 201 $self->join_query_parts(' ',
202 $self->format_keyword($clause),
203 $self->render_aqt($clause_expr, 1)
204 );
2b057b28 205 }
206 };
7c532574 207 push @parts, $part;
bbe638af 208 }
531d5231 209 my $q = $self->join_query_parts(' ', @parts);
210 return $self->join_query_parts('',
211 (our $Render_Top_Level ? $q : ('(', $q, ')'))
212 );
01222ad2 213}
214
215sub render_aqt {
216 my ($self, $aqt, $top_level) = @_;
217 local our $Render_Top_Level = $top_level;
218 return $self->next::method($aqt);
219}
220
221sub render_statement {
222 my ($self, $expr, $default_scalar_to) = @_;
21ad3e4b 223 @{$self->render_aqt(
01222ad2 224 $self->expand_expr($expr, $default_scalar_to), 1
21ad3e4b 225 )};
bbe638af 226}
227
81b270e7 228sub select {
229 my ($self, @args) = @_;
7c532574 230 my $stmt = do {
231 if (ref(my $sel = $args[0]) eq 'HASH') {
3f312d2e 232 $sel
7c532574 233 } else {
234 my %clauses;
235 @clauses{qw(from select where order_by)} = @args;
7958f5c6 236
7c532574 237 # This oddity is to literalify since historically SQLA doesn't quote
238 # a single identifier argument, so we convert it into a literal
8e6988ce 239
7c532574 240 $clauses{select} = { -literal => [ $clauses{select}||'*' ] }
241 unless ref($clauses{select});
242 \%clauses;
243 }
244 };
4ee74413 245
21ad3e4b 246 my @rendered = $self->render_statement({ -select => $stmt });
247 return wantarray ? @rendered : $rendered[0];
81b270e7 248}
249
1bcd361f 250sub update {
251 my ($self, $table, $set, $where, $options) = @_;
7958f5c6 252
3f312d2e 253 my $stmt = do {
254 if (ref($table) eq 'HASH') {
255 $table
256 } else {
257 my %clauses;
258 @clauses{qw(target set where)} = ($table, $set, $where);
259 puke "Unsupported data type specified to \$sql->update"
260 unless ref($clauses{set}) eq 'HASH';
261 @clauses{keys %$options} = values %$options;
262 \%clauses;
263 }
264 };
21ad3e4b 265 my @rendered = $self->render_statement({ -update => $stmt });
266 return wantarray ? @rendered : $rendered[0];
1bcd361f 267}
268
77e8c03d 269sub delete {
270 my ($self, $table, $where, $options) = @_;
7958f5c6 271
3f312d2e 272 my $stmt = do {
273 if (ref($table) eq 'HASH') {
274 $table;
275 } else {
276 my %clauses = (target => $table, where => $where, %{$options||{}});
277 \%clauses;
278 }
279 };
21ad3e4b 280 my @rendered = $self->render_statement({ -delete => $stmt });
281 return wantarray ? @rendered : $rendered[0];
77e8c03d 282}
283
39544e0e 284sub insert {
285 my ($self, $table, $data, $options) = @_;
7958f5c6 286
3f312d2e 287 my $stmt = do {
288 if (ref($table) eq 'HASH') {
289 $table;
290 } else {
291 my %clauses = (target => $table, values => $data, %{$options||{}});
292 \%clauses;
293 }
294 };
21ad3e4b 295 my @rendered = $self->render_statement({ -insert => $stmt });
296 return wantarray ? @rendered : $rendered[0];
39544e0e 297}
298
46158aa8 299sub _expand_insert_clause_target {
fe8b493f 300 +(target => $_[0]->_expand_maybe_list_expr($_[2], -ident));
46158aa8 301}
302
ddbe2793 303sub _expand_insert_clause_fields {
304 return +{ -row => [
305 $_[0]->_expand_maybe_list_expr($_[2], -ident)
306 ] } if ref($_[2]) eq 'ARRAY';
307 return $_[2]; # should maybe still expand somewhat?
308}
309
310sub _expand_insert_clause_from {
fe8b493f 311 my ($self, undef, $data) = @_;
f8d50921 312 if (ref($data) eq 'HASH' and (keys(%$data))[0] =~ /^-/) {
313 return $self->expand_expr($data);
314 }
e5feec9f 315 return $data if ref($data) eq 'HASH' and $data->{-row};
f33d93b5 316 my ($f_aqt, $v_aqt) = $self->_expand_insert_values($data);
67e082b8 317 return (
318 from => { -values => [ $v_aqt ] },
319 ($f_aqt ? (fields => $f_aqt) : ()),
320 );
f33d93b5 321}
322
ddbe2793 323sub _expand_insert_clause_returning {
324 +(returning => $_[0]->_expand_maybe_list_expr($_[2], -ident));
325}
326
01222ad2 327sub _expand_values {
328 my ($self, undef, $values) = @_;
329 return { -values => [
330 map +(
331 ref($_) eq 'HASH'
332 ? $self->expand_expr($_)
333 : +{ -row => [ map $self->expand_expr($_), @$_ ] }
334 ), ref($values) eq 'ARRAY' ? @$values : $values
335 ] };
336}
337
338sub _render_values {
a01911a2 339 my ($self, undef, $values) = @_;
3f312d2e 340 my $inner = $self->join_query_parts(' ',
341 $self->format_keyword('values'),
342 $self->join_query_parts(', ',
343 ref($values) eq 'ARRAY' ? @$values : $values
e7f248c4 344 ),
3f312d2e 345 );
e7f248c4 346 return $self->join_query_parts('',
347 (our $Render_Top_Level ? $inner : ('(', $inner, ')'))
01222ad2 348 );
349}
350
34570093 351sub _ext_rw {
352 my ($self, $name, $key, $value) = @_;
2b0b3d43 353 return $self->{$name}{$key} unless @_ > 3;
34570093 354 $self->{$name}{$key} = $value;
355 return $self;
356}
357
d390df44 358BEGIN {
359 foreach my $type (qw(
2b0b3d43 360 expand op_expand render op_render clause_expand clause_render
d390df44 361 )) {
3f9899e5 362 my $name = join '_', reverse split '_', $type;
58ebc7fa 363 my $singular = "${type}er";
3f9899e5 364 eval qq{sub ${singular} { shift->_ext_rw($name => \@_) }; 1 }
365 or die "Method builder failed for ${singular}: $@";
366 eval qq{sub wrap_${singular} {
367 my (\$self, \$key, \$builder) = \@_;
368 my \$orig = \$self->_ext_rw('${name}', \$key);
369 \$self->_ext_rw(
370 '${name}', \$key,
371 \$builder->(\$orig, '${name}', \$key)
372 );
373 }; 1 } or die "Method builder failed for wrap_${singular}: $@";
58ebc7fa 374 eval qq{sub ${singular}s {
375 my (\$self, \@args) = \@_;
376 while (my (\$this_key, \$this_value) = splice(\@args, 0, 2)) {
4dd6472f 377 \$self->_ext_rw('${name}', \$this_key, \$this_value);
58ebc7fa 378 }
379 return \$self;
380 }; 1 } or die "Method builder failed for ${singular}s: $@";
4dd6472f 381 eval qq{sub wrap_${singular}s {
382 my (\$self, \@args) = \@_;
383 while (my (\$this_key, \$this_builder) = splice(\@args, 0, 2)) {
384 my \$orig = \$self->_ext_rw('${name}', \$this_key);
385 \$self->_ext_rw(
386 '${name}', \$this_key,
387 \$this_builder->(\$orig, '${name}', \$this_key),
388 );
389 }
390 return \$self;
391 }; 1 } or die "Method builder failed for wrap_${singular}s: $@";
3f9899e5 392 eval qq{sub ${singular}_list { sort keys %{\$_[0]->{\$name}} }; 1; }
393 or die "Method builder failed for ${singular}_list: $@";
d390df44 394 }
395}
34570093 396
550eaa6f 397sub register_op { $_[0]->{is_op}{$_[1]} = 1; $_[0] }
398
a17df223 399sub statement_list { sort keys %{$_[0]->{clauses_of}} }
54a2a1e5 400
34570093 401sub clauses_of {
402 my ($self, $of, @clauses) = @_;
403 unless (@clauses) {
404 return @{$self->{clauses_of}{$of}||[]};
405 }
ee706e89 406 if (ref($clauses[0]) eq 'CODE') {
407 @clauses = $self->${\($clauses[0])}(@{$self->{clauses_of}{$of}||[]});
408 }
34570093 409 $self->{clauses_of}{$of} = \@clauses;
410 return $self;
411}
412
413sub clone {
414 my ($self) = @_;
415 bless(
416 {
417 (map +($_ => (
418 ref($self->{$_}) eq 'HASH'
419 ? { %{$self->{$_}} }
420 : $self->{$_}
421 )), keys %$self),
422 },
423 ref($self)
424 );
425}
426
bbe638af 4271;