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 // will be used in the logic for internal relations and foreign keys:
97 isset($_REQUEST['fields_name'])
98 ?
$_REQUEST['fields_name']
101 // 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
102 if (isset($destination) && $cfgRelation['relwork']) {
104 foreach ($destination as $master_field_md5 => $foreign_string) {
107 // Map the fieldname's md5 back to its real name
108 $master_field = $me_fields_name[$master_field_md5];
110 if (! empty($foreign_string)) {
111 $foreign_string = trim($foreign_string, '`');
112 list($foreign_db, $foreign_table, $foreign_field) =
113 explode('.', $foreign_string);
114 if (! isset($existrel[$master_field])) {
115 $upd_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
116 . '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
118 . '\'' . PMA_sqlAddslashes($db) . '\', '
119 . '\'' . PMA_sqlAddslashes($table) . '\', '
120 . '\'' . PMA_sqlAddslashes($master_field) . '\', '
121 . '\'' . PMA_sqlAddslashes($foreign_db) . '\', '
122 . '\'' . PMA_sqlAddslashes($foreign_table) . '\','
123 . '\'' . PMA_sqlAddslashes($foreign_field) . '\')';
124 } elseif ($existrel[$master_field]['foreign_db'] . '.' .$existrel[$master_field]['foreign_table'] . '.' . $existrel[$master_field]['foreign_field'] != $foreign_string) {
125 $upd_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation']) . ' SET'
126 . ' foreign_db = \'' . PMA_sqlAddslashes($foreign_db) . '\', '
127 . ' foreign_table = \'' . PMA_sqlAddslashes($foreign_table) . '\', '
128 . ' foreign_field = \'' . PMA_sqlAddslashes($foreign_field) . '\' '
129 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
130 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
131 . ' AND master_field = \'' . PMA_sqlAddslashes($master_field) . '\'';
132 } // end if... else....
133 } elseif (isset($existrel[$master_field])) {
134 $upd_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
135 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
136 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
137 . ' AND master_field = \'' . PMA_sqlAddslashes($master_field) . '\'';
138 } // end if... else....
140 PMA_query_as_controluser($upd_query);
143 } // end if (updates for internal relations)
145 // u p d a t e s f o r f o r e i g n k e y s
146 // (for now, one index name only; we keep the definitions if the
147 // foreign db is not the same)
148 // I use $sql_query to be able to display directly the query via
151 if (isset($_REQUEST['destination_foreign'])) {
154 foreach ($_REQUEST['destination_foreign'] as $master_field_md5 => $foreign_string) {
156 // Map the fieldname's md5 back to it's real name
157 $master_field = $me_fields_name[$master_field_md5];
159 if (! empty($foreign_string)) {
160 $foreign_string = trim($foreign_string, '`');
161 list($foreign_db, $foreign_table, $foreign_field) =
162 explode('.', $foreign_string);
163 if (!isset($existrel_foreign[$master_field])) {
164 // no key defined for this field
166 // The next few lines are repeated below, so they
167 // could be put in an include file
168 // Note: I tried to enclose the db and table name with
169 // backquotes but MySQL 4.0.16 did not like the syntax
170 // (for example: `base2`.`table1`)
172 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
173 . ' ADD FOREIGN KEY ('
174 . PMA_backquote($master_field) . ')'
176 . PMA_backquote($foreign_db) . '.'
177 . PMA_backquote($foreign_table) . '('
178 . PMA_backquote($foreign_field) . ')';
180 if (! empty($_REQUEST['on_delete'][$master_field_md5])) {
181 $sql_query .= ' ON DELETE ' . $options_array[$_REQUEST['on_delete'][$master_field_md5]];
183 if (! empty($_REQUEST['on_update'][$master_field_md5])) {
184 $sql_query .= ' ON UPDATE ' . $options_array[$_REQUEST['on_update'][$master_field_md5]];
187 $display_query .= $sql_query . "\n";
190 } elseif (($existrel_foreign[$master_field]['foreign_db'] . '.' .$existrel_foreign[$master_field]['foreign_table'] . '.' . $existrel_foreign[$master_field]['foreign_field'] != $foreign_string)
191 ||
($_REQUEST['on_delete'][$master_field_md5] != (!empty($existrel_foreign[$master_field]['on_delete']) ?
$existrel_foreign[$master_field]['on_delete'] : ''))
192 ||
($_REQUEST['on_update'][$master_field_md5] != (!empty($existrel_foreign[$master_field]['on_update']) ?
$existrel_foreign[$master_field]['on_update'] : ''))
194 // another foreign key is already defined for this field
196 // an option has been changed for ON DELETE or ON UPDATE
198 // remove existing key
199 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
200 . ' DROP FOREIGN KEY '
201 . PMA_backquote($existrel_foreign[$master_field]['constraint']) . ';';
203 // I tried to send both in one query but it failed
204 PMA_DBI_query($sql_query);
205 $display_query .= $sql_query . "\n";
208 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
209 . ' ADD FOREIGN KEY ('
210 . PMA_backquote($master_field) . ')'
212 . PMA_backquote($foreign_db) . '.'
213 . PMA_backquote($foreign_table) . '('
214 . PMA_backquote($foreign_field) . ')';
216 if (! empty($_REQUEST['on_delete'][$master_field_md5])) {
217 $sql_query .= ' ON DELETE '
218 . $options_array[$_REQUEST['on_delete'][$master_field_md5]];
220 if (! empty($_REQUEST['on_update'][$master_field_md5])) {
221 $sql_query .= ' ON UPDATE '
222 . $options_array[$_REQUEST['on_update'][$master_field_md5]];
225 $display_query .= $sql_query . "\n";
227 } // end if... else....
228 } elseif (isset($existrel_foreign[$master_field])) {
229 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
230 . ' DROP FOREIGN KEY '
231 . PMA_backquote($existrel_foreign[$master_field]['constraint']);
233 $display_query .= $sql_query . "\n";
234 } // end if... else....
236 if (! empty($sql_query)) {
237 PMA_DBI_try_query($sql_query);
238 $tmp_error = PMA_DBI_getError();
239 if (! empty($tmp_error)) {
242 if (substr($tmp_error, 1, 4) == '1216'
243 ||
substr($tmp_error, 1, 4) == '1452') {
244 PMA_mysqlDie($tmp_error, $sql_query, FALSE, '', FALSE);
245 echo PMA_showMySQLDocu('manual_Table_types', 'InnoDB_foreign_key_constraints') . "\n";
247 if (substr($tmp_error, 1, 4) == '1005') {
248 $message = PMA_Message
::warning('strForeignKeyError');
249 $message->addParam($master_field);
251 echo PMA_showMySQLDocu('manual_Table_types', 'InnoDB_foreign_key_constraints') . "\n";
257 if (!empty($display_query)) {
259 PMA_showMessage($strError, null, 'error');
261 PMA_showMessage($strSuccess, null, 'success');
264 } // end if isset($destination_foreign)
267 // U p d a t e s f o r d i s p l a y f i e l d
269 if ($cfgRelation['displaywork'] && isset($display_field)) {
272 if ($display_field != '') {
273 $upd_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
274 . ' SET display_field = \'' . PMA_sqlAddslashes($display_field) . '\''
275 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
276 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
278 $upd_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
279 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
280 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
282 } elseif ($display_field != '') {
283 $upd_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
284 . '(db_name, table_name, display_field) '
286 . '\'' . PMA_sqlAddslashes($db) . '\','
287 . '\'' . PMA_sqlAddslashes($table) . '\','
288 . '\'' . PMA_sqlAddslashes($display_field) . '\')';
292 PMA_query_as_controluser($upd_query);
296 // If we did an update, refresh our data
297 if (isset($destination) && $cfgRelation['relwork']) {
298 $existrel = PMA_getForeigners($db, $table, '', 'internal');
300 if (isset($destination_foreign) && PMA_foreignkey_supported($tbl_type)) {
301 $existrel_foreign = PMA_getForeigners($db, $table, '', 'foreign');
304 if ($cfgRelation['displaywork']) {
305 $disp = PMA_getDisplayField($db, $table);
314 echo '<form method="post" action="tbl_relation.php">' . "\n";
315 echo PMA_generate_common_hidden_inputs($db, $table);
320 if ($cfgRelation['relwork'] ||
PMA_foreignkey_supported($tbl_type)) {
321 // To choose relations we first need all tables names in current db
322 // and if the main table supports foreign keys
323 // we use SHOW TABLE STATUS because we need to find other tables of the
326 if (PMA_foreignkey_supported($tbl_type)) {
327 $tab_query = 'SHOW TABLE STATUS FROM ' . PMA_backquote($db);
328 // [0] of the row is the name
331 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($db);
332 // [0] of the row is the name
335 $tab_rs = PMA_DBI_query($tab_query, null, PMA_DBI_QUERY_STORE
);
336 $selectboxall[] = '';
337 $selectboxall_foreign[] = '';
339 while ($curr_table = PMA_DBI_fetch_row($tab_rs)) {
340 $current_table = new PMA_Table($curr_table[0], $db);
342 // explicitely ask for non-quoted list of indexed columns
343 $selectboxall = array_merge($selectboxall, $current_table->getUniqueColumns(false));
345 // if foreign keys are supported, collect all keys from other
346 // tables of the same engine
347 if (PMA_foreignkey_supported($tbl_type)
348 && isset($curr_table[1])
349 && strtoupper($curr_table[1]) == $tbl_type) {
350 // explicitely ask for non-quoted list of indexed columns
351 $selectboxall_foreign = array_merge($selectboxall_foreign, $current_table->getIndexedColumns(false));
353 } // end while over tables
356 // Now find out the columns of our $table
357 // need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
358 $col_rs = PMA_DBI_try_query('SHOW COLUMNS FROM ' . PMA_backquote($table) . ';', null, PMA_DBI_QUERY_STORE
);
360 if ($col_rs && PMA_DBI_num_rows($col_rs) > 0) {
361 while ($row = PMA_DBI_fetch_assoc($col_rs)) {
364 $saved_row_cnt = count($save_row);
367 <legend
><?php
echo $strLinksTo; ?
></legend
>
372 if ($cfgRelation['relwork']) {
373 echo '<th>' . $strInternalRelations;
374 if (PMA_foreignkey_supported($tbl_type)) {
375 echo PMA_showHint($strInternalAndForeign);
379 if (PMA_foreignkey_supported($tbl_type)) {
380 // this does not have to be translated, it's part of the MySQL syntax
381 echo '<th colspan="2">FOREIGN KEY (' . $tbl_type . ')';
388 for ($i = 0; $i < $saved_row_cnt; $i++
) {
389 $myfield = $save_row[$i]['Field'];
390 // Use an md5 as array index to avoid having special characters in the name atttibure (see bug #1746964 )
391 $myfield_md5 = md5($myfield);
392 $myfield_html = htmlspecialchars($myfield);
394 <tr
class="<?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
396 <strong
><?php
echo $myfield_html; ?
></strong
>
397 <input type
="hidden" name
="fields_name[<?php echo $myfield_md5; ?>]" value
="<?php echo $myfield_html; ?>"/>
400 if ($cfgRelation['relwork']) {
402 <td
><select name
="destination[<?php echo $myfield_md5; ?>]">
404 // PMA internal relations
405 if (isset($existrel[$myfield])) {
406 $foreign_field = $existrel[$myfield]['foreign_db'] . '.'
407 . $existrel[$myfield]['foreign_table'] . '.'
408 . $existrel[$myfield]['foreign_field'];
410 $foreign_field = FALSE;
413 foreach ($selectboxall as $value) {
415 . '<option value="' . htmlspecialchars($value) . '"';
416 if ($foreign_field && $value == $foreign_field) {
417 echo ' selected="selected"';
420 echo '>' . htmlspecialchars($value) . '</option>'. "\n";
423 // if the link defined in relationtable points to a foreign field
424 // that is not a key in the foreign table, we show the link
425 // (will not be shown with an arrow)
426 if ($foreign_field && !$seen_key) {
428 .'<option value="' . htmlspecialchars($foreign_field) . '"'
429 .' selected="selected"'
430 .'>' . $foreign_field . '</option>'. "\n";
436 } // end if (internal relations)
438 if (PMA_foreignkey_supported($tbl_type)) {
440 if (!empty($save_row[$i]['Key'])) {
442 <span
class="formelement">
443 <select name
="destination_foreign[<?php echo $myfield_md5; ?>]">
445 if (isset($existrel_foreign[$myfield])) {
446 $foreign_field = $existrel_foreign[$myfield]['foreign_db'] . '.'
447 . $existrel_foreign[$myfield]['foreign_table'] . '.'
448 . $existrel_foreign[$myfield]['foreign_field'];
450 $foreign_field = FALSE;
453 $found_foreign_field = FALSE;
454 foreach ($selectboxall_foreign as $value) {
456 . '<option value="' . htmlspecialchars($value) . '"';
457 if ($foreign_field && $value == $foreign_field) {
458 echo ' selected="selected"';
459 $found_foreign_field = TRUE;
461 echo '>' . htmlspecialchars($value) . '</option>'. "\n";
464 // we did not find the foreign field in the tables of current db,
465 // must be defined in another db so show it to avoid erasing it
466 if (!$found_foreign_field && $foreign_field) {
468 . '<option value="' . htmlspecialchars($foreign_field) . '"';
469 echo ' selected="selected"';
470 echo '>' . $foreign_field . '</option>' . "\n";
476 <span
class="formelement">
478 PMA_generate_dropdown('ON DELETE',
479 'on_delete[' . $myfield_md5 . ']',
481 isset($existrel_foreign[$myfield]['on_delete']) ?
$existrel_foreign[$myfield]['on_delete']: '');
483 echo '</span>' . "\n"
484 .'<span class="formelement">' . "\n";
486 PMA_generate_dropdown('ON UPDATE',
487 'on_update[' . $myfield_md5 . ']',
489 isset($existrel_foreign[$myfield]['on_update']) ?
$existrel_foreign[$myfield]['on_update']: '');
490 echo '</span>' . "\n";
493 } // end if (a key exists)
501 unset( $myfield, $myfield_md5, $myfield_html);
503 echo ' </table>' . "\n";
504 echo '</fieldset>' . "\n";
506 if ($cfgRelation['displaywork']) {
507 // Get "display_field" infos
508 $disp = PMA_getDisplayField($db, $table);
511 <label
><?php
echo $strChangeDisplay . ': '; ?
></label
>
512 <select name
="display_field" style
="vertical-align: middle">
513 <option value
="">---</option
>
515 foreach ($save_row AS $row) {
516 echo ' <option value="' . htmlspecialchars($row['Field']) . '"';
517 if (isset($disp) && $row['Field'] == $disp) {
518 echo ' selected="selected"';
520 echo '>' . htmlspecialchars($row['Field']) . '</option>'. "\n";
526 } // end if (displayworks)
528 <fieldset
class="tblFooters">
529 <input type
="submit" value
="<?php echo $strSave; ?>" />
533 } // end if (we have columns in this table)
536 * Displays the footer
538 require_once './libraries/footer.inc.php';