odbc.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  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.9.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. public $phptype = 'odbc';
  54. /**
  55. * The database syntax variant to be used (db2, access, etc.), if any
  56. * @var string
  57. */
  58. public $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. public $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. public $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. public $connection;
  127. /**
  128. * The DSN information for connecting to a database
  129. * @var array
  130. */
  131. public $dsn = array();
  132. /**
  133. * The number of rows affected by a data manipulation query
  134. * @var integer
  135. * @access private
  136. */
  137. public $affected = 0;
  138. // }}}
  139. // {{{ constructor
  140. /**
  141. * This constructor calls <kbd>parent::__construct()</kbd>
  142. *
  143. * @return void
  144. */
  145. public function __construct()
  146. {
  147. parent::__construct();
  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. public 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(
  196. $odbcdsn,
  197. $dsn['username'],
  198. $dsn['password']
  199. );
  200. } else {
  201. $this->connection = @$connect_function(
  202. $odbcdsn,
  203. $dsn['username'],
  204. $dsn['password'],
  205. $dsn['cursor']
  206. );
  207. }
  208. if (!is_resource($this->connection)) {
  209. return $this->raiseError(
  210. DB_ERROR_CONNECT_FAILED,
  211. null,
  212. null,
  213. null,
  214. $this->errorNative()
  215. );
  216. }
  217. return DB_OK;
  218. }
  219. // }}}
  220. // {{{ disconnect()
  221. /**
  222. * Disconnects from the database server
  223. *
  224. * @return bool TRUE on success, FALSE on failure
  225. */
  226. public function disconnect()
  227. {
  228. $err = @odbc_close($this->connection);
  229. $this->connection = null;
  230. return $err;
  231. }
  232. // }}}
  233. // {{{ simpleQuery()
  234. /**
  235. * Sends a query to the database server
  236. *
  237. * @param string the SQL query string
  238. *
  239. * @return mixed + a PHP result resrouce for successful SELECT queries
  240. * + the DB_OK constant for other successful queries
  241. * + a DB_Error object on failure
  242. */
  243. public function simpleQuery($query)
  244. {
  245. $this->last_query = $query;
  246. $query = $this->modifyQuery($query);
  247. $result = @odbc_exec($this->connection, $query);
  248. if (!$result) {
  249. return $this->odbcRaiseError(); // XXX ERRORMSG
  250. }
  251. // Determine which queries that should return data, and which
  252. // should return an error code only.
  253. if ($this->_checkManip($query)) {
  254. $this->affected = $result; // For affectedRows()
  255. return DB_OK;
  256. }
  257. $this->affected = 0;
  258. return $result;
  259. }
  260. // }}}
  261. // {{{ nextResult()
  262. /**
  263. * Move the internal odbc result pointer to the next available result
  264. *
  265. * @param a valid fbsql result resource
  266. *
  267. * @access public
  268. *
  269. * @return true if a result is available otherwise return false
  270. */
  271. public function nextResult($result)
  272. {
  273. return @odbc_next_result($result);
  274. }
  275. // }}}
  276. // {{{ fetchInto()
  277. /**
  278. * Places a row from the result set into the given array
  279. *
  280. * Formating of the array and the data therein are configurable.
  281. * See DB_result::fetchInto() for more information.
  282. *
  283. * This method is not meant to be called directly. Use
  284. * DB_result::fetchInto() instead. It can't be declared "protected"
  285. * because DB_result is a separate object.
  286. *
  287. * @param resource $result the query result resource
  288. * @param array $arr the referenced array to put the data in
  289. * @param int $fetchmode how the resulting array should be indexed
  290. * @param int $rownum the row number to fetch (0 = first row)
  291. *
  292. * @return mixed DB_OK on success, NULL when the end of a result set is
  293. * reached or on failure
  294. *
  295. * @see DB_result::fetchInto()
  296. */
  297. public function fetchInto($result, &$arr, $fetchmode, $rownum = null)
  298. {
  299. $arr = array();
  300. if ($rownum !== null) {
  301. $rownum++; // ODBC first row is 1
  302. if (version_compare(phpversion(), '4.2.0', 'ge')) {
  303. $cols = @odbc_fetch_into($result, $arr, $rownum);
  304. } else {
  305. $cols = @odbc_fetch_into($result, $rownum, $arr);
  306. }
  307. } else {
  308. $cols = @odbc_fetch_into($result, $arr);
  309. }
  310. if (!$cols) {
  311. return null;
  312. }
  313. if ($fetchmode !== DB_FETCHMODE_ORDERED) {
  314. for ($i = 0; $i < count($arr); $i++) {
  315. $colName = @odbc_field_name($result, $i+1);
  316. $a[$colName] = $arr[$i];
  317. }
  318. if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE) {
  319. $a = array_change_key_case($a, CASE_LOWER);
  320. }
  321. $arr = $a;
  322. }
  323. if ($this->options['portability'] & DB_PORTABILITY_RTRIM) {
  324. $this->_rtrimArrayValues($arr);
  325. }
  326. if ($this->options['portability'] & DB_PORTABILITY_NULL_TO_EMPTY) {
  327. $this->_convertNullArrayValuesToEmpty($arr);
  328. }
  329. return DB_OK;
  330. }
  331. // }}}
  332. // {{{ freeResult()
  333. /**
  334. * Deletes the result set and frees the memory occupied by the result set
  335. *
  336. * This method is not meant to be called directly. Use
  337. * DB_result::free() instead. It can't be declared "protected"
  338. * because DB_result is a separate object.
  339. *
  340. * @param resource $result PHP's query result resource
  341. *
  342. * @return bool TRUE on success, FALSE if $result is invalid
  343. *
  344. * @see DB_result::free()
  345. */
  346. public function freeResult($result)
  347. {
  348. return is_resource($result) ? odbc_free_result($result) : false;
  349. }
  350. // }}}
  351. // {{{ numCols()
  352. /**
  353. * Gets the number of columns in a result set
  354. *
  355. * This method is not meant to be called directly. Use
  356. * DB_result::numCols() instead. It can't be declared "protected"
  357. * because DB_result is a separate object.
  358. *
  359. * @param resource $result PHP's query result resource
  360. *
  361. * @return int the number of columns. A DB_Error object on failure.
  362. *
  363. * @see DB_result::numCols()
  364. */
  365. public function numCols($result)
  366. {
  367. $cols = @odbc_num_fields($result);
  368. if (!$cols) {
  369. return $this->odbcRaiseError();
  370. }
  371. return $cols;
  372. }
  373. // }}}
  374. // {{{ affectedRows()
  375. /**
  376. * Determines the number of rows affected by a data maniuplation query
  377. *
  378. * 0 is returned for queries that don't manipulate data.
  379. *
  380. * @return int the number of rows. A DB_Error object on failure.
  381. */
  382. public function affectedRows()
  383. {
  384. if (empty($this->affected)) { // In case of SELECT stms
  385. return 0;
  386. }
  387. $nrows = @odbc_num_rows($this->affected);
  388. if ($nrows == -1) {
  389. return $this->odbcRaiseError();
  390. }
  391. return $nrows;
  392. }
  393. // }}}
  394. // {{{ numRows()
  395. /**
  396. * Gets the number of rows in a result set
  397. *
  398. * Not all ODBC drivers support this functionality. If they don't
  399. * a DB_Error object for DB_ERROR_UNSUPPORTED is returned.
  400. *
  401. * This method is not meant to be called directly. Use
  402. * DB_result::numRows() instead. It can't be declared "protected"
  403. * because DB_result is a separate object.
  404. *
  405. * @param resource $result PHP's query result resource
  406. *
  407. * @return int the number of rows. A DB_Error object on failure.
  408. *
  409. * @see DB_result::numRows()
  410. */
  411. public function numRows($result)
  412. {
  413. $nrows = @odbc_num_rows($result);
  414. if ($nrows == -1) {
  415. return $this->odbcRaiseError(DB_ERROR_UNSUPPORTED);
  416. }
  417. if ($nrows === false) {
  418. return $this->odbcRaiseError();
  419. }
  420. return $nrows;
  421. }
  422. // }}}
  423. // {{{ quoteIdentifier()
  424. /**
  425. * Quotes a string so it can be safely used as a table or column name
  426. *
  427. * Use 'mssql' as the dbsyntax in the DB DSN only if you've unchecked
  428. * "Use ANSI quoted identifiers" when setting up the ODBC data source.
  429. *
  430. * @param string $str identifier name to be quoted
  431. *
  432. * @return string quoted identifier string
  433. *
  434. * @see DB_common::quoteIdentifier()
  435. * @since Method available since Release 1.6.0
  436. */
  437. public function quoteIdentifier($str)
  438. {
  439. switch ($this->dsn['dbsyntax']) {
  440. case 'access':
  441. return '[' . $str . ']';
  442. case 'mssql':
  443. case 'sybase':
  444. return '[' . str_replace(']', ']]', $str) . ']';
  445. case 'mysql':
  446. case 'mysqli':
  447. return '`' . $str . '`';
  448. default:
  449. return '"' . str_replace('"', '""', $str) . '"';
  450. }
  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. public 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. public 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. public 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. public 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. public 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. public 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. public 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(
  619. $code,
  620. null,
  621. null,
  622. null,
  623. $native_code . ' ' . $errormsg
  624. );
  625. }
  626. }
  627. $errno = DB_ERROR;
  628. } else {
  629. $errno = $this->errorCode($native_code);
  630. }
  631. break;
  632. default:
  633. $errno = $this->errorCode(odbc_error($this->connection));
  634. }
  635. }
  636. return $this->raiseError(
  637. $errno,
  638. null,
  639. null,
  640. null,
  641. $this->errorNative()
  642. );
  643. }
  644. // }}}
  645. // {{{ errorNative()
  646. /**
  647. * Gets the DBMS' native error code and message produced by the last query
  648. *
  649. * @return string the DBMS' error code and message
  650. */
  651. public function errorNative()
  652. {
  653. if (!is_resource($this->connection)) {
  654. return @odbc_error() . ' ' . @odbc_errormsg();
  655. }
  656. return @odbc_error($this->connection) . ' ' . @odbc_errormsg($this->connection);
  657. }
  658. // }}}
  659. // {{{ tableInfo()
  660. /**
  661. * Returns information about a table or a result set
  662. *
  663. * @param object|string $result DB_result object from a query or a
  664. * string containing the name of a table.
  665. * While this also accepts a query result
  666. * resource identifier, this behavior is
  667. * deprecated.
  668. * @param int $mode a valid tableInfo mode
  669. *
  670. * @return array an associative array with the information requested.
  671. * A DB_Error object on failure.
  672. *
  673. * @see DB_common::tableInfo()
  674. * @since Method available since Release 1.7.0
  675. */
  676. public function tableInfo($result, $mode = null)
  677. {
  678. if (is_string($result)) {
  679. /*
  680. * Probably received a table name.
  681. * Create a result resource identifier.
  682. */
  683. $id = @odbc_exec($this->connection, "SELECT * FROM $result");
  684. if (!$id) {
  685. return $this->odbcRaiseError();
  686. }
  687. $got_string = true;
  688. } elseif (isset($result->result)) {
  689. /*
  690. * Probably received a result object.
  691. * Extract the result resource identifier.
  692. */
  693. $id = $result->result;
  694. $got_string = false;
  695. } else {
  696. /*
  697. * Probably received a result resource identifier.
  698. * Copy it.
  699. * Deprecated. Here for compatibility only.
  700. */
  701. $id = $result;
  702. $got_string = false;
  703. }
  704. if (!is_resource($id)) {
  705. return $this->odbcRaiseError(DB_ERROR_NEED_MORE_DATA);
  706. }
  707. if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE) {
  708. $case_func = 'strtolower';
  709. } else {
  710. $case_func = 'strval';
  711. }
  712. $count = @odbc_num_fields($id);
  713. $res = array();
  714. if ($mode) {
  715. $res['num_fields'] = $count;
  716. }
  717. for ($i = 0; $i < $count; $i++) {
  718. $col = $i + 1;
  719. $res[$i] = array(
  720. 'table' => $got_string ? $case_func($result) : '',
  721. 'name' => $case_func(@odbc_field_name($id, $col)),
  722. 'type' => @odbc_field_type($id, $col),
  723. 'len' => @odbc_field_len($id, $col),
  724. 'flags' => '',
  725. );
  726. if ($mode & DB_TABLEINFO_ORDER) {
  727. $res['order'][$res[$i]['name']] = $i;
  728. }
  729. if ($mode & DB_TABLEINFO_ORDERTABLE) {
  730. $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
  731. }
  732. }
  733. // free the result only if we were called on a table
  734. if ($got_string) {
  735. @odbc_free_result($id);
  736. }
  737. return $res;
  738. }
  739. // }}}
  740. // {{{ getSpecialQuery()
  741. /**
  742. * Obtains the query string needed for listing a given type of objects
  743. *
  744. * Thanks to symbol1@gmail.com and Philippe.Jausions@11abacus.com.
  745. *
  746. * @param string $type the kind of objects you want to retrieve
  747. *
  748. * @return string the list of objects requested
  749. *
  750. * @access protected
  751. * @see DB_common::getListOf()
  752. * @since Method available since Release 1.7.0
  753. */
  754. public function getSpecialQuery($type)
  755. {
  756. switch ($type) {
  757. case 'databases':
  758. if (!function_exists('odbc_data_source')) {
  759. return null;
  760. }
  761. $res = @odbc_data_source($this->connection, SQL_FETCH_FIRST);
  762. if (is_array($res)) {
  763. $out = array($res['server']);
  764. while ($res = @odbc_data_source(
  765. $this->connection,
  766. SQL_FETCH_NEXT
  767. )) {
  768. $out[] = $res['server'];
  769. }
  770. return $out;
  771. } else {
  772. return $this->odbcRaiseError();
  773. }
  774. break;
  775. case 'tables':
  776. case 'schema.tables':
  777. $keep = 'TABLE';
  778. break;
  779. case 'views':
  780. $keep = 'VIEW';
  781. break;
  782. default:
  783. return null;
  784. }
  785. /*
  786. * Removing non-conforming items in the while loop rather than
  787. * in the odbc_tables() call because some backends choke on this:
  788. * odbc_tables($this->connection, '', '', '', 'TABLE')
  789. */
  790. $res = @odbc_tables($this->connection);
  791. if (!$res) {
  792. return $this->odbcRaiseError();
  793. }
  794. $out = array();
  795. while ($row = odbc_fetch_array($res)) {
  796. if ($row['TABLE_TYPE'] != $keep) {
  797. continue;
  798. }
  799. if ($type == 'schema.tables') {
  800. $out[] = $row['TABLE_SCHEM'] . '.' . $row['TABLE_NAME'];
  801. } else {
  802. $out[] = $row['TABLE_NAME'];
  803. }
  804. }
  805. return $out;
  806. }
  807. // }}}
  808. }
  809. /*
  810. * Local variables:
  811. * tab-width: 4
  812. * c-basic-offset: 4
  813. * End:
  814. */