Oracle.php 17 KB

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