fix for sql functions in group_by
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / ResultSet.pm
index a87a3cd..b06b497 100644 (file)
@@ -1155,10 +1155,11 @@ sub count {
   return $self->search(@_)->count if @_ and defined $_[0];
   return scalar @{ $self->get_cache } if $self->get_cache;
 
-  my @subq_attrs = qw/prefetch collapse distinct group_by having having_bind/;
+  my @subq_attrs = qw/prefetch collapse distinct group_by having/;
+  my $attrs = $self->_resolved_attrs;
 
   # if we are not paged - we are simply asking for a limit
-  if (not $self->{attrs}{page} and not $self->{attrs}{software_limit}) {
+  if (not $attrs->{page} and not $attrs->{software_limit}) {
     push @subq_attrs, qw/rows offset/;
   }
 
@@ -1176,7 +1177,7 @@ sub _count_subq {
   my $sub_attrs = { %$attrs };
 
   # these can not go in the subquery either
-  delete $sub_attrs->{$_} for qw/prefetch select +select as +as columns +columns/;
+  delete $sub_attrs->{$_} for qw/prefetch collapse select +select as +as columns +columns/;
 
   # force a group_by and the same set of columns (most databases require this)
   $sub_attrs->{columns} = $sub_attrs->{group_by} ||= [ map { "$attrs->{alias}.$_" } ($self->result_source->primary_columns) ];
@@ -1186,7 +1187,7 @@ sub _count_subq {
   }];
 
   # the subquery replaces this
-  delete $attrs->{$_} for qw/where bind prefetch collapse group_by having/;
+  delete $attrs->{$_} for qw/where bind prefetch collapse distinct group_by having having_bind/;
 
   return $self->__count ($attrs);
 }
@@ -1199,9 +1200,10 @@ sub _count_simple {
 
   # need to take offset from resolved attrs
 
-  $count -= $self->{_attrs}{offset} if $self->{_attrs}{offset};
-  $count = $self->{attrs}{rows} if
-    $self->{attrs}{rows} and $self->{attrs}{rows} < $count;
+  my $attrs = $self->_resolved_attrs;
+
+  $count -= $attrs->{offset} if $attrs->{offset};
+  $count = $attrs->{rows} if $attrs->{rows} and $attrs->{rows} < $count;
   $count = 0 if ($count < 0);
   return $count;
 }
@@ -1209,7 +1211,7 @@ sub _count_simple {
 sub __count {
   my ($self, $attrs) = @_;
 
-  $attrs ||= { %{$self->{attrs}} };
+  $attrs ||= { %{$self->_resolved_attrs} };
 
   # take off any column specs, any pagers, record_filter is cdbi, and no point of ordering a count
   delete $attrs->{$_} for (qw/columns +columns select +select as +as rows offset page pager order_by record_filter/); 
@@ -1352,7 +1354,7 @@ sub _rs_update_delete {
     # make a new $rs selecting only the PKs (that's all we really need)
     my $attrs = $self->_resolved_attrs;
 
-    delete $attrs->{$_} for qw/prefetch select +select as +as columns +columns/;
+    delete $attrs->{$_} for qw/prefetch collapse select +select as +as columns +columns/;
     $attrs->{columns} = [ map { "$attrs->{alias}.$_" } ($self->result_source->primary_columns) ];
 
     if ($needs_group_by_subq) {
@@ -2608,7 +2610,7 @@ sub _resolved_attrs {
 
   }
 
-  $attrs->{group_by} ||= $attrs->{select}
+  $attrs->{group_by} ||= [ grep { !ref($_) || (ref($_) ne 'HASH') } @{$attrs->{select}} ]
     if delete $attrs->{distinct};
   if ( $attrs->{order_by} ) {
     $attrs->{order_by} = (