Readd snapshot support.
[phpmyadmin/crack.git] / tbl_relation.php
blobb46b3447ae32cb32328e0df85b67b21625dd7dd7
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 /**
6 * Gets some core libraries
7 */
8 require_once('./libraries/common.lib.php');
9 require_once('./libraries/tbl_properties_common.php');
10 $url_query .= '&amp;goto=tbl_properties.php';
13 /**
14 * Gets tables informations
16 require_once('./libraries/tbl_properties_table_info.inc.php');
18 // Note: in libraries/tbl_properties_links.inc.php we get and display the table comment.
19 // For InnoDB, this comment contains the REFER information but any update
20 // has not been done yet (will be done in tbl_relation.php later).
21 $avoid_show_comment = TRUE;
23 /**
24 * Displays top menu links
26 require_once('./libraries/tbl_properties_links.inc.php');
28 require_once('./libraries/relation.lib.php');
30 $options_array = array('CASCADE' => 'CASCADE', 'SET_NULL' => 'SET NULL', 'NO_ACTION' => 'NO ACTION', 'RESTRICT' => 'RESTRICT');
32 /**
33 * Generate dropdown choices
35 * @param string Message to display
36 * @param string Name of the <select> field
37 * @param array Choices for dropdown
38 * @return string The existing value (for selected)
40 * @access public
42 function PMA_generate_dropdown($dropdown_question, $radio_name, $choices, $selected_value)
44 echo $dropdown_question . '&nbsp;&nbsp;';
46 echo '<select name="' . $radio_name . '">' . "\n";
47 echo '<option value="nix">--</option>' . "\n";
49 foreach ($choices AS $one_value => $one_label) {
50 echo '<option value="' . $one_value . '"';
51 if ($selected_value == $one_value) {
52 echo ' selected="selected" ';
54 echo '>' . $one_label . '</option>' . "\n";
56 echo '</select>' . "\n";
57 echo "\n";
61 /**
62 * Gets the relation settings
64 $cfgRelation = PMA_getRelationsParam();
67 /**
68 * Updates
71 // ensure we are positionned to our current db (since the previous reading
72 // of relations makes pmadb the current one, maybe depending on the MySQL version)
73 PMA_DBI_select_db($db);
75 if ($cfgRelation['relwork']) {
76 $existrel = PMA_getForeigners($db, $table, '', 'internal');
78 if ($tbl_type=='INNODB') {
79 $existrel_innodb = PMA_getForeigners($db, $table, '', 'innodb');
81 if ($cfgRelation['displaywork']) {
82 $disp = PMA_getDisplayField($db, $table);
85 // 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
86 if (isset($destination) && $cfgRelation['relwork']) {
88 foreach ($destination AS $master_field => $foreign_string) {
89 if ($foreign_string != 'nix') {
90 list($foreign_db, $foreign_table, $foreign_field) = explode('.', $foreign_string);
91 if (!isset($existrel[$master_field])) {
92 $upd_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
93 . '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
94 . ' values('
95 . '\'' . PMA_sqlAddslashes($db) . '\', '
96 . '\'' . PMA_sqlAddslashes($table) . '\', '
97 . '\'' . PMA_sqlAddslashes($master_field) . '\', '
98 . '\'' . PMA_sqlAddslashes($foreign_db) . '\', '
99 . '\'' . PMA_sqlAddslashes($foreign_table) . '\','
100 . '\'' . PMA_sqlAddslashes($foreign_field) . '\')';
101 } elseif ($existrel[$master_field]['foreign_db'] . '.' .$existrel[$master_field]['foreign_table'] . '.' . $existrel[$master_field]['foreign_field'] != $foreign_string) {
102 $upd_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation']) . ' SET'
103 . ' foreign_db = \'' . PMA_sqlAddslashes($foreign_db) . '\', '
104 . ' foreign_table = \'' . PMA_sqlAddslashes($foreign_table) . '\', '
105 . ' foreign_field = \'' . PMA_sqlAddslashes($foreign_field) . '\' '
106 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
107 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
108 . ' AND master_field = \'' . PMA_sqlAddslashes($master_field) . '\'';
109 } // end if... else....
110 } elseif (isset($existrel[$master_field])) {
111 $upd_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
112 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
113 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
114 . ' AND master_field = \'' . PMA_sqlAddslashes($master_field) . '\'';
115 } // end if... else....
116 if (isset($upd_query)) {
117 $upd_rs = PMA_query_as_cu($upd_query);
118 unset($upd_query);
120 } // end while
121 } // end if (updates for internal relations)
123 // u p d a t e s f o r I n n o D B
124 // ( for now, one index name only; we keep the definitions if the
125 // foreign db is not the same)
126 if (isset($_REQUEST['destination_innodb'])) {
127 foreach ($_REQUEST['destination_innodb'] as $master_field => $foreign_string) {
128 if ($foreign_string != 'nix') {
129 list($foreign_db, $foreign_table, $foreign_field) = explode('.', $foreign_string);
130 if (!isset($existrel_innodb[$master_field])) {
131 // no key defined for this field
133 // The next few lines are repeated below, so they
134 // could be put in an include file
135 // Note: I tried to enclose the db and table name with
136 // backquotes but MySQL 4.0.16 did not like the syntax
137 // (for example: `base2`.`table1` )
139 $upd_query = 'ALTER TABLE ' . PMA_backquote($table)
140 . ' ADD FOREIGN KEY ('
141 . PMA_backquote($master_field) . ')'
142 . ' REFERENCES '
143 . PMA_backquote($foreign_db) . '.'
144 . PMA_backquote($foreign_table) . '('
145 . PMA_backquote($foreign_field) . ')';
147 if ($_REQUEST['on_delete'][$master_field] != 'nix') {
148 $upd_query .= ' ON DELETE ' . $options_array[$_REQUEST['on_delete'][$master_field]];
150 if ($_REQUEST['on_update'][$master_field] != 'nix') {
151 $upd_query .= ' ON UPDATE ' . $options_array[$_REQUEST['on_update'][$master_field]];
154 // end repeated code
156 } elseif (($existrel_innodb[$master_field]['foreign_db'] . '.' .$existrel_innodb[$master_field]['foreign_table'] . '.' . $existrel_innodb[$master_field]['foreign_field'] != $foreign_string)
157 || ( $_REQUEST['on_delete'][$master_field] != (!empty($existrel_innodb[$master_field]['on_delete']) ? $existrel_innodb[$master_field]['on_delete'] : ''))
158 || ( $_REQUEST['on_update'][$master_field] != (!empty($existrel_innodb[$master_field]['on_update']) ? $existrel_innodb[$master_field]['on_update'] : ''))
160 // another foreign key is already defined for this field
161 // or
162 // an option has been changed for ON DELETE or ON UPDATE
164 // remove existing key
165 if (PMA_MYSQL_INT_VERSION >= 40013) {
166 $upd_query = 'ALTER TABLE ' . PMA_backquote($table)
167 . ' DROP FOREIGN KEY '
168 . PMA_backquote($existrel_innodb[$master_field]['constraint']);
170 // I tried to send both in one query but it failed
171 $upd_rs = PMA_DBI_query($upd_query);
174 // add another
175 $upd_query = 'ALTER TABLE ' . PMA_backquote($table)
176 . ' ADD FOREIGN KEY ('
177 . PMA_backquote($master_field) . ')'
178 . ' REFERENCES '
179 . PMA_backquote($foreign_db) . '.'
180 . PMA_backquote($foreign_table) . '('
181 . PMA_backquote($foreign_field) . ')';
183 if ($_REQUEST['on_delete'][$master_field] != 'nix') {
184 $upd_query .= ' ON DELETE '
185 . $options_array[$_REQUEST['on_delete'][$master_field]];
187 if ($_REQUEST['on_update'][$master_field] != 'nix') {
188 $upd_query .= ' ON UPDATE '
189 . $options_array[$_REQUEST['on_update'][$master_field]];
192 } // end if... else....
193 } elseif (isset($existrel_innodb[$master_field])) {
194 if (PMA_MYSQL_INT_VERSION >= 40013) {
195 $upd_query = 'ALTER TABLE ' . PMA_backquote($table)
196 . ' DROP FOREIGN KEY '
197 . PMA_backquote($existrel_innodb[$master_field]['constraint']);
199 } // end if... else....
201 if (isset($upd_query)) {
202 $upd_rs = PMA_DBI_try_query($upd_query);
203 $tmp_error = PMA_DBI_getError();
204 if (substr($tmp_error, 1, 4) == '1216'
205 || substr($tmp_error, 1, 4) == '1452') {
206 PMA_mysqlDie($tmp_error, $upd_query, FALSE, '', FALSE);
207 echo PMA_showMySQLDocu('manual_Table_types', 'InnoDB_foreign_key_constraints') . "\n";
209 if (substr($tmp_error, 1, 4) == '1005') {
210 echo '<p class="warning">' . $strNoIndex . ' (' . $master_field
211 .')</p>' . PMA_showMySQLDocu('manual_Table_types', 'InnoDB_foreign_key_constraints') . "\n";
213 unset($upd_query, $tmp_error);
215 } // end while
216 } // end if isset($destination_innodb)
219 // U p d a t e s f o r d i s p l a y f i e l d
221 if ($cfgRelation['displaywork']
222 && isset($display_field)) {
224 if ($disp) {
225 if ($display_field != '') {
226 $upd_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
227 . ' SET display_field = \'' . PMA_sqlAddslashes($display_field) . '\''
228 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
229 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
230 } else {
231 $upd_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
232 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
233 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
235 } elseif ($display_field != '') {
236 $upd_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
237 . '(db_name, table_name, display_field) '
238 . ' VALUES('
239 . '\'' . PMA_sqlAddslashes($db) . '\','
240 . '\'' . PMA_sqlAddslashes($table) . '\','
241 . '\'' . PMA_sqlAddslashes($display_field) . '\')';
244 if (isset($upd_query)) {
245 $upd_rs = PMA_query_as_cu($upd_query);
247 } // end if
249 // If we did an update, refresh our data
250 if (isset($destination) && $cfgRelation['relwork']) {
251 $existrel = PMA_getForeigners($db, $table, '', 'internal');
253 if (isset($destination_innodb) && $tbl_type=='INNODB') {
254 $existrel_innodb = PMA_getForeigners($db, $table, '', 'innodb');
257 if ($cfgRelation['displaywork']) {
258 $disp = PMA_getDisplayField($db, $table);
263 * Dialog
266 // common form
267 echo '<form method="post" action="tbl_relation.php">' . "\n";
268 echo PMA_generate_common_hidden_inputs($db, $table);
271 // relations
273 if ($cfgRelation['relwork'] || $tbl_type=='INNODB') {
274 // To choose relations we first need all tables names in current db
275 // and if PMA version permits and the main table is innodb,
276 // we use SHOW TABLE STATUS because we need to find other InnoDB tables
278 if ($tbl_type=='INNODB') {
279 $tab_query = 'SHOW TABLE STATUS FROM ' . PMA_backquote($db);
280 // [0] of the row is the name
281 // [1] is the type
282 } else {
283 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($db);
285 // [0] of the row is the name
287 $tab_rs = PMA_DBI_query($tab_query, null, PMA_DBI_QUERY_STORE);
288 $selectboxall['nix'] = '--';
289 $selectboxall_innodb['nix'] = '--';
291 while ($curr_table = @PMA_DBI_fetch_row($tab_rs)) {
292 if (($curr_table[0] != $table) && ($curr_table[0] != $cfg['Server']['relation'])) {
293 PMA_DBI_select_db($db);
295 // need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
296 $fi_rs = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($curr_table[0]) . ';', null, PMA_DBI_QUERY_STORE);
297 if ($fi_rs && PMA_DBI_num_rows($fi_rs) > 0) {
298 $seen_a_primary = FALSE;
299 while ($curr_field = PMA_DBI_fetch_assoc($fi_rs)) {
300 if (isset($curr_field['Key_name']) && $curr_field['Key_name'] == 'PRIMARY') {
301 $seen_a_primary = TRUE;
302 $field_full = $db . '.' .$curr_field['Table'] . '.' . $curr_field['Column_name'];
303 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
304 $selectboxall[$field_full] = $field_v;
305 // there could be more than one segment of the primary
306 // so do not break
308 // Please watch here, tbl_type is INNODB but the
309 // resulting value of SHOW KEYS is InnoDB
311 if ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
312 $selectboxall_innodb[$field_full] = $field_v;
315 } elseif (isset($curr_field['Non_unique']) && $curr_field['Non_unique'] == 0 && $seen_a_primary==FALSE) {
316 // if we can't find a primary key we take any unique one
317 // (in fact, we show all segments of unique keys
318 // and all unique keys)
319 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
320 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
321 $selectboxall[$field_full] = $field_v;
322 if ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
323 $selectboxall_innodb[$field_full] = $field_v;
326 // for InnoDB, any index is allowed
327 } elseif ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
328 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
329 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
330 $selectboxall_innodb[$field_full] = $field_v;
332 } // end if
333 } // end while over keys
334 } // end if (PMA_DBI_num_rows)
335 PMA_DBI_free_result($fi_rs);
336 unset($fi_rs);
337 // Mike Beck - 24.07.02: i've been asked to add all keys of the
338 // current table (see bug report #574851)
339 } elseif ($curr_table[0] == $table) {
340 PMA_DBI_select_db($db);
342 // need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
343 $fi_rs = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($curr_table[0]) . ';', null, PMA_DBI_QUERY_STORE);
344 if ($fi_rs && PMA_DBI_num_rows($fi_rs) > 0) {
345 while ($curr_field = PMA_DBI_fetch_assoc($fi_rs)) {
346 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
347 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
348 $selectboxall[$field_full] = $field_v;
349 if ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
350 $selectboxall_innodb[$field_full] = $field_v;
352 } // end while
353 } // end if (PMA_DBI_num_rows)
354 PMA_DBI_free_result($fi_rs);
355 unset($fi_rs);
357 } // end while over tables
359 } // end if
362 // Now find out the columns of our $table
363 // need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
364 $col_rs = PMA_DBI_try_query('SHOW COLUMNS FROM ' . PMA_backquote($table) . ';', null, PMA_DBI_QUERY_STORE);
366 if ($col_rs && PMA_DBI_num_rows($col_rs) > 0) {
367 while ($row = PMA_DBI_fetch_assoc($col_rs)) {
368 $save_row[] = $row;
370 $saved_row_cnt = count($save_row);
372 <fieldset>
373 <legend><?php echo $strLinksTo; ?></legend>
375 <table>
376 <tr><th></th>
377 <?php
378 if ( $cfgRelation['relwork'] ) {
379 echo '<th>' . $strInternalRelations;
380 if ($tbl_type=='INNODB') {
381 echo PMA_showHint( $strInternalNotNecessary );
383 echo '</th>';
385 if ( $tbl_type=='INNODB' ) {
386 echo '<th colspan="2">InnoDB';
387 if (PMA_MYSQL_INT_VERSION < 40013) {
388 echo '(**)';
390 echo '</th>';
393 </tr>
394 <?php
395 $odd_row = true;
396 for ($i = 0; $i < $saved_row_cnt; $i++) {
397 $myfield = $save_row[$i]['Field'];
399 <tr class="<?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
400 <td align="center">
401 <b><?php echo $save_row[$i]['Field']; ?></b></td>
402 <?php
403 if ($cfgRelation['relwork']) {
405 <td><select name="destination[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
406 <?php
407 // PMA internal relations
408 if (isset($existrel[$myfield])) {
409 $foreign_field = $existrel[$myfield]['foreign_db'] . '.'
410 . $existrel[$myfield]['foreign_table'] . '.'
411 . $existrel[$myfield]['foreign_field'];
412 } else {
413 $foreign_field = FALSE;
415 $seen_key = FALSE;
416 foreach ($selectboxall AS $key => $value) {
417 echo ' '
418 . '<option value="' . htmlspecialchars($key) . '"';
419 if ($foreign_field && $key == $foreign_field) {
420 echo ' selected="selected"';
421 $seen_key = TRUE;
423 echo '>' . $value . '</option>'. "\n";
424 } // end while
426 // if the link defined in relationtable points to a foreign field
427 // that is not a key in the foreign table, we show the link
428 // (will not be shown with an arrow)
429 if ($foreign_field && !$seen_key) {
430 echo ' '
431 .'<option value="' . htmlspecialchars($foreign_field) . '"'
432 .' selected="selected"'
433 .'>' . $foreign_field . '</option>'. "\n";
436 </select>
437 </td>
438 <?php
439 } // end if (internal relations)
441 if ($tbl_type=='INNODB') {
442 echo '<td>';
443 if (!empty($save_row[$i]['Key'])) {
445 <span class="formelement">
446 <select name="destination_innodb[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
447 <?php
448 if (isset($existrel_innodb[$myfield])) {
449 $foreign_field = $existrel_innodb[$myfield]['foreign_db'] . '.'
450 . $existrel_innodb[$myfield]['foreign_table'] . '.'
451 . $existrel_innodb[$myfield]['foreign_field'];
452 } else {
453 $foreign_field = FALSE;
456 $found_foreign_field = FALSE;
457 foreach ($selectboxall_innodb AS $key => $value) {
458 echo ' '
459 . '<option value="' . htmlspecialchars($key) . '"';
460 if ($foreign_field && $key == $foreign_field) {
461 echo ' selected="selected"';
462 $found_foreign_field = TRUE;
464 echo '>' . $value . '</option>'. "\n";
465 } // end while
467 // we did not find the foreign field in the tables of current db,
468 // must be defined in another db so show it to avoid erasing it
469 if (!$found_foreign_field && $foreign_field) {
470 echo ' '
471 . '<option value="' . htmlspecialchars($foreign_field) . '"';
472 echo ' selected="selected"';
473 echo '>' . $foreign_field . '</option>' . "\n";
477 </select>
478 </span>
479 <span class="formelement">
480 <?php
481 PMA_generate_dropdown('ON DELETE',
482 'on_delete[' . htmlspecialchars($save_row[$i]['Field']) . ']',
483 $options_array,
484 isset($existrel_innodb[$myfield]['on_delete']) ? $existrel_innodb[$myfield]['on_delete']: '' );
486 echo '</span>' . "\n"
487 .'<span class="formelement">' . "\n";
489 PMA_generate_dropdown('ON UPDATE',
490 'on_update[' . htmlspecialchars($save_row[$i]['Field']) . ']',
491 $options_array,
492 isset($existrel_innodb[$myfield]['on_update']) ? $existrel_innodb[$myfield]['on_update']: '' );
493 echo '</span>' . "\n";
494 } else {
495 echo $strNoIndex;
496 } // end if (a key exists)
497 echo ' </td>';
498 } // end if (InnoDB)
500 </tr>
501 <?php
502 } // end for
504 echo ' </table>' . "\n";
505 echo '</fieldset>' . "\n";
507 if ($cfgRelation['displaywork']) {
508 // Get "display_field" infos
509 $disp = PMA_getDisplayField($db, $table);
511 <fieldset>
512 <label><?php echo $strChangeDisplay . ': '; ?></label>
513 <select name="display_field" style="vertical-align: middle">
514 <option value="">---</option>
515 <?php
516 foreach ($save_row AS $row) {
517 echo ' <option value="' . htmlspecialchars($row['Field']) . '"';
518 if (isset($disp) && $row['Field'] == $disp) {
519 echo ' selected="selected"';
521 echo '>' . htmlspecialchars($row['Field']) . '</option>'. "\n";
522 } // end while
524 </select>
525 </fieldset>
526 <?php
527 } // end if (displayworks)
529 <fieldset class="tblFooters">
530 <input type="submit" value="<?php echo $strSave; ?>" />
531 </fieldset>
532 </form>
533 <?php
534 } // end if (we have columns in this table)
536 if ( $tbl_type === 'INNODB' && PMA_MYSQL_INT_VERSION < 40013 ) {
537 echo '<div class="warning">'
538 .'** ' . sprintf($strUpgrade, 'MySQL', '4.0.13')
539 .'</div>';
543 * Displays the footer
545 require_once('./libraries/footer.inc.php');