PEAR is discontinued

This is a static archive preserving historical releases and metadata. No new packages or updates will be published. For updates to existing packages, or to publish new packages, please use Composer and Packagist.

Home » Database » MDB2 » Bug #3675

MDB2_Iterator changes

Details

Request #3675MDB2_Iterator changes
Submitted2005-03-02 21:29 UTC
Fromjustinh at superglobals dot com
StatusClosed
PackageMDB2
PHP Version5.0.3
Roadmaps(Not assigned)

Comments

[2005-03-02 21:29 UTC] justinh at superglobals dot com

Description:
------------
I was just going though the MDB2_(Buffered)Iterator class and thought I'd submit a small patch to update the interfaces and return values to the SPL stated interfaces.

Changes:
MDB2_Iterator implements the SeekableIterator interface
MDB2_Iterator return values now match the interface
MDB2_BufferedIterator implements the Countable interface
MDB2_BuffererIterator size method renamed count
Updated PHPDocs where changes were made

It might also be a good idea to toss an MDB2_Error when rewinding the MDB2_Iterator, since this operation is illegal.

Here's the patch:
--- Iterator.php Wed Mar 2 15:42:10 2005
+++ public_html/sVote/Libraries/MDB2/Iterator.php Sun Jan 2 21:41:00 2005
@@ -50,7 +50,7 @@
* @author Lukas Smith <smith@backendmedia.com>
*/

-class MDB2_Iterator extends MDB2_Result implements SeekableIterator
+class MDB2_Iterator extends MDB2_Result implements Iterator
{
private $result;
private $row;
@@ -73,13 +73,20 @@
* seek forward to a specific row in a result set
*
* @param int $rownum number of the row where the data can be found
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
* @access public
*/
function seek($rownum = 0)
{
+ if ($this->result->rownum == $rownum) {
+ return true;
+ }
+ if ($this->result->rownum < $rownum) {
+ return false;
+ }
$this->row = null;
$this->buffer = null;
- $this->result->seek($rownum);
+ return $this->result->seek($rownum);
}

// }}}
@@ -88,6 +95,7 @@
/**
* Fetch next row of data
*
+ * @return mixed data array on success, a MDB2 error on failure
* @access public
*/
function next()
@@ -95,12 +103,15 @@
if ($this->buffer) {
$this->row = $this->buffer;
$this->buffer = null;
+ return true;
}
$row = $this->result->fetchRow();
if (MDB2::isError($row)) {
$this->row = null;
+ return false;
}
$this->row = $row;
+ return true;
}

// }}}
@@ -176,6 +187,7 @@
/**
* seek to the first row in a result set
*
+ * @return mixed MDB2_OK on success, a MDB2 error on failure
* @access public
*/
function rewind()
@@ -183,7 +195,7 @@
}
}

-class MDB2_BufferedIterator extends MDB2_Iterator implements Countable
+class MDB2_BufferedIterator extends MDB2_Iterator implements Iterator
{
// {{{ seek()

@@ -197,7 +209,7 @@
function seek($rownum = 0)
{
$this->row = null;
- $this->result->seek($rownum);
+ return $this->result->seek($rownum);
}

// }}}
@@ -220,6 +232,7 @@
/**
* Fetch next row of data
*
+ * @return mixed data array on success, a MDB2 error on failure
* @access public
*/
function next()
@@ -227,8 +240,10 @@
$row = $this->result->fetchRow();
if (MDB2::isError($row)) {
$this->row = null;
+ return false;
}
$this->row = $row;
+ return true;
}

// }}}
@@ -240,7 +255,7 @@
* @return mixed MDB2 Error Object or the number of rows
* @access public
*/
- function count()
+ function size()
{
return $this->result->numRows();
}
@@ -270,7 +285,7 @@
*/
function rewind()
{
- $this->seek(0);
+ return $this->seek(0);
}

// }}}
@@ -294,4 +309,4 @@
}
}

-?>
+?>
\ No newline at end of file

Reproduce code:
---------------
-

Expected result:
----------------
-

Actual result:
--------------
-

[2005-03-04 13:13 UTC] smith at backendmedia dot com

Does the SeekableIterator also apply when you can only seek forward, which is the case for the unbuffered iterator?

[2005-03-06 19:10 UTC] smith at backendmedia dot com

This bug has been fixed in CVS.

In case this was a documentation problem, the fix will show up at the
end of next Sunday (CET) on pear.php.net.

In case this was a pear.php.net website problem, the change will show
up on the website in short time.

Thank you for the report, and for helping us make PEAR better.

I am holding off of the Countable since its PHP 5.1 only. Also I will for now keep the SeekableIterator for the buffered result sets only as the unbuffered doesnt allow rewinding.