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
142 isset($_REQUEST['fields_name'])
143 ?
$_REQUEST['fields_name']
146 if (isset($_REQUEST['destination_foreign'])) {
149 foreach ($_REQUEST['destination_foreign'] as $master_field_md5 => $foreign_string) {
151 // Map the fieldname's md5 back to it's real name
152 $master_field = $me_fields_name[$master_field_md5];
154 if (! empty($foreign_string)) {
155 $foreign_string = trim($foreign_string, '`');
156 list($foreign_db, $foreign_table, $foreign_field) =
157 explode('.', $foreign_string);
158 if (!isset($existrel_foreign[$master_field])) {
159 // no key defined for this field
161 // The next few lines are repeated below, so they
162 // could be put in an include file
163 // Note: I tried to enclose the db and table name with
164 // backquotes but MySQL 4.0.16 did not like the syntax
165 // (for example: `base2`.`table1`)
167 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
168 . ' ADD FOREIGN KEY ('
169 . PMA_backquote($master_field) . ')'
171 . PMA_backquote($foreign_db) . '.'
172 . PMA_backquote($foreign_table) . '('
173 . PMA_backquote($foreign_field) . ')';
175 if (! empty($_REQUEST['on_delete'][$master_field_md5])) {
176 $sql_query .= ' ON DELETE ' . $options_array[$_REQUEST['on_delete'][$master_field_md5]];
178 if (! empty($_REQUEST['on_update'][$master_field])) {
179 $sql_query .= ' ON UPDATE ' . $options_array[$_REQUEST['on_update'][$master_field_md5]];
182 $display_query .= $sql_query . "\n";
185 } elseif (($existrel_foreign[$master_field]['foreign_db'] . '.' .$existrel_foreign[$master_field]['foreign_table'] . '.' . $existrel_foreign[$master_field]['foreign_field'] != $foreign_string)
186 ||
($_REQUEST['on_delete'][$master_field_md5] != (!empty($existrel_foreign[$master_field]['on_delete']) ?
$existrel_foreign[$master_field]['on_delete'] : ''))
187 ||
($_REQUEST['on_update'][$master_field_md5] != (!empty($existrel_foreign[$master_field]['on_update']) ?
$existrel_foreign[$master_field]['on_update'] : ''))
189 // another foreign key is already defined for this field
191 // an option has been changed for ON DELETE or ON UPDATE
193 // remove existing key
194 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
195 . ' DROP FOREIGN KEY '
196 . PMA_backquote($existrel_foreign[$master_field]['constraint']) . ';';
198 // I tried to send both in one query but it failed
199 PMA_DBI_query($sql_query);
200 $display_query .= $sql_query . "\n";
203 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
204 . ' ADD FOREIGN KEY ('
205 . PMA_backquote($master_field) . ')'
207 . PMA_backquote($foreign_db) . '.'
208 . PMA_backquote($foreign_table) . '('
209 . PMA_backquote($foreign_field) . ')';
211 if (! empty($_REQUEST['on_delete'][$master_field_md5])) {
212 $sql_query .= ' ON DELETE '
213 . $options_array[$_REQUEST['on_delete'][$master_field_md5]];
215 if (! empty($_REQUEST['on_update'][$master_field_md5])) {
216 $sql_query .= ' ON UPDATE '
217 . $options_array[$_REQUEST['on_update'][$master_field_md5]];
220 $display_query .= $sql_query . "\n";
222 } // end if... else....
223 } elseif (isset($existrel_foreign[$master_field])) {
224 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
225 . ' DROP FOREIGN KEY '
226 . PMA_backquote($existrel_foreign[$master_field]['constraint']);
228 $display_query .= $sql_query . "\n";
229 } // end if... else....
231 if (! empty($sql_query)) {
232 PMA_DBI_try_query($sql_query);
233 $tmp_error = PMA_DBI_getError();
234 if (! empty($tmp_error)) {
237 if (substr($tmp_error, 1, 4) == '1216'
238 ||
substr($tmp_error, 1, 4) == '1452') {
239 PMA_mysqlDie($tmp_error, $sql_query, FALSE, '', FALSE);
240 echo PMA_showMySQLDocu('manual_Table_types', 'InnoDB_foreign_key_constraints') . "\n";
242 if (substr($tmp_error, 1, 4) == '1005') {
243 $message = PMA_Message
::warning('strForeignKeyError');
244 $message->addParam($master_field);
246 echo PMA_showMySQLDocu('manual_Table_types', 'InnoDB_foreign_key_constraints') . "\n";
252 if (!empty($display_query)) {
254 PMA_showMessage($strError, null, 'error');
256 PMA_showMessage($strSuccess, null, 'success');
259 } // end if isset($destination_foreign)
262 // U p d a t e s f o r d i s p l a y f i e l d
264 if ($cfgRelation['displaywork'] && isset($display_field)) {
267 if ($display_field != '') {
268 $upd_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
269 . ' SET display_field = \'' . PMA_sqlAddslashes($display_field) . '\''
270 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
271 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
273 $upd_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
274 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
275 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
277 } elseif ($display_field != '') {
278 $upd_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
279 . '(db_name, table_name, display_field) '
281 . '\'' . PMA_sqlAddslashes($db) . '\','
282 . '\'' . PMA_sqlAddslashes($table) . '\','
283 . '\'' . PMA_sqlAddslashes($display_field) . '\')';
287 PMA_query_as_cu($upd_query);
291 // If we did an update, refresh our data
292 if (isset($destination) && $cfgRelation['relwork']) {
293 $existrel = PMA_getForeigners($db, $table, '', 'internal');
295 if (isset($destination_foreign) && PMA_foreignkey_supported($tbl_type)) {
296 $existrel_foreign = PMA_getForeigners($db, $table, '', 'foreign');
299 if ($cfgRelation['displaywork']) {
300 $disp = PMA_getDisplayField($db, $table);
309 echo '<form method="post" action="tbl_relation.php">' . "\n";
310 echo PMA_generate_common_hidden_inputs($db, $table);
315 if ($cfgRelation['relwork'] ||
PMA_foreignkey_supported($tbl_type)) {
316 // To choose relations we first need all tables names in current db
317 // and if the main table supports foreign keys
318 // we use SHOW TABLE STATUS because we need to find other tables of the
321 if (PMA_foreignkey_supported($tbl_type)) {
322 $tab_query = 'SHOW TABLE STATUS FROM ' . PMA_backquote($db);
323 // [0] of the row is the name
326 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($db);
327 // [0] of the row is the name
330 $tab_rs = PMA_DBI_query($tab_query, null, PMA_DBI_QUERY_STORE
);
331 $selectboxall[] = '';
332 $selectboxall_foreign[] = '';
334 while ($curr_table = PMA_DBI_fetch_row($tab_rs)) {
335 $current_table = new PMA_Table($curr_table[0], $db);
337 // explicitely ask for non-quoted list of indexed columns
338 $selectboxall = array_merge($selectboxall, $current_table->getUniqueColumns(false));
340 // if foreign keys are supported, collect all keys from other
341 // tables of the same engine
342 if (PMA_foreignkey_supported($tbl_type)
343 && isset($curr_table[1])
344 && strtoupper($curr_table[1]) == $tbl_type) {
345 // explicitely ask for non-quoted list of indexed columns
346 $selectboxall_foreign = array_merge($selectboxall_foreign, $current_table->getIndexedColumns(false));
348 } // end while over tables
351 // Now find out the columns of our $table
352 // need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
353 $col_rs = PMA_DBI_try_query('SHOW COLUMNS FROM ' . PMA_backquote($table) . ';', null, PMA_DBI_QUERY_STORE
);
355 if ($col_rs && PMA_DBI_num_rows($col_rs) > 0) {
356 while ($row = PMA_DBI_fetch_assoc($col_rs)) {
359 $saved_row_cnt = count($save_row);
362 <legend
><?php
echo $strLinksTo; ?
></legend
>
367 if ($cfgRelation['relwork']) {
368 echo '<th>' . $strInternalRelations;
369 if (PMA_foreignkey_supported($tbl_type)) {
370 echo PMA_showHint($strInternalAndForeign);
374 if (PMA_foreignkey_supported($tbl_type)) {
375 // this does not have to be translated, it's part of the MySQL syntax
376 echo '<th colspan="2">FOREIGN KEY (' . $tbl_type . ')';
383 for ($i = 0; $i < $saved_row_cnt; $i++
) {
384 $myfield = $save_row[$i]['Field'];
385 // Use an md5 as array index to avoid having special characters in the name atttibure (see bug #1746964 )
386 $myfield_md5 = md5($myfield);
387 $myfield_html = htmlspecialchars($myfield);
389 <tr
class="<?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
391 <strong
><?php
echo $myfield_html; ?
></strong
>
392 <input type
="hidden" name
="fields_name[<?php echo $myfield_md5; ?>]" value
="<?php echo $myfield_html; ?>"/>
395 if ($cfgRelation['relwork']) {
397 <td
><select name
="destination[<?php echo $myfield_md5; ?>]">
399 // PMA internal relations
400 if (isset($existrel[$myfield])) {
401 $foreign_field = $existrel[$myfield]['foreign_db'] . '.'
402 . $existrel[$myfield]['foreign_table'] . '.'
403 . $existrel[$myfield]['foreign_field'];
405 $foreign_field = FALSE;
408 foreach ($selectboxall as $value) {
410 . '<option value="' . htmlspecialchars($value) . '"';
411 if ($foreign_field && $value == $foreign_field) {
412 echo ' selected="selected"';
415 echo '>' . htmlspecialchars($value) . '</option>'. "\n";
418 // if the link defined in relationtable points to a foreign field
419 // that is not a key in the foreign table, we show the link
420 // (will not be shown with an arrow)
421 if ($foreign_field && !$seen_key) {
423 .'<option value="' . htmlspecialchars($foreign_field) . '"'
424 .' selected="selected"'
425 .'>' . $foreign_field . '</option>'. "\n";
431 } // end if (internal relations)
433 if (PMA_foreignkey_supported($tbl_type)) {
435 if (!empty($save_row[$i]['Key'])) {
437 <span
class="formelement">
438 <select name
="destination_foreign[<?php echo $myfield_md5; ?>]">
440 if (isset($existrel_foreign[$myfield])) {
441 $foreign_field = $existrel_foreign[$myfield]['foreign_db'] . '.'
442 . $existrel_foreign[$myfield]['foreign_table'] . '.'
443 . $existrel_foreign[$myfield]['foreign_field'];
445 $foreign_field = FALSE;
448 $found_foreign_field = FALSE;
449 foreach ($selectboxall_foreign as $value) {
451 . '<option value="' . htmlspecialchars($value) . '"';
452 if ($foreign_field && $value == $foreign_field) {
453 echo ' selected="selected"';
454 $found_foreign_field = TRUE;
456 echo '>' . htmlspecialchars($value) . '</option>'. "\n";
459 // we did not find the foreign field in the tables of current db,
460 // must be defined in another db so show it to avoid erasing it
461 if (!$found_foreign_field && $foreign_field) {
463 . '<option value="' . htmlspecialchars($foreign_field) . '"';
464 echo ' selected="selected"';
465 echo '>' . $foreign_field . '</option>' . "\n";
471 <span
class="formelement">
473 PMA_generate_dropdown('ON DELETE',
474 'on_delete[' . $myfield_md5 . ']',
476 isset($existrel_foreign[$myfield]['on_delete']) ?
$existrel_foreign[$myfield]['on_delete']: '');
478 echo '</span>' . "\n"
479 .'<span class="formelement">' . "\n";
481 PMA_generate_dropdown('ON UPDATE',
482 'on_update[' . $myfield_md5 . ']',
484 isset($existrel_foreign[$myfield]['on_update']) ?
$existrel_foreign[$myfield]['on_update']: '');
485 echo '</span>' . "\n";
488 } // end if (a key exists)
496 unset( $myfield, $myfield_md5, $myfield_html);
498 echo ' </table>' . "\n";
499 echo '</fieldset>' . "\n";
501 if ($cfgRelation['displaywork']) {
502 // Get "display_field" infos
503 $disp = PMA_getDisplayField($db, $table);
506 <label
><?php
echo $strChangeDisplay . ': '; ?
></label
>
507 <select name
="display_field" style
="vertical-align: middle">
508 <option value
="">---</option
>
510 foreach ($save_row AS $row) {
511 echo ' <option value="' . htmlspecialchars($row['Field']) . '"';
512 if (isset($disp) && $row['Field'] == $disp) {
513 echo ' selected="selected"';
515 echo '>' . htmlspecialchars($row['Field']) . '</option>'. "\n";
521 } // end if (displayworks)
523 <fieldset
class="tblFooters">
524 <input type
="submit" value
="<?php echo $strSave; ?>" />
528 } // end if (we have columns in this table)
531 * Displays the footer
533 require_once './libraries/footer.inc.php';