Subversion-Projekte lars-tiefland.prado

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/**
3
 * TDbDataReader class file
4
 *
5
 * @author Qiang Xue <qiang.xue@gmail.com>
6
 * @link http://www.pradosoft.com/
7
 * @copyright Copyright &copy; 2005-2008 PradoSoft
8
 * @license http://www.pradosoft.com/license/
9
 * @version $Id: TDbDataReader.php 2541 2008-10-21 15:05:13Z qiang.xue $
10
 * @package System.Data
11
 */
12
 
13
/**
14
 * TDbDataReader class.
15
 *
16
 * TDbDataReader represents a forward-only stream of rows from a query result set.
17
 *
18
 * To read the current row of data, call {@link read}. The method {@link readAll}
19
 * returns all the rows in a single array.
20
 *
21
 * One can also retrieve the rows of data in TDbDataReader by using foreach:
22
 * <code>
23
 * foreach($reader as $row)
24
 *     // $row represents a row of data
25
 * </code>
26
 * Since TDbDataReader is a forward-only stream, you can only traverse it once.
27
 *
28
 * It is possible to use a specific mode of data fetching by setting
29
 * {@link setFetchMode FetchMode}. See {@link http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php}
30
 * for more details.
31
 *
32
 * @author Qiang Xue <qiang.xue@gmail.com>
33
 * @version $Id: TDbDataReader.php 2541 2008-10-21 15:05:13Z qiang.xue $
34
 * @package System.Data
35
 * @since 3.0
36
 */
37
class TDbDataReader extends TComponent implements Iterator
38
{
39
	private $_statement;
40
	private $_closed=false;
41
	private $_row;
42
	private $_index=-1;
43
 
44
	/**
45
	 * Constructor.
46
	 * @param TDbCommand the command generating the query result
47
	 */
48
	public function __construct(TDbCommand $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 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 Name of the PHP variable to which the column will be bound.
62
	 * @param int 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
	 * @see http://www.php.net/manual/en/function.PDOStatement-setFetchMode.php
75
	 */
76
	public function setFetchMode($mode)
77
	{
78
		$params=func_get_args();
79
		call_user_func_array(array($this->_statement,'setFetchMode'),$params);
80
	}
81
 
82
	/**
83
	 * Advances the reader to the next row in a result set.
84
	 * @return array|false the current row, false if no more row available
85
	 */
86
	public function read()
87
	{
88
		return $this->_statement->fetch();
89
	}
90
 
91
	/**
92
	 * Returns a single column from the next row of a result set.
93
	 * @param int zero-based column index
94
	 * @return mixed|false the column of the current row, false if no more row available
95
	 */
96
	public function readColumn($columnIndex)
97
	{
98
		return $this->_statement->fetchColumn($columnIndex);
99
	}
100
 
101
	/**
102
	 * Returns a single column from the next row of a result set.
103
	 * @param string class name of the object to be created and populated
104
	 * @param array list of column names whose values are to be passed as parameters in the constructor of the class being created
105
	 * @return mixed|false the populated object, false if no more row of data available
106
	 */
107
	public function readObject($className,$fields)
108
	{
109
		return $this->_statement->fetchObject($className,$fields);
110
	}
111
 
112
	/**
113
	 * Reads the whole result set into an array.
114
	 * @return array the result set (each array element represents a row of data).
115
	 * An empty array will be returned if the result contains no row.
116
	 */
117
	public function readAll()
118
	{
119
		return $this->_statement->fetchAll();
120
	}
121
 
122
	/**
123
	 * Advances the reader to the next result when reading the results of a batch of statements.
124
	 * This method is only useful when there are multiple result sets
125
	 * returned by the query. Not all DBMS support this feature.
126
	 */
127
	public function nextResult()
128
	{
129
		return $this->_statement->nextRowset();
130
	}
131
 
132
	/**
133
	 * Closes the reader.
134
	 * Any further data reading will result in an exception.
135
	 */
136
	public function close()
137
	{
138
		$this->_statement->closeCursor();
139
		$this->_closed=true;
140
	}
141
 
142
	/**
143
	 * @return boolean whether the reader is closed or not.
144
	 */
145
	public function getIsClosed()
146
	{
147
		return $this->_closed;
148
	}
149
 
150
	/**
151
	 * @return int number of rows contained in the result.
152
	 * Note, most DBMS may not give a meaningful count.
153
	 * In this case, use "SELECT COUNT(*) FROM tableName" to obtain the number of rows.
154
	 */
155
	public function getRowCount()
156
	{
157
		return $this->_statement->rowCount();
158
	}
159
 
160
	/**
161
	 * @return int the number of columns in the result set.
162
	 * Note, even there's no row in the reader, this still gives correct column number.
163
	 */
164
	public function getColumnCount()
165
	{
166
		return $this->_statement->columnCount();
167
	}
168
 
169
	/**
170
	 * Resets the iterator to the initial state.
171
	 * This method is required by the interface Iterator.
172
	 * @throws TDbException if this method is invoked twice
173
	 */
174
	public function rewind()
175
	{
176
		if($this->_index<0)
177
		{
178
			$this->_row=$this->_statement->fetch();
179
			$this->_index=0;
180
		}
181
		else
182
			throw new TDbException('dbdatareader_rewind_invalid');
183
	}
184
 
185
	/**
186
	 * Returns the index of the current row.
187
	 * This method is required by the interface Iterator.
188
	 * @return integer the index of the current row.
189
	 */
190
	public function key()
191
	{
192
		return $this->_index;
193
	}
194
 
195
	/**
196
	 * Returns the current row.
197
	 * This method is required by the interface Iterator.
198
	 * @return mixed the current row.
199
	 */
200
	public function current()
201
	{
202
		return $this->_row;
203
	}
204
 
205
	/**
206
	 * Moves the internal pointer to the next row.
207
	 * This method is required by the interface Iterator.
208
	 */
209
	public function next()
210
	{
211
		$this->_row=$this->_statement->fetch();
212
		$this->_index++;
213
	}
214
 
215
	/**
216
	 * Returns whether there is a row of data at current position.
217
	 * This method is required by the interface Iterator.
218
	 * @return boolean whether there is a row of data at current position.
219
	 */
220
	public function valid()
221
	{
222
		return $this->_row!==false;
223
	}
224
}
225