Fix RowCountOrGenericSubQ dropping group/having/order on the floor (RT#73244)
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / SQLMaker / LimitDialects.pm
index e91a0c5..3df6daa 100644 (file)
@@ -121,10 +121,10 @@ sub _RowNumberOver {
   }
 
   # and this is order re-alias magic
-  for ($sq_attrs->{order_supplement}, $sq_attrs->{outer_renames}) {
-    for my $col (keys %$_) {
+  for my $map ($sq_attrs->{order_supplement}, $sq_attrs->{outer_renames}) {
+    for my $col (sort { (length $b) <=> (length $a) } keys %{$map||{}} ) {
       my $re_col = quotemeta ($col);
-      $rno_ord =~ s/$re_col/$_->{$col}/;
+      $rno_ord =~ s/$re_col/$map->{$col}/;
     }
   }
 
@@ -398,7 +398,7 @@ sub _prep_for_skimming_limit {
 
     # and this is order re-alias magic
     for my $map ($sq_attrs->{order_supplement}, $sq_attrs->{outer_renames}) {
-      for my $col (sort { $map->{$a} cmp $map->{$b} } keys %{$map||{}}) {
+      for my $col (sort { (length $b) <=> (length $a) } keys %{$map||{}}) {
         my $re_col = quotemeta ($col);
         $_ =~ s/$re_col/$map->{$col}/
           for ($sq_attrs->{order_by_middle}, $sq_attrs->{order_by_requested});
@@ -536,9 +536,9 @@ sub _RowCountOrGenericSubQ {
 
   return $self->_GenericSubQ(@_) if $offset;
 
-  return sprintf <<"EOF", $rows, $sql;
+  return sprintf <<"EOF", $rows, $sql, $self->_parse_rs_attrs( $rs_attrs );
 SET ROWCOUNT %d
-%s
+%s %s
 SET ROWCOUNT 0
 EOF
 }
@@ -735,11 +735,14 @@ sub _subqueried_limit_attrs {
   # unless we are dealing with the current source alias
   # (which will transcend the subqueries as it is necessary
   # for possible further chaining)
+  # same for anything we do not recognize
   my ($sel, $renamed);
   for my $node (@sel) {
     push @{$sel->{original}}, $node->{sql};
 
     if (
+      ! $in_sel_index->{$node->{sql}}
+        or
       $node->{as} =~ / (?<! ^ $re_alias ) \. /x
         or
       $node->{unquoted_sql} =~ / (?<! ^ $re_alias ) $re_sep /x