| // +----------------------------------------------------------------------+ // // $Id$ /** * @package MDB2 * @category Database * @author Lukas Smith */ class MDB2_Iterator implements Iterator { protected $fetchmode; protected $result; protected $row; // {{{ constructor /** * Constructor */ public function __construct($result, $fetchmode = MDB2_FETCHMODE_DEFAULT) { $this->result = $result; $this->fetchmode = $fetchmode; } // }}} // {{{ seek() /** * seek forward 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 */ public function seek($rownum) { $this->row = null; if ($this->result) { $this->result->seek($rownum); } } // }}} // {{{ next() /** * Fetch next row of data * * @return void * @access public */ public function next() { $this->row = null; } // }}} // {{{ current() /** * return a row of data * * @return void * @access public */ public function current() { if (is_null($this->row)) { $row = $this->result->fetchRow($this->fetchmode); if (PEAR::isError($row)) { $row = false; } $this->row = $row; } return $this->row; } // }}} // {{{ valid() /** * check if the end of the result set has been reached * * @return mixed true or false on sucess, a MDB2 error on failure * @access public */ public function valid() { return (bool)$this->current(); } // }}} // {{{ free() /** * Free the internal resources associated with result. * * @return boolean true on success, false if result is invalid * @access public */ public function free() { if ($this->result) { return $this->result->free(); } $this->result = null; $this->row = null; return false; } // }}} // {{{ key() /** * nothing, but Iterator wants to implement this. * * @return void * @access public */ public function key() { if ($this->result) { return $this->result->rowCount(); } return false; } // }}} // {{{ rewind() /** * seek to the first row in a result set * * @return void * @access public */ public function rewind() { } // }}} // {{{ destructor /** * Destructor */ public function __destruct() { $this->free(); } } class MDB2_BufferedIterator extends MDB2_Iterator implements SeekableIterator { // }}} // {{{ valid() /** * check if the end of the result set has been reached * * @return mixed true or false on sucess, a MDB2 error on failure * @access public */ public function valid() { if ($this->result) { return $this->result->valid(); } return false; } // }}} // {{{count() /** * returns the number of rows in a result object * * @return mixed MDB2 Error Object or the number of rows * @access public */ public function count() { if ($this->result) { return $this->result->numRows(); } return false; } // }}} // {{{ hasPrev() /** * check if there is a previous row * * @return mixed true or false on sucess, a MDB2 error on failure * @access public */ public function hasPrev() { if ($this->result) { return $this->result->rowCount() > 0; } return false; } // }}} // {{{ rewind() /** * seek to the first row in a result set * * @return mixed MDB2_OK on success, a MDB2 error on failure * @access public */ public function rewind() { $this->seek(0); } // }}} // {{{ prev() /** * move internal row point to the previous row * Fetch and return a row of data * * @return void * @access public */ public function prev() { if ($this->hasPrev()) { $this->seek($this->result->rowCount() - 1); } else { return false; } return $this->next(); } } ?>