|
1 <?php |
|
2 /** |
|
3 * Zend Framework |
|
4 * |
|
5 * LICENSE |
|
6 * |
|
7 * This source file is subject to the new BSD license that is bundled |
|
8 * with this package in the file LICENSE.txt. |
|
9 * It is also available through the world-wide-web at this URL: |
|
10 * http://framework.zend.com/license/new-bsd |
|
11 * If you did not receive a copy of the license and are unable to |
|
12 * obtain it through the world-wide-web, please send an email |
|
13 * to license@zend.com so we can send you a copy immediately. |
|
14 * |
|
15 * @category Zend |
|
16 * @package Zend_Test |
|
17 * @subpackage PHPUnit |
|
18 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
19 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
20 * @version $Id: DbStatement.php 20096 2010-01-06 02:05:09Z bkarwin $ |
|
21 */ |
|
22 |
|
23 /** |
|
24 * @see Zend_Db_Statement_Interface |
|
25 */ |
|
26 require_once "Zend/Db/Statement/Interface.php"; |
|
27 |
|
28 /** |
|
29 * Testing Database Statement that acts as a stack to SQL resultsets. |
|
30 * |
|
31 * @category Zend |
|
32 * @package Zend_Test |
|
33 * @subpackage PHPUnit |
|
34 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com) |
|
35 * @license http://framework.zend.com/license/new-bsd New BSD License |
|
36 */ |
|
37 class Zend_Test_DbStatement implements Zend_Db_Statement_Interface |
|
38 { |
|
39 /** |
|
40 * @var array |
|
41 */ |
|
42 protected $_fetchStack = array(); |
|
43 |
|
44 /** |
|
45 * @var int |
|
46 */ |
|
47 protected $_columnCount = 0; |
|
48 |
|
49 /** |
|
50 * @var int |
|
51 */ |
|
52 protected $_rowCount = 0; |
|
53 |
|
54 /** |
|
55 * @var Zend_Db_Profiler_Query |
|
56 */ |
|
57 protected $_queryProfile = null; |
|
58 |
|
59 /** |
|
60 * Create a Select statement which returns the given array of rows. |
|
61 * |
|
62 * @param array $rows |
|
63 * @return Zend_Test_DbStatement |
|
64 */ |
|
65 static public function createSelectStatement(array $rows=array()) |
|
66 { |
|
67 $stmt = new Zend_Test_DbStatement(); |
|
68 foreach($rows AS $row) { |
|
69 $stmt->append($row); |
|
70 } |
|
71 return $stmt; |
|
72 } |
|
73 |
|
74 /** |
|
75 * Create an Insert Statement |
|
76 * |
|
77 * @param int $affectedRows |
|
78 * @return Zend_Test_DbStatement |
|
79 */ |
|
80 static public function createInsertStatement($affectedRows=0) |
|
81 { |
|
82 return self::_createRowCountStatement($affectedRows); |
|
83 } |
|
84 |
|
85 /** |
|
86 * Create an Delete Statement |
|
87 * |
|
88 * @param int $affectedRows |
|
89 * @return Zend_Test_DbStatement |
|
90 */ |
|
91 static public function createDeleteStatement($affectedRows=0) |
|
92 { |
|
93 return self::_createRowCountStatement($affectedRows); |
|
94 } |
|
95 |
|
96 /** |
|
97 * Create an Update Statement |
|
98 * |
|
99 * @param int $affectedRows |
|
100 * @return Zend_Test_DbStatement |
|
101 */ |
|
102 static public function createUpdateStatement($affectedRows=0) |
|
103 { |
|
104 return self::_createRowCountStatement($affectedRows); |
|
105 } |
|
106 |
|
107 /** |
|
108 * Create a Row Count Statement |
|
109 * |
|
110 * @param int $affectedRows |
|
111 * @return Zend_Test_DbStatement |
|
112 */ |
|
113 static protected function _createRowCountStatement($affectedRows) |
|
114 { |
|
115 $stmt = new Zend_Test_DbStatement(); |
|
116 $stmt->setRowCount($affectedRows); |
|
117 return $stmt; |
|
118 } |
|
119 |
|
120 /** |
|
121 * @param Zend_Db_Profiler_Query $qp |
|
122 */ |
|
123 public function setQueryProfile(Zend_Db_Profiler_Query $qp) |
|
124 { |
|
125 $this->_queryProfile = $qp; |
|
126 } |
|
127 |
|
128 /** |
|
129 * @param int $rowCount |
|
130 */ |
|
131 public function setRowCount($rowCount) |
|
132 { |
|
133 $this->_rowCount = $rowCount; |
|
134 } |
|
135 |
|
136 /** |
|
137 * Append a new row to the fetch stack. |
|
138 * |
|
139 * @param array $row |
|
140 */ |
|
141 public function append($row) |
|
142 { |
|
143 $this->_columnCount = count($row); |
|
144 $this->_fetchStack[] = $row; |
|
145 } |
|
146 |
|
147 /** |
|
148 * Bind a column of the statement result set to a PHP variable. |
|
149 * |
|
150 * @param string $column Name the column in the result set, either by |
|
151 * position or by name. |
|
152 * @param mixed $param Reference to the PHP variable containing the value. |
|
153 * @param mixed $type OPTIONAL |
|
154 * @return bool |
|
155 * @throws Zend_Db_Statement_Exception |
|
156 */ |
|
157 public function bindColumn($column, &$param, $type = null) |
|
158 { |
|
159 return true; |
|
160 } |
|
161 |
|
162 /** |
|
163 * Binds a parameter to the specified variable name. |
|
164 * |
|
165 * @param mixed $parameter Name the parameter, either integer or string. |
|
166 * @param mixed $variable Reference to PHP variable containing the value. |
|
167 * @param mixed $type OPTIONAL Datatype of SQL parameter. |
|
168 * @param mixed $length OPTIONAL Length of SQL parameter. |
|
169 * @param mixed $options OPTIONAL Other options. |
|
170 * @return bool |
|
171 * @throws Zend_Db_Statement_Exception |
|
172 */ |
|
173 public function bindParam($parameter, &$variable, $type = null, $length = null, $options = null) |
|
174 { |
|
175 if($this->_queryProfile !== null) { |
|
176 $this->_queryProfile->bindParam($parameter, $variable); |
|
177 } |
|
178 return true; |
|
179 } |
|
180 |
|
181 /** |
|
182 * Binds a value to a parameter. |
|
183 * |
|
184 * @param mixed $parameter Name the parameter, either integer or string. |
|
185 * @param mixed $value Scalar value to bind to the parameter. |
|
186 * @param mixed $type OPTIONAL Datatype of the parameter. |
|
187 * @return bool |
|
188 * @throws Zend_Db_Statement_Exception |
|
189 */ |
|
190 public function bindValue($parameter, $value, $type = null) |
|
191 { |
|
192 return true; |
|
193 } |
|
194 |
|
195 /** |
|
196 * Closes the cursor, allowing the statement to be executed again. |
|
197 * |
|
198 * @return bool |
|
199 * @throws Zend_Db_Statement_Exception |
|
200 */ |
|
201 public function closeCursor() |
|
202 { |
|
203 return true; |
|
204 } |
|
205 |
|
206 /** |
|
207 * Returns the number of columns in the result set. |
|
208 * Returns null if the statement has no result set metadata. |
|
209 * |
|
210 * @return int The number of columns. |
|
211 * @throws Zend_Db_Statement_Exception |
|
212 */ |
|
213 public function columnCount() |
|
214 { |
|
215 return $this->_columnCount; |
|
216 } |
|
217 |
|
218 /** |
|
219 * Retrieves the error code, if any, associated with the last operation on |
|
220 * the statement handle. |
|
221 * |
|
222 * @return string error code. |
|
223 * @throws Zend_Db_Statement_Exception |
|
224 */ |
|
225 public function errorCode() |
|
226 { |
|
227 return false; |
|
228 } |
|
229 |
|
230 /** |
|
231 * Retrieves an array of error information, if any, associated with the |
|
232 * last operation on the statement handle. |
|
233 * |
|
234 * @return array |
|
235 * @throws Zend_Db_Statement_Exception |
|
236 */ |
|
237 public function errorInfo() |
|
238 { |
|
239 return false; |
|
240 } |
|
241 |
|
242 /** |
|
243 * Executes a prepared statement. |
|
244 * |
|
245 * @param array $params OPTIONAL Values to bind to parameter placeholders. |
|
246 * @return bool |
|
247 * @throws Zend_Db_Statement_Exception |
|
248 */ |
|
249 public function execute(array $params = array()) |
|
250 { |
|
251 if($this->_queryProfile !== null) { |
|
252 $this->_queryProfile->bindParams($params); |
|
253 $this->_queryProfile->end(); |
|
254 } |
|
255 return true; |
|
256 } |
|
257 |
|
258 /** |
|
259 * Fetches a row from the result set. |
|
260 * |
|
261 * @param int $style OPTIONAL Fetch mode for this fetch operation. |
|
262 * @param int $cursor OPTIONAL Absolute, relative, or other. |
|
263 * @param int $offset OPTIONAL Number for absolute or relative cursors. |
|
264 * @return mixed Array, object, or scalar depending on fetch mode. |
|
265 * @throws Zend_Db_Statement_Exception |
|
266 */ |
|
267 public function fetch($style = null, $cursor = null, $offset = null) |
|
268 { |
|
269 if(count($this->_fetchStack)) { |
|
270 $row = array_shift($this->_fetchStack); |
|
271 return $row; |
|
272 } else { |
|
273 return false; |
|
274 } |
|
275 } |
|
276 |
|
277 /** |
|
278 * Returns an array containing all of the result set rows. |
|
279 * |
|
280 * @param int $style OPTIONAL Fetch mode. |
|
281 * @param int $col OPTIONAL Column number, if fetch mode is by column. |
|
282 * @return array Collection of rows, each in a format by the fetch mode. |
|
283 * @throws Zend_Db_Statement_Exception |
|
284 */ |
|
285 public function fetchAll($style = null, $col = null) |
|
286 { |
|
287 $rows = $this->_fetchStack; |
|
288 $this->_fetchStack = array(); |
|
289 |
|
290 return $rows; |
|
291 } |
|
292 |
|
293 /** |
|
294 * Returns a single column from the next row of a result set. |
|
295 * |
|
296 * @param int $col OPTIONAL Position of the column to fetch. |
|
297 * @return string |
|
298 * @throws Zend_Db_Statement_Exception |
|
299 */ |
|
300 public function fetchColumn($col = 0) |
|
301 { |
|
302 $row = $this->fetch(); |
|
303 |
|
304 if($row == false) { |
|
305 return false; |
|
306 } else { |
|
307 if(count($row) < $col) { |
|
308 require_once "Zend/Db/Statement/Exception.php"; |
|
309 throw new Zend_Db_Statement_Exception( |
|
310 "Column Position '".$col."' is out of bounds." |
|
311 ); |
|
312 } |
|
313 |
|
314 $keys = array_keys($row); |
|
315 return $row[$keys[$col]]; |
|
316 } |
|
317 } |
|
318 |
|
319 /** |
|
320 * Fetches the next row and returns it as an object. |
|
321 * |
|
322 * @param string $class OPTIONAL Name of the class to create. |
|
323 * @param array $config OPTIONAL Constructor arguments for the class. |
|
324 * @return mixed One object instance of the specified class. |
|
325 * @throws Zend_Db_Statement_Exception |
|
326 */ |
|
327 public function fetchObject($class = 'stdClass', array $config = array()) |
|
328 { |
|
329 if(!class_exists($class)) { |
|
330 throw new Zend_Db_Statement_Exception("Class '".$class."' does not exist!"); |
|
331 } |
|
332 |
|
333 $object = new $class(); |
|
334 $row = $this->fetch(); |
|
335 foreach($row AS $k => $v) { |
|
336 $object->$k = $v; |
|
337 } |
|
338 |
|
339 return $object; |
|
340 } |
|
341 |
|
342 /** |
|
343 * Retrieve a statement attribute. |
|
344 * |
|
345 * @param string $key Attribute name. |
|
346 * @return mixed Attribute value. |
|
347 * @throws Zend_Db_Statement_Exception |
|
348 */ |
|
349 public function getAttribute($key) |
|
350 { |
|
351 return false; |
|
352 } |
|
353 |
|
354 /** |
|
355 * Retrieves the next rowset (result set) for a SQL statement that has |
|
356 * multiple result sets. An example is a stored procedure that returns |
|
357 * the results of multiple queries. |
|
358 * |
|
359 * @return bool |
|
360 * @throws Zend_Db_Statement_Exception |
|
361 */ |
|
362 public function nextRowset() |
|
363 { |
|
364 return false; |
|
365 } |
|
366 |
|
367 /** |
|
368 * Returns the number of rows affected by the execution of the |
|
369 * last INSERT, DELETE, or UPDATE statement executed by this |
|
370 * statement object. |
|
371 * |
|
372 * @return int The number of rows affected. |
|
373 * @throws Zend_Db_Statement_Exception |
|
374 */ |
|
375 public function rowCount() |
|
376 { |
|
377 return $this->_rowCount; |
|
378 } |
|
379 |
|
380 /** |
|
381 * Set a statement attribute. |
|
382 * |
|
383 * @param string $key Attribute name. |
|
384 * @param mixed $val Attribute value. |
|
385 * @return bool |
|
386 * @throws Zend_Db_Statement_Exception |
|
387 */ |
|
388 public function setAttribute($key, $val) |
|
389 { |
|
390 return true; |
|
391 } |
|
392 |
|
393 /** |
|
394 * Set the default fetch mode for this statement. |
|
395 * |
|
396 * @param int $mode The fetch mode. |
|
397 * @return bool |
|
398 * @throws Zend_Db_Statement_Exception |
|
399 */ |
|
400 public function setFetchMode($mode) |
|
401 { |
|
402 return true; |
|
403 } |
|
404 } |