oci8.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 foldmethod=marker: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Author: James L. Pine <jlp@valinux.com> |
  17. // | Maintainer: Daniel Convissor <danielc@php.net> |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: oci8.php,v 1.60 2004/02/19 07:28:34 danielc Exp $
  21. // be aware... OCIError() only appears to return anything when given a
  22. // statement, so functions return the generic DB_ERROR instead of more
  23. // useful errors that have to do with feedback from the database.
  24. require_once 'DB/common.php';
  25. /**
  26. * Database independent query interface definition for PHP's Oracle 8
  27. * call-interface extension.
  28. *
  29. * Definitely works with versions 8 and 9 of Oracle.
  30. *
  31. * @package DB
  32. * @version $Id: oci8.php,v 1.60 2004/02/19 07:28:34 danielc Exp $
  33. * @category Database
  34. * @author James L. Pine <jlp@valinux.com>
  35. */
  36. class DB_oci8 extends DB_common
  37. {
  38. // {{{ properties
  39. var $connection;
  40. var $phptype, $dbsyntax;
  41. var $manip_query = array();
  42. var $prepare_types = array();
  43. var $autoCommit = 1;
  44. var $last_stmt = false;
  45. // }}}
  46. // {{{ constructor
  47. function DB_oci8()
  48. {
  49. $this->DB_common();
  50. $this->phptype = 'oci8';
  51. $this->dbsyntax = 'oci8';
  52. $this->features = array(
  53. 'prepare' => false,
  54. 'pconnect' => true,
  55. 'transactions' => true,
  56. 'limit' => 'alter'
  57. );
  58. $this->errorcode_map = array(
  59. 1 => DB_ERROR_CONSTRAINT,
  60. 900 => DB_ERROR_SYNTAX,
  61. 904 => DB_ERROR_NOSUCHFIELD,
  62. 921 => DB_ERROR_SYNTAX,
  63. 923 => DB_ERROR_SYNTAX,
  64. 942 => DB_ERROR_NOSUCHTABLE,
  65. 955 => DB_ERROR_ALREADY_EXISTS,
  66. 1400 => DB_ERROR_CONSTRAINT_NOT_NULL,
  67. 1407 => DB_ERROR_CONSTRAINT_NOT_NULL,
  68. 1476 => DB_ERROR_DIVZERO,
  69. 1722 => DB_ERROR_INVALID_NUMBER,
  70. 2289 => DB_ERROR_NOSUCHTABLE,
  71. 2291 => DB_ERROR_CONSTRAINT,
  72. 2449 => DB_ERROR_CONSTRAINT,
  73. );
  74. }
  75. // }}}
  76. // {{{ connect()
  77. /**
  78. * Connect to a database and log in as the specified user.
  79. *
  80. * @param $dsn the data source name (see DB::parseDSN for syntax)
  81. * @param $persistent (optional) whether the connection should
  82. * be persistent
  83. *
  84. * @return int DB_OK on success, a DB error code on failure
  85. */
  86. function connect($dsninfo, $persistent = false)
  87. {
  88. if (!DB::assertExtension('oci8')) {
  89. return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND);
  90. }
  91. $this->dsn = $dsninfo;
  92. $connect_function = $persistent ? 'OCIPLogon' : 'OCILogon';
  93. if ($dsninfo['hostspec']) {
  94. $conn = @$connect_function($dsninfo['username'],
  95. $dsninfo['password'],
  96. $dsninfo['hostspec']);
  97. } elseif ($dsninfo['username'] || $dsninfo['password']) {
  98. $conn = @$connect_function($dsninfo['username'],
  99. $dsninfo['password']);
  100. } else {
  101. $conn = false;
  102. }
  103. if ($conn == false) {
  104. $error = OCIError();
  105. $error = (is_array($error)) ? $error['message'] : null;
  106. return $this->raiseError(DB_ERROR_CONNECT_FAILED, null, null,
  107. null, $error);
  108. }
  109. $this->connection = $conn;
  110. return DB_OK;
  111. }
  112. // }}}
  113. // {{{ disconnect()
  114. /**
  115. * Log out and disconnect from the database.
  116. *
  117. * @return bool TRUE on success, FALSE if not connected.
  118. */
  119. function disconnect()
  120. {
  121. $ret = @OCILogOff($this->connection);
  122. $this->connection = null;
  123. return $ret;
  124. }
  125. // }}}
  126. // {{{ simpleQuery()
  127. /**
  128. * Send a query to oracle and return the results as an oci8 resource
  129. * identifier.
  130. *
  131. * @param $query the SQL query
  132. *
  133. * @return int returns a valid oci8 result for successful SELECT
  134. * queries, DB_OK for other successful queries. A DB error code
  135. * is returned on failure.
  136. */
  137. function simpleQuery($query)
  138. {
  139. $this->last_query = $query;
  140. $query = $this->modifyQuery($query);
  141. $result = @OCIParse($this->connection, $query);
  142. if (!$result) {
  143. return $this->oci8RaiseError();
  144. }
  145. if ($this->autoCommit) {
  146. $success = @OCIExecute($result,OCI_COMMIT_ON_SUCCESS);
  147. } else {
  148. $success = @OCIExecute($result,OCI_DEFAULT);
  149. }
  150. if (!$success) {
  151. return $this->oci8RaiseError($result);
  152. }
  153. $this->last_stmt=$result;
  154. // Determine which queries that should return data, and which
  155. // should return an error code only.
  156. return DB::isManip($query) ? DB_OK : $result;
  157. }
  158. // }}}
  159. // {{{ nextResult()
  160. /**
  161. * Move the internal oracle result pointer to the next available result
  162. *
  163. * @param a valid oci8 result resource
  164. *
  165. * @access public
  166. *
  167. * @return true if a result is available otherwise return false
  168. */
  169. function nextResult($result)
  170. {
  171. return false;
  172. }
  173. // }}}
  174. // {{{ fetchInto()
  175. /**
  176. * Fetch a row and insert the data into an existing array.
  177. *
  178. * Formating of the array and the data therein are configurable.
  179. * See DB_result::fetchInto() for more information.
  180. *
  181. * @param resource $result query result identifier
  182. * @param array $arr (reference) array where data from the row
  183. * should be placed
  184. * @param int $fetchmode how the resulting array should be indexed
  185. * @param int $rownum the row number to fetch
  186. *
  187. * @return mixed DB_OK on success, NULL when end of result set is
  188. * reached or on failure
  189. *
  190. * @see DB_result::fetchInto()
  191. * @access private
  192. */
  193. function fetchInto($result, &$arr, $fetchmode, $rownum=null)
  194. {
  195. if ($rownum !== NULL) {
  196. return $this->raiseError(DB_ERROR_NOT_CAPABLE);
  197. }
  198. if ($fetchmode & DB_FETCHMODE_ASSOC) {
  199. $moredata = @OCIFetchInto($result,$arr,OCI_ASSOC+OCI_RETURN_NULLS+OCI_RETURN_LOBS);
  200. if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE &&
  201. $moredata)
  202. {
  203. $arr = array_change_key_case($arr, CASE_LOWER);
  204. }
  205. } else {
  206. $moredata = OCIFetchInto($result,$arr,OCI_RETURN_NULLS+OCI_RETURN_LOBS);
  207. }
  208. if (!$moredata) {
  209. return NULL;
  210. }
  211. if ($this->options['portability'] & DB_PORTABILITY_RTRIM) {
  212. $this->_rtrimArrayValues($arr);
  213. }
  214. if ($this->options['portability'] & DB_PORTABILITY_NULL_TO_EMPTY) {
  215. $this->_convertNullArrayValuesToEmpty($arr);
  216. }
  217. return DB_OK;
  218. }
  219. // }}}
  220. // {{{ freeResult()
  221. /**
  222. * Free the internal resources associated with $result.
  223. *
  224. * @param $result oci8 result identifier
  225. *
  226. * @return bool TRUE on success, FALSE if $result is invalid
  227. */
  228. function freeResult($result)
  229. {
  230. return @OCIFreeStatement($result);
  231. }
  232. /**
  233. * Free the internal resources associated with a prepared query.
  234. *
  235. * @param $stmt oci8 statement identifier
  236. *
  237. * @return bool TRUE on success, FALSE if $result is invalid
  238. */
  239. function freePrepared($stmt)
  240. {
  241. if (isset($this->prepare_types[(int)$stmt])) {
  242. unset($this->prepare_types[(int)$stmt]);
  243. unset($this->manip_query[(int)$stmt]);
  244. } else {
  245. return false;
  246. }
  247. return true;
  248. }
  249. // }}}
  250. // {{{ numRows()
  251. function numRows($result)
  252. {
  253. // emulate numRows for Oracle. yuck.
  254. if ($this->options['portability'] & DB_PORTABILITY_NUMROWS &&
  255. $result === $this->last_stmt)
  256. {
  257. $countquery = 'SELECT COUNT(*) FROM ('.$this->last_query.')';
  258. $save_query = $this->last_query;
  259. $save_stmt = $this->last_stmt;
  260. $count =& $this->query($countquery);
  261. if (DB::isError($count) ||
  262. DB::isError($row = $count->fetchRow(DB_FETCHMODE_ORDERED)))
  263. {
  264. $this->last_query = $save_query;
  265. $this->last_stmt = $save_stmt;
  266. return $this->raiseError(DB_ERROR_NOT_CAPABLE);
  267. }
  268. return $row[0];
  269. }
  270. return $this->raiseError(DB_ERROR_NOT_CAPABLE);
  271. }
  272. // }}}
  273. // {{{ numCols()
  274. /**
  275. * Get the number of columns in a result set.
  276. *
  277. * @param $result oci8 result identifier
  278. *
  279. * @return int the number of columns per row in $result
  280. */
  281. function numCols($result)
  282. {
  283. $cols = @OCINumCols($result);
  284. if (!$cols) {
  285. return $this->oci8RaiseError($result);
  286. }
  287. return $cols;
  288. }
  289. // }}}
  290. // {{{ errorNative()
  291. /**
  292. * Get the native error code of the last error (if any) that occured
  293. * on the current connection. This does not work, as OCIError does
  294. * not work unless given a statement. If OCIError does return
  295. * something, so will this.
  296. *
  297. * @return int native oci8 error code
  298. */
  299. function errorNative()
  300. {
  301. if (is_resource($this->last_stmt)) {
  302. $error = @OCIError($this->last_stmt);
  303. } else {
  304. $error = @OCIError($this->connection);
  305. }
  306. if (is_array($error)) {
  307. return $error['code'];
  308. }
  309. return false;
  310. }
  311. // }}}
  312. // {{{ prepare()
  313. /**
  314. * Prepares a query for multiple execution with execute().
  315. *
  316. * With oci8, this is emulated.
  317. *
  318. * prepare() requires a generic query as string like <code>
  319. * INSERT INTO numbers VALUES (?, ?, ?)
  320. * </code>. The <kbd>?</kbd> characters are placeholders.
  321. *
  322. * Three types of placeholders can be used:
  323. * + <kbd>?</kbd> a quoted scalar value, i.e. strings, integers
  324. * + <kbd>!</kbd> value is inserted 'as is'
  325. * + <kbd>&</kbd> requires a file name. The file's contents get
  326. * inserted into the query (i.e. saving binary
  327. * data in a db)
  328. *
  329. * Use backslashes to escape placeholder characters if you don't want
  330. * them to be interpreted as placeholders. Example: <code>
  331. * "UPDATE foo SET col=? WHERE col='over \& under'"
  332. * </code>
  333. *
  334. * @param string $query query to be prepared
  335. * @return mixed DB statement resource on success. DB_Error on failure.
  336. */
  337. function prepare($query)
  338. {
  339. $tokens = preg_split('/((?<!\\\)[&?!])/', $query, -1,
  340. PREG_SPLIT_DELIM_CAPTURE);
  341. $binds = count($tokens) - 1;
  342. $token = 0;
  343. $types = array();
  344. $newquery = '';
  345. foreach ($tokens as $key => $val) {
  346. switch ($val) {
  347. case '?':
  348. $types[$token++] = DB_PARAM_SCALAR;
  349. unset($tokens[$key]);
  350. break;
  351. case '&':
  352. $types[$token++] = DB_PARAM_OPAQUE;
  353. unset($tokens[$key]);
  354. break;
  355. case '!':
  356. $types[$token++] = DB_PARAM_MISC;
  357. unset($tokens[$key]);
  358. break;
  359. default:
  360. $tokens[$key] = preg_replace('/\\\([&?!])/', "\\1", $val);
  361. if ($key != $binds) {
  362. $newquery .= $tokens[$key] . ':bind' . $token;
  363. } else {
  364. $newquery .= $tokens[$key];
  365. }
  366. }
  367. }
  368. $this->last_query = $query;
  369. $newquery = $this->modifyQuery($newquery);
  370. if (!$stmt = @OCIParse($this->connection, $newquery)) {
  371. return $this->oci8RaiseError();
  372. }
  373. $this->prepare_types[$stmt] = $types;
  374. $this->manip_query[(int)$stmt] = DB::isManip($query);
  375. return $stmt;
  376. }
  377. // }}}
  378. // {{{ execute()
  379. /**
  380. * Executes a DB statement prepared with prepare().
  381. *
  382. * @param resource $stmt a DB statement resource returned from prepare()
  383. * @param mixed $data array, string or numeric data to be used in
  384. * execution of the statement. Quantity of items
  385. * passed must match quantity of placeholders in
  386. * query: meaning 1 for non-array items or the
  387. * quantity of elements in the array.
  388. * @return int returns an oci8 result resource for successful
  389. * SELECT queries, DB_OK for other successful queries. A DB error
  390. * code is returned on failure.
  391. * @see DB_oci::prepare()
  392. */
  393. function &execute($stmt, $data = array())
  394. {
  395. if (!is_array($data)) {
  396. $data = array($data);
  397. }
  398. $types =& $this->prepare_types[$stmt];
  399. if (count($types) != count($data)) {
  400. $tmp =& $this->raiseError(DB_ERROR_MISMATCH);
  401. return $tmp;
  402. }
  403. $i = 0;
  404. foreach ($data as $key => $value) {
  405. if ($types[$i] == DB_PARAM_MISC) {
  406. /*
  407. * Oracle doesn't seem to have the ability to pass a
  408. * parameter along unchanged, so strip off quotes from start
  409. * and end, plus turn two single quotes to one single quote,
  410. * in order to avoid the quotes getting escaped by
  411. * Oracle and ending up in the database.
  412. */
  413. $data[$key] = preg_replace("/^'(.*)'$/", "\\1", $data[$key]);
  414. $data[$key] = str_replace("''", "'", $data[$key]);
  415. } elseif ($types[$i] == DB_PARAM_OPAQUE) {
  416. $fp = @fopen($data[$key], 'rb');
  417. if (!$fp) {
  418. $tmp =& $this->raiseError(DB_ERROR_ACCESS_VIOLATION);
  419. return $tmp;
  420. }
  421. $data[$key] = fread($fp, filesize($data[$key]));
  422. fclose($fp);
  423. }
  424. if (!@OCIBindByName($stmt, ':bind' . $i, $data[$key], -1)) {
  425. $tmp = $this->oci8RaiseError($stmt);
  426. return $tmp;
  427. }
  428. $i++;
  429. }
  430. if ($this->autoCommit) {
  431. $success = @OCIExecute($stmt, OCI_COMMIT_ON_SUCCESS);
  432. } else {
  433. $success = @OCIExecute($stmt, OCI_DEFAULT);
  434. }
  435. if (!$success) {
  436. $tmp = $this->oci8RaiseError($stmt);
  437. return $tmp;
  438. }
  439. $this->last_stmt = $stmt;
  440. if ($this->manip_query[(int)$stmt]) {
  441. $tmp = DB_OK;
  442. } else {
  443. $tmp =& new DB_result($this, $stmt);
  444. }
  445. return $tmp;
  446. }
  447. // }}}
  448. // {{{ autoCommit()
  449. /**
  450. * Enable/disable automatic commits
  451. *
  452. * @param $onoff true/false whether to autocommit
  453. */
  454. function autoCommit($onoff = false)
  455. {
  456. $this->autoCommit = (bool)$onoff;;
  457. return DB_OK;
  458. }
  459. // }}}
  460. // {{{ commit()
  461. /**
  462. * Commit transactions on the current connection
  463. *
  464. * @return DB_ERROR or DB_OK
  465. */
  466. function commit()
  467. {
  468. $result = @OCICommit($this->connection);
  469. if (!$result) {
  470. return $this->oci8RaiseError();
  471. }
  472. return DB_OK;
  473. }
  474. // }}}
  475. // {{{ rollback()
  476. /**
  477. * Roll back all uncommitted transactions on the current connection.
  478. *
  479. * @return DB_ERROR or DB_OK
  480. */
  481. function rollback()
  482. {
  483. $result = @OCIRollback($this->connection);
  484. if (!$result) {
  485. return $this->oci8RaiseError();
  486. }
  487. return DB_OK;
  488. }
  489. // }}}
  490. // {{{ affectedRows()
  491. /**
  492. * Gets the number of rows affected by the last query.
  493. * if the last query was a select, returns 0.
  494. *
  495. * @return number of rows affected by the last query or DB_ERROR
  496. */
  497. function affectedRows()
  498. {
  499. if ($this->last_stmt === false) {
  500. return $this->oci8RaiseError();
  501. }
  502. $result = @OCIRowCount($this->last_stmt);
  503. if ($result === false) {
  504. return $this->oci8RaiseError($this->last_stmt);
  505. }
  506. return $result;
  507. }
  508. // }}}
  509. // {{{ modifyQuery()
  510. function modifyQuery($query)
  511. {
  512. // "SELECT 2+2" must be "SELECT 2+2 FROM dual" in Oracle
  513. if (preg_match('/^\s*SELECT/i', $query) &&
  514. !preg_match('/\sFROM\s/i', $query)) {
  515. $query .= ' FROM dual';
  516. }
  517. return $query;
  518. }
  519. // }}}
  520. // {{{ modifyLimitQuery()
  521. /**
  522. * Emulate the row limit support altering the query
  523. *
  524. * @param string $query The query to treat
  525. * @param int $from The row to start to fetch from
  526. * @param int $count The offset
  527. * @return string The modified query
  528. *
  529. * @author Tomas V.V.Cox <cox@idecnet.com>
  530. */
  531. function modifyLimitQuery($query, $from, $count)
  532. {
  533. // Let Oracle return the name of the columns instead of
  534. // coding a "home" SQL parser
  535. $q_fields = "SELECT * FROM ($query) WHERE NULL = NULL";
  536. if (!$result = @OCIParse($this->connection, $q_fields)) {
  537. $this->last_query = $q_fields;
  538. return $this->oci8RaiseError();
  539. }
  540. if (!@OCIExecute($result, OCI_DEFAULT)) {
  541. $this->last_query = $q_fields;
  542. return $this->oci8RaiseError($result);
  543. }
  544. $ncols = OCINumCols($result);
  545. $cols = array();
  546. for ( $i = 1; $i <= $ncols; $i++ ) {
  547. $cols[] = '"' . OCIColumnName($result, $i) . '"';
  548. }
  549. $fields = implode(', ', $cols);
  550. // XXX Test that (tip by John Lim)
  551. //if(preg_match('/^\s*SELECT\s+/is', $query, $match)) {
  552. // // Introduce the FIRST_ROWS Oracle query optimizer
  553. // $query = substr($query, strlen($match[0]), strlen($query));
  554. // $query = "SELECT /* +FIRST_ROWS */ " . $query;
  555. //}
  556. // Construct the query
  557. // more at: http://marc.theaimsgroup.com/?l=php-db&m=99831958101212&w=2
  558. // Perhaps this could be optimized with the use of Unions
  559. $query = "SELECT $fields FROM".
  560. " (SELECT rownum as linenum, $fields FROM".
  561. " ($query)".
  562. ' WHERE rownum <= '. ($from + $count) .
  563. ') WHERE linenum >= ' . ++$from;
  564. return $query;
  565. }
  566. // }}}
  567. // {{{ nextId()
  568. /**
  569. * Returns the next free id in a sequence
  570. *
  571. * @param string $seq_name name of the sequence
  572. * @param boolean $ondemand when true, the seqence is automatically
  573. * created if it does not exist
  574. *
  575. * @return int the next id number in the sequence. DB_Error if problem.
  576. *
  577. * @internal
  578. * @see DB_common::nextID()
  579. * @access public
  580. */
  581. function nextId($seq_name, $ondemand = true)
  582. {
  583. $seqname = $this->getSequenceName($seq_name);
  584. $repeat = 0;
  585. do {
  586. $this->expectError(DB_ERROR_NOSUCHTABLE);
  587. $result =& $this->query("SELECT ${seqname}.nextval FROM dual");
  588. $this->popExpect();
  589. if ($ondemand && DB::isError($result) &&
  590. $result->getCode() == DB_ERROR_NOSUCHTABLE) {
  591. $repeat = 1;
  592. $result = $this->createSequence($seq_name);
  593. if (DB::isError($result)) {
  594. return $this->raiseError($result);
  595. }
  596. } else {
  597. $repeat = 0;
  598. }
  599. } while ($repeat);
  600. if (DB::isError($result)) {
  601. return $this->raiseError($result);
  602. }
  603. $arr = $result->fetchRow(DB_FETCHMODE_ORDERED);
  604. return $arr[0];
  605. }
  606. /**
  607. * Creates a new sequence
  608. *
  609. * @param string $seq_name name of the new sequence
  610. *
  611. * @return int DB_OK on success. A DB_Error object is returned if
  612. * problems arise.
  613. *
  614. * @internal
  615. * @see DB_common::createSequence()
  616. * @access public
  617. */
  618. function createSequence($seq_name)
  619. {
  620. $seqname = $this->getSequenceName($seq_name);
  621. return $this->query("CREATE SEQUENCE ${seqname}");
  622. }
  623. // }}}
  624. // {{{ dropSequence()
  625. /**
  626. * Deletes a sequence
  627. *
  628. * @param string $seq_name name of the sequence to be deleted
  629. *
  630. * @return int DB_OK on success. DB_Error if problems.
  631. *
  632. * @internal
  633. * @see DB_common::dropSequence()
  634. * @access public
  635. */
  636. function dropSequence($seq_name)
  637. {
  638. $seqname = $this->getSequenceName($seq_name);
  639. return $this->query("DROP SEQUENCE ${seqname}");
  640. }
  641. // }}}
  642. // {{{ oci8RaiseError()
  643. /**
  644. * Gather information about an error, then use that info to create a
  645. * DB error object and finally return that object.
  646. *
  647. * @param integer $errno PEAR error number (usually a DB constant) if
  648. * manually raising an error
  649. * @return object DB error object
  650. * @see DB_common::errorCode()
  651. * @see DB_common::raiseError()
  652. */
  653. function oci8RaiseError($errno = null)
  654. {
  655. if ($errno === null) {
  656. $error = @OCIError($this->connection);
  657. return $this->raiseError($this->errorCode($error['code']),
  658. null, null, null, $error['message']);
  659. } elseif (is_resource($errno)) {
  660. $error = @OCIError($errno);
  661. return $this->raiseError($this->errorCode($error['code']),
  662. null, null, null, $error['message']);
  663. }
  664. return $this->raiseError($this->errorCode($errno));
  665. }
  666. // }}}
  667. // {{{ getSpecialQuery()
  668. /**
  669. * Returns the query needed to get some backend info
  670. * @param string $type What kind of info you want to retrieve
  671. * @return string The SQL query string
  672. */
  673. function getSpecialQuery($type)
  674. {
  675. switch ($type) {
  676. case 'tables':
  677. return 'SELECT table_name FROM user_tables';
  678. default:
  679. return null;
  680. }
  681. }
  682. // }}}
  683. // {{{ tableInfo()
  684. /**
  685. * Returns information about a table or a result set.
  686. *
  687. * NOTE: only supports 'table' and 'flags' if <var>$result</var>
  688. * is a table name.
  689. *
  690. * NOTE: flags won't contain index information.
  691. *
  692. * @param object|string $result DB_result object from a query or a
  693. * string containing the name of a table
  694. * @param int $mode a valid tableInfo mode
  695. * @return array an associative array with the information requested
  696. * or an error object if something is wrong
  697. * @access public
  698. * @internal
  699. * @see DB_common::tableInfo()
  700. */
  701. function tableInfo($result, $mode = null)
  702. {
  703. if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE) {
  704. $case_func = 'strtolower';
  705. } else {
  706. $case_func = 'strval';
  707. }
  708. if (is_string($result)) {
  709. /*
  710. * Probably received a table name.
  711. * Create a result resource identifier.
  712. */
  713. $result = strtoupper($result);
  714. $q_fields = 'SELECT column_name, data_type, data_length, '
  715. . 'nullable '
  716. . 'FROM user_tab_columns '
  717. . "WHERE table_name='$result' ORDER BY column_id";
  718. $this->last_query = $q_fields;
  719. if (!$stmt = @OCIParse($this->connection, $q_fields)) {
  720. return $this->oci8RaiseError(DB_ERROR_NEED_MORE_DATA);
  721. }
  722. if (!@OCIExecute($stmt, OCI_DEFAULT)) {
  723. return $this->oci8RaiseError($stmt);
  724. }
  725. $i = 0;
  726. while (@OCIFetch($stmt)) {
  727. $res[$i]['table'] = $case_func($result);
  728. $res[$i]['name'] = $case_func(@OCIResult($stmt, 1));
  729. $res[$i]['type'] = @OCIResult($stmt, 2);
  730. $res[$i]['len'] = @OCIResult($stmt, 3);
  731. $res[$i]['flags'] = (@OCIResult($stmt, 4) == 'N') ? 'not_null' : '';
  732. if ($mode & DB_TABLEINFO_ORDER) {
  733. $res['order'][$res[$i]['name']] = $i;
  734. }
  735. if ($mode & DB_TABLEINFO_ORDERTABLE) {
  736. $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
  737. }
  738. $i++;
  739. }
  740. $res['num_fields'] = $i;
  741. @OCIFreeStatement($stmt);
  742. } else {
  743. if (isset($result->result)) {
  744. /*
  745. * Probably received a result object.
  746. * Extract the result resource identifier.
  747. */
  748. $result = $result->result;
  749. } else {
  750. /*
  751. * ELSE, probably received a result resource identifier.
  752. * Depricated. Here for compatibility only.
  753. */
  754. }
  755. if ($result === $this->last_stmt) {
  756. $count = @OCINumCols($result);
  757. for ($i=0; $i<$count; $i++) {
  758. $res[$i]['table'] = '';
  759. $res[$i]['name'] = $case_func(@OCIColumnName($result, $i+1));
  760. $res[$i]['type'] = @OCIColumnType($result, $i+1);
  761. $res[$i]['len'] = @OCIColumnSize($result, $i+1);
  762. $res[$i]['flags'] = '';
  763. if ($mode & DB_TABLEINFO_ORDER) {
  764. $res['order'][$res[$i]['name']] = $i;
  765. }
  766. if ($mode & DB_TABLEINFO_ORDERTABLE) {
  767. $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
  768. }
  769. }
  770. $res['num_fields'] = $count;
  771. } else {
  772. return $this->raiseError(DB_ERROR_NOT_CAPABLE);
  773. }
  774. }
  775. return $res;
  776. }
  777. // }}}
  778. }
  779. /*
  780. * Local variables:
  781. * tab-width: 4
  782. * c-basic-offset: 4
  783. * End:
  784. */
  785. ?>