00001 <?php
00026 require_once 'Zend/Db/Statement.php';
00027
00037 class Zend_Db_Statement_Oracle extends Zend_Db_Statement
00038 {
00039
00043 protected $_keys;
00044
00048 protected $_values;
00049
00056 protected $_lobAsString = false;
00057
00064 public function setLobAsString($lob_as_string)
00065 {
00066 $this->_lobAsString = (bool) $lob_as_string;
00067 return $this;
00068 }
00069
00075 public function getLobAsString()
00076 {
00077 return $this->_lobAsString;
00078 }
00079
00087 protected function _prepare($sql)
00088 {
00089 $connection = $this->_adapter->getConnection();
00090 $this->_stmt = oci_parse($connection, $sql);
00091 if (!$this->_stmt) {
00095 require_once 'Zend/Db/Statement/Oracle/Exception.php';
00096 throw new Zend_Db_Statement_Oracle_Exception(oci_error($connection));
00097 }
00098 }
00099
00111 protected function _bindParam($parameter, &$variable, $type = null, $length = null, $options = null)
00112 {
00113
00114 if ($type === NULL) {
00115 $type = SQLT_CHR;
00116 }
00117
00118
00119 if ($length === NULL) {
00120 $length = -1;
00121 }
00122
00123 $retval = @oci_bind_by_name($this->_stmt, $parameter, $variable, $length, $type);
00124 if ($retval === false) {
00128 require_once 'Zend/Db/Statement/Oracle/Exception.php';
00129 throw new Zend_Db_Statement_Oracle_Exception(oci_error($this->_stmt));
00130 }
00131
00132 return true;
00133 }
00134
00140 public function closeCursor()
00141 {
00142 if (!$this->_stmt) {
00143 return false;
00144 }
00145
00146 oci_free_statement($this->_stmt);
00147 $this->_stmt = false;
00148 return true;
00149 }
00150
00157 public function columnCount()
00158 {
00159 if (!$this->_stmt) {
00160 return false;
00161 }
00162
00163 return oci_num_fields($this->_stmt);
00164 }
00165
00166
00173 public function errorCode()
00174 {
00175 if (!$this->_stmt) {
00176 return false;
00177 }
00178
00179 $error = oci_error($this->_stmt);
00180
00181 if (!$error) {
00182 return false;
00183 }
00184
00185 return $error['code'];
00186 }
00187
00188
00195 public function errorInfo()
00196 {
00197 if (!$this->_stmt) {
00198 return false;
00199 }
00200
00201 $error = oci_error($this->_stmt);
00202 if (!$error) {
00203 return false;
00204 }
00205
00206 if (isset($error['sqltext'])) {
00207 return array(
00208 $error['code'],
00209 $error['message'],
00210 $error['offset'],
00211 $error['sqltext'],
00212 );
00213 } else {
00214 return array(
00215 $error['code'],
00216 $error['message'],
00217 );
00218 }
00219 }
00220
00221
00229 public function _execute(array $params = null)
00230 {
00231 $connection = $this->_adapter->getConnection();
00232
00233 if (!$this->_stmt) {
00234 return false;
00235 }
00236
00237 if ($params !== null) {
00238 if (!is_array($params)) {
00239 $params = array($params);
00240 }
00241 $error = false;
00242 foreach (array_keys($params) as $name) {
00243 if (!@oci_bind_by_name($this->_stmt, $name, $params[$name], -1)) {
00244 $error = true;
00245 break;
00246 }
00247 }
00248 if ($error) {
00252 require_once 'Zend/Db/Statement/Oracle/Exception.php';
00253 throw new Zend_Db_Statement_Oracle_Exception(oci_error($this->_stmt));
00254 }
00255 }
00256
00257 $retval = @oci_execute($this->_stmt, $this->_adapter->_getExecuteMode());
00258 if ($retval === false) {
00262 require_once 'Zend/Db/Statement/Oracle/Exception.php';
00263 throw new Zend_Db_Statement_Oracle_Exception(oci_error($this->_stmt));
00264 }
00265
00266 $this->_keys = Array();
00267 if ($field_num = oci_num_fields($this->_stmt)) {
00268 for ($i = 1; $i <= $field_num; $i++) {
00269 $name = oci_field_name($this->_stmt, $i);
00270 $this->_keys[] = $name;
00271 }
00272 }
00273
00274 $this->_values = Array();
00275 if ($this->_keys) {
00276 $this->_values = array_fill(0, count($this->_keys), null);
00277 }
00278
00279 return $retval;
00280 }
00281
00291 public function fetch($style = null, $cursor = null, $offset = null)
00292 {
00293 if (!$this->_stmt) {
00294 return false;
00295 }
00296
00297 if ($style === null) {
00298 $style = $this->_fetchMode;
00299 }
00300
00301 $lob_as_string = $this->getLobAsString() ? OCI_RETURN_LOBS : 0;
00302
00303 switch ($style) {
00304 case Zend_Db::FETCH_NUM:
00305 $row = oci_fetch_array($this->_stmt, OCI_NUM | OCI_RETURN_NULLS | $lob_as_string);
00306 break;
00307 case Zend_Db::FETCH_ASSOC:
00308 $row = oci_fetch_array($this->_stmt, OCI_ASSOC | OCI_RETURN_NULLS | $lob_as_string);
00309 break;
00310 case Zend_Db::FETCH_BOTH:
00311 $row = oci_fetch_array($this->_stmt, OCI_BOTH | OCI_RETURN_NULLS | $lob_as_string);
00312 break;
00313 case Zend_Db::FETCH_OBJ:
00314 $row = oci_fetch_object($this->_stmt);
00315 break;
00316 case Zend_Db::FETCH_BOUND:
00317 $row = oci_fetch_array($this->_stmt, OCI_BOTH | OCI_RETURN_NULLS | $lob_as_string);
00318 if ($row !== false) {
00319 return $this->_fetchBound($row);
00320 }
00321 break;
00322 default:
00326 require_once 'Zend/Db/Statement/Oracle/Exception.php';
00327 throw new Zend_Db_Statement_Oracle_Exception(
00328 array(
00329 'code' => 'HYC00',
00330 'message' => "Invalid fetch mode '$style' specified"
00331 )
00332 );
00333 break;
00334 }
00335
00336 if (! $row && $error = oci_error($this->_stmt)) {
00340 require_once 'Zend/Db/Statement/Oracle/Exception.php';
00341 throw new Zend_Db_Statement_Oracle_Exception($error);
00342 }
00343
00344 if (is_array($row) && array_key_exists('zend_db_rownum', $row)) {
00345 unset($row['zend_db_rownum']);
00346 }
00347
00348 return $row;
00349 }
00350
00359 public function fetchAll($style = null, $col = 0)
00360 {
00361 if (!$this->_stmt) {
00362 return false;
00363 }
00364
00365
00366 if ($style === null) {
00367 $style = $this->_fetchMode;
00368 }
00369
00370 $flags = OCI_FETCHSTATEMENT_BY_ROW;
00371
00372 switch ($style) {
00373 case Zend_Db::FETCH_BOTH:
00377 require_once 'Zend/Db/Statement/Oracle/Exception.php';
00378 throw new Zend_Db_Statement_Oracle_Exception(
00379 array(
00380 'code' => 'HYC00',
00381 'message' => "OCI8 driver does not support fetchAll(FETCH_BOTH), use fetch() in a loop instead"
00382 )
00383 );
00384
00385 $flags |= OCI_NUM;
00386 $flags |= OCI_ASSOC;
00387 break;
00388 case Zend_Db::FETCH_NUM:
00389 $flags |= OCI_NUM;
00390 break;
00391 case Zend_Db::FETCH_ASSOC:
00392 $flags |= OCI_ASSOC;
00393 break;
00394 case Zend_Db::FETCH_OBJ:
00395 break;
00396 case Zend_Db::FETCH_COLUMN:
00397 $flags = $flags &~ OCI_FETCHSTATEMENT_BY_ROW;
00398 $flags |= OCI_FETCHSTATEMENT_BY_COLUMN;
00399 $flags |= OCI_NUM;
00400 break;
00401 default:
00405 require_once 'Zend/Db/Statement/Oracle/Exception.php';
00406 throw new Zend_Db_Statement_Oracle_Exception(
00407 array(
00408 'code' => 'HYC00',
00409 'message' => "Invalid fetch mode '$style' specified"
00410 )
00411 );
00412 break;
00413 }
00414
00415 $result = Array();
00416 if ($flags != OCI_FETCHSTATEMENT_BY_ROW) {
00417 if (! ($rows = oci_fetch_all($this->_stmt, $result, 0, -1, $flags) )) {
00418 if ($error = oci_error($this->_stmt)) {
00422 require_once 'Zend/Db/Statement/Oracle/Exception.php';
00423 throw new Zend_Db_Statement_Oracle_Exception($error);
00424 }
00425 if (!$rows) {
00426 return array();
00427 }
00428 }
00429 if ($style == Zend_Db::FETCH_COLUMN) {
00430 $result = $result[$col];
00431 }
00432 foreach ($result as &$row) {
00433 if (is_array($row) && array_key_exists('zend_db_rownum', $row)) {
00434 unset($row['zend_db_rownum']);
00435 }
00436 }
00437 } else {
00438 while (($row = oci_fetch_object($this->_stmt)) !== false) {
00439 $result [] = $row;
00440 }
00441 if ($error = oci_error($this->_stmt)) {
00445 require_once 'Zend/Db/Statement/Oracle/Exception.php';
00446 throw new Zend_Db_Statement_Oracle_Exception($error);
00447 }
00448 }
00449
00450 return $result;
00451 }
00452
00453
00461 public function fetchColumn($col = 0)
00462 {
00463 if (!$this->_stmt) {
00464 return false;
00465 }
00466
00467 if (!oci_fetch($this->_stmt)) {
00468
00469 if (!$error = oci_error($this->_stmt)) {
00470 return false;
00471 }
00475 require_once 'Zend/Db/Statement/Oracle/Exception.php';
00476 throw new Zend_Db_Statement_Oracle_Exception($error);
00477 }
00478
00479 $data = oci_result($this->_stmt, $col+1);
00480 if ($data === false) {
00484 require_once 'Zend/Db/Statement/Oracle/Exception.php';
00485 throw new Zend_Db_Statement_Oracle_Exception(oci_error($this->_stmt));
00486 }
00487
00488 if ($this->getLobAsString()) {
00489
00490 $type = 'OCI-Lob';
00491 if ($data instanceof $type) {
00492 $data = $data->read($data->size());
00493 }
00494 }
00495
00496 return $data;
00497 }
00498
00507 public function fetchObject($class = 'stdClass', array $config = array())
00508 {
00509 if (!$this->_stmt) {
00510 return false;
00511 }
00512
00513 $obj = oci_fetch_object($this->_stmt);
00514
00515 if ($error = oci_error($this->_stmt)) {
00519 require_once 'Zend/Db/Statement/Oracle/Exception.php';
00520 throw new Zend_Db_Statement_Oracle_Exception($error);
00521 }
00522
00523
00524
00525 return $obj;
00526 }
00527
00536 public function nextRowset()
00537 {
00541 require_once 'Zend/Db/Statement/Oracle/Exception.php';
00542 throw new Zend_Db_Statement_Oracle_Exception(
00543 array(
00544 'code' => 'HYC00',
00545 'message' => 'Optional feature not implemented'
00546 )
00547 );
00548 }
00549
00558 public function rowCount()
00559 {
00560 if (!$this->_stmt) {
00561 return false;
00562 }
00563
00564 $num_rows = oci_num_rows($this->_stmt);
00565
00566 if ($num_rows === false) {
00570 require_once 'Zend/Db/Statement/Oracle/Exception.php';
00571 throw new Zend_Db_Statement_Oracle_Exception(oci_error($this->_stmt));
00572 }
00573
00574 return $num_rows;
00575 }
00576
00577 }