File indexing completed on 2025-01-19 05:21:02

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Db
0017  * @subpackage Statement
0018  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0019  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0020  * @version    $Id$
0021  */
0022 
0023 /**
0024  * @see Zend_Db_Statement
0025  */
0026 // require_once 'Zend/Db/Statement.php';
0027 
0028 /**
0029  * Proxy class to wrap a PDOStatement object.
0030  * Matches the interface of PDOStatement.  All methods simply proxy to the
0031  * matching method in PDOStatement.  PDOExceptions thrown by PDOStatement
0032  * are re-thrown as Zend_Db_Statement_Exception.
0033  *
0034  * @category   Zend
0035  * @package    Zend_Db
0036  * @subpackage Statement
0037  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0038  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0039  */
0040 class Zend_Db_Statement_Pdo extends Zend_Db_Statement implements IteratorAggregate
0041 {
0042 
0043     /**
0044      * @var int
0045      */
0046     protected $_fetchMode = PDO::FETCH_ASSOC;
0047 
0048     /**
0049      * Prepare a string SQL statement and create a statement object.
0050      *
0051      * @param string $sql
0052      * @return void
0053      * @throws Zend_Db_Statement_Exception
0054      */
0055     protected function _prepare($sql)
0056     {
0057         try {
0058             $this->_stmt = $this->_adapter->getConnection()->prepare($sql);
0059         } catch (PDOException $e) {
0060             // require_once 'Zend/Db/Statement/Exception.php';
0061             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
0062         }
0063     }
0064 
0065     /**
0066      * Bind a column of the statement result set to a PHP variable.
0067      *
0068      * @param string $column Name the column in the result set, either by
0069      *                       position or by name.
0070      * @param mixed  $param  Reference to the PHP variable containing the value.
0071      * @param mixed  $type   OPTIONAL
0072      * @return bool
0073      * @throws Zend_Db_Statement_Exception
0074      */
0075     public function bindColumn($column, &$param, $type = null)
0076     {
0077         try {
0078             if ($type === null) {
0079                 return $this->_stmt->bindColumn($column, $param);
0080             } else {
0081                 return $this->_stmt->bindColumn($column, $param, $type);
0082             }
0083         } catch (PDOException $e) {
0084             // require_once 'Zend/Db/Statement/Exception.php';
0085             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
0086         }
0087     }
0088 
0089     /**
0090      * Binds a parameter to the specified variable name.
0091      *
0092      * @param mixed $parameter Name the parameter, either integer or string.
0093      * @param mixed $variable  Reference to PHP variable containing the value.
0094      * @param mixed $type      OPTIONAL Datatype of SQL parameter.
0095      * @param mixed $length    OPTIONAL Length of SQL parameter.
0096      * @param mixed $options   OPTIONAL Other options.
0097      * @return bool
0098      * @throws Zend_Db_Statement_Exception
0099      */
0100     protected function _bindParam($parameter, &$variable, $type = null, $length = null, $options = null)
0101     {
0102         try {
0103             if ($type === null) {
0104                 if (is_bool($variable)) {
0105                     $type = PDO::PARAM_BOOL;
0106                 } elseif ($variable === null) {
0107                     $type = PDO::PARAM_NULL;
0108                 } elseif (is_integer($variable)) {
0109                     $type = PDO::PARAM_INT;
0110                 } else {
0111                     $type = PDO::PARAM_STR;
0112                 }
0113             }
0114             return $this->_stmt->bindParam($parameter, $variable, $type, $length, $options);
0115         } catch (PDOException $e) {
0116             // require_once 'Zend/Db/Statement/Exception.php';
0117             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
0118         }
0119     }
0120 
0121     /**
0122      * Binds a value to a parameter.
0123      *
0124      * @param mixed $parameter Name the parameter, either integer or string.
0125      * @param mixed $value     Scalar value to bind to the parameter.
0126      * @param mixed $type      OPTIONAL Datatype of the parameter.
0127      * @return bool
0128      * @throws Zend_Db_Statement_Exception
0129      */
0130     public function bindValue($parameter, $value, $type = null)
0131     {
0132         if (is_string($parameter) && $parameter[0] != ':') {
0133             $parameter = ":$parameter";
0134         }
0135 
0136         $this->_bindParam[$parameter] = $value;
0137 
0138         try {
0139             if ($type === null) {
0140                 return $this->_stmt->bindValue($parameter, $value);
0141             } else {
0142                 return $this->_stmt->bindValue($parameter, $value, $type);
0143             }
0144         } catch (PDOException $e) {
0145             // require_once 'Zend/Db/Statement/Exception.php';
0146             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
0147         }
0148     }
0149 
0150     /**
0151      * Closes the cursor, allowing the statement to be executed again.
0152      *
0153      * @return bool
0154      * @throws Zend_Db_Statement_Exception
0155      */
0156     public function closeCursor()
0157     {
0158         try {
0159             return $this->_stmt->closeCursor();
0160         } catch (PDOException $e) {
0161             // require_once 'Zend/Db/Statement/Exception.php';
0162             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
0163         }
0164     }
0165 
0166     /**
0167      * Returns the number of columns in the result set.
0168      * Returns null if the statement has no result set metadata.
0169      *
0170      * @return int The number of columns.
0171      * @throws Zend_Db_Statement_Exception
0172      */
0173     public function columnCount()
0174     {
0175         try {
0176             return $this->_stmt->columnCount();
0177         } catch (PDOException $e) {
0178             // require_once 'Zend/Db/Statement/Exception.php';
0179             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
0180         }
0181     }
0182 
0183     /**
0184      * Retrieves the error code, if any, associated with the last operation on
0185      * the statement handle.
0186      *
0187      * @return string error code.
0188      * @throws Zend_Db_Statement_Exception
0189      */
0190     public function errorCode()
0191     {
0192         try {
0193             return $this->_stmt->errorCode();
0194         } catch (PDOException $e) {
0195             // require_once 'Zend/Db/Statement/Exception.php';
0196             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
0197         }
0198     }
0199 
0200     /**
0201      * Retrieves an array of error information, if any, associated with the
0202      * last operation on the statement handle.
0203      *
0204      * @return array
0205      * @throws Zend_Db_Statement_Exception
0206      */
0207     public function errorInfo()
0208     {
0209         try {
0210             return $this->_stmt->errorInfo();
0211         } catch (PDOException $e) {
0212             // require_once 'Zend/Db/Statement/Exception.php';
0213             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
0214         }
0215     }
0216 
0217     /**
0218      * Executes a prepared statement.
0219      *
0220      * @param array $params OPTIONAL Values to bind to parameter placeholders.
0221      * @return bool
0222      * @throws Zend_Db_Statement_Exception
0223      */
0224     public function _execute(array $params = null)
0225     {
0226         try {
0227             if ($params !== null) {
0228                 return $this->_stmt->execute($params);
0229             } else {
0230                 return $this->_stmt->execute();
0231             }
0232         } catch (PDOException $e) {
0233             // require_once 'Zend/Db/Statement/Exception.php';
0234             $message = sprintf('%s, query was: %s', $e->getMessage(), $this->_stmt->queryString);
0235             throw new Zend_Db_Statement_Exception($message, (int) $e->getCode(), $e);
0236         }
0237     }
0238 
0239     /**
0240      * Fetches a row from the result set.
0241      *
0242      * @param int $style  OPTIONAL Fetch mode for this fetch operation.
0243      * @param int $cursor OPTIONAL Absolute, relative, or other.
0244      * @param int $offset OPTIONAL Number for absolute or relative cursors.
0245      * @return mixed Array, object, or scalar depending on fetch mode.
0246      * @throws Zend_Db_Statement_Exception
0247      */
0248     public function fetch($style = null, $cursor = null, $offset = null)
0249     {
0250         if ($style === null) {
0251             $style = $this->_fetchMode;
0252         }
0253         try {
0254             return $this->_stmt->fetch($style, $cursor, $offset);
0255         } catch (PDOException $e) {
0256             // require_once 'Zend/Db/Statement/Exception.php';
0257             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
0258         }
0259     }
0260 
0261     /**
0262      * Required by IteratorAggregate interface
0263      *
0264      * @return IteratorIterator
0265      */
0266     public function getIterator()
0267     {
0268         return new IteratorIterator($this->_stmt);
0269     }
0270 
0271     /**
0272      * Returns an array containing all of the result set rows.
0273      *
0274      * @param int $style OPTIONAL Fetch mode.
0275      * @param int $col   OPTIONAL Column number, if fetch mode is by column.
0276      * @return array Collection of rows, each in a format by the fetch mode.
0277      * @throws Zend_Db_Statement_Exception
0278      */
0279     public function fetchAll($style = null, $col = null)
0280     {
0281         if ($style === null) {
0282             $style = $this->_fetchMode;
0283         }
0284         try {
0285             if ($style == PDO::FETCH_COLUMN) {
0286                 if ($col === null) {
0287                     $col = 0;
0288                 }
0289                 return $this->_stmt->fetchAll($style, $col);
0290             } else {
0291                 return $this->_stmt->fetchAll($style);
0292             }
0293         } catch (PDOException $e) {
0294             // require_once 'Zend/Db/Statement/Exception.php';
0295             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
0296         }
0297     }
0298 
0299     /**
0300      * Returns a single column from the next row of a result set.
0301      *
0302      * @param int $col OPTIONAL Position of the column to fetch.
0303      * @return string
0304      * @throws Zend_Db_Statement_Exception
0305      */
0306     public function fetchColumn($col = 0)
0307     {
0308         try {
0309             return $this->_stmt->fetchColumn($col);
0310         } catch (PDOException $e) {
0311             // require_once 'Zend/Db/Statement/Exception.php';
0312             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
0313         }
0314     }
0315 
0316     /**
0317      * Fetches the next row and returns it as an object.
0318      *
0319      * @param string $class  OPTIONAL Name of the class to create.
0320      * @param array  $config OPTIONAL Constructor arguments for the class.
0321      * @return mixed One object instance of the specified class.
0322      * @throws Zend_Db_Statement_Exception
0323      */
0324     public function fetchObject($class = 'stdClass', array $config = array())
0325     {
0326         try {
0327             return $this->_stmt->fetchObject($class, $config);
0328         } catch (PDOException $e) {
0329             // require_once 'Zend/Db/Statement/Exception.php';
0330             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
0331         }
0332     }
0333 
0334     /**
0335      * Retrieve a statement attribute.
0336      *
0337      * @param integer $key Attribute name.
0338      * @return mixed      Attribute value.
0339      * @throws Zend_Db_Statement_Exception
0340      */
0341     public function getAttribute($key)
0342     {
0343         try {
0344             return $this->_stmt->getAttribute($key);
0345         } catch (PDOException $e) {
0346             // require_once 'Zend/Db/Statement/Exception.php';
0347             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
0348         }
0349     }
0350 
0351     /**
0352      * Returns metadata for a column in a result set.
0353      *
0354      * @param int $column
0355      * @return mixed
0356      * @throws Zend_Db_Statement_Exception
0357      */
0358     public function getColumnMeta($column)
0359     {
0360         try {
0361             return $this->_stmt->getColumnMeta($column);
0362         } catch (PDOException $e) {
0363             // require_once 'Zend/Db/Statement/Exception.php';
0364             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
0365         }
0366     }
0367 
0368     /**
0369      * Retrieves the next rowset (result set) for a SQL statement that has
0370      * multiple result sets.  An example is a stored procedure that returns
0371      * the results of multiple queries.
0372      *
0373      * @return bool
0374      * @throws Zend_Db_Statement_Exception
0375      */
0376     public function nextRowset()
0377     {
0378         try {
0379             return $this->_stmt->nextRowset();
0380         } catch (PDOException $e) {
0381             // require_once 'Zend/Db/Statement/Exception.php';
0382             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
0383         }
0384     }
0385 
0386     /**
0387      * Returns the number of rows affected by the execution of the
0388      * last INSERT, DELETE, or UPDATE statement executed by this
0389      * statement object.
0390      *
0391      * @return int     The number of rows affected.
0392      * @throws Zend_Db_Statement_Exception
0393      */
0394     public function rowCount()
0395     {
0396         try {
0397             return $this->_stmt->rowCount();
0398         } catch (PDOException $e) {
0399             // require_once 'Zend/Db/Statement/Exception.php';
0400             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
0401         }
0402     }
0403 
0404     /**
0405      * Set a statement attribute.
0406      *
0407      * @param string $key Attribute name.
0408      * @param mixed  $val Attribute value.
0409      * @return bool
0410      * @throws Zend_Db_Statement_Exception
0411      */
0412     public function setAttribute($key, $val)
0413     {
0414         try {
0415             return $this->_stmt->setAttribute($key, $val);
0416         } catch (PDOException $e) {
0417             // require_once 'Zend/Db/Statement/Exception.php';
0418             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
0419         }
0420     }
0421 
0422     /**
0423      * Set the default fetch mode for this statement.
0424      *
0425      * @param int   $mode The fetch mode.
0426      * @return bool
0427      * @throws Zend_Db_Statement_Exception
0428      */
0429     public function setFetchMode($mode)
0430     {
0431         $this->_fetchMode = $mode;
0432         try {
0433             return $this->_stmt->setFetchMode($mode);
0434         } catch (PDOException $e) {
0435             // require_once 'Zend/Db/Statement/Exception.php';
0436             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
0437         }
0438     }
0439 
0440 }