sorry, wrong version checked in
[phpmyadmin/arisferyanto.git] / tbl_relation.php
blob6962d9184aa6b0b3ae7056929196356b248fec2e
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('./tbl_properties_common.php');
10 $url_query .= '&amp;goto=tbl_properties.php';
13 /**
14 * Gets tables informations
16 require_once('./tbl_properties_table_info.php');
18 // Note: in tbl_properties_links.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('./tbl_properties_links.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) {
43 global $font_smallest;
45 echo $dropdown_question . '&nbsp;&nbsp;';
47 //echo '<select name="' . $radio_name . '" style="font-size: ' . $font_smallest . '">' . "\n";
48 //echo '<option value="nix" style="font-size: ' . $font_smallest . '" >--</option>' . "\n";
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 ' style="font-size: ' . $font_smallest . '">'
58 echo '>' . $one_label . '</option>' . "\n";
60 echo '</select>' . "\n";
61 echo "\n";
65 /**
66 * Gets the relation settings
68 $cfgRelation = PMA_getRelationsParam();
71 /**
72 * Updates
75 // ensure we are positionned to our current db (since the previous reading
76 // of relations makes pmadb the current one, maybe depending on the MySQL version)
77 PMA_DBI_select_db($db);
79 if ($cfgRelation['relwork']) {
80 $existrel = PMA_getForeigners($db, $table, '', 'internal');
82 if ($tbl_type=='INNODB') {
83 $existrel_innodb = PMA_getForeigners($db, $table, '', 'innodb');
85 if ($cfgRelation['displaywork']) {
86 $disp = PMA_getDisplayField($db, $table);
89 // 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
90 if (isset($destination) && $cfgRelation['relwork']) {
92 foreach ($destination AS $master_field => $foreign_string) {
93 if ($foreign_string != 'nix') {
94 list($foreign_db, $foreign_table, $foreign_field) = explode('.', $foreign_string);
95 if (!isset($existrel[$master_field])) {
96 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['relation'])
97 . '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
98 . ' values('
99 . '\'' . PMA_sqlAddslashes($db) . '\', '
100 . '\'' . PMA_sqlAddslashes($table) . '\', '
101 . '\'' . PMA_sqlAddslashes($master_field) . '\', '
102 . '\'' . PMA_sqlAddslashes($foreign_db) . '\', '
103 . '\'' . PMA_sqlAddslashes($foreign_table) . '\','
104 . '\'' . PMA_sqlAddslashes($foreign_field) . '\')';
105 } elseif ($existrel[$master_field]['foreign_db'] . '.' .$existrel[$master_field]['foreign_table'] . '.' . $existrel[$master_field]['foreign_field'] != $foreign_string) {
106 $upd_query = 'UPDATE ' . PMA_backquote($cfgRelation['relation']) . ' SET'
107 . ' foreign_db = \'' . PMA_sqlAddslashes($foreign_db) . '\', '
108 . ' foreign_table = \'' . PMA_sqlAddslashes($foreign_table) . '\', '
109 . ' foreign_field = \'' . PMA_sqlAddslashes($foreign_field) . '\' '
110 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
111 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
112 . ' AND master_field = \'' . PMA_sqlAddslashes($master_field) . '\'';
113 } // end if... else....
114 } elseif (isset($existrel[$master_field])) {
115 $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['relation'])
116 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
117 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
118 . ' AND master_field = \'' . PMA_sqlAddslashes($master_field) . '\'';
119 } // end if... else....
120 if (isset($upd_query)) {
121 $upd_rs = PMA_query_as_cu($upd_query);
122 unset($upd_query);
124 } // end while
125 } // end if (updates for internal relations)
127 // u p d a t e s f o r I n n o D B
128 // ( for now, one index name only; we keep the definitions if the
129 // foreign db is not the same)
130 if (isset($destination_innodb)) {
131 foreach ($destination_innodb AS $master_field => $foreign_string) {
132 if ($foreign_string != 'nix') {
133 list($foreign_db, $foreign_table, $foreign_field) = explode('.', $foreign_string);
134 if (!isset($existrel_innodb[$master_field])) {
135 // no key defined for this field
137 // The next few lines are repeated below, so they
138 // could be put in an include file
139 // Note: I tried to enclose the db and table name with
140 // backquotes but MySQL 4.0.16 did not like the syntax
141 // (for example: `base2`.`table1` )
143 $upd_query = 'ALTER TABLE ' . PMA_backquote($table)
144 . ' ADD FOREIGN KEY ('
145 . PMA_backquote(PMA_sqlAddslashes($master_field)) . ')'
146 . ' REFERENCES '
147 . PMA_backquote(PMA_sqlAddslashes($foreign_db) . '.'
148 . PMA_sqlAddslashes($foreign_table)) . '('
149 . PMA_backquote(PMA_sqlAddslashes($foreign_field)) . ')';
151 if (${$master_field . '_on_delete'} != 'nix') {
152 $upd_query .= ' ON DELETE ' . $options_array[${$master_field . '_on_delete'}];
154 if (${$master_field . '_on_update'} != 'nix') {
155 $upd_query .= ' ON UPDATE ' . $options_array[${$master_field . '_on_update'}];
158 // end repeated code
160 } elseif (($existrel_innodb[$master_field]['foreign_db'] . '.' .$existrel_innodb[$master_field]['foreign_table'] . '.' . $existrel_innodb[$master_field]['foreign_field'] != $foreign_string)
161 || ( ${$master_field . '_on_delete'} != (!empty($existrel_innodb[$master_field]['on_delete']) ? $existrel_innodb[$master_field]['on_delete'] : ''))
162 || ( ${$master_field . '_on_update'} != (!empty($existrel_innodb[$master_field]['on_update']) ? $existrel_innodb[$master_field]['on_update'] : ''))
164 // another foreign key is already defined for this field
165 // or
166 // an option has been changed for ON DELETE or ON UPDATE
168 // remove existing key
169 if (PMA_MYSQL_INT_VERSION >= 40013) {
170 $upd_query = 'ALTER TABLE ' . PMA_backquote($table)
171 . ' DROP FOREIGN KEY '
172 . PMA_backquote($existrel_innodb[$master_field]['constraint']);
174 // I tried to send both in one query but it failed
175 $upd_rs = PMA_DBI_query($upd_query);
178 // add another
179 $upd_query = 'ALTER TABLE ' . PMA_backquote($table)
180 . ' ADD FOREIGN KEY ('
181 . PMA_backquote(PMA_sqlAddslashes($master_field)) . ')'
182 . ' REFERENCES '
183 . PMA_backquote(PMA_sqlAddslashes($foreign_db) . '.'
184 . PMA_sqlAddslashes($foreign_table)) . '('
185 . PMA_backquote(PMA_sqlAddslashes($foreign_field)) . ')';
187 if (${$master_field . '_on_delete'} != 'nix') {
188 $upd_query .= ' ON DELETE ' . $options_array[${$master_field . '_on_delete'}];
190 if (${$master_field . '_on_update'} != 'nix') {
191 $upd_query .= ' ON UPDATE ' . $options_array[${$master_field . '_on_update'}];
194 } // end if... else....
195 } elseif (isset($existrel_innodb[$master_field])) {
196 if (PMA_MYSQL_INT_VERSION >= 40013) {
197 $upd_query = 'ALTER TABLE ' . PMA_backquote($table)
198 . ' DROP FOREIGN KEY '
199 . PMA_backquote($existrel_innodb[$master_field]['constraint']);
201 } // end if... else....
203 if (isset($upd_query)) {
204 $upd_rs = PMA_DBI_try_query($upd_query);
205 $tmp_error = PMA_DBI_getError();
206 if (substr($tmp_error, 1, 4) == '1216') {
207 PMA_mysqlDie($tmp_error, $upd_query, FALSE, '', FALSE);
208 echo PMA_showMySQLDocu('manual_Table_types', 'InnoDB_foreign_key_constraints') . "\n";
210 if (substr($tmp_error, 1, 4) == '1005') {
211 echo '<p class="warning">' . $strNoIndex . ' (' . $master_field .')</p>' . PMA_showMySQLDocu('manual_Table_types', 'InnoDB_foreign_key_constraints') . "\n";
213 unset($upd_query);
214 unset($tmp_error);
216 } // end while
217 } // end if isset($destination_innodb)
220 // U p d a t e s f o r d i s p l a y f i e l d
222 if ($cfgRelation['displaywork']
223 && isset($display_field)) {
225 if ($disp) {
226 if ($display_field != '') {
227 $upd_query = 'UPDATE ' . PMA_backquote($cfgRelation['table_info'])
228 . ' SET display_field = \'' . PMA_sqlAddslashes($display_field) . '\''
229 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
230 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
231 } else {
232 $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['table_info'])
233 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
234 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
236 } elseif ($display_field != '') {
237 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['table_info'])
238 . '(db_name, table_name, display_field) '
239 . ' VALUES('
240 . '\'' . PMA_sqlAddslashes($db) . '\','
241 . '\'' . PMA_sqlAddslashes($table) . '\','
242 . '\'' . PMA_sqlAddslashes($display_field) . '\')';
245 if (isset($upd_query)) {
246 $upd_rs = PMA_query_as_cu($upd_query);
248 } // end if
250 // If we did an update, refresh our data
251 if (isset($destination) && $cfgRelation['relwork']) {
252 $existrel = PMA_getForeigners($db, $table, '', 'internal');
254 if (isset($destination_innodb) && $tbl_type=='INNODB') {
255 $existrel_innodb = PMA_getForeigners($db, $table, '', 'innodb');
258 if ($cfgRelation['displaywork']) {
259 $disp = PMA_getDisplayField($db, $table);
264 * Dialog
267 // common form
268 echo '<form method="post" action="tbl_relation.php">' . "\n";
269 echo PMA_generate_common_hidden_inputs($db, $table);
272 // relations
274 if ($cfgRelation['relwork'] || $tbl_type=='INNODB') {
275 // To choose relations we first need all tables names in current db
276 // and if PMA version permits and the main table is innodb,
277 // we use SHOW TABLE STATUS because we need to find other InnoDB tables
279 if ($tbl_type=='INNODB') {
280 $tab_query = 'SHOW TABLE STATUS FROM ' . PMA_backquote($db);
281 // [0] of the row is the name
282 // [1] is the type
283 } else {
284 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($db);
286 // [0] of the row is the name
288 $tab_rs = PMA_DBI_query($tab_query, NULL, PMA_DBI_QUERY_STORE);
289 $selectboxall['nix'] = '--';
290 $selectboxall_innodb['nix'] = '--';
292 while ($curr_table = @PMA_DBI_fetch_row($tab_rs)) {
293 if (($curr_table[0] != $table) && ($curr_table[0] != $cfg['Server']['relation'])) {
294 PMA_DBI_select_db($db);
296 // need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
297 $fi_rs = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($curr_table[0]) . ';', NULL, PMA_DBI_QUERY_STORE);
298 if ($fi_rs && PMA_DBI_num_rows($fi_rs) > 0) {
299 $seen_a_primary = FALSE;
300 while ($curr_field = PMA_DBI_fetch_assoc($fi_rs)) {
301 if (isset($curr_field['Key_name']) && $curr_field['Key_name'] == 'PRIMARY') {
302 $seen_a_primary = TRUE;
303 $field_full = $db . '.' .$curr_field['Table'] . '.' . $curr_field['Column_name'];
304 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
305 $selectboxall[$field_full] = $field_v;
306 // there could be more than one segment of the primary
307 // so do not break
309 // Please watch here, tbl_type is INNODB but the
310 // resulting value of SHOW KEYS is InnoDB
312 if ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
313 $selectboxall_innodb[$field_full] = $field_v;
316 } elseif (isset($curr_field['Non_unique']) && $curr_field['Non_unique'] == 0 && $seen_a_primary==FALSE) {
317 // if we can't find a primary key we take any unique one
318 // (in fact, we show all segments of unique keys
319 // and all unique keys)
320 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
321 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
322 $selectboxall[$field_full] = $field_v;
323 if ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
324 $selectboxall_innodb[$field_full] = $field_v;
327 // for InnoDB, any index is allowed
328 } elseif ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
329 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
330 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
331 $selectboxall_innodb[$field_full] = $field_v;
333 } // end if
334 } // end while over keys
335 } // end if (PMA_DBI_num_rows)
336 PMA_DBI_free_result($fi_rs);
337 unset($fi_rs);
338 // Mike Beck - 24.07.02: i've been asked to add all keys of the
339 // current table (see bug report #574851)
340 } elseif ($curr_table[0] == $table) {
341 PMA_DBI_select_db($db);
343 // need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
344 $fi_rs = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($curr_table[0]) . ';', NULL, PMA_DBI_QUERY_STORE);
345 if ($fi_rs && PMA_DBI_num_rows($fi_rs) > 0) {
346 while ($curr_field = PMA_DBI_fetch_assoc($fi_rs)) {
347 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
348 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
349 $selectboxall[$field_full] = $field_v;
350 if ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
351 $selectboxall_innodb[$field_full] = $field_v;
353 } // end while
354 } // end if (PMA_DBI_num_rows)
355 PMA_DBI_free_result($fi_rs);
356 unset($fi_rs);
358 } // end while over tables
360 } // end if
363 // Now find out the columns of our $table
364 // need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
365 $col_rs = PMA_DBI_try_query('SHOW COLUMNS FROM ' . PMA_backquote($table) . ';', NULL, PMA_DBI_QUERY_STORE);
367 if ($col_rs && PMA_DBI_num_rows($col_rs) > 0) {
368 while ($row = PMA_DBI_fetch_assoc($col_rs)) {
369 $save_row[] = $row;
371 $saved_row_cnt = count($save_row);
373 <fieldset>
374 <legend><?php echo $strLinksTo; ?></legend>
376 <table>
377 <tr><th></th>
378 <?php
379 if ( $cfgRelation['relwork'] ) {
380 echo '<th>' . $strInternalRelations;
381 if ($tbl_type=='INNODB') {
382 echo PMA_showHint( $strInternalNotNecessary );
384 echo '</th>';
386 if ( $tbl_type=='INNODB' ) {
387 echo '<th colspan="2">InnoDB';
388 if (PMA_MYSQL_INT_VERSION < 40013) {
389 echo '(**)';
391 echo '</th>';
394 </tr>
395 <?php
396 $odd_row = true;
397 for ($i = 0; $i < $saved_row_cnt; $i++) {
398 $myfield = $save_row[$i]['Field'];
400 <tr class="<?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
401 <td align="center">
402 <b><?php echo $save_row[$i]['Field']; ?></b></td>
403 <?php
404 if ($cfgRelation['relwork']) {
406 <td><select name="destination[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
407 <?php
408 // PMA internal relations
409 if (isset($existrel[$myfield])) {
410 $foreign_field = $existrel[$myfield]['foreign_db'] . '.'
411 . $existrel[$myfield]['foreign_table'] . '.'
412 . $existrel[$myfield]['foreign_field'];
413 } else {
414 $foreign_field = FALSE;
416 $seen_key = FALSE;
417 foreach ($selectboxall AS $key => $value) {
418 echo ' '
419 . '<option value="' . htmlspecialchars($key) . '"';
420 if ($foreign_field && $key == $foreign_field) {
421 echo ' selected="selected"';
422 $seen_key = TRUE;
424 echo '>' . $value . '</option>'. "\n";
425 } // end while
427 // if the link defined in relationtable points to a foreign field
428 // that is not a key in the foreign table, we show the link
429 // (will not be shown with an arrow)
430 if ($foreign_field && !$seen_key) {
431 echo ' '
432 .'<option value="' . htmlspecialchars($foreign_field) . '"'
433 .' selected="selected"'
434 .'>' . $foreign_field . '</option>'. "\n";
437 </select>
438 </td>
439 <?php
440 } // end if (internal relations)
442 if ($tbl_type=='INNODB') {
443 echo '<td>';
444 if (!empty($save_row[$i]['Key'])) {
446 <span class="formelement">
447 <select name="destination_innodb[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
448 <?php
449 if (isset($existrel_innodb[$myfield])) {
450 $foreign_field = $existrel_innodb[$myfield]['foreign_db'] . '.'
451 . $existrel_innodb[$myfield]['foreign_table'] . '.'
452 . $existrel_innodb[$myfield]['foreign_field'];
453 } else {
454 $foreign_field = FALSE;
457 $found_foreign_field = FALSE;
458 foreach ($selectboxall_innodb AS $key => $value) {
459 echo ' '
460 . '<option value="' . htmlspecialchars($key) . '"';
461 if ($foreign_field && $key == $foreign_field) {
462 echo ' selected="selected"';
463 $found_foreign_field = TRUE;
465 echo '>' . $value . '</option>'. "\n";
466 } // end while
468 // we did not find the foreign field in the tables of current db,
469 // must be defined in another db so show it to avoid erasing it
470 if (!$found_foreign_field && $foreign_field) {
471 echo ' '
472 . '<option value="' . htmlspecialchars($foreign_field) . '"';
473 echo ' selected="selected"';
474 echo '>' . $foreign_field . '</option>' . "\n";
478 </select>
479 </span>
480 <span class="formelement">
481 <?php
482 PMA_generate_dropdown('ON DELETE',
483 htmlspecialchars($save_row[$i]['Field']) . '_on_delete',
484 $options_array,
485 isset($existrel_innodb[$myfield]['on_delete']) ? $existrel_innodb[$myfield]['on_delete']: '' );
487 echo '</span>' . "\n"
488 .'<span class="formelement">' . "\n";
490 PMA_generate_dropdown('ON UPDATE',
491 htmlspecialchars($save_row[$i]['Field']) . '_on_update',
492 $options_array,
493 isset($existrel_innodb[$myfield]['on_update']) ? $existrel_innodb[$myfield]['on_update']: '' );
494 echo '</span>' . "\n";
495 } else {
496 echo $strNoIndex;
497 } // end if (a key exists)
498 echo ' </td>';
499 } // end if (InnoDB)
501 </tr>
502 <?php
503 } // end for
505 echo ' </table>' . "\n";
506 echo '</fieldset>' . "\n";
508 if ($cfgRelation['displaywork']) {
509 // Get "display_field" infos
510 $disp = PMA_getDisplayField($db, $table);
512 <fieldset>
513 <label><?php echo $strChangeDisplay . ': '; ?></label>
514 <select name="display_field" style="vertical-align: middle">
515 <option value="">---</option>
516 <?php
517 foreach ($save_row AS $row) {
518 echo ' <option value="' . htmlspecialchars($row['Field']) . '"';
519 if (isset($disp) && $row['Field'] == $disp) {
520 echo ' selected="selected"';
522 echo '>' . htmlspecialchars($row['Field']) . '</option>'. "\n";
523 } // end while
525 </select>
526 </fieldset>
527 <?php
528 } // end if (displayworks)
530 <fieldset class="tblFooters">
531 <input type="submit" value="<?php echo $strSave; ?>" />
532 </fieldset>
533 </form>
534 <?php
535 } // end if (we have columns in this table)
537 if ( $tbl_type === 'INNODB' && PMA_MYSQL_INT_VERSION < 40013 ) {
538 echo '<div class="warning">'
539 .'** ' . sprintf($strUpgrade, 'MySQL', '4.0.13')
540 .'</div>';
544 * Displays the footer
546 require_once('./footer.inc.php');