msql.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * The PEAR DB driver for PHP's msql extension
  5. * for interacting with Mini SQL databases
  6. *
  7. * PHP's mSQL extension did weird things with NULL values prior to PHP
  8. * 4.3.11 and 5.0.4. Make sure your version of PHP meets or exceeds
  9. * those versions.
  10. *
  11. * PHP version 5
  12. *
  13. * LICENSE: This source file is subject to version 3.0 of the PHP license
  14. * that is available through the world-wide-web at the following URI:
  15. * http://www.php.net/license/3_0.txt. If you did not receive a copy of
  16. * the PHP License and are unable to obtain it through the web, please
  17. * send a note to license@php.net so we can mail you a copy immediately.
  18. *
  19. * @category Database
  20. * @package DB
  21. * @author Daniel Convissor <danielc@php.net>
  22. * @copyright 1997-2007 The PHP Group
  23. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  24. * @version CVS: $Id$
  25. * @link http://pear.php.net/package/DB
  26. */
  27. /**
  28. * Obtain the DB_common class so it can be extended from
  29. */
  30. require_once 'DB/common.php';
  31. /**
  32. * The methods PEAR DB uses to interact with PHP's msql extension
  33. * for interacting with Mini SQL databases
  34. *
  35. * These methods overload the ones declared in DB_common.
  36. *
  37. * PHP's mSQL extension did weird things with NULL values prior to PHP
  38. * 4.3.11 and 5.0.4. Make sure your version of PHP meets or exceeds
  39. * those versions.
  40. *
  41. * @category Database
  42. * @package DB
  43. * @author Daniel Convissor <danielc@php.net>
  44. * @copyright 1997-2007 The PHP Group
  45. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  46. * @version Release: 1.8.2
  47. * @link http://pear.php.net/package/DB
  48. * @since Class not functional until Release 1.7.0
  49. */
  50. class DB_msql extends DB_common
  51. {
  52. // {{{ properties
  53. /**
  54. * The DB driver type (mysql, oci8, odbc, etc.)
  55. * @var string
  56. */
  57. var $phptype = 'msql';
  58. /**
  59. * The database syntax variant to be used (db2, access, etc.), if any
  60. * @var string
  61. */
  62. var $dbsyntax = 'msql';
  63. /**
  64. * The capabilities of this DB implementation
  65. *
  66. * The 'new_link' element contains the PHP version that first provided
  67. * new_link support for this DBMS. Contains false if it's unsupported.
  68. *
  69. * Meaning of the 'limit' element:
  70. * + 'emulate' = emulate with fetch row by number
  71. * + 'alter' = alter the query
  72. * + false = skip rows
  73. *
  74. * @var array
  75. */
  76. var $features = array(
  77. 'limit' => 'emulate',
  78. 'new_link' => false,
  79. 'numrows' => true,
  80. 'pconnect' => true,
  81. 'prepare' => false,
  82. 'ssl' => false,
  83. 'transactions' => false,
  84. );
  85. /**
  86. * A mapping of native error codes to DB error codes
  87. * @var array
  88. */
  89. var $errorcode_map = array(
  90. );
  91. /**
  92. * The raw database connection created by PHP
  93. * @var resource
  94. */
  95. var $connection;
  96. /**
  97. * The DSN information for connecting to a database
  98. * @var array
  99. */
  100. var $dsn = array();
  101. /**
  102. * The query result resource created by PHP
  103. *
  104. * Used to make affectedRows() work. Only contains the result for
  105. * data manipulation queries. Contains false for other queries.
  106. *
  107. * @var resource
  108. * @access private
  109. */
  110. var $_result;
  111. // }}}
  112. // {{{ constructor
  113. /**
  114. * This constructor calls <kbd>$this->DB_common()</kbd>
  115. *
  116. * @return void
  117. */
  118. function DB_msql()
  119. {
  120. $this->DB_common();
  121. }
  122. // }}}
  123. // {{{ connect()
  124. /**
  125. * Connect to the database server, log in and open the database
  126. *
  127. * Don't call this method directly. Use DB::connect() instead.
  128. *
  129. * Example of how to connect:
  130. * <code>
  131. * require_once 'DB.php';
  132. *
  133. * // $dsn = 'msql://hostname/dbname'; // use a TCP connection
  134. * $dsn = 'msql:///dbname'; // use a socket
  135. * $options = array(
  136. * 'portability' => DB_PORTABILITY_ALL,
  137. * );
  138. *
  139. * $db = DB::connect($dsn, $options);
  140. * if (PEAR::isError($db)) {
  141. * die($db->getMessage());
  142. * }
  143. * </code>
  144. *
  145. * @param array $dsn the data source name
  146. * @param bool $persistent should the connection be persistent?
  147. *
  148. * @return int DB_OK on success. A DB_Error object on failure.
  149. */
  150. function connect($dsn, $persistent = false)
  151. {
  152. if (!PEAR::loadExtension('msql')) {
  153. return $this->raiseError(DB_ERROR_EXTENSION_NOT_FOUND);
  154. }
  155. $this->dsn = $dsn;
  156. if ($dsn['dbsyntax']) {
  157. $this->dbsyntax = $dsn['dbsyntax'];
  158. }
  159. $params = array();
  160. if ($dsn['hostspec']) {
  161. $params[] = $dsn['port']
  162. ? $dsn['hostspec'] . ',' . $dsn['port']
  163. : $dsn['hostspec'];
  164. }
  165. $connect_function = $persistent ? 'msql_pconnect' : 'msql_connect';
  166. $ini = ini_get('track_errors');
  167. $php_errormsg = '';
  168. if ($ini) {
  169. $this->connection = @call_user_func_array($connect_function,
  170. $params);
  171. } else {
  172. @ini_set('track_errors', 1);
  173. $this->connection = @call_user_func_array($connect_function,
  174. $params);
  175. @ini_set('track_errors', $ini);
  176. }
  177. if (!$this->connection) {
  178. if (($err = @msql_error()) != '') {
  179. return $this->raiseError(DB_ERROR_CONNECT_FAILED,
  180. null, null, null,
  181. $err);
  182. } else {
  183. return $this->raiseError(DB_ERROR_CONNECT_FAILED,
  184. null, null, null,
  185. $php_errormsg);
  186. }
  187. }
  188. if (!@msql_select_db($dsn['database'], $this->connection)) {
  189. return $this->msqlRaiseError();
  190. }
  191. return DB_OK;
  192. }
  193. // }}}
  194. // {{{ disconnect()
  195. /**
  196. * Disconnects from the database server
  197. *
  198. * @return bool TRUE on success, FALSE on failure
  199. */
  200. function disconnect()
  201. {
  202. $ret = @msql_close($this->connection);
  203. $this->connection = null;
  204. return $ret;
  205. }
  206. // }}}
  207. // {{{ simpleQuery()
  208. /**
  209. * Sends a query to the database server
  210. *
  211. * @param string the SQL query string
  212. *
  213. * @return mixed + a PHP result resrouce for successful SELECT queries
  214. * + the DB_OK constant for other successful queries
  215. * + a DB_Error object on failure
  216. */
  217. function simpleQuery($query)
  218. {
  219. $this->last_query = $query;
  220. $query = $this->modifyQuery($query);
  221. $result = @msql_query($query, $this->connection);
  222. if (!$result) {
  223. return $this->msqlRaiseError();
  224. }
  225. // Determine which queries that should return data, and which
  226. // should return an error code only.
  227. if ($this->_checkManip($query)) {
  228. $this->_result = $result;
  229. return DB_OK;
  230. } else {
  231. $this->_result = false;
  232. return $result;
  233. }
  234. }
  235. // }}}
  236. // {{{ nextResult()
  237. /**
  238. * Move the internal msql result pointer to the next available result
  239. *
  240. * @param a valid fbsql result resource
  241. *
  242. * @access public
  243. *
  244. * @return true if a result is available otherwise return false
  245. */
  246. function nextResult($result)
  247. {
  248. return false;
  249. }
  250. // }}}
  251. // {{{ fetchInto()
  252. /**
  253. * Places a row from the result set into the given array
  254. *
  255. * Formating of the array and the data therein are configurable.
  256. * See DB_result::fetchInto() for more information.
  257. *
  258. * This method is not meant to be called directly. Use
  259. * DB_result::fetchInto() instead. It can't be declared "protected"
  260. * because DB_result is a separate object.
  261. *
  262. * PHP's mSQL extension did weird things with NULL values prior to PHP
  263. * 4.3.11 and 5.0.4. Make sure your version of PHP meets or exceeds
  264. * those versions.
  265. *
  266. * @param resource $result the query result resource
  267. * @param array $arr the referenced array to put the data in
  268. * @param int $fetchmode how the resulting array should be indexed
  269. * @param int $rownum the row number to fetch (0 = first row)
  270. *
  271. * @return mixed DB_OK on success, NULL when the end of a result set is
  272. * reached or on failure
  273. *
  274. * @see DB_result::fetchInto()
  275. */
  276. function fetchInto($result, &$arr, $fetchmode, $rownum = null)
  277. {
  278. if ($rownum !== null) {
  279. if (!@msql_data_seek($result, $rownum)) {
  280. return null;
  281. }
  282. }
  283. if ($fetchmode & DB_FETCHMODE_ASSOC) {
  284. $arr = @msql_fetch_array($result, MSQL_ASSOC);
  285. if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE && $arr) {
  286. $arr = array_change_key_case($arr, CASE_LOWER);
  287. }
  288. } else {
  289. $arr = @msql_fetch_row($result);
  290. }
  291. if (!$arr) {
  292. return null;
  293. }
  294. if ($this->options['portability'] & DB_PORTABILITY_RTRIM) {
  295. $this->_rtrimArrayValues($arr);
  296. }
  297. if ($this->options['portability'] & DB_PORTABILITY_NULL_TO_EMPTY) {
  298. $this->_convertNullArrayValuesToEmpty($arr);
  299. }
  300. return DB_OK;
  301. }
  302. // }}}
  303. // {{{ freeResult()
  304. /**
  305. * Deletes the result set and frees the memory occupied by the result set
  306. *
  307. * This method is not meant to be called directly. Use
  308. * DB_result::free() instead. It can't be declared "protected"
  309. * because DB_result is a separate object.
  310. *
  311. * @param resource $result PHP's query result resource
  312. *
  313. * @return bool TRUE on success, FALSE if $result is invalid
  314. *
  315. * @see DB_result::free()
  316. */
  317. function freeResult($result)
  318. {
  319. return is_resource($result) ? msql_free_result($result) : false;
  320. }
  321. // }}}
  322. // {{{ numCols()
  323. /**
  324. * Gets the number of columns in a result set
  325. *
  326. * This method is not meant to be called directly. Use
  327. * DB_result::numCols() 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 int the number of columns. A DB_Error object on failure.
  333. *
  334. * @see DB_result::numCols()
  335. */
  336. function numCols($result)
  337. {
  338. $cols = @msql_num_fields($result);
  339. if (!$cols) {
  340. return $this->msqlRaiseError();
  341. }
  342. return $cols;
  343. }
  344. // }}}
  345. // {{{ numRows()
  346. /**
  347. * Gets the number of rows in a result set
  348. *
  349. * This method is not meant to be called directly. Use
  350. * DB_result::numRows() instead. It can't be declared "protected"
  351. * because DB_result is a separate object.
  352. *
  353. * @param resource $result PHP's query result resource
  354. *
  355. * @return int the number of rows. A DB_Error object on failure.
  356. *
  357. * @see DB_result::numRows()
  358. */
  359. function numRows($result)
  360. {
  361. $rows = @msql_num_rows($result);
  362. if ($rows === false) {
  363. return $this->msqlRaiseError();
  364. }
  365. return $rows;
  366. }
  367. // }}}
  368. // {{{ affected()
  369. /**
  370. * Determines the number of rows affected by a data maniuplation query
  371. *
  372. * 0 is returned for queries that don't manipulate data.
  373. *
  374. * @return int the number of rows. A DB_Error object on failure.
  375. */
  376. function affectedRows()
  377. {
  378. if (!$this->_result) {
  379. return 0;
  380. }
  381. return msql_affected_rows($this->_result);
  382. }
  383. // }}}
  384. // {{{ nextId()
  385. /**
  386. * Returns the next free id in a sequence
  387. *
  388. * @param string $seq_name name of the sequence
  389. * @param boolean $ondemand when true, the seqence is automatically
  390. * created if it does not exist
  391. *
  392. * @return int the next id number in the sequence.
  393. * A DB_Error object on failure.
  394. *
  395. * @see DB_common::nextID(), DB_common::getSequenceName(),
  396. * DB_msql::createSequence(), DB_msql::dropSequence()
  397. */
  398. function nextId($seq_name, $ondemand = true)
  399. {
  400. $seqname = $this->getSequenceName($seq_name);
  401. $repeat = false;
  402. do {
  403. $this->pushErrorHandling(PEAR_ERROR_RETURN);
  404. $result = $this->query("SELECT _seq FROM ${seqname}");
  405. $this->popErrorHandling();
  406. if ($ondemand && DB::isError($result) &&
  407. $result->getCode() == DB_ERROR_NOSUCHTABLE) {
  408. $repeat = true;
  409. $this->pushErrorHandling(PEAR_ERROR_RETURN);
  410. $result = $this->createSequence($seq_name);
  411. $this->popErrorHandling();
  412. if (DB::isError($result)) {
  413. return $this->raiseError($result);
  414. }
  415. } else {
  416. $repeat = false;
  417. }
  418. } while ($repeat);
  419. if (DB::isError($result)) {
  420. return $this->raiseError($result);
  421. }
  422. $arr = $result->fetchRow(DB_FETCHMODE_ORDERED);
  423. $result->free();
  424. return $arr[0];
  425. }
  426. // }}}
  427. // {{{ createSequence()
  428. /**
  429. * Creates a new sequence
  430. *
  431. * Also creates a new table to associate the sequence with. Uses
  432. * a separate table to ensure portability with other drivers.
  433. *
  434. * @param string $seq_name name of the new sequence
  435. *
  436. * @return int DB_OK on success. A DB_Error object on failure.
  437. *
  438. * @see DB_common::createSequence(), DB_common::getSequenceName(),
  439. * DB_msql::nextID(), DB_msql::dropSequence()
  440. */
  441. function createSequence($seq_name)
  442. {
  443. $seqname = $this->getSequenceName($seq_name);
  444. $res = $this->query('CREATE TABLE ' . $seqname
  445. . ' (id INTEGER NOT NULL)');
  446. if (DB::isError($res)) {
  447. return $res;
  448. }
  449. $res = $this->query("CREATE SEQUENCE ON ${seqname}");
  450. return $res;
  451. }
  452. // }}}
  453. // {{{ dropSequence()
  454. /**
  455. * Deletes a sequence
  456. *
  457. * @param string $seq_name name of the sequence to be deleted
  458. *
  459. * @return int DB_OK on success. A DB_Error object on failure.
  460. *
  461. * @see DB_common::dropSequence(), DB_common::getSequenceName(),
  462. * DB_msql::nextID(), DB_msql::createSequence()
  463. */
  464. function dropSequence($seq_name)
  465. {
  466. return $this->query('DROP TABLE ' . $this->getSequenceName($seq_name));
  467. }
  468. // }}}
  469. // {{{ quoteIdentifier()
  470. /**
  471. * mSQL does not support delimited identifiers
  472. *
  473. * @param string $str the identifier name to be quoted
  474. *
  475. * @return object a DB_Error object
  476. *
  477. * @see DB_common::quoteIdentifier()
  478. * @since Method available since Release 1.7.0
  479. */
  480. function quoteIdentifier($str)
  481. {
  482. return $this->raiseError(DB_ERROR_UNSUPPORTED);
  483. }
  484. // }}}
  485. // {{{ quoteFloat()
  486. /**
  487. * Formats a float value for use within a query in a locale-independent
  488. * manner.
  489. *
  490. * @param float the float value to be quoted.
  491. * @return string the quoted string.
  492. * @see DB_common::quoteSmart()
  493. * @since Method available since release 1.7.8.
  494. */
  495. function quoteFloat($float) {
  496. return $this->escapeSimple(str_replace(',', '.', strval(floatval($float))));
  497. }
  498. // }}}
  499. // {{{ escapeSimple()
  500. /**
  501. * Escapes a string according to the current DBMS's standards
  502. *
  503. * @param string $str the string to be escaped
  504. *
  505. * @return string the escaped string
  506. *
  507. * @see DB_common::quoteSmart()
  508. * @since Method available since Release 1.7.0
  509. */
  510. function escapeSimple($str)
  511. {
  512. return addslashes($str);
  513. }
  514. // }}}
  515. // {{{ msqlRaiseError()
  516. /**
  517. * Produces a DB_Error object regarding the current problem
  518. *
  519. * @param int $errno if the error is being manually raised pass a
  520. * DB_ERROR* constant here. If this isn't passed
  521. * the error information gathered from the DBMS.
  522. *
  523. * @return object the DB_Error object
  524. *
  525. * @see DB_common::raiseError(),
  526. * DB_msql::errorNative(), DB_msql::errorCode()
  527. */
  528. function msqlRaiseError($errno = null)
  529. {
  530. $native = $this->errorNative();
  531. if ($errno === null) {
  532. $errno = $this->errorCode($native);
  533. }
  534. return $this->raiseError($errno, null, null, null, $native);
  535. }
  536. // }}}
  537. // {{{ errorNative()
  538. /**
  539. * Gets the DBMS' native error message produced by the last query
  540. *
  541. * @return string the DBMS' error message
  542. */
  543. function errorNative()
  544. {
  545. return @msql_error();
  546. }
  547. // }}}
  548. // {{{ errorCode()
  549. /**
  550. * Determines PEAR::DB error code from the database's text error message
  551. *
  552. * @param string $errormsg the error message returned from the database
  553. *
  554. * @return integer the error number from a DB_ERROR* constant
  555. */
  556. function errorCode($errormsg)
  557. {
  558. static $error_regexps;
  559. // PHP 5.2+ prepends the function name to $php_errormsg, so we need
  560. // this hack to work around it, per bug #9599.
  561. $errormsg = preg_replace('/^msql[a-z_]+\(\): /', '', $errormsg);
  562. if (!isset($error_regexps)) {
  563. $error_regexps = array(
  564. '/^Access to database denied/i'
  565. => DB_ERROR_ACCESS_VIOLATION,
  566. '/^Bad index name/i'
  567. => DB_ERROR_ALREADY_EXISTS,
  568. '/^Bad order field/i'
  569. => DB_ERROR_SYNTAX,
  570. '/^Bad type for comparison/i'
  571. => DB_ERROR_SYNTAX,
  572. '/^Can\'t perform LIKE on/i'
  573. => DB_ERROR_SYNTAX,
  574. '/^Can\'t use TEXT fields in LIKE comparison/i'
  575. => DB_ERROR_SYNTAX,
  576. '/^Couldn\'t create temporary table/i'
  577. => DB_ERROR_CANNOT_CREATE,
  578. '/^Error creating table file/i'
  579. => DB_ERROR_CANNOT_CREATE,
  580. '/^Field .* cannot be null$/i'
  581. => DB_ERROR_CONSTRAINT_NOT_NULL,
  582. '/^Index (field|condition) .* cannot be null$/i'
  583. => DB_ERROR_SYNTAX,
  584. '/^Invalid date format/i'
  585. => DB_ERROR_INVALID_DATE,
  586. '/^Invalid time format/i'
  587. => DB_ERROR_INVALID,
  588. '/^Literal value for .* is wrong type$/i'
  589. => DB_ERROR_INVALID_NUMBER,
  590. '/^No Database Selected/i'
  591. => DB_ERROR_NODBSELECTED,
  592. '/^No value specified for field/i'
  593. => DB_ERROR_VALUE_COUNT_ON_ROW,
  594. '/^Non unique value for unique index/i'
  595. => DB_ERROR_CONSTRAINT,
  596. '/^Out of memory for temporary table/i'
  597. => DB_ERROR_CANNOT_CREATE,
  598. '/^Permission denied/i'
  599. => DB_ERROR_ACCESS_VIOLATION,
  600. '/^Reference to un-selected table/i'
  601. => DB_ERROR_SYNTAX,
  602. '/^syntax error/i'
  603. => DB_ERROR_SYNTAX,
  604. '/^Table .* exists$/i'
  605. => DB_ERROR_ALREADY_EXISTS,
  606. '/^Unknown database/i'
  607. => DB_ERROR_NOSUCHDB,
  608. '/^Unknown field/i'
  609. => DB_ERROR_NOSUCHFIELD,
  610. '/^Unknown (index|system variable)/i'
  611. => DB_ERROR_NOT_FOUND,
  612. '/^Unknown table/i'
  613. => DB_ERROR_NOSUCHTABLE,
  614. '/^Unqualified field/i'
  615. => DB_ERROR_SYNTAX,
  616. );
  617. }
  618. foreach ($error_regexps as $regexp => $code) {
  619. if (preg_match($regexp, $errormsg)) {
  620. return $code;
  621. }
  622. }
  623. return DB_ERROR;
  624. }
  625. // }}}
  626. // {{{ tableInfo()
  627. /**
  628. * Returns information about a table or a result set
  629. *
  630. * @param object|string $result DB_result object from a query or a
  631. * string containing the name of a table.
  632. * While this also accepts a query result
  633. * resource identifier, this behavior is
  634. * deprecated.
  635. * @param int $mode a valid tableInfo mode
  636. *
  637. * @return array an associative array with the information requested.
  638. * A DB_Error object on failure.
  639. *
  640. * @see DB_common::setOption()
  641. */
  642. function tableInfo($result, $mode = null)
  643. {
  644. if (is_string($result)) {
  645. /*
  646. * Probably received a table name.
  647. * Create a result resource identifier.
  648. */
  649. $id = @msql_query("SELECT * FROM $result",
  650. $this->connection);
  651. $got_string = true;
  652. } elseif (isset($result->result)) {
  653. /*
  654. * Probably received a result object.
  655. * Extract the result resource identifier.
  656. */
  657. $id = $result->result;
  658. $got_string = false;
  659. } else {
  660. /*
  661. * Probably received a result resource identifier.
  662. * Copy it.
  663. * Deprecated. Here for compatibility only.
  664. */
  665. $id = $result;
  666. $got_string = false;
  667. }
  668. if (!is_resource($id)) {
  669. return $this->raiseError(DB_ERROR_NEED_MORE_DATA);
  670. }
  671. if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE) {
  672. $case_func = 'strtolower';
  673. } else {
  674. $case_func = 'strval';
  675. }
  676. $count = @msql_num_fields($id);
  677. $res = array();
  678. if ($mode) {
  679. $res['num_fields'] = $count;
  680. }
  681. for ($i = 0; $i < $count; $i++) {
  682. $tmp = @msql_fetch_field($id);
  683. $flags = '';
  684. if ($tmp->not_null) {
  685. $flags .= 'not_null ';
  686. }
  687. if ($tmp->unique) {
  688. $flags .= 'unique_key ';
  689. }
  690. $flags = trim($flags);
  691. $res[$i] = array(
  692. 'table' => $case_func($tmp->table),
  693. 'name' => $case_func($tmp->name),
  694. 'type' => $tmp->type,
  695. 'len' => msql_field_len($id, $i),
  696. 'flags' => $flags,
  697. );
  698. if ($mode & DB_TABLEINFO_ORDER) {
  699. $res['order'][$res[$i]['name']] = $i;
  700. }
  701. if ($mode & DB_TABLEINFO_ORDERTABLE) {
  702. $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
  703. }
  704. }
  705. // free the result only if we were called on a table
  706. if ($got_string) {
  707. @msql_free_result($id);
  708. }
  709. return $res;
  710. }
  711. // }}}
  712. // {{{ getSpecialQuery()
  713. /**
  714. * Obtain a list of a given type of objects
  715. *
  716. * @param string $type the kind of objects you want to retrieve
  717. *
  718. * @return array the array containing the list of objects requested
  719. *
  720. * @access protected
  721. * @see DB_common::getListOf()
  722. */
  723. function getSpecialQuery($type)
  724. {
  725. switch ($type) {
  726. case 'databases':
  727. $id = @msql_list_dbs($this->connection);
  728. break;
  729. case 'tables':
  730. $id = @msql_list_tables($this->dsn['database'],
  731. $this->connection);
  732. break;
  733. default:
  734. return null;
  735. }
  736. if (!$id) {
  737. return $this->msqlRaiseError();
  738. }
  739. $out = array();
  740. while ($row = @msql_fetch_row($id)) {
  741. $out[] = $row[0];
  742. }
  743. return $out;
  744. }
  745. // }}}
  746. }
  747. /*
  748. * Local variables:
  749. * tab-width: 4
  750. * c-basic-offset: 4
  751. * End:
  752. */
  753. ?>