Created
February 21, 2012 07:28
-
-
Save eddieajau/1874789 to your computer and use it in GitHub Desktop.
Joomla Database Iterator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @package Joomla.Platform | |
* @subpackage Database | |
* | |
* @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved. | |
* @license GNU General Public License version 2 or later; see LICENSE | |
*/ | |
defined('JPATH_PLATFORM') or die; | |
/** | |
* Joomla Platform Database Driver Class | |
* | |
* @package Joomla.Platform | |
* @subpackage Database | |
* @since 12.1 | |
*/ | |
abstract class JDatabaseIterator implements Countable, Iterator | |
{ | |
/** | |
* The database cursor. | |
* | |
* @var mixed | |
* @since 12.1 | |
*/ | |
protected $cursor; | |
/** | |
* The class of object to create. | |
* | |
* @var string | |
* @since 12.1 | |
*/ | |
private $_class; | |
/** | |
* The name of the column to use for the key of the database record. | |
* | |
* @var mixed | |
* @since 12.1 | |
*/ | |
private $_column; | |
/** | |
* The current database record. | |
* | |
* @var mixed | |
* @since 12.1 | |
*/ | |
private $_current; | |
/** | |
* A numeric or string key for the current database record. | |
* | |
* @var scalar | |
* @since 12.1 | |
*/ | |
private $_key; | |
/** | |
* The number of fetched records. | |
* | |
* @var integer | |
* @since 12.1 | |
*/ | |
private $_fetched = 0; | |
/** | |
* Database iterator constructor. | |
* | |
* @param mixed $cursor The database cursor. | |
* @param string $class The class of object that is returned. | |
* @param string $column An option column to use as the iterator key. | |
* | |
* @throws InvalidArgumentException | |
*/ | |
public function __construct($cursor, $class = 'stdClass', $column = null) | |
{ | |
if (!class_exists($class)) | |
{ | |
throw new InvalidArgumentException(sprintf('new %s(cursor, *%s*)', get_class($this), gettype($class))); | |
} | |
$this->cursor = $cursor; | |
$this->_class = $class; | |
$this->_column = $column; | |
} | |
/** | |
* Get the number of rows in the result set for the executed SQL given by the cursor. | |
* | |
* @return integer The number of rows in the result set. | |
* | |
* @since 12.1 | |
*/ | |
public function count() | |
{ | |
return $this->fetchCount(); | |
} | |
/** | |
* The current element in the iterator. | |
* | |
* @return object | |
* | |
* @see Iterator::current() | |
* @since 12.1 | |
*/ | |
public function current() | |
{ | |
return $this->_current; | |
} | |
/** | |
* Frees the result memory. | |
* | |
* @return object | |
* | |
* @see Iterator::current() | |
* @since 12.1 | |
*/ | |
public function free() | |
{ | |
if ($this->_cursor) | |
{ | |
$this->freeResult(); | |
$this->_cursor = null; | |
} | |
} | |
/** | |
* The key of the current element in the iterator. | |
* | |
* @return scalar | |
* | |
* @see Iterator::key() | |
* @since 12.1 | |
*/ | |
public function key() | |
{ | |
return $this->_key; | |
} | |
/** | |
* Moves forward to the next result from the SQL query. | |
* | |
* @return void | |
* | |
* @see Iterator::next() | |
* @since 12.1 | |
*/ | |
public function next() | |
{ | |
$this->_key = $this->_fetched; | |
$this->_current = $this->fetchObject($this->_class); | |
if ($this->_current) | |
{ | |
if (isset($this->_current->{$this->column})) | |
{ | |
$this->_key = $this->_current->{$this->column}; | |
} | |
$this->_fetched++; | |
} | |
else | |
{ | |
$this->_key = null; | |
} | |
} | |
/** | |
* Rewinds the iterator. | |
* | |
* @return void | |
* | |
* @see Iterator::rewind() | |
* @since 12.1 | |
*/ | |
public function rewind() | |
{ | |
$this->_key = null; | |
$this->_current = null; | |
$this->_fetched = 0; | |
} | |
/** | |
* Checks if the current position of the iterator is valid. | |
* | |
* @return boolean | |
* | |
* @see Iterator::valid() | |
* @since 12.1 | |
*/ | |
public function valid() | |
{ | |
if ($this->_current) | |
{ | |
return true; | |
} | |
else | |
{ | |
$this->free(); | |
return false; | |
} | |
} | |
/** | |
* Method to fetch a row from the result set cursor as an object. | |
* | |
* @param string $class The class name to use for the returned row object. | |
* | |
* @return mixed Either the next row from the result set or false if there are no more rows. | |
* | |
* @since 11.1 | |
*/ | |
abstract protected function fetchObject($class = 'stdClass'); | |
/** | |
* Method to free up the memory used for the result set. | |
* | |
* @return void | |
* | |
* @since 11.1 | |
*/ | |
abstract protected function freeResult(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @package Joomla.Platform | |
* @subpackage Database | |
* | |
* @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved. | |
* @license GNU General Public License version 2 or later; see LICENSE | |
*/ | |
defined('JPATH_PLATFORM') or die; | |
/** | |
* MySQL database iterator. | |
* | |
* @package Joomla.Platform | |
* @subpackage Database | |
* @see http://dev.mysql.com/doc/ | |
* @since 12.1 | |
*/ | |
class JDatabaseIteratorMysql extends JDatabaseIterator | |
{ | |
/** | |
* Get the number of returned rows for the previous executed SQL statement. | |
* | |
* @return integer The number of returned rows. | |
* | |
* @since 12.1 | |
*/ | |
public function fetchCount() | |
{ | |
return mysql_num_rows($this->cursor); | |
} | |
/** | |
* Method to fetch a row from the result set cursor as an object. | |
* | |
* @param string $class The class name to use for the returned row object. | |
* | |
* @return mixed Either the next row from the result set or false if there are no more rows. | |
* | |
* @since 12.1 | |
*/ | |
protected function fetchObject($class = 'stdClass') | |
{ | |
return mysql_fetch_object($this->cursor, $class); | |
} | |
/** | |
* Method to free up the memory used for the result set. | |
* | |
* @return void | |
* | |
* @since 12.1 | |
*/ | |
protected function freeResult() | |
{ | |
mysql_free_result($this->cursor); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment