Typo.
[phpmyadmin/last10db.git] / tbl_relation.php
blob66b866f59644c7d44f476d05fc2f170c5144cefc
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @version $Id$
6 */
8 /**
9 * Gets some core libraries
11 require_once './libraries/common.inc.php';
12 require_once './libraries/tbl_common.php';
13 $url_query .= '&amp;goto=tbl_sql.php';
16 /**
17 * Gets tables informations
19 require_once './libraries/tbl_info.inc.php';
21 // Note: in libraries/tbl_links.inc.php we get and display the table comment.
22 // For InnoDB, this comment contains the REFER information but any update
23 // has not been done yet (will be done in tbl_relation.php later).
24 $avoid_show_comment = TRUE;
26 /**
27 * Displays top menu links
29 require_once './libraries/tbl_links.inc.php';
31 require_once './libraries/relation.lib.php';
33 $options_array = array('CASCADE' => 'CASCADE', 'SET_NULL' => 'SET NULL', 'NO_ACTION' => 'NO ACTION', 'RESTRICT' => 'RESTRICT');
35 /**
36 * Generate dropdown choices
38 * @param string Message to display
39 * @param string Name of the <select> field
40 * @param array Choices for dropdown
41 * @return string The existing value (for selected)
43 * @access public
45 function PMA_generate_dropdown($dropdown_question, $radio_name, $choices, $selected_value)
47 echo $dropdown_question . '&nbsp;&nbsp;';
49 echo '<select name="' . $radio_name . '">' . "\n";
50 echo '<option value="nix">--</option>' . "\n";
52 foreach ($choices AS $one_value => $one_label) {
53 echo '<option value="' . $one_value . '"';
54 if ($selected_value == $one_value) {
55 echo ' selected="selected" ';
57 echo '>' . $one_label . '</option>' . "\n";
59 echo '</select>' . "\n";
60 echo "\n";
64 /**
65 * Gets the relation settings
67 $cfgRelation = PMA_getRelationsParam();
70 /**
71 * Updates
74 // ensure we are positionned to our current db (since the previous reading
75 // of relations makes pmadb the current one, maybe depending on the MySQL version)
76 PMA_DBI_select_db($db);
78 if ($cfgRelation['relwork']) {
79 $existrel = PMA_getForeigners($db, $table, '', 'internal');
81 if ($tbl_type=='INNODB') {
82 $existrel_innodb = PMA_getForeigners($db, $table, '', 'innodb');
84 if ($cfgRelation['displaywork']) {
85 $disp = PMA_getDisplayField($db, $table);
88 // 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
89 if (isset($destination) && $cfgRelation['relwork']) {
91 foreach ($destination AS $master_field => $foreign_string) {
92 if ($foreign_string != 'nix') {
93 list($foreign_db, $foreign_table, $foreign_field) = explode('.', $foreign_string);
94 if (!isset($existrel[$master_field])) {
95 $upd_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
96 . '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
97 . ' values('
98 . '\'' . PMA_sqlAddslashes($db) . '\', '
99 . '\'' . PMA_sqlAddslashes($table) . '\', '
100 . '\'' . PMA_sqlAddslashes($master_field) . '\', '
101 . '\'' . PMA_sqlAddslashes($foreign_db) . '\', '
102 . '\'' . PMA_sqlAddslashes($foreign_table) . '\','
103 . '\'' . PMA_sqlAddslashes($foreign_field) . '\')';
104 } elseif ($existrel[$master_field]['foreign_db'] . '.' .$existrel[$master_field]['foreign_table'] . '.' . $existrel[$master_field]['foreign_field'] != $foreign_string) {
105 $upd_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation']) . ' SET'
106 . ' foreign_db = \'' . PMA_sqlAddslashes($foreign_db) . '\', '
107 . ' foreign_table = \'' . PMA_sqlAddslashes($foreign_table) . '\', '
108 . ' foreign_field = \'' . PMA_sqlAddslashes($foreign_field) . '\' '
109 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
110 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
111 . ' AND master_field = \'' . PMA_sqlAddslashes($master_field) . '\'';
112 } // end if... else....
113 } elseif (isset($existrel[$master_field])) {
114 $upd_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
115 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
116 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
117 . ' AND master_field = \'' . PMA_sqlAddslashes($master_field) . '\'';
118 } // end if... else....
119 if (isset($upd_query)) {
120 $upd_rs = PMA_query_as_cu($upd_query);
121 unset($upd_query);
123 } // end while
124 } // end if (updates for internal relations)
126 // u p d a t e s f o r I n n o D B
127 // (for now, one index name only; we keep the definitions if the
128 // foreign db is not the same)
129 // I use $sql_query to be able to display directly the query via
130 // PMA_showMessage()
132 if (isset($_REQUEST['destination_innodb'])) {
133 $display_query = '';
134 $seen_error = false;
135 foreach ($_REQUEST['destination_innodb'] as $master_field => $foreign_string) {
136 if ($foreign_string != 'nix') {
137 list($foreign_db, $foreign_table, $foreign_field) = explode('.', $foreign_string);
138 if (!isset($existrel_innodb[$master_field])) {
139 // no key defined for this field
141 // The next few lines are repeated below, so they
142 // could be put in an include file
143 // Note: I tried to enclose the db and table name with
144 // backquotes but MySQL 4.0.16 did not like the syntax
145 // (for example: `base2`.`table1`)
147 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
148 . ' ADD FOREIGN KEY ('
149 . PMA_backquote($master_field) . ')'
150 . ' REFERENCES '
151 . PMA_backquote($foreign_db) . '.'
152 . PMA_backquote($foreign_table) . '('
153 . PMA_backquote($foreign_field) . ')';
155 if ($_REQUEST['on_delete'][$master_field] != 'nix') {
156 $sql_query .= ' ON DELETE ' . $options_array[$_REQUEST['on_delete'][$master_field]];
158 if ($_REQUEST['on_update'][$master_field] != 'nix') {
159 $sql_query .= ' ON UPDATE ' . $options_array[$_REQUEST['on_update'][$master_field]];
161 $sql_query .= ';';
162 $display_query .= $sql_query . "\n";
163 // end repeated code
165 } elseif (($existrel_innodb[$master_field]['foreign_db'] . '.' .$existrel_innodb[$master_field]['foreign_table'] . '.' . $existrel_innodb[$master_field]['foreign_field'] != $foreign_string)
166 || ($_REQUEST['on_delete'][$master_field] != (!empty($existrel_innodb[$master_field]['on_delete']) ? $existrel_innodb[$master_field]['on_delete'] : ''))
167 || ($_REQUEST['on_update'][$master_field] != (!empty($existrel_innodb[$master_field]['on_update']) ? $existrel_innodb[$master_field]['on_update'] : ''))
169 // another foreign key is already defined for this field
170 // or
171 // an option has been changed for ON DELETE or ON UPDATE
173 // remove existing key
174 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
175 . ' DROP FOREIGN KEY '
176 . PMA_backquote($existrel_innodb[$master_field]['constraint']) . ';';
178 // I tried to send both in one query but it failed
179 $upd_rs = PMA_DBI_query($sql_query);
180 $display_query .= $sql_query . "\n";
182 // add another
183 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
184 . ' ADD FOREIGN KEY ('
185 . PMA_backquote($master_field) . ')'
186 . ' REFERENCES '
187 . PMA_backquote($foreign_db) . '.'
188 . PMA_backquote($foreign_table) . '('
189 . PMA_backquote($foreign_field) . ')';
191 if ($_REQUEST['on_delete'][$master_field] != 'nix') {
192 $sql_query .= ' ON DELETE '
193 . $options_array[$_REQUEST['on_delete'][$master_field]];
195 if ($_REQUEST['on_update'][$master_field] != 'nix') {
196 $sql_query .= ' ON UPDATE '
197 . $options_array[$_REQUEST['on_update'][$master_field]];
199 $sql_query .= ';';
200 $display_query .= $sql_query . "\n";
202 } // end if... else....
203 } elseif (isset($existrel_innodb[$master_field])) {
204 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
205 . ' DROP FOREIGN KEY '
206 . PMA_backquote($existrel_innodb[$master_field]['constraint']);
207 $sql_query .= ';';
208 $display_query .= $sql_query . "\n";
209 } // end if... else....
211 if (! empty($sql_query)) {
212 $upd_rs = PMA_DBI_try_query($sql_query);
213 $tmp_error = PMA_DBI_getError();
214 if (! empty($tmp_error)) {
215 $seen_error = true;
217 if (substr($tmp_error, 1, 4) == '1216'
218 || substr($tmp_error, 1, 4) == '1452') {
219 PMA_mysqlDie($tmp_error, $sql_query, FALSE, '', FALSE);
220 echo PMA_showMySQLDocu('manual_Table_types', 'InnoDB_foreign_key_constraints') . "\n";
222 if (substr($tmp_error, 1, 4) == '1005') {
223 $message = PMA_Message::warning('strForeignKeyError');
224 $message->addParam($master_field);
225 $message->display();
226 echo PMA_showMySQLDocu('manual_Table_types', 'InnoDB_foreign_key_constraints') . "\n";
228 unset($tmp_error);
229 $sql_query = '';
231 } // end foreach
232 if (!empty($display_query)) {
233 if ($seen_error) {
234 PMA_showMessage($strError, null, 'error');
235 } else {
236 PMA_showMessage($strSuccess, null, 'success');
239 } // end if isset($destination_innodb)
242 // U p d a t e s f o r d i s p l a y f i e l d
244 if ($cfgRelation['displaywork']
245 && isset($display_field)) {
247 if ($disp) {
248 if ($display_field != '') {
249 $upd_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
250 . ' SET display_field = \'' . PMA_sqlAddslashes($display_field) . '\''
251 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
252 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
253 } else {
254 $upd_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
255 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
256 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
258 } elseif ($display_field != '') {
259 $upd_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
260 . '(db_name, table_name, display_field) '
261 . ' VALUES('
262 . '\'' . PMA_sqlAddslashes($db) . '\','
263 . '\'' . PMA_sqlAddslashes($table) . '\','
264 . '\'' . PMA_sqlAddslashes($display_field) . '\')';
267 if (isset($upd_query)) {
268 $upd_rs = PMA_query_as_cu($upd_query);
270 } // end if
272 // If we did an update, refresh our data
273 if (isset($destination) && $cfgRelation['relwork']) {
274 $existrel = PMA_getForeigners($db, $table, '', 'internal');
276 if (isset($destination_innodb) && $tbl_type=='INNODB') {
277 $existrel_innodb = PMA_getForeigners($db, $table, '', 'innodb');
280 if ($cfgRelation['displaywork']) {
281 $disp = PMA_getDisplayField($db, $table);
286 * Dialog
289 // common form
290 echo '<form method="post" action="tbl_relation.php">' . "\n";
291 echo PMA_generate_common_hidden_inputs($db, $table);
294 // relations
296 if ($cfgRelation['relwork'] || $tbl_type=='INNODB') {
297 // To choose relations we first need all tables names in current db
298 // and if PMA version permits and the main table is innodb,
299 // we use SHOW TABLE STATUS because we need to find other InnoDB tables
301 if ($tbl_type=='INNODB') {
302 $tab_query = 'SHOW TABLE STATUS FROM ' . PMA_backquote($db);
303 // [0] of the row is the name
304 // [1] is the type
305 } else {
306 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($db);
308 // [0] of the row is the name
310 $tab_rs = PMA_DBI_query($tab_query, null, PMA_DBI_QUERY_STORE);
311 $selectboxall['nix'] = '--';
312 $selectboxall_innodb['nix'] = '--';
314 while ($curr_table = @PMA_DBI_fetch_row($tab_rs)) {
315 if (($curr_table[0] != $table) && ($curr_table[0] != $cfg['Server']['relation'])) {
316 PMA_DBI_select_db($db);
318 // need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
319 $fi_rs = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($curr_table[0]) . ';', null, PMA_DBI_QUERY_STORE);
320 if ($fi_rs && PMA_DBI_num_rows($fi_rs) > 0) {
321 $seen_a_primary = FALSE;
322 while ($curr_field = PMA_DBI_fetch_assoc($fi_rs)) {
323 if (isset($curr_field['Key_name']) && $curr_field['Key_name'] == 'PRIMARY') {
324 $seen_a_primary = TRUE;
325 $field_full = $db . '.' .$curr_field['Table'] . '.' . $curr_field['Column_name'];
326 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
327 $selectboxall[$field_full] = $field_v;
328 // there could be more than one segment of the primary
329 // so do not break
331 // Please watch here, tbl_type is INNODB but the
332 // resulting value of SHOW KEYS is InnoDB
334 if ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
335 $selectboxall_innodb[$field_full] = $field_v;
338 } elseif (isset($curr_field['Non_unique']) && $curr_field['Non_unique'] == 0 && $seen_a_primary==FALSE) {
339 // if we can't find a primary key we take any unique one
340 // (in fact, we show all segments of unique keys
341 // and all unique keys)
342 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
343 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
344 $selectboxall[$field_full] = $field_v;
345 if ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
346 $selectboxall_innodb[$field_full] = $field_v;
349 // for InnoDB, any index is allowed
350 } elseif ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
351 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
352 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
353 $selectboxall_innodb[$field_full] = $field_v;
355 } // end if
356 } // end while over keys
357 } // end if (PMA_DBI_num_rows)
358 PMA_DBI_free_result($fi_rs);
359 unset($fi_rs);
360 // Mike Beck - 24.07.02: i've been asked to add all keys of the
361 // current table (see bug report #574851)
362 } elseif ($curr_table[0] == $table) {
363 PMA_DBI_select_db($db);
365 // need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
366 $fi_rs = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($curr_table[0]) . ';', null, PMA_DBI_QUERY_STORE);
367 if ($fi_rs && PMA_DBI_num_rows($fi_rs) > 0) {
368 while ($curr_field = PMA_DBI_fetch_assoc($fi_rs)) {
369 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
370 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
371 $selectboxall[$field_full] = $field_v;
372 if ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
373 $selectboxall_innodb[$field_full] = $field_v;
375 } // end while
376 } // end if (PMA_DBI_num_rows)
377 PMA_DBI_free_result($fi_rs);
378 unset($fi_rs);
380 } // end while over tables
382 } // end if
385 // Now find out the columns of our $table
386 // need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
387 $col_rs = PMA_DBI_try_query('SHOW COLUMNS FROM ' . PMA_backquote($table) . ';', null, PMA_DBI_QUERY_STORE);
389 if ($col_rs && PMA_DBI_num_rows($col_rs) > 0) {
390 while ($row = PMA_DBI_fetch_assoc($col_rs)) {
391 $save_row[] = $row;
393 $saved_row_cnt = count($save_row);
395 <fieldset>
396 <legend><?php echo $strLinksTo; ?></legend>
398 <table>
399 <tr><th></th>
400 <?php
401 if ($cfgRelation['relwork']) {
402 echo '<th>' . $strInternalRelations;
403 if ($tbl_type=='INNODB') {
404 echo PMA_showHint($strInternalNotNecessary);
406 echo '</th>';
408 if ($tbl_type == 'INNODB') {
409 echo '<th colspan="2">InnoDB';
410 echo '</th>';
413 </tr>
414 <?php
415 $odd_row = true;
416 for ($i = 0; $i < $saved_row_cnt; $i++) {
417 $myfield = $save_row[$i]['Field'];
419 <tr class="<?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
420 <td align="center">
421 <b><?php echo $save_row[$i]['Field']; ?></b></td>
422 <?php
423 if ($cfgRelation['relwork']) {
425 <td><select name="destination[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
426 <?php
427 // PMA internal relations
428 if (isset($existrel[$myfield])) {
429 $foreign_field = $existrel[$myfield]['foreign_db'] . '.'
430 . $existrel[$myfield]['foreign_table'] . '.'
431 . $existrel[$myfield]['foreign_field'];
432 } else {
433 $foreign_field = FALSE;
435 $seen_key = FALSE;
436 foreach ($selectboxall AS $key => $value) {
437 echo ' '
438 . '<option value="' . htmlspecialchars($key) . '"';
439 if ($foreign_field && $key == $foreign_field) {
440 echo ' selected="selected"';
441 $seen_key = TRUE;
443 echo '>' . $value . '</option>'. "\n";
444 } // end while
446 // if the link defined in relationtable points to a foreign field
447 // that is not a key in the foreign table, we show the link
448 // (will not be shown with an arrow)
449 if ($foreign_field && !$seen_key) {
450 echo ' '
451 .'<option value="' . htmlspecialchars($foreign_field) . '"'
452 .' selected="selected"'
453 .'>' . $foreign_field . '</option>'. "\n";
456 </select>
457 </td>
458 <?php
459 } // end if (internal relations)
461 if ($tbl_type=='INNODB') {
462 echo '<td>';
463 if (!empty($save_row[$i]['Key'])) {
465 <span class="formelement">
466 <select name="destination_innodb[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
467 <?php
468 if (isset($existrel_innodb[$myfield])) {
469 $foreign_field = $existrel_innodb[$myfield]['foreign_db'] . '.'
470 . $existrel_innodb[$myfield]['foreign_table'] . '.'
471 . $existrel_innodb[$myfield]['foreign_field'];
472 } else {
473 $foreign_field = FALSE;
476 $found_foreign_field = FALSE;
477 foreach ($selectboxall_innodb AS $key => $value) {
478 echo ' '
479 . '<option value="' . htmlspecialchars($key) . '"';
480 if ($foreign_field && $key == $foreign_field) {
481 echo ' selected="selected"';
482 $found_foreign_field = TRUE;
484 echo '>' . $value . '</option>'. "\n";
485 } // end while
487 // we did not find the foreign field in the tables of current db,
488 // must be defined in another db so show it to avoid erasing it
489 if (!$found_foreign_field && $foreign_field) {
490 echo ' '
491 . '<option value="' . htmlspecialchars($foreign_field) . '"';
492 echo ' selected="selected"';
493 echo '>' . $foreign_field . '</option>' . "\n";
497 </select>
498 </span>
499 <span class="formelement">
500 <?php
501 PMA_generate_dropdown('ON DELETE',
502 'on_delete[' . htmlspecialchars($save_row[$i]['Field']) . ']',
503 $options_array,
504 isset($existrel_innodb[$myfield]['on_delete']) ? $existrel_innodb[$myfield]['on_delete']: '');
506 echo '</span>' . "\n"
507 .'<span class="formelement">' . "\n";
509 PMA_generate_dropdown('ON UPDATE',
510 'on_update[' . htmlspecialchars($save_row[$i]['Field']) . ']',
511 $options_array,
512 isset($existrel_innodb[$myfield]['on_update']) ? $existrel_innodb[$myfield]['on_update']: '');
513 echo '</span>' . "\n";
514 } else {
515 echo $strNoIndex;
516 } // end if (a key exists)
517 echo ' </td>';
518 } // end if (InnoDB)
520 </tr>
521 <?php
522 } // end for
524 echo ' </table>' . "\n";
525 echo '</fieldset>' . "\n";
527 if ($cfgRelation['displaywork']) {
528 // Get "display_field" infos
529 $disp = PMA_getDisplayField($db, $table);
531 <fieldset>
532 <label><?php echo $strChangeDisplay . ': '; ?></label>
533 <select name="display_field" style="vertical-align: middle">
534 <option value="">---</option>
535 <?php
536 foreach ($save_row AS $row) {
537 echo ' <option value="' . htmlspecialchars($row['Field']) . '"';
538 if (isset($disp) && $row['Field'] == $disp) {
539 echo ' selected="selected"';
541 echo '>' . htmlspecialchars($row['Field']) . '</option>'. "\n";
542 } // end while
544 </select>
545 </fieldset>
546 <?php
547 } // end if (displayworks)
549 <fieldset class="tblFooters">
550 <input type="submit" value="<?php echo $strSave; ?>" />
551 </fieldset>
552 </form>
553 <?php
554 } // end if (we have columns in this table)
557 * Displays the footer
559 require_once './libraries/footer.inc.php';