2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Display table relations for viewing and editing
6 * includes phpMyAdmin relations and InnoDB relations
8 * @todo fix name handling: currently names with dots (.) are not properly handled
9 * @todo foreign key constraints require both fields being of equal type and size
10 * @todo check foreign fields to be from same type and size, all other makes no sense
11 * @todo add an link to create an index required for constraints, or an option to do automatically
12 * @todo if above todos are fullfilled we can add all fields meet requirements in the select dropdown
17 * Gets some core libraries
19 require_once './libraries/common.inc.php';
20 require_once './libraries/tbl_common.php';
21 $url_query .= '&goto=tbl_sql.php';
25 * Gets tables informations
27 require_once './libraries/tbl_info.inc.php';
29 // Note: in libraries/tbl_links.inc.php we get and display the table comment.
30 // For InnoDB, this comment contains the REFER information but any update
31 // has not been done yet (will be done in tbl_relation.php later).
32 $avoid_show_comment = TRUE;
35 * Displays top menu links
37 require_once './libraries/tbl_links.inc.php';
39 require_once './libraries/relation.lib.php';
41 $options_array = array(
42 'CASCADE' => 'CASCADE',
43 'SET_NULL' => 'SET NULL',
44 'NO_ACTION' => 'NO ACTION',
45 'RESTRICT' => 'RESTRICT',
49 * Generate dropdown choices
51 * @param string Message to display
52 * @param string Name of the <select> field
53 * @param array Choices for dropdown
54 * @return string The existing value (for selected)
58 function PMA_generate_dropdown($dropdown_question, $radio_name, $choices, $selected_value)
60 echo htmlspecialchars($dropdown_question) . ' ';
62 echo '<select name="' . htmlspecialchars($radio_name) . '">' . "\n";
63 echo '<option value=""></option>' . "\n";
65 foreach ($choices as $one_value => $one_label) {
66 echo '<option value="' . htmlspecialchars($one_value) . '"';
67 if ($selected_value == $one_value) {
68 echo ' selected="selected" ';
70 echo '>' . htmlspecialchars($one_label) . '</option>' . "\n";
72 echo '</select>' . "\n";
76 * Gets the relation settings
78 $cfgRelation = PMA_getRelationsParam();
84 if ($cfgRelation['relwork']) {
85 $existrel = PMA_getForeigners($db, $table, '', 'internal');
87 if ($tbl_type == 'INNODB') {
88 $existrel_innodb = PMA_getForeigners($db, $table, '', 'innodb');
90 if ($cfgRelation['displaywork']) {
91 $disp = PMA_getDisplayField($db, $table);
94 // u p d a t e s f o r I n t e r n a l r e l a t i o n s
95 if (isset($destination) && $cfgRelation['relwork']) {
97 foreach ($destination as $master_field => $foreign_string) {
99 if (! empty($foreign_string)) {
100 $foreign_string = trim($foreign_string, '`');
101 list($foreign_db, $foreign_table, $foreign_field) =
102 explode('`.`', $foreign_string);
103 if (! isset($existrel[$master_field])) {
104 $upd_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
105 . '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
107 . '\'' . PMA_sqlAddslashes($db) . '\', '
108 . '\'' . PMA_sqlAddslashes($table) . '\', '
109 . '\'' . PMA_sqlAddslashes($master_field) . '\', '
110 . '\'' . PMA_sqlAddslashes($foreign_db) . '\', '
111 . '\'' . PMA_sqlAddslashes($foreign_table) . '\','
112 . '\'' . PMA_sqlAddslashes($foreign_field) . '\')';
113 } elseif ($existrel[$master_field]['foreign_db'] . '.' .$existrel[$master_field]['foreign_table'] . '.' . $existrel[$master_field]['foreign_field'] != $foreign_string) {
114 $upd_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation']) . ' SET'
115 . ' foreign_db = \'' . PMA_sqlAddslashes($foreign_db) . '\', '
116 . ' foreign_table = \'' . PMA_sqlAddslashes($foreign_table) . '\', '
117 . ' foreign_field = \'' . PMA_sqlAddslashes($foreign_field) . '\' '
118 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
119 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
120 . ' AND master_field = \'' . PMA_sqlAddslashes($master_field) . '\'';
121 } // end if... else....
122 } elseif (isset($existrel[$master_field])) {
123 $upd_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
124 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
125 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
126 . ' AND master_field = \'' . PMA_sqlAddslashes($master_field) . '\'';
127 } // end if... else....
129 PMA_query_as_cu($upd_query);
132 } // end if (updates for internal relations)
134 // u p d a t e s f o r I n n o D B
135 // (for now, one index name only; we keep the definitions if the
136 // foreign db is not the same)
137 // I use $sql_query to be able to display directly the query via
140 if (isset($_REQUEST['destination_innodb'])) {
143 foreach ($_REQUEST['destination_innodb'] as $master_field => $foreign_string) {
144 if (! empty($foreign_string)) {
145 $foreign_string = trim($foreign_string, '`');
146 list($foreign_db, $foreign_table, $foreign_field) =
147 explode('`.`', $foreign_string);
148 if (!isset($existrel_innodb[$master_field])) {
149 // no key defined for this field
151 // The next few lines are repeated below, so they
152 // could be put in an include file
153 // Note: I tried to enclose the db and table name with
154 // backquotes but MySQL 4.0.16 did not like the syntax
155 // (for example: `base2`.`table1`)
157 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
158 . ' ADD FOREIGN KEY ('
159 . PMA_backquote($master_field) . ')'
161 . PMA_backquote($foreign_db) . '.'
162 . PMA_backquote($foreign_table) . '('
163 . PMA_backquote($foreign_field) . ')';
165 if (! empty($_REQUEST['on_delete'][$master_field])) {
166 $sql_query .= ' ON DELETE ' . $options_array[$_REQUEST['on_delete'][$master_field]];
168 if (! empty($_REQUEST['on_update'][$master_field])) {
169 $sql_query .= ' ON UPDATE ' . $options_array[$_REQUEST['on_update'][$master_field]];
172 $display_query .= $sql_query . "\n";
175 } elseif (($existrel_innodb[$master_field]['foreign_db'] . '.' .$existrel_innodb[$master_field]['foreign_table'] . '.' . $existrel_innodb[$master_field]['foreign_field'] != $foreign_string)
176 ||
($_REQUEST['on_delete'][$master_field] != (!empty($existrel_innodb[$master_field]['on_delete']) ?
$existrel_innodb[$master_field]['on_delete'] : ''))
177 ||
($_REQUEST['on_update'][$master_field] != (!empty($existrel_innodb[$master_field]['on_update']) ?
$existrel_innodb[$master_field]['on_update'] : ''))
179 // another foreign key is already defined for this field
181 // an option has been changed for ON DELETE or ON UPDATE
183 // remove existing key
184 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
185 . ' DROP FOREIGN KEY '
186 . PMA_backquote($existrel_innodb[$master_field]['constraint']) . ';';
188 // I tried to send both in one query but it failed
189 PMA_DBI_query($sql_query);
190 $display_query .= $sql_query . "\n";
193 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
194 . ' ADD FOREIGN KEY ('
195 . PMA_backquote($master_field) . ')'
197 . PMA_backquote($foreign_db) . '.'
198 . PMA_backquote($foreign_table) . '('
199 . PMA_backquote($foreign_field) . ')';
201 if ($_REQUEST['on_delete'][$master_field] != 'nix') {
202 $sql_query .= ' ON DELETE '
203 . $options_array[$_REQUEST['on_delete'][$master_field]];
205 if ($_REQUEST['on_update'][$master_field] != 'nix') {
206 $sql_query .= ' ON UPDATE '
207 . $options_array[$_REQUEST['on_update'][$master_field]];
210 $display_query .= $sql_query . "\n";
212 } // end if... else....
213 } elseif (isset($existrel_innodb[$master_field])) {
214 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
215 . ' DROP FOREIGN KEY '
216 . PMA_backquote($existrel_innodb[$master_field]['constraint']);
218 $display_query .= $sql_query . "\n";
219 } // end if... else....
221 if (! empty($sql_query)) {
222 PMA_DBI_try_query($sql_query);
223 $tmp_error = PMA_DBI_getError();
224 if (! empty($tmp_error)) {
227 if (substr($tmp_error, 1, 4) == '1216'
228 ||
substr($tmp_error, 1, 4) == '1452') {
229 PMA_mysqlDie($tmp_error, $sql_query, FALSE, '', FALSE);
230 echo PMA_showMySQLDocu('manual_Table_types', 'InnoDB_foreign_key_constraints') . "\n";
232 if (substr($tmp_error, 1, 4) == '1005') {
233 $message = PMA_Message
::warning('strForeignKeyError');
234 $message->addParam($master_field);
236 echo PMA_showMySQLDocu('manual_Table_types', 'InnoDB_foreign_key_constraints') . "\n";
242 if (!empty($display_query)) {
244 PMA_showMessage($strError, null, 'error');
246 PMA_showMessage($strSuccess, null, 'success');
249 } // end if isset($destination_innodb)
252 // U p d a t e s f o r d i s p l a y f i e l d
254 if ($cfgRelation['displaywork'] && isset($display_field)) {
257 if ($display_field != '') {
258 $upd_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
259 . ' SET display_field = \'' . PMA_sqlAddslashes($display_field) . '\''
260 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
261 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
263 $upd_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
264 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
265 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
267 } elseif ($display_field != '') {
268 $upd_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
269 . '(db_name, table_name, display_field) '
271 . '\'' . PMA_sqlAddslashes($db) . '\','
272 . '\'' . PMA_sqlAddslashes($table) . '\','
273 . '\'' . PMA_sqlAddslashes($display_field) . '\')';
277 PMA_query_as_cu($upd_query);
281 // If we did an update, refresh our data
282 if (isset($destination) && $cfgRelation['relwork']) {
283 $existrel = PMA_getForeigners($db, $table, '', 'internal');
285 if (isset($destination_innodb) && $tbl_type=='INNODB') {
286 $existrel_innodb = PMA_getForeigners($db, $table, '', 'innodb');
289 if ($cfgRelation['displaywork']) {
290 $disp = PMA_getDisplayField($db, $table);
299 echo '<form method="post" action="tbl_relation.php">' . "\n";
300 echo PMA_generate_common_hidden_inputs($db, $table);
305 if ($cfgRelation['relwork'] ||
$tbl_type == 'INNODB') {
306 // To choose relations we first need all tables names in current db
307 // and if PMA version permits and the main table is innodb,
308 // we use SHOW TABLE STATUS because we need to find other InnoDB tables
310 if ($tbl_type == 'INNODB') {
311 $tab_query = 'SHOW TABLE STATUS FROM ' . PMA_backquote($db);
312 // [0] of the row is the name
315 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($db);
316 // [0] of the row is the name
319 $tab_rs = PMA_DBI_query($tab_query, null, PMA_DBI_QUERY_STORE
);
320 $selectboxall[] = '';
321 $selectboxall_innodb[] = '';
323 while ($curr_table = PMA_DBI_fetch_row($tab_rs)) {
324 $current_table = new PMA_Table($curr_table[0], $db);
326 $selectboxall = array_merge($selectboxall, $current_table->getUniqueColumns());
328 if ($tbl_type == 'INNODB'
329 && isset($curr_table[1])
330 && $curr_table[1] == 'InnoDB') {
331 $selectboxall_innodb = array_merge($selectboxall_innodb, $current_table->getIndexedColumns());
333 } // end while over tables
337 // Now find out the columns of our $table
338 // need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
339 $col_rs = PMA_DBI_try_query('SHOW COLUMNS FROM ' . PMA_backquote($table) . ';', null, PMA_DBI_QUERY_STORE
);
341 if ($col_rs && PMA_DBI_num_rows($col_rs) > 0) {
342 while ($row = PMA_DBI_fetch_assoc($col_rs)) {
345 $saved_row_cnt = count($save_row);
348 <legend
><?php
echo $strLinksTo; ?
></legend
>
353 if ($cfgRelation['relwork']) {
354 echo '<th>' . $strInternalRelations;
355 if ($tbl_type=='INNODB') {
356 echo PMA_showHint($strInternalNotNecessary);
360 if ($tbl_type == 'INNODB') {
361 echo '<th colspan="2">InnoDB';
368 for ($i = 0; $i < $saved_row_cnt; $i++
) {
369 $myfield = $save_row[$i]['Field'];
371 <tr
class="<?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
373 <b
><?php
echo $save_row[$i]['Field']; ?
></b
></td
>
375 if ($cfgRelation['relwork']) {
377 <td
><select name
="destination[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
379 // PMA internal relations
380 if (isset($existrel[$myfield])) {
381 $foreign_field = $existrel[$myfield]['foreign_db'] . '.'
382 . $existrel[$myfield]['foreign_table'] . '.'
383 . $existrel[$myfield]['foreign_field'];
385 $foreign_field = FALSE;
388 foreach ($selectboxall as $value) {
390 . '<option value="' . htmlspecialchars($value) . '"';
391 if ($foreign_field && $value == $foreign_field) {
392 echo ' selected="selected"';
395 echo '>' . htmlspecialchars($value) . '</option>'. "\n";
398 // if the link defined in relationtable points to a foreign field
399 // that is not a key in the foreign table, we show the link
400 // (will not be shown with an arrow)
401 if ($foreign_field && !$seen_key) {
403 .'<option value="' . htmlspecialchars($foreign_field) . '"'
404 .' selected="selected"'
405 .'>' . $foreign_field . '</option>'. "\n";
411 } // end if (internal relations)
413 if ($tbl_type=='INNODB') {
415 if (!empty($save_row[$i]['Key'])) {
417 <span
class="formelement">
418 <select name
="destination_innodb[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
420 if (isset($existrel_innodb[$myfield])) {
421 $foreign_field = $existrel_innodb[$myfield]['foreign_db'] . '.'
422 . $existrel_innodb[$myfield]['foreign_table'] . '.'
423 . $existrel_innodb[$myfield]['foreign_field'];
425 $foreign_field = FALSE;
428 $found_foreign_field = FALSE;
429 foreach ($selectboxall_innodb as $value) {
431 . '<option value="' . htmlspecialchars($value) . '"';
432 if ($foreign_field && $value == $foreign_field) {
433 echo ' selected="selected"';
434 $found_foreign_field = TRUE;
436 echo '>' . htmlspecialchars($value) . '</option>'. "\n";
439 // we did not find the foreign field in the tables of current db,
440 // must be defined in another db so show it to avoid erasing it
441 if (!$found_foreign_field && $foreign_field) {
443 . '<option value="' . htmlspecialchars($foreign_field) . '"';
444 echo ' selected="selected"';
445 echo '>' . $foreign_field . '</option>' . "\n";
451 <span
class="formelement">
453 PMA_generate_dropdown('ON DELETE',
454 'on_delete[' . $save_row[$i]['Field'] . ']',
456 isset($existrel_innodb[$myfield]['on_delete']) ?
$existrel_innodb[$myfield]['on_delete']: '');
458 echo '</span>' . "\n"
459 .'<span class="formelement">' . "\n";
461 PMA_generate_dropdown('ON UPDATE',
462 'on_update[' . $save_row[$i]['Field'] . ']',
464 isset($existrel_innodb[$myfield]['on_update']) ?
$existrel_innodb[$myfield]['on_update']: '');
465 echo '</span>' . "\n";
468 } // end if (a key exists)
476 echo ' </table>' . "\n";
477 echo '</fieldset>' . "\n";
479 if ($cfgRelation['displaywork']) {
480 // Get "display_field" infos
481 $disp = PMA_getDisplayField($db, $table);
484 <label
><?php
echo $strChangeDisplay . ': '; ?
></label
>
485 <select name
="display_field" style
="vertical-align: middle">
486 <option value
="">---</option
>
488 foreach ($save_row AS $row) {
489 echo ' <option value="' . htmlspecialchars($row['Field']) . '"';
490 if (isset($disp) && $row['Field'] == $disp) {
491 echo ' selected="selected"';
493 echo '>' . htmlspecialchars($row['Field']) . '</option>'. "\n";
499 } // end if (displayworks)
501 <fieldset
class="tblFooters">
502 <input type
="submit" value
="<?php echo $strSave; ?>" />
506 } // end if (we have columns in this table)
509 * Displays the footer
511 require_once './libraries/footer.inc.php';