Ibm
2 years ago
Abstract.php
2 years ago
Ibm.php
2 years ago
Mssql.php
2 years ago
Mysql.php
1 year ago
Oci.php
2 years ago
Pgsql.php
2 years ago
Sqlite.php
2 years ago
Ibm.php
312 lines
| 1 | <?php |
| 2 | |
| 3 | namespace { |
| 4 | /** |
| 5 | * Zend Framework |
| 6 | * |
| 7 | * LICENSE |
| 8 | * |
| 9 | * This source file is subject to the new BSD license that is bundled |
| 10 | * with this package in the file LICENSE.txt. |
| 11 | * It is also available through the world-wide-web at this URL: |
| 12 | * http://framework.zend.com/license/new-bsd |
| 13 | * If you did not receive a copy of the license and are unable to |
| 14 | * obtain it through the world-wide-web, please send an email |
| 15 | * to license@zend.com so we can send you a copy immediately. |
| 16 | * |
| 17 | * @category Zend |
| 18 | * @package Zend_Db |
| 19 | * @subpackage Adapter |
| 20 | * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) |
| 21 | * @license http://framework.zend.com/license/new-bsd New BSD License |
| 22 | * @version $Id: Ibm.php 23775 2011-03-01 17:25:24Z ralph $ |
| 23 | */ |
| 24 | /** @see Zend_Db_Adapter_Pdo_Abstract */ |
| 25 | // require_once 'Zend/Db/Adapter/Pdo/Abstract.php'; |
| 26 | /** @see Zend_Db_Abstract_Pdo_Ibm_Db2 */ |
| 27 | // require_once 'Zend/Db/Adapter/Pdo/Ibm/Db2.php'; |
| 28 | /** @see Zend_Db_Abstract_Pdo_Ibm_Ids */ |
| 29 | // require_once 'Zend/Db/Adapter/Pdo/Ibm/Ids.php'; |
| 30 | /** @see Zend_Db_Statement_Pdo_Ibm */ |
| 31 | // require_once 'Zend/Db/Statement/Pdo/Ibm.php'; |
| 32 | /** |
| 33 | * @category Zend |
| 34 | * @package Zend_Db |
| 35 | * @subpackage Adapter |
| 36 | * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) |
| 37 | * @license http://framework.zend.com/license/new-bsd New BSD License |
| 38 | */ |
| 39 | class Zend_Db_Adapter_Pdo_Ibm extends \Zend_Db_Adapter_Pdo_Abstract |
| 40 | { |
| 41 | /** |
| 42 | * PDO type. |
| 43 | * |
| 44 | * @var string |
| 45 | */ |
| 46 | protected $_pdoType = 'ibm'; |
| 47 | /** |
| 48 | * The IBM data server connected to |
| 49 | * |
| 50 | * @var string |
| 51 | */ |
| 52 | protected $_serverType = null; |
| 53 | /** |
| 54 | * Keys are UPPERCASE SQL datatypes or the constants |
| 55 | * Zend_Db::INT_TYPE, Zend_Db::BIGINT_TYPE, or Zend_Db::FLOAT_TYPE. |
| 56 | * |
| 57 | * Values are: |
| 58 | * 0 = 32-bit integer |
| 59 | * 1 = 64-bit integer |
| 60 | * 2 = float or decimal |
| 61 | * |
| 62 | * @var array Associative array of datatypes to values 0, 1, or 2. |
| 63 | */ |
| 64 | protected $_numericDataTypes = array(\Zend_Db::INT_TYPE => \Zend_Db::INT_TYPE, \Zend_Db::BIGINT_TYPE => \Zend_Db::BIGINT_TYPE, \Zend_Db::FLOAT_TYPE => \Zend_Db::FLOAT_TYPE, 'INTEGER' => \Zend_Db::INT_TYPE, 'SMALLINT' => \Zend_Db::INT_TYPE, 'BIGINT' => \Zend_Db::BIGINT_TYPE, 'DECIMAL' => \Zend_Db::FLOAT_TYPE, 'DEC' => \Zend_Db::FLOAT_TYPE, 'REAL' => \Zend_Db::FLOAT_TYPE, 'NUMERIC' => \Zend_Db::FLOAT_TYPE, 'DOUBLE PRECISION' => \Zend_Db::FLOAT_TYPE, 'FLOAT' => \Zend_Db::FLOAT_TYPE); |
| 65 | /** |
| 66 | * Creates a PDO object and connects to the database. |
| 67 | * |
| 68 | * The IBM data server is set. |
| 69 | * Current options are DB2 or IDS |
| 70 | * @todo also differentiate between z/OS and i/5 |
| 71 | * |
| 72 | * @return void |
| 73 | * @throws Zend_Db_Adapter_Exception |
| 74 | */ |
| 75 | public function _connect() |
| 76 | { |
| 77 | if ($this->_connection) { |
| 78 | return; |
| 79 | } |
| 80 | parent::_connect(); |
| 81 | $this->getConnection()->setAttribute(\Zend_Db::ATTR_STRINGIFY_FETCHES, \true); |
| 82 | try { |
| 83 | if ($this->_serverType === null) { |
| 84 | $server = \substr($this->getConnection()->getAttribute(\PDO::ATTR_SERVER_INFO), 0, 3); |
| 85 | switch ($server) { |
| 86 | case 'DB2': |
| 87 | $this->_serverType = new \Zend_Db_Adapter_Pdo_Ibm_Db2($this); |
| 88 | // Add DB2-specific numeric types |
| 89 | $this->_numericDataTypes['DECFLOAT'] = \Zend_Db::FLOAT_TYPE; |
| 90 | $this->_numericDataTypes['DOUBLE'] = \Zend_Db::FLOAT_TYPE; |
| 91 | $this->_numericDataTypes['NUM'] = \Zend_Db::FLOAT_TYPE; |
| 92 | break; |
| 93 | case 'IDS': |
| 94 | $this->_serverType = new \Zend_Db_Adapter_Pdo_Ibm_Ids($this); |
| 95 | // Add IDS-specific numeric types |
| 96 | $this->_numericDataTypes['SERIAL'] = \Zend_Db::INT_TYPE; |
| 97 | $this->_numericDataTypes['SERIAL8'] = \Zend_Db::BIGINT_TYPE; |
| 98 | $this->_numericDataTypes['INT8'] = \Zend_Db::BIGINT_TYPE; |
| 99 | $this->_numericDataTypes['SMALLFLOAT'] = \Zend_Db::FLOAT_TYPE; |
| 100 | $this->_numericDataTypes['MONEY'] = \Zend_Db::FLOAT_TYPE; |
| 101 | break; |
| 102 | } |
| 103 | } |
| 104 | } catch (\PDOException $e) { |
| 105 | /** @see Zend_Db_Adapter_Exception */ |
| 106 | // require_once 'Zend/Db/Adapter/Exception.php'; |
| 107 | $error = \strpos($e->getMessage(), 'driver does not support that attribute'); |
| 108 | if ($error) { |
| 109 | throw new \Zend_Db_Adapter_Exception("PDO_IBM driver extension is downlevel. Please use driver release version 1.2.1 or later", 0, $e); |
| 110 | } else { |
| 111 | throw new \Zend_Db_Adapter_Exception($e->getMessage(), $e->getCode(), $e); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | /** |
| 116 | * Creates a PDO DSN for the adapter from $this->_config settings. |
| 117 | * |
| 118 | * @return string |
| 119 | */ |
| 120 | protected function _dsn() |
| 121 | { |
| 122 | $this->_checkRequiredOptions($this->_config); |
| 123 | // check if using full connection string |
| 124 | if (\array_key_exists('host', $this->_config)) { |
| 125 | $dsn = ';DATABASE=' . $this->_config['dbname'] . ';HOSTNAME=' . $this->_config['host'] . ';PORT=' . $this->_config['port'] . ';PROTOCOL=' . 'TCPIP;'; |
| 126 | } else { |
| 127 | // catalogued connection |
| 128 | $dsn = $this->_config['dbname']; |
| 129 | } |
| 130 | return $this->_pdoType . ': ' . $dsn; |
| 131 | } |
| 132 | /** |
| 133 | * Checks required options |
| 134 | * |
| 135 | * @param array $config |
| 136 | * @throws Zend_Db_Adapter_Exception |
| 137 | * @return void |
| 138 | */ |
| 139 | protected function _checkRequiredOptions(array $config) |
| 140 | { |
| 141 | parent::_checkRequiredOptions($config); |
| 142 | if (\array_key_exists('host', $this->_config) && !\array_key_exists('port', $config)) { |
| 143 | /** @see Zend_Db_Adapter_Exception */ |
| 144 | // require_once 'Zend/Db/Adapter/Exception.php'; |
| 145 | throw new \Zend_Db_Adapter_Exception("Configuration must have a key for 'port' when 'host' is specified"); |
| 146 | } |
| 147 | } |
| 148 | /** |
| 149 | * Prepares an SQL statement. |
| 150 | * |
| 151 | * @param string $sql The SQL statement with placeholders. |
| 152 | * @param array $bind An array of data to bind to the placeholders. |
| 153 | * @return PDOStatement |
| 154 | */ |
| 155 | public function prepare($sql) |
| 156 | { |
| 157 | $this->_connect(); |
| 158 | $stmtClass = $this->_defaultStmtClass; |
| 159 | $stmt = new $stmtClass($this, $sql); |
| 160 | $stmt->setFetchMode($this->_fetchMode); |
| 161 | return $stmt; |
| 162 | } |
| 163 | /** |
| 164 | * Returns a list of the tables in the database. |
| 165 | * |
| 166 | * @return array |
| 167 | */ |
| 168 | public function listTables() |
| 169 | { |
| 170 | $this->_connect(); |
| 171 | return $this->_serverType->listTables(); |
| 172 | } |
| 173 | /** |
| 174 | * Returns the column descriptions for a table. |
| 175 | * |
| 176 | * The return value is an associative array keyed by the column name, |
| 177 | * as returned by the RDBMS. |
| 178 | * |
| 179 | * The value of each array element is an associative array |
| 180 | * with the following keys: |
| 181 | * |
| 182 | * SCHEMA_NAME => string; name of database or schema |
| 183 | * TABLE_NAME => string; |
| 184 | * COLUMN_NAME => string; column name |
| 185 | * COLUMN_POSITION => number; ordinal position of column in table |
| 186 | * DATA_TYPE => string; SQL datatype name of column |
| 187 | * DEFAULT => string; default expression of column, null if none |
| 188 | * NULLABLE => boolean; true if column can have nulls |
| 189 | * LENGTH => number; length of CHAR/VARCHAR |
| 190 | * SCALE => number; scale of NUMERIC/DECIMAL |
| 191 | * PRECISION => number; precision of NUMERIC/DECIMAL |
| 192 | * UNSIGNED => boolean; unsigned property of an integer type |
| 193 | * PRIMARY => boolean; true if column is part of the primary key |
| 194 | * PRIMARY_POSITION => integer; position of column in primary key |
| 195 | * |
| 196 | * @todo Discover integer unsigned property. |
| 197 | * |
| 198 | * @param string $tableName |
| 199 | * @param string $schemaName OPTIONAL |
| 200 | * @return array |
| 201 | */ |
| 202 | public function describeTable($tableName, $schemaName = null) |
| 203 | { |
| 204 | $this->_connect(); |
| 205 | return $this->_serverType->describeTable($tableName, $schemaName); |
| 206 | } |
| 207 | /** |
| 208 | * Inserts a table row with specified data. |
| 209 | * Special handling for PDO_IBM |
| 210 | * remove empty slots |
| 211 | * |
| 212 | * @param mixed $table The table to insert data into. |
| 213 | * @param array $bind Column-value pairs. |
| 214 | * @return int The number of affected rows. |
| 215 | */ |
| 216 | public function insert($table, array $bind) |
| 217 | { |
| 218 | $this->_connect(); |
| 219 | $newbind = array(); |
| 220 | if (\is_array($bind)) { |
| 221 | foreach ($bind as $name => $value) { |
| 222 | if ($value !== null) { |
| 223 | $newbind[$name] = $value; |
| 224 | } |
| 225 | } |
| 226 | } |
| 227 | return parent::insert($table, $newbind); |
| 228 | } |
| 229 | /** |
| 230 | * Adds an adapter-specific LIMIT clause to the SELECT statement. |
| 231 | * |
| 232 | * @param string $sql |
| 233 | * @param integer $count |
| 234 | * @param integer $offset OPTIONAL |
| 235 | * @return string |
| 236 | */ |
| 237 | public function limit($sql, $count, $offset = 0) |
| 238 | { |
| 239 | $this->_connect(); |
| 240 | return $this->_serverType->limit($sql, $count, $offset); |
| 241 | } |
| 242 | /** |
| 243 | * Gets the last ID generated automatically by an IDENTITY/AUTOINCREMENT |
| 244 | * column. |
| 245 | * |
| 246 | * @param string $tableName OPTIONAL |
| 247 | * @param string $primaryKey OPTIONAL |
| 248 | * @return integer |
| 249 | */ |
| 250 | public function lastInsertId($tableName = null, $primaryKey = null) |
| 251 | { |
| 252 | $this->_connect(); |
| 253 | if ($tableName !== null) { |
| 254 | $sequenceName = $tableName; |
| 255 | if ($primaryKey) { |
| 256 | $sequenceName .= "_{$primaryKey}"; |
| 257 | } |
| 258 | $sequenceName .= '_seq'; |
| 259 | return $this->lastSequenceId($sequenceName); |
| 260 | } |
| 261 | $id = $this->getConnection()->lastInsertId(); |
| 262 | return $id; |
| 263 | } |
| 264 | /** |
| 265 | * Return the most recent value from the specified sequence in the database. |
| 266 | * |
| 267 | * @param string $sequenceName |
| 268 | * @return integer |
| 269 | */ |
| 270 | public function lastSequenceId($sequenceName) |
| 271 | { |
| 272 | $this->_connect(); |
| 273 | return $this->_serverType->lastSequenceId($sequenceName); |
| 274 | } |
| 275 | /** |
| 276 | * Generate a new value from the specified sequence in the database, |
| 277 | * and return it. |
| 278 | * |
| 279 | * @param string $sequenceName |
| 280 | * @return integer |
| 281 | */ |
| 282 | public function nextSequenceId($sequenceName) |
| 283 | { |
| 284 | $this->_connect(); |
| 285 | return $this->_serverType->nextSequenceId($sequenceName); |
| 286 | } |
| 287 | /** |
| 288 | * Retrieve server version in PHP style |
| 289 | * Pdo_Idm doesn't support getAttribute(PDO::ATTR_SERVER_VERSION) |
| 290 | * @return string |
| 291 | */ |
| 292 | public function getServerVersion() |
| 293 | { |
| 294 | try { |
| 295 | $stmt = $this->query('SELECT service_level, fixpack_num FROM TABLE (sysproc.env_get_inst_info()) as INSTANCEINFO'); |
| 296 | $result = $stmt->fetchAll(\Zend_Db::FETCH_NUM); |
| 297 | if (\count($result)) { |
| 298 | $matches = null; |
| 299 | if (\preg_match('/((?:[0-9]{1,2}\\.){1,3}[0-9]{1,2})/', $result[0][0], $matches)) { |
| 300 | return $matches[1]; |
| 301 | } else { |
| 302 | return null; |
| 303 | } |
| 304 | } |
| 305 | return null; |
| 306 | } catch (\PDOException $e) { |
| 307 | return null; |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 |