PageRenderTime 58ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/php/DB/odbc.php

https://bitbucket.org/adarshj/convenient_website
PHP | 883 lines | 418 code | 78 blank | 387 comment | 73 complexity | 0b4652f75f847167b836aa9605ed1209 MD5 | raw file
Possible License(s): Apache-2.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-2-Clause, GPL-2.0, LGPL-3.0
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * The PEAR DB driver for PHP's odbc extension
  5. * for interacting with databases via ODBC connections
  6. *
  7. * PHP versions 4 and 5
  8. *
  9. * LICENSE: This source file is subject to version 3.0 of the PHP license
  10. * that is available through the world-wide-web at the following URI:
  11. * http://www.php.net/license/3_0.txt. If you did not receive a copy of
  12. * the PHP License and are unable to obtain it through the web, please
  13. * send a note to license@php.net so we can mail you a copy immediately.
  14. *
  15. * @category Database
  16. * @package DB
  17. * @author Stig Bakken <ssb@php.net>
  18. * @author Daniel Convissor <danielc@php.net>
  19. * @copyright 1997-2005 The PHP Group
  20. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  21. * @version CVS: $Id: odbc.php,v 1.78 2005/02/28 01:42:17 danielc Exp $
  22. * @link http://pear.php.net/package/DB
  23. */
  24. /**
  25. * Obtain the DB_common class so it can be extended from
  26. */
  27. require_once 'DB/common.php';
  28. /**
  29. * The methods PEAR DB uses to interact with PHP's odbc extension
  30. * for interacting with databases via ODBC connections
  31. *
  32. * These methods overload the ones declared in DB_common.
  33. *
  34. * More info on ODBC errors could be found here:
  35. * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/trblsql/tr_err_odbc_5stz.asp
  36. *
  37. * @category Database
  38. * @package DB
  39. * @author Stig Bakken <ssb@php.net>
  40. * @author Daniel Convissor <danielc@php.net>
  41. * @copyright 1997-2005 The PHP Group
  42. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  43. * @version Release: 1.7.6
  44. * @link http://pear.php.net/package/DB
  45. */
  46. class DB_odbc extends DB_common
  47. {
  48. // {{{ properties
  49. /**
  50. * The DB driver type (mysql, oci8, odbc, etc.)
  51. * @var string
  52. */
  53. var $phptype = 'odbc';
  54. /**
  55. * The database syntax variant to be used (db2, access, etc.), if any
  56. * @var string
  57. */
  58. var $dbsyntax = 'sql92';
  59. /**
  60. * The capabilities of this DB implementation
  61. *
  62. * The 'new_link' element contains the PHP version that first provided
  63. * new_link support for this DBMS. Contains false if it's unsupported.
  64. *
  65. * Meaning of the 'limit' element:
  66. * + 'emulate' = emulate with fetch row by number
  67. * + 'alter' = alter the query
  68. * + false = skip rows
  69. *
  70. * NOTE: The feature set of the following drivers are different than
  71. * the default:
  72. * + solid: 'transactions' = true
  73. * + navision: 'limit' = false
  74. *
  75. * @var array
  76. */
  77. var $features = array(
  78. 'limit' => 'emulate',
  79. 'new_link' => false,
  80. 'numrows' => true,
  81. 'pconnect' => true,
  82. 'prepare' => false,
  83. 'ssl' => false,
  84. 'transactions' => false,
  85. );
  86. /**
  87. * A mapping of native error codes to DB error codes
  88. * @var array
  89. */
  90. var $errorcode_map = array(
  91. '01004' => DB_ERROR_TRUNCATED,
  92. '07001' => DB_ERROR_MISMATCH,
  93. '21S01' => DB_ERROR_VALUE_COUNT_ON_ROW,
  94. '21S02' => DB_ERROR_MISMATCH,
  95. '22001' => DB_ERROR_INVALID,
  96. '22003' => DB_ERROR_INVALID_NUMBER,
  97. '22005' => DB_ERROR_INVALID_NUMBER,
  98. '22008' => DB_ERROR_INVALID_DATE,
  99. '22012' => DB_ERROR_DIVZERO,
  100. '23000' => DB_ERROR_CONSTRAINT,
  101. '23502' => DB_ERROR_CONSTRAINT_NOT_NULL,
  102. '23503' => DB_ERROR_CONSTRAINT,
  103. '23504' => DB_ERROR_CONSTRAINT,
  104. '23505' => DB_ERROR_CONSTRAINT,
  105. '24000' => DB_ERROR_INVALID,
  106. '34000' => DB_ERROR_INVALID,
  107. '37000' => DB_ERROR_SYNTAX,
  108. '42000' => DB_ERROR_SYNTAX,
  109. '42601' => DB_ERROR_SYNTAX,
  110. 'IM001' => DB_ERROR_UNSUPPORTED,
  111. 'S0000' => DB_ERROR_NOSUCHTABLE,
  112. 'S0001' => DB_ERROR_ALREADY_EXISTS,
  113. 'S0002' => DB_ERROR_NOSUCHTABLE,
  114. 'S0011' => DB_ERROR_ALREADY_EXISTS,
  115. 'S0012' => DB_ERROR_NOT_FOUND,
  116. 'S0021' => DB_ERROR_ALREADY_EXISTS,
  117. 'S0022' => DB_ERROR_NOSUCHFIELD,
  118. 'S1009' => DB_ERROR_INVALID,
  119. 'S1090' => DB_ERROR_INVALID,
  120. 'S1C00' => DB_ERROR_NOT_CAPABLE,
  121. );
  122. /**
  123. * The raw database connection created by PHP
  124. * @var resource
  125. */
  126. var $connection;
  127. /**
  128. * The DSN information for connecting to a database
  129. * @var array
  130. */
  131. var $dsn = array();
  132. /**
  133. * The number of rows affected by a data manipulation query
  134. * @var integer
  135. * @access private
  136. */
  137. var $affected = 0;
  138. // }}}
  139. // {{{ constructor
  140. /**
  141. * This constructor calls <kbd>$this->DB_common()</kbd>
  142. *
  143. * @return void
  144. */
  145. function DB_odbc()
  146. {
  147. $this->DB_common();
  148. }
  149. // }}}
  150. // {{{ connect()
  151. /**
  152. * Connect to the database server, log in and open the database
  153. *
  154. * Don't call this method directly. Use DB::connect() instead.
  155. *
  156. * PEAR DB's odbc driver supports the following extra DSN options:
  157. * + cursor The type of cursor to be used for this connection.
  158. *
  159. * @param array $dsn the data source name
  160. * @param bool $persistent should the connection be persistent?
  161. *
  162. * @return int DB_OK on success. A DB_Error object on failure.
  163. */
  164. function connect($dsn, $persistent = false)
  165. {
  166. if (!PEAR::loadExtension('odbc')) {
  167. return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND);
  168. }
  169. $this->dsn = $dsn;
  170. if ($dsn['dbsyntax']) {
  171. $this->dbsyntax = $dsn['dbsyntax'];
  172. }
  173. switch ($this->dbsyntax) {
  174. case 'access':
  175. case 'db2':
  176. case 'solid':
  177. $this->features['transactions'] = true;
  178. break;
  179. case 'navision':
  180. $this->features['limit'] = false;
  181. }
  182. /*
  183. * This is hear for backwards compatibility. Should have been using
  184. * 'database' all along, but prior to 1.6.0RC3 'hostspec' was used.
  185. */
  186. if ($dsn['database']) {
  187. $odbcdsn = $dsn['database'];
  188. } elseif ($dsn['hostspec']) {
  189. $odbcdsn = $dsn['hostspec'];
  190. } else {
  191. $odbcdsn = 'localhost';
  192. }
  193. $connect_function = $persistent ? 'odbc_pconnect' : 'odbc_connect';
  194. if (empty($dsn['cursor'])) {
  195. $this->connection = @$connect_function($odbcdsn, $dsn['username'],
  196. $dsn['password']);
  197. } else {
  198. $this->connection = @$connect_function($odbcdsn, $dsn['username'],
  199. $dsn['password'],
  200. $dsn['cursor']);
  201. }
  202. if (!is_resource($this->connection)) {
  203. return $this->raiseError(DB_ERROR_CONNECT_FAILED,
  204. null, null, null,
  205. $this->errorNative());
  206. }
  207. return DB_OK;
  208. }
  209. // }}}
  210. // {{{ disconnect()
  211. /**
  212. * Disconnects from the database server
  213. *
  214. * @return bool TRUE on success, FALSE on failure
  215. */
  216. function disconnect()
  217. {
  218. $err = @odbc_close($this->connection);
  219. $this->connection = null;
  220. return $err;
  221. }
  222. // }}}
  223. // {{{ simpleQuery()
  224. /**
  225. * Sends a query to the database server
  226. *
  227. * @param string the SQL query string
  228. *
  229. * @return mixed + a PHP result resrouce for successful SELECT queries
  230. * + the DB_OK constant for other successful queries
  231. * + a DB_Error object on failure
  232. */
  233. function simpleQuery($query)
  234. {
  235. $this->last_query = $query;
  236. $query = $this->modifyQuery($query);
  237. $result = @odbc_exec($this->connection, $query);
  238. if (!$result) {
  239. return $this->odbcRaiseError(); // XXX ERRORMSG
  240. }
  241. // Determine which queries that should return data, and which
  242. // should return an error code only.
  243. if (DB::isManip($query)) {
  244. $this->affected = $result; // For affectedRows()
  245. return DB_OK;
  246. }
  247. $this->affected = 0;
  248. return $result;
  249. }
  250. // }}}
  251. // {{{ nextResult()
  252. /**
  253. * Move the internal odbc result pointer to the next available result
  254. *
  255. * @param a valid fbsql result resource
  256. *
  257. * @access public
  258. *
  259. * @return true if a result is available otherwise return false
  260. */
  261. function nextResult($result)
  262. {
  263. return @odbc_next_result($result);
  264. }
  265. // }}}
  266. // {{{ fetchInto()
  267. /**
  268. * Places a row from the result set into the given array
  269. *
  270. * Formating of the array and the data therein are configurable.
  271. * See DB_result::fetchInto() for more information.
  272. *
  273. * This method is not meant to be called directly. Use
  274. * DB_result::fetchInto() instead. It can't be declared "protected"
  275. * because DB_result is a separate object.
  276. *
  277. * @param resource $result the query result resource
  278. * @param array $arr the referenced array to put the data in
  279. * @param int $fetchmode how the resulting array should be indexed
  280. * @param int $rownum the row number to fetch (0 = first row)
  281. *
  282. * @return mixed DB_OK on success, NULL when the end of a result set is
  283. * reached or on failure
  284. *
  285. * @see DB_result::fetchInto()
  286. */
  287. function fetchInto($result, &$arr, $fetchmode, $rownum = null)
  288. {
  289. $arr = array();
  290. if ($rownum !== null) {
  291. $rownum++; // ODBC first row is 1
  292. if (version_compare(phpversion(), '4.2.0', 'ge')) {
  293. $cols = @odbc_fetch_into($result, $arr, $rownum);
  294. } else {
  295. $cols = @odbc_fetch_into($result, $rownum, $arr);
  296. }
  297. } else {
  298. $cols = @odbc_fetch_into($result, $arr);
  299. }
  300. if (!$cols) {
  301. return null;
  302. }
  303. if ($fetchmode !== DB_FETCHMODE_ORDERED) {
  304. for ($i = 0; $i < count($arr); $i++) {
  305. $colName = @odbc_field_name($result, $i+1);
  306. $a[$colName] = $arr[$i];
  307. }
  308. if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE) {
  309. $a = array_change_key_case($a, CASE_LOWER);
  310. }
  311. $arr = $a;
  312. }
  313. if ($this->options['portability'] & DB_PORTABILITY_RTRIM) {
  314. $this->_rtrimArrayValues($arr);
  315. }
  316. if ($this->options['portability'] & DB_PORTABILITY_NULL_TO_EMPTY) {
  317. $this->_convertNullArrayValuesToEmpty($arr);
  318. }
  319. return DB_OK;
  320. }
  321. // }}}
  322. // {{{ freeResult()
  323. /**
  324. * Deletes the result set and frees the memory occupied by the result set
  325. *
  326. * This method is not meant to be called directly. Use
  327. * DB_result::free() instead. It can't be declared "protected"
  328. * because DB_result is a separate object.
  329. *
  330. * @param resource $result PHP's query result resource
  331. *
  332. * @return bool TRUE on success, FALSE if $result is invalid
  333. *
  334. * @see DB_result::free()
  335. */
  336. function freeResult($result)
  337. {
  338. return @odbc_free_result($result);
  339. }
  340. // }}}
  341. // {{{ numCols()
  342. /**
  343. * Gets the number of columns in a result set
  344. *
  345. * This method is not meant to be called directly. Use
  346. * DB_result::numCols() instead. It can't be declared "protected"
  347. * because DB_result is a separate object.
  348. *
  349. * @param resource $result PHP's query result resource
  350. *
  351. * @return int the number of columns. A DB_Error object on failure.
  352. *
  353. * @see DB_result::numCols()
  354. */
  355. function numCols($result)
  356. {
  357. $cols = @odbc_num_fields($result);
  358. if (!$cols) {
  359. return $this->odbcRaiseError();
  360. }
  361. return $cols;
  362. }
  363. // }}}
  364. // {{{ affectedRows()
  365. /**
  366. * Determines the number of rows affected by a data maniuplation query
  367. *
  368. * 0 is returned for queries that don't manipulate data.
  369. *
  370. * @return int the number of rows. A DB_Error object on failure.
  371. */
  372. function affectedRows()
  373. {
  374. if (empty($this->affected)) { // In case of SELECT stms
  375. return 0;
  376. }
  377. $nrows = @odbc_num_rows($this->affected);
  378. if ($nrows == -1) {
  379. return $this->odbcRaiseError();
  380. }
  381. return $nrows;
  382. }
  383. // }}}
  384. // {{{ numRows()
  385. /**
  386. * Gets the number of rows in a result set
  387. *
  388. * Not all ODBC drivers support this functionality. If they don't
  389. * a DB_Error object for DB_ERROR_UNSUPPORTED is returned.
  390. *
  391. * This method is not meant to be called directly. Use
  392. * DB_result::numRows() instead. It can't be declared "protected"
  393. * because DB_result is a separate object.
  394. *
  395. * @param resource $result PHP's query result resource
  396. *
  397. * @return int the number of rows. A DB_Error object on failure.
  398. *
  399. * @see DB_result::numRows()
  400. */
  401. function numRows($result)
  402. {
  403. $nrows = @odbc_num_rows($result);
  404. if ($nrows == -1) {
  405. return $this->odbcRaiseError(DB_ERROR_UNSUPPORTED);
  406. }
  407. if ($nrows === false) {
  408. return $this->odbcRaiseError();
  409. }
  410. return $nrows;
  411. }
  412. // }}}
  413. // {{{ quoteIdentifier()
  414. /**
  415. * Quotes a string so it can be safely used as a table or column name
  416. *
  417. * Use 'mssql' as the dbsyntax in the DB DSN only if you've unchecked
  418. * "Use ANSI quoted identifiers" when setting up the ODBC data source.
  419. *
  420. * @param string $str identifier name to be quoted
  421. *
  422. * @return string quoted identifier string
  423. *
  424. * @see DB_common::quoteIdentifier()
  425. * @since Method available since Release 1.6.0
  426. */
  427. function quoteIdentifier($str)
  428. {
  429. switch ($this->dsn['dbsyntax']) {
  430. case 'access':
  431. return '[' . $str . ']';
  432. case 'mssql':
  433. case 'sybase':
  434. return '[' . str_replace(']', ']]', $str) . ']';
  435. case 'mysql':
  436. case 'mysqli':
  437. return '`' . $str . '`';
  438. default:
  439. return '"' . str_replace('"', '""', $str) . '"';
  440. }
  441. }
  442. // }}}
  443. // {{{ quote()
  444. /**
  445. * @deprecated Deprecated in release 1.6.0
  446. * @internal
  447. */
  448. function quote($str)
  449. {
  450. return $this->quoteSmart($str);
  451. }
  452. // }}}
  453. // {{{ nextId()
  454. /**
  455. * Returns the next free id in a sequence
  456. *
  457. * @param string $seq_name name of the sequence
  458. * @param boolean $ondemand when true, the seqence is automatically
  459. * created if it does not exist
  460. *
  461. * @return int the next id number in the sequence.
  462. * A DB_Error object on failure.
  463. *
  464. * @see DB_common::nextID(), DB_common::getSequenceName(),
  465. * DB_odbc::createSequence(), DB_odbc::dropSequence()
  466. */
  467. function nextId($seq_name, $ondemand = true)
  468. {
  469. $seqname = $this->getSequenceName($seq_name);
  470. $repeat = 0;
  471. do {
  472. $this->pushErrorHandling(PEAR_ERROR_RETURN);
  473. $result = $this->query("update ${seqname} set id = id + 1");
  474. $this->popErrorHandling();
  475. if ($ondemand && DB::isError($result) &&
  476. $result->getCode() == DB_ERROR_NOSUCHTABLE) {
  477. $repeat = 1;
  478. $this->pushErrorHandling(PEAR_ERROR_RETURN);
  479. $result = $this->createSequence($seq_name);
  480. $this->popErrorHandling();
  481. if (DB::isError($result)) {
  482. return $this->raiseError($result);
  483. }
  484. $result = $this->query("insert into ${seqname} (id) values(0)");
  485. } else {
  486. $repeat = 0;
  487. }
  488. } while ($repeat);
  489. if (DB::isError($result)) {
  490. return $this->raiseError($result);
  491. }
  492. $result = $this->query("select id from ${seqname}");
  493. if (DB::isError($result)) {
  494. return $result;
  495. }
  496. $row = $result->fetchRow(DB_FETCHMODE_ORDERED);
  497. if (DB::isError($row || !$row)) {
  498. return $row;
  499. }
  500. return $row[0];
  501. }
  502. /**
  503. * Creates a new sequence
  504. *
  505. * @param string $seq_name name of the new sequence
  506. *
  507. * @return int DB_OK on success. A DB_Error object on failure.
  508. *
  509. * @see DB_common::createSequence(), DB_common::getSequenceName(),
  510. * DB_odbc::nextID(), DB_odbc::dropSequence()
  511. */
  512. function createSequence($seq_name)
  513. {
  514. return $this->query('CREATE TABLE '
  515. . $this->getSequenceName($seq_name)
  516. . ' (id integer NOT NULL,'
  517. . ' PRIMARY KEY(id))');
  518. }
  519. // }}}
  520. // {{{ dropSequence()
  521. /**
  522. * Deletes a sequence
  523. *
  524. * @param string $seq_name name of the sequence to be deleted
  525. *
  526. * @return int DB_OK on success. A DB_Error object on failure.
  527. *
  528. * @see DB_common::dropSequence(), DB_common::getSequenceName(),
  529. * DB_odbc::nextID(), DB_odbc::createSequence()
  530. */
  531. function dropSequence($seq_name)
  532. {
  533. return $this->query('DROP TABLE ' . $this->getSequenceName($seq_name));
  534. }
  535. // }}}
  536. // {{{ autoCommit()
  537. /**
  538. * Enables or disables automatic commits
  539. *
  540. * @param bool $onoff true turns it on, false turns it off
  541. *
  542. * @return int DB_OK on success. A DB_Error object if the driver
  543. * doesn't support auto-committing transactions.
  544. */
  545. function autoCommit($onoff = false)
  546. {
  547. if (!@odbc_autocommit($this->connection, $onoff)) {
  548. return $this->odbcRaiseError();
  549. }
  550. return DB_OK;
  551. }
  552. // }}}
  553. // {{{ commit()
  554. /**
  555. * Commits the current transaction
  556. *
  557. * @return int DB_OK on success. A DB_Error object on failure.
  558. */
  559. function commit()
  560. {
  561. if (!@odbc_commit($this->connection)) {
  562. return $this->odbcRaiseError();
  563. }
  564. return DB_OK;
  565. }
  566. // }}}
  567. // {{{ rollback()
  568. /**
  569. * Reverts the current transaction
  570. *
  571. * @return int DB_OK on success. A DB_Error object on failure.
  572. */
  573. function rollback()
  574. {
  575. if (!@odbc_rollback($this->connection)) {
  576. return $this->odbcRaiseError();
  577. }
  578. return DB_OK;
  579. }
  580. // }}}
  581. // {{{ odbcRaiseError()
  582. /**
  583. * Produces a DB_Error object regarding the current problem
  584. *
  585. * @param int $errno if the error is being manually raised pass a
  586. * DB_ERROR* constant here. If this isn't passed
  587. * the error information gathered from the DBMS.
  588. *
  589. * @return object the DB_Error object
  590. *
  591. * @see DB_common::raiseError(),
  592. * DB_odbc::errorNative(), DB_common::errorCode()
  593. */
  594. function odbcRaiseError($errno = null)
  595. {
  596. if ($errno === null) {
  597. switch ($this->dbsyntax) {
  598. case 'access':
  599. if ($this->options['portability'] & DB_PORTABILITY_ERRORS) {
  600. $this->errorcode_map['07001'] = DB_ERROR_NOSUCHFIELD;
  601. } else {
  602. // Doing this in case mode changes during runtime.
  603. $this->errorcode_map['07001'] = DB_ERROR_MISMATCH;
  604. }
  605. $native_code = odbc_error($this->connection);
  606. // S1000 is for "General Error." Let's be more specific.
  607. if ($native_code == 'S1000') {
  608. $errormsg = odbc_errormsg($this->connection);
  609. static $error_regexps;
  610. if (!isset($error_regexps)) {
  611. $error_regexps = array(
  612. '/includes related records.$/i' => DB_ERROR_CONSTRAINT,
  613. '/cannot contain a Null value/i' => DB_ERROR_CONSTRAINT_NOT_NULL,
  614. );
  615. }
  616. foreach ($error_regexps as $regexp => $code) {
  617. if (preg_match($regexp, $errormsg)) {
  618. return $this->raiseError($code,
  619. null, null, null,
  620. $native_code . ' ' . $errormsg);
  621. }
  622. }
  623. $errno = DB_ERROR;
  624. } else {
  625. $errno = $this->errorCode($native_code);
  626. }
  627. break;
  628. default:
  629. $errno = $this->errorCode(odbc_error($this->connection));
  630. }
  631. }
  632. return $this->raiseError($errno, null, null, null,
  633. $this->errorNative());
  634. }
  635. // }}}
  636. // {{{ errorNative()
  637. /**
  638. * Gets the DBMS' native error code and message produced by the last query
  639. *
  640. * @return string the DBMS' error code and message
  641. */
  642. function errorNative()
  643. {
  644. if (!is_resource($this->connection)) {
  645. return @odbc_error() . ' ' . @odbc_errormsg();
  646. }
  647. return @odbc_error($this->connection) . ' ' . @odbc_errormsg($this->connection);
  648. }
  649. // }}}
  650. // {{{ tableInfo()
  651. /**
  652. * Returns information about a table or a result set
  653. *
  654. * @param object|string $result DB_result object from a query or a
  655. * string containing the name of a table.
  656. * While this also accepts a query result
  657. * resource identifier, this behavior is
  658. * deprecated.
  659. * @param int $mode a valid tableInfo mode
  660. *
  661. * @return array an associative array with the information requested.
  662. * A DB_Error object on failure.
  663. *
  664. * @see DB_common::tableInfo()
  665. * @since Method available since Release 1.7.0
  666. */
  667. function tableInfo($result, $mode = null)
  668. {
  669. if (is_string($result)) {
  670. /*
  671. * Probably received a table name.
  672. * Create a result resource identifier.
  673. */
  674. $id = @odbc_exec($this->connection, "SELECT * FROM $result");
  675. if (!$id) {
  676. return $this->odbcRaiseError();
  677. }
  678. $got_string = true;
  679. } elseif (isset($result->result)) {
  680. /*
  681. * Probably received a result object.
  682. * Extract the result resource identifier.
  683. */
  684. $id = $result->result;
  685. $got_string = false;
  686. } else {
  687. /*
  688. * Probably received a result resource identifier.
  689. * Copy it.
  690. * Deprecated. Here for compatibility only.
  691. */
  692. $id = $result;
  693. $got_string = false;
  694. }
  695. if (!is_resource($id)) {
  696. return $this->odbcRaiseError(DB_ERROR_NEED_MORE_DATA);
  697. }
  698. if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE) {
  699. $case_func = 'strtolower';
  700. } else {
  701. $case_func = 'strval';
  702. }
  703. $count = @odbc_num_fields($id);
  704. $res = array();
  705. if ($mode) {
  706. $res['num_fields'] = $count;
  707. }
  708. for ($i = 0; $i < $count; $i++) {
  709. $col = $i + 1;
  710. $res[$i] = array(
  711. 'table' => $got_string ? $case_func($result) : '',
  712. 'name' => $case_func(@odbc_field_name($id, $col)),
  713. 'type' => @odbc_field_type($id, $col),
  714. 'len' => @odbc_field_len($id, $col),
  715. 'flags' => '',
  716. );
  717. if ($mode & DB_TABLEINFO_ORDER) {
  718. $res['order'][$res[$i]['name']] = $i;
  719. }
  720. if ($mode & DB_TABLEINFO_ORDERTABLE) {
  721. $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
  722. }
  723. }
  724. // free the result only if we were called on a table
  725. if ($got_string) {
  726. @odbc_free_result($id);
  727. }
  728. return $res;
  729. }
  730. // }}}
  731. // {{{ getSpecialQuery()
  732. /**
  733. * Obtains the query string needed for listing a given type of objects
  734. *
  735. * Thanks to symbol1@gmail.com and Philippe.Jausions@11abacus.com.
  736. *
  737. * @param string $type the kind of objects you want to retrieve
  738. *
  739. * @return string the list of objects requested
  740. *
  741. * @access protected
  742. * @see DB_common::getListOf()
  743. * @since Method available since Release 1.7.0
  744. */
  745. function getSpecialQuery($type)
  746. {
  747. switch ($type) {
  748. case 'databases':
  749. if (!function_exists('odbc_data_source')) {
  750. return null;
  751. }
  752. $res = @odbc_data_source($this->connection, SQL_FETCH_FIRST);
  753. if (is_array($res)) {
  754. $out = array($res['server']);
  755. while($res = @odbc_data_source($this->connection,
  756. SQL_FETCH_NEXT))
  757. {
  758. $out[] = $res['server'];
  759. }
  760. return $out;
  761. } else {
  762. return $this->odbcRaiseError();
  763. }
  764. break;
  765. case 'tables':
  766. case 'schema.tables':
  767. $keep = 'TABLE';
  768. break;
  769. case 'views':
  770. $keep = 'VIEW';
  771. break;
  772. default:
  773. return null;
  774. }
  775. /*
  776. * Removing non-conforming items in the while loop rather than
  777. * in the odbc_tables() call because some backends choke on this:
  778. * odbc_tables($this->connection, '', '', '', 'TABLE')
  779. */
  780. $res = @odbc_tables($this->connection);
  781. if (!$res) {
  782. return $this->odbcRaiseError();
  783. }
  784. $out = array();
  785. while ($row = odbc_fetch_array($res)) {
  786. if ($row['TABLE_TYPE'] != $keep) {
  787. continue;
  788. }
  789. if ($type == 'schema.tables') {
  790. $out[] = $row['TABLE_SCHEM'] . '.' . $row['TABLE_NAME'];
  791. } else {
  792. $out[] = $row['TABLE_NAME'];
  793. }
  794. }
  795. return $out;
  796. }
  797. // }}}
  798. }
  799. /*
  800. * Local variables:
  801. * tab-width: 4
  802. * c-basic-offset: 4
  803. * End:
  804. */
  805. ?>