Statement.php 18.7 KB
Newer Older
zYne's avatar
zYne committed
1
<?php
zYne's avatar
zYne committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 *  $Id: Statement.php 1532 2007-05-31 17:45:07Z zYne $
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
19
 * <http://www.phpdoctrine.org>.
zYne's avatar
zYne committed
20
 */
romanb's avatar
romanb committed
21

zYne's avatar
zYne committed
22
/**
romanb's avatar
romanb committed
23
 * A thin wrapper around PDOStatement.
zYne's avatar
zYne committed
24 25 26
 *
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
27
 * @link        www.phpdoctrine.org
zYne's avatar
zYne committed
28
 * @since       1.0
29 30
 * @version     $Revision: 1532 $
 * @todo Do we seriously need this wrapper?
zYne's avatar
zYne committed
31
 */
romanb's avatar
romanb committed
32
class Doctrine_Connection_Statement
zYne's avatar
zYne committed
33
{
zYne's avatar
zYne committed
34 35 36 37 38
    /**
     * @var Doctrine_Connection $conn       Doctrine_Connection object, every connection
     *                                      statement holds an instance of Doctrine_Connection
     */
    protected $_conn;
39

40
    /**
romanb's avatar
romanb committed
41
     * @var PDOStatement $_stmt                    PDOStatement object, boolean false or Doctrine_Adapter_Statement object
42
     */
zYne's avatar
zYne committed
43
    protected $_stmt;
44

zYne's avatar
zYne committed
45 46 47 48 49 50 51 52
    /**
     * constructor
     *
     * @param Doctrine_Connection $conn     Doctrine_Connection object, every connection
     *                                      statement holds an instance of Doctrine_Connection
     * @param mixed $stmt
     */
    public function __construct(Doctrine_Connection $conn, $stmt)
zYne's avatar
zYne committed
53
    {
zYne's avatar
zYne committed
54
        $this->_conn = $conn;
zYne's avatar
zYne committed
55
        $this->_stmt = $stmt;
56

zYne's avatar
zYne committed
57
        if ($stmt === false) {
58
            throw new Doctrine_Exception('Unknown statement object given.');
zYne's avatar
zYne committed
59 60
        }
    }
61

zYne's avatar
zYne committed
62
    /**
zYne's avatar
zYne committed
63 64
     * getConnection
     * returns the connection object this statement uses
zYne's avatar
zYne committed
65
     *
zYne's avatar
zYne committed
66
     * @return Doctrine_Connection
zYne's avatar
zYne committed
67
     */
zYne's avatar
zYne committed
68
    public function getConnection()
zYne's avatar
zYne committed
69
    {
70
        return $this->_conn;
zYne's avatar
zYne committed
71 72 73 74 75 76 77
    }
    public function getStatement()
    {
        return $this->_stmt;
    }
    public function getQuery()
    {
zYne's avatar
zYne committed
78
        return $this->_stmt->queryString;
zYne's avatar
zYne committed
79
    }
80

zYne's avatar
zYne committed
81 82 83 84 85 86 87 88 89 90 91 92 93
    /**
     * Bind a column to a PHP variable
     *
     * @param mixed $column         Number of the column (1-indexed) or name of the column in the result set.
     *                              If using the column name, be aware that the name should match
     *                              the case of the column, as returned by the driver.
     *
     * @param string $param         Name of the PHP variable to which the column will be bound.
     * @param integer $type         Data type of the parameter, specified by the Doctrine::PARAM_* constants.
     * @return boolean              Returns TRUE on success or FALSE on failure
     */
    public function bindColumn($column, $param, $type = null)
    {
94
        if ($type === null) {
zYne's avatar
zYne committed
95 96 97 98 99
            return $this->_stmt->bindColumn($column, $param);
        } else {
            return $this->_stmt->bindColumn($column, $param, $type);
        }
    }
100

zYne's avatar
zYne committed
101 102
    /**
     * bindValue
103
     * Binds a value to a corresponding named or question mark
zYne's avatar
zYne committed
104 105 106 107 108 109 110 111 112 113 114 115 116
     * placeholder in the SQL statement that was use to prepare the statement.
     *
     * @param mixed $param          Parameter identifier. For a prepared statement using named placeholders,
     *                              this will be a parameter name of the form :name. For a prepared statement
     *                              using question mark placeholders, this will be the 1-indexed position of the parameter
     *
     * @param mixed $value          The value to bind to the parameter.
     * @param integer $type         Explicit data type for the parameter using the Doctrine::PARAM_* constants.
     *
     * @return boolean              Returns TRUE on success or FALSE on failure.
     */
    public function bindValue($param, $value, $type = null)
    {
117
        if ($type === null) {
zYne's avatar
zYne committed
118 119 120 121 122
            return $this->_stmt->bindValue($param, $value);
        } else {
            return $this->_stmt->bindValue($param, $value, $type);
        }
    }
123

zYne's avatar
zYne committed
124
    /**
125
     * Binds a PHP variable to a corresponding named or question mark placeholder in the
zYne's avatar
zYne committed
126
     * SQL statement that was use to prepare the statement. Unlike Doctrine_Adapter_Statement_Interface->bindValue(),
127
     * the variable is bound as a reference and will only be evaluated at the time
zYne's avatar
zYne committed
128 129
     * that Doctrine_Adapter_Statement_Interface->execute() is called.
     *
130 131
     * Most parameters are input parameters, that is, parameters that are
     * used in a read-only fashion to build up the query. Some drivers support the invocation
zYne's avatar
zYne committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
     * of stored procedures that return data as output parameters, and some also as input/output
     * parameters that both send in data and are updated to receive it.
     *
     * @param mixed $param          Parameter identifier. For a prepared statement using named placeholders,
     *                              this will be a parameter name of the form :name. For a prepared statement
     *                              using question mark placeholders, this will be the 1-indexed position of the parameter
     *
     * @param mixed $variable       Name of the PHP variable to bind to the SQL statement parameter.
     *
     * @param integer $type         Explicit data type for the parameter using the Doctrine::PARAM_* constants. To return
     *                              an INOUT parameter from a stored procedure, use the bitwise OR operator to set the
     *                              Doctrine::PARAM_INPUT_OUTPUT bits for the data_type parameter.
     *
     * @param integer $length       Length of the data type. To indicate that a parameter is an OUT parameter
     *                              from a stored procedure, you must explicitly set the length.
     * @param mixed $driverOptions
     * @return boolean              Returns TRUE on success or FALSE on failure.
     */
150
    public function bindParam($column, &$variable, $type = null, $length = null, $driverOptions = array())
zYne's avatar
zYne committed
151
    {
152
        if ($type === null) {
zYne's avatar
zYne committed
153 154 155 156 157
            return $this->_stmt->bindParam($column, $variable);
        } else {
            return $this->_stmt->bindParam($column, $variable, $type, $length, $driverOptions);
        }
    }
158

zYne's avatar
zYne committed
159 160 161 162 163 164 165 166 167
    /**
     * Closes the cursor, enabling the statement to be executed again.
     *
     * @return boolean              Returns TRUE on success or FALSE on failure.
     */
    public function closeCursor()
    {
        return $this->_stmt->closeCursor();
    }
168

169 170
    /**
     * Returns the number of columns in the result set
zYne's avatar
zYne committed
171 172 173 174 175 176 177 178 179
     *
     * @return integer              Returns the number of columns in the result set represented
     *                              by the Doctrine_Adapter_Statement_Interface object. If there is no result set,
     *                              this method should return 0.
     */
    public function columnCount()
    {
        return $this->_stmt->columnCount();
    }
180

zYne's avatar
zYne committed
181
    /**
182
     * Fetch the SQLSTATE associated with the last operation on the statement handle
zYne's avatar
zYne committed
183 184 185 186 187 188 189 190
     *
     * @see Doctrine_Adapter_Interface::errorCode()
     * @return string       error code string
     */
    public function errorCode()
    {
        return $this->_stmt->errorCode();
    }
191

zYne's avatar
zYne committed
192 193 194 195 196 197
    /**
     * Fetch extended error information associated with the last operation on the statement handle
     *
     * @see Doctrine_Adapter_Interface::errorInfo()
     * @return array        error info array
     */
198
    public function errorInfo()
zYne's avatar
zYne committed
199 200 201
    {
        return $this->_stmt->errorInfo();
    }
202

zYne's avatar
zYne committed
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
    /**
     * Executes a prepared statement
     *
     * If the prepared statement included parameter markers, you must either:
     * call PDOStatement->bindParam() to bind PHP variables to the parameter markers:
     * bound variables pass their value as input and receive the output value,
     * if any, of their associated parameter markers or pass an array of input-only
     * parameter values
     *
     *
     * @param array $params             An array of values with as many elements as there are
     *                                  bound parameters in the SQL statement being executed.
     * @return boolean                  Returns TRUE on success or FALSE on failure.
     */
    public function execute($params = null)
    {
219
        try {
romanb's avatar
romanb committed
220 221
            //$event = new Doctrine_Event($this, Doctrine_Event::STMT_EXECUTE, $this->getQuery(), $params);
            //$this->_conn->getListener()->preStmtExecute($event);
222 223

            $result = true;
romanb's avatar
romanb committed
224
            //if ( ! $event->skipOperation) {
225
                $result = $this->_stmt->execute($params);
romanb's avatar
romanb committed
226 227
                //$this->_conn->incrementQueryCount();
            //}
228

romanb's avatar
romanb committed
229
            //$this->_conn->getListener()->postStmtExecute($event);
230 231

            return $result;
romanb's avatar
romanb committed
232 233
        } catch (PDOException $e) {
            $this->_conn->rethrowException($e, $this);
zYne's avatar
zYne committed
234 235
        }

236
        return false;
zYne's avatar
zYne committed
237
    }
238

zYne's avatar
zYne committed
239 240 241 242 243 244 245 246
    /**
     * fetch
     *
     * @see Doctrine::FETCH_* constants
     * @param integer $fetchStyle           Controls how the next row will be returned to the caller.
     *                                      This value must be one of the Doctrine::FETCH_* constants,
     *                                      defaulting to Doctrine::FETCH_BOTH
     *
247 248
     * @param integer $cursorOrientation    For a PDOStatement object representing a scrollable cursor,
     *                                      this value determines which row will be returned to the caller.
zYne's avatar
zYne committed
249
     *                                      This value must be one of the Doctrine::FETCH_ORI_* constants, defaulting to
250
     *                                      Doctrine::FETCH_ORI_NEXT. To request a scrollable cursor for your
zYne's avatar
zYne committed
251 252 253 254 255 256 257
     *                                      Doctrine_Adapter_Statement_Interface object,
     *                                      you must set the Doctrine::ATTR_CURSOR attribute to Doctrine::CURSOR_SCROLL when you
     *                                      prepare the SQL statement with Doctrine_Adapter_Interface->prepare().
     *
     * @param integer $cursorOffset         For a Doctrine_Adapter_Statement_Interface object representing a scrollable cursor for which the
     *                                      $cursorOrientation parameter is set to Doctrine::FETCH_ORI_ABS, this value specifies
     *                                      the absolute number of the row in the result set that shall be fetched.
258 259 260 261
     *
     *                                      For a Doctrine_Adapter_Statement_Interface object representing a scrollable cursor for
     *                                      which the $cursorOrientation parameter is set to Doctrine::FETCH_ORI_REL, this value
     *                                      specifies the row to fetch relative to the cursor position before
zYne's avatar
zYne committed
262 263 264 265
     *                                      Doctrine_Adapter_Statement_Interface->fetch() was called.
     *
     * @return mixed
     */
266
    public function fetch($fetchMode = Doctrine::FETCH_BOTH,
zYne's avatar
zYne committed
267 268 269
                          $cursorOrientation = Doctrine::FETCH_ORI_NEXT,
                          $cursorOffset = null)
    {
romanb's avatar
romanb committed
270 271 272 273
        //$event = new Doctrine_Event($this, Doctrine_Event::STMT_FETCH, $this->getQuery());
        //$event->fetchMode = $fetchMode;
        //$event->cursorOrientation = $cursorOrientation;
        //$event->cursorOffset = $cursorOffset;
zYne's avatar
zYne committed
274

romanb's avatar
romanb committed
275
        //$data = $this->_conn->getListener()->preFetch($event);
zYne's avatar
zYne committed
276

romanb's avatar
romanb committed
277
        //if ( ! $event->skipOperation) {
278
            $data = $this->_stmt->fetch($fetchMode, $cursorOrientation, $cursorOffset);
romanb's avatar
romanb committed
279
        //}
280

romanb's avatar
romanb committed
281
        //$this->_conn->getListener()->postFetch($event);
282

zYne's avatar
zYne committed
283 284
        return $data;
    }
285

zYne's avatar
zYne committed
286 287 288
    /**
     * Returns an array containing all of the result set rows
     *
289 290 291
     * @param integer $fetchMode            Controls how the next row will be returned to the caller.
     *                                      This value must be one of the Doctrine::FETCH_* constants,
     *                                      defaulting to Doctrine::FETCH_BOTH
zYne's avatar
zYne committed
292 293 294 295 296 297
     *
     * @param integer $columnIndex          Returns the indicated 0-indexed column when the value of $fetchStyle is
     *                                      Doctrine::FETCH_COLUMN. Defaults to 0.
     *
     * @return array
     */
romanb's avatar
romanb committed
298
    public function fetchAll($fetchMode = Doctrine::FETCH_BOTH, $columnIndex = null)
zYne's avatar
zYne committed
299
    {
romanb's avatar
romanb committed
300 301 302 303
        //$event = new Doctrine_Event($this, Doctrine_Event::STMT_FETCHALL, $this->getQuery());
        //$event->fetchMode = $fetchMode;
        //$event->columnIndex = $columnIndex;
        //$this->_conn->getListener()->preFetchAll($event);
zYne's avatar
zYne committed
304

romanb's avatar
romanb committed
305
        //if ( ! $event->skipOperation) {
zYne's avatar
zYne committed
306
            if ($columnIndex !== null) {
307
                $data = $this->_stmt->fetchAll($fetchMode, $columnIndex);
zYne's avatar
zYne committed
308
            } else {
309
                $data = $this->_stmt->fetchAll($fetchMode);
zYne's avatar
zYne committed
310
            }
romanb's avatar
romanb committed
311 312
            //$event->data = $data;
        //}
313

romanb's avatar
romanb committed
314
        //$this->_conn->getListener()->postFetchAll($event);
zYne's avatar
zYne committed
315 316 317

        return $data;
    }
318

zYne's avatar
zYne committed
319 320 321 322
    /**
     * Returns a single column from the next row of a
     * result set or FALSE if there are no more rows.
     *
323 324
     * @param integer $columnIndex          0-indexed number of the column you wish to retrieve from the row. If no
     *                                      value is supplied, Doctrine_Adapter_Statement_Interface->fetchColumn()
zYne's avatar
zYne committed
325 326 327 328 329 330 331 332
     *                                      fetches the first column.
     *
     * @return string                       returns a single column in the next row of a result set.
     */
    public function fetchColumn($columnIndex = 0)
    {
        return $this->_stmt->fetchColumn($columnIndex);
    }
333

zYne's avatar
zYne committed
334 335 336
    /**
     * Fetches the next row and returns it as an object.
     *
337
     * Fetches the next row and returns it as an object. This function is an alternative to
zYne's avatar
zYne committed
338 339
     * Doctrine_Adapter_Statement_Interface->fetch() with Doctrine::FETCH_CLASS or Doctrine::FETCH_OBJ style.
     *
340
     * @param string $className             Name of the created class, defaults to stdClass.
zYne's avatar
zYne committed
341 342
     * @param array $args                   Elements of this array are passed to the constructor.
     *
343
     * @return mixed                        an instance of the required class with property names that correspond
zYne's avatar
zYne committed
344 345 346 347 348 349
     *                                      to the column names or FALSE in case of an error.
     */
    public function fetchObject($className = 'stdClass', $args = array())
    {
        return $this->_stmt->fetchObject($className, $args);
    }
350

zYne's avatar
zYne committed
351
    /**
352
     * Retrieve a statement attribute
zYne's avatar
zYne committed
353 354 355 356 357 358 359 360 361
     *
     * @param integer $attribute
     * @see Doctrine::ATTR_* constants
     * @return mixed                        the attribute value
     */
    public function getAttribute($attribute)
    {
        return $this->_stmt->getAttribute($attribute);
    }
362

zYne's avatar
zYne committed
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
    /**
     * Returns metadata for a column in a result set
     *
     * @param integer $column               The 0-indexed column in the result set.
     *
     * @return array                        Associative meta data array with the following structure:
     *
     *          native_type                 The PHP native type used to represent the column value.
     *          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().
     *          flags                       Any flags set for this column.
     *          name                        The name of this column as returned by the database.
     *          len                         The length of this column. Normally -1 for types other than floating point decimals.
     *          precision                   The numeric precision of this column. Normally 0 for types other than floating point decimals.
     *          pdo_type                    The type of this column as represented by the PDO::PARAM_* constants.
     */
    public function getColumnMeta($column)
    {
        return $this->_stmt->getColumnMeta($column);
    }
382

zYne's avatar
zYne committed
383 384
    /**
     * Advances to the next rowset in a multi-rowset statement handle
385 386 387 388
     *
     * Some database servers support stored procedures that return more than one rowset
     * (also known as a result set). The nextRowset() method enables you to access the second
     * and subsequent rowsets associated with a PDOStatement object. Each rowset can have a
zYne's avatar
zYne committed
389 390 391 392
     * different set of columns from the preceding rowset.
     *
     * @return boolean                      Returns TRUE on success or FALSE on failure.
     */
393
    public function nextRowset()
zYne's avatar
zYne committed
394 395
    {
        return $this->_stmt->nextRowset();
396
    }
397

zYne's avatar
zYne committed
398
    /**
399
     * rowCount() returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement
zYne's avatar
zYne committed
400 401
     * executed by the corresponding object.
     *
402 403 404
     * If the last SQL statement executed by the associated Statement object was a SELECT statement,
     * some databases may return the number of rows returned by that statement. However,
     * this behaviour is not guaranteed for all databases and should not be
zYne's avatar
zYne committed
405 406 407 408
     * relied on for portable applications.
     *
     * @return integer                      Returns the number of rows.
     */
409
    public function rowCount()
zYne's avatar
zYne committed
410 411 412
    {
        return $this->_stmt->rowCount();
    }
413

zYne's avatar
zYne committed
414 415 416 417 418 419 420 421 422 423 424
    /**
     * Set a statement attribute
     *
     * @param integer $attribute
     * @param mixed $value                  the value of given attribute
     * @return boolean                      Returns TRUE on success or FALSE on failure.
     */
    public function setAttribute($attribute, $value)
    {
        return $this->_stmt->setAttribute($attribute, $value);
    }
425

zYne's avatar
zYne committed
426
    /**
427
     * Set the default fetch mode for this statement
zYne's avatar
zYne committed
428 429 430 431 432 433 434 435
     *
     * @param integer $mode                 The fetch mode must be one of the Doctrine::FETCH_* constants.
     * @return boolean                      Returns 1 on success or FALSE on failure.
     */
    public function setFetchMode($mode, $arg1 = null, $arg2 = null)
    {
        return $this->_stmt->setFetchMode($mode, $arg1, $arg2);
    }
436
}