Merge branch 'master' of https://github.com/phpmyadmin/phpmyadmin into 0609_monitor
[phpmyadmin/aamir.git] / db_datadict.php
blob7278c576c7755b624167f3e2075e47e4fff80005
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Renders data dictionary
6 * @package PhpMyAdmin
7 */
9 /**
10 * Gets the variables sent or posted to this script, then displays headers
12 require_once 'libraries/common.inc.php';
14 if (! isset($selected_tbl)) {
15 include 'libraries/db_common.inc.php';
16 include 'libraries/db_info.inc.php';
19 $response = PMA_Response::getInstance();
20 $header = $response->getHeader();
21 $header->enablePrintView();
23 /**
24 * Gets the relations settings
26 $cfgRelation = PMA_getRelationsParam();
28 require_once 'libraries/transformations.lib.php';
29 require_once 'libraries/Index.class.php';
31 /**
32 * Check parameters
34 PMA_Util::checkParameters(array('db'));
36 /**
37 * Defines the url to return to in case of error in a sql statement
39 if (strlen($table)) {
40 $err_url = 'tbl_sql.php?' . PMA_generate_common_url($db, $table);
41 } else {
42 $err_url = 'db_sql.php?' . PMA_generate_common_url($db);
45 if ($cfgRelation['commwork']) {
46 $comment = PMA_getDbComment($db);
48 /**
49 * Displays DB comment
51 if ($comment) {
52 echo '<p>' . __('Database comment: ')
53 . '<i>' . htmlspecialchars($comment) . '</i></p>';
54 } // end if
57 /**
58 * Selects the database and gets tables names
60 $GLOBALS['dbi']->selectDb($db);
61 $tables = $GLOBALS['dbi']->getTables($db);
63 $count = 0;
64 foreach ($tables as $table) {
65 $comments = PMA_getComments($db, $table);
67 echo '<div>' . "\n";
69 echo '<h2>' . htmlspecialchars($table) . '</h2>' . "\n";
71 /**
72 * Gets table informations
74 $show_comment = PMA_Table::sGetStatusInfo($db, $table, 'TABLE_COMMENT');
76 /**
77 * Gets table keys and retains them
80 $GLOBALS['dbi']->selectDb($db);
81 $indexes = $GLOBALS['dbi']->getTableIndexes($db, $table);
82 $primary = '';
83 $indexes = array();
84 $lastIndex = '';
85 $indexes_info = array();
86 $indexes_data = array();
87 $pk_array = array(); // will be use to emphasis prim. keys in the table
88 // view
89 foreach ($indexes as $row) {
90 // Backups the list of primary keys
91 if ($row['Key_name'] == 'PRIMARY') {
92 $primary .= $row['Column_name'] . ', ';
93 $pk_array[$row['Column_name']] = 1;
95 // Retains keys informations
96 if ($row['Key_name'] != $lastIndex) {
97 $indexes[] = $row['Key_name'];
98 $lastIndex = $row['Key_name'];
100 $indexes_info[$row['Key_name']]['Sequences'][] = $row['Seq_in_index'];
101 $indexes_info[$row['Key_name']]['Non_unique'] = $row['Non_unique'];
102 if (isset($row['Cardinality'])) {
103 $indexes_info[$row['Key_name']]['Cardinality'] = $row['Cardinality'];
105 // I don't know what does following column mean....
106 // $indexes_info[$row['Key_name']]['Packed'] = $row['Packed'];
108 $indexes_info[$row['Key_name']]['Comment'] = $row['Comment'];
110 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Column_name'] = $row['Column_name'];
111 if (isset($row['Sub_part'])) {
112 $indexes_data[$row['Key_name']][$row['Seq_in_index']]['Sub_part'] = $row['Sub_part'];
115 } // end while
118 * Gets columns properties
120 $columns = $GLOBALS['dbi']->getColumns($db, $table);
122 if (PMA_MYSQL_INT_VERSION < 50025) {
123 // We need this to correctly learn if a TIMESTAMP is NOT NULL, since
124 // SHOW FULL COLUMNS or INFORMATION_SCHEMA incorrectly says NULL
125 // and SHOW CREATE TABLE says NOT NULL
126 // http://bugs.mysql.com/20910.
128 $show_create_table_query = 'SHOW CREATE TABLE '
129 . PMA_Util::backquote($db) . '.'
130 . PMA_Util::backquote($table);
131 $show_create_table = $GLOBALS['dbi']->fetchValue(
132 $show_create_table_query, 0, 1
134 $analyzed_sql = PMA_SQP_analyze(PMA_SQP_parse($show_create_table));
137 // Check if we can use Relations
138 if (!empty($cfgRelation['relation'])) {
139 // Find which tables are related with the current one and write it in
140 // an array
141 $res_rel = PMA_getForeigners($db, $table);
143 if (count($res_rel) > 0) {
144 $have_rel = true;
145 } else {
146 $have_rel = false;
148 } else {
149 $have_rel = false;
150 } // end if
154 * Displays the comments of the table if MySQL >= 3.23
156 if (!empty($show_comment)) {
157 echo __('Table comments:') . ' ';
158 echo htmlspecialchars($show_comment) . '<br /><br />';
162 * Displays the table structure
165 echo '<table width="100%" class="print">';
166 echo '<tr><th width="50">' . __('Column') . '</th>';
167 echo '<th width="80">' . __('Type') . '</th>';
168 echo '<th width="40">' . __('Null') . '</th>';
169 echo '<th width="70">' . __('Default') . '</th>';
170 if ($have_rel) {
171 echo ' <th>' . __('Links to') . '</th>' . "\n";
173 echo ' <th>' . __('Comments') . '</th>' . "\n";
174 if ($cfgRelation['mimework']) {
175 echo ' <th>MIME</th>' . "\n";
177 echo '</tr>';
178 $odd_row = true;
179 foreach ($columns as $row) {
181 if ($row['Null'] == '') {
182 $row['Null'] = 'NO';
184 $extracted_columnspec
185 = PMA_Util::extractColumnSpec($row['Type']);
187 // reformat mysql query output
188 // set or enum types: slashes single quotes inside options
189 if ('set' == $extracted_columnspec['type']
190 || 'enum' == $extracted_columnspec['type']
192 $type_nowrap = '';
194 } else {
195 $type_nowrap = ' class="nowrap"';
197 $type = htmlspecialchars($extracted_columnspec['print_type']);
198 $attribute = $extracted_columnspec['attribute'];
199 if (! isset($row['Default'])) {
200 if ($row['Null'] != 'NO') {
201 $row['Default'] = '<i>NULL</i>';
203 } else {
204 $row['Default'] = htmlspecialchars($row['Default']);
206 $column_name = $row['Field'];
208 if (PMA_MYSQL_INT_VERSION < 50025
209 && ! empty($analyzed_sql[0]['create_table_fields'][$column_name]['type'])
210 && $analyzed_sql[0]['create_table_fields'][$column_name]['type'] == 'TIMESTAMP'
211 && $analyzed_sql[0]['create_table_fields'][$column_name]['timestamp_not_null']
213 // here, we have a TIMESTAMP that SHOW FULL COLUMNS reports as
214 // having the NULL attribute, but SHOW CREATE TABLE says the
215 // contrary. Believe the latter.
217 * @todo merge this logic with the one in tbl_structure.php
218 * or move it in a function similar to $GLOBALS['dbi']->getColumnsFull()
219 * but based on SHOW CREATE TABLE because information_schema
220 * cannot be trusted in this case (MySQL bug)
222 $row['Null'] = 'NO';
224 echo '<tr class="';
225 echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row;
226 echo '">';
227 echo '<td class="nowrap">';
229 if (isset($pk_array[$row['Field']])) {
230 echo '<u>' . htmlspecialchars($column_name) . '</u>';
231 } else {
232 echo htmlspecialchars($column_name);
234 echo '</td>';
235 echo '<td' . $type_nowrap . ' lang="en" dir="ltr">' . $type . '</td>';
236 echo '<td>';
237 echo (($row['Null'] == 'NO') ? __('No') : __('Yes'));
238 echo '</td>';
239 echo '<td class="nowrap">';
240 if (isset($row['Default'])) {
241 echo $row['Default'];
243 echo '</td>';
245 if ($have_rel) {
246 echo ' <td>';
247 if (isset($res_rel[$column_name])) {
248 echo htmlspecialchars(
249 $res_rel[$column_name]['foreign_table']
250 . ' -> '
251 . $res_rel[$column_name]['foreign_field']
254 echo '</td>' . "\n";
256 echo ' <td>';
257 if (isset($comments[$column_name])) {
258 echo htmlspecialchars($comments[$column_name]);
260 echo '</td>' . "\n";
261 if ($cfgRelation['mimework']) {
262 $mime_map = PMA_getMIME($db, $table, true);
264 echo ' <td>';
265 if (isset($mime_map[$column_name])) {
266 echo htmlspecialchars(
267 str_replace('_', '/', $mime_map[$column_name]['mimetype'])
270 echo '</td>' . "\n";
272 echo '</tr>';
273 } // end foreach
274 $count++;
275 echo '</table>';
276 // display indexes information
277 if (count(PMA_Index::getFromTable($table, $db)) > 0) {
278 echo PMA_Index::getView($table, $db, true);
280 echo '</div>';
281 } //ends main while
284 * Displays the footer
286 echo PMA_Util::getButton();