odbc.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872
  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 version 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-2007 The PHP Group
  20. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  21. * @version CVS: $Id$
  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-2007 The PHP Group
  42. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  43. * @version Release: 1.8.2
  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 ($this->_checkManip($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 is_resource($result) ? odbc_free_result($result) : false;
  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. // {{{ nextId()
  444. /**
  445. * Returns the next free id in a sequence
  446. *
  447. * @param string $seq_name name of the sequence
  448. * @param boolean $ondemand when true, the seqence is automatically
  449. * created if it does not exist
  450. *
  451. * @return int the next id number in the sequence.
  452. * A DB_Error object on failure.
  453. *
  454. * @see DB_common::nextID(), DB_common::getSequenceName(),
  455. * DB_odbc::createSequence(), DB_odbc::dropSequence()
  456. */
  457. function nextId($seq_name, $ondemand = true)
  458. {
  459. $seqname = $this->getSequenceName($seq_name);
  460. $repeat = 0;
  461. do {
  462. $this->pushErrorHandling(PEAR_ERROR_RETURN);
  463. $result = $this->query("update ${seqname} set id = id + 1");
  464. $this->popErrorHandling();
  465. if ($ondemand && DB::isError($result) &&
  466. $result->getCode() == DB_ERROR_NOSUCHTABLE) {
  467. $repeat = 1;
  468. $this->pushErrorHandling(PEAR_ERROR_RETURN);
  469. $result = $this->createSequence($seq_name);
  470. $this->popErrorHandling();
  471. if (DB::isError($result)) {
  472. return $this->raiseError($result);
  473. }
  474. $result = $this->query("insert into ${seqname} (id) values(0)");
  475. } else {
  476. $repeat = 0;
  477. }
  478. } while ($repeat);
  479. if (DB::isError($result)) {
  480. return $this->raiseError($result);
  481. }
  482. $result = $this->query("select id from ${seqname}");
  483. if (DB::isError($result)) {
  484. return $result;
  485. }
  486. $row = $result->fetchRow(DB_FETCHMODE_ORDERED);
  487. if (DB::isError($row || !$row)) {
  488. return $row;
  489. }
  490. return $row[0];
  491. }
  492. /**
  493. * Creates a new sequence
  494. *
  495. * @param string $seq_name name of the new sequence
  496. *
  497. * @return int DB_OK on success. A DB_Error object on failure.
  498. *
  499. * @see DB_common::createSequence(), DB_common::getSequenceName(),
  500. * DB_odbc::nextID(), DB_odbc::dropSequence()
  501. */
  502. function createSequence($seq_name)
  503. {
  504. return $this->query('CREATE TABLE '
  505. . $this->getSequenceName($seq_name)
  506. . ' (id integer NOT NULL,'
  507. . ' PRIMARY KEY(id))');
  508. }
  509. // }}}
  510. // {{{ dropSequence()
  511. /**
  512. * Deletes a sequence
  513. *
  514. * @param string $seq_name name of the sequence to be deleted
  515. *
  516. * @return int DB_OK on success. A DB_Error object on failure.
  517. *
  518. * @see DB_common::dropSequence(), DB_common::getSequenceName(),
  519. * DB_odbc::nextID(), DB_odbc::createSequence()
  520. */
  521. function dropSequence($seq_name)
  522. {
  523. return $this->query('DROP TABLE ' . $this->getSequenceName($seq_name));
  524. }
  525. // }}}
  526. // {{{ autoCommit()
  527. /**
  528. * Enables or disables automatic commits
  529. *
  530. * @param bool $onoff true turns it on, false turns it off
  531. *
  532. * @return int DB_OK on success. A DB_Error object if the driver
  533. * doesn't support auto-committing transactions.
  534. */
  535. function autoCommit($onoff = false)
  536. {
  537. if (!@odbc_autocommit($this->connection, $onoff)) {
  538. return $this->odbcRaiseError();
  539. }
  540. return DB_OK;
  541. }
  542. // }}}
  543. // {{{ commit()
  544. /**
  545. * Commits the current transaction
  546. *
  547. * @return int DB_OK on success. A DB_Error object on failure.
  548. */
  549. function commit()
  550. {
  551. if (!@odbc_commit($this->connection)) {
  552. return $this->odbcRaiseError();
  553. }
  554. return DB_OK;
  555. }
  556. // }}}
  557. // {{{ rollback()
  558. /**
  559. * Reverts the current transaction
  560. *
  561. * @return int DB_OK on success. A DB_Error object on failure.
  562. */
  563. function rollback()
  564. {
  565. if (!@odbc_rollback($this->connection)) {
  566. return $this->odbcRaiseError();
  567. }
  568. return DB_OK;
  569. }
  570. // }}}
  571. // {{{ odbcRaiseError()
  572. /**
  573. * Produces a DB_Error object regarding the current problem
  574. *
  575. * @param int $errno if the error is being manually raised pass a
  576. * DB_ERROR* constant here. If this isn't passed
  577. * the error information gathered from the DBMS.
  578. *
  579. * @return object the DB_Error object
  580. *
  581. * @see DB_common::raiseError(),
  582. * DB_odbc::errorNative(), DB_common::errorCode()
  583. */
  584. function odbcRaiseError($errno = null)
  585. {
  586. if ($errno === null) {
  587. switch ($this->dbsyntax) {
  588. case 'access':
  589. if ($this->options['portability'] & DB_PORTABILITY_ERRORS) {
  590. $this->errorcode_map['07001'] = DB_ERROR_NOSUCHFIELD;
  591. } else {
  592. // Doing this in case mode changes during runtime.
  593. $this->errorcode_map['07001'] = DB_ERROR_MISMATCH;
  594. }
  595. $native_code = odbc_error($this->connection);
  596. // S1000 is for "General Error." Let's be more specific.
  597. if ($native_code == 'S1000') {
  598. $errormsg = odbc_errormsg($this->connection);
  599. static $error_regexps;
  600. if (!isset($error_regexps)) {
  601. $error_regexps = array(
  602. '/includes related records.$/i' => DB_ERROR_CONSTRAINT,
  603. '/cannot contain a Null value/i' => DB_ERROR_CONSTRAINT_NOT_NULL,
  604. );
  605. }
  606. foreach ($error_regexps as $regexp => $code) {
  607. if (preg_match($regexp, $errormsg)) {
  608. return $this->raiseError($code,
  609. null, null, null,
  610. $native_code . ' ' . $errormsg);
  611. }
  612. }
  613. $errno = DB_ERROR;
  614. } else {
  615. $errno = $this->errorCode($native_code);
  616. }
  617. break;
  618. default:
  619. $errno = $this->errorCode(odbc_error($this->connection));
  620. }
  621. }
  622. return $this->raiseError($errno, null, null, null,
  623. $this->errorNative());
  624. }
  625. // }}}
  626. // {{{ errorNative()
  627. /**
  628. * Gets the DBMS' native error code and message produced by the last query
  629. *
  630. * @return string the DBMS' error code and message
  631. */
  632. function errorNative()
  633. {
  634. if (!is_resource($this->connection)) {
  635. return @odbc_error() . ' ' . @odbc_errormsg();
  636. }
  637. return @odbc_error($this->connection) . ' ' . @odbc_errormsg($this->connection);
  638. }
  639. // }}}
  640. // {{{ tableInfo()
  641. /**
  642. * Returns information about a table or a result set
  643. *
  644. * @param object|string $result DB_result object from a query or a
  645. * string containing the name of a table.
  646. * While this also accepts a query result
  647. * resource identifier, this behavior is
  648. * deprecated.
  649. * @param int $mode a valid tableInfo mode
  650. *
  651. * @return array an associative array with the information requested.
  652. * A DB_Error object on failure.
  653. *
  654. * @see DB_common::tableInfo()
  655. * @since Method available since Release 1.7.0
  656. */
  657. function tableInfo($result, $mode = null)
  658. {
  659. if (is_string($result)) {
  660. /*
  661. * Probably received a table name.
  662. * Create a result resource identifier.
  663. */
  664. $id = @odbc_exec($this->connection, "SELECT * FROM $result");
  665. if (!$id) {
  666. return $this->odbcRaiseError();
  667. }
  668. $got_string = true;
  669. } elseif (isset($result->result)) {
  670. /*
  671. * Probably received a result object.
  672. * Extract the result resource identifier.
  673. */
  674. $id = $result->result;
  675. $got_string = false;
  676. } else {
  677. /*
  678. * Probably received a result resource identifier.
  679. * Copy it.
  680. * Deprecated. Here for compatibility only.
  681. */
  682. $id = $result;
  683. $got_string = false;
  684. }
  685. if (!is_resource($id)) {
  686. return $this->odbcRaiseError(DB_ERROR_NEED_MORE_DATA);
  687. }
  688. if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE) {
  689. $case_func = 'strtolower';
  690. } else {
  691. $case_func = 'strval';
  692. }
  693. $count = @odbc_num_fields($id);
  694. $res = array();
  695. if ($mode) {
  696. $res['num_fields'] = $count;
  697. }
  698. for ($i = 0; $i < $count; $i++) {
  699. $col = $i + 1;
  700. $res[$i] = array(
  701. 'table' => $got_string ? $case_func($result) : '',
  702. 'name' => $case_func(@odbc_field_name($id, $col)),
  703. 'type' => @odbc_field_type($id, $col),
  704. 'len' => @odbc_field_len($id, $col),
  705. 'flags' => '',
  706. );
  707. if ($mode & DB_TABLEINFO_ORDER) {
  708. $res['order'][$res[$i]['name']] = $i;
  709. }
  710. if ($mode & DB_TABLEINFO_ORDERTABLE) {
  711. $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
  712. }
  713. }
  714. // free the result only if we were called on a table
  715. if ($got_string) {
  716. @odbc_free_result($id);
  717. }
  718. return $res;
  719. }
  720. // }}}
  721. // {{{ getSpecialQuery()
  722. /**
  723. * Obtains the query string needed for listing a given type of objects
  724. *
  725. * Thanks to symbol1@gmail.com and Philippe.Jausions@11abacus.com.
  726. *
  727. * @param string $type the kind of objects you want to retrieve
  728. *
  729. * @return string the list of objects requested
  730. *
  731. * @access protected
  732. * @see DB_common::getListOf()
  733. * @since Method available since Release 1.7.0
  734. */
  735. function getSpecialQuery($type)
  736. {
  737. switch ($type) {
  738. case 'databases':
  739. if (!function_exists('odbc_data_source')) {
  740. return null;
  741. }
  742. $res = @odbc_data_source($this->connection, SQL_FETCH_FIRST);
  743. if (is_array($res)) {
  744. $out = array($res['server']);
  745. while($res = @odbc_data_source($this->connection,
  746. SQL_FETCH_NEXT))
  747. {
  748. $out[] = $res['server'];
  749. }
  750. return $out;
  751. } else {
  752. return $this->odbcRaiseError();
  753. }
  754. break;
  755. case 'tables':
  756. case 'schema.tables':
  757. $keep = 'TABLE';
  758. break;
  759. case 'views':
  760. $keep = 'VIEW';
  761. break;
  762. default:
  763. return null;
  764. }
  765. /*
  766. * Removing non-conforming items in the while loop rather than
  767. * in the odbc_tables() call because some backends choke on this:
  768. * odbc_tables($this->connection, '', '', '', 'TABLE')
  769. */
  770. $res = @odbc_tables($this->connection);
  771. if (!$res) {
  772. return $this->odbcRaiseError();
  773. }
  774. $out = array();
  775. while ($row = odbc_fetch_array($res)) {
  776. if ($row['TABLE_TYPE'] != $keep) {
  777. continue;
  778. }
  779. if ($type == 'schema.tables') {
  780. $out[] = $row['TABLE_SCHEM'] . '.' . $row['TABLE_NAME'];
  781. } else {
  782. $out[] = $row['TABLE_NAME'];
  783. }
  784. }
  785. return $out;
  786. }
  787. // }}}
  788. }
  789. /*
  790. * Local variables:
  791. * tab-width: 4
  792. * c-basic-offset: 4
  793. * End:
  794. */
  795. ?>