mysqli.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PHP versions 4 and 5 |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1998-2007 Manuel Lemos, Tomas V.V.Cox, |
  6. // | Stig. S. Bakken, Lukas Smith |
  7. // | All rights reserved. |
  8. // +----------------------------------------------------------------------+
  9. // | MDB2 is a merge of PEAR DB and Metabases that provides a unified DB |
  10. // | API as well as database abstraction for PHP applications. |
  11. // | This LICENSE is in the BSD license style. |
  12. // | |
  13. // | Redistribution and use in source and binary forms, with or without |
  14. // | modification, are permitted provided that the following conditions |
  15. // | are met: |
  16. // | |
  17. // | Redistributions of source code must retain the above copyright |
  18. // | notice, this list of conditions and the following disclaimer. |
  19. // | |
  20. // | Redistributions in binary form must reproduce the above copyright |
  21. // | notice, this list of conditions and the following disclaimer in the |
  22. // | documentation and/or other materials provided with the distribution. |
  23. // | |
  24. // | Neither the name of Manuel Lemos, Tomas V.V.Cox, Stig. S. Bakken, |
  25. // | Lukas Smith nor the names of his contributors may be used to endorse |
  26. // | or promote products derived from this software without specific prior|
  27. // | written permission. |
  28. // | |
  29. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
  30. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
  31. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
  32. // | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
  33. // | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
  34. // | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
  35. // | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
  36. // | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
  37. // | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
  38. // | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
  39. // | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
  40. // | POSSIBILITY OF SUCH DAMAGE. |
  41. // +----------------------------------------------------------------------+
  42. // | Author: Lukas Smith <smith@pooteeweet.org> |
  43. // +----------------------------------------------------------------------+
  44. //
  45. // $Id$
  46. //
  47. require_once 'MDB2/Driver/Reverse/Common.php';
  48. /**
  49. * MDB2 MySQLi driver for the schema reverse engineering module
  50. *
  51. * @package MDB2
  52. * @category Database
  53. * @author Lukas Smith <smith@pooteeweet.org>
  54. * @author Lorenzo Alberton <l.alberton@quipo.it>
  55. */
  56. class MDB2_Driver_Reverse_mysqli extends MDB2_Driver_Reverse_Common
  57. {
  58. /**
  59. * Array for converting MYSQLI_*_FLAG constants to text values
  60. * @var array
  61. * @access public
  62. */
  63. var $flags = array(
  64. MYSQLI_NOT_NULL_FLAG => 'not_null',
  65. MYSQLI_PRI_KEY_FLAG => 'primary_key',
  66. MYSQLI_UNIQUE_KEY_FLAG => 'unique_key',
  67. MYSQLI_MULTIPLE_KEY_FLAG => 'multiple_key',
  68. MYSQLI_BLOB_FLAG => 'blob',
  69. MYSQLI_UNSIGNED_FLAG => 'unsigned',
  70. MYSQLI_ZEROFILL_FLAG => 'zerofill',
  71. MYSQLI_AUTO_INCREMENT_FLAG => 'auto_increment',
  72. MYSQLI_TIMESTAMP_FLAG => 'timestamp',
  73. MYSQLI_SET_FLAG => 'set',
  74. // MYSQLI_NUM_FLAG => 'numeric', // unnecessary
  75. // MYSQLI_PART_KEY_FLAG => 'multiple_key', // duplicatvie
  76. MYSQLI_GROUP_FLAG => 'group_by'
  77. );
  78. /**
  79. * Array for converting MYSQLI_TYPE_* constants to text values
  80. * @var array
  81. * @access public
  82. */
  83. var $types = array(
  84. MYSQLI_TYPE_DECIMAL => 'decimal',
  85. 246 => 'decimal',
  86. MYSQLI_TYPE_TINY => 'tinyint',
  87. MYSQLI_TYPE_SHORT => 'int',
  88. MYSQLI_TYPE_LONG => 'int',
  89. MYSQLI_TYPE_FLOAT => 'float',
  90. MYSQLI_TYPE_DOUBLE => 'double',
  91. // MYSQLI_TYPE_NULL => 'DEFAULT NULL', // let flags handle it
  92. MYSQLI_TYPE_TIMESTAMP => 'timestamp',
  93. MYSQLI_TYPE_LONGLONG => 'bigint',
  94. MYSQLI_TYPE_INT24 => 'mediumint',
  95. MYSQLI_TYPE_DATE => 'date',
  96. MYSQLI_TYPE_TIME => 'time',
  97. MYSQLI_TYPE_DATETIME => 'datetime',
  98. MYSQLI_TYPE_YEAR => 'year',
  99. MYSQLI_TYPE_NEWDATE => 'date',
  100. MYSQLI_TYPE_ENUM => 'enum',
  101. MYSQLI_TYPE_SET => 'set',
  102. MYSQLI_TYPE_TINY_BLOB => 'tinyblob',
  103. MYSQLI_TYPE_MEDIUM_BLOB => 'mediumblob',
  104. MYSQLI_TYPE_LONG_BLOB => 'longblob',
  105. MYSQLI_TYPE_BLOB => 'blob',
  106. MYSQLI_TYPE_VAR_STRING => 'varchar',
  107. MYSQLI_TYPE_STRING => 'char',
  108. MYSQLI_TYPE_GEOMETRY => 'geometry',
  109. );
  110. // {{{ getTableFieldDefinition()
  111. /**
  112. * Get the structure of a field into an array
  113. *
  114. * @param string $table_name name of table that should be used in method
  115. * @param string $field_name name of field that should be used in method
  116. * @return mixed data array on success, a MDB2 error on failure
  117. * @access public
  118. */
  119. function getTableFieldDefinition($table_name, $field_name)
  120. {
  121. $db = $this->getDBInstance();
  122. if (MDB2::isError($db)) {
  123. return $db;
  124. }
  125. $result = $db->loadModule('Datatype', null, true);
  126. if (MDB2::isError($result)) {
  127. return $result;
  128. }
  129. list($schema, $table) = $this->splitTableSchema($table_name);
  130. $table = $db->quoteIdentifier($table, true);
  131. $query = "SHOW FULL COLUMNS FROM $table LIKE ".$db->quote($field_name);
  132. $columns = $db->queryAll($query, null, MDB2_FETCHMODE_ASSOC);
  133. if (MDB2::isError($columns)) {
  134. return $columns;
  135. }
  136. foreach ($columns as $column) {
  137. $column = array_change_key_case($column, CASE_LOWER);
  138. $column['name'] = $column['field'];
  139. unset($column['field']);
  140. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  141. if ($db->options['field_case'] == CASE_LOWER) {
  142. $column['name'] = strtolower($column['name']);
  143. } else {
  144. $column['name'] = strtoupper($column['name']);
  145. }
  146. } else {
  147. $column = array_change_key_case($column, $db->options['field_case']);
  148. }
  149. if ($field_name == $column['name']) {
  150. $mapped_datatype = $db->datatype->mapNativeDatatype($column);
  151. if (MDB2::isError($mapped_datatype)) {
  152. return $mapped_datatype;
  153. }
  154. list($types, $length, $unsigned, $fixed) = $mapped_datatype;
  155. $notnull = false;
  156. if (empty($column['null']) || $column['null'] !== 'YES') {
  157. $notnull = true;
  158. }
  159. $default = false;
  160. if (array_key_exists('default', $column)) {
  161. $default = $column['default'];
  162. if ((null === $default) && $notnull) {
  163. $default = '';
  164. }
  165. }
  166. $definition[0] = array(
  167. 'notnull' => $notnull,
  168. 'nativetype' => preg_replace('/^([a-z]+)[^a-z].*/i', '\\1', $column['type'])
  169. );
  170. $autoincrement = false;
  171. if (!empty($column['extra'])) {
  172. if ($column['extra'] == 'auto_increment') {
  173. $autoincrement = true;
  174. } else {
  175. $definition[0]['extra'] = $column['extra'];
  176. }
  177. }
  178. $collate = null;
  179. if (!empty($column['collation'])) {
  180. $collate = $column['collation'];
  181. $charset = preg_replace('/(.+?)(_.+)?/', '$1', $collate);
  182. }
  183. if (null !== $length) {
  184. $definition[0]['length'] = $length;
  185. }
  186. if (null !== $unsigned) {
  187. $definition[0]['unsigned'] = $unsigned;
  188. }
  189. if (null !== $fixed) {
  190. $definition[0]['fixed'] = $fixed;
  191. }
  192. if ($default !== false) {
  193. $definition[0]['default'] = $default;
  194. }
  195. if ($autoincrement !== false) {
  196. $definition[0]['autoincrement'] = $autoincrement;
  197. }
  198. if (null !== $collate) {
  199. $definition[0]['collate'] = $collate;
  200. $definition[0]['charset'] = $charset;
  201. }
  202. foreach ($types as $key => $type) {
  203. $definition[$key] = $definition[0];
  204. if ($type == 'clob' || $type == 'blob') {
  205. unset($definition[$key]['default']);
  206. } elseif ($type == 'timestamp' && $notnull && empty($definition[$key]['default'])) {
  207. $definition[$key]['default'] = '0000-00-00 00:00:00';
  208. }
  209. $definition[$key]['type'] = $type;
  210. $definition[$key]['mdb2type'] = $type;
  211. }
  212. return $definition;
  213. }
  214. }
  215. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  216. 'it was not specified an existing table column', __FUNCTION__);
  217. }
  218. // }}}
  219. // {{{ getTableIndexDefinition()
  220. /**
  221. * Get the structure of an index into an array
  222. *
  223. * @param string $table_name name of table that should be used in method
  224. * @param string $index_name name of index that should be used in method
  225. * @return mixed data array on success, a MDB2 error on failure
  226. * @access public
  227. */
  228. function getTableIndexDefinition($table_name, $index_name)
  229. {
  230. $db = $this->getDBInstance();
  231. if (MDB2::isError($db)) {
  232. return $db;
  233. }
  234. list($schema, $table) = $this->splitTableSchema($table_name);
  235. $table = $db->quoteIdentifier($table, true);
  236. $query = "SHOW INDEX FROM $table /*!50002 WHERE Key_name = %s */";
  237. $index_name_mdb2 = $db->getIndexName($index_name);
  238. $result = $db->queryRow(sprintf($query, $db->quote($index_name_mdb2)));
  239. if (!MDB2::isError($result) && (null !== $result)) {
  240. // apply 'idxname_format' only if the query succeeded, otherwise
  241. // fallback to the given $index_name, without transformation
  242. $index_name = $index_name_mdb2;
  243. }
  244. $result = $db->query(sprintf($query, $db->quote($index_name)));
  245. if (MDB2::isError($result)) {
  246. return $result;
  247. }
  248. $colpos = 1;
  249. $definition = array();
  250. while (is_array($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  251. $row = array_change_key_case($row, CASE_LOWER);
  252. $key_name = $row['key_name'];
  253. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  254. if ($db->options['field_case'] == CASE_LOWER) {
  255. $key_name = strtolower($key_name);
  256. } else {
  257. $key_name = strtoupper($key_name);
  258. }
  259. }
  260. if ($index_name == $key_name) {
  261. if (!$row['non_unique']) {
  262. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  263. $index_name . ' is not an existing table index', __FUNCTION__);
  264. }
  265. $column_name = $row['column_name'];
  266. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  267. if ($db->options['field_case'] == CASE_LOWER) {
  268. $column_name = strtolower($column_name);
  269. } else {
  270. $column_name = strtoupper($column_name);
  271. }
  272. }
  273. $definition['fields'][$column_name] = array(
  274. 'position' => $colpos++
  275. );
  276. if (!empty($row['collation'])) {
  277. $definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'A'
  278. ? 'ascending' : 'descending');
  279. }
  280. }
  281. }
  282. $result->free();
  283. if (empty($definition['fields'])) {
  284. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  285. $index_name . ' is not an existing table index', __FUNCTION__);
  286. }
  287. return $definition;
  288. }
  289. // }}}
  290. // {{{ getTableConstraintDefinition()
  291. /**
  292. * Get the structure of a constraint into an array
  293. *
  294. * @param string $table_name name of table that should be used in method
  295. * @param string $constraint_name name of constraint that should be used in method
  296. * @return mixed data array on success, a MDB2 error on failure
  297. * @access public
  298. */
  299. function getTableConstraintDefinition($table_name, $constraint_name)
  300. {
  301. $db = $this->getDBInstance();
  302. if (MDB2::isError($db)) {
  303. return $db;
  304. }
  305. list($schema, $table) = $this->splitTableSchema($table_name);
  306. $constraint_name_original = $constraint_name;
  307. $table = $db->quoteIdentifier($table, true);
  308. $query = "SHOW INDEX FROM $table /*!50002 WHERE Key_name = %s */";
  309. if (strtolower($constraint_name) != 'primary') {
  310. $constraint_name_mdb2 = $db->getIndexName($constraint_name);
  311. $result = $db->queryRow(sprintf($query, $db->quote($constraint_name_mdb2)));
  312. if (!MDB2::isError($result) && (null !== $result)) {
  313. // apply 'idxname_format' only if the query succeeded, otherwise
  314. // fallback to the given $index_name, without transformation
  315. $constraint_name = $constraint_name_mdb2;
  316. }
  317. }
  318. $result = $db->query(sprintf($query, $db->quote($constraint_name)));
  319. if (MDB2::isError($result)) {
  320. return $result;
  321. }
  322. $colpos = 1;
  323. //default values, eventually overridden
  324. $definition = array(
  325. 'primary' => false,
  326. 'unique' => false,
  327. 'foreign' => false,
  328. 'check' => false,
  329. 'fields' => array(),
  330. 'references' => array(
  331. 'table' => '',
  332. 'fields' => array(),
  333. ),
  334. 'onupdate' => '',
  335. 'ondelete' => '',
  336. 'match' => '',
  337. 'deferrable' => false,
  338. 'initiallydeferred' => false,
  339. );
  340. while (is_array($row = $result->fetchRow(MDB2_FETCHMODE_ASSOC))) {
  341. $row = array_change_key_case($row, CASE_LOWER);
  342. $key_name = $row['key_name'];
  343. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  344. if ($db->options['field_case'] == CASE_LOWER) {
  345. $key_name = strtolower($key_name);
  346. } else {
  347. $key_name = strtoupper($key_name);
  348. }
  349. }
  350. if ($constraint_name == $key_name) {
  351. if ($row['non_unique']) {
  352. //FOREIGN KEY?
  353. return $this->_getTableFKConstraintDefinition($table, $constraint_name_original, $definition);
  354. }
  355. if ($row['key_name'] == 'PRIMARY') {
  356. $definition['primary'] = true;
  357. } elseif (!$row['non_unique']) {
  358. $definition['unique'] = true;
  359. }
  360. $column_name = $row['column_name'];
  361. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  362. if ($db->options['field_case'] == CASE_LOWER) {
  363. $column_name = strtolower($column_name);
  364. } else {
  365. $column_name = strtoupper($column_name);
  366. }
  367. }
  368. $definition['fields'][$column_name] = array(
  369. 'position' => $colpos++
  370. );
  371. if (!empty($row['collation'])) {
  372. $definition['fields'][$column_name]['sorting'] = ($row['collation'] == 'A'
  373. ? 'ascending' : 'descending');
  374. }
  375. }
  376. }
  377. $result->free();
  378. if (empty($definition['fields'])) {
  379. return $this->_getTableFKConstraintDefinition($table, $constraint_name_original, $definition);
  380. }
  381. return $definition;
  382. }
  383. // }}}
  384. // {{{ _getTableFKConstraintDefinition()
  385. /**
  386. * Get the FK definition from the CREATE TABLE statement
  387. *
  388. * @param string $table table name
  389. * @param string $constraint_name constraint name
  390. * @param array $definition default values for constraint definition
  391. *
  392. * @return array|PEAR_Error
  393. * @access private
  394. */
  395. function _getTableFKConstraintDefinition($table, $constraint_name, $definition)
  396. {
  397. $db = $this->getDBInstance();
  398. if (MDB2::isError($db)) {
  399. return $db;
  400. }
  401. //Use INFORMATION_SCHEMA instead?
  402. //SELECT *
  403. // FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
  404. // WHERE CONSTRAINT_SCHEMA = '$dbname'
  405. // AND TABLE_NAME = '$table'
  406. // AND CONSTRAINT_NAME = '$constraint_name';
  407. $query = 'SHOW CREATE TABLE '. $db->escape($table);
  408. $constraint = $db->queryOne($query, 'text', 1);
  409. if (!MDB2::isError($constraint) && !empty($constraint)) {
  410. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  411. if ($db->options['field_case'] == CASE_LOWER) {
  412. $constraint = strtolower($constraint);
  413. } else {
  414. $constraint = strtoupper($constraint);
  415. }
  416. }
  417. $constraint_name_original = $constraint_name;
  418. $constraint_name = $db->getIndexName($constraint_name);
  419. $pattern = '/\bCONSTRAINT\s+'.$constraint_name.'\s+FOREIGN KEY\s+\(([^\)]+)\) \bREFERENCES\b ([^\s]+) \(([^\)]+)\)(?: ON DELETE ([^\s]+))?(?: ON UPDATE ([^\s]+))?/i';
  420. if (!preg_match($pattern, str_replace('`', '', $constraint), $matches)) {
  421. //fallback to original constraint name
  422. $pattern = '/\bCONSTRAINT\s+'.$constraint_name_original.'\s+FOREIGN KEY\s+\(([^\)]+)\) \bREFERENCES\b ([^\s]+) \(([^\)]+)\)(?: ON DELETE ([^\s]+))?(?: ON UPDATE ([^\s]+))?/i';
  423. }
  424. if (preg_match($pattern, str_replace('`', '', $constraint), $matches)) {
  425. $definition['foreign'] = true;
  426. $column_names = explode(',', $matches[1]);
  427. $referenced_cols = explode(',', $matches[3]);
  428. $definition['references'] = array(
  429. 'table' => $matches[2],
  430. 'fields' => array(),
  431. );
  432. $colpos = 1;
  433. foreach ($column_names as $column_name) {
  434. $definition['fields'][trim($column_name)] = array(
  435. 'position' => $colpos++
  436. );
  437. }
  438. $colpos = 1;
  439. foreach ($referenced_cols as $column_name) {
  440. $definition['references']['fields'][trim($column_name)] = array(
  441. 'position' => $colpos++
  442. );
  443. }
  444. $definition['ondelete'] = empty($matches[4]) ? 'RESTRICT' : strtoupper($matches[4]);
  445. $definition['onupdate'] = empty($matches[5]) ? 'RESTRICT' : strtoupper($matches[5]);
  446. $definition['match'] = 'SIMPLE';
  447. return $definition;
  448. }
  449. }
  450. return $db->raiseError(MDB2_ERROR_NOT_FOUND, null, null,
  451. $constraint_name . ' is not an existing table constraint', __FUNCTION__);
  452. }
  453. // }}}
  454. // {{{ getTriggerDefinition()
  455. /**
  456. * Get the structure of a trigger into an array
  457. *
  458. * EXPERIMENTAL
  459. *
  460. * WARNING: this function is experimental and may change the returned value
  461. * at any time until labelled as non-experimental
  462. *
  463. * @param string $trigger name of trigger that should be used in method
  464. * @return mixed data array on success, a MDB2 error on failure
  465. * @access public
  466. */
  467. function getTriggerDefinition($trigger)
  468. {
  469. $db = $this->getDBInstance();
  470. if (MDB2::isError($db)) {
  471. return $db;
  472. }
  473. $query = 'SELECT trigger_name,
  474. event_object_table AS table_name,
  475. action_statement AS trigger_body,
  476. action_timing AS trigger_type,
  477. event_manipulation AS trigger_event
  478. FROM information_schema.triggers
  479. WHERE trigger_name = '. $db->quote($trigger, 'text');
  480. $types = array(
  481. 'trigger_name' => 'text',
  482. 'table_name' => 'text',
  483. 'trigger_body' => 'text',
  484. 'trigger_type' => 'text',
  485. 'trigger_event' => 'text',
  486. );
  487. $def = $db->queryRow($query, $types, MDB2_FETCHMODE_ASSOC);
  488. if (MDB2::isError($def)) {
  489. return $def;
  490. }
  491. $def['trigger_comment'] = '';
  492. $def['trigger_enabled'] = true;
  493. return $def;
  494. }
  495. // }}}
  496. // {{{ tableInfo()
  497. /**
  498. * Returns information about a table or a result set
  499. *
  500. * @param object|string $result MDB2_result object from a query or a
  501. * string containing the name of a table.
  502. * While this also accepts a query result
  503. * resource identifier, this behavior is
  504. * deprecated.
  505. * @param int $mode a valid tableInfo mode
  506. *
  507. * @return array an associative array with the information requested.
  508. * A MDB2_Error object on failure.
  509. *
  510. * @see MDB2_Driver_Common::setOption()
  511. */
  512. function tableInfo($result, $mode = null)
  513. {
  514. if (is_string($result)) {
  515. return parent::tableInfo($result, $mode);
  516. }
  517. $db = $this->getDBInstance();
  518. if (MDB2::isError($db)) {
  519. return $db;
  520. }
  521. $resource = MDB2::isResultCommon($result) ? $result->getResource() : $result;
  522. if (!is_object($resource)) {
  523. return $db->raiseError(MDB2_ERROR_NEED_MORE_DATA, null, null,
  524. 'Could not generate result resource', __FUNCTION__);
  525. }
  526. if ($db->options['portability'] & MDB2_PORTABILITY_FIX_CASE) {
  527. if ($db->options['field_case'] == CASE_LOWER) {
  528. $case_func = 'strtolower';
  529. } else {
  530. $case_func = 'strtoupper';
  531. }
  532. } else {
  533. $case_func = 'strval';
  534. }
  535. $count = @mysqli_num_fields($resource);
  536. $res = array();
  537. if ($mode) {
  538. $res['num_fields'] = $count;
  539. }
  540. $db->loadModule('Datatype', null, true);
  541. for ($i = 0; $i < $count; $i++) {
  542. $tmp = @mysqli_fetch_field($resource);
  543. $flags = '';
  544. foreach ($this->flags as $const => $means) {
  545. if ($tmp->flags & $const) {
  546. $flags.= $means . ' ';
  547. }
  548. }
  549. if ($tmp->def) {
  550. $flags.= 'default_' . rawurlencode($tmp->def);
  551. }
  552. $flags = trim($flags);
  553. $res[$i] = array(
  554. 'table' => $case_func($tmp->table),
  555. 'name' => $case_func($tmp->name),
  556. 'type' => isset($this->types[$tmp->type])
  557. ? $this->types[$tmp->type] : 'unknown',
  558. // http://bugs.php.net/?id=36579
  559. 'length' => $tmp->length,
  560. 'flags' => $flags,
  561. );
  562. $mdb2type_info = $db->datatype->mapNativeDatatype($res[$i]);
  563. if (MDB2::isError($mdb2type_info)) {
  564. return $mdb2type_info;
  565. }
  566. $res[$i]['mdb2type'] = $mdb2type_info[0][0];
  567. if ($mode & MDB2_TABLEINFO_ORDER) {
  568. $res['order'][$res[$i]['name']] = $i;
  569. }
  570. if ($mode & MDB2_TABLEINFO_ORDERTABLE) {
  571. $res['ordertable'][$res[$i]['table']][$res[$i]['name']] = $i;
  572. }
  573. }
  574. return $res;
  575. }
  576. }
  577. ?>