searchcode logo
Dogecoin is an open source peer-to-peer cryptocurrency

zf2 /library/Zend/Db/Statement/Oracle.php

git clone git://github.com/zendframework/zf2.git

Language

PHP

MD5 Hash

ddab1bf5072157d81d4d883f64fdf2c6

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