From: Peter Rabbitson Date: Thu, 4 Jun 2009 09:52:18 +0000 (+0000) Subject: Delegate actual counting to the storage class X-Git-Tag: v0.08106~39 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=1685b25e531b0446832933e0f498ddff8855b285;p=dbsrgits%2FDBIx-Class.git Delegate actual counting to the storage class --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index b53e85c..bdd124a 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -1176,6 +1176,7 @@ sub _count_subq { my ($self, $add_group_by) = @_; my $attrs = $self->_resolved_attrs_copy; + my $rsrc = $self->result_source; # copy for the subquery, we need to do some adjustments to it too my $sub_attrs = { %$attrs }; @@ -1193,27 +1194,29 @@ sub _count_subq { delete $sub_attrs->{group_by}; } - $sub_attrs->{columns} = $sub_attrs->{group_by} ||= [ map { "$attrs->{alias}.$_" } ($self->result_source->primary_columns) ]; + $sub_attrs->{columns} = $sub_attrs->{group_by} ||= [ map { "$attrs->{alias}.$_" } ($rsrc->primary_columns) ]; } $attrs->{from} = [{ - count_subq => (ref $self)->new ($self->result_source, $sub_attrs )->as_query + count_subq => (ref $self)->new ($rsrc, $sub_attrs )->as_query }]; # the subquery replaces this delete $attrs->{$_} for qw/where bind prefetch collapse distinct group_by having having_bind/; - return $self->__count ($attrs); + return $rsrc->storage->count ($rsrc, $attrs); } sub _count_simple { my $self = shift; - my $count = $self->__count; + my $rsrc = $self->result_source; + + # the attrs supplied here are getting modified, do not reuse below + my $count = $rsrc->storage->count ($rsrc, $self->_resolved_attrs_copy); return 0 unless $count; # need to take offset from resolved attrs - my $attrs = $self->_resolved_attrs; $count -= $attrs->{offset} if $attrs->{offset}; @@ -1222,23 +1225,6 @@ sub _count_simple { return $count; } -sub __count { - my ($self, $attrs) = @_; - - $attrs ||= $self->_resolved_attrs_copy; - - # 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/); - - $attrs->{select} = { count => '*' }; - $attrs->{as} = [qw/count/]; - - my $tmp_rs = (ref $self)->new($self->result_source, $attrs); - my ($count) = $tmp_rs->cursor->next; - - return $count; -} - sub _bool { return 1; } diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index 14764a2..f7ba454 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -1229,6 +1229,21 @@ sub _select_args { return @args; } +sub count { + my ($self, $source, $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/); + + $attrs->{select} = { count => '*' }; + $attrs->{as} = [qw/count/]; + + my $tmp_rs = $source->resultset_class->new($source, $attrs); + my ($count) = $tmp_rs->cursor->next; + + return $count; +} + sub source_bind_attributes { my ($self, $source) = @_;