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  * Extends for DB2 native adapter.
0030  *
0031  * @package    Zend_Db
0032  * @subpackage Statement
0033  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0034  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0035  */
0036 class Zend_Db_Statement_Db2 extends Zend_Db_Statement
0037 {
0038 
0039     /**
0040      * Column names.
0041      */
0042     protected $_keys;
0043 
0044     /**
0045      * Fetched result values.
0046      */
0047     protected $_values;
0048 
0049     /**
0050      * Prepare a statement handle.
0051      *
0052      * @param string $sql
0053      * @return void
0054      * @throws Zend_Db_Statement_Db2_Exception
0055      */
0056     public function _prepare($sql)
0057     {
0058         $connection = $this->_adapter->getConnection();
0059 
0060         // db2_prepare on i5 emits errors, these need to be
0061         // suppressed so that proper exceptions can be thrown
0062         $this->_stmt = @db2_prepare($connection, $sql);
0063 
0064         if (!$this->_stmt) {
0065             /**
0066              * @see Zend_Db_Statement_Db2_Exception
0067              */
0068             // require_once 'Zend/Db/Statement/Db2/Exception.php';
0069             throw new Zend_Db_Statement_Db2_Exception(
0070                 db2_stmt_errormsg(),
0071                 db2_stmt_error()
0072             );
0073         }
0074     }
0075 
0076     /**
0077      * Binds a parameter to the specified variable name.
0078      *
0079      * @param mixed $parameter Name the parameter, either integer or string.
0080      * @param mixed $variable  Reference to PHP variable containing the value.
0081      * @param mixed $type      OPTIONAL Datatype of SQL parameter.
0082      * @param mixed $length    OPTIONAL Length of SQL parameter.
0083      * @param mixed $options   OPTIONAL Other options.
0084      * @return bool
0085      * @throws Zend_Db_Statement_Db2_Exception
0086      */
0087     public function _bindParam($parameter, &$variable, $type = null, $length = null, $options = null)
0088     {
0089         if ($type === null) {
0090             $type = DB2_PARAM_IN;
0091         }
0092 
0093         if (isset($options['data-type'])) {
0094             $datatype = $options['data-type'];
0095         } else {
0096             $datatype = DB2_CHAR;
0097         }
0098 
0099         if (!db2_bind_param($this->_stmt, $parameter, "variable", $type, $datatype)) {
0100             /**
0101              * @see Zend_Db_Statement_Db2_Exception
0102              */
0103             // require_once 'Zend/Db/Statement/Db2/Exception.php';
0104             throw new Zend_Db_Statement_Db2_Exception(
0105                 db2_stmt_errormsg(),
0106                 db2_stmt_error()
0107             );
0108         }
0109 
0110         return true;
0111     }
0112 
0113     /**
0114      * Closes the cursor, allowing the statement to be executed again.
0115      *
0116      * @return bool
0117      */
0118     public function closeCursor()
0119     {
0120         if (!$this->_stmt) {
0121             return false;
0122         }
0123         db2_free_stmt($this->_stmt);
0124         $this->_stmt = false;
0125         return true;
0126     }
0127 
0128 
0129     /**
0130      * Returns the number of columns in the result set.
0131      * Returns null if the statement has no result set metadata.
0132      *
0133      * @return int The number of columns.
0134      */
0135     public function columnCount()
0136     {
0137         if (!$this->_stmt) {
0138             return false;
0139         }
0140         return db2_num_fields($this->_stmt);
0141     }
0142 
0143     /**
0144      * Retrieves the error code, if any, associated with the last operation on
0145      * the statement handle.
0146      *
0147      * @return string error code.
0148      */
0149     public function errorCode()
0150     {
0151         if (!$this->_stmt) {
0152             return false;
0153         }
0154 
0155         $error = db2_stmt_error();
0156         if ($error === '') {
0157             return false;
0158         }
0159 
0160         return $error;
0161     }
0162 
0163     /**
0164      * Retrieves an array of error information, if any, associated with the
0165      * last operation on the statement handle.
0166      *
0167      * @return array
0168      */
0169     public function errorInfo()
0170     {
0171         $error = $this->errorCode();
0172         if ($error === false){
0173             return false;
0174         }
0175 
0176         /*
0177          * Return three-valued array like PDO.  But DB2 does not distinguish
0178          * between SQLCODE and native RDBMS error code, so repeat the SQLCODE.
0179          */
0180         return array(
0181             $error,
0182             $error,
0183             db2_stmt_errormsg()
0184         );
0185     }
0186 
0187     /**
0188      * Executes a prepared statement.
0189      *
0190      * @param array $params OPTIONAL Values to bind to parameter placeholders.
0191      * @return bool
0192      * @throws Zend_Db_Statement_Db2_Exception
0193      */
0194     public function _execute(array $params = null)
0195     {
0196         if (!$this->_stmt) {
0197             return false;
0198         }
0199 
0200         $retval = true;
0201         if ($params !== null) {
0202             $retval = @db2_execute($this->_stmt, $params);
0203         } else {
0204             $retval = @db2_execute($this->_stmt);
0205         }
0206 
0207         if ($retval === false) {
0208             /**
0209              * @see Zend_Db_Statement_Db2_Exception
0210              */
0211             // require_once 'Zend/Db/Statement/Db2/Exception.php';
0212             throw new Zend_Db_Statement_Db2_Exception(
0213                 db2_stmt_errormsg(),
0214                 db2_stmt_error());
0215         }
0216 
0217         $this->_keys = array();
0218         if ($field_num = $this->columnCount()) {
0219             for ($i = 0; $i < $field_num; $i++) {
0220                 $name = db2_field_name($this->_stmt, $i);
0221                 $this->_keys[] = $name;
0222             }
0223         }
0224 
0225         $this->_values = array();
0226         if ($this->_keys) {
0227             $this->_values = array_fill(0, count($this->_keys), null);
0228         }
0229 
0230         return $retval;
0231     }
0232 
0233     /**
0234      * Fetches a row from the result set.
0235      *
0236      * @param int $style  OPTIONAL Fetch mode for this fetch operation.
0237      * @param int $cursor OPTIONAL Absolute, relative, or other.
0238      * @param int $offset OPTIONAL Number for absolute or relative cursors.
0239      * @return mixed Array, object, or scalar depending on fetch mode.
0240      * @throws Zend_Db_Statement_Db2_Exception
0241      */
0242     public function fetch($style = null, $cursor = null, $offset = null)
0243     {
0244         if (!$this->_stmt) {
0245             return false;
0246         }
0247 
0248         if ($style === null) {
0249             $style = $this->_fetchMode;
0250         }
0251 
0252         switch ($style) {
0253             case Zend_Db::FETCH_NUM :
0254                 $row = db2_fetch_array($this->_stmt);
0255                 break;
0256             case Zend_Db::FETCH_ASSOC :
0257                 $row = db2_fetch_assoc($this->_stmt);
0258                 break;
0259             case Zend_Db::FETCH_BOTH :
0260                 $row = db2_fetch_both($this->_stmt);
0261                 break;
0262             case Zend_Db::FETCH_OBJ :
0263                 $row = db2_fetch_object($this->_stmt);
0264                 break;
0265             case Zend_Db::FETCH_BOUND:
0266                 $row = db2_fetch_both($this->_stmt);
0267                 if ($row !== false) {
0268                     return $this->_fetchBound($row);
0269                 }
0270                 break;
0271             default:
0272                 /**
0273                  * @see Zend_Db_Statement_Db2_Exception
0274                  */
0275                 // require_once 'Zend/Db/Statement/Db2/Exception.php';
0276                 throw new Zend_Db_Statement_Db2_Exception("Invalid fetch mode '$style' specified");
0277                 break;
0278         }
0279 
0280         return $row;
0281     }
0282 
0283     /**
0284      * Fetches the next row and returns it as an object.
0285      *
0286      * @param string $class  OPTIONAL Name of the class to create.
0287      * @param array  $config OPTIONAL Constructor arguments for the class.
0288      * @return mixed One object instance of the specified class.
0289      */
0290     public function fetchObject($class = 'stdClass', array $config = array())
0291     {
0292         $obj = $this->fetch(Zend_Db::FETCH_OBJ);
0293         return $obj;
0294     }
0295 
0296     /**
0297      * Retrieves the next rowset (result set) for a SQL statement that has
0298      * multiple result sets.  An example is a stored procedure that returns
0299      * the results of multiple queries.
0300      *
0301      * @return bool
0302      * @throws Zend_Db_Statement_Db2_Exception
0303      */
0304     public function nextRowset()
0305     {
0306         /**
0307          * @see Zend_Db_Statement_Db2_Exception
0308          */
0309         // require_once 'Zend/Db/Statement/Db2/Exception.php';
0310         throw new Zend_Db_Statement_Db2_Exception(__FUNCTION__ . '() is not implemented');
0311     }
0312 
0313     /**
0314      * Returns the number of rows affected by the execution of the
0315      * last INSERT, DELETE, or UPDATE statement executed by this
0316      * statement object.
0317      *
0318      * @return int     The number of rows affected.
0319      */
0320     public function rowCount()
0321     {
0322         if (!$this->_stmt) {
0323             return false;
0324         }
0325 
0326         $num = @db2_num_rows($this->_stmt);
0327 
0328         if ($num === false) {
0329             return 0;
0330         }
0331 
0332         return $num;
0333     }
0334 
0335      /**
0336      * Returns an array containing all of the result set rows.
0337      *
0338      * @param int $style OPTIONAL Fetch mode.
0339      * @param int $col   OPTIONAL Column number, if fetch mode is by column.
0340      * @return array Collection of rows, each in a format by the fetch mode.
0341      *
0342      * Behaves like parent, but if limit()
0343      * is used, the final result removes the extra column
0344      * 'zend_db_rownum'
0345      */
0346     public function fetchAll($style = null, $col = null)
0347     {
0348         $data = parent::fetchAll($style, $col);
0349         $results = array();
0350         $remove = $this->_adapter->foldCase('ZEND_DB_ROWNUM');
0351 
0352         foreach ($data as $row) {
0353             if (is_array($row) && array_key_exists($remove, $row)) {
0354                 unset($row[$remove]);
0355             }
0356             $results[] = $row;
0357         }
0358         return $results;
0359     }
0360 }