Rev Author Line No. Line
250 kaklik 1 <?php
2 /* $Id: tbl_relation.php,v 2.31.2.1 2006/04/11 17:51:03 cybot_tm Exp $ */
3 // vim: expandtab sw=4 ts=4 sts=4:
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';
11  
12  
13 /**
14 * Gets tables informations
15 */
16 require_once('./libraries/tbl_properties_table_info.inc.php');
17  
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;
22  
23 /**
24 * Displays top menu links
25 */
26 require_once('./libraries/tbl_properties_links.inc.php');
27  
28 require_once('./libraries/relation.lib.php');
29  
30 $options_array = array('CASCADE' => 'CASCADE', 'SET_NULL' => 'SET NULL', 'NO_ACTION' => 'NO ACTION', 'RESTRICT' => 'RESTRICT');
31  
32 /**
33 * Generate dropdown choices
34 *
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)
39 *
40 * @access public
41 */
42 function PMA_generate_dropdown($dropdown_question, $radio_name, $choices, $selected_value)
43 {
44 echo $dropdown_question . '&nbsp;&nbsp;';
45  
46 echo '<select name="' . $radio_name . '">' . "\n";
47 echo '<option value="nix">--</option>' . "\n";
48  
49 foreach ($choices AS $one_value => $one_label) {
50 echo '<option value="' . $one_value . '"';
51 if ($selected_value == $one_value) {
52 echo ' selected="selected" ';
53 }
54 echo '>' . $one_label . '</option>' . "\n";
55 }
56 echo '</select>' . "\n";
57 echo "\n";
58 }
59  
60  
61 /**
62 * Gets the relation settings
63 */
64 $cfgRelation = PMA_getRelationsParam();
65  
66  
67 /**
68 * Updates
69 */
70  
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);
74  
75 if ($cfgRelation['relwork']) {
76 $existrel = PMA_getForeigners($db, $table, '', 'internal');
77 }
78 if ($tbl_type=='INNODB') {
79 $existrel_innodb = PMA_getForeigners($db, $table, '', 'innodb');
80 }
81 if ($cfgRelation['displaywork']) {
82 $disp = PMA_getDisplayField($db, $table);
83 }
84  
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']) {
87  
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);
119 }
120 } // end while
121 } // end if (updates for internal relations)
122  
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
132  
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` )
138  
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) . ')';
146  
147 if ($_REQUEST['on_delete'][$master_field] != 'nix') {
148 $upd_query .= ' ON DELETE ' . $options_array[$_REQUEST['on_delete'][$master_field]];
149 }
150 if ($_REQUEST['on_update'][$master_field] != 'nix') {
151 $upd_query .= ' ON UPDATE ' . $options_array[$_REQUEST['on_update'][$master_field]];
152 }
153  
154 // end repeated code
155  
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'] : ''))
159 ) {
160 // another foreign key is already defined for this field
161 // or
162 // an option has been changed for ON DELETE or ON UPDATE
163  
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']);
169  
170 // I tried to send both in one query but it failed
171 $upd_rs = PMA_DBI_query($upd_query);
172 }
173  
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) . ')';
182  
183 if ($_REQUEST['on_delete'][$master_field] != 'nix') {
184 $upd_query .= ' ON DELETE '
185 . $options_array[$_REQUEST['on_delete'][$master_field]];
186 }
187 if ($_REQUEST['on_update'][$master_field] != 'nix') {
188 $upd_query .= ' ON UPDATE '
189 . $options_array[$_REQUEST['on_update'][$master_field]];
190 }
191  
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']);
198 }
199 } // end if... else....
200  
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 PMA_mysqlDie($tmp_error, $upd_query, FALSE, '', FALSE);
206 echo PMA_showMySQLDocu('manual_Table_types', 'InnoDB_foreign_key_constraints') . "\n";
207 }
208 if (substr($tmp_error, 1, 4) == '1005') {
209 echo '<p class="warning">' . $strNoIndex . ' (' . $master_field
210 .')</p>' . PMA_showMySQLDocu('manual_Table_types', 'InnoDB_foreign_key_constraints') . "\n";
211 }
212 unset($upd_query, $tmp_error);
213 }
214 } // end while
215 } // end if isset($destination_innodb)
216  
217  
218 // U p d a t e s f o r d i s p l a y f i e l d
219  
220 if ($cfgRelation['displaywork']
221 && isset($display_field)) {
222  
223 if ($disp) {
224 if ($display_field != '') {
225 $upd_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
226 . ' SET display_field = \'' . PMA_sqlAddslashes($display_field) . '\''
227 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
228 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
229 } else {
230 $upd_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
231 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
232 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
233 }
234 } elseif ($display_field != '') {
235 $upd_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
236 . '(db_name, table_name, display_field) '
237 . ' VALUES('
238 . '\'' . PMA_sqlAddslashes($db) . '\','
239 . '\'' . PMA_sqlAddslashes($table) . '\','
240 . '\'' . PMA_sqlAddslashes($display_field) . '\')';
241 }
242  
243 if (isset($upd_query)) {
244 $upd_rs = PMA_query_as_cu($upd_query);
245 }
246 } // end if
247  
248 // If we did an update, refresh our data
249 if (isset($destination) && $cfgRelation['relwork']) {
250 $existrel = PMA_getForeigners($db, $table, '', 'internal');
251 }
252 if (isset($destination_innodb) && $tbl_type=='INNODB') {
253 $existrel_innodb = PMA_getForeigners($db, $table, '', 'innodb');
254 }
255  
256 if ($cfgRelation['displaywork']) {
257 $disp = PMA_getDisplayField($db, $table);
258 }
259  
260  
261 /**
262 * Dialog
263 */
264  
265 // common form
266 echo '<form method="post" action="tbl_relation.php">' . "\n";
267 echo PMA_generate_common_hidden_inputs($db, $table);
268  
269  
270 // relations
271  
272 if ($cfgRelation['relwork'] || $tbl_type=='INNODB') {
273 // To choose relations we first need all tables names in current db
274 // and if PMA version permits and the main table is innodb,
275 // we use SHOW TABLE STATUS because we need to find other InnoDB tables
276  
277 if ($tbl_type=='INNODB') {
278 $tab_query = 'SHOW TABLE STATUS FROM ' . PMA_backquote($db);
279 // [0] of the row is the name
280 // [1] is the type
281 } else {
282 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($db);
283 }
284 // [0] of the row is the name
285  
286 $tab_rs = PMA_DBI_query($tab_query, null, PMA_DBI_QUERY_STORE);
287 $selectboxall['nix'] = '--';
288 $selectboxall_innodb['nix'] = '--';
289  
290 while ($curr_table = @PMA_DBI_fetch_row($tab_rs)) {
291 if (($curr_table[0] != $table) && ($curr_table[0] != $cfg['Server']['relation'])) {
292 PMA_DBI_select_db($db);
293  
294 // need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
295 $fi_rs = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($curr_table[0]) . ';', null, PMA_DBI_QUERY_STORE);
296 if ($fi_rs && PMA_DBI_num_rows($fi_rs) > 0) {
297 $seen_a_primary = FALSE;
298 while ($curr_field = PMA_DBI_fetch_assoc($fi_rs)) {
299 if (isset($curr_field['Key_name']) && $curr_field['Key_name'] == 'PRIMARY') {
300 $seen_a_primary = TRUE;
301 $field_full = $db . '.' .$curr_field['Table'] . '.' . $curr_field['Column_name'];
302 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
303 $selectboxall[$field_full] = $field_v;
304 // there could be more than one segment of the primary
305 // so do not break
306  
307 // Please watch here, tbl_type is INNODB but the
308 // resulting value of SHOW KEYS is InnoDB
309  
310 if ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
311 $selectboxall_innodb[$field_full] = $field_v;
312 }
313  
314 } elseif (isset($curr_field['Non_unique']) && $curr_field['Non_unique'] == 0 && $seen_a_primary==FALSE) {
315 // if we can't find a primary key we take any unique one
316 // (in fact, we show all segments of unique keys
317 // and all unique keys)
318 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
319 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
320 $selectboxall[$field_full] = $field_v;
321 if ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
322 $selectboxall_innodb[$field_full] = $field_v;
323 }
324  
325 // for InnoDB, any index is allowed
326 } elseif ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
327 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
328 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
329 $selectboxall_innodb[$field_full] = $field_v;
330  
331 } // end if
332 } // end while over keys
333 } // end if (PMA_DBI_num_rows)
334 PMA_DBI_free_result($fi_rs);
335 unset($fi_rs);
336 // Mike Beck - 24.07.02: i've been asked to add all keys of the
337 // current table (see bug report #574851)
338 } elseif ($curr_table[0] == $table) {
339 PMA_DBI_select_db($db);
340  
341 // need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
342 $fi_rs = PMA_DBI_query('SHOW KEYS FROM ' . PMA_backquote($curr_table[0]) . ';', null, PMA_DBI_QUERY_STORE);
343 if ($fi_rs && PMA_DBI_num_rows($fi_rs) > 0) {
344 while ($curr_field = PMA_DBI_fetch_assoc($fi_rs)) {
345 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
346 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
347 $selectboxall[$field_full] = $field_v;
348 if ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
349 $selectboxall_innodb[$field_full] = $field_v;
350 }
351 } // end while
352 } // end if (PMA_DBI_num_rows)
353 PMA_DBI_free_result($fi_rs);
354 unset($fi_rs);
355 }
356 } // end while over tables
357  
358 } // end if
359  
360  
361 // Now find out the columns of our $table
362 // need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
363 $col_rs = PMA_DBI_try_query('SHOW COLUMNS FROM ' . PMA_backquote($table) . ';', null, PMA_DBI_QUERY_STORE);
364  
365 if ($col_rs && PMA_DBI_num_rows($col_rs) > 0) {
366 while ($row = PMA_DBI_fetch_assoc($col_rs)) {
367 $save_row[] = $row;
368 }
369 $saved_row_cnt = count($save_row);
370 ?>
371 <fieldset>
372 <legend><?php echo $strLinksTo; ?></legend>
373  
374 <table>
375 <tr><th></th>
376 <?php
377 if ( $cfgRelation['relwork'] ) {
378 echo '<th>' . $strInternalRelations;
379 if ($tbl_type=='INNODB') {
380 echo PMA_showHint( $strInternalNotNecessary );
381 }
382 echo '</th>';
383 }
384 if ( $tbl_type=='INNODB' ) {
385 echo '<th colspan="2">InnoDB';
386 if (PMA_MYSQL_INT_VERSION < 40013) {
387 echo '(**)';
388 }
389 echo '</th>';
390 }
391 ?>
392 </tr>
393 <?php
394 $odd_row = true;
395 for ($i = 0; $i < $saved_row_cnt; $i++) {
396 $myfield = $save_row[$i]['Field'];
397 ?>
398 <tr class="<?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
399 <td align="center">
400 <b><?php echo $save_row[$i]['Field']; ?></b></td>
401 <?php
402 if ($cfgRelation['relwork']) {
403 ?>
404 <td><select name="destination[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
405 <?php
406 // PMA internal relations
407 if (isset($existrel[$myfield])) {
408 $foreign_field = $existrel[$myfield]['foreign_db'] . '.'
409 . $existrel[$myfield]['foreign_table'] . '.'
410 . $existrel[$myfield]['foreign_field'];
411 } else {
412 $foreign_field = FALSE;
413 }
414 $seen_key = FALSE;
415 foreach ($selectboxall AS $key => $value) {
416 echo ' '
417 . '<option value="' . htmlspecialchars($key) . '"';
418 if ($foreign_field && $key == $foreign_field) {
419 echo ' selected="selected"';
420 $seen_key = TRUE;
421 }
422 echo '>' . $value . '</option>'. "\n";
423 } // end while
424  
425 // if the link defined in relationtable points to a foreign field
426 // that is not a key in the foreign table, we show the link
427 // (will not be shown with an arrow)
428 if ($foreign_field && !$seen_key) {
429 echo ' '
430 .'<option value="' . htmlspecialchars($foreign_field) . '"'
431 .' selected="selected"'
432 .'>' . $foreign_field . '</option>'. "\n";
433 }
434 ?>
435 </select>
436 </td>
437 <?php
438 } // end if (internal relations)
439  
440 if ($tbl_type=='INNODB') {
441 echo '<td>';
442 if (!empty($save_row[$i]['Key'])) {
443 ?>
444 <span class="formelement">
445 <select name="destination_innodb[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
446 <?php
447 if (isset($existrel_innodb[$myfield])) {
448 $foreign_field = $existrel_innodb[$myfield]['foreign_db'] . '.'
449 . $existrel_innodb[$myfield]['foreign_table'] . '.'
450 . $existrel_innodb[$myfield]['foreign_field'];
451 } else {
452 $foreign_field = FALSE;
453 }
454  
455 $found_foreign_field = FALSE;
456 foreach ($selectboxall_innodb AS $key => $value) {
457 echo ' '
458 . '<option value="' . htmlspecialchars($key) . '"';
459 if ($foreign_field && $key == $foreign_field) {
460 echo ' selected="selected"';
461 $found_foreign_field = TRUE;
462 }
463 echo '>' . $value . '</option>'. "\n";
464 } // end while
465  
466 // we did not find the foreign field in the tables of current db,
467 // must be defined in another db so show it to avoid erasing it
468 if (!$found_foreign_field && $foreign_field) {
469 echo ' '
470 . '<option value="' . htmlspecialchars($foreign_field) . '"';
471 echo ' selected="selected"';
472 echo '>' . $foreign_field . '</option>' . "\n";
473 }
474  
475 ?>
476 </select>
477 </span>
478 <span class="formelement">
479 <?php
480 PMA_generate_dropdown('ON DELETE',
481 'on_delete[' . htmlspecialchars($save_row[$i]['Field']) . ']',
482 $options_array,
483 isset($existrel_innodb[$myfield]['on_delete']) ? $existrel_innodb[$myfield]['on_delete']: '' );
484  
485 echo '</span>' . "\n"
486 .'<span class="formelement">' . "\n";
487  
488 PMA_generate_dropdown('ON UPDATE',
489 'on_update[' . htmlspecialchars($save_row[$i]['Field']) . ']',
490 $options_array,
491 isset($existrel_innodb[$myfield]['on_update']) ? $existrel_innodb[$myfield]['on_update']: '' );
492 echo '</span>' . "\n";
493 } else {
494 echo $strNoIndex;
495 } // end if (a key exists)
496 echo ' </td>';
497 } // end if (InnoDB)
498 ?>
499 </tr>
500 <?php
501 } // end for
502  
503 echo ' </table>' . "\n";
504 echo '</fieldset>' . "\n";
505  
506 if ($cfgRelation['displaywork']) {
507 // Get "display_field" infos
508 $disp = PMA_getDisplayField($db, $table);
509 ?>
510 <fieldset>
511 <label><?php echo $strChangeDisplay . ': '; ?></label>
512 <select name="display_field" style="vertical-align: middle">
513 <option value="">---</option>
514 <?php
515 foreach ($save_row AS $row) {
516 echo ' <option value="' . htmlspecialchars($row['Field']) . '"';
517 if (isset($disp) && $row['Field'] == $disp) {
518 echo ' selected="selected"';
519 }
520 echo '>' . htmlspecialchars($row['Field']) . '</option>'. "\n";
521 } // end while
522 ?>
523 </select>
524 </fieldset>
525 <?php
526 } // end if (displayworks)
527 ?>
528 <fieldset class="tblFooters">
529 <input type="submit" value="<?php echo $strSave; ?>" />
530 </fieldset>
531 </form>
532 <?php
533 } // end if (we have columns in this table)
534  
535 if ( $tbl_type === 'INNODB' && PMA_MYSQL_INT_VERSION < 40013 ) {
536 echo '<div class="warning">'
537 .'** ' . sprintf($strUpgrade, 'MySQL', '4.0.13')
538 .'</div>';
539 }
540  
541 /**
542 * Displays the footer
543 */
544 require_once('./libraries/footer.inc.php');
545 ?>