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. /**
  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.9.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. public $phptype = 'msql';
  58. /**
  59. * The database syntax variant to be used (db2, access, etc.), if any
  60. * @var string
  61. */
  62. public $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. public $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. public $errorcode_map = array(
  90. );
  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 (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. 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. * Disconnects from the database server
  209. *
  210. * @return bool TRUE on success, FALSE on failure
  211. */
  212. public function disconnect()
  213. {
  214. $ret = @msql_close($this->connection);
  215. $this->connection = null;
  216. return $ret;
  217. }
  218. // }}}
  219. // {{{ simpleQuery()
  220. /**
  221. * Sends a query to the database server
  222. *
  223. * @param string the SQL query string
  224. *
  225. * @return mixed + a PHP result resrouce for successful SELECT queries
  226. * + the DB_OK constant for other successful queries
  227. * + a DB_Error object on failure
  228. */
  229. public function simpleQuery($query)
  230. {
  231. $this->last_query = $query;
  232. $query = $this->modifyQuery($query);
  233. $result = @msql_query($query, $this->connection);
  234. if (!$result) {
  235. return $this->msqlRaiseError();
  236. }
  237. // Determine which queries that should return data, and which
  238. // should return an error code only.
  239. if ($this->_checkManip($query)) {
  240. $this->_result = $result;
  241. return DB_OK;
  242. } else {
  243. $this->_result = false;
  244. return $result;
  245. }
  246. }
  247. // }}}
  248. // {{{ nextResult()
  249. /**
  250. * Move the internal msql result pointer to the next available result
  251. *
  252. * @param a valid fbsql result resource
  253. *
  254. * @access public
  255. *
  256. * @return true if a result is available otherwise return false
  257. */
  258. public function nextResult($result)
  259. {
  260. return false;
  261. }
  262. // }}}
  263. // {{{ fetchInto()
  264. /**
  265. * Places a row from the result set into the given array
  266. *
  267. * Formating of the array and the data therein are configurable.
  268. * See DB_result::fetchInto() for more information.
  269. *
  270. * This method is not meant to be called directly. Use
  271. * DB_result::fetchInto() instead. It can't be declared "protected"
  272. * because DB_result is a separate object.
  273. *
  274. * PHP's mSQL extension did weird things with NULL values prior to PHP
  275. * 4.3.11 and 5.0.4. Make sure your version of PHP meets or exceeds
  276. * those versions.
  277. *
  278. * @param resource $result the query result resource
  279. * @param array $arr the referenced array to put the data in
  280. * @param int $fetchmode how the resulting array should be indexed
  281. * @param int $rownum the row number to fetch (0 = first row)
  282. *
  283. * @return mixed DB_OK on success, NULL when the end of a result set is
  284. * reached or on failure
  285. *
  286. * @see DB_result::fetchInto()
  287. */
  288. public function fetchInto($result, &$arr, $fetchmode, $rownum = null)
  289. {
  290. if ($rownum !== null) {
  291. if (!@msql_data_seek($result, $rownum)) {
  292. return null;
  293. }
  294. }
  295. if ($fetchmode & DB_FETCHMODE_ASSOC) {
  296. $arr = @msql_fetch_array($result, MSQL_ASSOC);
  297. if ($this->options['portability'] & DB_PORTABILITY_LOWERCASE && $arr) {
  298. $arr = array_change_key_case($arr, CASE_LOWER);
  299. }
  300. } else {
  301. $arr = @msql_fetch_row($result);
  302. }
  303. if (!$arr) {
  304. return null;
  305. }
  306. if ($this->options['portability'] & DB_PORTABILITY_RTRIM) {
  307. $this->_rtrimArrayValues($arr);
  308. }
  309. if ($this->options['portability'] & DB_PORTABILITY_NULL_TO_EMPTY) {
  310. $this->_convertNullArrayValuesToEmpty($arr);
  311. }
  312. return DB_OK;
  313. }
  314. // }}}
  315. // {{{ freeResult()
  316. /**
  317. * Deletes the result set and frees the memory occupied by the result set
  318. *
  319. * This method is not meant to be called directly. Use
  320. * DB_result::free() instead. It can't be declared "protected"
  321. * because DB_result is a separate object.
  322. *
  323. * @param resource $result PHP's query result resource
  324. *
  325. * @return bool TRUE on success, FALSE if $result is invalid
  326. *
  327. * @see DB_result::free()
  328. */
  329. public function freeResult($result)
  330. {
  331. return is_resource($result) ? msql_free_result($result) : false;
  332. }
  333. // }}}
  334. // {{{ numCols()
  335. /**
  336. * Gets the number of columns in a result set
  337. *
  338. * This method is not meant to be called directly. Use
  339. * DB_result::numCols() instead. It can't be declared "protected"
  340. * because DB_result is a separate object.
  341. *
  342. * @param resource $result PHP's query result resource
  343. *
  344. * @return int the number of columns. A DB_Error object on failure.
  345. *
  346. * @see DB_result::numCols()
  347. */
  348. public function numCols($result)
  349. {
  350. $cols = @msql_num_fields($result);
  351. if (!$cols) {
  352. return $this->msqlRaiseError();
  353. }
  354. return $cols;
  355. }
  356. // }}}
  357. // {{{ numRows()
  358. /**
  359. * Gets the number of rows in a result set
  360. *
  361. * This method is not meant to be called directly. Use
  362. * DB_result::numRows() instead. It can't be declared "protected"
  363. * because DB_result is a separate object.
  364. *
  365. * @param resource $result PHP's query result resource
  366. *
  367. * @return int the number of rows. A DB_Error object on failure.
  368. *
  369. * @see DB_result::numRows()
  370. */
  371. public function numRows($result)
  372. {
  373. $rows = @msql_num_rows($result);
  374. if ($rows === false) {
  375. return $this->msqlRaiseError();
  376. }
  377. return $rows;
  378. }
  379. // }}}
  380. // {{{ affected()
  381. /**
  382. * Determines the number of rows affected by a data maniuplation query
  383. *
  384. * 0 is returned for queries that don't manipulate data.
  385. *
  386. * @return int the number of rows. A DB_Error object on failure.
  387. */
  388. public function affectedRows()
  389. {
  390. if (!$this->_result) {
  391. return 0;
  392. }
  393. return msql_affected_rows($this->_result);
  394. }
  395. // }}}
  396. // {{{ nextId()
  397. /**
  398. * Returns the next free id in a sequence
  399. *
  400. * @param string $seq_name name of the sequence
  401. * @param boolean $ondemand when true, the seqence is automatically
  402. * created if it does not exist
  403. *
  404. * @return int the next id number in the sequence.
  405. * A DB_Error object on failure.
  406. *
  407. * @see DB_common::nextID(), DB_common::getSequenceName(),
  408. * DB_msql::createSequence(), DB_msql::dropSequence()
  409. */
  410. public function nextId($seq_name, $ondemand = true)
  411. {
  412. $seqname = $this->getSequenceName($seq_name);
  413. $repeat = false;
  414. do {
  415. $this->pushErrorHandling(PEAR_ERROR_RETURN);
  416. $result = $this->query("SELECT _seq FROM ${seqname}");
  417. $this->popErrorHandling();
  418. if ($ondemand && DB::isError($result) &&
  419. $result->getCode() == DB_ERROR_NOSUCHTABLE) {
  420. $repeat = true;
  421. $this->pushErrorHandling(PEAR_ERROR_RETURN);
  422. $result = $this->createSequence($seq_name);
  423. $this->popErrorHandling();
  424. if (DB::isError($result)) {
  425. return $this->raiseError($result);
  426. }
  427. } else {
  428. $repeat = false;
  429. }
  430. } while ($repeat);
  431. if (DB::isError($result)) {
  432. return $this->raiseError($result);
  433. }
  434. $arr = $result->fetchRow(DB_FETCHMODE_ORDERED);
  435. $result->free();
  436. return $arr[0];
  437. }
  438. // }}}
  439. // {{{ createSequence()
  440. /**
  441. * Creates a new sequence
  442. *
  443. * Also creates a new table to associate the sequence with. Uses
  444. * a separate table to ensure portability with other drivers.
  445. *
  446. * @param string $seq_name name of the new sequence
  447. *
  448. * @return int DB_OK on success. A DB_Error object on failure.
  449. *
  450. * @see DB_common::createSequence(), DB_common::getSequenceName(),
  451. * DB_msql::nextID(), DB_msql::dropSequence()
  452. */
  453. public function createSequence($seq_name)
  454. {
  455. $seqname = $this->getSequenceName($seq_name);
  456. $res = $this->query('CREATE TABLE ' . $seqname
  457. . ' (id INTEGER NOT NULL)');
  458. if (DB::isError($res)) {
  459. return $res;
  460. }
  461. $res = $this->query("CREATE SEQUENCE ON ${seqname}");
  462. return $res;
  463. }
  464. // }}}
  465. // {{{ dropSequence()
  466. /**
  467. * Deletes a sequence
  468. *
  469. * @param string $seq_name name of the sequence to be deleted
  470. *
  471. * @return int DB_OK on success. A DB_Error object on failure.
  472. *
  473. * @see DB_common::dropSequence(), DB_common::getSequenceName(),
  474. * DB_msql::nextID(), DB_msql::createSequence()
  475. */
  476. public function dropSequence($seq_name)
  477. {
  478. return $this->query('DROP TABLE ' . $this->getSequenceName($seq_name));
  479. }
  480. // }}}
  481. // {{{ quoteIdentifier()
  482. /**
  483. * mSQL does not support delimited identifiers
  484. *
  485. * @param string $str the identifier name to be quoted
  486. *
  487. * @return object a DB_Error object
  488. *
  489. * @see DB_common::quoteIdentifier()
  490. * @since Method available since Release 1.7.0
  491. */
  492. public function quoteIdentifier($str)
  493. {
  494. return $this->raiseError(DB_ERROR_UNSUPPORTED);
  495. }
  496. // }}}
  497. // {{{ quoteFloat()
  498. /**
  499. * Formats a float value for use within a query in a locale-independent
  500. * manner.
  501. *
  502. * @param float the float value to be quoted.
  503. * @return string the quoted string.
  504. * @see DB_common::quoteSmart()
  505. * @since Method available since release 1.7.8.
  506. */
  507. public function quoteFloat($float)
  508. {
  509. return $this->escapeSimple(str_replace(',', '.', strval(floatval($float))));
  510. }
  511. // }}}
  512. // {{{ escapeSimple()
  513. /**
  514. * Escapes a string according to the current DBMS's standards
  515. *
  516. * @param string $str the string to be escaped
  517. *
  518. * @return string the escaped string
  519. *
  520. * @see DB_common::quoteSmart()
  521. * @since Method available since Release 1.7.0
  522. */
  523. public function escapeSimple($str)
  524. {
  525. return addslashes($str);
  526. }
  527. // }}}
  528. // {{{ msqlRaiseError()
  529. /**
  530. * Produces a DB_Error object regarding the current problem
  531. *
  532. * @param int $errno if the error is being manually raised pass a
  533. * DB_ERROR* constant here. If this isn't passed
  534. * the error information gathered from the DBMS.
  535. *
  536. * @return object the DB_Error object
  537. *
  538. * @see DB_common::raiseError(),
  539. * DB_msql::errorNative(), DB_msql::errorCode()
  540. */
  541. public function msqlRaiseError($errno = null)
  542. {
  543. $native = $this->errorNative();
  544. if ($errno === null) {
  545. $errno = $this->errorCode($native);
  546. }
  547. return $this->raiseError($errno, null, null, null, $native);
  548. }
  549. // }}}
  550. // {{{ errorNative()
  551. /**
  552. * Gets the DBMS' native error message produced by the last query
  553. *
  554. * @return string the DBMS' error message
  555. */
  556. public function errorNative()
  557. {
  558. return @msql_error();
  559. }
  560. // }}}
  561. // {{{ errorCode()
  562. /**
  563. * Determines PEAR::DB error code from the database's text error message
  564. *
  565. * @param string $errormsg the error message returned from the database
  566. *
  567. * @return integer the error number from a DB_ERROR* constant
  568. */
  569. public function errorCode($errormsg)
  570. {
  571. static $error_regexps;
  572. // PHP 5.2+ prepends the function name to $php_errormsg, so we need
  573. // this hack to work around it, per bug #9599.
  574. $errormsg = preg_replace('/^msql[a-z_]+\(\): /', '', $errormsg);
  575. if (!isset($error_regexps)) {
  576. $error_regexps = array(
  577. '/^Access to database denied/i'
  578. => DB_ERROR_ACCESS_VIOLATION,
  579. '/^Bad index name/i'
  580. => DB_ERROR_ALREADY_EXISTS,
  581. '/^Bad order field/i'
  582. => DB_ERROR_SYNTAX,
  583. '/^Bad type for comparison/i'
  584. => DB_ERROR_SYNTAX,
  585. '/^Can\'t perform LIKE on/i'
  586. => DB_ERROR_SYNTAX,
  587. '/^Can\'t use TEXT fields in LIKE comparison/i'
  588. => DB_ERROR_SYNTAX,
  589. '/^Couldn\'t create temporary table/i'
  590. => DB_ERROR_CANNOT_CREATE,
  591. '/^Error creating table file/i'
  592. => DB_ERROR_CANNOT_CREATE,
  593. '/^Field .* cannot be null$/i'
  594. => DB_ERROR_CONSTRAINT_NOT_NULL,
  595. '/^Index (field|condition) .* cannot be null$/i'
  596. => DB_ERROR_SYNTAX,
  597. '/^Invalid date format/i'
  598. => DB_ERROR_INVALID_DATE,
  599. '/^Invalid time format/i'
  600. => DB_ERROR_INVALID,
  601. '/^Literal value for .* is wrong type$/i'
  602. => DB_ERROR_INVALID_NUMBER,
  603. '/^No Database Selected/i'
  604. => DB_ERROR_NODBSELECTED,
  605. '/^No value specified for field/i'
  606. => DB_ERROR_VALUE_COUNT_ON_ROW,
  607. '/^Non unique value for unique index/i'
  608. => DB_ERROR_CONSTRAINT,
  609. '/^Out of memory for temporary table/i'
  610. => DB_ERROR_CANNOT_CREATE,
  611. '/^Permission denied/i'
  612. => DB_ERROR_ACCESS_VIOLATION,
  613. '/^Reference to un-selected table/i'
  614. => DB_ERROR_SYNTAX,
  615. '/^syntax error/i'
  616. => DB_ERROR_SYNTAX,
  617. '/^Table .* exists$/i'
  618. => DB_ERROR_ALREADY_EXISTS,
  619. '/^Unknown database/i'
  620. => DB_ERROR_NOSUCHDB,
  621. '/^Unknown field/i'
  622. => DB_ERROR_NOSUCHFIELD,
  623. '/^Unknown (index|system variable)/i'
  624. => DB_ERROR_NOT_FOUND,
  625. '/^Unknown table/i'
  626. => DB_ERROR_NOSUCHTABLE,
  627. '/^Unqualified field/i'
  628. => DB_ERROR_SYNTAX,
  629. );
  630. }
  631. foreach ($error_regexps as $regexp => $code) {
  632. if (preg_match($regexp, $errormsg)) {
  633. return $code;
  634. }
  635. }
  636. return DB_ERROR;
  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 an associative array with the information requested.
  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 the array containing the list of objects requested
  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. */