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.
18 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
19 * @license http://framework.zend.com/license/new-bsd New BSD License
20 * @version $Id: Ibm.php 16203 2009-06-21 18:56:17Z thomas $
24 /** @see Zend_Db_Adapter_Pdo_Abstract */
25 require_once 'Zend/Db/Adapter/Pdo/Abstract.php';
27 /** @see Zend_Db_Abstract_Pdo_Ibm_Db2 */
28 require_once 'Zend/Db/Adapter/Pdo/Ibm/Db2.php';
30 /** @see Zend_Db_Abstract_Pdo_Ibm_Ids */
31 require_once 'Zend/Db/Adapter/Pdo/Ibm/Ids.php';
33 /** @see Zend_Db_Statement_Pdo_Ibm */
34 require_once 'Zend/Db/Statement/Pdo/Ibm.php';
41 * @copyright Copyright (c) 2005-2009 Zend Technologies Inc. (http://www.zend.com)
42 * @license http://framework.zend.com/license/new-bsd New BSD License
44 class Zend_Db_Adapter_Pdo_Ibm
extends Zend_Db_Adapter_Pdo_Abstract
51 protected $_pdoType = 'ibm';
54 * The IBM data server connected to
58 protected $_serverType = null;
61 * Keys are UPPERCASE SQL datatypes or the constants
62 * Zend_Db::INT_TYPE, Zend_Db::BIGINT_TYPE, or Zend_Db::FLOAT_TYPE.
67 * 2 = float or decimal
69 * @var array Associative array of datatypes to values 0, 1, or 2.
71 protected $_numericDataTypes = array(
72 Zend_Db
::INT_TYPE
=> Zend_Db
::INT_TYPE
,
73 Zend_Db
::BIGINT_TYPE
=> Zend_Db
::BIGINT_TYPE
,
74 Zend_Db
::FLOAT_TYPE
=> Zend_Db
::FLOAT_TYPE
,
75 'INTEGER' => Zend_Db
::INT_TYPE
,
76 'SMALLINT' => Zend_Db
::INT_TYPE
,
77 'BIGINT' => Zend_Db
::BIGINT_TYPE
,
78 'DECIMAL' => Zend_Db
::FLOAT_TYPE
,
79 'DEC' => Zend_Db
::FLOAT_TYPE
,
80 'REAL' => Zend_Db
::FLOAT_TYPE
,
81 'NUMERIC' => Zend_Db
::FLOAT_TYPE
,
82 'DOUBLE PRECISION' => Zend_Db
::FLOAT_TYPE
,
83 'FLOAT' => Zend_Db
::FLOAT_TYPE
87 * Creates a PDO object and connects to the database.
89 * The IBM data server is set.
90 * Current options are DB2 or IDS
91 * @todo also differentiate between z/OS and i/5
94 * @throws Zend_Db_Adapter_Exception
96 public function _connect()
98 if ($this->_connection
) {
103 $this->getConnection()->setAttribute(Zend_Db
::ATTR_STRINGIFY_FETCHES
, true);
106 if ($this->_serverType
=== null) {
107 $server = substr($this->getConnection()->getAttribute(PDO
::ATTR_SERVER_INFO
), 0, 3);
111 $this->_serverType
= new Zend_Db_Adapter_Pdo_Ibm_Db2($this);
113 // Add DB2-specific numeric types
114 $this->_numericDataTypes
['DECFLOAT'] = Zend_Db
::FLOAT_TYPE
;
115 $this->_numericDataTypes
['DOUBLE'] = Zend_Db
::FLOAT_TYPE
;
116 $this->_numericDataTypes
['NUM'] = Zend_Db
::FLOAT_TYPE
;
120 $this->_serverType
= new Zend_Db_Adapter_Pdo_Ibm_Ids($this);
122 // Add IDS-specific numeric types
123 $this->_numericDataTypes
['SERIAL'] = Zend_Db
::INT_TYPE
;
124 $this->_numericDataTypes
['SERIAL8'] = Zend_Db
::BIGINT_TYPE
;
125 $this->_numericDataTypes
['INT8'] = Zend_Db
::BIGINT_TYPE
;
126 $this->_numericDataTypes
['SMALLFLOAT'] = Zend_Db
::FLOAT_TYPE
;
127 $this->_numericDataTypes
['MONEY'] = Zend_Db
::FLOAT_TYPE
;
132 } catch (PDOException
$e) {
133 /** @see Zend_Db_Adapter_Exception */
134 require_once 'Zend/Db/Adapter/Exception.php';
135 $error = strpos($e->getMessage(), 'driver does not support that attribute');
137 throw new Zend_Db_Adapter_Exception("PDO_IBM driver extension is downlevel. Please use driver release version 1.2.1 or later");
139 throw new Zend_Db_Adapter_Exception($e->getMessage());
145 * Creates a PDO DSN for the adapter from $this->_config settings.
149 protected function _dsn()
151 $this->_checkRequiredOptions($this->_config
);
153 // check if using full connection string
154 if (array_key_exists('host', $this->_config
)) {
155 $dsn = ';DATABASE=' . $this->_config
['dbname']
156 . ';HOSTNAME=' . $this->_config
['host']
157 . ';PORT=' . $this->_config
['port']
158 // PDO_IBM supports only DB2 TCPIP protocol
159 . ';PROTOCOL=' . 'TCPIP;';
161 // catalogued connection
162 $dsn = $this->_config
['dbname'];
164 return $this->_pdoType
. ': ' . $dsn;
168 * Checks required options
170 * @param array $config
171 * @throws Zend_Db_Adapter_Exception
174 protected function _checkRequiredOptions(array $config)
176 parent
::_checkRequiredOptions($config);
178 if (array_key_exists('host', $this->_config
) &&
179 !array_key_exists('port', $config)) {
180 /** @see Zend_Db_Adapter_Exception */
181 require_once 'Zend/Db/Adapter/Exception.php';
182 throw new Zend_Db_Adapter_Exception("Configuration must have a key for 'port' when 'host' is specified");
187 * Prepares an SQL statement.
189 * @param string $sql The SQL statement with placeholders.
190 * @param array $bind An array of data to bind to the placeholders.
191 * @return PDOStatement
193 public function prepare($sql)
196 $stmtClass = $this->_defaultStmtClass
;
197 $stmt = new $stmtClass($this, $sql);
198 $stmt->setFetchMode($this->_fetchMode
);
203 * Returns a list of the tables in the database.
207 public function listTables()
210 return $this->_serverType
->listTables();
214 * Returns the column descriptions for a table.
216 * The return value is an associative array keyed by the column name,
217 * as returned by the RDBMS.
219 * The value of each array element is an associative array
220 * with the following keys:
222 * SCHEMA_NAME => string; name of database or schema
223 * TABLE_NAME => string;
224 * COLUMN_NAME => string; column name
225 * COLUMN_POSITION => number; ordinal position of column in table
226 * DATA_TYPE => string; SQL datatype name of column
227 * DEFAULT => string; default expression of column, null if none
228 * NULLABLE => boolean; true if column can have nulls
229 * LENGTH => number; length of CHAR/VARCHAR
230 * SCALE => number; scale of NUMERIC/DECIMAL
231 * PRECISION => number; precision of NUMERIC/DECIMAL
232 * UNSIGNED => boolean; unsigned property of an integer type
233 * PRIMARY => boolean; true if column is part of the primary key
234 * PRIMARY_POSITION => integer; position of column in primary key
236 * @todo Discover integer unsigned property.
238 * @param string $tableName
239 * @param string $schemaName OPTIONAL
242 public function describeTable($tableName, $schemaName = null)
245 return $this->_serverType
->describeTable($tableName, $schemaName);
249 * Inserts a table row with specified data.
250 * Special handling for PDO_IBM
253 * @param mixed $table The table to insert data into.
254 * @param array $bind Column-value pairs.
255 * @return int The number of affected rows.
257 public function insert($table, array $bind)
261 if (is_array($bind)) {
262 foreach ($bind as $name => $value) {
263 if($value !== null) {
264 $newbind[$name] = $value;
269 return parent
::insert($table, $newbind);
273 * Adds an adapter-specific LIMIT clause to the SELECT statement.
276 * @param integer $count
277 * @param integer $offset OPTIONAL
280 public function limit($sql, $count, $offset = 0)
283 return $this->_serverType
->limit($sql, $count, $offset);
287 * Gets the last ID generated automatically by an IDENTITY/AUTOINCREMENT
290 * @param string $tableName OPTIONAL
291 * @param string $primaryKey OPTIONAL
294 public function lastInsertId($tableName = null, $primaryKey = null)
298 if ($tableName !== null) {
299 $sequenceName = $tableName;
301 $sequenceName .= "_$primaryKey";
303 $sequenceName .= '_seq';
304 return $this->lastSequenceId($sequenceName);
307 $id = $this->getConnection()->lastInsertId();
313 * Return the most recent value from the specified sequence in the database.
315 * @param string $sequenceName
318 public function lastSequenceId($sequenceName)
321 return $this->_serverType
->lastSequenceId($sequenceName);
325 * Generate a new value from the specified sequence in the database,
328 * @param string $sequenceName
331 public function nextSequenceId($sequenceName)
334 return $this->_serverType
->nextSequenceId($sequenceName);
338 * Retrieve server version in PHP style
339 * Pdo_Idm doesn't support getAttribute(PDO::ATTR_SERVER_VERSION)
342 public function getServerVersion()
345 $stmt = $this->query('SELECT service_level, fixpack_num FROM TABLE (sysproc.env_get_inst_info()) as INSTANCEINFO');
346 $result = $stmt->fetchAll(Zend_Db
::FETCH_NUM
);
347 if (count($result)) {
349 if (preg_match('/((?:[0-9]{1,2}\.){1,3}[0-9]{1,2})/', $result[0][0], $matches)) {
356 } catch (PDOException
$e) {