PageRenderTime 63ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/ railoapacheportable/php/PEAR/MDB2/Driver/oci8.php

http://railoapacheportable.googlecode.com/
PHP | 1514 lines | 1097 code | 89 blank | 328 comment | 207 complexity | de0f5761819216fd8580e3aba853eacc MD5 | raw file
Possible License(s): BSD-2-Clause, Apache-2.0, EPL-1.0, LGPL-3.0, GPL-3.0, AGPL-1.0, LGPL-2.0, MPL-2.0-no-copyleft-exception, GPL-2.0, LGPL-2.1, BSD-3-Clause, AGPL-3.0, CC-BY-SA-3.0
  1. <?php
  2. // vim: set et ts=4 sw=4 fdm=marker:
  3. // +----------------------------------------------------------------------+
  4. // | PHP versions 4 and 5 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox, |
  7. // | Stig. S. Bakken, Lukas Smith |
  8. // | All rights reserved. |
  9. // +----------------------------------------------------------------------+
  10. // | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB |
  11. // | API as well as database abstraction for PHP applications. |
  12. // | This LICENSE is in the BSD license style. |
  13. // | |
  14. // | Redistribution and use in source and binary forms, with or without |
  15. // | modification, are permitted provided that the following conditions |
  16. // | are met: |
  17. // | |
  18. // | Redistributions of source code must retain the above copyright |
  19. // | notice, this list of conditions and the following disclaimer. |
  20. // | |
  21. // | Redistributions in binary form must reproduce the above copyright |
  22. // | notice, this list of conditions and the following disclaimer in the |
  23. // | documentation and/or other materials provided with the distribution. |
  24. // | |
  25. // | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |
  26. // | Lukas Smith nor the names of his contributors may be used to endorse |
  27. // | or promote products derived from this software without specific prior|
  28. // | written permission. |
  29. // | |
  30. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
  31. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
  32. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
  33. // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
  34. // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
  35. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  36. // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
  37. // | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
  38. // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
  39. // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
  40. // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
  41. // | POSSIBILITY OF SUCH DAMAGE. |
  42. // +----------------------------------------------------------------------+
  43. // | Author: Lukas Smith <smith@pooteeweet.org> |
  44. // +----------------------------------------------------------------------+
  45. // $Id: oci8.php,v 1.192 2007/03/04 22:27:11 quipo Exp $
  46. /**
  47. * MDB2 OCI8 driver
  48. *
  49. * @package MDB2
  50. * @category Database
  51. * @author Lukas Smith <smith@pooteeweet.org>
  52. */
  53. class MDB2_Driver_oci8 extends MDB2_Driver_Common
  54. {
  55. // {{{ properties
  56. var $string_quoting = array('start' => "'", 'end' => "'", 'escape' => "'", 'escape_pattern' => '@');
  57. var $identifier_quoting = array('start' => '"', 'end' => '"', 'escape' => '"');
  58. var $uncommitedqueries = 0;
  59. // }}}
  60. // {{{ constructor
  61. /**
  62. * Constructor
  63. */
  64. function __construct()
  65. {
  66. parent::__construct();
  67. $this->phptype = 'oci8';
  68. $this->dbsyntax = 'oci8';
  69. $this->supported['sequences'] = true;
  70. $this->supported['indexes'] = true;
  71. $this->supported['summary_functions'] = true;
  72. $this->supported['order_by_text'] = true;
  73. $this->supported['current_id'] = true;
  74. $this->supported['affected_rows'] = true;
  75. $this->supported['transactions'] = true;
  76. $this->supported['savepoints'] = true;
  77. $this->supported['limit_queries'] = true;
  78. $this->supported['LOBs'] = true;
  79. $this->supported['replace'] = 'emulated';
  80. $this->supported['sub_selects'] = true;
  81. $this->supported['auto_increment'] = false; // implementation is broken
  82. $this->supported['primary_key'] = true;
  83. $this->supported['result_introspection'] = true;
  84. $this->supported['prepared_statements'] = true;
  85. $this->supported['identifier_quoting'] = true;
  86. $this->supported['pattern_escaping'] = true;
  87. $this->supported['new_link'] = true;
  88. $this->options['DBA_username'] = false;
  89. $this->options['DBA_password'] = false;
  90. $this->options['database_name_prefix'] = false;
  91. $this->options['emulate_database'] = true;
  92. $this->options['default_tablespace'] = false;
  93. $this->options['default_text_field_length'] = 2000;
  94. $this->options['result_prefetching'] = false;
  95. }
  96. // }}}
  97. // {{{ errorInfo()
  98. /**
  99. * This method is used to collect information about an error
  100. *
  101. * @param integer $error
  102. * @return array
  103. * @access public
  104. */
  105. function errorInfo($error = null)
  106. {
  107. if (is_resource($error)) {
  108. $error_data = @OCIError($error);
  109. $error = null;
  110. } elseif ($this->connection) {
  111. $error_data = @OCIError($this->connection);
  112. } else {
  113. $error_data = @OCIError();
  114. }
  115. $native_code = $error_data['code'];
  116. $native_msg = $error_data['message'];
  117. if (is_null($error)) {
  118. static $ecode_map;
  119. if (empty($ecode_map)) {
  120. $ecode_map = array(
  121. 1 => MDB2_ERROR_CONSTRAINT,
  122. 900 => MDB2_ERROR_SYNTAX,
  123. 904 => MDB2_ERROR_NOSUCHFIELD,
  124. 911 => MDB2_ERROR_SYNTAX, //invalid character
  125. 913 => MDB2_ERROR_VALUE_COUNT_ON_ROW,
  126. 921 => MDB2_ERROR_SYNTAX,
  127. 923 => MDB2_ERROR_SYNTAX,
  128. 942 => MDB2_ERROR_NOSUCHTABLE,
  129. 955 => MDB2_ERROR_ALREADY_EXISTS,
  130. 1400 => MDB2_ERROR_CONSTRAINT_NOT_NULL,
  131. 1401 => MDB2_ERROR_INVALID,
  132. 1407 => MDB2_ERROR_CONSTRAINT_NOT_NULL,
  133. 1418 => MDB2_ERROR_NOT_FOUND,
  134. 1476 => MDB2_ERROR_DIVZERO,
  135. 1722 => MDB2_ERROR_INVALID_NUMBER,
  136. 2289 => MDB2_ERROR_NOSUCHTABLE,
  137. 2291 => MDB2_ERROR_CONSTRAINT,
  138. 2292 => MDB2_ERROR_CONSTRAINT,
  139. 2449 => MDB2_ERROR_CONSTRAINT,
  140. 24344 => MDB2_ERROR_SYNTAX, //success with compilation error
  141. );
  142. }
  143. if (isset($ecode_map[$native_code])) {
  144. $error = $ecode_map[$native_code];
  145. }
  146. }
  147. return array($error, $native_code, $native_msg);
  148. }
  149. // }}}
  150. // {{{ beginTransaction()
  151. /**
  152. * Start a transaction or set a savepoint.
  153. *
  154. * @param string name of a savepoint to set
  155. * @return mixed MDB2_OK on success, a MDB2 error on failure
  156. *
  157. * @access public
  158. */
  159. function beginTransaction($savepoint = null)
  160. {
  161. $this->debug('Starting transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint));
  162. if (!is_null($savepoint)) {
  163. if (!$this->in_transaction) {
  164. return $this->raiseError(MDB2_ERROR_INVALID, null, null,
  165. 'savepoint cannot be released when changes are auto committed', __FUNCTION__);
  166. }
  167. $query = 'SAVEPOINT '.$savepoint;
  168. return $this->_doQuery($query, true);
  169. } elseif ($this->in_transaction) {
  170. return MDB2_OK; //nothing to do
  171. }
  172. if (!$this->destructor_registered && $this->opened_persistent) {
  173. $this->destructor_registered = true;
  174. register_shutdown_function('MDB2_closeOpenTransactions');
  175. }
  176. $this->in_transaction = true;
  177. ++$this->uncommitedqueries;
  178. return MDB2_OK;
  179. }
  180. // }}}
  181. // {{{ commit()
  182. /**
  183. * Commit the database changes done during a transaction that is in
  184. * progress or release a savepoint. This function may only be called when
  185. * auto-committing is disabled, otherwise it will fail. Therefore, a new
  186. * transaction is implicitly started after committing the pending changes.
  187. *
  188. * @param string name of a savepoint to release
  189. * @return mixed MDB2_OK on success, a MDB2 error on failure
  190. *
  191. * @access public
  192. */
  193. function commit($savepoint = null)
  194. {
  195. $this->debug('Committing transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint));
  196. if (!$this->in_transaction) {
  197. return $this->raiseError(MDB2_ERROR_INVALID, null, null,
  198. 'commit/release savepoint cannot be done changes are auto committed', __FUNCTION__);
  199. }
  200. if (!is_null($savepoint)) {
  201. return MDB2_OK;
  202. }
  203. if ($this->uncommitedqueries) {
  204. $connection = $this->getConnection();
  205. if (PEAR::isError($connection)) {
  206. return $connection;
  207. }
  208. if (!@OCICommit($connection)) {
  209. return $this->raiseError(null, null, null,
  210. 'Unable to commit transaction', __FUNCTION__);
  211. }
  212. $this->uncommitedqueries = 0;
  213. }
  214. $this->in_transaction = false;
  215. return MDB2_OK;
  216. }
  217. // }}}
  218. // {{{ rollback()
  219. /**
  220. * Cancel any database changes done during a transaction or since a specific
  221. * savepoint that is in progress. This function may only be called when
  222. * auto-committing is disabled, otherwise it will fail. Therefore, a new
  223. * transaction is implicitly started after canceling the pending changes.
  224. *
  225. * @param string name of a savepoint to rollback to
  226. * @return mixed MDB2_OK on success, a MDB2 error on failure
  227. *
  228. * @access public
  229. */
  230. function rollback($savepoint = null)
  231. {
  232. $this->debug('Rolling back transaction/savepoint', __FUNCTION__, array('is_manip' => true, 'savepoint' => $savepoint));
  233. if (!$this->in_transaction) {
  234. return $this->raiseError(MDB2_ERROR_INVALID, null, null,
  235. 'rollback cannot be done changes are auto committed', __FUNCTION__);
  236. }
  237. if (!is_null($savepoint)) {
  238. $query = 'ROLLBACK TO SAVEPOINT '.$savepoint;
  239. return $this->_doQuery($query, true);
  240. }
  241. if ($this->uncommitedqueries) {
  242. $connection = $this->getConnection();
  243. if (PEAR::isError($connection)) {
  244. return $connection;
  245. }
  246. if (!@OCIRollback($connection)) {
  247. return $this->raiseError(null, null, null,
  248. 'Unable to rollback transaction', __FUNCTION__);
  249. }
  250. $this->uncommitedqueries = 0;
  251. }
  252. $this->in_transaction = false;
  253. return MDB2_OK;
  254. }
  255. // }}}
  256. // {{{ function setTransactionIsolation()
  257. /**
  258. * Set the transacton isolation level.
  259. *
  260. * @param string standard isolation level
  261. * READ UNCOMMITTED (allows dirty reads)
  262. * READ COMMITTED (prevents dirty reads)
  263. * REPEATABLE READ (prevents nonrepeatable reads)
  264. * SERIALIZABLE (prevents phantom reads)
  265. * @return mixed MDB2_OK on success, a MDB2 error on failure
  266. *
  267. * @access public
  268. * @since 2.1.1
  269. */
  270. function setTransactionIsolation($isolation)
  271. {
  272. $this->debug('Setting transaction isolation level', __FUNCTION__, array('is_manip' => true));
  273. switch ($isolation) {
  274. case 'READ UNCOMMITTED':
  275. $isolation = 'READ COMMITTED';
  276. case 'READ COMMITTED':
  277. case 'REPEATABLE READ':
  278. $isolation = 'SERIALIZABLE';
  279. case 'SERIALIZABLE':
  280. break;
  281. default:
  282. return $this->raiseError(MDB2_ERROR_UNSUPPORTED, null, null,
  283. 'isolation level is not supported: '.$isolation, __FUNCTION__);
  284. }
  285. $query = "ALTER SESSION ISOLATION LEVEL $isolation";
  286. return $this->_doQuery($query, true);
  287. }
  288. // }}}
  289. // {{{ _doConnect()
  290. /**
  291. * do the grunt work of the connect
  292. *
  293. * @return connection on success or MDB2 Error Object on failure
  294. * @access protected
  295. */
  296. function _doConnect($username, $password, $persistent = false)
  297. {
  298. if (!PEAR::loadExtension($this->phptype)) {
  299. return $this->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  300. 'extension '.$this->phptype.' is not compiled into PHP', __FUNCTION__);
  301. }
  302. $sid = '';
  303. if (!empty($this->dsn['service']) && $this->dsn['hostspec']) {
  304. //oci8://username:password@foo.example.com[:port]/?service=service
  305. // service name is given, it is assumed that hostspec is really a
  306. // hostname, we try to construct an oracle connection string from this
  307. $port = $this->dsn['port'] ? $this->dsn['port'] : 1521;
  308. $sid = sprintf("(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)
  309. (HOST=%s) (PORT=%s)))
  310. (CONNECT_DATA=(SERVICE_NAME=%s)))",
  311. $this->dsn['hostspec'],
  312. $port,
  313. $this->dsn['service']
  314. );
  315. } elseif ($this->dsn['hostspec']) {
  316. // we are given something like 'oci8://username:password@foo/'
  317. // we have hostspec but not a service name, now we assume that
  318. // hostspec is a tnsname defined in tnsnames.ora
  319. $sid = $this->dsn['hostspec'];
  320. } else {
  321. // oci://username:password@
  322. // if everything fails, we have to rely on environment variables
  323. // not before a check to 'emulate_database'
  324. if (!$this->options['emulate_database'] && $this->database_name) {
  325. $sid = $this->database_name;
  326. } elseif (getenv('ORACLE_SID')) {
  327. $sid = getenv('ORACLE_SID');
  328. } elseif ($sid = getenv('TWO_TASK')) {
  329. $sid = getenv('TWO_TASK');
  330. } else {
  331. return $this->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  332. 'not a valid connection string or environment variable [ORACLE_SID|TWO_TASK] not set',
  333. __FUNCTION__);
  334. }
  335. }
  336. if (function_exists('oci_connect')) {
  337. if (isset($this->dsn['new_link'])
  338. && ($this->dsn['new_link'] == 'true' || $this->dsn['new_link'] === true)
  339. ) {
  340. $connect_function = 'oci_new_connect';
  341. } else {
  342. $connect_function = $persistent ? 'oci_pconnect' : 'oci_connect';
  343. }
  344. $charset = empty($this->dsn['charset']) ? null : $this->dsn['charset'];
  345. $connection = @$connect_function($username, $password, $sid, $charset);
  346. $error = @OCIError();
  347. if (isset($error['code']) && $error['code'] == 12541) {
  348. // Couldn't find TNS listener. Try direct connection.
  349. $connection = @$connect_function($username, $password, null, $charset);
  350. }
  351. } else {
  352. $connect_function = $persistent ? 'OCIPLogon' : 'OCILogon';
  353. $connection = @$connect_function($username, $password, $sid);
  354. if (!empty($this->dsn['charset'])) {
  355. $result = $this->setCharset($this->dsn['charset'], $connection);
  356. if (PEAR::isError($result)) {
  357. return $result;
  358. }
  359. }
  360. }
  361. if (!$connection) {
  362. return $this->raiseError(MDB2_ERROR_CONNECT_FAILED, null, null,
  363. 'unable to establish a connection', __FUNCTION__);
  364. }
  365. if (empty($this->dsn['disable_iso_date'])) {
  366. $query = "ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD HH24:MI:SS'";
  367. $err =& $this->_doQuery($query, true, $connection);
  368. if (PEAR::isError($err)) {
  369. $this->disconnect(false);
  370. return $err;
  371. }
  372. }
  373. $query = "ALTER SESSION SET NLS_NUMERIC_CHARACTERS='. '";
  374. $err =& $this->_doQuery($query, true, $connection);
  375. if (PEAR::isError($err)) {
  376. $this->disconnect(false);
  377. return $err;
  378. }
  379. return $connection;
  380. }
  381. // }}}
  382. // {{{ connect()
  383. /**
  384. * Connect to the database
  385. *
  386. * @return MDB2_OK on success, MDB2 Error Object on failure
  387. * @access public
  388. */
  389. function connect()
  390. {
  391. if ($this->database_name && $this->options['emulate_database']) {
  392. $this->dsn['username'] = $this->options['database_name_prefix'].$this->database_name;
  393. }
  394. if (is_resource($this->connection)) {
  395. if (count(array_diff($this->connected_dsn, $this->dsn)) == 0
  396. && $this->connected_database_name == $this->database_name
  397. && $this->opened_persistent == $this->options['persistent']
  398. ) {
  399. return MDB2_OK;
  400. }
  401. $this->disconnect(false);
  402. }
  403. $connection = $this->_doConnect(
  404. $this->dsn['username'],
  405. $this->dsn['password'],
  406. $this->options['persistent']
  407. );
  408. if (PEAR::isError($connection)) {
  409. return $connection;
  410. }
  411. $this->connection = $connection;
  412. $this->connected_dsn = $this->dsn;
  413. $this->connected_database_name = $this->database_name;
  414. $this->opened_persistent = $this->options['persistent'];
  415. $this->dbsyntax = $this->dsn['dbsyntax'] ? $this->dsn['dbsyntax'] : $this->phptype;
  416. $this->as_keyword = ' ';
  417. $server_info = $this->getServerVersion();
  418. if (is_array($server_info)) {
  419. if ($server_info['major'] >= '10') {
  420. $this->as_keyword = ' AS ';
  421. }
  422. }
  423. return MDB2_OK;
  424. }
  425. // }}}
  426. // {{{ disconnect()
  427. /**
  428. * Log out and disconnect from the database.
  429. *
  430. * @param boolean $force if the disconnect should be forced even if the
  431. * connection is opened persistently
  432. * @return mixed true on success, false if not connected and error
  433. * object on error
  434. * @access public
  435. */
  436. function disconnect($force = true)
  437. {
  438. if (is_resource($this->connection)) {
  439. if ($this->in_transaction) {
  440. $dsn = $this->dsn;
  441. $database_name = $this->database_name;
  442. $persistent = $this->options['persistent'];
  443. $this->dsn = $this->connected_dsn;
  444. $this->database_name = $this->connected_database_name;
  445. $this->options['persistent'] = $this->opened_persistent;
  446. $this->rollback();
  447. $this->dsn = $dsn;
  448. $this->database_name = $database_name;
  449. $this->options['persistent'] = $persistent;
  450. }
  451. if (!$this->opened_persistent || $force) {
  452. if (function_exists('oci_close')) {
  453. @oci_close($this->connection);
  454. } else {
  455. @OCILogOff($this->connection);
  456. }
  457. }
  458. $this->uncommitedqueries = 0;
  459. }
  460. return parent::disconnect($force);
  461. }
  462. // }}}
  463. // {{{ standaloneExec()
  464. /**
  465. * execute a query as database administrator
  466. *
  467. * @param string $query the SQL query
  468. * @return mixed MDB2_OK on success, a MDB2 error on failure
  469. * @access public
  470. */
  471. function &standaloneExec($query)
  472. {
  473. $connection = $this->_doConnect(
  474. $this->options['DBA_username'],
  475. $this->options['DBA_password'],
  476. $this->options['persistent']
  477. );
  478. if (PEAR::isError($connection)) {
  479. return $connection;
  480. }
  481. $offset = $this->offset;
  482. $limit = $this->limit;
  483. $this->offset = $this->limit = 0;
  484. $query = $this->_modifyQuery($query, false, $limit, $offset);
  485. $result =& $this->_doQuery($query, false, $connection, false);
  486. if (PEAR::isError($result)) {
  487. @OCILogOff($connection);
  488. return $result;
  489. }
  490. $ret = $this->_affectedRows($connection, $result);
  491. @OCILogOff($connection);
  492. return $ret;
  493. }
  494. // }}}
  495. // {{{ standaloneQuery()
  496. /**
  497. * execute a query as DBA
  498. *
  499. * @param string $query the SQL query
  500. * @param mixed $types array that contains the types of the columns in
  501. * the result set
  502. * @param boolean $is_manip if the query is a manipulation query
  503. * @return mixed MDB2_OK on success, a MDB2 error on failure
  504. * @access public
  505. */
  506. function &standaloneQuery($query, $types = null, $is_manip = false)
  507. {
  508. $connection = $this->_doConnect(
  509. $this->options['DBA_username'],
  510. $this->options['DBA_password'],
  511. $this->options['persistent']
  512. );
  513. if (PEAR::isError($connection)) {
  514. return $connection;
  515. }
  516. $offset = $this->offset;
  517. $limit = $this->limit;
  518. $this->offset = $this->limit = 0;
  519. $query = $this->_modifyQuery($query, $is_manip, $limit, $offset);
  520. $result =& $this->_doQuery($query, $is_manip, $connection, false);
  521. @OCILogOff($connection);
  522. if (PEAR::isError($result)) {
  523. return $result;
  524. }
  525. if ($is_manip) {
  526. $affected_rows = $this->_affectedRows($connection, $result);
  527. return $affected_rows;
  528. }
  529. $return =& $this->_wrapResult($result, $types, true, false, $limit, $offset);
  530. return $return;
  531. }
  532. // }}}
  533. // {{{ _modifyQuery()
  534. /**
  535. * Changes a query string for various DBMS specific reasons
  536. *
  537. * @param string $query query to modify
  538. * @param boolean $is_manip if it is a DML query
  539. * @param integer $limit limit the number of rows
  540. * @param integer $offset start reading from given offset
  541. * @return string modified query
  542. * @access protected
  543. */
  544. function _modifyQuery($query, $is_manip, $limit, $offset)
  545. {
  546. if (preg_match('/^\s*SELECT/i', $query)) {
  547. if (!preg_match('/\sFROM\s/i', $query)) {
  548. $query.= " FROM dual";
  549. }
  550. if ($limit > 0) {
  551. // taken from http://svn.ez.no/svn/ezcomponents/packages/Database
  552. $max = $offset + $limit;
  553. if ($offset > 0) {
  554. $min = $offset + 1;
  555. $query = "SELECT * FROM (SELECT a.*, ROWNUM mdb2rn FROM ($query) a WHERE ROWNUM <= $max) WHERE mdb2rn >= $min";
  556. } else {
  557. $query = "SELECT a.* FROM ($query) a WHERE ROWNUM <= $max";
  558. }
  559. }
  560. }
  561. return $query;
  562. }
  563. // }}}
  564. // {{{ _doQuery()
  565. /**
  566. * Execute a query
  567. * @param string $query query
  568. * @param boolean $is_manip if the query is a manipulation query
  569. * @param resource $connection
  570. * @param string $database_name
  571. * @return result or error object
  572. * @access protected
  573. */
  574. function &_doQuery($query, $is_manip = false, $connection = null, $database_name = null)
  575. {
  576. $this->last_query = $query;
  577. $result = $this->debug($query, 'query', array('is_manip' => $is_manip, 'when' => 'pre'));
  578. if ($result) {
  579. if (PEAR::isError($result)) {
  580. return $result;
  581. }
  582. $query = $result;
  583. }
  584. if ($this->getOption('disable_query')) {
  585. if ($is_manip) {
  586. return 0;
  587. }
  588. return null;
  589. }
  590. if (is_null($connection)) {
  591. $connection = $this->getConnection();
  592. if (PEAR::isError($connection)) {
  593. return $connection;
  594. }
  595. }
  596. $result = @OCIParse($connection, $query);
  597. if (!$result) {
  598. $err = $this->raiseError(null, null, null,
  599. 'Could not create statement', __FUNCTION__);
  600. return $err;
  601. }
  602. $mode = $this->in_transaction ? OCI_DEFAULT : OCI_COMMIT_ON_SUCCESS;
  603. if (!@OCIExecute($result, $mode)) {
  604. $err =& $this->raiseError($result, null, null,
  605. 'Could not execute statement', __FUNCTION__);
  606. return $err;
  607. }
  608. if (is_numeric($this->options['result_prefetching'])) {
  609. @ocisetprefetch($result, $this->options['result_prefetching']);
  610. }
  611. $this->debug($query, 'query', array('is_manip' => $is_manip, 'when' => 'post', 'result' => $result));
  612. return $result;
  613. }
  614. // }}}
  615. // {{{ _affectedRows()
  616. /**
  617. * Returns the number of rows affected
  618. *
  619. * @param resource $result
  620. * @param resource $connection
  621. * @return mixed MDB2 Error Object or the number of rows affected
  622. * @access private
  623. */
  624. function _affectedRows($connection, $result = null)
  625. {
  626. if (is_null($connection)) {
  627. $connection = $this->getConnection();
  628. if (PEAR::isError($connection)) {
  629. return $connection;
  630. }
  631. }
  632. return @OCIRowCount($result);
  633. }
  634. // }}}
  635. // {{{ getServerVersion()
  636. /**
  637. * return version information about the server
  638. *
  639. * @param bool $native determines if the raw version string should be returned
  640. * @return mixed array/string with version information or MDB2 error object
  641. * @access public
  642. */
  643. function getServerVersion($native = false)
  644. {
  645. $connection = $this->getConnection();
  646. if (PEAR::isError($connection)) {
  647. return $connection;
  648. }
  649. if ($this->connected_server_info) {
  650. $server_info = $this->connected_server_info;
  651. } else {
  652. $server_info = @ociserverversion($connection);
  653. }
  654. if (!$server_info) {
  655. return $this->raiseError(null, null, null,
  656. 'Could not get server information', __FUNCTION__);
  657. }
  658. // cache server_info
  659. $this->connected_server_info = $server_info;
  660. if (!$native) {
  661. if (!preg_match('/ (\d+)\.(\d+)\.(\d+)\.([\d\.]+) /', $server_info, $tmp)) {
  662. return $this->raiseError(MDB2_ERROR_INVALID, null, null,
  663. 'Could not parse version information:'.$server_info, __FUNCTION__);
  664. }
  665. $server_info = array(
  666. 'major' => $tmp[1],
  667. 'minor' => $tmp[2],
  668. 'patch' => $tmp[3],
  669. 'extra' => $tmp[4],
  670. 'native' => $server_info,
  671. );
  672. }
  673. return $server_info;
  674. }
  675. // }}}
  676. // {{{ prepare()
  677. /**
  678. * Prepares a query for multiple execution with execute().
  679. * With some database backends, this is emulated.
  680. * prepare() requires a generic query as string like
  681. * 'INSERT INTO numbers VALUES(?,?)' or
  682. * 'INSERT INTO numbers VALUES(:foo,:bar)'.
  683. * The ? and :[a-zA-Z] and are placeholders which can be set using
  684. * bindParam() and the query can be send off using the execute() method.
  685. *
  686. * @param string $query the query to prepare
  687. * @param mixed $types array that contains the types of the placeholders
  688. * @param mixed $result_types array that contains the types of the columns in
  689. * the result set or MDB2_PREPARE_RESULT, if set to
  690. * MDB2_PREPARE_MANIP the query is handled as a manipulation query
  691. * @param mixed $lobs key (field) value (parameter) pair for all lob placeholders
  692. * @return mixed resource handle for the prepared query on success, a MDB2
  693. * error on failure
  694. * @access public
  695. * @see bindParam, execute
  696. */
  697. function &prepare($query, $types = null, $result_types = null, $lobs = array())
  698. {
  699. if ($this->options['emulate_prepared']) {
  700. $obj =& parent::prepare($query, $types, $result_types, $lobs);
  701. return $obj;
  702. }
  703. $is_manip = ($result_types === MDB2_PREPARE_MANIP);
  704. $offset = $this->offset;
  705. $limit = $this->limit;
  706. $this->offset = $this->limit = 0;
  707. $result = $this->debug($query, __FUNCTION__, array('is_manip' => $is_manip, 'when' => 'pre'));
  708. if ($result) {
  709. if (PEAR::isError($result)) {
  710. return $result;
  711. }
  712. $query = $result;
  713. }
  714. $query = $this->_modifyQuery($query, $is_manip, $limit, $offset);
  715. $placeholder_type_guess = $placeholder_type = null;
  716. $question = '?';
  717. $colon = ':';
  718. $positions = array();
  719. $position = 0;
  720. $parameter = -1;
  721. while ($position < strlen($query)) {
  722. $q_position = strpos($query, $question, $position);
  723. $c_position = strpos($query, $colon, $position);
  724. if ($q_position && $c_position) {
  725. $p_position = min($q_position, $c_position);
  726. } elseif ($q_position) {
  727. $p_position = $q_position;
  728. } elseif ($c_position) {
  729. $p_position = $c_position;
  730. } else {
  731. break;
  732. }
  733. if (is_null($placeholder_type)) {
  734. $placeholder_type_guess = $query[$p_position];
  735. }
  736. $new_pos = $this->_skipDelimitedStrings($query, $position, $p_position);
  737. if (PEAR::isError($new_pos)) {
  738. return $new_pos;
  739. }
  740. if ($new_pos != $position) {
  741. $position = $new_pos;
  742. continue; //evaluate again starting from the new position
  743. }
  744. if ($query[$position] == $placeholder_type_guess) {
  745. if (is_null($placeholder_type)) {
  746. $placeholder_type = $query[$p_position];
  747. $question = $colon = $placeholder_type;
  748. if (!empty($types) && is_array($types)) {
  749. if ($placeholder_type == ':') {
  750. if (is_int(key($types))) {
  751. $types_tmp = $types;
  752. $types = array();
  753. $count = -1;
  754. }
  755. } else {
  756. $types = array_values($types);
  757. }
  758. }
  759. }
  760. if ($placeholder_type == ':') {
  761. $parameter = preg_replace('/^.{'.($position+1).'}([a-z0-9_]+).*$/si', '\\1', $query);
  762. if ($parameter === '') {
  763. $err =& $this->raiseError(MDB2_ERROR_SYNTAX, null, null,
  764. 'named parameter with an empty name', __FUNCTION__);
  765. return $err;
  766. }
  767. // use parameter name in type array
  768. if (isset($count) && isset($types_tmp[++$count])) {
  769. $types[$parameter] = $types_tmp[$count];
  770. }
  771. $length = strlen($parameter) + 1;
  772. } else {
  773. ++$parameter;
  774. $length = strlen($parameter);
  775. }
  776. if (!in_array($parameter, $positions)) {
  777. $positions[] = $parameter;
  778. }
  779. if (isset($types[$parameter])
  780. && ($types[$parameter] == 'clob' || $types[$parameter] == 'blob')
  781. ) {
  782. if (!isset($lobs[$parameter])) {
  783. $lobs[$parameter] = $parameter;
  784. }
  785. $value = $this->quote(true, $types[$parameter]);
  786. $query = substr_replace($query, $value, $p_position, $length);
  787. $position = $p_position + strlen($value) - 1;
  788. } elseif ($placeholder_type == '?') {
  789. $query = substr_replace($query, ':'.$parameter, $p_position, 1);
  790. $position = $p_position + $length;
  791. } else {
  792. $position = $p_position + 1;
  793. }
  794. } else {
  795. $position = $p_position;
  796. }
  797. }
  798. if (is_array($lobs)) {
  799. $columns = $variables = '';
  800. foreach ($lobs as $parameter => $field) {
  801. $columns.= ($columns ? ', ' : ' RETURNING ').$field;
  802. $variables.= ($variables ? ', ' : ' INTO ').':'.$parameter;
  803. }
  804. $query.= $columns.$variables;
  805. }
  806. $connection = $this->getConnection();
  807. if (PEAR::isError($connection)) {
  808. return $connection;
  809. }
  810. $statement = @OCIParse($connection, $query);
  811. if (!$statement) {
  812. $err =& $this->raiseError(null, null, null,
  813. 'Could not create statement', __FUNCTION__);
  814. return $err;
  815. }
  816. $class_name = 'MDB2_Statement_'.$this->phptype;
  817. $obj =& new $class_name($this, $statement, $positions, $query, $types, $result_types, $is_manip, $limit, $offset);
  818. $this->debug($query, __FUNCTION__, array('is_manip' => $is_manip, 'when' => 'post', 'result' => $obj));
  819. return $obj;
  820. }
  821. // }}}
  822. // {{{ nextID()
  823. /**
  824. * Returns the next free id of a sequence
  825. *
  826. * @param string $seq_name name of the sequence
  827. * @param boolean $ondemand when true the sequence is
  828. * automatic created, if it
  829. * not exists
  830. * @return mixed MDB2 Error Object or id
  831. * @access public
  832. */
  833. function nextID($seq_name, $ondemand = true)
  834. {
  835. $sequence_name = $this->quoteIdentifier($this->getSequenceName($seq_name), true);
  836. $query = "SELECT $sequence_name.nextval FROM DUAL";
  837. $this->expectError(MDB2_ERROR_NOSUCHTABLE);
  838. $result = $this->queryOne($query, 'integer');
  839. $this->popExpect();
  840. if (PEAR::isError($result)) {
  841. if ($ondemand && $result->getCode() == MDB2_ERROR_NOSUCHTABLE) {
  842. $this->loadModule('Manager', null, true);
  843. $result = $this->manager->createSequence($seq_name);
  844. if (PEAR::isError($result)) {
  845. return $result;
  846. }
  847. return $this->nextId($seq_name, false);
  848. }
  849. }
  850. return $result;
  851. }
  852. // }}}
  853. // {{{ lastInsertID()
  854. /**
  855. * Returns the autoincrement ID if supported or $id or fetches the current
  856. * ID in a sequence called: $table.(empty($field) ? '' : '_'.$field)
  857. *
  858. * @param string $table name of the table into which a new row was inserted
  859. * @param string $field name of the field into which a new row was inserted
  860. * @return mixed MDB2 Error Object or id
  861. * @access public
  862. */
  863. function lastInsertID($table = null, $field = null)
  864. {
  865. $seq = $table.(empty($field) ? '' : '_'.$field);
  866. $sequence_name = $this->quoteIdentifier($this->getSequenceName($seq), true);
  867. return $this->queryOne("SELECT $sequence_name.currval", 'integer');
  868. }
  869. // }}}
  870. // {{{ currId()
  871. /**
  872. * Returns the current id of a sequence
  873. *
  874. * @param string $seq_name name of the sequence
  875. * @return mixed MDB2_Error or id
  876. * @access public
  877. */
  878. function currId($seq_name)
  879. {
  880. $sequence_name = $this->getSequenceName($seq_name);
  881. $query = 'SELECT (last_number-1) FROM user_sequences';
  882. $query.= ' WHERE sequence_name='.$this->quote($sequence_name, 'text');
  883. $query.= ' OR sequence_name='.$this->quote(strtoupper($sequence_name), 'text');
  884. return $this->queryOne($query, 'integer');
  885. }
  886. }
  887. /**
  888. * MDB2 OCI8 result driver
  889. *
  890. * @package MDB2
  891. * @category Database
  892. * @author Lukas Smith <smith@pooteeweet.org>
  893. */
  894. class MDB2_Result_oci8 extends MDB2_Result_Common
  895. {
  896. // }}}
  897. // {{{ fetchRow()
  898. /**
  899. * Fetch a row and insert the data into an existing array.
  900. *
  901. * @param int $fetchmode how the array data should be indexed
  902. * @param int $rownum number of the row where the data can be found
  903. * @return int data array on success, a MDB2 error on failure
  904. * @access public
  905. */
  906. function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT, $rownum = null)
  907. {
  908. if (!is_null($rownum)) {
  909. $seek = $this->seek($rownum);
  910. if (PEAR::isError($seek)) {
  911. return $seek;
  912. }
  913. }
  914. if ($fetchmode == MDB2_FETCHMODE_DEFAULT) {
  915. $fetchmode = $this->db->fetchmode;
  916. }
  917. if ($fetchmode & MDB2_FETCHMODE_ASSOC) {
  918. @OCIFetchInto($this->result, $row, OCI_ASSOC+OCI_RETURN_NULLS);
  919. if (is_array($row)
  920. && $this->db->options['portability'] & MDB2_PORTABILITY_FIX_CASE
  921. ) {
  922. $row = array_change_key_case($row, $this->db->options['field_case']);
  923. }
  924. } else {
  925. @OCIFetchInto($this->result, $row, OCI_RETURN_NULLS);
  926. }
  927. if (!$row) {
  928. if ($this->result === false) {
  929. $err =& $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
  930. 'resultset has already been freed', __FUNCTION__);
  931. return $err;
  932. }
  933. $null = null;
  934. return $null;
  935. }
  936. // remove additional column at the end
  937. if ($this->offset > 0) {
  938. array_pop($row);
  939. }
  940. $mode = 0;
  941. $rtrim = false;
  942. if ($this->db->options['portability'] & MDB2_PORTABILITY_RTRIM) {
  943. if (empty($this->types)) {
  944. $mode += MDB2_PORTABILITY_RTRIM;
  945. } else {
  946. $rtrim = true;
  947. }
  948. }
  949. if ($mode) {
  950. $this->db->_fixResultArrayValues($row, $mode);
  951. }
  952. if (!empty($this->types)) {
  953. $row = $this->db->datatype->convertResultRow($this->types, $row, $rtrim);
  954. }
  955. if (!empty($this->values)) {
  956. $this->_assignBindColumns($row);
  957. }
  958. if ($fetchmode === MDB2_FETCHMODE_OBJECT) {
  959. $object_class = $this->db->options['fetch_class'];
  960. if ($object_class == 'stdClass') {
  961. $row = (object) $row;
  962. } else {
  963. $row = &new $object_class($row);
  964. }
  965. }
  966. ++$this->rownum;
  967. return $row;
  968. }
  969. // }}}
  970. // {{{ _getColumnNames()
  971. /**
  972. * Retrieve the names of columns returned by the DBMS in a query result.
  973. *
  974. * @return mixed Array variable that holds the names of columns as keys
  975. * or an MDB2 error on failure.
  976. * Some DBMS may not return any columns when the result set
  977. * does not contain any rows.
  978. * @access private
  979. */
  980. function _getColumnNames()
  981. {
  982. $columns = array();
  983. $numcols = $this->numCols();
  984. if (PEAR::isError($numcols)) {
  985. return $numcols;
  986. }
  987. for ($column = 0; $column < $numcols; $column++) {
  988. $column_name = @OCIColumnName($this->result, $column + 1);
  989. $columns[$column_name] = $column;
  990. }
  991. if ($this->db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  992. $columns = array_change_key_case($columns, $this->db->options['field_case']);
  993. }
  994. return $columns;
  995. }
  996. // }}}
  997. // {{{ numCols()
  998. /**
  999. * Count the number of columns returned by the DBMS in a query result.
  1000. *
  1001. * @return mixed integer value with the number of columns, a MDB2 error
  1002. * on failure
  1003. * @access public
  1004. */
  1005. function numCols()
  1006. {
  1007. $cols = @OCINumCols($this->result);
  1008. if (is_null($cols)) {
  1009. if ($this->result === false) {
  1010. return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
  1011. 'resultset has already been freed', __FUNCTION__);
  1012. } elseif (is_null($this->result)) {
  1013. return count($this->types);
  1014. }
  1015. return $this->db->raiseError(null, null, null,
  1016. 'Could not get column count', __FUNCTION__);
  1017. }
  1018. if ($this->offset > 0) {
  1019. --$cols;
  1020. }
  1021. return $cols;
  1022. }
  1023. // }}}
  1024. // {{{ free()
  1025. /**
  1026. * Free the internal resources associated with $result.
  1027. *
  1028. * @return boolean true on success, false if $result is invalid
  1029. * @access public
  1030. */
  1031. function free()
  1032. {
  1033. if (is_resource($this->result) && $this->db->connection) {
  1034. $free = @OCIFreeCursor($this->result);
  1035. if ($free === false) {
  1036. return $this->db->raiseError(null, null, null,
  1037. 'Could not free result', __FUNCTION__);
  1038. }
  1039. }
  1040. $this->result = false;
  1041. return MDB2_OK;
  1042. }
  1043. }
  1044. /**
  1045. * MDB2 OCI8 buffered result driver
  1046. *
  1047. * @package MDB2
  1048. * @category Database
  1049. * @author Lukas Smith <smith@pooteeweet.org>
  1050. */
  1051. class MDB2_BufferedResult_oci8 extends MDB2_Result_oci8
  1052. {
  1053. var $buffer;
  1054. var $buffer_rownum = - 1;
  1055. // {{{ _fillBuffer()
  1056. /**
  1057. * Fill the row buffer
  1058. *
  1059. * @param int $rownum row number upto which the buffer should be filled
  1060. if the row number is null all rows are ready into the buffer
  1061. * @return boolean true on success, false on failure
  1062. * @access protected
  1063. */
  1064. function _fillBuffer($rownum = null)
  1065. {
  1066. if (isset($this->buffer) && is_array($this->buffer)) {
  1067. if (is_null($rownum)) {
  1068. if (!end($this->buffer)) {
  1069. return false;
  1070. }
  1071. } elseif (isset($this->buffer[$rownum])) {
  1072. return (bool)$this->buffer[$rownum];
  1073. }
  1074. }
  1075. $row = true;
  1076. while ((is_null($rownum) || $this->buffer_rownum < $rownum)
  1077. && ($row = @OCIFetchInto($this->result, $buffer, OCI_RETURN_NULLS))
  1078. ) {
  1079. ++$this->buffer_rownum;
  1080. // remove additional column at the end
  1081. if ($this->offset > 0) {
  1082. array_pop($buffer);
  1083. }
  1084. if (empty($this->types)) {
  1085. foreach (array_keys($buffer) as $key) {
  1086. if (is_a($buffer[$key], 'oci-lob')) {
  1087. $buffer[$key] = $buffer[$key]->load();
  1088. }
  1089. }
  1090. }
  1091. $this->buffer[$this->buffer_rownum] = $buffer;
  1092. }
  1093. if (!$row) {
  1094. ++$this->buffer_rownum;
  1095. $this->buffer[$this->buffer_rownum] = false;
  1096. return false;
  1097. }
  1098. return true;
  1099. }
  1100. // }}}
  1101. // {{{ fetchRow()
  1102. /**
  1103. * Fetch a row and insert the data into an existing array.
  1104. *
  1105. * @param int $fetchmode how the array data should be indexed
  1106. * @param int $rownum number of the row where the data can be found
  1107. * @return int data array on success, a MDB2 error on failure
  1108. * @access public
  1109. */
  1110. function &fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT, $rownum = null)
  1111. {
  1112. if ($this->result === false) {
  1113. $err =& $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
  1114. 'resultset has already been freed', __FUNCTION__);
  1115. return $err;
  1116. } elseif (is_null($this->result)) {
  1117. return null;
  1118. }
  1119. if (!is_null($rownum)) {
  1120. $seek = $this->seek($rownum);
  1121. if (PEAR::isError($seek)) {
  1122. return $seek;
  1123. }
  1124. }
  1125. $target_rownum = $this->rownum + 1;
  1126. if ($fetchmode == MDB2_FETCHMODE_DEFAULT) {
  1127. $fetchmode = $this->db->fetchmode;
  1128. }
  1129. if (!$this->_fillBuffer($target_rownum)) {
  1130. $null = null;
  1131. return $null;
  1132. }
  1133. $row = $this->buffer[$target_rownum];
  1134. if ($fetchmode & MDB2_FETCHMODE_ASSOC) {
  1135. $column_names = $this->getColumnNames();
  1136. foreach ($column_names as $name => $i) {
  1137. $column_names[$name] = $row[$i];
  1138. }
  1139. $row = $column_names;
  1140. }
  1141. $mode = 0;
  1142. $rtrim = false;
  1143. if ($this->db->options['portability'] & MDB2_PORTABILITY_RTRIM) {
  1144. if (empty($this->types)) {
  1145. $mode += MDB2_PORTABILITY_RTRIM;
  1146. } else {
  1147. $rtrim = true;
  1148. }
  1149. }
  1150. if ($mode) {
  1151. $this->db->_fixResultArrayValues($row, $mode);
  1152. }
  1153. if (!empty($this->types)) {
  1154. $row = $this->db->datatype->convertResultRow($this->types, $row, $rtrim);
  1155. }
  1156. if (!empty($this->values)) {
  1157. $this->_assignBindColumns($row);
  1158. }
  1159. if ($fetchmode === MDB2_FETCHMODE_OBJECT) {
  1160. $object_class = $this->db->options['fetch_class'];
  1161. if ($object_class == 'stdClass') {
  1162. $row = (object) $row;
  1163. } else {
  1164. $row = &new $object_class($row);
  1165. }
  1166. }
  1167. ++$this->rownum;
  1168. return $row;
  1169. }
  1170. // }}}
  1171. // {{{ seek()
  1172. /**
  1173. * Seek to a specific row in a result set
  1174. *
  1175. * @param int $rownum number of the row where the data can be found
  1176. * @return mixed MDB2_OK on success, a MDB2 error on failure
  1177. * @access public
  1178. */
  1179. function seek($rownum = 0)
  1180. {
  1181. if ($this->result === false) {
  1182. return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
  1183. 'resultset has already been freed', __FUNCTION__);
  1184. }
  1185. $this->rownum = $rownum - 1;
  1186. return MDB2_OK;
  1187. }
  1188. // }}}
  1189. // {{{ valid()
  1190. /**
  1191. * Check if the end of the result set has been reached
  1192. *
  1193. * @return mixed true or false on sucess, a MDB2 error on failure
  1194. * @access public
  1195. */
  1196. function valid()
  1197. {
  1198. if ($this->result === false) {
  1199. return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
  1200. 'resultset has already been freed', __FUNCTION__);
  1201. } elseif (is_null($this->result)) {
  1202. return true;
  1203. }
  1204. if ($this->_fillBuffer($this->rownum + 1)) {
  1205. return true;
  1206. }
  1207. return false;
  1208. }
  1209. // }}}
  1210. // {{{ numRows()
  1211. /**
  1212. * Returns the number of rows in a result object
  1213. *
  1214. * @return mixed MDB2 Error Object or the number of rows
  1215. * @access public
  1216. */
  1217. function numRows()
  1218. {
  1219. if ($this->result === false) {
  1220. return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
  1221. 'resultset has already been freed', __FUNCTION__);
  1222. } elseif (is_null($this->result)) {
  1223. return 0;
  1224. }
  1225. $this->_fillBuffer();
  1226. return $this->buffer_rownum;
  1227. }
  1228. // }}}
  1229. // {{{ free()
  1230. /**
  1231. * Free the internal resources associated with $result.
  1232. *
  1233. * @return boolean true on success, false if $result is invalid
  1234. * @access public
  1235. */
  1236. function free()
  1237. {
  1238. $this->buffer = null;
  1239. $this->buffer_rownum = null;
  1240. return parent::free();
  1241. }
  1242. }
  1243. /**
  1244. * MDB2 OCI8 statement driver
  1245. *
  1246. * @package MDB2
  1247. * @category Database
  1248. * @author Lukas Smith <smith@pooteeweet.org>
  1249. */
  1250. class MDB2_Statement_oci8 extends MDB2_Statement_Common
  1251. {
  1252. // }}}
  1253. // {{{ _execute()
  1254. /**
  1255. * Execute a prepared query statement helper method.
  1256. *
  1257. * @param mixed $result_class string which specifies which result class to use
  1258. * @param mixed $result_wrap_class string which specifies which class to wrap results in
  1259. * @return mixed a result handle or MDB2_OK on success, a MDB2 error on failure
  1260. * @access private
  1261. */
  1262. function &_execute($result_class = true, $result_wrap_class = false)
  1263. {
  1264. if (is_null($this->statement)) {
  1265. $result =& parent::_execute($result_class, $result_wrap_class);
  1266. return $result;
  1267. }
  1268. $this->db->last_query = $this->query;
  1269. $this->db->debug($this->query, 'execute', array('is_manip' => $this->is_manip, 'when' => 'pre', 'parameters' => $this->values));
  1270. if ($this->db->getOption('disable_query')) {
  1271. $result = $this->is_manip ? 0 : null;
  1272. return $result;
  1273. }
  1274. $connection = $this->db->getConnection();
  1275. if (PEAR::isError($connection)) {
  1276. return $connection;
  1277. }
  1278. $result = MDB2_OK;
  1279. $lobs = $quoted_values = array();
  1280. $i = 0;
  1281. foreach ($this->positions as $parameter) {
  1282. if (!array_key_exists($parameter, $this->values)) {
  1283. return $this->db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  1284. 'Unable to bind to missing placeholder: '.$parameter, __FUNCTION__);
  1285. }
  1286. $value = $this->values[$parameter];
  1287. $type = array_key_exists($parameter, $this->types) ? $this->types[$parameter] : null;
  1288. if ($type == 'clob' || $type == 'blob') {
  1289. $lobs[$i]['file'] = false;
  1290. if (is_resource($value)) {
  1291. $fp = $value;
  1292. $value = '';
  1293. while (!feof($fp)) {
  1294. $value.= fread($fp, 8192);
  1295. }
  1296. } elseif (preg_match('/^(\w+:\/\/)(.*)$/', $value, $match)) {
  1297. $lobs[$i]['file'] = true;
  1298. if ($match[1] == 'file://') {
  1299. $value = $match[2];
  1300. }
  1301. }
  1302. $lobs[$i]['value'] = $value;
  1303. $lobs[$i]['descriptor'] = @OCINewDescriptor($connection, OCI_D_LOB);
  1304. if (!is_object($lobs[$i]['descriptor'])) {
  1305. $result = $this->db->raiseError(null, null, null,
  1306. 'Unable to create descriptor for LOB in parameter: '.$parameter, __FUNCTION__);
  1307. break;
  1308. }
  1309. $lob_type = ($type == 'blob' ? OCI_B_BLOB : OCI_B_CLOB);
  1310. if (!@OCIBindByName($this->statement, ':'.$parameter, $lobs[$i]['descriptor'], -1, $lob_type)) {
  1311. $result = $this->db->raiseError($this->statement, null, null,
  1312. 'could not bind LOB parameter', __FUNCTION__);
  1313. break;
  1314. }
  1315. } else {
  1316. $quoted_values[$i] = $this->db->quote($value, $type, false);
  1317. if (PEAR::isError($quoted_values[$i])) {
  1318. return $quoted_values[$i];
  1319. }
  1320. if (!@OCIBindByName($this->statement, ':'.$parameter, $quoted_values[$i])) {
  1321. $result = $this->db->raiseError($this->statement, null, null,
  1322. 'could not bind non LOB parameter', __FUNCTION__);
  1323. break;
  1324. }
  1325. }
  1326. ++$i;
  1327. }
  1328. $lob_keys = array_keys($lobs);
  1329. if (!PEAR::isError($result)) {
  1330. $mode = (!empty($lobs) || $this->db->in_transaction) ? OCI_DEFAULT : OCI_COMMIT_ON_SUCCESS;
  1331. if (!@OCIExecute($this->statement, $mode)) {
  1332. $err =& $this->db->raiseError($this->statement, null, null,
  1333. 'could not execute statement', __FUNCTION__);
  1334. return $err;
  1335. }
  1336. if (!empty($lobs)) {
  1337. foreach ($lob_keys as $i) {
  1338. if (!is_null($lobs[$i]['value']) && $lobs[$i]['value'] !== '') {
  1339. if ($lobs[$i]['file']) {
  1340. $result = $lobs[$i]['descriptor']->savefile($lobs[$i]['value']);
  1341. } else {
  1342. $result = $lobs[$i]['descriptor']->save($lobs[$i]['value']);
  1343. }
  1344. if (!$result) {
  1345. $result = $this->db->raiseError(null, null, null,
  1346. 'Unable to save descriptor contents', __FUNCTION__);
  1347. break;
  1348. }
  1349. }
  1350. }
  1351. if (!PEAR::isError($result)) {
  1352. if (!$this->db->in_transaction) {
  1353. if (!@OCICommit($connection)) {
  1354. $result = $this->db->raiseError(null, null, null,
  1355. 'Unable to commit transaction', __FUNCTION__);
  1356. }
  1357. } else {
  1358. ++$this->db->uncommitedqueries;
  1359. }
  1360. }
  1361. }
  1362. }
  1363. $lob_keys = array_keys($lobs);
  1364. foreach ($lob_keys as $i) {
  1365. $lobs[$i]['descriptor']->free();
  1366. }
  1367. if (PEAR::isError($result)) {
  1368. return $result;
  1369. }
  1370. if ($this->is_manip) {
  1371. $affected_rows = $this->db->_affectedRows($connection, $this->statement);
  1372. return $affected_rows;
  1373. }
  1374. $result =& $this->db->_wrapResult($this->statement, $this->result_types,
  1375. $result_class, $result_wrap_class, $this->limit, $this->offset);
  1376. $this->db->debug($this->query, 'execute', array('is_manip' => $this->is_manip, 'when' => 'post', 'result' => $result));
  1377. return $result;
  1378. }
  1379. // }}}
  1380. // {{{ free()
  1381. /**
  1382. * Release resources allocated for the specified prepared query.
  1383. *
  1384. * @return mixed MDB2_OK on success, a MDB2 error on failure
  1385. * @access public
  1386. */
  1387. function free()
  1388. {
  1389. if (is_null($this->positions)) {
  1390. return $this->db->raiseError(MDB2_ERROR, null, null,
  1391. 'Prepared statement has already been freed', __FUNCTION__);
  1392. }
  1393. $result = MDB2_OK;
  1394. if (!is_null($this->statement) && !@OCIFreeStatement($this->statement)) {
  1395. $result = $this->db->raiseError(null, null, null,
  1396. 'Could not free statement', __FUNCTION__);
  1397. }
  1398. parent::free();
  1399. return $result;
  1400. }
  1401. }
  1402. ?>