oci8.php 62 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668
  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$
  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 (MDB2::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 (MDB2::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 (!extension_loaded($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 (MDB2::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 (MDB2::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 (MDB2::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 (MDB2::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 (MDB2::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 (MDB2::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 (MDB2::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 (MDB2::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 (!MDB2::isError($result)) {
  545. if ($is_manip) {
  546. $result = $this->_affectedRows($connection, $result);
  547. } else {
  548. $result = $this->_wrapResult($result, $types, true, true, $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 (MDB2::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 (MDB2::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 (MDB2::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 (MDB2::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 (MDB2::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 (MDB2::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 (MDB2::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 (MDB2::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 (MDB2::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 (MDB2::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 (MDB2::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 (MDB2::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. || $fetchmode == MDB2_FETCHMODE_OBJECT
  954. ) {
  955. @OCIFetchInto($this->result, $row, OCI_ASSOC+OCI_RETURN_NULLS);
  956. if (is_array($row)
  957. && $this->db->options['portability'] & MDB2_PORTABILITY_FIX_CASE
  958. ) {
  959. $row = array_change_key_case($row, $this->db->options['field_case']);
  960. }
  961. } else {
  962. @OCIFetchInto($this->result, $row, OCI_RETURN_NULLS);
  963. }
  964. if (!$row) {
  965. if (false === $this->result) {
  966. $err = $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
  967. 'resultset has already been freed', __FUNCTION__);
  968. return $err;
  969. }
  970. return null;
  971. }
  972. // remove additional column at the end
  973. if ($this->offset > 0) {
  974. array_pop($row);
  975. }
  976. $mode = 0;
  977. $rtrim = false;
  978. if ($this->db->options['portability'] & MDB2_PORTABILITY_RTRIM) {
  979. if (empty($this->types)) {
  980. $mode += MDB2_PORTABILITY_RTRIM;
  981. } else {
  982. $rtrim = true;
  983. }
  984. }
  985. if ($mode) {
  986. $this->db->_fixResultArrayValues($row, $mode);
  987. }
  988. if ( ( $fetchmode != MDB2_FETCHMODE_ASSOC
  989. && $fetchmode != MDB2_FETCHMODE_OBJECT)
  990. && !empty($this->types)
  991. ) {
  992. $row = $this->db->datatype->convertResultRow($this->types, $row, $rtrim);
  993. } elseif (($fetchmode == MDB2_FETCHMODE_ASSOC
  994. || $fetchmode == MDB2_FETCHMODE_OBJECT)
  995. && !empty($this->types_assoc)
  996. ) {
  997. $row = $this->db->datatype->convertResultRow($this->types_assoc, $row, $rtrim);
  998. }
  999. if (!empty($this->values)) {
  1000. $this->_assignBindColumns($row);
  1001. }
  1002. if ($fetchmode === MDB2_FETCHMODE_OBJECT) {
  1003. $object_class = $this->db->options['fetch_class'];
  1004. if ($object_class == 'stdClass') {
  1005. $row = (object) $row;
  1006. } else {
  1007. $rowObj = new $object_class($row);
  1008. $row = $rowObj;
  1009. }
  1010. }
  1011. ++$this->rownum;
  1012. return $row;
  1013. }
  1014. // }}}
  1015. // {{{ _getColumnNames()
  1016. /**
  1017. * Retrieve the names of columns returned by the DBMS in a query result.
  1018. *
  1019. * @return mixed Array variable that holds the names of columns as keys
  1020. * or an MDB2 error on failure.
  1021. * Some DBMS may not return any columns when the result set
  1022. * does not contain any rows.
  1023. * @access private
  1024. */
  1025. function _getColumnNames()
  1026. {
  1027. $columns = array();
  1028. $numcols = $this->numCols();
  1029. if (MDB2::isError($numcols)) {
  1030. return $numcols;
  1031. }
  1032. for ($column = 0; $column < $numcols; $column++) {
  1033. $column_name = @OCIColumnName($this->result, $column + 1);
  1034. $columns[$column_name] = $column;
  1035. }
  1036. if ($this->db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  1037. $columns = array_change_key_case($columns, $this->db->options['field_case']);
  1038. }
  1039. return $columns;
  1040. }
  1041. // }}}
  1042. // {{{ numCols()
  1043. /**
  1044. * Count the number of columns returned by the DBMS in a query result.
  1045. *
  1046. * @return mixed integer value with the number of columns, a MDB2 error
  1047. * on failure
  1048. * @access public
  1049. */
  1050. function numCols()
  1051. {
  1052. $cols = @OCINumCols($this->result);
  1053. if (null === $cols) {
  1054. if (false === $this->result) {
  1055. return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
  1056. 'resultset has already been freed', __FUNCTION__);
  1057. }
  1058. if (null === $this->result) {
  1059. return count($this->types);
  1060. }
  1061. return $this->db->raiseError(null, null, null,
  1062. 'Could not get column count', __FUNCTION__);
  1063. }
  1064. if ($this->offset > 0) {
  1065. --$cols;
  1066. }
  1067. return $cols;
  1068. }
  1069. // }}}
  1070. // {{{ free()
  1071. /**
  1072. * Free the internal resources associated with $result.
  1073. *
  1074. * @return boolean true on success, false if $result is invalid
  1075. * @access public
  1076. */
  1077. function free()
  1078. {
  1079. if (is_resource($this->result) && $this->db->connection) {
  1080. $free = @OCIFreeCursor($this->result);
  1081. if (false === $free) {
  1082. return $this->db->raiseError(null, null, null,
  1083. 'Could not free result', __FUNCTION__);
  1084. }
  1085. }
  1086. $this->result = false;
  1087. return MDB2_OK;
  1088. }
  1089. }
  1090. /**
  1091. * MDB2 OCI8 buffered result driver
  1092. *
  1093. * @package MDB2
  1094. * @category Database
  1095. * @author Lukas Smith <smith@pooteeweet.org>
  1096. */
  1097. class MDB2_BufferedResult_oci8 extends MDB2_Result_oci8
  1098. {
  1099. var $buffer;
  1100. var $buffer_rownum = - 1;
  1101. // {{{ _fillBuffer()
  1102. /**
  1103. * Fill the row buffer
  1104. *
  1105. * @param int $rownum row number upto which the buffer should be filled
  1106. if the row number is null all rows are ready into the buffer
  1107. * @return boolean true on success, false on failure
  1108. * @access protected
  1109. */
  1110. function _fillBuffer($rownum = null)
  1111. {
  1112. if (isset($this->buffer) && is_array($this->buffer)) {
  1113. if (null === $rownum) {
  1114. if (!end($this->buffer)) {
  1115. return false;
  1116. }
  1117. } elseif (isset($this->buffer[$rownum])) {
  1118. return (bool)$this->buffer[$rownum];
  1119. }
  1120. }
  1121. $row = true;
  1122. while (((null === $rownum) || $this->buffer_rownum < $rownum)
  1123. && ($row = @OCIFetchInto($this->result, $buffer, OCI_RETURN_NULLS))
  1124. ) {
  1125. ++$this->buffer_rownum;
  1126. // remove additional column at the end
  1127. if ($this->offset > 0) {
  1128. array_pop($buffer);
  1129. }
  1130. if (empty($this->types)) {
  1131. foreach (array_keys($buffer) as $key) {
  1132. if (is_object($buffer[$key]) && is_a($buffer[$key], 'oci-lob')) {
  1133. $buffer[$key] = $buffer[$key]->load();
  1134. }
  1135. }
  1136. }
  1137. $this->buffer[$this->buffer_rownum] = $buffer;
  1138. }
  1139. if (!$row) {
  1140. ++$this->buffer_rownum;
  1141. $this->buffer[$this->buffer_rownum] = false;
  1142. return false;
  1143. }
  1144. return true;
  1145. }
  1146. // }}}
  1147. // {{{ fetchRow()
  1148. /**
  1149. * Fetch a row and insert the data into an existing array.
  1150. *
  1151. * @param int $fetchmode how the array data should be indexed
  1152. * @param int $rownum number of the row where the data can be found
  1153. * @return int data array on success, a MDB2 error on failure
  1154. * @access public
  1155. */
  1156. function fetchRow($fetchmode = MDB2_FETCHMODE_DEFAULT, $rownum = null)
  1157. {
  1158. if (false === $this->result) {
  1159. $err = $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
  1160. 'resultset has already been freed', __FUNCTION__);
  1161. return $err;
  1162. }
  1163. if (null === $this->result) {
  1164. return null;
  1165. }
  1166. if (null !== $rownum) {
  1167. $seek = $this->seek($rownum);
  1168. if (MDB2::isError($seek)) {
  1169. return $seek;
  1170. }
  1171. }
  1172. $target_rownum = $this->rownum + 1;
  1173. if ($fetchmode == MDB2_FETCHMODE_DEFAULT) {
  1174. $fetchmode = $this->db->fetchmode;
  1175. }
  1176. if (!$this->_fillBuffer($target_rownum)) {
  1177. return null;
  1178. }
  1179. $row = $this->buffer[$target_rownum];
  1180. if ( $fetchmode == MDB2_FETCHMODE_ASSOC
  1181. || $fetchmode == MDB2_FETCHMODE_OBJECT
  1182. ) {
  1183. $column_names = $this->getColumnNames();
  1184. foreach ($column_names as $name => $i) {
  1185. $column_names[$name] = $row[$i];
  1186. }
  1187. $row = $column_names;
  1188. }
  1189. $mode = 0;
  1190. $rtrim = false;
  1191. if ($this->db->options['portability'] & MDB2_PORTABILITY_RTRIM) {
  1192. if (empty($this->types)) {
  1193. $mode += MDB2_PORTABILITY_RTRIM;
  1194. } else {
  1195. $rtrim = true;
  1196. }
  1197. }
  1198. if ($mode) {
  1199. $this->db->_fixResultArrayValues($row, $mode);
  1200. }
  1201. if ( ( $fetchmode != MDB2_FETCHMODE_ASSOC
  1202. && $fetchmode != MDB2_FETCHMODE_OBJECT)
  1203. && !empty($this->types)
  1204. ) {
  1205. $row = $this->db->datatype->convertResultRow($this->types, $row, $rtrim);
  1206. } elseif (($fetchmode == MDB2_FETCHMODE_ASSOC
  1207. || $fetchmode == MDB2_FETCHMODE_OBJECT)
  1208. && !empty($this->types_assoc)
  1209. ) {
  1210. $row = $this->db->datatype->convertResultRow($this->types_assoc, $row, $rtrim);
  1211. }
  1212. if (!empty($this->values)) {
  1213. $this->_assignBindColumns($row);
  1214. }
  1215. if ($fetchmode === MDB2_FETCHMODE_OBJECT) {
  1216. $object_class = $this->db->options['fetch_class'];
  1217. if ($object_class == 'stdClass') {
  1218. $row = (object) $row;
  1219. } else {
  1220. $rowObj = new $object_class($row);
  1221. $row = $rowObj;
  1222. }
  1223. }
  1224. ++$this->rownum;
  1225. return $row;
  1226. }
  1227. // }}}
  1228. // {{{ seek()
  1229. /**
  1230. * Seek to a specific row in a result set
  1231. *
  1232. * @param int $rownum number of the row where the data can be found
  1233. * @return mixed MDB2_OK on success, a MDB2 error on failure
  1234. * @access public
  1235. */
  1236. function seek($rownum = 0)
  1237. {
  1238. if (false === $this->result) {
  1239. return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
  1240. 'resultset has already been freed', __FUNCTION__);
  1241. }
  1242. $this->rownum = $rownum - 1;
  1243. return MDB2_OK;
  1244. }
  1245. // }}}
  1246. // {{{ valid()
  1247. /**
  1248. * Check if the end of the result set has been reached
  1249. *
  1250. * @return mixed true or false on sucess, a MDB2 error on failure
  1251. * @access public
  1252. */
  1253. function valid()
  1254. {
  1255. if (false === $this->result) {
  1256. return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
  1257. 'resultset has already been freed', __FUNCTION__);
  1258. }
  1259. if (null === $this->result) {
  1260. return true;
  1261. }
  1262. if ($this->_fillBuffer($this->rownum + 1)) {
  1263. return true;
  1264. }
  1265. return false;
  1266. }
  1267. // }}}
  1268. // {{{ numRows()
  1269. /**
  1270. * Returns the number of rows in a result object
  1271. *
  1272. * @return mixed MDB2 Error Object or the number of rows
  1273. * @access public
  1274. */
  1275. function numRows()
  1276. {
  1277. if (false === $this->result) {
  1278. return $this->db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
  1279. 'resultset has already been freed', __FUNCTION__);
  1280. }
  1281. if (null === $this->result) {
  1282. return 0;
  1283. }
  1284. $this->_fillBuffer();
  1285. return $this->buffer_rownum;
  1286. }
  1287. // }}}
  1288. // {{{ free()
  1289. /**
  1290. * Free the internal resources associated with $result.
  1291. *
  1292. * @return boolean true on success, false if $result is invalid
  1293. * @access public
  1294. */
  1295. function free()
  1296. {
  1297. $this->buffer = null;
  1298. $this->buffer_rownum = null;
  1299. return parent::free();
  1300. }
  1301. }
  1302. /**
  1303. * MDB2 OCI8 statement driver
  1304. *
  1305. * @package MDB2
  1306. * @category Database
  1307. * @author Lukas Smith <smith@pooteeweet.org>
  1308. */
  1309. class MDB2_Statement_oci8 extends MDB2_Statement_Common
  1310. {
  1311. // {{{ Variables (Properties)
  1312. var $type_maxlengths = array();
  1313. // }}}
  1314. // {{{ bindParam()
  1315. /**
  1316. * Bind a variable to a parameter of a prepared query.
  1317. *
  1318. * @param int $parameter the order number of the parameter in the query
  1319. * statement. The order number of the first parameter is 1.
  1320. * @param mixed &$value variable that is meant to be bound to specified
  1321. * parameter. The type of the value depends on the $type argument.
  1322. * @param string $type specifies the type of the field
  1323. * @param int $maxlength specifies the maximum length of the field; if set to -1, the
  1324. * current length of $value is used
  1325. *
  1326. * @return mixed MDB2_OK on success, a MDB2 error on failure
  1327. *
  1328. * @access public
  1329. */
  1330. function bindParam($parameter, &$value, $type = null, $maxlength = -1)
  1331. {
  1332. if (!is_numeric($parameter)) {
  1333. $parameter = preg_replace('/^:(.*)$/', '\\1', $parameter);
  1334. }
  1335. if (MDB2_OK === ($ret = parent::bindParam($parameter, $value, $type))) {
  1336. $this->type_maxlengths[$parameter] = $maxlength;
  1337. }
  1338. return $ret;
  1339. }
  1340. // }}}
  1341. // {{{ _execute()
  1342. /**
  1343. * Execute a prepared query statement helper method.
  1344. *
  1345. * @param mixed $result_class string which specifies which result class to use
  1346. * @param mixed $result_wrap_class string which specifies which class to wrap results in
  1347. *
  1348. * @return mixed MDB2_Result or integer (affected rows) on success,
  1349. * a MDB2 error on failure
  1350. * @access private
  1351. */
  1352. function _execute($result_class = true, $result_wrap_class = true)
  1353. {
  1354. if (null === $this->statement) {
  1355. return parent::_execute($result_class, $result_wrap_class);
  1356. }
  1357. $this->db->last_query = $this->query;
  1358. $this->db->debug($this->query, 'execute', array('is_manip' => $this->is_manip, 'when' => 'pre', 'parameters' => $this->values));
  1359. if ($this->db->getOption('disable_query')) {
  1360. $result = $this->is_manip ? 0 : null;
  1361. return $result;
  1362. }
  1363. $connection = $this->db->getConnection();
  1364. if (MDB2::isError($connection)) {
  1365. return $connection;
  1366. }
  1367. $result = MDB2_OK;
  1368. $lobs = $quoted_values = array();
  1369. $i = 0;
  1370. foreach ($this->positions as $parameter) {
  1371. if (!array_key_exists($parameter, $this->values)) {
  1372. return $this->db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  1373. 'Unable to bind to missing placeholder: '.$parameter, __FUNCTION__);
  1374. }
  1375. $type = array_key_exists($parameter, $this->types) ? $this->types[$parameter] : null;
  1376. if ($type == 'clob' || $type == 'blob') {
  1377. $lobs[$i]['file'] = false;
  1378. if (is_resource($this->values[$parameter])) {
  1379. $fp = $this->values[$parameter];
  1380. $this->values[$parameter] = '';
  1381. while (!feof($fp)) {
  1382. $this->values[$parameter] .= fread($fp, 8192);
  1383. }
  1384. } elseif (is_object($this->values[$parameter]) && is_a($this->values[$parameter], 'OCI-Lob')) {
  1385. //do nothing
  1386. } elseif ($this->db->getOption('lob_allow_url_include')
  1387. && preg_match('/^(\w+:\/\/)(.*)$/', $this->values[$parameter], $match)
  1388. ) {
  1389. $lobs[$i]['file'] = true;
  1390. if ($match[1] == 'file://') {
  1391. $this->values[$parameter] = $match[2];
  1392. }
  1393. }
  1394. $lobs[$i]['value'] = $this->values[$parameter];
  1395. $lobs[$i]['descriptor'] =& $this->values[$parameter];
  1396. // Test to see if descriptor has already been created for this
  1397. // variable (i.e. if it has been bound more than once):
  1398. if (!(is_object($this->values[$parameter]) && is_a($this->values[$parameter], 'OCI-Lob'))) {
  1399. $this->values[$parameter] = @OCINewDescriptor($connection, OCI_D_LOB);
  1400. if (false === $this->values[$parameter]) {
  1401. $result = $this->db->raiseError(null, null, null,
  1402. 'Unable to create descriptor for LOB in parameter: '.$parameter, __FUNCTION__);
  1403. break;
  1404. }
  1405. }
  1406. $lob_type = ($type == 'blob' ? OCI_B_BLOB : OCI_B_CLOB);
  1407. if (!@OCIBindByName($this->statement, ':'.$parameter, $lobs[$i]['descriptor'], -1, $lob_type)) {
  1408. $result = $this->db->raiseError($this->statement, null, null,
  1409. 'could not bind LOB parameter', __FUNCTION__);
  1410. break;
  1411. }
  1412. } else if ($type == OCI_B_BFILE) {
  1413. // Test to see if descriptor has already been created for this
  1414. // variable (i.e. if it has been bound more than once):
  1415. if (!(is_object($this->values[$parameter]) && is_a($this->values[$parameter], "OCI-Lob"))) {
  1416. $this->values[$parameter] = @OCINewDescriptor($connection, OCI_D_FILE);
  1417. if (false === $this->values[$parameter]) {
  1418. $result = $this->db->raiseError(null, null, null,
  1419. 'Unable to create descriptor for BFILE in parameter: '.$parameter, __FUNCTION__);
  1420. break;
  1421. }
  1422. }
  1423. if (!@OCIBindByName($this->statement, ':'.$parameter, $this->values[$parameter], -1, $type)) {
  1424. $result = $this->db->raiseError($this->statement, null, null,
  1425. 'Could not bind BFILE parameter', __FUNCTION__);
  1426. break;
  1427. }
  1428. } else if ($type == OCI_B_ROWID) {
  1429. // Test to see if descriptor has already been created for this
  1430. // variable (i.e. if it has been bound more than once):
  1431. if (!is_a($this->values[$parameter], "OCI-Lob")) {
  1432. $this->values[$parameter] = @OCINewDescriptor($connection, OCI_D_ROWID);
  1433. if (false === $this->values[$parameter]) {
  1434. $result = $this->db->raiseError(null, null, null,
  1435. 'Unable to create descriptor for ROWID in parameter: '.$parameter, __FUNCTION__);
  1436. break;
  1437. }
  1438. }
  1439. if (!@OCIBindByName($this->statement, ':'.$parameter, $this->values[$parameter], -1, $type)) {
  1440. $result = $this->db->raiseError($this->statement, null, null,
  1441. 'Could not bind ROWID parameter', __FUNCTION__);
  1442. break;
  1443. }
  1444. } else if ($type == OCI_B_CURSOR) {
  1445. // Test to see if cursor has already been allocated for this
  1446. // variable (i.e. if it has been bound more than once):
  1447. if (!is_resource($this->values[$parameter]) || !get_resource_type($this->values[$parameter]) == "oci8 statement") {
  1448. $this->values[$parameter] = @OCINewCursor($connection);
  1449. if (false === $this->values[$parameter]) {
  1450. $result = $this->db->raiseError(null, null, null,
  1451. 'Unable to allocate cursor for parameter: '.$parameter, __FUNCTION__);
  1452. break;
  1453. }
  1454. }
  1455. if (!@OCIBindByName($this->statement, ':'.$parameter, $this->values[$parameter], -1, $type)) {
  1456. $result = $this->db->raiseError($this->statement, null, null,
  1457. 'Could not bind CURSOR parameter', __FUNCTION__);
  1458. break;
  1459. }
  1460. } else {
  1461. $maxlength = array_key_exists($parameter, $this->type_maxlengths) ? $this->type_maxlengths[$parameter] : -1;
  1462. $this->values[$parameter] = $this->db->quote($this->values[$parameter], $type, false);
  1463. $quoted_values[$i] =& $this->values[$parameter];
  1464. if (MDB2::isError($quoted_values[$i])) {
  1465. return $quoted_values[$i];
  1466. }
  1467. if (!@OCIBindByName($this->statement, ':'.$parameter, $quoted_values[$i], $maxlength)) {
  1468. $result = $this->db->raiseError($this->statement, null, null,
  1469. 'could not bind non-abstract parameter', __FUNCTION__);
  1470. break;
  1471. }
  1472. }
  1473. ++$i;
  1474. }
  1475. $lob_keys = array_keys($lobs);
  1476. if (!MDB2::isError($result)) {
  1477. $mode = (!empty($lobs) || $this->db->in_transaction) ? OCI_DEFAULT : OCI_COMMIT_ON_SUCCESS;
  1478. if (!@OCIExecute($this->statement, $mode)) {
  1479. $err = $this->db->raiseError($this->statement, null, null,
  1480. 'could not execute statement', __FUNCTION__);
  1481. return $err;
  1482. }
  1483. if (!empty($lobs)) {
  1484. foreach ($lob_keys as $i) {
  1485. if ((null !== $lobs[$i]['value']) && $lobs[$i]['value'] !== '') {
  1486. if (is_object($lobs[$i]['value'])) {
  1487. // Probably a NULL LOB
  1488. // @see http://bugs.php.net/bug.php?id=27485
  1489. continue;
  1490. }
  1491. if ($lobs[$i]['file']) {
  1492. $result = $lobs[$i]['descriptor']->savefile($lobs[$i]['value']);
  1493. } else {
  1494. $result = $lobs[$i]['descriptor']->save($lobs[$i]['value']);
  1495. }
  1496. if (!$result) {
  1497. $result = $this->db->raiseError(null, null, null,
  1498. 'Unable to save descriptor contents', __FUNCTION__);
  1499. break;
  1500. }
  1501. }
  1502. }
  1503. if (!MDB2::isError($result)) {
  1504. if (!$this->db->in_transaction) {
  1505. if (!@OCICommit($connection)) {
  1506. $result = $this->db->raiseError(null, null, null,
  1507. 'Unable to commit transaction', __FUNCTION__);
  1508. }
  1509. } else {
  1510. ++$this->db->uncommitedqueries;
  1511. }
  1512. }
  1513. }
  1514. }
  1515. if (MDB2::isError($result)) {
  1516. return $result;
  1517. }
  1518. if ($this->is_manip) {
  1519. $affected_rows = $this->db->_affectedRows($connection, $this->statement);
  1520. return $affected_rows;
  1521. }
  1522. $result = $this->db->_wrapResult($this->statement, $this->result_types,
  1523. $result_class, $result_wrap_class, $this->limit, $this->offset);
  1524. $this->db->debug($this->query, 'execute', array('is_manip' => $this->is_manip, 'when' => 'post', 'result' => $result));
  1525. return $result;
  1526. }
  1527. // }}}
  1528. // {{{ free()
  1529. /**
  1530. * Release resources allocated for the specified prepared query.
  1531. *
  1532. * @return mixed MDB2_OK on success, a MDB2 error on failure
  1533. * @access public
  1534. */
  1535. function free()
  1536. {
  1537. if (null === $this->positions) {
  1538. return $this->db->raiseError(MDB2_ERROR, null, null,
  1539. 'Prepared statement has already been freed', __FUNCTION__);
  1540. }
  1541. $result = MDB2_OK;
  1542. if ((null !== $this->statement) && !@OCIFreeStatement($this->statement)) {
  1543. $result = $this->db->raiseError(null, null, null,
  1544. 'Could not free statement', __FUNCTION__);
  1545. }
  1546. parent::free();
  1547. return $result;
  1548. }
  1549. }
  1550. ?>