From: Peter Rabbitson Date: Thu, 4 Jun 2009 11:38:21 +0000 (-0700) Subject: Merge 'trunk' into 'sybase' X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ec10b4644f46ea1b51fe4be0addff6831cc3a267;p=dbsrgits%2FDBIx-Class-Historic.git Merge 'trunk' into 'sybase' r5334@hlagh (orig r6501): ribasushi | 2009-06-04 06:38:21 -0700 Really delegate counting to the storage class - now we have either a grouped count or a regular one --- ec10b4644f46ea1b51fe4be0addff6831cc3a267 diff --cc lib/DBIx/Class/Storage/DBI.pm index f3ea456,c20a931..0141e1c --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@@ -1245,27 -1229,26 +1245,35 @@@ sub _select_args return @args; } -sub count { +sub _trim_attributes_for_count { my ($self, $source, $attrs) = @_; - - my $tmp_attrs = { %$attrs }; + my %attrs = %$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/); - delete $tmp_attrs->{$_} for (qw/columns +columns select +select as +as rows offset page pager order_by record_filter/); ++ delete @attrs{qw/ ++ columns +columns select +select as +as rows offset page pager order_by ++ record_filter/)}; + + return \%attrs; +} + +sub count { + my ($self, $source, $attrs) = @_; + + my $new_attrs = $self->_trim_attributes_for_count($source, $attrs); - $tmp_attrs->{select} = { count => '*' }; - $tmp_attrs->{as} = [qw/count/]; + $new_attrs->{select} = { count => '*' }; + $new_attrs->{as} = [qw/count/]; - my $tmp_rs = $source->resultset_class->new($source, $tmp_attrs); + my $tmp_rs = $source->resultset_class->new($source, $new_attrs); my ($count) = $tmp_rs->cursor->next; + # if the offset/rows attributes are still present, we did not use + # a subquery, so we need to make the calculations in software + $count -= $attrs->{offset} if $attrs->{offset}; + $count = $attrs->{rows} if $attrs->{rows} and $attrs->{rows} < $count; + $count = 0 if ($count < 0); + return $count; }