remove old hint for number of tables
[phpmyadmin/crack.git] / tbl_relation.php
blob4d13362e96089f17f0068e66834953ba7112d8e8
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 if (PMA_MYSQL_INT_VERSION >= 40013) {
175 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
176 . ' DROP FOREIGN KEY '
177 . PMA_backquote($existrel_innodb[$master_field]['constraint']) . ';';
179 // I tried to send both in one query but it failed
180 $upd_rs = PMA_DBI_query($sql_query);
181 $display_query .= $sql_query . "\n";
184 // add another
185 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
186 . ' ADD FOREIGN KEY ('
187 . PMA_backquote($master_field) . ')'
188 . ' REFERENCES '
189 . PMA_backquote($foreign_db) . '.'
190 . PMA_backquote($foreign_table) . '('
191 . PMA_backquote($foreign_field) . ')';
193 if ($_REQUEST['on_delete'][$master_field] != 'nix') {
194 $sql_query .= ' ON DELETE '
195 . $options_array[$_REQUEST['on_delete'][$master_field]];
197 if ($_REQUEST['on_update'][$master_field] != 'nix') {
198 $sql_query .= ' ON UPDATE '
199 . $options_array[$_REQUEST['on_update'][$master_field]];
201 $sql_query .= ';';
202 $display_query .= $sql_query . "\n";
204 } // end if... else....
205 } elseif (isset($existrel_innodb[$master_field])) {
206 if (PMA_MYSQL_INT_VERSION >= 40013) {
207 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
208 . ' DROP FOREIGN KEY '
209 . PMA_backquote($existrel_innodb[$master_field]['constraint']);
210 $sql_query .= ';';
211 $display_query .= $sql_query . "\n";
213 } // end if... else....
215 if (! empty($sql_query)) {
216 $upd_rs = PMA_DBI_try_query($sql_query);
217 $tmp_error = PMA_DBI_getError();
218 if (! empty($tmp_error)) {
219 $seen_error = true;
221 if (substr($tmp_error, 1, 4) == '1216'
222 || substr($tmp_error, 1, 4) == '1452') {
223 PMA_mysqlDie($tmp_error, $sql_query, FALSE, '', FALSE);
224 echo PMA_showMySQLDocu('manual_Table_types', 'InnoDB_foreign_key_constraints') . "\n";
226 if (substr($tmp_error, 1, 4) == '1005') {
227 echo '<p class="warning">' . $strForeignKeyError . ' : ' . $master_field
228 .'</p>' . PMA_showMySQLDocu('manual_Table_types', 'InnoDB_foreign_key_constraints') . "\n";
230 unset($tmp_error);
231 $sql_query = '';
233 } // end foreach
234 if (!empty($display_query)) {
235 if ($seen_error) {
236 PMA_showMessage($strError);
237 } else {
238 PMA_showMessage($strSuccess);
241 } // end if isset($destination_innodb)
244 // U p d a t e s f o r d i s p l a y f i e l d
246 if ($cfgRelation['displaywork']
247 && isset($display_field)) {
249 if ($disp) {
250 if ($display_field != '') {
251 $upd_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
252 . ' SET display_field = \'' . PMA_sqlAddslashes($display_field) . '\''
253 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
254 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
255 } else {
256 $upd_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
257 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
258 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
260 } elseif ($display_field != '') {
261 $upd_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
262 . '(db_name, table_name, display_field) '
263 . ' VALUES('
264 . '\'' . PMA_sqlAddslashes($db) . '\','
265 . '\'' . PMA_sqlAddslashes($table) . '\','
266 . '\'' . PMA_sqlAddslashes($display_field) . '\')';
269 if (isset($upd_query)) {
270 $upd_rs = PMA_query_as_cu($upd_query);
272 } // end if
274 // If we did an update, refresh our data
275 if (isset($destination) && $cfgRelation['relwork']) {
276 $existrel = PMA_getForeigners($db, $table, '', 'internal');
278 if (isset($destination_innodb) && $tbl_type=='INNODB') {
279 $existrel_innodb = PMA_getForeigners($db, $table, '', 'innodb');
282 if ($cfgRelation['displaywork']) {
283 $disp = PMA_getDisplayField($db, $table);
288 * Dialog
291 // common form
292 echo '<form method="post" action="tbl_relation.php">' . "\n";
293 echo PMA_generate_common_hidden_inputs($db, $table);
296 // relations
298 if ($cfgRelation['relwork'] || $tbl_type=='INNODB') {
299 // To choose relations we first need all tables names in current db
300 // and if PMA version permits and the main table is innodb,
301 // we use SHOW TABLE STATUS because we need to find other InnoDB tables
303 if ($tbl_type=='INNODB') {
304 $tab_query = 'SHOW TABLE STATUS FROM ' . PMA_backquote($db);
305 // [0] of the row is the name
306 // [1] is the type
307 } else {
308 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($db);
310 // [0] of the row is the name
312 $tab_rs = PMA_DBI_query($tab_query, null, PMA_DBI_QUERY_STORE);
313 $selectboxall['nix'] = '--';
314 $selectboxall_innodb['nix'] = '--';
316 while ($curr_table = @PMA_DBI_fetch_row($tab_rs)) {
317 if (($curr_table[0] != $table) && ($curr_table[0] != $cfg['Server']['relation'])) {
318 PMA_DBI_select_db($db);
320 // need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
321 $fi_rs = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($curr_table[0]) . ';', null, PMA_DBI_QUERY_STORE);
322 if ($fi_rs && PMA_DBI_num_rows($fi_rs) > 0) {
323 $seen_a_primary = FALSE;
324 while ($curr_field = PMA_DBI_fetch_assoc($fi_rs)) {
325 if (isset($curr_field['Key_name']) && $curr_field['Key_name'] == 'PRIMARY') {
326 $seen_a_primary = TRUE;
327 $field_full = $db . '.' .$curr_field['Table'] . '.' . $curr_field['Column_name'];
328 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
329 $selectboxall[$field_full] = $field_v;
330 // there could be more than one segment of the primary
331 // so do not break
333 // Please watch here, tbl_type is INNODB but the
334 // resulting value of SHOW KEYS is InnoDB
336 if ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
337 $selectboxall_innodb[$field_full] = $field_v;
340 } elseif (isset($curr_field['Non_unique']) && $curr_field['Non_unique'] == 0 && $seen_a_primary==FALSE) {
341 // if we can't find a primary key we take any unique one
342 // (in fact, we show all segments of unique keys
343 // and all unique keys)
344 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
345 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
346 $selectboxall[$field_full] = $field_v;
347 if ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
348 $selectboxall_innodb[$field_full] = $field_v;
351 // for InnoDB, any index is allowed
352 } elseif ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
353 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
354 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
355 $selectboxall_innodb[$field_full] = $field_v;
357 } // end if
358 } // end while over keys
359 } // end if (PMA_DBI_num_rows)
360 PMA_DBI_free_result($fi_rs);
361 unset($fi_rs);
362 // Mike Beck - 24.07.02: i've been asked to add all keys of the
363 // current table (see bug report #574851)
364 } elseif ($curr_table[0] == $table) {
365 PMA_DBI_select_db($db);
367 // need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
368 $fi_rs = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($curr_table[0]) . ';', null, PMA_DBI_QUERY_STORE);
369 if ($fi_rs && PMA_DBI_num_rows($fi_rs) > 0) {
370 while ($curr_field = PMA_DBI_fetch_assoc($fi_rs)) {
371 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
372 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
373 $selectboxall[$field_full] = $field_v;
374 if ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
375 $selectboxall_innodb[$field_full] = $field_v;
377 } // end while
378 } // end if (PMA_DBI_num_rows)
379 PMA_DBI_free_result($fi_rs);
380 unset($fi_rs);
382 } // end while over tables
384 } // end if
387 // Now find out the columns of our $table
388 // need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
389 $col_rs = PMA_DBI_try_query('SHOW COLUMNS FROM ' . PMA_backquote($table) . ';', null, PMA_DBI_QUERY_STORE);
391 if ($col_rs && PMA_DBI_num_rows($col_rs) > 0) {
392 while ($row = PMA_DBI_fetch_assoc($col_rs)) {
393 $save_row[] = $row;
395 $saved_row_cnt = count($save_row);
397 <fieldset>
398 <legend><?php echo $strLinksTo; ?></legend>
400 <table>
401 <tr><th></th>
402 <?php
403 if ($cfgRelation['relwork']) {
404 echo '<th>' . $strInternalRelations;
405 if ($tbl_type=='INNODB') {
406 echo PMA_showHint($strInternalNotNecessary);
408 echo '</th>';
410 if ($tbl_type=='INNODB') {
411 echo '<th colspan="2">InnoDB';
412 if (PMA_MYSQL_INT_VERSION < 40013) {
413 echo '(**)';
415 echo '</th>';
418 </tr>
419 <?php
420 $odd_row = true;
421 for ($i = 0; $i < $saved_row_cnt; $i++) {
422 $myfield = $save_row[$i]['Field'];
424 <tr class="<?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
425 <td align="center">
426 <b><?php echo $save_row[$i]['Field']; ?></b></td>
427 <?php
428 if ($cfgRelation['relwork']) {
430 <td><select name="destination[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
431 <?php
432 // PMA internal relations
433 if (isset($existrel[$myfield])) {
434 $foreign_field = $existrel[$myfield]['foreign_db'] . '.'
435 . $existrel[$myfield]['foreign_table'] . '.'
436 . $existrel[$myfield]['foreign_field'];
437 } else {
438 $foreign_field = FALSE;
440 $seen_key = FALSE;
441 foreach ($selectboxall AS $key => $value) {
442 echo ' '
443 . '<option value="' . htmlspecialchars($key) . '"';
444 if ($foreign_field && $key == $foreign_field) {
445 echo ' selected="selected"';
446 $seen_key = TRUE;
448 echo '>' . $value . '</option>'. "\n";
449 } // end while
451 // if the link defined in relationtable points to a foreign field
452 // that is not a key in the foreign table, we show the link
453 // (will not be shown with an arrow)
454 if ($foreign_field && !$seen_key) {
455 echo ' '
456 .'<option value="' . htmlspecialchars($foreign_field) . '"'
457 .' selected="selected"'
458 .'>' . $foreign_field . '</option>'. "\n";
461 </select>
462 </td>
463 <?php
464 } // end if (internal relations)
466 if ($tbl_type=='INNODB') {
467 echo '<td>';
468 if (!empty($save_row[$i]['Key'])) {
470 <span class="formelement">
471 <select name="destination_innodb[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
472 <?php
473 if (isset($existrel_innodb[$myfield])) {
474 $foreign_field = $existrel_innodb[$myfield]['foreign_db'] . '.'
475 . $existrel_innodb[$myfield]['foreign_table'] . '.'
476 . $existrel_innodb[$myfield]['foreign_field'];
477 } else {
478 $foreign_field = FALSE;
481 $found_foreign_field = FALSE;
482 foreach ($selectboxall_innodb AS $key => $value) {
483 echo ' '
484 . '<option value="' . htmlspecialchars($key) . '"';
485 if ($foreign_field && $key == $foreign_field) {
486 echo ' selected="selected"';
487 $found_foreign_field = TRUE;
489 echo '>' . $value . '</option>'. "\n";
490 } // end while
492 // we did not find the foreign field in the tables of current db,
493 // must be defined in another db so show it to avoid erasing it
494 if (!$found_foreign_field && $foreign_field) {
495 echo ' '
496 . '<option value="' . htmlspecialchars($foreign_field) . '"';
497 echo ' selected="selected"';
498 echo '>' . $foreign_field . '</option>' . "\n";
502 </select>
503 </span>
504 <span class="formelement">
505 <?php
506 PMA_generate_dropdown('ON DELETE',
507 'on_delete[' . htmlspecialchars($save_row[$i]['Field']) . ']',
508 $options_array,
509 isset($existrel_innodb[$myfield]['on_delete']) ? $existrel_innodb[$myfield]['on_delete']: '');
511 echo '</span>' . "\n"
512 .'<span class="formelement">' . "\n";
514 PMA_generate_dropdown('ON UPDATE',
515 'on_update[' . htmlspecialchars($save_row[$i]['Field']) . ']',
516 $options_array,
517 isset($existrel_innodb[$myfield]['on_update']) ? $existrel_innodb[$myfield]['on_update']: '');
518 echo '</span>' . "\n";
519 } else {
520 echo $strNoIndex;
521 } // end if (a key exists)
522 echo ' </td>';
523 } // end if (InnoDB)
525 </tr>
526 <?php
527 } // end for
529 echo ' </table>' . "\n";
530 echo '</fieldset>' . "\n";
532 if ($cfgRelation['displaywork']) {
533 // Get "display_field" infos
534 $disp = PMA_getDisplayField($db, $table);
536 <fieldset>
537 <label><?php echo $strChangeDisplay . ': '; ?></label>
538 <select name="display_field" style="vertical-align: middle">
539 <option value="">---</option>
540 <?php
541 foreach ($save_row AS $row) {
542 echo ' <option value="' . htmlspecialchars($row['Field']) . '"';
543 if (isset($disp) && $row['Field'] == $disp) {
544 echo ' selected="selected"';
546 echo '>' . htmlspecialchars($row['Field']) . '</option>'. "\n";
547 } // end while
549 </select>
550 </fieldset>
551 <?php
552 } // end if (displayworks)
554 <fieldset class="tblFooters">
555 <input type="submit" value="<?php echo $strSave; ?>" />
556 </fieldset>
557 </form>
558 <?php
559 } // end if (we have columns in this table)
561 if ($tbl_type === 'INNODB' && PMA_MYSQL_INT_VERSION < 40013) {
562 echo '<div class="warning">'
563 .'** ' . sprintf($strUpgrade, 'MySQL', '4.0.13')
564 .'</div>';
568 * Displays the footer
570 require_once './libraries/footer.inc.php';