squash, fixes subquery with insert_bulk
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / SQLMaker.pm
1 package DBIx::Class::SQLMaker;
2
3 use strict;
4 use warnings;
5
6 =head1 NAME
7
8 DBIx::Class::SQLMaker - An SQL::Abstract-based SQL maker class
9
10 =head1 DESCRIPTION
11
12 This module is a subclass of L<SQL::Abstract> and includes a number of
13 DBIC-specific workarounds, not yet suitable for inclusion into the
14 L<SQL::Abstract> core. It also provides all (and more than) the functionality
15 of L<SQL::Abstract::Limit>, see L<DBIx::Class::SQLMaker::LimitDialects> for
16 more info.
17
18 Currently the enhancements to L<SQL::Abstract> are:
19
20 =over
21
22 =item * Support for C<JOIN> statements (via extended C<table/from> support)
23
24 =item * Support of functions in C<SELECT> lists
25
26 =item * C<GROUP BY>/C<HAVING> support (via extensions to the order_by parameter)
27
28 =item * Support of C<...FOR UPDATE> type of select statement modifiers
29
30 =back
31
32 =cut
33
34 use base qw/
35   DBIx::Class::SQLMaker::LimitDialects
36   SQL::Abstract
37   DBIx::Class
38 /;
39 use mro 'c3';
40
41 use Sub::Name 'subname';
42 use DBIx::Class::Carp;
43 use namespace::clean;
44
45 __PACKAGE__->mk_group_accessors (simple => qw/quote_char name_sep limit_dialect/);
46
47 # for when I need a normalized l/r pair
48 sub _quote_chars {
49   map
50     { defined $_ ? $_ : '' }
51     ( ref $_[0]->{quote_char} ? (@{$_[0]->{quote_char}}) : ( ($_[0]->{quote_char}) x 2 ) )
52   ;
53 }
54
55 # FIXME when we bring in the storage weaklink, check its schema
56 # weaklink and channel through $schema->throw_exception
57 sub throw_exception { DBIx::Class::Exception->throw($_[1]) }
58
59 BEGIN {
60   # reinstall the belch()/puke() functions of SQL::Abstract with custom versions
61   # that use DBIx::Class::Carp/DBIx::Class::Exception instead of plain Carp
62   no warnings qw/redefine/;
63
64   *SQL::Abstract::belch = subname 'SQL::Abstract::belch' => sub (@) {
65     my($func) = (caller(1))[3];
66     carp "[$func] Warning: ", @_;
67   };
68
69   *SQL::Abstract::puke = subname 'SQL::Abstract::puke' => sub (@) {
70     my($func) = (caller(1))[3];
71     __PACKAGE__->throw_exception("[$func] Fatal: " . join ('',  @_));
72   };
73 }
74
75 # the "oh noes offset/top without limit" constant
76 # limited to 31 bits for sanity (and consistency,
77 # since it may be handed to the like of sprintf %u)
78 #
79 # Also *some* builds of SQLite fail the test
80 #   some_column BETWEEN ? AND ?: 1, 4294967295
81 # with the proper integer bind attrs
82 #
83 # Implemented as a method, since ::Storage::DBI also
84 # refers to it (i.e. for the case of software_limit or
85 # as the value to abuse with MSSQL ordered subqueries)
86 sub __max_int () { 0x7FFFFFFF };
87
88 # we ne longer need to check this - DBIC has ways of dealing with it
89 # specifically ::Storage::DBI::_resolve_bindattrs()
90 sub _assert_bindval_matches_bindtype () { 1 };
91
92 # poor man's de-qualifier
93 sub _quote {
94   $_[0]->next::method( ( $_[0]{_dequalify_idents} and ! ref $_[1] )
95     ? $_[1] =~ / ([^\.]+) $ /x
96     : $_[1]
97   );
98 }
99
100 sub _where_op_NEST {
101   carp_unique ("-nest in search conditions is deprecated, you most probably wanted:\n"
102       .q|{..., -and => [ \%cond0, \@cond1, \'cond2', \[ 'cond3', [ col => bind ] ], etc. ], ... }|
103   );
104
105   shift->next::method(@_);
106 }
107
108 # Handle limit-dialect selection
109 sub select {
110   my ($self, $table, $fields, $where, $rs_attrs, $limit, $offset) = @_;
111
112
113   $fields = $self->_recurse_fields($fields);
114
115   if (defined $offset) {
116     $self->throw_exception('A supplied offset must be a non-negative integer')
117       if ( $offset =~ /\D/ or $offset < 0 );
118   }
119   $offset ||= 0;
120
121   if (defined $limit) {
122     $self->throw_exception('A supplied limit must be a positive integer')
123       if ( $limit =~ /\D/ or $limit <= 0 );
124   }
125   elsif ($offset) {
126     $limit = $self->__max_int;
127   }
128
129
130   my ($sql, @bind);
131   if ($limit) {
132     # this is legacy code-flow from SQLA::Limit, it is not set in stone
133
134     ($sql, @bind) = $self->next::method ($table, $fields, $where);
135
136     my $limiter;
137
138     if( $limiter = $self->can ('emulate_limit') ) {
139       carp_unique(
140         'Support for the legacy emulate_limit() mechanism inherited from '
141       . 'SQL::Abstract::Limit has been deprecated, and will be removed when '
142       . 'DBIC transitions to Data::Query. If your code uses this type of '
143       . 'limit specification please file an RT and provide the source of '
144       . 'your emulate_limit() implementation, so an acceptable upgrade-path '
145       . 'can be devised'
146       );
147     }
148     else {
149       my $dialect = $self->limit_dialect
150         or $self->throw_exception( "Unable to generate SQL-limit - no limit dialect specified on $self" );
151
152       $limiter = $self->can ("_$dialect")
153         or $self->throw_exception(__PACKAGE__ . " does not implement the requested dialect '$dialect'");
154     }
155
156     $sql = $self->$limiter (
157       $sql,
158       { %{$rs_attrs||{}}, _selector_sql => $fields },
159       $limit,
160       $offset
161     );
162   }
163   else {
164     ($sql, @bind) = $self->next::method ($table, $fields, $where, $rs_attrs);
165   }
166
167   push @{$self->{where_bind}}, @bind;
168
169 # this *must* be called, otherwise extra binds will remain in the sql-maker
170   my @all_bind = $self->_assemble_binds;
171
172   $sql .= $self->_lock_select ($rs_attrs->{for})
173     if $rs_attrs->{for};
174
175   return wantarray ? ($sql, @all_bind) : $sql;
176 }
177
178 sub _assemble_binds {
179   my $self = shift;
180   return map { @{ (delete $self->{"${_}_bind"}) || [] } } (qw/pre_select select from where group having order limit/);
181 }
182
183 my $for_syntax = {
184   update => 'FOR UPDATE',
185   shared => 'FOR SHARE',
186 };
187 sub _lock_select {
188   my ($self, $type) = @_;
189
190   my $sql;
191   if (ref($type) eq 'SCALAR') {
192     $sql = "FOR $$type";
193   }
194   else {
195     $sql = $for_syntax->{$type} || $self->throw_exception( "Unknown SELECT .. FOR type '$type' requested" );
196   }
197
198   return " $sql";
199 }
200
201 # Handle default inserts
202 sub insert {
203 # optimized due to hotttnesss
204 #  my ($self, $table, $data, $options) = @_;
205
206   # SQLA will emit INSERT INTO $table ( ) VALUES ( )
207   # which is sadly understood only by MySQL. Change default behavior here,
208   # until SQLA2 comes with proper dialect support
209   if (! $_[2] or (ref $_[2] eq 'HASH' and !keys %{$_[2]} ) ) {
210     my @bind;
211     my $sql = sprintf(
212       'INSERT INTO %s DEFAULT VALUES', $_[0]->_quote($_[1])
213     );
214
215     if ( ($_[3]||{})->{returning} ) {
216       my $s;
217       ($s, @bind) = $_[0]->_insert_returning ($_[3]);
218       $sql .= $s;
219     }
220
221     return ($sql, @bind);
222   }
223
224   next::method(@_);
225 }
226
227 sub _insert_ARRAYREFREF {
228   my ($self, $data) = @_;
229
230   my ($cols, $subquery) = @$$data;
231   if (not $subquery) {
232     return next::method($self, $cols);
233   }
234
235   my @fields = sort @$cols;
236
237   my ($sql, @bind) = next::method($self, $subquery);
238
239   # as_query wraps in brackets. drop them
240   $_ = $self->_quote($_) for @fields;
241   $sql = "( ".join(", ", @fields).") " . substr($sql, 1, -1);
242
243   return ($sql, @bind);
244 }
245
246 sub _recurse_fields {
247   my ($self, $fields) = @_;
248   my $ref = ref $fields;
249   return $self->_quote($fields) unless $ref;
250   return $$fields if $ref eq 'SCALAR';
251
252   if ($ref eq 'ARRAY') {
253     return join(', ', map { $self->_recurse_fields($_) } @$fields);
254   }
255   elsif ($ref eq 'HASH') {
256     my %hash = %$fields;  # shallow copy
257
258     my $as = delete $hash{-as};   # if supplied
259
260     my ($func, $args, @toomany) = %hash;
261
262     # there should be only one pair
263     if (@toomany) {
264       $self->throw_exception( "Malformed select argument - too many keys in hash: " . join (',', keys %$fields ) );
265     }
266
267     if (lc ($func) eq 'distinct' && ref $args eq 'ARRAY' && @$args > 1) {
268       $self->throw_exception (
269         'The select => { distinct => ... } syntax is not supported for multiple columns.'
270        .' Instead please use { group_by => [ qw/' . (join ' ', @$args) . '/ ] }'
271        .' or { select => [ qw/' . (join ' ', @$args) . '/ ], distinct => 1 }'
272       );
273     }
274
275     my $select = sprintf ('%s( %s )%s',
276       $self->_sqlcase($func),
277       $self->_recurse_fields($args),
278       $as
279         ? sprintf (' %s %s', $self->_sqlcase('as'), $self->_quote ($as) )
280         : ''
281     );
282
283     return $select;
284   }
285   # Is the second check absolutely necessary?
286   elsif ( $ref eq 'REF' and ref($$fields) eq 'ARRAY' ) {
287     push @{$self->{select_bind}}, @{$$fields}[1..$#$$fields];
288     return $$fields->[0];
289   }
290   else {
291     $self->throw_exception( $ref . qq{ unexpected in _recurse_fields()} );
292   }
293 }
294
295
296 # this used to be a part of _order_by but is broken out for clarity.
297 # What we have been doing forever is hijacking the $order arg of
298 # SQLA::select to pass in arbitrary pieces of data (first the group_by,
299 # then pretty much the entire resultset attr-hash, as more and more
300 # things in the SQLA space need to have more info about the $rs they
301 # create SQL for. The alternative would be to keep expanding the
302 # signature of _select with more and more positional parameters, which
303 # is just gross. All hail SQLA2!
304 sub _parse_rs_attrs {
305   my ($self, $arg) = @_;
306
307   my $sql = '';
308
309   if ($arg->{group_by}) {
310     # horrible horrible, waiting for refactor
311     local $self->{select_bind};
312     if (my $g = $self->_recurse_fields($arg->{group_by}) ) {
313       $sql .= $self->_sqlcase(' group by ') . $g;
314       push @{$self->{group_bind} ||= []}, @{$self->{select_bind}||[]};
315     }
316   }
317
318   if (defined $arg->{having}) {
319     my ($frag, @bind) = $self->_recurse_where($arg->{having});
320     push(@{$self->{having_bind}}, @bind);
321     $sql .= $self->_sqlcase(' having ') . $frag;
322   }
323
324   if (defined $arg->{order_by}) {
325     $sql .= $self->_order_by ($arg->{order_by});
326   }
327
328   return $sql;
329 }
330
331 sub _order_by {
332   my ($self, $arg) = @_;
333
334   # check that we are not called in legacy mode (order_by as 4th argument)
335   if (ref $arg eq 'HASH' and not grep { $_ =~ /^-(?:desc|asc)/i } keys %$arg ) {
336     return $self->_parse_rs_attrs ($arg);
337   }
338   else {
339     my ($sql, @bind) = $self->next::method($arg);
340     push @{$self->{order_bind}}, @bind;
341     return $sql;
342   }
343 }
344
345 sub _split_order_chunk {
346   my ($self, $chunk) = @_;
347
348   # strip off sort modifiers, but always succeed, so $1 gets reset
349   $chunk =~ s/ (?: \s+ (ASC|DESC) )? \s* $//ix;
350
351   return (
352     $chunk,
353     ( $1 and uc($1) eq 'DESC' ) ? 1 : 0,
354   );
355 }
356
357 sub _table {
358 # optimized due to hotttnesss
359 #  my ($self, $from) = @_;
360   if (my $ref = ref $_[1] ) {
361     if ($ref eq 'ARRAY') {
362       return $_[0]->_recurse_from(@{$_[1]});
363     }
364     elsif ($ref eq 'HASH') {
365       return $_[0]->_recurse_from($_[1]);
366     }
367     elsif ($ref eq 'REF' && ref ${$_[1]} eq 'ARRAY') {
368       my ($sql, @bind) = @{ ${$_[1]} };
369       push @{$_[0]->{from_bind}}, @bind;
370       return $sql
371     }
372   }
373   return $_[0]->next::method ($_[1]);
374 }
375
376 sub _generate_join_clause {
377     my ($self, $join_type) = @_;
378
379     $join_type = $self->{_default_jointype}
380       if ! defined $join_type;
381
382     return sprintf ('%s JOIN ',
383       $join_type ?  $self->_sqlcase($join_type) : ''
384     );
385 }
386
387 sub _recurse_from {
388   my $self = shift;
389   return join (' ', $self->_gen_from_blocks(@_) );
390 }
391
392 sub _gen_from_blocks {
393   my ($self, $from, @joins) = @_;
394
395   my @fchunks = $self->_from_chunk_to_sql($from);
396
397   for (@joins) {
398     my ($to, $on) = @$_;
399
400     # check whether a join type exists
401     my $to_jt = ref($to) eq 'ARRAY' ? $to->[0] : $to;
402     my $join_type;
403     if (ref($to_jt) eq 'HASH' and defined($to_jt->{-join_type})) {
404       $join_type = $to_jt->{-join_type};
405       $join_type =~ s/^\s+ | \s+$//xg;
406     }
407
408     my @j = $self->_generate_join_clause( $join_type );
409
410     if (ref $to eq 'ARRAY') {
411       push(@j, '(', $self->_recurse_from(@$to), ')');
412     }
413     else {
414       push(@j, $self->_from_chunk_to_sql($to));
415     }
416
417     my ($sql, @bind) = $self->_join_condition($on);
418     push(@j, ' ON ', $sql);
419     push @{$self->{from_bind}}, @bind;
420
421     push @fchunks, join '', @j;
422   }
423
424   return @fchunks;
425 }
426
427 sub _from_chunk_to_sql {
428   my ($self, $fromspec) = @_;
429
430   return join (' ', do {
431     if (! ref $fromspec) {
432       $self->_quote($fromspec);
433     }
434     elsif (ref $fromspec eq 'SCALAR') {
435       $$fromspec;
436     }
437     elsif (ref $fromspec eq 'REF' and ref $$fromspec eq 'ARRAY') {
438       push @{$self->{from_bind}}, @{$$fromspec}[1..$#$$fromspec];
439       $$fromspec->[0];
440     }
441     elsif (ref $fromspec eq 'HASH') {
442       my ($as, $table, $toomuch) = ( map
443         { $_ => $fromspec->{$_} }
444         ( grep { $_ !~ /^\-/ } keys %$fromspec )
445       );
446
447       $self->throw_exception( "Only one table/as pair expected in from-spec but an exra '$toomuch' key present" )
448         if defined $toomuch;
449
450       ($self->_from_chunk_to_sql($table), $self->_quote($as) );
451     }
452     else {
453       $self->throw_exception('Unsupported from refkind: ' . ref $fromspec );
454     }
455   });
456 }
457
458 sub _join_condition {
459   my ($self, $cond) = @_;
460
461   # Backcompat for the old days when a plain hashref
462   # { 't1.col1' => 't2.col2' } meant ON t1.col1 = t2.col2
463   # Once things settle we should start warning here so that
464   # folks unroll their hacks
465   if (
466     ref $cond eq 'HASH'
467       and
468     keys %$cond == 1
469       and
470     (keys %$cond)[0] =~ /\./
471       and
472     ! ref ( (values %$cond)[0] )
473   ) {
474     $cond = { keys %$cond => { -ident => values %$cond } }
475   }
476   elsif ( ref $cond eq 'ARRAY' ) {
477     # do our own ORing so that the hashref-shim above is invoked
478     my @parts;
479     my @binds;
480     foreach my $c (@$cond) {
481       my ($sql, @bind) = $self->_join_condition($c);
482       push @binds, @bind;
483       push @parts, $sql;
484     }
485     return join(' OR ', @parts), @binds;
486   }
487
488   return $self->_recurse_where($cond);
489 }
490
491 # This is hideously ugly, but SQLA does not understand multicol IN expressions
492 # FIXME TEMPORARY - DQ should have native syntax for this
493 # moved here to raise API questions
494 #
495 # !!! EXPERIMENTAL API !!! WILL CHANGE !!!
496 sub _where_op_multicolumn_in {
497   my ($self, $lhs, $rhs) = @_;
498
499   if (! ref $lhs or ref $lhs eq 'ARRAY') {
500     my (@sql, @bind);
501     for (ref $lhs ? @$lhs : $lhs) {
502       if (! ref $_) {
503         push @sql, $self->_quote($_);
504       }
505       elsif (ref $_ eq 'SCALAR') {
506         push @sql, $$_;
507       }
508       elsif (ref $_ eq 'REF' and ref $$_ eq 'ARRAY') {
509         my ($s, @b) = @$$_;
510         push @sql, $s;
511         push @bind, @b;
512       }
513       else {
514         $self->throw_exception("ARRAY of @{[ ref $_ ]}es unsupported for multicolumn IN lhs...");
515       }
516     }
517     $lhs = \[ join(', ', @sql), @bind];
518   }
519   elsif (ref $lhs eq 'SCALAR') {
520     $lhs = \[ $$lhs ];
521   }
522   elsif (ref $lhs eq 'REF' and ref $$lhs eq 'ARRAY' ) {
523     # noop
524   }
525   else {
526     $self->throw_exception( ref($lhs) . "es unsupported for multicolumn IN lhs...");
527   }
528
529   # is this proper...?
530   $rhs = \[ $self->_recurse_where($rhs) ];
531
532   for ($lhs, $rhs) {
533     $$_->[0] = "( $$_->[0] )"
534       unless $$_->[0] =~ /^ \s* \( .* \) \s* ^/xs;
535   }
536
537   \[ join( ' IN ', shift @$$lhs, shift @$$rhs ), @$$lhs, @$$rhs ];
538 }
539
540 1;
541
542 =head1 AUTHORS
543
544 See L<DBIx::Class/CONTRIBUTORS>.
545
546 =head1 LICENSE
547
548 You may distribute this code under the same terms as Perl itself.
549
550 =cut