web/Zend/Db/Statement/Pdo.php
changeset 0 4eba9c11703f
equal deleted inserted replaced
-1:000000000000 0:4eba9c11703f
       
     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_Db
       
    17  * @subpackage Statement
       
    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: Pdo.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    21  */
       
    22 
       
    23 /**
       
    24  * @see Zend_Db_Statement
       
    25  */
       
    26 require_once 'Zend/Db/Statement.php';
       
    27 
       
    28 /**
       
    29  * Proxy class to wrap a PDOStatement object.
       
    30  * Matches the interface of PDOStatement.  All methods simply proxy to the
       
    31  * matching method in PDOStatement.  PDOExceptions thrown by PDOStatement
       
    32  * are re-thrown as Zend_Db_Statement_Exception.
       
    33  *
       
    34  * @category   Zend
       
    35  * @package    Zend_Db
       
    36  * @subpackage Statement
       
    37  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    38  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    39  */
       
    40 class Zend_Db_Statement_Pdo extends Zend_Db_Statement implements IteratorAggregate
       
    41 {
       
    42 
       
    43     /**
       
    44      * @var int
       
    45      */
       
    46     protected $_fetchMode = PDO::FETCH_ASSOC;
       
    47 
       
    48     /**
       
    49      * Prepare a string SQL statement and create a statement object.
       
    50      *
       
    51      * @param string $sql
       
    52      * @return void
       
    53      * @throws Zend_Db_Statement_Exception
       
    54      */
       
    55     protected function _prepare($sql)
       
    56     {
       
    57         try {
       
    58             $this->_stmt = $this->_adapter->getConnection()->prepare($sql);
       
    59         } catch (PDOException $e) {
       
    60             require_once 'Zend/Db/Statement/Exception.php';
       
    61             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
       
    62         }
       
    63     }
       
    64 
       
    65     /**
       
    66      * Bind a column of the statement result set to a PHP variable.
       
    67      *
       
    68      * @param string $column Name the column in the result set, either by
       
    69      *                       position or by name.
       
    70      * @param mixed  $param  Reference to the PHP variable containing the value.
       
    71      * @param mixed  $type   OPTIONAL
       
    72      * @return bool
       
    73      * @throws Zend_Db_Statement_Exception
       
    74      */
       
    75     public function bindColumn($column, &$param, $type = null)
       
    76     {
       
    77         try {
       
    78             if ($type === null) {
       
    79                 return $this->_stmt->bindColumn($column, $param);
       
    80             } else {
       
    81                 return $this->_stmt->bindColumn($column, $param, $type);
       
    82             }
       
    83         } catch (PDOException $e) {
       
    84             require_once 'Zend/Db/Statement/Exception.php';
       
    85             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
       
    86         }
       
    87     }
       
    88 
       
    89     /**
       
    90      * Binds a parameter to the specified variable name.
       
    91      *
       
    92      * @param mixed $parameter Name the parameter, either integer or string.
       
    93      * @param mixed $variable  Reference to PHP variable containing the value.
       
    94      * @param mixed $type      OPTIONAL Datatype of SQL parameter.
       
    95      * @param mixed $length    OPTIONAL Length of SQL parameter.
       
    96      * @param mixed $options   OPTIONAL Other options.
       
    97      * @return bool
       
    98      * @throws Zend_Db_Statement_Exception
       
    99      */
       
   100     protected function _bindParam($parameter, &$variable, $type = null, $length = null, $options = null)
       
   101     {
       
   102         try {
       
   103             if ($type === null) {
       
   104                 if (is_bool($variable)) {
       
   105                     $type = PDO::PARAM_BOOL;
       
   106                 } elseif ($variable === null) {
       
   107                     $type = PDO::PARAM_NULL;
       
   108                 } elseif (is_integer($variable)) {
       
   109                     $type = PDO::PARAM_INT;
       
   110                 } else {
       
   111                     $type = PDO::PARAM_STR;
       
   112                 }
       
   113             }
       
   114             return $this->_stmt->bindParam($parameter, $variable, $type, $length, $options);
       
   115         } catch (PDOException $e) {
       
   116             require_once 'Zend/Db/Statement/Exception.php';
       
   117             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
       
   118         }
       
   119     }
       
   120 
       
   121     /**
       
   122      * Binds a value to a parameter.
       
   123      *
       
   124      * @param mixed $parameter Name the parameter, either integer or string.
       
   125      * @param mixed $value     Scalar value to bind to the parameter.
       
   126      * @param mixed $type      OPTIONAL Datatype of the parameter.
       
   127      * @return bool
       
   128      * @throws Zend_Db_Statement_Exception
       
   129      */
       
   130     public function bindValue($parameter, $value, $type = null)
       
   131     {
       
   132         if (is_string($parameter) && $parameter[0] != ':') {
       
   133             $parameter = ":$parameter";
       
   134         }
       
   135 
       
   136         $this->_bindParam[$parameter] = $value;
       
   137 
       
   138         try {
       
   139             if ($type === null) {
       
   140                 return $this->_stmt->bindValue($parameter, $value);
       
   141             } else {
       
   142                 return $this->_stmt->bindValue($parameter, $value, $type);
       
   143             }
       
   144         } catch (PDOException $e) {
       
   145             require_once 'Zend/Db/Statement/Exception.php';
       
   146             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
       
   147         }
       
   148     }
       
   149 
       
   150     /**
       
   151      * Closes the cursor, allowing the statement to be executed again.
       
   152      *
       
   153      * @return bool
       
   154      * @throws Zend_Db_Statement_Exception
       
   155      */
       
   156     public function closeCursor()
       
   157     {
       
   158         try {
       
   159             return $this->_stmt->closeCursor();
       
   160         } catch (PDOException $e) {
       
   161             require_once 'Zend/Db/Statement/Exception.php';
       
   162             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
       
   163         }
       
   164     }
       
   165 
       
   166     /**
       
   167      * Returns the number of columns in the result set.
       
   168      * Returns null if the statement has no result set metadata.
       
   169      *
       
   170      * @return int The number of columns.
       
   171      * @throws Zend_Db_Statement_Exception
       
   172      */
       
   173     public function columnCount()
       
   174     {
       
   175         try {
       
   176             return $this->_stmt->columnCount();
       
   177         } catch (PDOException $e) {
       
   178             require_once 'Zend/Db/Statement/Exception.php';
       
   179             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
       
   180         }
       
   181     }
       
   182 
       
   183     /**
       
   184      * Retrieves the error code, if any, associated with the last operation on
       
   185      * the statement handle.
       
   186      *
       
   187      * @return string error code.
       
   188      * @throws Zend_Db_Statement_Exception
       
   189      */
       
   190     public function errorCode()
       
   191     {
       
   192         try {
       
   193             return $this->_stmt->errorCode();
       
   194         } catch (PDOException $e) {
       
   195             require_once 'Zend/Db/Statement/Exception.php';
       
   196             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
       
   197         }
       
   198     }
       
   199 
       
   200     /**
       
   201      * Retrieves an array of error information, if any, associated with the
       
   202      * last operation on the statement handle.
       
   203      *
       
   204      * @return array
       
   205      * @throws Zend_Db_Statement_Exception
       
   206      */
       
   207     public function errorInfo()
       
   208     {
       
   209         try {
       
   210             return $this->_stmt->errorInfo();
       
   211         } catch (PDOException $e) {
       
   212             require_once 'Zend/Db/Statement/Exception.php';
       
   213             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
       
   214         }
       
   215     }
       
   216 
       
   217     /**
       
   218      * Executes a prepared statement.
       
   219      *
       
   220      * @param array $params OPTIONAL Values to bind to parameter placeholders.
       
   221      * @return bool
       
   222      * @throws Zend_Db_Statement_Exception
       
   223      */
       
   224     public function _execute(array $params = null)
       
   225     {
       
   226         try {
       
   227             if ($params !== null) {
       
   228                 return $this->_stmt->execute($params);
       
   229             } else {
       
   230                 return $this->_stmt->execute();
       
   231             }
       
   232         } catch (PDOException $e) {
       
   233             require_once 'Zend/Db/Statement/Exception.php';
       
   234             throw new Zend_Db_Statement_Exception($e->getMessage(), (int) $e->getCode(), $e);
       
   235         }
       
   236     }
       
   237 
       
   238     /**
       
   239      * Fetches a row from the result set.
       
   240      *
       
   241      * @param int $style  OPTIONAL Fetch mode for this fetch operation.
       
   242      * @param int $cursor OPTIONAL Absolute, relative, or other.
       
   243      * @param int $offset OPTIONAL Number for absolute or relative cursors.
       
   244      * @return mixed Array, object, or scalar depending on fetch mode.
       
   245      * @throws Zend_Db_Statement_Exception
       
   246      */
       
   247     public function fetch($style = null, $cursor = null, $offset = null)
       
   248     {
       
   249         if ($style === null) {
       
   250             $style = $this->_fetchMode;
       
   251         }
       
   252         try {
       
   253             return $this->_stmt->fetch($style, $cursor, $offset);
       
   254         } catch (PDOException $e) {
       
   255             require_once 'Zend/Db/Statement/Exception.php';
       
   256             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
       
   257         }
       
   258     }
       
   259 
       
   260     /**
       
   261      * Required by IteratorAggregate interface
       
   262      *
       
   263      * @return IteratorIterator
       
   264      */
       
   265     public function getIterator()
       
   266     {
       
   267         return new IteratorIterator($this->_stmt);
       
   268     }
       
   269 
       
   270     /**
       
   271      * Returns an array containing all of the result set rows.
       
   272      *
       
   273      * @param int $style OPTIONAL Fetch mode.
       
   274      * @param int $col   OPTIONAL Column number, if fetch mode is by column.
       
   275      * @return array Collection of rows, each in a format by the fetch mode.
       
   276      * @throws Zend_Db_Statement_Exception
       
   277      */
       
   278     public function fetchAll($style = null, $col = null)
       
   279     {
       
   280         if ($style === null) {
       
   281             $style = $this->_fetchMode;
       
   282         }
       
   283         try {
       
   284             if ($style == PDO::FETCH_COLUMN) {
       
   285                 if ($col === null) {
       
   286                     $col = 0;
       
   287                 }
       
   288                 return $this->_stmt->fetchAll($style, $col);
       
   289             } else {
       
   290                 return $this->_stmt->fetchAll($style);
       
   291             }
       
   292         } catch (PDOException $e) {
       
   293             require_once 'Zend/Db/Statement/Exception.php';
       
   294             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
       
   295         }
       
   296     }
       
   297 
       
   298     /**
       
   299      * Returns a single column from the next row of a result set.
       
   300      *
       
   301      * @param int $col OPTIONAL Position of the column to fetch.
       
   302      * @return string
       
   303      * @throws Zend_Db_Statement_Exception
       
   304      */
       
   305     public function fetchColumn($col = 0)
       
   306     {
       
   307         try {
       
   308             return $this->_stmt->fetchColumn($col);
       
   309         } catch (PDOException $e) {
       
   310             require_once 'Zend/Db/Statement/Exception.php';
       
   311             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
       
   312         }
       
   313     }
       
   314 
       
   315     /**
       
   316      * Fetches the next row and returns it as an object.
       
   317      *
       
   318      * @param string $class  OPTIONAL Name of the class to create.
       
   319      * @param array  $config OPTIONAL Constructor arguments for the class.
       
   320      * @return mixed One object instance of the specified class.
       
   321      * @throws Zend_Db_Statement_Exception
       
   322      */
       
   323     public function fetchObject($class = 'stdClass', array $config = array())
       
   324     {
       
   325         try {
       
   326             return $this->_stmt->fetchObject($class, $config);
       
   327         } catch (PDOException $e) {
       
   328             require_once 'Zend/Db/Statement/Exception.php';
       
   329             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
       
   330         }
       
   331     }
       
   332 
       
   333     /**
       
   334      * Retrieve a statement attribute.
       
   335      *
       
   336      * @param integer $key Attribute name.
       
   337      * @return mixed      Attribute value.
       
   338      * @throws Zend_Db_Statement_Exception
       
   339      */
       
   340     public function getAttribute($key)
       
   341     {
       
   342         try {
       
   343             return $this->_stmt->getAttribute($key);
       
   344         } catch (PDOException $e) {
       
   345             require_once 'Zend/Db/Statement/Exception.php';
       
   346             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
       
   347         }
       
   348     }
       
   349 
       
   350     /**
       
   351      * Returns metadata for a column in a result set.
       
   352      *
       
   353      * @param int $column
       
   354      * @return mixed
       
   355      * @throws Zend_Db_Statement_Exception
       
   356      */
       
   357     public function getColumnMeta($column)
       
   358     {
       
   359         try {
       
   360             return $this->_stmt->getColumnMeta($column);
       
   361         } catch (PDOException $e) {
       
   362             require_once 'Zend/Db/Statement/Exception.php';
       
   363             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
       
   364         }
       
   365     }
       
   366 
       
   367     /**
       
   368      * Retrieves the next rowset (result set) for a SQL statement that has
       
   369      * multiple result sets.  An example is a stored procedure that returns
       
   370      * the results of multiple queries.
       
   371      *
       
   372      * @return bool
       
   373      * @throws Zend_Db_Statement_Exception
       
   374      */
       
   375     public function nextRowset()
       
   376     {
       
   377         try {
       
   378             return $this->_stmt->nextRowset();
       
   379         } catch (PDOException $e) {
       
   380             require_once 'Zend/Db/Statement/Exception.php';
       
   381             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
       
   382         }
       
   383     }
       
   384 
       
   385     /**
       
   386      * Returns the number of rows affected by the execution of the
       
   387      * last INSERT, DELETE, or UPDATE statement executed by this
       
   388      * statement object.
       
   389      *
       
   390      * @return int     The number of rows affected.
       
   391      * @throws Zend_Db_Statement_Exception
       
   392      */
       
   393     public function rowCount()
       
   394     {
       
   395         try {
       
   396             return $this->_stmt->rowCount();
       
   397         } catch (PDOException $e) {
       
   398             require_once 'Zend/Db/Statement/Exception.php';
       
   399             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
       
   400         }
       
   401     }
       
   402 
       
   403     /**
       
   404      * Set a statement attribute.
       
   405      *
       
   406      * @param string $key Attribute name.
       
   407      * @param mixed  $val Attribute value.
       
   408      * @return bool
       
   409      * @throws Zend_Db_Statement_Exception
       
   410      */
       
   411     public function setAttribute($key, $val)
       
   412     {
       
   413         try {
       
   414             return $this->_stmt->setAttribute($key, $val);
       
   415         } catch (PDOException $e) {
       
   416             require_once 'Zend/Db/Statement/Exception.php';
       
   417             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
       
   418         }
       
   419     }
       
   420 
       
   421     /**
       
   422      * Set the default fetch mode for this statement.
       
   423      *
       
   424      * @param int   $mode The fetch mode.
       
   425      * @return bool
       
   426      * @throws Zend_Db_Statement_Exception
       
   427      */
       
   428     public function setFetchMode($mode)
       
   429     {
       
   430         $this->_fetchMode = $mode;
       
   431         try {
       
   432             return $this->_stmt->setFetchMode($mode);
       
   433         } catch (PDOException $e) {
       
   434             require_once 'Zend/Db/Statement/Exception.php';
       
   435             throw new Zend_Db_Statement_Exception($e->getMessage(), $e->getCode(), $e);
       
   436         }
       
   437     }
       
   438 
       
   439 }