::Replicated -- check for master rebless and reapply role if necessary
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / SQLAHacks.pm
1 package # Hide from PAUSE
2   DBIx::Class::SQLAHacks;
3
4 use base qw/SQL::Abstract::Limit/;
5 use strict;
6 use warnings;
7 use Carp::Clan qw/^DBIx::Class/;
8
9 sub new {
10   my $self = shift->SUPER::new(@_);
11
12   # This prevents the caching of $dbh in S::A::L, I believe
13   # If limit_dialect is a ref (like a $dbh), go ahead and replace
14   #   it with what it resolves to:
15   $self->{limit_dialect} = $self->_find_syntax($self->{limit_dialect})
16     if ref $self->{limit_dialect};
17
18   $self;
19 }
20
21
22 # Some databases (sqlite) do not handle multiple parenthesis
23 # around in/between arguments. A tentative x IN ( ( 1, 2 ,3) )
24 # is interpreted as x IN 1 or something similar.
25 #
26 # Since we currently do not have access to the SQLA AST, resort
27 # to barbaric mutilation of any SQL supplied in literal form
28
29 sub _strip_outer_paren {
30   my ($self, $arg) = @_;
31
32   return $self->_SWITCH_refkind ($arg, {
33     ARRAYREFREF => sub {
34       $$arg->[0] = __strip_outer_paren ($$arg->[0]);
35       return $arg;
36     },
37     SCALARREF => sub {
38       return \__strip_outer_paren( $$arg );
39     },
40     FALLBACK => sub {
41       return $arg
42     },
43   });
44 }
45
46 sub __strip_outer_paren {
47   my $sql = shift;
48
49   if ($sql and not ref $sql) {
50     while ($sql =~ /^ \s* \( (.*) \) \s* $/x ) {
51       $sql = $1;
52     }
53   }
54
55   return $sql;
56 }
57
58 sub _where_field_IN {
59   my ($self, $lhs, $op, $rhs) = @_;
60   $rhs = $self->_strip_outer_paren ($rhs);
61   return $self->SUPER::_where_field_IN ($lhs, $op, $rhs);
62 }
63
64 sub _where_field_BETWEEN {
65   my ($self, $lhs, $op, $rhs) = @_;
66   $rhs = $self->_strip_outer_paren ($rhs);
67   return $self->SUPER::_where_field_BETWEEN ($lhs, $op, $rhs);
68 }
69
70
71
72 # DB2 is the only remaining DB using this. Even though we are not sure if
73 # RowNumberOver is still needed here (should be part of SQLA) leave the 
74 # code in place
75 sub _RowNumberOver {
76   my ($self, $sql, $order, $rows, $offset ) = @_;
77
78   $offset += 1;
79   my $last = $rows + $offset - 1;
80   my ( $order_by ) = $self->_order_by( $order );
81
82   $sql = <<"SQL";
83 SELECT * FROM
84 (
85    SELECT Q1.*, ROW_NUMBER() OVER( ) AS ROW_NUM FROM (
86       $sql
87       $order_by
88    ) Q1
89 ) Q2
90 WHERE ROW_NUM BETWEEN $offset AND $last
91
92 SQL
93
94   return $sql;
95 }
96
97
98 # While we're at it, this should make LIMIT queries more efficient,
99 #  without digging into things too deeply
100 use Scalar::Util 'blessed';
101 sub _find_syntax {
102   my ($self, $syntax) = @_;
103   
104   # DB2 is the only remaining DB using this. Even though we are not sure if
105   # RowNumberOver is still needed here (should be part of SQLA) leave the 
106   # code in place
107   my $dbhname = blessed($syntax) ? $syntax->{Driver}{Name} : $syntax;
108   if(ref($self) && $dbhname && $dbhname eq 'DB2') {
109     return 'RowNumberOver';
110   }
111   
112   $self->{_cached_syntax} ||= $self->SUPER::_find_syntax($syntax);
113 }
114
115 sub select {
116   my ($self, $table, $fields, $where, $order, @rest) = @_;
117   local $self->{having_bind} = [];
118   local $self->{from_bind} = [];
119
120   if (ref $table eq 'SCALAR') {
121     $table = $$table;
122   }
123   elsif (not ref $table) {
124     $table = $self->_quote($table);
125   }
126   local $self->{rownum_hack_count} = 1
127     if (defined $rest[0] && $self->{limit_dialect} eq 'RowNum');
128   @rest = (-1) unless defined $rest[0];
129   croak "LIMIT 0 Does Not Compute" if $rest[0] == 0;
130     # and anyway, SQL::Abstract::Limit will cause a barf if we don't first
131   my ($sql, @where_bind) = $self->SUPER::select(
132     $table, $self->_recurse_fields($fields), $where, $order, @rest
133   );
134   $sql .= 
135     $self->{for} ?
136     (
137       $self->{for} eq 'update' ? ' FOR UPDATE' :
138       $self->{for} eq 'shared' ? ' FOR SHARE'  :
139       ''
140     ) :
141     ''
142   ;
143   return wantarray ? ($sql, @{$self->{from_bind}}, @where_bind, @{$self->{having_bind}}) : $sql;
144 }
145
146 sub insert {
147   my $self = shift;
148   my $table = shift;
149   $table = $self->_quote($table) unless ref($table);
150   $self->SUPER::insert($table, @_);
151 }
152
153 sub update {
154   my $self = shift;
155   my $table = shift;
156   $table = $self->_quote($table) unless ref($table);
157   $self->SUPER::update($table, @_);
158 }
159
160 sub delete {
161   my $self = shift;
162   my $table = shift;
163   $table = $self->_quote($table) unless ref($table);
164   $self->SUPER::delete($table, @_);
165 }
166
167 sub _emulate_limit {
168   my $self = shift;
169   if ($_[3] == -1) {
170     return $_[1].$self->_order_by($_[2]);
171   } else {
172     return $self->SUPER::_emulate_limit(@_);
173   }
174 }
175
176 sub _recurse_fields {
177   my ($self, $fields, $params) = @_;
178   my $ref = ref $fields;
179   return $self->_quote($fields) unless $ref;
180   return $$fields if $ref eq 'SCALAR';
181
182   if ($ref eq 'ARRAY') {
183     return join(', ', map {
184       $self->_recurse_fields($_)
185         .(exists $self->{rownum_hack_count} && !($params && $params->{no_rownum_hack})
186           ? ' AS col'.$self->{rownum_hack_count}++
187           : '')
188       } @$fields);
189   } elsif ($ref eq 'HASH') {
190     foreach my $func (keys %$fields) {
191       if ($func eq 'distinct') {
192         my $_fields = $fields->{$func};
193         if (ref $_fields eq 'ARRAY' && @{$_fields} > 1) {
194           croak "Unsupported syntax, please use " . 
195               "{ group_by => [ qw/" . (join ' ', @$_fields) . "/ ] }" .
196               " or " .
197               "{ select => [ qw/" . (join ' ', @$_fields) . "/ ], distinct => 1 }";
198         }
199         else {
200           $_fields = @{$_fields}[0] if ref $_fields eq 'ARRAY';
201           carp "This syntax will be deprecated in 09, please use " . 
202                "{ group_by => '${_fields}' }" . 
203                " or " .
204                "{ select => '${_fields}', distinct => 1 }";
205         }
206       }
207       
208       return $self->_sqlcase($func)
209         .'( '.$self->_recurse_fields($fields->{$func}).' )';
210     }
211   }
212   # Is the second check absolutely necessary?
213   elsif ( $ref eq 'REF' and ref($$fields) eq 'ARRAY' ) {
214     return $self->_fold_sqlbind( $fields );
215   }
216   else {
217     croak($ref . qq{ unexpected in _recurse_fields()})
218   }
219 }
220
221 sub _order_by {
222   my $self = shift;
223   my $ret = '';
224   my @extra;
225   if (ref $_[0] eq 'HASH') {
226     if (defined $_[0]->{group_by}) {
227       $ret = $self->_sqlcase(' group by ')
228         .$self->_recurse_fields($_[0]->{group_by}, { no_rownum_hack => 1 });
229     }
230     if (defined $_[0]->{having}) {
231       my $frag;
232       ($frag, @extra) = $self->_recurse_where($_[0]->{having});
233       push(@{$self->{having_bind}}, @extra);
234       $ret .= $self->_sqlcase(' having ').$frag;
235     }
236     if (defined $_[0]->{order_by}) {
237       $ret .= $self->_order_by($_[0]->{order_by});
238     }
239     if (grep { $_ =~ /^-(desc|asc)/i } keys %{$_[0]}) {
240       return $self->SUPER::_order_by($_[0]);
241     }
242   } elsif (ref $_[0] eq 'SCALAR') {
243     $ret = $self->_sqlcase(' order by ').${ $_[0] };
244   } elsif (ref $_[0] eq 'ARRAY' && @{$_[0]}) {
245     my @order = @{+shift};
246     $ret = $self->_sqlcase(' order by ')
247           .join(', ', map {
248                         my $r = $self->_order_by($_, @_);
249                         $r =~ s/^ ?ORDER BY //i;
250                         $r;
251                       } @order);
252   } else {
253     $ret = $self->SUPER::_order_by(@_);
254   }
255   return $ret;
256 }
257
258 sub _order_directions {
259   my ($self, $order) = @_;
260   $order = $order->{order_by} if ref $order eq 'HASH';
261   if (ref $order eq 'HASH') {
262     $order = [$self->_order_directions_hash($order)];
263   } elsif (ref $order eq 'ARRAY') {
264     $order = [map {
265       if (ref $_ eq 'HASH') {
266         $self->_order_directions_hash($_);
267       } else {
268         $_;
269       }
270     } @{ $order }];
271   }
272   return $self->SUPER::_order_directions($order);
273 }
274
275 sub _order_directions_hash {
276   my ($self, $order) = @_;
277   my @new_order;
278   foreach my $key (keys %{ $order }) {
279     if ($key =~ /^-(desc|asc)/i ) {
280       my $direction = $1;
281       my $type = ref $order->{ $key };
282       if ($type eq 'ARRAY') {
283         push @new_order, map( "$_ $direction", @{ $order->{ $key } } );
284       } elsif (!$type) {
285         push @new_order, "$order->{$key} $direction";
286       } else {
287         croak "hash order_by can only contain Scalar or Array, not $type";
288       }
289     } else {
290       croak "$key is not a valid direction, use -asc or -desc";
291     }
292   }
293   return @new_order;
294 }
295
296 sub _table {
297   my ($self, $from) = @_;
298   if (ref $from eq 'ARRAY') {
299     return $self->_recurse_from(@$from);
300   } elsif (ref $from eq 'HASH') {
301     return $self->_make_as($from);
302   } else {
303     return $from; # would love to quote here but _table ends up getting called
304                   # twice during an ->select without a limit clause due to
305                   # the way S::A::Limit->select works. should maybe consider
306                   # bypassing this and doing S::A::select($self, ...) in
307                   # our select method above. meantime, quoting shims have
308                   # been added to select/insert/update/delete here
309   }
310 }
311
312 sub _recurse_from {
313   my ($self, $from, @join) = @_;
314   my @sqlf;
315   push(@sqlf, $self->_make_as($from));
316   foreach my $j (@join) {
317     my ($to, $on) = @$j;
318
319     # check whether a join type exists
320     my $join_clause = '';
321     my $to_jt = ref($to) eq 'ARRAY' ? $to->[0] : $to;
322     if (ref($to_jt) eq 'HASH' and exists($to_jt->{-join_type})) {
323       $join_clause = ' '.uc($to_jt->{-join_type}).' JOIN ';
324     } else {
325       $join_clause = ' JOIN ';
326     }
327     push(@sqlf, $join_clause);
328
329     if (ref $to eq 'ARRAY') {
330       push(@sqlf, '(', $self->_recurse_from(@$to), ')');
331     } else {
332       push(@sqlf, $self->_make_as($to));
333     }
334     push(@sqlf, ' ON ', $self->_join_condition($on));
335   }
336   return join('', @sqlf);
337 }
338
339 sub _fold_sqlbind {
340   my ($self, $sqlbind) = @_;
341   my $sql = shift @$$sqlbind;
342   push @{$self->{from_bind}}, @$$sqlbind;
343   return $sql;
344 }
345
346 sub _make_as {
347   my ($self, $from) = @_;
348   return join(' ', map { (ref $_ eq 'SCALAR' ? $$_
349                         : ref $_ eq 'REF'    ? $self->_fold_sqlbind($_)
350                         : $self->_quote($_))
351                        } reverse each %{$self->_skip_options($from)});
352 }
353
354 sub _skip_options {
355   my ($self, $hash) = @_;
356   my $clean_hash = {};
357   $clean_hash->{$_} = $hash->{$_}
358     for grep {!/^-/} keys %$hash;
359   return $clean_hash;
360 }
361
362 sub _join_condition {
363   my ($self, $cond) = @_;
364   if (ref $cond eq 'HASH') {
365     my %j;
366     for (keys %$cond) {
367       my $v = $cond->{$_};
368       if (ref $v) {
369         croak (ref($v) . qq{ reference arguments are not supported in JOINS - try using \"..." instead'})
370             if ref($v) ne 'SCALAR';
371         $j{$_} = $v;
372       }
373       else {
374         my $x = '= '.$self->_quote($v); $j{$_} = \$x;
375       }
376     };
377     return scalar($self->_recurse_where(\%j));
378   } elsif (ref $cond eq 'ARRAY') {
379     return join(' OR ', map { $self->_join_condition($_) } @$cond);
380   } else {
381     die "Can't handle this yet!";
382   }
383 }
384
385 sub _quote {
386   my ($self, $label) = @_;
387   return '' unless defined $label;
388   return "*" if $label eq '*';
389   return $label unless $self->{quote_char};
390   if(ref $self->{quote_char} eq "ARRAY"){
391     return $self->{quote_char}->[0] . $label . $self->{quote_char}->[1]
392       if !defined $self->{name_sep};
393     my $sep = $self->{name_sep};
394     return join($self->{name_sep},
395         map { $self->{quote_char}->[0] . $_ . $self->{quote_char}->[1]  }
396        split(/\Q$sep\E/,$label));
397   }
398   return $self->SUPER::_quote($label);
399 }
400
401 sub limit_dialect {
402     my $self = shift;
403     $self->{limit_dialect} = shift if @_;
404     return $self->{limit_dialect};
405 }
406
407 sub quote_char {
408     my $self = shift;
409     $self->{quote_char} = shift if @_;
410     return $self->{quote_char};
411 }
412
413 sub name_sep {
414     my $self = shift;
415     $self->{name_sep} = shift if @_;
416     return $self->{name_sep};
417 }
418
419 1;
420
421 __END__
422
423 =pod
424
425 =head1 NAME
426
427 DBIx::Class::SQLAHacks - This module is a subclass of SQL::Abstract::Limit
428 and includes a number of DBIC-specific workarounds, not yet suitable for
429 inclusion into SQLA proper.
430
431 =head1 METHODS
432
433 =head2 new
434
435 Tries to determine limit dialect.
436
437 =head2 select
438
439 Quotes table names, handles "limit" dialects (e.g. where rownum between x and
440 y), supports SELECT ... FOR UPDATE and SELECT ... FOR SHARE.
441
442 =head2 insert update delete
443
444 Just quotes table names.
445
446 =head2 limit_dialect
447
448 Specifies the dialect of used for implementing an SQL "limit" clause for
449 restricting the number of query results returned.  Valid values are: RowNum.
450
451 See L<DBIx::Class::Storage::DBI/connect_info> for details.
452
453 =head2 name_sep
454
455 Character separating quoted table names.
456
457 See L<DBIx::Class::Storage::DBI/connect_info> for details.
458
459 =head2 quote_char
460
461 Set to an array-ref to specify separate left and right quotes for table names.
462
463 See L<DBIx::Class::Storage::DBI/connect_info> for details.
464
465 =cut
466