You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
265 lines
7.5 KiB
265 lines
7.5 KiB
14 years ago
|
<?php
|
||
|
/**
|
||
|
* @link http://www.yiiframework.com/
|
||
12 years ago
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
||
14 years ago
|
* @license http://www.yiiframework.com/license/
|
||
|
*/
|
||
|
|
||
12 years ago
|
namespace yii\db;
|
||
14 years ago
|
|
||
12 years ago
|
use yii\base\InvalidCallException;
|
||
14 years ago
|
|
||
14 years ago
|
/**
|
||
14 years ago
|
* DataReader represents a forward-only stream of rows from a query result set.
|
||
14 years ago
|
*
|
||
12 years ago
|
* To read the current row of data, call [[read()]]. The method [[readAll()]]
|
||
|
* returns all the rows in a single array. Rows of data can also be read by
|
||
|
* iterating through the reader. For example,
|
||
14 years ago
|
*
|
||
|
* ~~~
|
||
12 years ago
|
* $reader = $command->query('SELECT * FROM tbl_post');
|
||
|
*
|
||
|
* while ($row = $reader->read()) {
|
||
|
* $rows[] = $row;
|
||
|
* }
|
||
|
*
|
||
|
* // equivalent to:
|
||
14 years ago
|
* foreach($reader as $row) {
|
||
12 years ago
|
* $rows[] = $row;
|
||
14 years ago
|
* }
|
||
12 years ago
|
*
|
||
|
* // equivalent to:
|
||
|
* $rows = $reader->readAll();
|
||
14 years ago
|
* ~~~
|
||
|
*
|
||
12 years ago
|
* Note that since DataReader is a forward-only stream, you can only traverse it once.
|
||
|
* Doing it the second time will throw an exception.
|
||
14 years ago
|
*
|
||
|
* It is possible to use a specific mode of data fetching by setting
|
||
14 years ago
|
* [[fetchMode]]. See the [PHP manual](http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php)
|
||
|
* for more details about possible fetch mode.
|
||
14 years ago
|
*
|
||
13 years ago
|
* @property boolean $isClosed whether the reader is closed or not.
|
||
|
* @property integer $rowCount number of rows contained in the result.
|
||
|
* @property integer $columnCount the number of columns in the result set.
|
||
|
* @property mixed $fetchMode fetch mode used when retrieving the data.
|
||
|
*
|
||
14 years ago
|
* @author Qiang Xue <qiang.xue@gmail.com>
|
||
14 years ago
|
* @since 2.0
|
||
14 years ago
|
*/
|
||
13 years ago
|
class DataReader extends \yii\base\Object implements \Iterator, \Countable
|
||
14 years ago
|
{
|
||
13 years ago
|
/**
|
||
|
* @var \PDOStatement the PDOStatement associated with the command
|
||
|
*/
|
||
14 years ago
|
private $_statement;
|
||
|
private $_closed = false;
|
||
|
private $_row;
|
||
|
private $_index = -1;
|
||
|
|
||
|
/**
|
||
|
* Constructor.
|
||
14 years ago
|
* @param Command $command the command generating the query result
|
||
13 years ago
|
* @param array $config name-value pairs that will be used to initialize the object properties
|
||
14 years ago
|
*/
|
||
13 years ago
|
public function __construct(Command $command, $config = array())
|
||
14 years ago
|
{
|
||
13 years ago
|
$this->_statement = $command->pdoStatement;
|
||
14 years ago
|
$this->_statement->setFetchMode(\PDO::FETCH_ASSOC);
|
||
13 years ago
|
parent::__construct($config);
|
||
14 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Binds a column to a PHP variable.
|
||
|
* When rows of data are being fetched, the corresponding column value
|
||
|
* will be set in the variable. Note, the fetch mode must include PDO::FETCH_BOUND.
|
||
|
* @param mixed $column Number of the column (1-indexed) or name of the column
|
||
|
* in the result set. If using the column name, be aware that the name
|
||
|
* should match the case of the column, as returned by the driver.
|
||
|
* @param mixed $value Name of the PHP variable to which the column will be bound.
|
||
|
* @param integer $dataType Data type of the parameter
|
||
|
* @see http://www.php.net/manual/en/function.PDOStatement-bindColumn.php
|
||
|
*/
|
||
|
public function bindColumn($column, &$value, $dataType = null)
|
||
|
{
|
||
14 years ago
|
if ($dataType === null) {
|
||
14 years ago
|
$this->_statement->bindColumn($column, $value);
|
||
13 years ago
|
} else {
|
||
14 years ago
|
$this->_statement->bindColumn($column, $value, $dataType);
|
||
14 years ago
|
}
|
||
14 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Set the default fetch mode for this statement
|
||
|
* @param mixed $mode fetch mode
|
||
|
* @see http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php
|
||
|
*/
|
||
|
public function setFetchMode($mode)
|
||
|
{
|
||
|
$params = func_get_args();
|
||
|
call_user_func_array(array($this->_statement, 'setFetchMode'), $params);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Advances the reader to the next row in a result set.
|
||
12 years ago
|
* @return array the current row, false if no more row available
|
||
14 years ago
|
*/
|
||
|
public function read()
|
||
|
{
|
||
|
return $this->_statement->fetch();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns a single column from the next row of a result set.
|
||
|
* @param integer $columnIndex zero-based column index
|
||
12 years ago
|
* @return mixed the column of the current row, false if no more row available
|
||
14 years ago
|
*/
|
||
|
public function readColumn($columnIndex)
|
||
|
{
|
||
|
return $this->_statement->fetchColumn($columnIndex);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns an object populated with the next row of data.
|
||
|
* @param string $className class name of the object to be created and populated
|
||
|
* @param array $fields Elements of this array are passed to the constructor
|
||
12 years ago
|
* @return mixed the populated object, false if no more row of data available
|
||
14 years ago
|
*/
|
||
|
public function readObject($className, $fields)
|
||
|
{
|
||
|
return $this->_statement->fetchObject($className, $fields);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Reads the whole result set into an array.
|
||
|
* @return array the result set (each array element represents a row of data).
|
||
|
* An empty array will be returned if the result contains no row.
|
||
|
*/
|
||
|
public function readAll()
|
||
|
{
|
||
|
return $this->_statement->fetchAll();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Advances the reader to the next result when reading the results of a batch of statements.
|
||
|
* This method is only useful when there are multiple result sets
|
||
|
* returned by the query. Not all DBMS support this feature.
|
||
|
* @return boolean Returns true on success or false on failure.
|
||
|
*/
|
||
|
public function nextResult()
|
||
|
{
|
||
14 years ago
|
if (($result = $this->_statement->nextRowset()) !== false) {
|
||
14 years ago
|
$this->_index = -1;
|
||
14 years ago
|
}
|
||
14 years ago
|
return $result;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Closes the reader.
|
||
|
* This frees up the resources allocated for executing this SQL statement.
|
||
12 years ago
|
* Read attempts after this method call are unpredictable.
|
||
14 years ago
|
*/
|
||
|
public function close()
|
||
|
{
|
||
|
$this->_statement->closeCursor();
|
||
|
$this->_closed = true;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* whether the reader is closed or not.
|
||
|
* @return boolean whether the reader is closed or not.
|
||
|
*/
|
||
|
public function getIsClosed()
|
||
|
{
|
||
|
return $this->_closed;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the number of rows in the result set.
|
||
|
* Note, most DBMS may not give a meaningful count.
|
||
|
* In this case, use "SELECT COUNT(*) FROM tableName" to obtain the number of rows.
|
||
|
* @return integer number of rows contained in the result.
|
||
|
*/
|
||
|
public function getRowCount()
|
||
|
{
|
||
|
return $this->_statement->rowCount();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the number of rows in the result set.
|
||
|
* This method is required by the Countable interface.
|
||
|
* Note, most DBMS may not give a meaningful count.
|
||
|
* In this case, use "SELECT COUNT(*) FROM tableName" to obtain the number of rows.
|
||
|
* @return integer number of rows contained in the result.
|
||
|
*/
|
||
|
public function count()
|
||
|
{
|
||
|
return $this->getRowCount();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the number of columns in the result set.
|
||
|
* Note, even there's no row in the reader, this still gives correct column number.
|
||
|
* @return integer the number of columns in the result set.
|
||
|
*/
|
||
|
public function getColumnCount()
|
||
|
{
|
||
|
return $this->_statement->columnCount();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Resets the iterator to the initial state.
|
||
|
* This method is required by the interface Iterator.
|
||
12 years ago
|
* @throws InvalidCallException if this method is invoked twice
|
||
14 years ago
|
*/
|
||
|
public function rewind()
|
||
|
{
|
||
14 years ago
|
if ($this->_index < 0) {
|
||
14 years ago
|
$this->_row = $this->_statement->fetch();
|
||
|
$this->_index = 0;
|
||
13 years ago
|
} else {
|
||
12 years ago
|
throw new InvalidCallException('DataReader cannot rewind. It is a forward-only reader.');
|
||
14 years ago
|
}
|
||
14 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the index of the current row.
|
||
|
* This method is required by the interface Iterator.
|
||
|
* @return integer the index of the current row.
|
||
|
*/
|
||
|
public function key()
|
||
|
{
|
||
|
return $this->_index;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns the current row.
|
||
|
* This method is required by the interface Iterator.
|
||
|
* @return mixed the current row.
|
||
|
*/
|
||
|
public function current()
|
||
|
{
|
||
|
return $this->_row;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Moves the internal pointer to the next row.
|
||
|
* This method is required by the interface Iterator.
|
||
|
*/
|
||
|
public function next()
|
||
|
{
|
||
|
$this->_row = $this->_statement->fetch();
|
||
|
$this->_index++;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Returns whether there is a row of data at current position.
|
||
|
* This method is required by the interface Iterator.
|
||
|
* @return boolean whether there is a row of data at current position.
|
||
|
*/
|
||
|
public function valid()
|
||
|
{
|
||
|
return $this->_row !== false;
|
||
|
}
|
||
|
}
|