db_datadict.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. *
  5. * @package PhpMyAdmin
  6. */
  7. /**
  8. * Gets the variables sent or posted to this script, then displays headers
  9. */
  10. require_once 'libraries/common.inc.php';
  11. if (! isset($selected_tbl)) {
  12. include 'libraries/db_common.inc.php';
  13. include 'libraries/db_info.inc.php';
  14. }
  15. $response = PMA_Response::getInstance();
  16. $header = $response->getHeader();
  17. $header->enablePrintView();
  18. /**
  19. * Gets the relations settings
  20. */
  21. $cfgRelation = PMA_getRelationsParam();
  22. require_once 'libraries/transformations.lib.php';
  23. require_once 'libraries/Index.class.php';
  24. /**
  25. * Check parameters
  26. */
  27. PMA_Util::checkParameters(array('db'));
  28. /**
  29. * Defines the url to return to in case of error in a sql statement
  30. */
  31. if (strlen($table)) {
  32. $err_url = 'tbl_sql.php?' . PMA_generate_common_url($db, $table);
  33. } else {
  34. $err_url = 'db_sql.php?' . PMA_generate_common_url($db);
  35. }
  36. if ($cfgRelation['commwork']) {
  37. $comment = PMA_getDbComment($db);
  38. /**
  39. * Displays DB comment
  40. */
  41. if ($comment) {
  42. echo '<p>' . __('Database comment: ')
  43. . '<i>' . htmlspecialchars($comment) . '</i></p>';
  44. } // end if
  45. }
  46. /**
  47. * Selects the database and gets tables names
  48. */
  49. PMA_DBI_select_db($db);
  50. $tables = PMA_DBI_get_tables($db);
  51. $count = 0;
  52. foreach ($tables as $table) {
  53. $comments = PMA_getComments($db, $table);
  54. echo '<div>' . "\n";
  55. echo '<h2>' . htmlspecialchars($table) . '</h2>' . "\n";
  56. /**
  57. * Gets table informations
  58. */
  59. $show_comment = PMA_Table::sGetStatusInfo($db, $table, 'TABLE_COMMENT');
  60. /**
  61. * Gets table keys and retains them
  62. */
  63. PMA_DBI_select_db($db);
  64. $indexes = PMA_DBI_get_table_indexes($db, $table);
  65. $primary = '';
  66. $indexes = array();
  67. $lastIndex = '';
  68. $indexes_info = array();
  69. $indexes_data = array();
  70. $pk_array = array(); // will be use to emphasis prim. keys in the table
  71. // view
  72. foreach ($indexes as $row) {
  73. // Backups the list of primary keys
  74. if ($row['Key_name'] == 'PRIMARY') {
  75. $primary .= $row['Column_name'] . ', ';
  76. $pk_array[$row['Column_name']] = 1;
  77. }
  78. // Retains keys informations
  79. if ($row['Key_name'] != $lastIndex) {
  80. $indexes[] = $row['Key_name'];
  81. $lastIndex = $row['Key_name'];
  82. }
  83. $indexes_info[$row['Key_name']]['Sequences'][] = $row['Seq_in_index'];
  84. $indexes_info[$row['Key_name']]['Non_unique'] = $row['Non_unique'];
  85. if (isset($row['Cardinality'])) {
  86. $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality'];
  87. }
  88. // I don't know what does following column mean....
  89. // $indexes_info[$row['Key_name']]['Packed'] = $row['Packed'];
  90. $indexes_info[$row['Key_name']]['Comment'] = $row['Comment'];
  91. $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name'] = $row['Column_name'];
  92. if (isset($row['Sub_part'])) {
  93. $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part'];
  94. }
  95. } // end while
  96. /**
  97. * Gets columns properties
  98. */
  99. $columns = PMA_DBI_get_columns($db, $table);
  100. $fields_cnt = count($columns);
  101. if (PMA_MYSQL_INT_VERSION < 50025) {
  102. // We need this to correctly learn if a TIMESTAMP is NOT NULL, since
  103. // SHOW FULL COLUMNS or INFORMATION_SCHEMA incorrectly says NULL
  104. // and SHOW CREATE TABLE says NOT NULL
  105. // http://bugs.mysql.com/20910.
  106. $show_create_table_query = 'SHOW CREATE TABLE '
  107. . PMA_Util::backquote($db) . '.'
  108. . PMA_Util::backquote($table);
  109. $show_create_table = PMA_DBI_fetch_value(
  110. $show_create_table_query, 0, 1
  111. );
  112. $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
  113. }
  114. // Check if we can use Relations
  115. if (!empty($cfgRelation['relation'])) {
  116. // Find which tables are related with the current one and write it in
  117. // an array
  118. $res_rel = PMA_getForeigners($db, $table);
  119. if (count($res_rel) > 0) {
  120. $have_rel = true;
  121. } else {
  122. $have_rel = false;
  123. }
  124. } else {
  125. $have_rel = false;
  126. } // end if
  127. /**
  128. * Displays the comments of the table if MySQL >= 3.23
  129. */
  130. if (!empty($show_comment)) {
  131. echo __('Table comments') . ': ' . htmlspecialchars($show_comment) . '<br /><br />';
  132. }
  133. /**
  134. * Displays the table structure
  135. */
  136. ?>
  137. <table width="100%" class="print">
  138. <tr><th width="50"><?php echo __('Column'); ?></th>
  139. <th width="80"><?php echo __('Type'); ?></th>
  140. <?php /* <th width="50"><?php echo __('Attributes'); ?></th>*/ ?>
  141. <th width="40"><?php echo __('Null'); ?></th>
  142. <th width="70"><?php echo __('Default'); ?></th>
  143. <?php /* <th width="50"><?php echo __('Extra'); ?></th>*/ ?>
  144. <?php
  145. if ($have_rel) {
  146. echo ' <th>' . __('Links to') . '</th>' . "\n";
  147. }
  148. echo ' <th>' . __('Comments') . '</th>' . "\n";
  149. if ($cfgRelation['mimework']) {
  150. echo ' <th>MIME</th>' . "\n";
  151. }
  152. ?>
  153. </tr>
  154. <?php
  155. $odd_row = true;
  156. foreach ($columns as $row) {
  157. if ($row['Null'] == '') {
  158. $row['Null'] = 'NO';
  159. }
  160. $extracted_columnspec
  161. = PMA_Util::extractColumnSpec($row['Type']);
  162. // reformat mysql query output
  163. // set or enum types: slashes single quotes inside options
  164. if ('set' == $extracted_columnspec['type'] || 'enum' == $extracted_columnspec['type']) {
  165. $type_nowrap = '';
  166. } else {
  167. $type_nowrap = ' class="nowrap"';
  168. }
  169. $type = htmlspecialchars($extracted_columnspec['print_type']);
  170. $attribute = $extracted_columnspec['attribute'];
  171. if (! isset($row['Default'])) {
  172. if ($row['Null'] != 'NO') {
  173. $row['Default'] = '<i>NULL</i>';
  174. }
  175. } else {
  176. $row['Default'] = htmlspecialchars($row['Default']);
  177. }
  178. $field_name = $row['Field'];
  179. if (PMA_MYSQL_INT_VERSION < 50025
  180. && ! empty($analyzed_sql[0]['create_table_fields'][$field_name]['type'])
  181. && $analyzed_sql[0]['create_table_fields'][$field_name]['type'] == 'TIMESTAMP'
  182. && $analyzed_sql[0]['create_table_fields'][$field_name]['timestamp_not_null']
  183. ) {
  184. // here, we have a TIMESTAMP that SHOW FULL COLUMNS reports as having the
  185. // NULL attribute, but SHOW CREATE TABLE says the contrary. Believe
  186. // the latter.
  187. /**
  188. * @todo merge this logic with the one in tbl_structure.php
  189. * or move it in a function similar to PMA_DBI_get_columns_full()
  190. * but based on SHOW CREATE TABLE because information_schema
  191. * cannot be trusted in this case (MySQL bug)
  192. */
  193. $row['Null'] = 'NO';
  194. }
  195. ?>
  196. <tr class="<?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
  197. <td class="nowrap">
  198. <?php
  199. if (isset($pk_array[$row['Field']])) {
  200. echo '<u>' . htmlspecialchars($field_name) . '</u>';
  201. } else {
  202. echo htmlspecialchars($field_name);
  203. }
  204. ?>
  205. </td>
  206. <td<?php echo $type_nowrap; ?> lang="en" dir="ltr"><?php echo $type; ?></td>
  207. <?php /* <td<?php echo $type_nowrap; ?>><?php echo $attribute; ?></td>*/ ?>
  208. <td><?php echo (($row['Null'] == 'NO') ? __('No') : __('Yes')); ?></td>
  209. <td class="nowrap"><?php
  210. if (isset($row['Default'])) {
  211. echo $row['Default'];
  212. }
  213. ?></td>
  214. <?php /* <td<?php echo $type_nowrap; ?>><?php echo $row['Extra']; ?></td>*/ ?>
  215. <?php
  216. if ($have_rel) {
  217. echo ' <td>';
  218. if (isset($res_rel[$field_name])) {
  219. echo htmlspecialchars($res_rel[$field_name]['foreign_table'] . ' -> ' . $res_rel[$field_name]['foreign_field']);
  220. }
  221. echo '</td>' . "\n";
  222. }
  223. echo ' <td>';
  224. if (isset($comments[$field_name])) {
  225. echo htmlspecialchars($comments[$field_name]);
  226. }
  227. echo '</td>' . "\n";
  228. if ($cfgRelation['mimework']) {
  229. $mime_map = PMA_getMIME($db, $table, true);
  230. echo ' <td>';
  231. if (isset($mime_map[$field_name])) {
  232. echo htmlspecialchars(str_replace('_', '/', $mime_map[$field_name]['mimetype']));
  233. }
  234. echo '</td>' . "\n";
  235. }
  236. ?>
  237. </tr>
  238. <?php
  239. } // end foreach
  240. $count++;
  241. ?>
  242. </table>
  243. <?php
  244. // display indexes information
  245. if (count(PMA_Index::getFromTable($table, $db)) > 0) {
  246. echo PMA_Index::getView($table, $db, true);
  247. }
  248. ?>
  249. </div>
  250. <?php
  251. } //ends main while
  252. /**
  253. * Displays the footer
  254. */
  255. echo PMA_Util::getButton();
  256. ?>