msql.php 24 KB

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