Overview

Packages

  • application
    • commands
    • components
      • actions
      • filters
      • leftWidget
      • permissions
      • sortableWidget
      • util
      • webupdater
      • x2flow
        • actions
        • triggers
      • X2GridView
      • X2Settings
    • controllers
    • models
      • embedded
    • modules
      • accounts
        • controllers
        • models
      • actions
        • controllers
        • models
      • calendar
        • controllers
        • models
      • charts
        • models
      • contacts
        • controllers
        • models
      • docs
        • components
        • controllers
        • models
      • groups
        • controllers
        • models
      • marketing
        • components
        • controllers
        • models
      • media
        • controllers
        • models
      • mobile
        • components
      • opportunities
        • controllers
        • models
      • products
        • controllers
        • models
      • quotes
        • controllers
        • models
      • services
        • controllers
        • models
      • template
        • models
      • users
        • controllers
        • models
      • workflow
        • controllers
        • models
      • x2Leads
        • controllers
        • models
  • Net
  • None
  • PHP
  • system
    • base
    • caching
      • dependencies
    • collections
    • console
    • db
      • ar
      • schema
        • cubrid
        • mssql
        • mysql
        • oci
        • pgsql
        • sqlite
    • i18n
      • gettext
    • logging
    • test
    • utils
    • validators
    • web
      • actions
      • auth
      • filters
      • form
      • helpers
      • renderers
      • services
      • widgets
        • captcha
        • pagers
  • Text
    • Highlighter
  • zii
    • behaviors
    • widgets
      • grid
      • jui

Classes

  • CDbCommand
  • CDbConnection
  • CDbDataReader
  • CDbMigration
  • CDbTransaction

