Thanks Philip.
I've applied it, with just a couple of changes. The first is cosmetic,
the second fixes a bug when proxy_rows is zero.
Any chance you could write some tests for this?
Tim.
--- lib/DBD/Proxy.pm (revision 9871)
+++ lib/DBD/Proxy.pm (working copy)
@@ -593,8 +593,7 @@
my $data = $sth->{'proxy_data'};
- defined($sth->{'proxy_rows'}) ||
- ( $sth->{'proxy_rows'} = 0 );
+ $sth->{'proxy_rows'} = 0 unless defined $sth->{'proxy_rows'};
if(!$data || [EMAIL PROTECTED]) {
return undef unless $sth->SUPER::FETCH('Active');
@@ -625,8 +624,8 @@
*fetchrow_arrayref = \&fetch;
sub rows ($) {
- my($sth) = @_;
- $sth->{'proxy_rows'} || -1;
+ my $rows = shift->{'proxy_rows'};
+ return (defined $rows) ? $rows : -1;
}
sub finish ($) {
On Fri, Aug 17, 2007 at 08:18:43AM -0400, Philip Dye wrote:
> Here is the patch implementing 'rows fetched so far'. rows() now also
> properly returns -1 when the
> $sth->{'proxy_rows'} is not defined. It is debatable whether execute
> should undefine proxy _rows or set
> it to 0. Unless someone has a good argument otherwise, it seems to me
> that undefining proxy_rows is the
> most reasonable thing to do there.
> - philip
>
> Philip Dye wrote:
>
> Tim,
>
> You are quite correct. I was too hasty in writing that first patch.
> According to the DBI
> documentation there are actually two approaches in vogue for select
> operations:
>
> [1]rows
>
> $rv = $sth->rows;
>
> Returns the number of rows affected by the last row affecting command,
> or -1 if the number of rows
> is not known or not available.
>
> Generally, you can only rely on a row count after a non-SELECT execute
> (for some specific operations
> like UPDATE and DELETE), or after fetching all the rows of a SELECT
> statement.
>
> For SELECT statements, it is generally not possible to know how many
> rows will be returned except by
> fetching them all. Some drivers will return the number of rows the
> application has fetched so far,
> but others may return -1 until all rows have been fetched. So use of
> the rows method or $DBI::rows
> with SELECT statements is not recommended.
>
> I will implement the 'rows fetched so far' model. Oddly enough neither
> my earlier fix or this fix
> will fix the problem I had with Rose::DB::Object::load() that expects
> sth->rows() to be greater than
> zero if and only if the most recent fetch() has returned a row. I
> posted a fix
> Rose::DB::Object::load() to its mailing list.
>
> Ideally, the implementation of sth->rows() would become standardized so
> that its value would be
> dependable one way or the other after a fetch().
>
> Respectfully,
> Philip Dye
> Senior Research Systems Programmer
> Computing Facilities Group
> School of Computer Science
> [EMAIL PROTECTED]
>
> References
>
> Visible links
> 1. http://search.cpan.org/%7Etimb/DBI/DBI.pm#___top
> 2. mailto:[EMAIL PROTECTED]
> *** DBD/Proxy.pm Fri Aug 17 07:54:55 2007
> --- DBD/Proxy.pm.new Fri Aug 17 07:55:06 2007
> *************** sub execute ($@) {
> *** 510,515 ****
> --- 510,516 ----
>
> # new execute, so delete any cached rows from previous execute
> undef $sth->{'proxy_data'};
> + undef $sth->{'proxy_rows'};
>
> my $rsth = $sth->{proxy_sth};
> my $dbh = $sth->FETCH('Database');
> *************** sub fetch ($) {
> *** 590,595 ****
> --- 591,599 ----
>
> my $data = $sth->{'proxy_data'};
>
> + defined($sth->{'proxy_rows'}) ||
> + ( $sth->{'proxy_rows'} = 0 );
> +
> if(!$data || [EMAIL PROTECTED]) {
> return undef unless $sth->SUPER::FETCH('Active');
>
> *************** sub fetch ($) {
> *** 613,625 ****
> my $row = shift @$data;
>
> $sth->SUPER::STORE(Active => 0) if ( $sth->{proxy_cache_only} and
> [EMAIL PROTECTED] );
> return $sth->_set_fbav($row);
> }
> *fetchrow_arrayref = \&fetch;
>
> sub rows ($) {
> my($sth) = @_;
> ! $sth->{'proxy_rows'};
> }
>
> sub finish ($) {
> --- 617,630 ----
> my $row = shift @$data;
>
> $sth->SUPER::STORE(Active => 0) if ( $sth->{proxy_cache_only} and
> [EMAIL PROTECTED] );
> + $sth->{'proxy_rows'}++;
> return $sth->_set_fbav($row);
> }
> *fetchrow_arrayref = \&fetch;
>
> sub rows ($) {
> my($sth) = @_;
> ! $sth->{'proxy_rows'} || -1;
> }
>
> sub finish ($) {