oci8.php 61 KB

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