From: Justin Hunter Date: Fri, 13 Mar 2009 04:20:32 +0000 (+0000) Subject: created count_distinct branch X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e284619ffc1be06fcf967a32f3c1af92bf261ee8;p=dbsrgits%2FDBIx-Class-Historic.git created count_distinct branch --- diff --git a/lib/DBIx/Class/ResultSet.pm b/lib/DBIx/Class/ResultSet.pm index 5969744..f4ac871 100644 --- a/lib/DBIx/Class/ResultSet.pm +++ b/lib/DBIx/Class/ResultSet.pm @@ -1128,11 +1128,11 @@ sub count { sub _count { # Separated out so pager can get the full count my $self = shift; - my $select = { count => '*' }; - my $attrs = { %{$self->_resolved_attrs} }; - if (my $group_by = delete $attrs->{group_by}) { + + if (my $group_by = $attrs->{group_by}) { delete $attrs->{having}; + delete $attrs->{order_by}; my @distinct = (ref $group_by ? @$group_by : ($group_by)); # todo: try CONCAT for multi-column pk my @pk = $self->result_source->primary_columns; @@ -1146,15 +1146,17 @@ sub _count { # Separated out so pager can get the full count } } - $select = { count => { distinct => \@distinct } }; + $attrs->{select} = $group_by; + $attrs->{from} = (ref $self)->new($self->result_source, $attrs)->cursor->as_query; } - $attrs->{select} = $select; + $attrs->{select} = { count => '*' }; $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/; + # offset, order by, group by, where and page are not needed to count. record_filter is cdbi + delete $attrs->{$_} for qw/rows offset order_by group_by where page pager record_filter/; + $self->result_source->resultset; my $tmp_rs = (ref $self)->new($self->result_source, $attrs); my ($count) = $tmp_rs->cursor->next; return $count; diff --git a/lib/DBIx/Class/Storage/DBI.pm b/lib/DBIx/Class/Storage/DBI.pm index 4bfa2e8..09dc92a 100644 --- a/lib/DBIx/Class/Storage/DBI.pm +++ b/lib/DBIx/Class/Storage/DBI.pm @@ -95,9 +95,19 @@ sub _find_syntax { sub select { my ($self, $table, $fields, $where, $order, @rest) = @_; + local $self->{having_bind} = []; if (ref $table eq 'SCALAR') { $table = $$table; } + elsif (ref $table eq 'HASH') { + ## what if they want to alias a sub query? + } + elsif (ref $table eq 'REF') { + #my ($sql, @bind) = @{${$t}}; push(@{$self->{having_bind}}, @bind;); + my $t = $table; + $table = shift @$$t; + while (my $b = shift @$$t) { push @{$self->{having_bind}}, $b; } + } elsif (not ref $table) { $table = $self->_quote($table); } @@ -106,7 +116,6 @@ sub select { @rest = (-1) unless defined $rest[0]; die "LIMIT 0 Does Not Compute" if $rest[0] == 0; # and anyway, SQL::Abstract::Limit will cause a barf if we don't first - local $self->{having_bind} = []; my ($sql, @ret) = $self->SUPER::select( $table, $self->_recurse_fields($fields), $where, $order, @rest );