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

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 Adapter
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_Adapter_Pdo_Abstract */
0025 // require_once 'Zend/Db/Adapter/Pdo/Abstract.php';
0026 
0027 /** @see Zend_Db_Abstract_Pdo_Ibm_Db2 */
0028 // require_once 'Zend/Db/Adapter/Pdo/Ibm/Db2.php';
0029 
0030 /** @see Zend_Db_Abstract_Pdo_Ibm_Ids */
0031 // require_once 'Zend/Db/Adapter/Pdo/Ibm/Ids.php';
0032 
0033 /** @see Zend_Db_Statement_Pdo_Ibm */
0034 // require_once 'Zend/Db/Statement/Pdo/Ibm.php';
0035 
0036 
0037 /**
0038  * @category   Zend
0039  * @package    Zend_Db
0040  * @subpackage Adapter
0041  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0042  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0043  */
0044 class Zend_Db_Adapter_Pdo_Ibm extends Zend_Db_Adapter_Pdo_Abstract
0045 {
0046     /**
0047      * PDO type.
0048      *
0049      * @var string
0050      */
0051     protected $_pdoType = 'ibm';
0052 
0053     /**
0054      * The IBM data server connected to
0055      *
0056      * @var string
0057      */
0058     protected $_serverType = null;
0059 
0060     /**
0061      * Keys are UPPERCASE SQL datatypes or the constants
0062      * Zend_Db::INT_TYPE, Zend_Db::BIGINT_TYPE, or Zend_Db::FLOAT_TYPE.
0063      *
0064      * Values are:
0065      * 0 = 32-bit integer
0066      * 1 = 64-bit integer
0067      * 2 = float or decimal
0068      *
0069      * @var array Associative array of datatypes to values 0, 1, or 2.
0070      */
0071     protected $_numericDataTypes = array(
0072                         Zend_Db::INT_TYPE    => Zend_Db::INT_TYPE,
0073                         Zend_Db::BIGINT_TYPE => Zend_Db::BIGINT_TYPE,
0074                         Zend_Db::FLOAT_TYPE  => Zend_Db::FLOAT_TYPE,
0075                         'INTEGER'            => Zend_Db::INT_TYPE,
0076                         'SMALLINT'           => Zend_Db::INT_TYPE,
0077                         'BIGINT'             => Zend_Db::BIGINT_TYPE,
0078                         'DECIMAL'            => Zend_Db::FLOAT_TYPE,
0079                         'DEC'                => Zend_Db::FLOAT_TYPE,
0080                         'REAL'               => Zend_Db::FLOAT_TYPE,
0081                         'NUMERIC'            => Zend_Db::FLOAT_TYPE,
0082                         'DOUBLE PRECISION'   => Zend_Db::FLOAT_TYPE,
0083                         'FLOAT'              => Zend_Db::FLOAT_TYPE
0084                         );
0085 
0086     /**
0087      * Creates a PDO object and connects to the database.
0088      *
0089      * The IBM data server is set.
0090      * Current options are DB2 or IDS
0091      * @todo also differentiate between z/OS and i/5
0092      *
0093      * @return void
0094      * @throws Zend_Db_Adapter_Exception
0095      */
0096     public function _connect()
0097     {
0098         if ($this->_connection) {
0099             return;
0100         }
0101         parent::_connect();
0102 
0103         $this->getConnection()->setAttribute(Zend_Db::ATTR_STRINGIFY_FETCHES, true);
0104 
0105         try {
0106             if ($this->_serverType === null) {
0107                 $server = substr($this->getConnection()->getAttribute(PDO::ATTR_SERVER_INFO), 0, 3);
0108 
0109                 switch ($server) {
0110                     case 'DB2':
0111                         $this->_serverType = new Zend_Db_Adapter_Pdo_Ibm_Db2($this);
0112 
0113                         // Add DB2-specific numeric types
0114                         $this->_numericDataTypes['DECFLOAT'] = Zend_Db::FLOAT_TYPE;
0115                         $this->_numericDataTypes['DOUBLE']   = Zend_Db::FLOAT_TYPE;
0116                         $this->_numericDataTypes['NUM']      = Zend_Db::FLOAT_TYPE;
0117 
0118                         break;
0119                     case 'IDS':
0120                         $this->_serverType = new Zend_Db_Adapter_Pdo_Ibm_Ids($this);
0121 
0122                         // Add IDS-specific numeric types
0123                         $this->_numericDataTypes['SERIAL']       = Zend_Db::INT_TYPE;
0124                         $this->_numericDataTypes['SERIAL8']      = Zend_Db::BIGINT_TYPE;
0125                         $this->_numericDataTypes['INT8']         = Zend_Db::BIGINT_TYPE;
0126                         $this->_numericDataTypes['SMALLFLOAT']   = Zend_Db::FLOAT_TYPE;
0127                         $this->_numericDataTypes['MONEY']        = Zend_Db::FLOAT_TYPE;
0128 
0129                         break;
0130                     }
0131             }
0132         } catch (PDOException $e) {
0133             /** @see Zend_Db_Adapter_Exception */
0134             // require_once 'Zend/Db/Adapter/Exception.php';
0135             $error = strpos($e->getMessage(), 'driver does not support that attribute');
0136             if ($error) {
0137                 throw new Zend_Db_Adapter_Exception("PDO_IBM driver extension is downlevel.  Please use driver release version 1.2.1 or later", 0, $e);
0138             } else {
0139                 throw new Zend_Db_Adapter_Exception($e->getMessage(), $e->getCode(), $e);
0140             }
0141         }
0142     }
0143 
0144     /**
0145      * Creates a PDO DSN for the adapter from $this->_config settings.
0146      *
0147      * @return string
0148      */
0149     protected function _dsn()
0150     {
0151         $this->_checkRequiredOptions($this->_config);
0152 
0153         // check if using full connection string
0154         if (array_key_exists('host', $this->_config)) {
0155             $dsn = ';DATABASE=' . $this->_config['dbname']
0156             . ';HOSTNAME=' . $this->_config['host']
0157             . ';PORT='     . $this->_config['port']
0158             // PDO_IBM supports only DB2 TCPIP protocol
0159             . ';PROTOCOL=' . 'TCPIP;';
0160         } else {
0161             // catalogued connection
0162             $dsn = $this->_config['dbname'];
0163         }
0164         return $this->_pdoType . ': ' . $dsn;
0165     }
0166 
0167     /**
0168      * Checks required options
0169      *
0170      * @param  array $config
0171      * @throws Zend_Db_Adapter_Exception
0172      * @return void
0173      */
0174     protected function _checkRequiredOptions(array $config)
0175     {
0176         parent::_checkRequiredOptions($config);
0177 
0178         if (array_key_exists('host', $this->_config) &&
0179         !array_key_exists('port', $config)) {
0180             /** @see Zend_Db_Adapter_Exception */
0181             // require_once 'Zend/Db/Adapter/Exception.php';
0182             throw new Zend_Db_Adapter_Exception("Configuration must have a key for 'port' when 'host' is specified");
0183         }
0184     }
0185 
0186     /**
0187      * Prepares an SQL statement.
0188      *
0189      * @param string $sql The SQL statement with placeholders.
0190      * @param array $bind An array of data to bind to the placeholders.
0191      * @return PDOStatement
0192      */
0193     public function prepare($sql)
0194     {
0195         $this->_connect();
0196         $stmtClass = $this->_defaultStmtClass;
0197         $stmt = new $stmtClass($this, $sql);
0198         $stmt->setFetchMode($this->_fetchMode);
0199         return $stmt;
0200     }
0201 
0202     /**
0203      * Returns a list of the tables in the database.
0204      *
0205      * @return array
0206      */
0207     public function listTables()
0208     {
0209         $this->_connect();
0210         return $this->_serverType->listTables();
0211     }
0212 
0213     /**
0214      * Returns the column descriptions for a table.
0215      *
0216      * The return value is an associative array keyed by the column name,
0217      * as returned by the RDBMS.
0218      *
0219      * The value of each array element is an associative array
0220      * with the following keys:
0221      *
0222      * SCHEMA_NAME      => string; name of database or schema
0223      * TABLE_NAME       => string;
0224      * COLUMN_NAME      => string; column name
0225      * COLUMN_POSITION  => number; ordinal position of column in table
0226      * DATA_TYPE        => string; SQL datatype name of column
0227      * DEFAULT          => string; default expression of column, null if none
0228      * NULLABLE         => boolean; true if column can have nulls
0229      * LENGTH           => number; length of CHAR/VARCHAR
0230      * SCALE            => number; scale of NUMERIC/DECIMAL
0231      * PRECISION        => number; precision of NUMERIC/DECIMAL
0232      * UNSIGNED         => boolean; unsigned property of an integer type
0233      * PRIMARY          => boolean; true if column is part of the primary key
0234      * PRIMARY_POSITION => integer; position of column in primary key
0235      *
0236      * @todo Discover integer unsigned property.
0237      *
0238      * @param string $tableName
0239      * @param string $schemaName OPTIONAL
0240      * @return array
0241      */
0242     public function describeTable($tableName, $schemaName = null)
0243     {
0244         $this->_connect();
0245         return $this->_serverType->describeTable($tableName, $schemaName);
0246     }
0247 
0248     /**
0249      * Inserts a table row with specified data.
0250      * Special handling for PDO_IBM
0251      * remove empty slots
0252      *
0253      * @param mixed $table The table to insert data into.
0254      * @param array $bind Column-value pairs.
0255      * @return int The number of affected rows.
0256      */
0257     public function insert($table, array $bind)
0258     {
0259         $this->_connect();
0260         $newbind = array();
0261         if (is_array($bind)) {
0262             foreach ($bind as $name => $value) {
0263                 if($value !== null) {
0264                     $newbind[$name] = $value;
0265                 }
0266             }
0267         }
0268 
0269         return parent::insert($table, $newbind);
0270     }
0271 
0272     /**
0273      * Adds an adapter-specific LIMIT clause to the SELECT statement.
0274      *
0275      * @param string $sql
0276      * @param integer $count
0277      * @param integer $offset OPTIONAL
0278      * @return string
0279      */
0280     public function limit($sql, $count, $offset = 0)
0281     {
0282        $this->_connect();
0283        return $this->_serverType->limit($sql, $count, $offset);
0284     }
0285 
0286     /**
0287      * Gets the last ID generated automatically by an IDENTITY/AUTOINCREMENT
0288      * column.
0289      *
0290      * @param string $tableName OPTIONAL
0291      * @param string $primaryKey OPTIONAL
0292      * @return integer
0293      */
0294     public function lastInsertId($tableName = null, $primaryKey = null)
0295     {
0296         $this->_connect();
0297 
0298          if ($tableName !== null) {
0299             $sequenceName = $tableName;
0300             if ($primaryKey) {
0301                 $sequenceName .= "_$primaryKey";
0302             }
0303             $sequenceName .= '_seq';
0304             return $this->lastSequenceId($sequenceName);
0305         }
0306 
0307         $id = $this->getConnection()->lastInsertId();
0308 
0309         return $id;
0310     }
0311 
0312     /**
0313      * Return the most recent value from the specified sequence in the database.
0314      *
0315      * @param string $sequenceName
0316      * @return integer
0317      */
0318     public function lastSequenceId($sequenceName)
0319     {
0320         $this->_connect();
0321         return $this->_serverType->lastSequenceId($sequenceName);
0322     }
0323 
0324     /**
0325      * Generate a new value from the specified sequence in the database,
0326      * and return it.
0327      *
0328      * @param string $sequenceName
0329      * @return integer
0330      */
0331     public function nextSequenceId($sequenceName)
0332     {
0333         $this->_connect();
0334         return $this->_serverType->nextSequenceId($sequenceName);
0335     }
0336 
0337     /**
0338      * Retrieve server version in PHP style
0339      * Pdo_Idm doesn't support getAttribute(PDO::ATTR_SERVER_VERSION)
0340      * @return string
0341      */
0342     public function getServerVersion()
0343     {
0344         try {
0345             $stmt = $this->query('SELECT service_level, fixpack_num FROM TABLE (sysproc.env_get_inst_info()) as INSTANCEINFO');
0346             $result = $stmt->fetchAll(Zend_Db::FETCH_NUM);
0347             if (count($result)) {
0348                 $matches = null;
0349                 if (preg_match('/((?:[0-9]{1,2}\.){1,3}[0-9]{1,2})/', $result[0][0], $matches)) {
0350                     return $matches[1];
0351                 } else {
0352                     return null;
0353                 }
0354             }
0355             return null;
0356         } catch (PDOException $e) {
0357             return null;
0358         }
0359     }
0360 }