3 * TDbDataReader class file
5 * @author Qiang Xue <qiang.xue@gmail.com>
6 * @link https://github.com/pradosoft/prado
7 * @copyright Copyright © 2005-2016 The PRADO Group
8 * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
13 * TDbDataReader class.
15 * TDbDataReader represents a forward-only stream of rows from a query result set.
17 * To read the current row of data, call {@link read}. The method {@link readAll}
18 * returns all the rows in a single array.
20 * One can also retrieve the rows of data in TDbDataReader by using foreach:
22 * foreach($reader as $row)
23 * // $row represents a row of data
25 * Since TDbDataReader is a forward-only stream, you can only traverse it once.
27 * It is possible to use a specific mode of data fetching by setting
28 * {@link setFetchMode FetchMode}. See {@link http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php}
31 * @author Qiang Xue <qiang.xue@gmail.com>
32 * @package System.Data
35 class TDbDataReader extends TComponent implements Iterator
38 private $_closed=false;
44 * @param TDbCommand the command generating the query result
46 public function __construct(TDbCommand $command)
48 $this->_statement=$command->getPdoStatement();
49 $this->_statement->setFetchMode(PDO::FETCH_ASSOC);
53 * Binds a column to a PHP variable.
54 * When rows of data are being fetched, the corresponding column value
55 * will be set in the variable. Note, the fetch mode must include PDO::FETCH_BOUND.
56 * @param mixed Number of the column (1-indexed) or name of the column
57 * in the result set. If using the column name, be aware that the name
58 * should match the case of the column, as returned by the driver.
59 * @param mixed Name of the PHP variable to which the column will be bound.
60 * @param int Data type of the parameter
61 * @see http://www.php.net/manual/en/function.PDOStatement-bindColumn.php
63 public function bindColumn($column, &$value, $dataType=null)
66 $this->_statement->bindColumn($column,$value);
68 $this->_statement->bindColumn($column,$value,$dataType);
72 * @see http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php
74 public function setFetchMode($mode)
76 $params=func_get_args();
77 call_user_func_array(array($this->_statement,'setFetchMode'),$params);
81 * Advances the reader to the next row in a result set.
82 * @return array|false the current row, false if no more row available
84 public function read()
86 return $this->_statement->fetch();
90 * Returns a single column from the next row of a result set.
91 * @param int zero-based column index
92 * @return mixed|false the column of the current row, false if no more row available
94 public function readColumn($columnIndex)
96 return $this->_statement->fetchColumn($columnIndex);
100 * Returns a single column from the next row of a result set.
101 * @param string class name of the object to be created and populated
102 * @param array list of column names whose values are to be passed as parameters in the constructor of the class being created
103 * @return mixed|false the populated object, false if no more row of data available
105 public function readObject($className,$fields)
107 return $this->_statement->fetchObject($className,$fields);
111 * Reads the whole result set into an array.
112 * @return array the result set (each array element represents a row of data).
113 * An empty array will be returned if the result contains no row.
115 public function readAll()
117 return $this->_statement->fetchAll();
121 * Advances the reader to the next result when reading the results of a batch of statements.
122 * This method is only useful when there are multiple result sets
123 * returned by the query. Not all DBMS support this feature.
125 public function nextResult()
127 return $this->_statement->nextRowset();
132 * Any further data reading will result in an exception.
134 public function close()
136 $this->_statement->closeCursor();
141 * @return boolean whether the reader is closed or not.
143 public function getIsClosed()
145 return $this->_closed;
149 * @return int number of rows contained in the result.
150 * Note, most DBMS may not give a meaningful count.
151 * In this case, use "SELECT COUNT(*) FROM tableName" to obtain the number of rows.
153 public function getRowCount()
155 return $this->_statement->rowCount();
159 * @return int the number of columns in the result set.
160 * Note, even there's no row in the reader, this still gives correct column number.
162 public function getColumnCount()
164 return $this->_statement->columnCount();
168 * Resets the iterator to the initial state.
169 * This method is required by the interface Iterator.
170 * @throws TDbException if this method is invoked twice
172 public function rewind()
176 $this->_row=$this->_statement->fetch();
180 throw new TDbException('dbdatareader_rewind_invalid');
184 * Returns the index of the current row.
185 * This method is required by the interface Iterator.
186 * @return integer the index of the current row.
188 public function key()
190 return $this->_index;
194 * Returns the current row.
195 * This method is required by the interface Iterator.
196 * @return mixed the current row.
198 public function current()
204 * Moves the internal pointer to the next row.
205 * This method is required by the interface Iterator.
207 public function next()
209 $this->_row=$this->_statement->fetch();
214 * Returns whether there is a row of data at current position.
215 * This method is required by the interface Iterator.
216 * @return boolean whether there is a row of data at current position.
218 public function valid()
220 return $this->_row!==false;