From: Matt S Trout Date: Sat, 18 Mar 2006 17:51:28 +0000 (+0000) Subject: Fixup for count X-Git-Tag: v0.06000~60^2~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=84e3c1143175058ea5d3d24eb31d529be09a6806;p=dbsrgits%2FDBIx-Class.git Fixup for count --- diff --git a/lib/DBIx/Class.pm b/lib/DBIx/Class.pm index 0a0bce5..f59fc22 100644 --- a/lib/DBIx/Class.pm +++ b/lib/DBIx/Class.pm @@ -125,57 +125,55 @@ Matt S. Trout =head1 CONTRIBUTORS +Alexander Hartmaier + Andy Grundman -Brian Cassidy +Andres Kievsky -Dan Kubb +Brandon Black -Dan Sully +Brian Cassidy -David Kamholz +Christopher H. Laco -Jules Bean +CL Kao -Marcus Ramberg +Daisuke Murase -Paul Makepeace +Dan Kubb -CL Kao +Dan Sully -Jess Robinson +Daniel Westermann-Clark -Marcus Ramberg +David Kamholz -Will Hawes +Jesper Krogh -Todd Lipcon +Jess Robinson -Daniel Westermann-Clark +Jules Bean -Alexander Hartmaier +Justin Guenther -Zbigniew Lukasiak +Marcus Ramberg Nigel Metheringham -Jesper Krogh - -Brandon Black - -Christopher H. Laco +Paul Makepeace -Scotty Allen +Robert Sedlacek -sc_ +sc_ of irc.perl.org#dbix-class -Robert Sedlacek +Scotty Allen -Daisuke Murase +Todd Lipcon -Scott McWhirter (konobi) +Will Hawes =head1 LICENSE diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 626e1f7..4e5d1ee 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -505,43 +505,49 @@ clause. sub count { my $self = shift; return $self->search(@_)->count if @_ and defined $_[0]; - unless (defined $self->{count}) { - return scalar @{ $self->get_cache } if @{ $self->get_cache }; - my $select = { count => '*' }; - my $attrs = { %{ $self->{attrs} } }; - if (my $group_by = delete $attrs->{group_by}) { - delete $attrs->{having}; - my @distinct = (ref $group_by ? @$group_by : ($group_by)); - # todo: try CONCAT for multi-column pk - my @pk = $self->result_source->primary_columns; - if (@pk == 1) { - foreach my $column (@distinct) { - if ($column =~ qr/^(?:\Q$attrs->{alias}.\E)?$pk[0]$/) { - @distinct = ($column); - last; - } - } - } + return scalar @{ $self->get_cache } if @{ $self->get_cache }; - $select = { count => { distinct => \@distinct } }; - #use Data::Dumper; die Dumper $select; - } + my $count = $self->_count; + return 0 unless $count; - $attrs->{select} = $select; - $attrs->{as} = [qw/count/]; - # offset, order by and page are not needed to count. record_filter is cdbi - delete $attrs->{$_} for qw/rows offset order_by page pager record_filter/; - - ($self->{count}) = (ref $self)->new($self->result_source, $attrs)->cursor->next; - } - return 0 unless $self->{count}; - my $count = $self->{count}; $count -= $self->{attrs}{offset} if $self->{attrs}{offset}; $count = $self->{attrs}{rows} if $self->{attrs}{rows} and $self->{attrs}{rows} < $count; return $count; } +sub _count { # Separated out so pager can get the full count + my $self = shift; + my $select = { count => '*' }; + my $attrs = { %{ $self->{attrs} } }; + if (my $group_by = delete $attrs->{group_by}) { + delete $attrs->{having}; + my @distinct = (ref $group_by ? @$group_by : ($group_by)); + # todo: try CONCAT for multi-column pk + my @pk = $self->result_source->primary_columns; + if (@pk == 1) { + foreach my $column (@distinct) { + if ($column =~ qr/^(?:\Q$attrs->{alias}.\E)?$pk[0]$/) { + @distinct = ($column); + last; + } + } + } + + $select = { count => { distinct => \@distinct } }; + #use Data::Dumper; die Dumper $select; + } + + $attrs->{select} = $select; + $attrs->{as} = [qw/count/]; + + # offset, order by and page are not needed to count. record_filter is cdbi + delete $attrs->{$_} for qw/rows offset order_by page pager record_filter/; + + my ($count) = (ref $self)->new($self->result_source, $attrs)->cursor->next; + return $count; +} + =head2 count_literal Calls L with the passed arguments, then L. @@ -698,9 +704,8 @@ sub pager { my $attrs = $self->{attrs}; $self->throw_exception("Can't create pager for non-paged rs") unless $self->{page}; $attrs->{rows} ||= 10; - $self->count; return $self->{pager} ||= Data::Page->new( - $self->{count}, $attrs->{rows}, $self->{page}); + $self->_count, $attrs->{rows}, $self->{page}); } =head2 page diff --git a/lib/DBIx/Class/Schema.pm b/lib/DBIx/Class/Schema.pm index 7e39162..d5c0658 100644 --- a/lib/DBIx/Class/Schema.pm +++ b/lib/DBIx/Class/Schema.pm @@ -519,11 +519,13 @@ sub populate { my ($self, $name, $data) = @_; my $rs = $self->resultset($name); my @names = @{shift(@$data)}; + my @created; foreach my $item (@$data) { my %create; @create{@names} = @$item; - $rs->create(\%create); + push(@created, $rs->create(\%create)); } + return @created; } =head2 throw_exception