Db2
2 years ago
Mysqli
2 years ago
Oracle
2 years ago
Pdo
2 years ago
Sqlsrv
2 years ago
Db2.php
1 year ago
Exception.php
2 years ago
Interface.php
2 years ago
Mysqli.php
1 year ago
Oracle.php
1 year ago
Pdo.php
1 year ago
Sqlsrv.php
1 year ago
Oracle.php
492 lines
| 1 | <?php |
| 2 | |
| 3 | namespace { |
| 4 | /** |
| 5 | * Zend Framework |
| 6 | * |
| 7 | * LICENSE |
| 8 | * |
| 9 | * This source file is subject to the new BSD license that is bundled |
| 10 | * with this package in the file LICENSE.txt. |
| 11 | * It is also available through the world-wide-web at this URL: |
| 12 | * http://framework.zend.com/license/new-bsd |
| 13 | * If you did not receive a copy of the license and are unable to |
| 14 | * obtain it through the world-wide-web, please send an email |
| 15 | * to license@zend.com so we can send you a copy immediately. |
| 16 | * |
| 17 | * @category Zend |
| 18 | * @package Zend_Db |
| 19 | * @subpackage Statement |
| 20 | * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) |
| 21 | * @license http://framework.zend.com/license/new-bsd New BSD License |
| 22 | * @version $Id: Oracle.php 23959 2011-05-03 10:45:47Z yoshida@zend.co.jp $ |
| 23 | */ |
| 24 | /** |
| 25 | * @see Zend_Db_Statement |
| 26 | */ |
| 27 | // require_once 'Zend/Db/Statement.php'; |
| 28 | /** |
| 29 | * Extends for Oracle. |
| 30 | * |
| 31 | * @category Zend |
| 32 | * @package Zend_Db |
| 33 | * @subpackage Statement |
| 34 | * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com) |
| 35 | * @license http://framework.zend.com/license/new-bsd New BSD License |
| 36 | */ |
| 37 | class Zend_Db_Statement_Oracle extends \Zend_Db_Statement |
| 38 | { |
| 39 | /** |
| 40 | * Column names. |
| 41 | */ |
| 42 | protected $_keys; |
| 43 | /** |
| 44 | * Fetched result values. |
| 45 | */ |
| 46 | protected $_values; |
| 47 | /** |
| 48 | * Check if LOB field are returned as string |
| 49 | * instead of OCI-Lob object |
| 50 | * |
| 51 | * @var boolean |
| 52 | */ |
| 53 | protected $_lobAsString = \false; |
| 54 | /** |
| 55 | * Activate/deactivate return of LOB as string |
| 56 | * |
| 57 | * @param string $lob_as_string |
| 58 | * @return Zend_Db_Statement_Oracle |
| 59 | */ |
| 60 | public function setLobAsString($lob_as_string) |
| 61 | { |
| 62 | $this->_lobAsString = (bool) $lob_as_string; |
| 63 | return $this; |
| 64 | } |
| 65 | /** |
| 66 | * Return whether or not LOB are returned as string |
| 67 | * |
| 68 | * @return boolean |
| 69 | */ |
| 70 | public function getLobAsString() |
| 71 | { |
| 72 | return $this->_lobAsString; |
| 73 | } |
| 74 | /** |
| 75 | * Prepares statement handle |
| 76 | * |
| 77 | * @param string $sql |
| 78 | * @return void |
| 79 | * @throws Zend_Db_Statement_Oracle_Exception |
| 80 | */ |
| 81 | protected function _prepare($sql) |
| 82 | { |
| 83 | $connection = $this->_adapter->getConnection(); |
| 84 | $this->_stmt = @\oci_parse($connection, $sql); |
| 85 | if (!$this->_stmt) { |
| 86 | /** |
| 87 | * @see Zend_Db_Statement_Oracle_Exception |
| 88 | */ |
| 89 | // require_once 'Zend/Db/Statement/Oracle/Exception.php'; |
| 90 | throw new \Zend_Db_Statement_Oracle_Exception(\oci_error($connection)); |
| 91 | } |
| 92 | } |
| 93 | /** |
| 94 | * Binds a parameter to the specified variable name. |
| 95 | * |
| 96 | * @param mixed $parameter Name the parameter, either integer or string. |
| 97 | * @param mixed $variable Reference to PHP variable containing the value. |
| 98 | * @param mixed $type OPTIONAL Datatype of SQL parameter. |
| 99 | * @param mixed $length OPTIONAL Length of SQL parameter. |
| 100 | * @param mixed $options OPTIONAL Other options. |
| 101 | * @return bool |
| 102 | * @throws Zend_Db_Statement_Exception |
| 103 | */ |
| 104 | protected function _bindParam($parameter, &$variable, $type = null, $length = null, $options = null) |
| 105 | { |
| 106 | // default value |
| 107 | if ($type === NULL) { |
| 108 | $type = \SQLT_CHR; |
| 109 | } |
| 110 | // default value |
| 111 | if ($length === NULL) { |
| 112 | $length = -1; |
| 113 | } |
| 114 | $retval = @\oci_bind_by_name($this->_stmt, $parameter, $variable, $length, $type); |
| 115 | if ($retval === \false) { |
| 116 | /** |
| 117 | * @see Zend_Db_Adapter_Oracle_Exception |
| 118 | */ |
| 119 | // require_once 'Zend/Db/Statement/Oracle/Exception.php'; |
| 120 | throw new \Zend_Db_Statement_Oracle_Exception(\oci_error($this->_stmt)); |
| 121 | } |
| 122 | return \true; |
| 123 | } |
| 124 | /** |
| 125 | * Closes the cursor, allowing the statement to be executed again. |
| 126 | * |
| 127 | * @return bool |
| 128 | */ |
| 129 | public function closeCursor() |
| 130 | { |
| 131 | if (!$this->_stmt) { |
| 132 | return \false; |
| 133 | } |
| 134 | \oci_free_statement($this->_stmt); |
| 135 | $this->_stmt = \false; |
| 136 | return \true; |
| 137 | } |
| 138 | /** |
| 139 | * Returns the number of columns in the result set. |
| 140 | * Returns null if the statement has no result set metadata. |
| 141 | * |
| 142 | * @return int The number of columns. |
| 143 | */ |
| 144 | public function columnCount() |
| 145 | { |
| 146 | if (!$this->_stmt) { |
| 147 | return \false; |
| 148 | } |
| 149 | return \oci_num_fields($this->_stmt); |
| 150 | } |
| 151 | /** |
| 152 | * Retrieves the error code, if any, associated with the last operation on |
| 153 | * the statement handle. |
| 154 | * |
| 155 | * @return string error code. |
| 156 | */ |
| 157 | public function errorCode() |
| 158 | { |
| 159 | if (!$this->_stmt) { |
| 160 | return \false; |
| 161 | } |
| 162 | $error = \oci_error($this->_stmt); |
| 163 | if (!$error) { |
| 164 | return \false; |
| 165 | } |
| 166 | return $error['code']; |
| 167 | } |
| 168 | /** |
| 169 | * Retrieves an array of error information, if any, associated with the |
| 170 | * last operation on the statement handle. |
| 171 | * |
| 172 | * @return array |
| 173 | */ |
| 174 | public function errorInfo() |
| 175 | { |
| 176 | if (!$this->_stmt) { |
| 177 | return \false; |
| 178 | } |
| 179 | $error = \oci_error($this->_stmt); |
| 180 | if (!$error) { |
| 181 | return \false; |
| 182 | } |
| 183 | if (isset($error['sqltext'])) { |
| 184 | return array($error['code'], $error['message'], $error['offset'], $error['sqltext']); |
| 185 | } else { |
| 186 | return array($error['code'], $error['message']); |
| 187 | } |
| 188 | } |
| 189 | /** |
| 190 | * Executes a prepared statement. |
| 191 | * |
| 192 | * @param array $params OPTIONAL Values to bind to parameter placeholders. |
| 193 | * @return bool |
| 194 | * @throws Zend_Db_Statement_Exception |
| 195 | */ |
| 196 | public function _execute(?array $params = null) |
| 197 | { |
| 198 | $connection = $this->_adapter->getConnection(); |
| 199 | if (!$this->_stmt) { |
| 200 | return \false; |
| 201 | } |
| 202 | if ($params !== null) { |
| 203 | if (!\is_array($params)) { |
| 204 | $params = array($params); |
| 205 | } |
| 206 | $error = \false; |
| 207 | foreach (\array_keys($params) as $name) { |
| 208 | if (!@\oci_bind_by_name($this->_stmt, $name, $params[$name], -1)) { |
| 209 | $error = \true; |
| 210 | break; |
| 211 | } |
| 212 | } |
| 213 | if ($error) { |
| 214 | /** |
| 215 | * @see Zend_Db_Adapter_Oracle_Exception |
| 216 | */ |
| 217 | // require_once 'Zend/Db/Statement/Oracle/Exception.php'; |
| 218 | throw new \Zend_Db_Statement_Oracle_Exception(\oci_error($this->_stmt)); |
| 219 | } |
| 220 | } |
| 221 | $retval = @\oci_execute($this->_stmt, $this->_adapter->_getExecuteMode()); |
| 222 | if ($retval === \false) { |
| 223 | /** |
| 224 | * @see Zend_Db_Adapter_Oracle_Exception |
| 225 | */ |
| 226 | // require_once 'Zend/Db/Statement/Oracle/Exception.php'; |
| 227 | throw new \Zend_Db_Statement_Oracle_Exception(\oci_error($this->_stmt)); |
| 228 | } |
| 229 | $this->_keys = array(); |
| 230 | if ($field_num = \oci_num_fields($this->_stmt)) { |
| 231 | for ($i = 1; $i <= $field_num; $i++) { |
| 232 | $name = \oci_field_name($this->_stmt, $i); |
| 233 | $this->_keys[] = $name; |
| 234 | } |
| 235 | } |
| 236 | $this->_values = array(); |
| 237 | if ($this->_keys) { |
| 238 | $this->_values = \array_fill(0, \count($this->_keys), null); |
| 239 | } |
| 240 | return $retval; |
| 241 | } |
| 242 | /** |
| 243 | * Fetches a row from the result set. |
| 244 | * |
| 245 | * @param int $style OPTIONAL Fetch mode for this fetch operation. |
| 246 | * @param int $cursor OPTIONAL Absolute, relative, or other. |
| 247 | * @param int $offset OPTIONAL Number for absolute or relative cursors. |
| 248 | * @return mixed Array, object, or scalar depending on fetch mode. |
| 249 | * @throws Zend_Db_Statement_Exception |
| 250 | */ |
| 251 | public function fetch($style = null, $cursor = null, $offset = null) |
| 252 | { |
| 253 | if (!$this->_stmt) { |
| 254 | return \false; |
| 255 | } |
| 256 | if ($style === null) { |
| 257 | $style = $this->_fetchMode; |
| 258 | } |
| 259 | $lob_as_string = $this->getLobAsString() ? \OCI_RETURN_LOBS : 0; |
| 260 | switch ($style) { |
| 261 | case \Zend_Db::FETCH_NUM: |
| 262 | $row = \oci_fetch_array($this->_stmt, \OCI_NUM | \OCI_RETURN_NULLS | $lob_as_string); |
| 263 | break; |
| 264 | case \Zend_Db::FETCH_ASSOC: |
| 265 | $row = \oci_fetch_array($this->_stmt, \OCI_ASSOC | \OCI_RETURN_NULLS | $lob_as_string); |
| 266 | break; |
| 267 | case \Zend_Db::FETCH_BOTH: |
| 268 | $row = \oci_fetch_array($this->_stmt, \OCI_BOTH | \OCI_RETURN_NULLS | $lob_as_string); |
| 269 | break; |
| 270 | case \Zend_Db::FETCH_OBJ: |
| 271 | $row = \oci_fetch_object($this->_stmt); |
| 272 | break; |
| 273 | case \Zend_Db::FETCH_BOUND: |
| 274 | $row = \oci_fetch_array($this->_stmt, \OCI_BOTH | \OCI_RETURN_NULLS | $lob_as_string); |
| 275 | if ($row !== \false) { |
| 276 | return $this->_fetchBound($row); |
| 277 | } |
| 278 | break; |
| 279 | default: |
| 280 | /** |
| 281 | * @see Zend_Db_Adapter_Oracle_Exception |
| 282 | */ |
| 283 | // require_once 'Zend/Db/Statement/Oracle/Exception.php'; |
| 284 | throw new \Zend_Db_Statement_Oracle_Exception(array('code' => 'HYC00', 'message' => "Invalid fetch mode '{$style}' specified")); |
| 285 | break; |
| 286 | } |
| 287 | if (!$row && ($error = \oci_error($this->_stmt))) { |
| 288 | /** |
| 289 | * @see Zend_Db_Adapter_Oracle_Exception |
| 290 | */ |
| 291 | // require_once 'Zend/Db/Statement/Oracle/Exception.php'; |
| 292 | throw new \Zend_Db_Statement_Oracle_Exception($error); |
| 293 | } |
| 294 | if (\is_array($row) && \array_key_exists('zend_db_rownum', $row)) { |
| 295 | unset($row['zend_db_rownum']); |
| 296 | } |
| 297 | return $row; |
| 298 | } |
| 299 | /** |
| 300 | * Returns an array containing all of the result set rows. |
| 301 | * |
| 302 | * @param int $style OPTIONAL Fetch mode. |
| 303 | * @param int $col OPTIONAL Column number, if fetch mode is by column. |
| 304 | * @return array Collection of rows, each in a format by the fetch mode. |
| 305 | * @throws Zend_Db_Statement_Exception |
| 306 | */ |
| 307 | public function fetchAll($style = null, $col = 0) |
| 308 | { |
| 309 | if (!$this->_stmt) { |
| 310 | return \false; |
| 311 | } |
| 312 | // make sure we have a fetch mode |
| 313 | if ($style === null) { |
| 314 | $style = $this->_fetchMode; |
| 315 | } |
| 316 | $flags = \OCI_FETCHSTATEMENT_BY_ROW; |
| 317 | switch ($style) { |
| 318 | case \Zend_Db::FETCH_BOTH: |
| 319 | /** |
| 320 | * @see Zend_Db_Adapter_Oracle_Exception |
| 321 | */ |
| 322 | // require_once 'Zend/Db/Statement/Oracle/Exception.php'; |
| 323 | throw new \Zend_Db_Statement_Oracle_Exception(array('code' => 'HYC00', 'message' => "OCI8 driver does not support fetchAll(FETCH_BOTH), use fetch() in a loop instead")); |
| 324 | // notreached |
| 325 | $flags |= \OCI_NUM; |
| 326 | $flags |= \OCI_ASSOC; |
| 327 | break; |
| 328 | case \Zend_Db::FETCH_NUM: |
| 329 | $flags |= \OCI_NUM; |
| 330 | break; |
| 331 | case \Zend_Db::FETCH_ASSOC: |
| 332 | $flags |= \OCI_ASSOC; |
| 333 | break; |
| 334 | case \Zend_Db::FETCH_OBJ: |
| 335 | break; |
| 336 | case \Zend_Db::FETCH_COLUMN: |
| 337 | $flags = $flags & ~\OCI_FETCHSTATEMENT_BY_ROW; |
| 338 | $flags |= \OCI_FETCHSTATEMENT_BY_COLUMN; |
| 339 | $flags |= \OCI_NUM; |
| 340 | break; |
| 341 | default: |
| 342 | /** |
| 343 | * @see Zend_Db_Adapter_Oracle_Exception |
| 344 | */ |
| 345 | // require_once 'Zend/Db/Statement/Oracle/Exception.php'; |
| 346 | throw new \Zend_Db_Statement_Oracle_Exception(array('code' => 'HYC00', 'message' => "Invalid fetch mode '{$style}' specified")); |
| 347 | break; |
| 348 | } |
| 349 | $result = array(); |
| 350 | if ($flags != \OCI_FETCHSTATEMENT_BY_ROW) { |
| 351 | /* not Zend_Db::FETCH_OBJ */ |
| 352 | if (!($rows = \oci_fetch_all($this->_stmt, $result, 0, -1, $flags))) { |
| 353 | if ($error = \oci_error($this->_stmt)) { |
| 354 | /** |
| 355 | * @see Zend_Db_Adapter_Oracle_Exception |
| 356 | */ |
| 357 | // require_once 'Zend/Db/Statement/Oracle/Exception.php'; |
| 358 | throw new \Zend_Db_Statement_Oracle_Exception($error); |
| 359 | } |
| 360 | if (!$rows) { |
| 361 | return array(); |
| 362 | } |
| 363 | } |
| 364 | if ($style == \Zend_Db::FETCH_COLUMN) { |
| 365 | $result = $result[$col]; |
| 366 | } |
| 367 | foreach ($result as &$row) { |
| 368 | if (\is_array($row) && \array_key_exists('zend_db_rownum', $row)) { |
| 369 | unset($row['zend_db_rownum']); |
| 370 | } |
| 371 | } |
| 372 | } else { |
| 373 | while (($row = \oci_fetch_object($this->_stmt)) !== \false) { |
| 374 | $result[] = $row; |
| 375 | } |
| 376 | if ($error = \oci_error($this->_stmt)) { |
| 377 | /** |
| 378 | * @see Zend_Db_Adapter_Oracle_Exception |
| 379 | */ |
| 380 | // require_once 'Zend/Db/Statement/Oracle/Exception.php'; |
| 381 | throw new \Zend_Db_Statement_Oracle_Exception($error); |
| 382 | } |
| 383 | } |
| 384 | return $result; |
| 385 | } |
| 386 | /** |
| 387 | * Returns a single column from the next row of a result set. |
| 388 | * |
| 389 | * @param int $col OPTIONAL Position of the column to fetch. |
| 390 | * @return string |
| 391 | * @throws Zend_Db_Statement_Exception |
| 392 | */ |
| 393 | public function fetchColumn($col = 0) |
| 394 | { |
| 395 | if (!$this->_stmt) { |
| 396 | return \false; |
| 397 | } |
| 398 | if (!\oci_fetch($this->_stmt)) { |
| 399 | // if no error, there is simply no record |
| 400 | if (!($error = \oci_error($this->_stmt))) { |
| 401 | return \false; |
| 402 | } |
| 403 | /** |
| 404 | * @see Zend_Db_Adapter_Oracle_Exception |
| 405 | */ |
| 406 | // require_once 'Zend/Db/Statement/Oracle/Exception.php'; |
| 407 | throw new \Zend_Db_Statement_Oracle_Exception($error); |
| 408 | } |
| 409 | $data = \oci_result($this->_stmt, $col + 1); |
| 410 | //1-based |
| 411 | if ($data === \false) { |
| 412 | /** |
| 413 | * @see Zend_Db_Adapter_Oracle_Exception |
| 414 | */ |
| 415 | // require_once 'Zend/Db/Statement/Oracle/Exception.php'; |
| 416 | throw new \Zend_Db_Statement_Oracle_Exception(\oci_error($this->_stmt)); |
| 417 | } |
| 418 | if ($this->getLobAsString()) { |
| 419 | // instanceof doesn't allow '-', we must use a temporary string |
| 420 | $type = 'OCI-Lob'; |
| 421 | if ($data instanceof $type) { |
| 422 | $data = $data->read($data->size()); |
| 423 | } |
| 424 | } |
| 425 | return $data; |
| 426 | } |
| 427 | /** |
| 428 | * Fetches the next row and returns it as an object. |
| 429 | * |
| 430 | * @param string $class OPTIONAL Name of the class to create. |
| 431 | * @param array $config OPTIONAL Constructor arguments for the class. |
| 432 | * @return mixed One object instance of the specified class. |
| 433 | * @throws Zend_Db_Statement_Exception |
| 434 | */ |
| 435 | public function fetchObject($class = 'stdClass', array $config = array()) |
| 436 | { |
| 437 | if (!$this->_stmt) { |
| 438 | return \false; |
| 439 | } |
| 440 | $obj = \oci_fetch_object($this->_stmt); |
| 441 | if ($error = \oci_error($this->_stmt)) { |
| 442 | /** |
| 443 | * @see Zend_Db_Adapter_Oracle_Exception |
| 444 | */ |
| 445 | // require_once 'Zend/Db/Statement/Oracle/Exception.php'; |
| 446 | throw new \Zend_Db_Statement_Oracle_Exception($error); |
| 447 | } |
| 448 | /* @todo XXX handle parameters */ |
| 449 | return $obj; |
| 450 | } |
| 451 | /** |
| 452 | * Retrieves the next rowset (result set) for a SQL statement that has |
| 453 | * multiple result sets. An example is a stored procedure that returns |
| 454 | * the results of multiple queries. |
| 455 | * |
| 456 | * @return bool |
| 457 | * @throws Zend_Db_Statement_Exception |
| 458 | */ |
| 459 | public function nextRowset() |
| 460 | { |
| 461 | /** |
| 462 | * @see Zend_Db_Statement_Oracle_Exception |
| 463 | */ |
| 464 | // require_once 'Zend/Db/Statement/Oracle/Exception.php'; |
| 465 | throw new \Zend_Db_Statement_Oracle_Exception(array('code' => 'HYC00', 'message' => 'Optional feature not implemented')); |
| 466 | } |
| 467 | /** |
| 468 | * Returns the number of rows affected by the execution of the |
| 469 | * last INSERT, DELETE, or UPDATE statement executed by this |
| 470 | * statement object. |
| 471 | * |
| 472 | * @return int The number of rows affected. |
| 473 | * @throws Zend_Db_Statement_Exception |
| 474 | */ |
| 475 | public function rowCount() |
| 476 | { |
| 477 | if (!$this->_stmt) { |
| 478 | return \false; |
| 479 | } |
| 480 | $num_rows = \oci_num_rows($this->_stmt); |
| 481 | if ($num_rows === \false) { |
| 482 | /** |
| 483 | * @see Zend_Db_Adapter_Oracle_Exception |
| 484 | */ |
| 485 | // require_once 'Zend/Db/Statement/Oracle/Exception.php'; |
| 486 | throw new \Zend_Db_Statement_Oracle_Exception(\oci_error($this->_stmt)); |
| 487 | } |
| 488 | return $num_rows; |
| 489 | } |
| 490 | } |
| 491 | } |
| 492 |