# Coverage for Doctrine_Connection_Statement

Back to coverage report

1 | <?php
2 | /*
3 | * $Id: Statement.php 1532 2007-05-31 17:45:07Z zYne $
4 | *
5 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16 | *
17 | * This software consists of voluntary contributions made by many individuals
18 | * and is licensed under the LGPL. For more information, see
19 | * <http://www.phpdoctrine.org>.
20 | */
21 | Doctrine::autoload('Doctrine_Adapter_Statement_Interface');
22 | /**
23 | * Doctrine_Connection_Statement
24 | *
25 | * @package Doctrine
26 | * @subpackage Connection
27 | * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
28 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
29 | * @link www.phpdoctrine.org
30 | * @since 1.0
31 | * @version $Revision: 1532 $
32 | */
33 | class Doctrine_Connection_Statement implements Doctrine_Adapter_Statement_Interface
34 | {
35 | /**
36 | * @var Doctrine_Connection $conn Doctrine_Connection object, every connection
37 | * statement holds an instance of Doctrine_Connection
38 | */
39 | protected $_conn;
40 |
41 | /**
42 | * @var mixed $_stmt PDOStatement object, boolean false or Doctrine_Adapter_Statement object
43 | */
44 | protected $_stmt;
45 |
46 | /**
47 | * constructor
48 | *
49 | * @param Doctrine_Connection $conn Doctrine_Connection object, every connection
50 | * statement holds an instance of Doctrine_Connection
51 | * @param mixed $stmt
52 | */
53 | public function __construct(Doctrine_Connection $conn, $stmt)
54 | {
55 | $this->_conn = $conn;
56 | $this->_stmt = $stmt;
57 |
58 | if ($stmt === false) {
59 | throw new Doctrine_Exception('Unknown statement object given.');
60 | }
61 | }
62 |
63 | /**
64 | * getConnection
65 | * returns the connection object this statement uses
66 | *
67 | * @return Doctrine_Connection
68 | */
69 | public function getConnection()
70 | {
71 | return $this->_conn;
72 | }
73 | public function getStatement()
74 | {
75 | return $this->_stmt;
76 | }
77 | public function getQuery()
78 | {
79 | return $this->_stmt->queryString;
80 | }
81 |
82 | /**
83 | * bindColumn
84 | * Bind a column to a PHP variable
85 | *
86 | * @param mixed $column Number of the column (1-indexed) or name of the column in the result set.
87 | * If using the column name, be aware that the name should match
88 | * the case of the column, as returned by the driver.
89 | *
90 | * @param string $param Name of the PHP variable to which the column will be bound.
91 | * @param integer $type Data type of the parameter, specified by the Doctrine::PARAM_* constants.
92 | * @return boolean Returns TRUE on success or FALSE on failure
93 | */
94 | public function bindColumn($column, $param, $type = null)
95 | {
96 | if ($type === null) {
97 | return $this->_stmt->bindColumn($column, $param);
98 | } else {
99 | return $this->_stmt->bindColumn($column, $param, $type);
100 | }
101 | }
102 |
103 | /**
104 | * bindValue
105 | * Binds a value to a corresponding named or question mark
106 | * placeholder in the SQL statement that was use to prepare the statement.
107 | *
108 | * @param mixed $param Parameter identifier. For a prepared statement using named placeholders,
109 | * this will be a parameter name of the form :name. For a prepared statement
110 | * using question mark placeholders, this will be the 1-indexed position of the parameter
111 | *
112 | * @param mixed $value The value to bind to the parameter.
113 | * @param integer $type Explicit data type for the parameter using the Doctrine::PARAM_* constants.
114 | *
115 | * @return boolean Returns TRUE on success or FALSE on failure.
116 | */
117 | public function bindValue($param, $value, $type = null)
118 | {
119 | if ($type === null) {
120 | return $this->_stmt->bindValue($param, $value);
121 | } else {
122 | return $this->_stmt->bindValue($param, $value, $type);
123 | }
124 | }
125 |
126 | /**
127 | * bindParam
128 | * Binds a PHP variable to a corresponding named or question mark placeholder in the
129 | * SQL statement that was use to prepare the statement. Unlike Doctrine_Adapter_Statement_Interface->bindValue(),
130 | * the variable is bound as a reference and will only be evaluated at the time
131 | * that Doctrine_Adapter_Statement_Interface->execute() is called.
132 | *
133 | * Most parameters are input parameters, that is, parameters that are
134 | * used in a read-only fashion to build up the query. Some drivers support the invocation
135 | * of stored procedures that return data as output parameters, and some also as input/output
136 | * parameters that both send in data and are updated to receive it.
137 | *
138 | * @param mixed $param Parameter identifier. For a prepared statement using named placeholders,
139 | * this will be a parameter name of the form :name. For a prepared statement
140 | * using question mark placeholders, this will be the 1-indexed position of the parameter
141 | *
142 | * @param mixed $variable Name of the PHP variable to bind to the SQL statement parameter.
143 | *
144 | * @param integer $type Explicit data type for the parameter using the Doctrine::PARAM_* constants. To return
145 | * an INOUT parameter from a stored procedure, use the bitwise OR operator to set the
146 | * Doctrine::PARAM_INPUT_OUTPUT bits for the data_type parameter.
147 | *
148 | * @param integer $length Length of the data type. To indicate that a parameter is an OUT parameter
149 | * from a stored procedure, you must explicitly set the length.
150 | * @param mixed $driverOptions
151 | * @return boolean Returns TRUE on success or FALSE on failure.
152 | */
153 | public function bindParam($column, &$variable, $type = null, $length = null, $driverOptions = array())
154 | {
155 | if ($type === null) {
156 | return $this->_stmt->bindParam($column, $variable);
157 | } else {
158 | return $this->_stmt->bindParam($column, $variable, $type, $length, $driverOptions);
159 | }
160 | }
161 |
162 | /**
163 | * closeCursor
164 | * Closes the cursor, enabling the statement to be executed again.
165 | *
166 | * @return boolean Returns TRUE on success or FALSE on failure.
167 | */
168 | public function closeCursor()
169 | {
170 | return $this->_stmt->closeCursor();
171 | }
172 |
173 | /**
174 | * columnCount
175 | * Returns the number of columns in the result set
176 | *
177 | * @return integer Returns the number of columns in the result set represented
178 | * by the Doctrine_Adapter_Statement_Interface object. If there is no result set,
179 | * this method should return 0.
180 | */
181 | public function columnCount()
182 | {
183 | return $this->_stmt->columnCount();
184 | }
185 |
186 | /**
187 | * errorCode
188 | * Fetch the SQLSTATE associated with the last operation on the statement handle
189 | *
190 | * @see Doctrine_Adapter_Interface::errorCode()
191 | * @return string error code string
192 | */
193 | public function errorCode()
194 | {
195 | return $this->_stmt->errorCode();
196 | }
197 |
198 | /**
199 | * errorInfo
200 | * Fetch extended error information associated with the last operation on the statement handle
201 | *
202 | * @see Doctrine_Adapter_Interface::errorInfo()
203 | * @return array error info array
204 | */
205 | public function errorInfo()
206 | {
207 | return $this->_stmt->errorInfo();
208 | }
209 |
210 | /**
211 | * execute
212 | * Executes a prepared statement
213 | *
214 | * If the prepared statement included parameter markers, you must either:
215 | * call PDOStatement->bindParam() to bind PHP variables to the parameter markers:
216 | * bound variables pass their value as input and receive the output value,
217 | * if any, of their associated parameter markers or pass an array of input-only
218 | * parameter values
219 | *
220 | *
221 | * @param array $params An array of values with as many elements as there are
222 | * bound parameters in the SQL statement being executed.
223 | * @return boolean Returns TRUE on success or FALSE on failure.
224 | */
225 | public function execute($params = null)
226 | {
227 | try {
228 | $event = new Doctrine_Event($this, Doctrine_Event::STMT_EXECUTE, $this->getQuery(), $params);
229 | $this->_conn->getListener()->preStmtExecute($event);
230 |
231 | $result = true;
232 | if ( ! $event->skipOperation) {
233 | $result = $this->_stmt->execute($params);
234 | $this->_conn->incrementQueryCount();
235 | }
236 |
237 | $this->_conn->getListener()->postStmtExecute($event);
238 |
239 | return $result;
240 | } catch (PDOException $e) {
241 | } catch (Doctrine_Adapter_Exception $e) {
242 | }
243 |
244 | $this->_conn->rethrowException($e, $this);
245 |
246 | return false;
247 | }
248 |
249 | /**
250 | * fetch
251 | *
252 | * @see Doctrine::FETCH_* constants
253 | * @param integer $fetchStyle Controls how the next row will be returned to the caller.
254 | * This value must be one of the Doctrine::FETCH_* constants,
255 | * defaulting to Doctrine::FETCH_BOTH
256 | *
257 | * @param integer $cursorOrientation For a PDOStatement object representing a scrollable cursor,
258 | * this value determines which row will be returned to the caller.
259 | * This value must be one of the Doctrine::FETCH_ORI_* constants, defaulting to
260 | * Doctrine::FETCH_ORI_NEXT. To request a scrollable cursor for your
261 | * Doctrine_Adapter_Statement_Interface object,
262 | * you must set the Doctrine::ATTR_CURSOR attribute to Doctrine::CURSOR_SCROLL when you
263 | * prepare the SQL statement with Doctrine_Adapter_Interface->prepare().
264 | *
265 | * @param integer $cursorOffset For a Doctrine_Adapter_Statement_Interface object representing a scrollable cursor for which the
266 | * $cursorOrientation parameter is set to Doctrine::FETCH_ORI_ABS, this value specifies
267 | * the absolute number of the row in the result set that shall be fetched.
268 | *
269 | * For a Doctrine_Adapter_Statement_Interface object representing a scrollable cursor for
270 | * which the $cursorOrientation parameter is set to Doctrine::FETCH_ORI_REL, this value
271 | * specifies the row to fetch relative to the cursor position before
272 | * Doctrine_Adapter_Statement_Interface->fetch() was called.
273 | *
274 | * @return mixed
275 | */
276 | public function fetch($fetchMode = Doctrine::FETCH_BOTH,
277 | $cursorOrientation = Doctrine::FETCH_ORI_NEXT,
278 | $cursorOffset = null)
279 | {
280 | $event = new Doctrine_Event($this, Doctrine_Event::STMT_FETCH, $this->getQuery());
281 |
282 | $event->fetchMode = $fetchMode;
283 | $event->cursorOrientation = $cursorOrientation;
284 | $event->cursorOffset = $cursorOffset;
285 |
286 | $data = $this->_conn->getListener()->preFetch($event);
287 |
288 | if ( ! $event->skipOperation) {
289 | $data = $this->_stmt->fetch($fetchMode, $cursorOrientation, $cursorOffset);
290 | }
291 |
292 | $this->_conn->getListener()->postFetch($event);
293 |
294 | return $data;
295 | }
296 |
297 | /**
298 | * fetchAll
299 | * Returns an array containing all of the result set rows
300 | *
301 | * @param integer $fetchMode Controls how the next row will be returned to the caller.
302 | * This value must be one of the Doctrine::FETCH_* constants,
303 | * defaulting to Doctrine::FETCH_BOTH
304 | *
305 | * @param integer $columnIndex Returns the indicated 0-indexed column when the value of $fetchStyle is
306 | * Doctrine::FETCH_COLUMN. Defaults to 0.
307 | *
308 | * @return array
309 | */
310 | public function fetchAll($fetchMode = Doctrine::FETCH_BOTH,
311 | $columnIndex = null)
312 | {
313 | $event = new Doctrine_Event($this, Doctrine_Event::STMT_FETCHALL, $this->getQuery());
314 | $event->fetchMode = $fetchMode;
315 | $event->columnIndex = $columnIndex;
316 |
317 | $this->_conn->getListener()->preFetchAll($event);
318 |
319 | if ( ! $event->skipOperation) {
320 | if ($columnIndex !== null) {
321 | $data = $this->_stmt->fetchAll($fetchMode, $columnIndex);
322 | } else {
323 | $data = $this->_stmt->fetchAll($fetchMode);
324 | }
325 |
326 | $event->data = $data;
327 | }
328 |
329 | $this->_conn->getListener()->postFetchAll($event);
330 |
331 | return $data;
332 | }
333 |
334 | /**
335 | * fetchColumn
336 | * Returns a single column from the next row of a
337 | * result set or FALSE if there are no more rows.
338 | *
339 | * @param integer $columnIndex 0-indexed number of the column you wish to retrieve from the row. If no
340 | * value is supplied, Doctrine_Adapter_Statement_Interface->fetchColumn()
341 | * fetches the first column.
342 | *
343 | * @return string returns a single column in the next row of a result set.
344 | */
345 | public function fetchColumn($columnIndex = 0)
346 | {
347 | return $this->_stmt->fetchColumn($columnIndex);
348 | }
349 |
350 | /**
351 | * fetchObject
352 | * Fetches the next row and returns it as an object.
353 | *
354 | * Fetches the next row and returns it as an object. This function is an alternative to
355 | * Doctrine_Adapter_Statement_Interface->fetch() with Doctrine::FETCH_CLASS or Doctrine::FETCH_OBJ style.
356 | *
357 | * @param string $className Name of the created class, defaults to stdClass.
358 | * @param array $args Elements of this array are passed to the constructor.
359 | *
360 | * @return mixed an instance of the required class with property names that correspond
361 | * to the column names or FALSE in case of an error.
362 | */
363 | public function fetchObject($className = 'stdClass', $args = array())
364 | {
365 | return $this->_stmt->fetchObject($className, $args);
366 | }
367 |
368 | /**
369 | * getAttribute
370 | * Retrieve a statement attribute
371 | *
372 | * @param integer $attribute
373 | * @see Doctrine::ATTR_* constants
374 | * @return mixed the attribute value
375 | */
376 | public function getAttribute($attribute)
377 | {
378 | return $this->_stmt->getAttribute($attribute);
379 | }
380 |
381 | /**
382 | * getColumnMeta
383 | * Returns metadata for a column in a result set
384 | *
385 | * @param integer $column The 0-indexed column in the result set.
386 | *
387 | * @return array Associative meta data array with the following structure:
388 | *
389 | * native_type The PHP native type used to represent the column value.
390 | * driver:decl_ type The SQL type used to represent the column value in the database. If the column in the result set is the result of a function, this value is not returned by PDOStatement->getColumnMeta().
391 | * flags Any flags set for this column.
392 | * name The name of this column as returned by the database.
393 | * len The length of this column. Normally -1 for types other than floating point decimals.
394 | * precision The numeric precision of this column. Normally 0 for types other than floating point decimals.
395 | * pdo_type The type of this column as represented by the PDO::PARAM_* constants.
396 | */
397 | public function getColumnMeta($column)
398 | {
399 | return $this->_stmt->getColumnMeta($column);
400 | }
401 |
402 | /**
403 | * nextRowset
404 | * Advances to the next rowset in a multi-rowset statement handle
405 | *
406 | * Some database servers support stored procedures that return more than one rowset
407 | * (also known as a result set). The nextRowset() method enables you to access the second
408 | * and subsequent rowsets associated with a PDOStatement object. Each rowset can have a
409 | * different set of columns from the preceding rowset.
410 | *
411 | * @return boolean Returns TRUE on success or FALSE on failure.
412 | */
413 | public function nextRowset()
414 | {
415 | return $this->_stmt->nextRowset();
416 | }
417 |
418 | /**
419 | * rowCount
420 | * rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement
421 | * executed by the corresponding object.
422 | *
423 | * If the last SQL statement executed by the associated Statement object was a SELECT statement,
424 | * some databases may return the number of rows returned by that statement. However,
425 | * this behaviour is not guaranteed for all databases and should not be
426 | * relied on for portable applications.
427 | *
428 | * @return integer Returns the number of rows.
429 | */
430 | public function rowCount()
431 | {
432 | return $this->_stmt->rowCount();
433 | }
434 |
435 | /**
436 | * setAttribute
437 | * Set a statement attribute
438 | *
439 | * @param integer $attribute
440 | * @param mixed $value the value of given attribute
441 | * @return boolean Returns TRUE on success or FALSE on failure.
442 | */
443 | public function setAttribute($attribute, $value)
444 | {
445 | return $this->_stmt->setAttribute($attribute, $value);
446 | }
447 |
448 | /**
449 | * setFetchMode
450 | * Set the default fetch mode for this statement
451 | *
452 | * @param integer $mode The fetch mode must be one of the Doctrine::FETCH_* constants.
453 | * @return boolean Returns 1 on success or FALSE on failure.
454 | */
455 | public function setFetchMode($mode, $arg1 = null, $arg2 = null)
456 | {
457 | return $this->_stmt->setFetchMode($mode, $arg1, $arg2);
458 | }
459 | }