Exceptions

  • CDbException
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * CDbDataReader class file
  4:  *
  5:  * @author Qiang Xue <qiang.xue@gmail.com>
  6:  * @link http://www.yiiframework.com/
  7:  * @copyright 2008-2013 Yii Software LLC
  8:  * @license http://www.yiiframework.com/license/
  9:  */
 10: 
 11: /**
 12:  * CDbDataReader represents a forward-only stream of rows from a query result set.
 13:  *
 14:  * To read the current row of data, call {@link read}. The method {@link readAll}
 15:  * returns all the rows in a single array.
 16:  *
 17:  * One can also retrieve the rows of data in CDbDataReader by using foreach:
 18:  * <pre>
 19:  * foreach($reader as $row)
 20:  *     // $row represents a row of data
 21:  * </pre>
 22:  * Since CDbDataReader is a forward-only stream, you can only traverse it once.
 23:  *
 24:  * It is possible to use a specific mode of data fetching by setting
 25:  * {@link setFetchMode FetchMode}. See {@link http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php}
 26:  * for more details.
 27:  *
 28:  * @property boolean $isClosed Whether the reader is closed or not.
 29:  * @property integer $rowCount Number of rows contained in the result.
 30:  * @property integer $columnCount The number of columns in the result set.
 31:  * @property mixed $fetchMode Fetch mode.
 32:  *
 33:  * @author Qiang Xue <qiang.xue@gmail.com>
 34:  * @package system.db
 35:  * @since 1.0
 36:  */
 37: class CDbDataReader extends CComponent implements Iterator, Countable
 38: {
 39:     private $_statement;
 40:     private $_closed=false;
 41:     private $_row;
 42:     private $_index=-1;
 43: 
 44:     /**
 45:      * Constructor.
 46:      * @param CDbCommand $command the command generating the query result
 47:      */
 48:     public function __construct(CDbCommand $command)
 49:     {
 50:         $this->_statement=$command->getPdoStatement();
 51:         $this->_statement->setFetchMode(PDO::FETCH_ASSOC);
 52:     }
 53: 
 54:     /**
 55:      * Binds a column to a PHP variable.
 56:      * When rows of data are being fetched, the corresponding column value
 57:      * will be set in the variable. Note, the fetch mode must include PDO::FETCH_BOUND.
 58:      * @param mixed $column Number of the column (1-indexed) or name of the column
 59:      * in the result set. If using the column name, be aware that the name
 60:      * should match the case of the column, as returned by the driver.
 61:      * @param mixed $value Name of the PHP variable to which the column will be bound.
 62:      * @param integer $dataType Data type of the parameter
 63:      * @see http://www.php.net/manual/en/function.PDOStatement-bindColumn.php
 64:      */
 65:     public function bindColumn($column, &$value, $dataType=null)
 66:     {
 67:         if($dataType===null)
 68:             $this->_statement->bindColumn($column,$value);
 69:         else
 70:             $this->_statement->bindColumn($column,$value,$dataType);
 71:     }
 72: 
 73:     /**
 74:      * Set the default fetch mode for this statement
 75:      * @param mixed $mode fetch mode
 76:      * @see http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php
 77:      */
 78:     public function setFetchMode($mode)
 79:     {
 80:         $params=func_get_args();
 81:         call_user_func_array(array($this->_statement,'setFetchMode'),$params);
 82:     }
 83: 
 84:     /**
 85:      * Advances the reader to the next row in a result set.
 86:      * @return array|false the current row, false if no more row available
 87:      */
 88:     public function read()
 89:     {
 90:         return $this->_statement->fetch();
 91:     }
 92: 
 93:     /**
 94:      * Returns a single column from the next row of a result set.
 95:      * @param integer $columnIndex zero-based column index
 96:      * @return mixed|false the column of the current row, false if no more row available
 97:      */
 98:     public function readColumn($columnIndex)
 99:     {
100:         return $this->_statement->fetchColumn($columnIndex);
101:     }
102: 
103:     /**
104:      * Returns an object populated with the next row of data.
105:      * @param string $className class name of the object to be created and populated
106:      * @param array $fields Elements of this array are passed to the constructor
107:      * @return mixed|false the populated object, false if no more row of data available
108:      */
109:     public function readObject($className,$fields)
110:     {
111:         return $this->_statement->fetchObject($className,$fields);
112:     }
113: 
114:     /**
115:      * Reads the whole result set into an array.
116:      * @return array the result set (each array element represents a row of data).
117:      * An empty array will be returned if the result contains no row.
118:      */
119:     public function readAll()
120:     {
121:         return $this->_statement->fetchAll();
122:     }
123: 
124:     /**
125:      * Advances the reader to the next result when reading the results of a batch of statements.
126:      * This method is only useful when there are multiple result sets
127:      * returned by the query. Not all DBMS support this feature.
128:      * @return boolean Returns true on success or false on failure.
129:      */
130:     public function nextResult()
131:     {
132:         if(($result=$this->_statement->nextRowset())!==false)
133:             $this->_index=-1;
134:         return $result;
135:     }
136: 
137:     /**
138:      * Closes the reader.
139:      * This frees up the resources allocated for executing this SQL statement.
140:      * Read attempts after this method call are unpredictable.
141:      */
142:     public function close()
143:     {
144:         $this->_statement->closeCursor();
145:         $this->_closed=true;
146:     }
147: 
148:     /**
149:      * whether the reader is closed or not.
150:      * @return boolean whether the reader is closed or not.
151:      */
152:     public function getIsClosed()
153:     {
154:         return $this->_closed;
155:     }
156: 
157:     /**
158:      * Returns the number of rows in the result set.
159:      * Note, most DBMS may not give a meaningful count.
160:      * In this case, use "SELECT COUNT(*) FROM tableName" to obtain the number of rows.
161:      * @return integer number of rows contained in the result.
162:      */
163:     public function getRowCount()
164:     {
165:         return $this->_statement->rowCount();
166:     }
167: 
168:     /**
169:      * Returns the number of rows in the result set.
170:      * This method is required by the Countable interface.
171:      * Note, most DBMS may not give a meaningful count.
172:      * In this case, use "SELECT COUNT(*) FROM tableName" to obtain the number of rows.
173:      * @return integer number of rows contained in the result.
174:      */
175:     public function count()
176:     {
177:         return $this->getRowCount();
178:     }
179: 
180:     /**
181:      * Returns the number of columns in the result set.
182:      * Note, even there's no row in the reader, this still gives correct column number.
183:      * @return integer the number of columns in the result set.
184:      */
185:     public function getColumnCount()
186:     {
187:         return $this->_statement->columnCount();
188:     }
189: 
190:     /**
191:      * Resets the iterator to the initial state.
192:      * This method is required by the interface Iterator.
193:      * @throws CException if this method is invoked twice
194:      */
195:     public function rewind()
196:     {
197:         if($this->_index<0)
198:         {
199:             $this->_row=$this->_statement->fetch();
200:             $this->_index=0;
201:         }
202:         else
203:             throw new CDbException(Yii::t('yii','CDbDataReader cannot rewind. It is a forward-only reader.'));
204:     }
205: 
206:     /**
207:      * Returns the index of the current row.
208:      * This method is required by the interface Iterator.
209:      * @return integer the index of the current row.
210:      */
211:     public function key()
212:     {
213:         return $this->_index;
214:     }
215: 
216:     /**
217:      * Returns the current row.
218:      * This method is required by the interface Iterator.
219:      * @return mixed the current row.
220:      */
221:     public function current()
222:     {
223:         return $this->_row;
224:     }
225: 
226:     /**
227:      * Moves the internal pointer to the next row.
228:      * This method is required by the interface Iterator.
229:      */
230:     public function next()
231:     {
232:         $this->_row=$this->_statement->fetch();
233:         $this->_index++;
234:     }
235: 
236:     /**
237:      * Returns whether there is a row of data at current position.
238:      * This method is required by the interface Iterator.
239:      * @return boolean whether there is a row of data at current position.
240:      */
241:     public function valid()
242:     {
243:         return $this->_row!==false;
244:     }
245: }
246: 
API documentation generated by ApiGen 2.8.0