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
18 * Gets some core libraries
20 require_once './libraries/common.inc.php';
21 require_once './libraries/tbl_common.php';
22 $url_query .= '&goto=tbl_sql.php';
26 * Gets tables informations
28 require_once './libraries/tbl_info.inc.php';
30 // Note: in libraries/tbl_links.inc.php we get and display the table comment.
31 // For InnoDB, this comment contains the REFER information but any update
32 // has not been done yet (will be done in tbl_relation.php later).
33 $avoid_show_comment = TRUE;
36 * Displays top menu links
38 require_once './libraries/tbl_links.inc.php';
40 require_once './libraries/relation.lib.php';
42 $options_array = array(
43 'CASCADE' => 'CASCADE',
44 'SET_NULL' => 'SET NULL',
45 'NO_ACTION' => 'NO ACTION',
46 'RESTRICT' => 'RESTRICT',
50 * Generate dropdown choices
52 * @param string Message to display
53 * @param string Name of the <select> field
54 * @param array Choices for dropdown
55 * @return string The existing value (for selected)
59 function PMA_generate_dropdown($dropdown_question, $select_name, $choices, $selected_value)
61 echo htmlspecialchars($dropdown_question) . ' ';
63 echo '<select name="' . htmlspecialchars($select_name) . '">' . "\n";
64 echo '<option value=""></option>' . "\n";
66 foreach ($choices as $one_value => $one_label) {
67 echo '<option value="' . htmlspecialchars($one_value) . '"';
68 if ($selected_value == $one_value) {
69 echo ' selected="selected" ';
71 echo '>' . htmlspecialchars($one_label) . '</option>' . "\n";
73 echo '</select>' . "\n";
77 * Gets the relation settings
79 $cfgRelation = PMA_getRelationsParam();
85 if ($cfgRelation['relwork']) {
86 $existrel = PMA_getForeigners($db, $table, '', 'internal');
88 if (PMA_foreignkey_supported($tbl_type)) {
89 $existrel_foreign = PMA_getForeigners($db, $table, '', 'foreign');
91 if ($cfgRelation['displaywork']) {
92 $disp = PMA_getDisplayField($db, $table);
95 // 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
96 if (isset($destination) && $cfgRelation['relwork']) {
98 foreach ($destination as $master_field => $foreign_string) {
100 if (! empty($foreign_string)) {
101 $foreign_string = trim($foreign_string, '`');
102 list($foreign_db, $foreign_table, $foreign_field) =
103 explode('.', $foreign_string);
104 if (! isset($existrel[$master_field])) {
105 $upd_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
106 . '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
108 . '\'' . PMA_sqlAddslashes($db) . '\', '
109 . '\'' . PMA_sqlAddslashes($table) . '\', '
110 . '\'' . PMA_sqlAddslashes($master_field) . '\', '
111 . '\'' . PMA_sqlAddslashes($foreign_db) . '\', '
112 . '\'' . PMA_sqlAddslashes($foreign_table) . '\','
113 . '\'' . PMA_sqlAddslashes($foreign_field) . '\')';
114 } elseif ($existrel[$master_field]['foreign_db'] . '.' .$existrel[$master_field]['foreign_table'] . '.' . $existrel[$master_field]['foreign_field'] != $foreign_string) {
115 $upd_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation']) . ' SET'
116 . ' foreign_db = \'' . PMA_sqlAddslashes($foreign_db) . '\', '
117 . ' foreign_table = \'' . PMA_sqlAddslashes($foreign_table) . '\', '
118 . ' foreign_field = \'' . PMA_sqlAddslashes($foreign_field) . '\' '
119 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
120 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
121 . ' AND master_field = \'' . PMA_sqlAddslashes($master_field) . '\'';
122 } // end if... else....
123 } elseif (isset($existrel[$master_field])) {
124 $upd_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
125 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
126 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
127 . ' AND master_field = \'' . PMA_sqlAddslashes($master_field) . '\'';
128 } // end if... else....
130 PMA_query_as_cu($upd_query);
133 } // end if (updates for internal relations)
135 // u p d a t e s f o r f o r e i g n k e y s
136 // (for now, one index name only; we keep the definitions if the
137 // foreign db is not the same)
138 // I use $sql_query to be able to display directly the query via
141 if (isset($_REQUEST['destination_foreign'])) {
144 foreach ($_REQUEST['destination_foreign'] as $master_field => $foreign_string) {
145 if (! empty($foreign_string)) {
146 $foreign_string = trim($foreign_string, '`');
147 list($foreign_db, $foreign_table, $foreign_field) =
148 explode('.', $foreign_string);
149 if (!isset($existrel_foreign[$master_field])) {
150 // no key defined for this field
152 // The next few lines are repeated below, so they
153 // could be put in an include file
154 // Note: I tried to enclose the db and table name with
155 // backquotes but MySQL 4.0.16 did not like the syntax
156 // (for example: `base2`.`table1`)
158 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
159 . ' ADD FOREIGN KEY ('
160 . PMA_backquote($master_field) . ')'
162 . PMA_backquote($foreign_db) . '.'
163 . PMA_backquote($foreign_table) . '('
164 . PMA_backquote($foreign_field) . ')';
166 if (! empty($_REQUEST['on_delete'][$master_field])) {
167 $sql_query .= ' ON DELETE ' . $options_array[$_REQUEST['on_delete'][$master_field]];
169 if (! empty($_REQUEST['on_update'][$master_field])) {
170 $sql_query .= ' ON UPDATE ' . $options_array[$_REQUEST['on_update'][$master_field]];
173 $display_query .= $sql_query . "\n";
176 } elseif (($existrel_foreign[$master_field]['foreign_db'] . '.' .$existrel_foreign[$master_field]['foreign_table'] . '.' . $existrel_foreign[$master_field]['foreign_field'] != $foreign_string)
177 ||
($_REQUEST['on_delete'][$master_field] != (!empty($existrel_foreign[$master_field]['on_delete']) ?
$existrel_foreign[$master_field]['on_delete'] : ''))
178 ||
($_REQUEST['on_update'][$master_field] != (!empty($existrel_foreign[$master_field]['on_update']) ?
$existrel_foreign[$master_field]['on_update'] : ''))
180 // another foreign key is already defined for this field
182 // an option has been changed for ON DELETE or ON UPDATE
184 // remove existing key
185 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
186 . ' DROP FOREIGN KEY '
187 . PMA_backquote($existrel_foreign[$master_field]['constraint']) . ';';
189 // I tried to send both in one query but it failed
190 PMA_DBI_query($sql_query);
191 $display_query .= $sql_query . "\n";
194 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
195 . ' ADD FOREIGN KEY ('
196 . PMA_backquote($master_field) . ')'
198 . PMA_backquote($foreign_db) . '.'
199 . PMA_backquote($foreign_table) . '('
200 . PMA_backquote($foreign_field) . ')';
202 if (! empty($_REQUEST['on_delete'][$master_field])) {
203 $sql_query .= ' ON DELETE '
204 . $options_array[$_REQUEST['on_delete'][$master_field]];
206 if (! empty($_REQUEST['on_update'][$master_field])) {
207 $sql_query .= ' ON UPDATE '
208 . $options_array[$_REQUEST['on_update'][$master_field]];
211 $display_query .= $sql_query . "\n";
213 } // end if... else....
214 } elseif (isset($existrel_foreign[$master_field])) {
215 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
216 . ' DROP FOREIGN KEY '
217 . PMA_backquote($existrel_foreign[$master_field]['constraint']);
219 $display_query .= $sql_query . "\n";
220 } // end if... else....
222 if (! empty($sql_query)) {
223 PMA_DBI_try_query($sql_query);
224 $tmp_error = PMA_DBI_getError();
225 if (! empty($tmp_error)) {
228 if (substr($tmp_error, 1, 4) == '1216'
229 ||
substr($tmp_error, 1, 4) == '1452') {
230 PMA_mysqlDie($tmp_error, $sql_query, FALSE, '', FALSE);
231 echo PMA_showMySQLDocu('manual_Table_types', 'InnoDB_foreign_key_constraints') . "\n";
233 if (substr($tmp_error, 1, 4) == '1005') {
234 $message = PMA_Message
::warning('strForeignKeyError');
235 $message->addParam($master_field);
237 echo PMA_showMySQLDocu('manual_Table_types', 'InnoDB_foreign_key_constraints') . "\n";
243 if (!empty($display_query)) {
245 PMA_showMessage($strError, null, 'error');
247 PMA_showMessage($strSuccess, null, 'success');
250 } // end if isset($destination_foreign)
253 // U p d a t e s f o r d i s p l a y f i e l d
255 if ($cfgRelation['displaywork'] && isset($display_field)) {
258 if ($display_field != '') {
259 $upd_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
260 . ' SET display_field = \'' . PMA_sqlAddslashes($display_field) . '\''
261 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
262 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
264 $upd_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
265 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
266 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
268 } elseif ($display_field != '') {
269 $upd_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
270 . '(db_name, table_name, display_field) '
272 . '\'' . PMA_sqlAddslashes($db) . '\','
273 . '\'' . PMA_sqlAddslashes($table) . '\','
274 . '\'' . PMA_sqlAddslashes($display_field) . '\')';
278 PMA_query_as_cu($upd_query);
282 // If we did an update, refresh our data
283 if (isset($destination) && $cfgRelation['relwork']) {
284 $existrel = PMA_getForeigners($db, $table, '', 'internal');
286 if (isset($destination_foreign) && PMA_foreignkey_supported($tbl_type)) {
287 $existrel_foreign = PMA_getForeigners($db, $table, '', 'foreign');
290 if ($cfgRelation['displaywork']) {
291 $disp = PMA_getDisplayField($db, $table);
300 echo '<form method="post" action="tbl_relation.php">' . "\n";
301 echo PMA_generate_common_hidden_inputs($db, $table);
306 if ($cfgRelation['relwork'] ||
PMA_foreignkey_supported($tbl_type)) {
307 // To choose relations we first need all tables names in current db
308 // and if the main table supports foreign keys
309 // we use SHOW TABLE STATUS because we need to find other tables of the
312 if (PMA_foreignkey_supported($tbl_type)) {
313 $tab_query = 'SHOW TABLE STATUS FROM ' . PMA_backquote($db);
314 // [0] of the row is the name
317 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($db);
318 // [0] of the row is the name
321 $tab_rs = PMA_DBI_query($tab_query, null, PMA_DBI_QUERY_STORE
);
322 $selectboxall[] = '';
323 $selectboxall_foreign[] = '';
325 while ($curr_table = PMA_DBI_fetch_row($tab_rs)) {
326 $current_table = new PMA_Table($curr_table[0], $db);
328 // explicitely ask for non-quoted list of indexed columns
329 $selectboxall = array_merge($selectboxall, $current_table->getUniqueColumns(false));
331 // if foreign keys are supported, collect all keys from other
332 // tables of the same engine
333 if (PMA_foreignkey_supported($tbl_type)
334 && isset($curr_table[1])
335 && strtoupper($curr_table[1]) == $tbl_type) {
336 // explicitely ask for non-quoted list of indexed columns
337 $selectboxall_foreign = array_merge($selectboxall_foreign, $current_table->getIndexedColumns(false));
339 } // end while over tables
342 // Now find out the columns of our $table
343 // need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
344 $col_rs = PMA_DBI_try_query('SHOW COLUMNS FROM ' . PMA_backquote($table) . ';', null, PMA_DBI_QUERY_STORE
);
346 if ($col_rs && PMA_DBI_num_rows($col_rs) > 0) {
347 while ($row = PMA_DBI_fetch_assoc($col_rs)) {
350 $saved_row_cnt = count($save_row);
353 <legend
><?php
echo $strLinksTo; ?
></legend
>
358 if ($cfgRelation['relwork']) {
359 echo '<th>' . $strInternalRelations;
360 if (PMA_foreignkey_supported($tbl_type)) {
361 echo PMA_showHint($strInternalAndForeign);
365 if (PMA_foreignkey_supported($tbl_type)) {
366 // this does not have to be translated, it's part of the MySQL syntax
367 echo '<th colspan="2">FOREIGN KEY (' . $tbl_type . ')';
374 for ($i = 0; $i < $saved_row_cnt; $i++
) {
375 $myfield = $save_row[$i]['Field'];
377 <tr
class="<?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
379 <strong
><?php
echo $save_row[$i]['Field']; ?
></strong
></td
>
381 if ($cfgRelation['relwork']) {
383 <td
><select name
="destination[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
385 // PMA internal relations
386 if (isset($existrel[$myfield])) {
387 $foreign_field = $existrel[$myfield]['foreign_db'] . '.'
388 . $existrel[$myfield]['foreign_table'] . '.'
389 . $existrel[$myfield]['foreign_field'];
391 $foreign_field = FALSE;
394 foreach ($selectboxall as $value) {
396 . '<option value="' . htmlspecialchars($value) . '"';
397 if ($foreign_field && $value == $foreign_field) {
398 echo ' selected="selected"';
401 echo '>' . htmlspecialchars($value) . '</option>'. "\n";
404 // if the link defined in relationtable points to a foreign field
405 // that is not a key in the foreign table, we show the link
406 // (will not be shown with an arrow)
407 if ($foreign_field && !$seen_key) {
409 .'<option value="' . htmlspecialchars($foreign_field) . '"'
410 .' selected="selected"'
411 .'>' . $foreign_field . '</option>'. "\n";
417 } // end if (internal relations)
419 if (PMA_foreignkey_supported($tbl_type)) {
421 if (!empty($save_row[$i]['Key'])) {
423 <span
class="formelement">
424 <select name
="destination_foreign[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
426 if (isset($existrel_foreign[$myfield])) {
427 $foreign_field = $existrel_foreign[$myfield]['foreign_db'] . '.'
428 . $existrel_foreign[$myfield]['foreign_table'] . '.'
429 . $existrel_foreign[$myfield]['foreign_field'];
431 $foreign_field = FALSE;
434 $found_foreign_field = FALSE;
435 foreach ($selectboxall_foreign as $value) {
437 . '<option value="' . htmlspecialchars($value) . '"';
438 if ($foreign_field && $value == $foreign_field) {
439 echo ' selected="selected"';
440 $found_foreign_field = TRUE;
442 echo '>' . htmlspecialchars($value) . '</option>'. "\n";
445 // we did not find the foreign field in the tables of current db,
446 // must be defined in another db so show it to avoid erasing it
447 if (!$found_foreign_field && $foreign_field) {
449 . '<option value="' . htmlspecialchars($foreign_field) . '"';
450 echo ' selected="selected"';
451 echo '>' . $foreign_field . '</option>' . "\n";
457 <span
class="formelement">
459 PMA_generate_dropdown('ON DELETE',
460 'on_delete[' . $save_row[$i]['Field'] . ']',
462 isset($existrel_foreign[$myfield]['on_delete']) ?
$existrel_foreign[$myfield]['on_delete']: '');
464 echo '</span>' . "\n"
465 .'<span class="formelement">' . "\n";
467 PMA_generate_dropdown('ON UPDATE',
468 'on_update[' . $save_row[$i]['Field'] . ']',
470 isset($existrel_foreign[$myfield]['on_update']) ?
$existrel_foreign[$myfield]['on_update']: '');
471 echo '</span>' . "\n";
474 } // end if (a key exists)
482 echo ' </table>' . "\n";
483 echo '</fieldset>' . "\n";
485 if ($cfgRelation['displaywork']) {
486 // Get "display_field" infos
487 $disp = PMA_getDisplayField($db, $table);
490 <label
><?php
echo $strChangeDisplay . ': '; ?
></label
>
491 <select name
="display_field" style
="vertical-align: middle">
492 <option value
="">---</option
>
494 foreach ($save_row AS $row) {
495 echo ' <option value="' . htmlspecialchars($row['Field']) . '"';
496 if (isset($disp) && $row['Field'] == $disp) {
497 echo ' selected="selected"';
499 echo '>' . htmlspecialchars($row['Field']) . '</option>'. "\n";
505 } // end if (displayworks)
507 <fieldset
class="tblFooters">
508 <input type
="submit" value
="<?php echo $strSave; ?>" />
512 } // end if (we have columns in this table)
515 * Displays the footer
517 require_once './libraries/footer.inc.php';