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/Statement.php';
27 
38 {
39 
43  protected $_keys;
44 
48  protected $_values;
49 
56  protected $_lobAsString = false;
57 
64  public function setLobAsString($lob_as_string)
65  {
66  $this->_lobAsString = (bool) $lob_as_string;
67  return $this;
68  }
69 
75  public function getLobAsString()
76  {
77  return $this->_lobAsString;
78  }
79 
87  protected function _prepare($sql)
88  {
89  $connection = $this->_adapter->getConnection();
90  $this->_stmt = @oci_parse($connection, $sql);
91  if (!$this->_stmt) {
95  #require_once 'Zend/Db/Statement/Oracle/Exception.php';
96  throw new Zend_Db_Statement_Oracle_Exception(oci_error($connection));
97  }
98  }
99 
111  protected function _bindParam($parameter, &$variable, $type = null, $length = null, $options = null)
112  {
113  // default value
114  if ($type === NULL) {
115  $type = SQLT_CHR;
116  }
117 
118  // default value
119  if ($length === NULL) {
120  $length = -1;
121  }
122 
123  $retval = @oci_bind_by_name($this->_stmt, $parameter, $variable, $length, $type);
124  if ($retval === false) {
128  #require_once 'Zend/Db/Statement/Oracle/Exception.php';
129  throw new Zend_Db_Statement_Oracle_Exception(oci_error($this->_stmt));
130  }
131 
132  return true;
133  }
134 
140  public function closeCursor()
141  {
142  if (!$this->_stmt) {
143  return false;
144  }
145 
146  oci_free_statement($this->_stmt);
147  $this->_stmt = false;
148  return true;
149  }
150 
157  public function columnCount()
158  {
159  if (!$this->_stmt) {
160  return false;
161  }
162 
163  return oci_num_fields($this->_stmt);
164  }
165 
166 
173  public function errorCode()
174  {
175  if (!$this->_stmt) {
176  return false;
177  }
178 
179  $error = oci_error($this->_stmt);
180 
181  if (!$error) {
182  return false;
183  }
184 
185  return $error['code'];
186  }
187 
188 
195  public function errorInfo()
196  {
197  if (!$this->_stmt) {
198  return false;
199  }
200 
201  $error = oci_error($this->_stmt);
202  if (!$error) {
203  return false;
204  }
205 
206  if (isset($error['sqltext'])) {
207  return array(
208  $error['code'],
209  $error['message'],
210  $error['offset'],
211  $error['sqltext'],
212  );
213  } else {
214  return array(
215  $error['code'],
216  $error['message'],
217  );
218  }
219  }
220 
221 
229  public function _execute(array $params = null)
230  {
231  $connection = $this->_adapter->getConnection();
232 
233  if (!$this->_stmt) {
234  return false;
235  }
236 
237  if ($params !== null) {
238  if (!is_array($params)) {
239  $params = array($params);
240  }
241  $error = false;
242  foreach (array_keys($params) as $name) {
243  if (!$this->bindParam($name, $params[$name], null, -1)) {
244  $error = true;
245  break;
246  }
247  }
248  if ($error) {
252  #require_once 'Zend/Db/Statement/Oracle/Exception.php';
253  throw new Zend_Db_Statement_Oracle_Exception(oci_error($this->_stmt));
254  }
255  }
256 
257  $retval = @oci_execute($this->_stmt, $this->_adapter->_getExecuteMode());
258  if ($retval === false) {
262  #require_once 'Zend/Db/Statement/Oracle/Exception.php';
263  throw new Zend_Db_Statement_Oracle_Exception(oci_error($this->_stmt));
264  }
265 
266  $this->_keys = Array();
267  if ($field_num = oci_num_fields($this->_stmt)) {
268  for ($i = 1; $i <= $field_num; $i++) {
269  $name = oci_field_name($this->_stmt, $i);
270  $this->_keys[] = $name;
271  }
272  }
273 
274  $this->_values = Array();
275  if ($this->_keys) {
276  $this->_values = array_fill(0, count($this->_keys), null);
277  }
278 
279  return $retval;
280  }
281 
291  public function fetch($style = null, $cursor = null, $offset = null)
292  {
293  if (!$this->_stmt) {
294  return false;
295  }
296 
297  if ($style === null) {
298  $style = $this->_fetchMode;
299  }
300 
301  $lob_as_string = $this->getLobAsString() ? OCI_RETURN_LOBS : 0;
302 
303  switch ($style) {
304  case Zend_Db::FETCH_NUM:
305  $row = oci_fetch_array($this->_stmt, OCI_NUM | OCI_RETURN_NULLS | $lob_as_string);
306  break;
308  $row = oci_fetch_array($this->_stmt, OCI_ASSOC | OCI_RETURN_NULLS | $lob_as_string);
309  break;
310  case Zend_Db::FETCH_BOTH:
311  $row = oci_fetch_array($this->_stmt, OCI_BOTH | OCI_RETURN_NULLS | $lob_as_string);
312  break;
313  case Zend_Db::FETCH_OBJ:
314  $row = oci_fetch_object($this->_stmt);
315  break;
317  $row = oci_fetch_array($this->_stmt, OCI_BOTH | OCI_RETURN_NULLS | $lob_as_string);
318  if ($row !== false) {
319  return $this->_fetchBound($row);
320  }
321  break;
322  default:
326  #require_once 'Zend/Db/Statement/Oracle/Exception.php';
328  array(
329  'code' => 'HYC00',
330  'message' => "Invalid fetch mode '$style' specified"
331  )
332  );
333  break;
334  }
335 
336  if (! $row && $error = oci_error($this->_stmt)) {
340  #require_once 'Zend/Db/Statement/Oracle/Exception.php';
341  throw new Zend_Db_Statement_Oracle_Exception($error);
342  }
343 
344  if (is_array($row) && array_key_exists('zend_db_rownum', $row)) {
345  unset($row['zend_db_rownum']);
346  }
347 
348  return $row;
349  }
350 
359  public function fetchAll($style = null, $col = 0)
360  {
361  if (!$this->_stmt) {
362  return false;
363  }
364 
365  // make sure we have a fetch mode
366  if ($style === null) {
367  $style = $this->_fetchMode;
368  }
369 
370  $flags = OCI_FETCHSTATEMENT_BY_ROW;
371 
372  switch ($style) {
373  case Zend_Db::FETCH_BOTH:
377  #require_once 'Zend/Db/Statement/Oracle/Exception.php';
379  array(
380  'code' => 'HYC00',
381  'message' => "OCI8 driver does not support fetchAll(FETCH_BOTH), use fetch() in a loop instead"
382  )
383  );
384  // notreached
385  $flags |= OCI_NUM;
386  $flags |= OCI_ASSOC;
387  break;
388  case Zend_Db::FETCH_NUM:
389  $flags |= OCI_NUM;
390  break;
392  $flags |= OCI_ASSOC;
393  break;
394  case Zend_Db::FETCH_OBJ:
395  break;
397  $flags = $flags &~ OCI_FETCHSTATEMENT_BY_ROW;
398  $flags |= OCI_FETCHSTATEMENT_BY_COLUMN;
399  $flags |= OCI_NUM;
400  break;
401  default:
405  #require_once 'Zend/Db/Statement/Oracle/Exception.php';
407  array(
408  'code' => 'HYC00',
409  'message' => "Invalid fetch mode '$style' specified"
410  )
411  );
412  break;
413  }
414 
415  $result = Array();
416  if ($flags != OCI_FETCHSTATEMENT_BY_ROW) { /* not Zend_Db::FETCH_OBJ */
417  if (! ($rows = oci_fetch_all($this->_stmt, $result, 0, -1, $flags) )) {
418  if ($error = oci_error($this->_stmt)) {
422  #require_once 'Zend/Db/Statement/Oracle/Exception.php';
423  throw new Zend_Db_Statement_Oracle_Exception($error);
424  }
425  if (!$rows) {
426  return array();
427  }
428  }
429  if ($style == Zend_Db::FETCH_COLUMN) {
430  $result = $result[$col];
431  }
432  foreach ($result as &$row) {
433  if (is_array($row) && array_key_exists('zend_db_rownum', $row)) {
434  unset($row['zend_db_rownum']);
435  }
436  }
437  } else {
438  while (($row = oci_fetch_object($this->_stmt)) !== false) {
439  $result [] = $row;
440  }
441  if ($error = oci_error($this->_stmt)) {
445  #require_once 'Zend/Db/Statement/Oracle/Exception.php';
446  throw new Zend_Db_Statement_Oracle_Exception($error);
447  }
448  }
449 
450  return $result;
451  }
452 
453 
461  public function fetchColumn($col = 0)
462  {
463  if (!$this->_stmt) {
464  return false;
465  }
466 
467  if (!oci_fetch($this->_stmt)) {
468  // if no error, there is simply no record
469  if (!$error = oci_error($this->_stmt)) {
470  return false;
471  }
475  #require_once 'Zend/Db/Statement/Oracle/Exception.php';
476  throw new Zend_Db_Statement_Oracle_Exception($error);
477  }
478 
479  $data = oci_result($this->_stmt, $col+1); //1-based
480  if ($data === false) {
484  #require_once 'Zend/Db/Statement/Oracle/Exception.php';
485  throw new Zend_Db_Statement_Oracle_Exception(oci_error($this->_stmt));
486  }
487 
488  if ($this->getLobAsString()) {
489  // instanceof doesn't allow '-', we must use a temporary string
490  $type = 'OCI-Lob';
491  if ($data instanceof $type) {
492  $data = $data->read($data->size());
493  }
494  }
495 
496  return $data;
497  }
498 
507  public function fetchObject($class = 'stdClass', array $config = array())
508  {
509  if (!$this->_stmt) {
510  return false;
511  }
512 
513  $obj = oci_fetch_object($this->_stmt);
514 
515  if ($error = oci_error($this->_stmt)) {
519  #require_once 'Zend/Db/Statement/Oracle/Exception.php';
520  throw new Zend_Db_Statement_Oracle_Exception($error);
521  }
522 
523  /* @todo XXX handle parameters */
524 
525  return $obj;
526  }
527 
536  public function nextRowset()
537  {
541  #require_once 'Zend/Db/Statement/Oracle/Exception.php';
543  array(
544  'code' => 'HYC00',
545  'message' => 'Optional feature not implemented'
546  )
547  );
548  }
549 
558  public function rowCount()
559  {
560  if (!$this->_stmt) {
561  return false;
562  }
563 
564  $num_rows = oci_num_rows($this->_stmt);
565 
566  if ($num_rows === false) {
570  #require_once 'Zend/Db/Statement/Oracle/Exception.php';
571  throw new Zend_Db_Statement_Oracle_Exception(oci_error($this->_stmt));
572  }
573 
574  return $num_rows;
575  }
576 
577 }
fetchObject($class='stdClass', array $config=array())
Definition: Oracle.php:507
$config
Definition: fraud_order.php:17
bindParam($parameter, &$variable, $type=null, $length=null, $options=null)
Definition: Statement.php:241
const FETCH_BOUND
Definition: Db.php:144
const FETCH_ASSOC
Definition: Db.php:142
const FETCH_COLUMN
Definition: Db.php:147
$variable
Definition: variable.php:7
$type
Definition: item.phtml:13
const FETCH_BOTH
Definition: Db.php:143
$_option $_optionId $class
Definition: date.phtml:13
const FETCH_NUM
Definition: Db.php:153
_bindParam($parameter, &$variable, $type=null, $length=null, $options=null)
Definition: Oracle.php:111
fetchAll($style=null, $col=0)
Definition: Oracle.php:359
const FETCH_OBJ
Definition: Db.php:154
_execute(array $params=null)
Definition: Oracle.php:229
setLobAsString($lob_as_string)
Definition: Oracle.php:64
$connection
Definition: bulk.php:13
$params[\Magento\Store\Model\StoreManager::PARAM_RUN_CODE]
Definition: website.php:18
$i
Definition: gallery.phtml:31
if(!isset($_GET['name'])) $name
Definition: log.php:14
fetch($style=null, $cursor=null, $offset=null)
Definition: Oracle.php:291