Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Oracle.php
Go to the documentation of this file.
1 <?php
26 #require_once 'Zend/Db/Adapter/Abstract.php';
27 
31 #require_once 'Zend/Db/Statement/Oracle.php';
32 
41 {
54  protected $_config = array(
55  'dbname' => null,
56  'username' => null,
57  'password' => null,
58  'persistent' => false
59  );
60 
72  protected $_numericDataTypes = array(
76  'BINARY_DOUBLE' => Zend_Db::FLOAT_TYPE,
77  'BINARY_FLOAT' => Zend_Db::FLOAT_TYPE,
78  'NUMBER' => Zend_Db::FLOAT_TYPE,
79  );
80 
84  protected $_execute_mode = null;
85 
91  protected $_defaultStmtClass = 'Zend_Db_Statement_Oracle';
92 
99  protected $_lobAsString = null;
100 
107  protected function _connect()
108  {
109  if (is_resource($this->_connection)) {
110  // connection already exists
111  return;
112  }
113 
114  if (!extension_loaded('oci8')) {
118  #require_once 'Zend/Db/Adapter/Oracle/Exception.php';
119  throw new Zend_Db_Adapter_Oracle_Exception('The OCI8 extension is required for this adapter but the extension is not loaded');
120  }
121 
122  $this->_setExecuteMode(OCI_COMMIT_ON_SUCCESS);
123 
124  $connectionFuncName = ($this->_config['persistent'] == true) ? 'oci_pconnect' : 'oci_connect';
125 
126  $this->_connection = @$connectionFuncName(
127  $this->_config['username'],
128  $this->_config['password'],
129  $this->_config['dbname'],
130  $this->_config['charset']);
131 
132  // check the connection
133  if (!$this->_connection) {
137  #require_once 'Zend/Db/Adapter/Oracle/Exception.php';
138  throw new Zend_Db_Adapter_Oracle_Exception(oci_error());
139  }
140  }
141 
147  public function isConnected()
148  {
149  return ((bool) (is_resource($this->_connection)
150  && (get_resource_type($this->_connection) == 'oci8 connection'
151  || get_resource_type($this->_connection) == 'oci8 persistent connection')));
152  }
153 
159  public function closeConnection()
160  {
161  if ($this->isConnected()) {
162  oci_close($this->_connection);
163  }
164  $this->_connection = null;
165  }
166 
173  public function setLobAsString($lobAsString)
174  {
175  $this->_lobAsString = (bool) $lobAsString;
176  return $this;
177  }
178 
184  public function getLobAsString()
185  {
186  if ($this->_lobAsString === null) {
187  // if never set by user, we use driver option if it exists otherwise false
188  if (isset($this->_config['driver_options']) &&
189  isset($this->_config['driver_options']['lob_as_string'])) {
190  $this->_lobAsString = (bool) $this->_config['driver_options']['lob_as_string'];
191  } else {
192  $this->_lobAsString = false;
193  }
194  }
195  return $this->_lobAsString;
196  }
197 
204  public function prepare($sql)
205  {
206  $this->_connect();
207  $stmtClass = $this->_defaultStmtClass;
208  if (!class_exists($stmtClass)) {
209  #require_once 'Zend/Loader.php';
210  Zend_Loader::loadClass($stmtClass);
211  }
212  $stmt = new $stmtClass($this, $sql);
213  if ($stmt instanceof Zend_Db_Statement_Oracle) {
214  $stmt->setLobAsString($this->getLobAsString());
215  }
216  $stmt->setFetchMode($this->_fetchMode);
217  return $stmt;
218  }
219 
226  protected function _quote($value)
227  {
228  if (is_int($value) || is_float($value)) {
229  return $value;
230  }
231  $value = str_replace("'", "''", $value);
232  return "'" . addcslashes($value, "\000\n\r\\\032") . "'";
233  }
234 
243  public function quoteTableAs($ident, $alias = null, $auto = false)
244  {
245  // Oracle doesn't allow the 'AS' keyword between the table identifier/expression and alias.
246  return $this->_quoteIdentifierAs($ident, $alias, $auto, ' ');
247  }
248 
257  public function lastSequenceId($sequenceName)
258  {
259  $this->_connect();
260  $sql = 'SELECT '.$this->quoteIdentifier($sequenceName, true).'.CURRVAL FROM dual';
261  $value = $this->fetchOne($sql);
262  return $value;
263  }
264 
273  public function nextSequenceId($sequenceName)
274  {
275  $this->_connect();
276  $sql = 'SELECT '.$this->quoteIdentifier($sequenceName, true).'.NEXTVAL FROM dual';
277  $value = $this->fetchOne($sql);
278  return $value;
279  }
280 
298  public function lastInsertId($tableName = null, $primaryKey = null)
299  {
300  if ($tableName !== null) {
301  $sequenceName = $tableName;
302  if ($primaryKey) {
303  $sequenceName .= "_$primaryKey";
304  }
305  $sequenceName .= '_seq';
306  return $this->lastSequenceId($sequenceName);
307  }
308 
309  // No support for IDENTITY columns; return null
310  return null;
311  }
312 
318  public function listTables()
319  {
320  $this->_connect();
321  $data = $this->fetchCol('SELECT table_name FROM all_tables');
322  return $data;
323  }
324 
355  public function describeTable($tableName, $schemaName = null)
356  {
357  $version = $this->getServerVersion();
358  if (($version === null) || version_compare($version, '9.0.0', '>=')) {
359  $sql = "SELECT TC.TABLE_NAME, TC.OWNER, TC.COLUMN_NAME, TC.DATA_TYPE,
360  TC.DATA_DEFAULT, TC.NULLABLE, TC.COLUMN_ID, TC.DATA_LENGTH,
361  TC.DATA_SCALE, TC.DATA_PRECISION, C.CONSTRAINT_TYPE, CC.POSITION
362  FROM ALL_TAB_COLUMNS TC
363  LEFT JOIN (ALL_CONS_COLUMNS CC JOIN ALL_CONSTRAINTS C
364  ON (CC.CONSTRAINT_NAME = C.CONSTRAINT_NAME AND CC.TABLE_NAME = C.TABLE_NAME AND CC.OWNER = C.OWNER AND C.CONSTRAINT_TYPE = 'P'))
365  ON TC.TABLE_NAME = CC.TABLE_NAME AND TC.COLUMN_NAME = CC.COLUMN_NAME
366  WHERE UPPER(TC.TABLE_NAME) = UPPER(:TBNAME)";
367  $bind[':TBNAME'] = $tableName;
368  if ($schemaName) {
369  $sql .= ' AND UPPER(TC.OWNER) = UPPER(:SCNAME)';
370  $bind[':SCNAME'] = $schemaName;
371  }
372  $sql .= ' ORDER BY TC.COLUMN_ID';
373  } else {
374  $subSql="SELECT AC.OWNER, AC.TABLE_NAME, ACC.COLUMN_NAME, AC.CONSTRAINT_TYPE, ACC.POSITION
375  from ALL_CONSTRAINTS AC, ALL_CONS_COLUMNS ACC
376  WHERE ACC.CONSTRAINT_NAME = AC.CONSTRAINT_NAME
377  AND ACC.TABLE_NAME = AC.TABLE_NAME
378  AND ACC.OWNER = AC.OWNER
379  AND AC.CONSTRAINT_TYPE = 'P'
380  AND UPPER(AC.TABLE_NAME) = UPPER(:TBNAME)";
381  $bind[':TBNAME'] = $tableName;
382  if ($schemaName) {
383  $subSql .= ' AND UPPER(ACC.OWNER) = UPPER(:SCNAME)';
384  $bind[':SCNAME'] = $schemaName;
385  }
386  $sql="SELECT TC.TABLE_NAME, TC.OWNER, TC.COLUMN_NAME, TC.DATA_TYPE,
387  TC.DATA_DEFAULT, TC.NULLABLE, TC.COLUMN_ID, TC.DATA_LENGTH,
388  TC.DATA_SCALE, TC.DATA_PRECISION, CC.CONSTRAINT_TYPE, CC.POSITION
389  FROM ALL_TAB_COLUMNS TC, ($subSql) CC
390  WHERE UPPER(TC.TABLE_NAME) = UPPER(:TBNAME)
391  AND TC.OWNER = CC.OWNER(+) AND TC.TABLE_NAME = CC.TABLE_NAME(+) AND TC.COLUMN_NAME = CC.COLUMN_NAME(+)";
392  if ($schemaName) {
393  $sql .= ' AND UPPER(TC.OWNER) = UPPER(:SCNAME)';
394  }
395  $sql .= ' ORDER BY TC.COLUMN_ID';
396  }
397 
398  $stmt = $this->query($sql, $bind);
399 
403  $result = $stmt->fetchAll(Zend_Db::FETCH_NUM);
404 
405  $table_name = 0;
406  $owner = 1;
407  $column_name = 2;
408  $data_type = 3;
409  $data_default = 4;
410  $nullable = 5;
411  $column_id = 6;
412  $data_length = 7;
413  $data_scale = 8;
414  $data_precision = 9;
415  $constraint_type = 10;
416  $position = 11;
417 
418  $desc = array();
419  foreach ($result as $key => $row) {
420  list ($primary, $primaryPosition, $identity) = array(false, null, false);
421  if ($row[$constraint_type] == 'P') {
422  $primary = true;
423  $primaryPosition = $row[$position];
427  $identity = false;
428  }
429  $desc[$this->foldCase($row[$column_name])] = array(
430  'SCHEMA_NAME' => $this->foldCase($row[$owner]),
431  'TABLE_NAME' => $this->foldCase($row[$table_name]),
432  'COLUMN_NAME' => $this->foldCase($row[$column_name]),
433  'COLUMN_POSITION' => $row[$column_id],
434  'DATA_TYPE' => $row[$data_type],
435  'DEFAULT' => $row[$data_default],
436  'NULLABLE' => (bool) ($row[$nullable] == 'Y'),
437  'LENGTH' => $row[$data_length],
438  'SCALE' => $row[$data_scale],
439  'PRECISION' => $row[$data_precision],
440  'UNSIGNED' => null, // @todo
441  'PRIMARY' => $primary,
442  'PRIMARY_POSITION' => $primaryPosition,
443  'IDENTITY' => $identity
444  );
445  }
446  return $desc;
447  }
448 
454  protected function _beginTransaction()
455  {
456  $this->_setExecuteMode(OCI_DEFAULT);
457  }
458 
465  protected function _commit()
466  {
467  if (!oci_commit($this->_connection)) {
471  #require_once 'Zend/Db/Adapter/Oracle/Exception.php';
472  throw new Zend_Db_Adapter_Oracle_Exception(oci_error($this->_connection));
473  }
474  $this->_setExecuteMode(OCI_COMMIT_ON_SUCCESS);
475  }
476 
483  protected function _rollBack()
484  {
485  if (!oci_rollback($this->_connection)) {
489  #require_once 'Zend/Db/Adapter/Oracle/Exception.php';
490  throw new Zend_Db_Adapter_Oracle_Exception(oci_error($this->_connection));
491  }
492  $this->_setExecuteMode(OCI_COMMIT_ON_SUCCESS);
493  }
494 
504  public function setFetchMode($mode)
505  {
506  switch ($mode) {
507  case Zend_Db::FETCH_NUM: // seq array
508  case Zend_Db::FETCH_ASSOC: // assoc array
509  case Zend_Db::FETCH_BOTH: // seq+assoc array
510  case Zend_Db::FETCH_OBJ: // object
511  $this->_fetchMode = $mode;
512  break;
513  case Zend_Db::FETCH_BOUND: // bound to PHP variable
517  #require_once 'Zend/Db/Adapter/Oracle/Exception.php';
518  throw new Zend_Db_Adapter_Oracle_Exception('FETCH_BOUND is not supported yet');
519  break;
520  default:
524  #require_once 'Zend/Db/Adapter/Oracle/Exception.php';
525  throw new Zend_Db_Adapter_Oracle_Exception("Invalid fetch mode '$mode' specified");
526  break;
527  }
528  }
529 
539  public function limit($sql, $count, $offset = 0)
540  {
541  $count = intval($count);
542  if ($count <= 0) {
546  #require_once 'Zend/Db/Adapter/Oracle/Exception.php';
547  throw new Zend_Db_Adapter_Oracle_Exception("LIMIT argument count=$count is not valid");
548  }
549 
550  $offset = intval($offset);
551  if ($offset < 0) {
555  #require_once 'Zend/Db/Adapter/Oracle/Exception.php';
556  throw new Zend_Db_Adapter_Oracle_Exception("LIMIT argument offset=$offset is not valid");
557  }
558 
565  $limit_sql = "SELECT z2.*
566  FROM (
567  SELECT z1.*, ROWNUM AS \"zend_db_rownum\"
568  FROM (
569  " . $sql . "
570  ) z1
571  ) z2
572  WHERE z2.\"zend_db_rownum\" BETWEEN " . ($offset+1) . " AND " . ($offset+$count);
573  return $limit_sql;
574  }
575 
580  private function _setExecuteMode($mode)
581  {
582  switch($mode) {
583  case OCI_COMMIT_ON_SUCCESS:
584  case OCI_DEFAULT:
585  case OCI_DESCRIBE_ONLY:
586  $this->_execute_mode = $mode;
587  break;
588  default:
592  #require_once 'Zend/Db/Adapter/Oracle/Exception.php';
593  throw new Zend_Db_Adapter_Oracle_Exception("Invalid execution mode '$mode' specified");
594  break;
595  }
596  }
597 
601  public function _getExecuteMode()
602  {
603  return $this->_execute_mode;
604  }
605 
612  public function supportsParameters($type)
613  {
614  switch ($type) {
615  case 'named':
616  return true;
617  case 'positional':
618  default:
619  return false;
620  }
621  }
622 
628  public function getServerVersion()
629  {
630  $this->_connect();
631  $version = oci_server_version($this->_connection);
632  if ($version !== false) {
633  $matches = null;
634  if (preg_match('/((?:[0-9]{1,2}\.){1,3}[0-9]{1,2})/', $version, $matches)) {
635  return $matches[1];
636  } else {
637  return null;
638  }
639  } else {
640  return null;
641  }
642  }
643 }
describeTable($tableName, $schemaName=null)
Definition: Oracle.php:355
setLobAsString($lobAsString)
Definition: Oracle.php:173
$tableName
Definition: trigger.php:13
fetchOne($sql, $bind=array())
Definition: Abstract.php:826
static loadClass($class, $dirs=null)
Definition: Loader.php:52
fetchCol($sql, $bind=array())
Definition: Abstract.php:792
const BIGINT_TYPE
Definition: Db.php:69
const INT_TYPE
Definition: Db.php:68
$count
Definition: recent.phtml:13
const FETCH_BOUND
Definition: Db.php:144
const FETCH_ASSOC
Definition: Db.php:142
limit($sql, $count, $offset=0)
Definition: Oracle.php:539
const FLOAT_TYPE
Definition: Db.php:70
$type
Definition: item.phtml:13
nextSequenceId($sequenceName)
Definition: Oracle.php:273
const FETCH_BOTH
Definition: Db.php:143
$value
Definition: gender.phtml:16
const FETCH_NUM
Definition: Db.php:153
quoteTableAs($ident, $alias=null, $auto=false)
Definition: Oracle.php:243
_quoteIdentifierAs($ident, $alias=null, $auto=false, $as=' AS ')
Definition: Abstract.php:999
if($exist=($block->getProductCollection() && $block->getProductCollection() ->getSize())) $mode
Definition: grid.phtml:15
supportsParameters($type)
Definition: Oracle.php:612
const FETCH_OBJ
Definition: Db.php:154
lastInsertId($tableName=null, $primaryKey=null)
Definition: Oracle.php:298
query($sql, $bind=array())
Definition: Abstract.php:457
if(!trim($html)) $alias
Definition: details.phtml:20
lastSequenceId($sequenceName)
Definition: Oracle.php:257