]> git.sur5r.net Git - bacula/bacula/blob - gui/baculum/framework/Data/TDbDataReader.php
6c4b3141d0b6e8a8d280b6aec27b1d74c52e6e1e
[bacula/bacula] / gui / baculum / framework / Data / TDbDataReader.php
1 <?php
2 /**
3  * TDbDataReader class file
4  *
5  * @author Qiang Xue <qiang.xue@gmail.com>
6  * @link https://github.com/pradosoft/prado
7  * @copyright Copyright &copy; 2005-2016 The PRADO Group
8  * @license https://github.com/pradosoft/prado/blob/master/COPYRIGHT
9  * @package System.Data
10  */
11
12 /**
13  * TDbDataReader class.
14  *
15  * TDbDataReader represents a forward-only stream of rows from a query result set.
16  *
17  * To read the current row of data, call {@link read}. The method {@link readAll}
18  * returns all the rows in a single array.
19  *
20  * One can also retrieve the rows of data in TDbDataReader by using foreach:
21  * <code>
22  * foreach($reader as $row)
23  *     // $row represents a row of data
24  * </code>
25  * Since TDbDataReader is a forward-only stream, you can only traverse it once.
26  *
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}
29  * for more details.
30  *
31  * @author Qiang Xue <qiang.xue@gmail.com>
32  * @package System.Data
33  * @since 3.0
34  */
35 class TDbDataReader extends TComponent implements Iterator
36 {
37         private $_statement;
38         private $_closed=false;
39         private $_row;
40         private $_index=-1;
41
42         /**
43          * Constructor.
44          * @param TDbCommand the command generating the query result
45          */
46         public function __construct(TDbCommand $command)
47         {
48                 $this->_statement=$command->getPdoStatement();
49                 $this->_statement->setFetchMode(PDO::FETCH_ASSOC);
50         }
51
52         /**
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
62          */
63         public function bindColumn($column, &$value, $dataType=null)
64         {
65                 if($dataType===null)
66                         $this->_statement->bindColumn($column,$value);
67                 else
68                         $this->_statement->bindColumn($column,$value,$dataType);
69         }
70
71         /**
72          * @see http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php
73          */
74         public function setFetchMode($mode)
75         {
76                 $params=func_get_args();
77                 call_user_func_array(array($this->_statement,'setFetchMode'),$params);
78         }
79
80         /**
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
83          */
84         public function read()
85         {
86                 return $this->_statement->fetch();
87         }
88
89         /**
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
93          */
94         public function readColumn($columnIndex)
95         {
96                 return $this->_statement->fetchColumn($columnIndex);
97         }
98
99         /**
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
104          */
105         public function readObject($className,$fields)
106         {
107                 return $this->_statement->fetchObject($className,$fields);
108         }
109
110         /**
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.
114          */
115         public function readAll()
116         {
117                 return $this->_statement->fetchAll();
118         }
119
120         /**
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.
124          */
125         public function nextResult()
126         {
127                 return $this->_statement->nextRowset();
128         }
129
130         /**
131          * Closes the reader.
132          * Any further data reading will result in an exception.
133          */
134         public function close()
135         {
136                 $this->_statement->closeCursor();
137                 $this->_closed=true;
138         }
139
140         /**
141          * @return boolean whether the reader is closed or not.
142          */
143         public function getIsClosed()
144         {
145                 return $this->_closed;
146         }
147
148         /**
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.
152          */
153         public function getRowCount()
154         {
155                 return $this->_statement->rowCount();
156         }
157
158         /**
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.
161          */
162         public function getColumnCount()
163         {
164                 return $this->_statement->columnCount();
165         }
166
167         /**
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
171          */
172         public function rewind()
173         {
174                 if($this->_index<0)
175                 {
176                         $this->_row=$this->_statement->fetch();
177                         $this->_index=0;
178                 }
179                 else
180                         throw new TDbException('dbdatareader_rewind_invalid');
181         }
182
183         /**
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.
187          */
188         public function key()
189         {
190                 return $this->_index;
191         }
192
193         /**
194          * Returns the current row.
195          * This method is required by the interface Iterator.
196          * @return mixed the current row.
197          */
198         public function current()
199         {
200                 return $this->_row;
201         }
202
203         /**
204          * Moves the internal pointer to the next row.
205          * This method is required by the interface Iterator.
206          */
207         public function next()
208         {
209                 $this->_row=$this->_statement->fetch();
210                 $this->_index++;
211         }
212
213         /**
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.
217          */
218         public function valid()
219         {
220                 return $this->_row!==false;
221         }
222 }
223