cvs: pear /MDB2/MDB2 Iterator.php

From: Date: Thu, 07 Apr 2005 15:54:43 +0000
Subject: cvs: pear /MDB2/MDB2 Iterator.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-31021@lists.php.net to get a copy of this message
lsmith Thu Apr 7 11:54:43 2005 EDT Modified files: /pear/MDB2/MDB2 Iterator.php Log: - severely reworked how data is fetched and buffered - better handle freed results http://cvs.php.net/diff.php/pear/MDB2/MDB2/Iterator.php?r1=1.11&r2=1.12&ty=u Index: pear/MDB2/MDB2/Iterator.php diff -u pear/MDB2/MDB2/Iterator.php:1.11 pear/MDB2/MDB2/Iterator.php:1.12 --- pear/MDB2/MDB2/Iterator.php:1.11 Fri Apr 1 05:10:11 2005 +++ pear/MDB2/MDB2/Iterator.php Thu Apr 7 11:54:41 2005 @@ -42,27 +42,26 @@ // | Author: Lukas Smith <smith@backendmedia.com> | // +----------------------------------------------------------------------+ // -// $Id: Iterator.php,v 1.11 2005/04/01 10:10:11 lsmith Exp $ +// $Id: Iterator.php,v 1.12 2005/04/07 15:54:41 lsmith Exp $ /** * @package MDB2 * @category Database * @author Lukas Smith <smith@backendmedia.com> */ -class MDB2_Iterator extends MDB2_Result implements Iterator +class MDB2_Iterator implements Iterator { - private $result; - private $row; - private $buffer; + protected $result; + protected $row; // {{{ constructor /** * Constructor */ - function __construct(&$result) + public function __construct($result) { - $this->result =& $result; + $this->result = $result; } // }}} @@ -75,9 +74,12 @@ * @return void * @access public */ - function seek($rownum = 0) + public function seek($rownum) { - $this->result->seek($rownum); + $this->row = null; + if ($this->result) { + $this->result->seek($rownum); + } } // }}} @@ -89,19 +91,9 @@ * @return void * @access public */ - function next() + public function next() { - if ($this->buffer) { - $this->row = $this->buffer; - $this->buffer = null; - } else { - $row = $this->result->fetchRow(); - if (PEAR::isError($row)) { - $this->row = null; - } else { - $this->row = $row; - } - } + $this->row = null; } // }}} @@ -113,8 +105,15 @@ * @return void * @access public */ - function current() + public function current() { + if (is_null($this->row)) { + $row = $this->result->fetchRow(); + if (PEAR::isError($row)) { + $row = false; + } + $this->row = $row; + } return $this->row; } @@ -127,9 +126,9 @@ * @return mixed true or false on sucess, a MDB2 error on failure * @access public */ - function valid() + public function valid() { - return $this->result->valid(); + return (bool)$this->current(); } // }}} @@ -141,20 +140,14 @@ * @return boolean true on success, false if result is invalid * @access public */ - function free() - { - return $this->result->free(); - } - - // }}} - // {{{ destructor - - /** - * Destructor - */ - function __destruct() + public function free() { - $this->free(); + if ($this->result) { + return $this->result->free(); + } + $this->result = null; + $this->row = null; + return false; } // }}} @@ -166,9 +159,12 @@ * @return void * @access public */ - function key() + public function key() { - $this->result->getRowCount(); + if ($this->result) { + return $this->result->getRowCounter(); + } + return false; } // }}} @@ -180,61 +176,39 @@ * @return void * @access public */ - function rewind() + public function rewind() { - MDB2::raiseError(MDB2_ERROR_NOT_CAPABLE); } -} -class MDB2_BufferedIterator extends MDB2_Iterator implements SeekableIterator -{ - // {{{ seek() + // }}} + // {{{ destructor /** - * seek to a specific row in a result set - * - * @param int $rownum number of the row where the data can be found - * @return void - * @access public - */ - function seek($rownum) + * Destructor + */ + public function __destruct() { - $this->row = null; - $this->result->seek($rownum); + $this->free(); } +} +class MDB2_BufferedIterator extends MDB2_Iterator implements SeekableIterator +{ // }}} // {{{ valid() /** * check if the end of the result set has been reached * - * @return void - * @access public - */ - function valid() - { - return $this->result->valid(); - } - - // }}} - // {{{ next() - - /** - * Fetch next row of data - * - * @return void + * @return mixed true or false on sucess, a MDB2 error on failure * @access public */ - function next() + public function valid() { - $row = $this->result->fetchRow(); - if (PEAR::isError($row)) { - $this->row = null; - return false; + if ($this->result) { + return $this->result->valid(); } - $this->row = $row; - return true; + return false; } // }}} @@ -246,9 +220,12 @@ * @return mixed MDB2 Error Object or the number of rows * @access public */ - function count() + public function count() { - return $this->result->numRows(); + if ($this->result) { + return $this->result->numRows(); + } + return false; } // }}} @@ -260,9 +237,12 @@ * @return mixed true or false on sucess, a MDB2 error on failure * @access public */ - function hasPrev() + public function hasPrev() { - return $this->result->rownum > - 1; + if ($this->result) { + return $this->result->getRowCounter() > 0; + } + return false; } // }}} @@ -274,7 +254,7 @@ * @return mixed MDB2_OK on success, a MDB2 error on failure * @access public */ - function rewind() + public function rewind() { $this->seek(0); } @@ -289,10 +269,10 @@ * @return void * @access public */ - function prev() + public function prev() { if ($this->hasPrev()) { - $this->seek($this->result->rownum - 1); + $this->seek($this->result->getRowCounter() - 1); } else { return false; }

« previous php.pear.cvs (#31021) next »