Fixed group_by bind position problem, reported and patch by Alexande Keusch. Patch...
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / SQLAHacks.pm
index f3fa030..674b90c 100644 (file)
@@ -132,11 +132,7 @@ sub _subqueried_limit_attrs {
 
   my %extra_order_sel;
   if ($scan_order) {
-    for my $chunk ($self->_order_by_chunks (
-      ref $rs_attrs->{order_by} eq 'ARRAY'
-        ? @{$rs_attrs->{order_by}}
-        : $rs_attrs->{order_by}
-    )) {
+    for my $chunk ($self->_order_by_chunks ($rs_attrs->{order_by})) {
       # order with bind
       $chunk = $chunk->[0] if (ref $chunk) eq 'ARRAY';
       $chunk =~ s/\s+ (?: ASC|DESC ) \s* $//ix;
@@ -144,7 +140,7 @@ sub _subqueried_limit_attrs {
       next if $in_sel_index->{$chunk};
 
       $extra_order_sel{$chunk} ||= $self->_quote (
-        '__ORDER_BY_' . scalar keys %extra_order_sel
+        'ORDER__BY__' . scalar keys %extra_order_sel
       );
     }
   }
@@ -178,7 +174,10 @@ sub _RowNumberOver {
   # this is the order supplement magic
   my $mid_sel = $out_sel;
   if ($extra_order_sel) {
-    for my $extra_col (keys %$extra_order_sel) {
+    for my $extra_col (sort
+      { $extra_order_sel->{$a} cmp $extra_order_sel->{$b} }
+      keys %$extra_order_sel
+    ) {
       $in_sel .= sprintf (', %s AS %s',
         $extra_col,
         $extra_order_sel->{$extra_col},
@@ -320,19 +319,23 @@ sub _Top {
     my @out_chunks;
     for my $ch ($self->_order_by_chunks ($inner_order)) {
       $ch = $ch->[0] if ref $ch eq 'ARRAY';
+
       $ch =~ s/\s+ ( ASC|DESC ) \s* $//ix;
       my $dir = uc ($1||'ASC');
 
       push @out_chunks, \join (' ', $ch, $dir eq 'ASC' ? 'DESC' : 'ASC' );
     }
 
-    $order_by_reversed = $self->_order_by (@out_chunks);
+    $order_by_reversed = $self->_order_by (\@out_chunks);
   }
 
   # this is the order supplement magic
   my $mid_sel = $out_sel;
   if ($extra_order_sel) {
-    for my $extra_col (keys %$extra_order_sel) {
+    for my $extra_col (sort
+      { $extra_order_sel->{$a} cmp $extra_order_sel->{$b} }
+      keys %$extra_order_sel
+    ) {
       $in_sel .= sprintf (', %s AS %s',
         $extra_col,
         $extra_order_sel->{$extra_col},
@@ -374,18 +377,11 @@ sub _Top {
 
   $sql = sprintf ('SELECT TOP %d %s FROM ( %s ) %s %s',
     $rows,
-    $mid_sel,
-    $sql,
-    $quoted_rs_alias,
-    $order_by_requested,
-  ) if $order_by_requested;
-
-  $sql = sprintf ('SELECT TOP %d %s FROM ( %s ) %s',
-    $rows,
     $out_sel,
     $sql,
     $quoted_rs_alias,
-  ) if ($mid_sel ne $out_sel);
+    $order_by_requested,
+  ) if ( ($offset && $order_by_requested) || ($mid_sel ne $out_sel) );
 
   return $sql;
 }
@@ -403,22 +399,21 @@ sub _find_syntax {
 sub select {
   my ($self, $table, $fields, $where, $rs_attrs, @rest) = @_;
 
-  $self->{"${_}_bind"} = [] for (qw/having from order/);
+  $self->{"${_}_bind"} = [] for (qw/having from order where/);
 
   if (not ref($table) or ref($table) eq 'SCALAR') {
     $table = $self->_quote($table);
   }
 
-  local $self->{rownum_hack_count} = 1
-    if (defined $rest[0] && $self->{limit_dialect} eq 'RowNum');
   @rest = (-1) unless defined $rest[0];
   croak "LIMIT 0 Does Not Compute" if $rest[0] == 0;
     # and anyway, SQL::Abstract::Limit will cause a barf if we don't first
 
-  my ($sql, @where_bind) = $self->SUPER::select(
+  my $sql = '';
+  ($sql, @{$self->{where_bind}}) = $self->SUPER::select(
     $table, $self->_recurse_fields($fields), $where, $rs_attrs, @rest
   );
-  return wantarray ? ($sql, @{$self->{from_bind}}, @where_bind, @{$self->{having_bind}}, @{$self->{order_bind}} ) : $sql;
+  return wantarray ? ($sql, @{$self->{from_bind}}, @{$self->{where_bind}}, @{$self->{having_bind}}, @{$self->{order_bind}} ) : $sql;
 }
 
 # Quotes table names, and handles default inserts
@@ -536,7 +531,7 @@ sub _parse_rs_attrs {
 
   my $sql = '';
 
-  if (my $g = $self->_recurse_fields($arg->{group_by}, { no_rownum_hack => 1 }) ) {
+  if (my $g = $self->_recurse_fields($arg->{group_by}) ) {
     $sql .= $self->_sqlcase(' group by ') . $g;
   }