Re: DB_Result fetchRow() && fetchInto()
| From: | Roman Neuhauser | Date: | Wed, 11 Jun 2003 09:57:29 +0000 |
| Subject: | Re: DB_Result fetchRow() && fetchInto() | ||
| References: | 1 2 | Groups: | php.pear.dev |
| Request: | Send a blank email to pear-dev+get-17302@lists.php.net to get a copy of this message | ||
# cox@idecnet.com / 2003-06-11 11:14:24 +0200:
> From: "Roman Neuhauser" <neuhauser@bellavista.cz>
> > these two methods are quite long, and have basically identical bodies.
> > why doesn't one just call the other?
>
> Performance.
Have you volunteered to keep the redundant code in sync? If you did,
you failed. The answer should've been: bugs and more maintenance
work. Diff their bodies:
With DB_FETCHMODE_OBJECT, one does $ret =& new $class, the other
$ret = new $class => different behavior.
Also, one of them doesn't free the result in certain circumstances.
Performance is nothing when compared to hard to maintain software,
and hard to find bugs that appear as a result of 1). Use cpp(1)
if you want performance without (that much of) lost maintainability.
If you like buggy but fast software, that's your call. I prefer
software that works correctly.
Log message:
DB_Result::fetchInto()
* fixed a bug where the result was not freed if autofree was set and
the code has reached the limit end
* changed the logic after the call to $this->dbh->fetchInto() to
mirror code in DB_Result::fetchRow()
* fixed a bug where with DB_FETCHMODE_OBJECT the object was created
with copying instead of by reference (as was done in
DB_Result::fetchRow())
DB_Result::fetchRow()
* calls DB_Result::fetchInto() instead of duplicating ~50 lines of
code already found in this method
--
If you cc me or remove the list(s) completely I'll most likely ignore
your message. see http://www.eyrie.org./~eagle/faqs/questions.html
Index: DB/DB.php =================================================================== RCS file: /repository/pear/DB/DB.php,v retrieving revision 1.21 diff -u -r1.21 DB.php --- DB/DB.php 7 Jun 2003 18:33:04 -0000 1.21 +++ DB/DB.php 11 Jun 2003 09:29:33 -0000 @@ -722,52 +722,9 @@ */ function fetchRow($fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null) { - if ($fetchmode === DB_FETCHMODE_DEFAULT) { - $fetchmode = $this->fetchmode; - } - if ($fetchmode === DB_FETCHMODE_OBJECT) { - $fetchmode = DB_FETCHMODE_ASSOC; - $object_class = $this->fetchmode_object_class; - } - if ($this->limit_from !== null) { - if ($this->row_counter === null) { - $this->row_counter = $this->limit_from; - // Skip rows - if ($this->limit_type == false) { - $i = 0; - while ($i++ < $this->limit_from) { - $this->dbh->fetchInto($this->result, $arr, $fetchmode); - } - } - } - if ($this->row_counter >= ( - $this->limit_from + $this->limit_count)) - { - if ($this->autofree) { - $this->free(); - } - return null; - } - if ($this->limit_type == 'emulate') { - $rownum = $this->row_counter; - } - $this->row_counter++; - } - $res = $this->dbh->fetchInto($this->result, $arr, $fetchmode, $rownum); - if ($res !== DB_OK) { - if ($res == null && $this->autofree) { - $this->free(); - } - return $res; - } - if (isset($object_class)) { - // default mode specified in DB_common::fetchmode_object_class property - if ($object_class == 'stdClass') { - $ret = (object) $arr; - } else { - $ret =& new $object_class($arr); - } - return $ret; + $rv = $this->fetchInto($arr, $fetchmode, $rownum); + if (DB_OK != $rv) { + return $rv; } return $arr; } @@ -809,6 +766,9 @@ if ($this->row_counter >= ( $this->limit_from + $this->limit_count)) { + if ($this->autofree) { + $this->free(); + } return null; } if ($this->limit_type == 'emulate') { @@ -818,15 +778,19 @@ $this->row_counter++; } $res = $this->dbh->fetchInto($this->result, $arr, $fetchmode, $rownum); - if (($res === DB_OK) && isset($object_class)) { + if ($res !== DB_OK) { + if ($res == null && $this->autofree) { + $this->free(); + } + return $res; + } + if (isset($object_class)) { // default mode specified in DB_common::fetchmode_object_class property if ($object_class == 'stdClass') { $arr = (object) $arr; } else { - $arr = new $object_class($arr); + $arr =& new $object_class($arr); } - } elseif ($res == null && $this->autofree) { - $this->free(); } return $res; }
Index: DB/DB.php =================================================================== RCS file: /repository/pear/DB/DB.php,v retrieving revision 1.21 diff -u -r1.21 DB.php --- DB/DB.php 7 Jun 2003 18:33:04 -0000 1.21 +++ DB/DB.php 11 Jun 2003 09:29:33 -0000 @@ -722,52 +722,9 @@ */ function fetchRow($fetchmode = DB_FETCHMODE_DEFAULT, $rownum=null) { - if ($fetchmode === DB_FETCHMODE_DEFAULT) { - $fetchmode = $this->fetchmode; - } - if ($fetchmode === DB_FETCHMODE_OBJECT) { - $fetchmode = DB_FETCHMODE_ASSOC; - $object_class = $this->fetchmode_object_class; - } - if ($this->limit_from !== null) { - if ($this->row_counter === null) { - $this->row_counter = $this->limit_from; - // Skip rows - if ($this->limit_type == false) { - $i = 0; - while ($i++ < $this->limit_from) { - $this->dbh->fetchInto($this->result, $arr, $fetchmode); - } - } - } - if ($this->row_counter >= ( - $this->limit_from + $this->limit_count)) - { - if ($this->autofree) { - $this->free(); - } - return null; - } - if ($this->limit_type == 'emulate') { - $rownum = $this->row_counter; - } - $this->row_counter++; - } - $res = $this->dbh->fetchInto($this->result, $arr, $fetchmode, $rownum); - if ($res !== DB_OK) { - if ($res == null && $this->autofree) { - $this->free(); - } - return $res; - } - if (isset($object_class)) { - // default mode specified in DB_common::fetchmode_object_class property - if ($object_class == 'stdClass') { - $ret = (object) $arr; - } else { - $ret =& new $object_class($arr); - } - return $ret; + $rv = $this->fetchInto($arr, $fetchmode, $rownum); + if (DB_OK != $rv) { + return $rv; } return $arr; } @@ -809,6 +766,9 @@ if ($this->row_counter >= ( $this->limit_from + $this->limit_count)) { + if ($this->autofree) { + $this->free(); + } return null; } if ($this->limit_type == 'emulate') { @@ -818,15 +778,19 @@ $this->row_counter++; } $res = $this->dbh->fetchInto($this->result, $arr, $fetchmode, $rownum); - if (($res === DB_OK) && isset($object_class)) { + if ($res !== DB_OK) { + if ($res == null && $this->autofree) { + $this->free(); + } + return $res; + } + if (isset($object_class)) { // default mode specified in DB_common::fetchmode_object_class property if ($object_class == 'stdClass') { $arr = (object) $arr; } else { - $arr = new $object_class($arr); + $arr =& new $object_class($arr); } - } elseif ($res == null && $this->autofree) { - $this->free(); } return $res; }