From: Matt S Trout Date: Sun, 31 Jul 2005 22:56:09 +0000 (+0000) Subject: Modified Cursor to use COUNT(*) for count if possible X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=5464485558c3ed1050cfb6d3dfbab11a0de16915;p=dbsrgits%2FDBIx-Class-Historic.git Modified Cursor to use COUNT(*) for count if possible --- diff --git a/lib/DBIx/Class/Cursor.pm b/lib/DBIx/Class/Cursor.pm index d492ebb..b28bf27 100644 --- a/lib/DBIx/Class/Cursor.pm +++ b/lib/DBIx/Class/Cursor.pm @@ -7,13 +7,14 @@ use overload fallback => 1; sub new { - my ($it_class, $db_class, $sth, $args, $cols) = @_; + my ($it_class, $db_class, $sth, $args, $cols, $attrs) = @_; $sth->execute(@{$args || []}) unless $sth->{Active}; my $new = { class => $db_class, sth => $sth, cols => $cols, - args => $args }; + args => $args, + attrs => $attrs }; return bless ($new, $it_class); } @@ -26,7 +27,18 @@ sub next { } sub count { - return scalar $_[0]->all; # So inefficient + my ($self) = @_; + if (my $cond = $self->{attrs}->{where}) { + my $class = $self->{class}; + my $sth = $class->_get_sth( 'select', [ 'COUNT(*)' ], + $class->_table_name, $cond); + $sth->execute(@{$self->{args} || []}); + my ($count) = $sth->fetchrow_array; + $sth->finish; + return $count; + } else { + return scalar $_[0]->all; # So inefficient + } } sub all { diff --git a/lib/DBIx/Class/Table.pm b/lib/DBIx/Class/Table.pm index d7d67a8..6caae42 100644 --- a/lib/DBIx/Class/Table.pm +++ b/lib/DBIx/Class/Table.pm @@ -162,15 +162,15 @@ sub retrieve_from_sql { my @cols = $class->_select_columns($attrs); my $sth = $class->_get_sth( 'select', \@cols, $class->_table_name, $cond); #warn "$cond @vals"; - return $class->sth_to_objects($sth, \@vals, \@cols); + return $class->sth_to_objects($sth, \@vals, \@cols, { where => $cond }); } sub sth_to_objects { - my ($class, $sth, $args, $cols) = @_; + my ($class, $sth, $args, $cols, $attrs) = @_; my @cols = ((ref $cols eq 'ARRAY') ? @$cols : @{$sth->{NAME_lc}} ); my $cursor_class = $class->_cursor_class; eval "use $cursor_class;"; - my $cursor = $cursor_class->new($class, $sth, $args, \@cols); + my $cursor = $cursor_class->new($class, $sth, $args, \@cols, $attrs); return (wantarray ? $cursor->all : $cursor); }