Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Ibm.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Db/Adapter/Pdo/Abstract.php';
26 
28 #require_once 'Zend/Db/Adapter/Pdo/Ibm/Db2.php';
29 
31 #require_once 'Zend/Db/Adapter/Pdo/Ibm/Ids.php';
32 
34 #require_once 'Zend/Db/Statement/Pdo/Ibm.php';
35 
36 
45 {
51  protected $_pdoType = 'ibm';
52 
58  protected $_serverType = null;
59 
71  protected $_numericDataTypes = array(
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
84  );
85 
96  public function _connect()
97  {
98  if ($this->_connection) {
99  return;
100  }
101  parent::_connect();
102 
103  $this->getConnection()->setAttribute(Zend_Db::ATTR_STRINGIFY_FETCHES, true);
104 
105  try {
106  if ($this->_serverType === null) {
107  $server = substr($this->getConnection()->getAttribute(PDO::ATTR_SERVER_INFO), 0, 3);
108 
109  switch ($server) {
110  case 'DB2':
111  $this->_serverType = new Zend_Db_Adapter_Pdo_Ibm_Db2($this);
112 
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;
117 
118  break;
119  case 'IDS':
120  $this->_serverType = new Zend_Db_Adapter_Pdo_Ibm_Ids($this);
121 
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;
128 
129  break;
130  }
131  }
132  } catch (PDOException $e) {
134  #require_once 'Zend/Db/Adapter/Exception.php';
135  $error = strpos($e->getMessage(), 'driver does not support that attribute');
136  if ($error) {
137  throw new Zend_Db_Adapter_Exception("PDO_IBM driver extension is downlevel. Please use driver release version 1.2.1 or later", 0, $e);
138  } else {
139  throw new Zend_Db_Adapter_Exception($e->getMessage(), $e->getCode(), $e);
140  }
141  }
142  }
143 
149  protected function _dsn()
150  {
151  $this->_checkRequiredOptions($this->_config);
152 
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;';
160  } else {
161  // catalogued connection
162  $dsn = $this->_config['dbname'];
163  }
164  return $this->_pdoType . ': ' . $dsn;
165  }
166 
174  protected function _checkRequiredOptions(array $config)
175  {
176  parent::_checkRequiredOptions($config);
177 
178  if (array_key_exists('host', $this->_config) &&
179  !array_key_exists('port', $config)) {
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");
183  }
184  }
185 
193  public function prepare($sql)
194  {
195  $this->_connect();
196  $stmtClass = $this->_defaultStmtClass;
197  $stmt = new $stmtClass($this, $sql);
198  $stmt->setFetchMode($this->_fetchMode);
199  return $stmt;
200  }
201 
207  public function listTables()
208  {
209  $this->_connect();
210  return $this->_serverType->listTables();
211  }
212 
242  public function describeTable($tableName, $schemaName = null)
243  {
244  $this->_connect();
245  return $this->_serverType->describeTable($tableName, $schemaName);
246  }
247 
257  public function insert($table, array $bind)
258  {
259  $this->_connect();
260  $newbind = array();
261  if (is_array($bind)) {
262  foreach ($bind as $name => $value) {
263  if($value !== null) {
264  $newbind[$name] = $value;
265  }
266  }
267  }
268 
269  return parent::insert($table, $newbind);
270  }
271 
280  public function limit($sql, $count, $offset = 0)
281  {
282  $this->_connect();
283  return $this->_serverType->limit($sql, $count, $offset);
284  }
285 
294  public function lastInsertId($tableName = null, $primaryKey = null)
295  {
296  $this->_connect();
297 
298  if ($tableName !== null) {
299  $sequenceName = $tableName;
300  if ($primaryKey) {
301  $sequenceName .= "_$primaryKey";
302  }
303  $sequenceName .= '_seq';
304  return $this->lastSequenceId($sequenceName);
305  }
306 
307  $id = $this->getConnection()->lastInsertId();
308 
309  return $id;
310  }
311 
318  public function lastSequenceId($sequenceName)
319  {
320  $this->_connect();
321  return $this->_serverType->lastSequenceId($sequenceName);
322  }
323 
331  public function nextSequenceId($sequenceName)
332  {
333  $this->_connect();
334  return $this->_serverType->nextSequenceId($sequenceName);
335  }
336 
342  public function getServerVersion()
343  {
344  try {
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)) {
348  $matches = null;
349  if (preg_match('/((?:[0-9]{1,2}\.){1,3}[0-9]{1,2})/', $result[0][0], $matches)) {
350  return $matches[1];
351  } else {
352  return null;
353  }
354  }
355  return null;
356  } catch (PDOException $e) {
357  return null;
358  }
359  }
360 }
const ATTR_STRINGIFY_FETCHES
Definition: Db.php:121
describeTable($tableName, $schemaName=null)
Definition: Ibm.php:242
$tableName
Definition: trigger.php:13
limit($sql, $count, $offset=0)
Definition: Ibm.php:280
$config
Definition: fraud_order.php:17
$id
Definition: fieldset.phtml:14
const BIGINT_TYPE
Definition: Db.php:69
const INT_TYPE
Definition: Db.php:68
$count
Definition: recent.phtml:13
const FLOAT_TYPE
Definition: Db.php:70
lastSequenceId($sequenceName)
Definition: Ibm.php:318
$value
Definition: gender.phtml:16
const FETCH_NUM
Definition: Db.php:153
_checkRequiredOptions(array $config)
Definition: Ibm.php:174
nextSequenceId($sequenceName)
Definition: Ibm.php:331
insert($table, array $bind)
Definition: Ibm.php:257
lastInsertId($tableName=null, $primaryKey=null)
Definition: Ibm.php:294
$table
Definition: trigger.php:14
query($sql, $bind=array())
Definition: Abstract.php:221
if(!isset($_GET['name'])) $name
Definition: log.php:14