From: Matt S Trout Date: Tue, 2 Aug 2005 16:46:41 +0000 (+0000) Subject: Added count_from_sql, refactored andyg's count_related implementation X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=fcbc5f294ee9df62da9d6123c1cffda1f0368aa8;p=dbsrgits%2FDBIx-Class-Historic.git Added count_from_sql, refactored andyg's count_related implementation --- diff --git a/lib/DBIx/Class/Relationship.pm b/lib/DBIx/Class/Relationship.pm index 87f8f11..569de9b 100644 --- a/lib/DBIx/Class/Relationship.pm +++ b/lib/DBIx/Class/Relationship.pm @@ -104,6 +104,18 @@ sub _cond_value { sub search_related { my $self = shift; + $self->_from_sql_related('retrieve', @_); +} + +sub count_related { + my $self = shift; + $self->_from_sql_related('count', @_); +} + +sub _from_sql_related { + my $self = shift; + my $op = shift; + my $meth = "${op}_from_sql"; my $rel = shift; my $attrs = { }; if (@_ > 1 && ref $_[$#_] eq 'HASH') { @@ -118,11 +130,11 @@ sub search_related { my $query = ((@_ > 1) ? {@_} : shift); $s_cond = $self->_cond_resolve($query, $attrs); } - $attrs->{_action} = 'convert'; + $attrs->{_action} = 'convert'; # shouldn't we resolve the cond to something + # to merge into the AST really? my ($cond) = $self->_cond_resolve($rel_obj->{cond}, $attrs); $cond = "${s_cond} AND ${cond}" if $s_cond; - return $rel_obj->{class}->retrieve_from_sql($cond, @{$attrs->{bind} || []}, - $attrs); + return $rel_obj->{class}->$meth($cond, @{$attrs->{bind} || []}, $attrs); } sub create_related { @@ -182,22 +194,6 @@ sub update_from_related { $self->update; } -sub count_related { - my $self = shift; - my $rel = shift; - my $rel_obj = $self->_relationships->{$rel}; - $self->throw( "No such relationship ${rel}" ) unless $rel_obj; - my $cond = $rel_obj->{cond}; - my $count_cond = {}; - foreach my $key (keys %$cond) { - $key =~ m/^foreign\.([^\.]+)$/; - my $count_key = $1; - $cond->{$key} =~ m/^self\.([^\.]+)$/; - $count_cond->{$count_key} = $self->get_column($1); - } - return $rel_obj->{class}->count( $count_cond ); -} - 1; =back diff --git a/lib/DBIx/Class/Table.pm b/lib/DBIx/Class/Table.pm index ec750fe..b02f071 100644 --- a/lib/DBIx/Class/Table.pm +++ b/lib/DBIx/Class/Table.pm @@ -170,6 +170,19 @@ sub retrieve_from_sql { return $class->sth_to_objects($sth, \@vals, \@cols, { where => $cond }); } +sub count_from_sql { + my ($class, $cond, @vals) = @_; + $cond =~ s/^\s*WHERE//i; + my $attrs = (ref $vals[$#vals] eq 'HASH' ? pop(@vals) : {}); + my @cols = 'COUNT(*)'; + my $sth = $class->_get_sth( 'select', \@cols, $class->_table_name, $cond); + #warn "$cond @vals"; + $sth->execute(@vals); + my ($count) = $sth->fetchrow_array; + $sth->finish; + return $count; +} + sub count { my $class = shift; my $attrs = { }; @@ -178,12 +191,7 @@ sub count { } my $query = ref $_[0] eq "HASH" ? shift: {@_}; my ($cond, @param) = $class->_cond_resolve($query, $attrs); - my $sth = $class->_get_sth( 'select', [ 'COUNT(*)' ], - $class->_table_name, $cond ); - $sth->execute(@param); - my ($count) = $sth->fetchrow_array; - $sth->finish; - return $count; + return $class->count_from_sql($cond, @param, $attrs); } sub sth_to_objects {