3 // vim: expandtab sw=4 ts=4 sts=4:
6 * Gets some core libraries
8 require_once('./libraries/grab_globals.lib.php');
9 require_once('./libraries/common.lib.php');
10 require_once('./tbl_properties_common.php');
11 $url_query .= '&goto=tbl_properties.php';
15 * Gets tables informations
17 require('./tbl_properties_table_info.php');
19 // Note: in tbl_properties_links.php we get and display the table comment.
20 // For InnoDB, this comment contains the REFER information but any update
21 // has not been done yet (will be done in tbl_relation.php later).
22 $avoid_show_comment = TRUE;
25 * Displays top menu links
27 require('./tbl_properties_links.php');
29 require_once('./libraries/relation.lib.php');
31 $options_array = array('CASCADE' => 'CASCADE', 'SET_NULL' => 'SET NULL', 'NO_ACTION' => 'NO ACTION', 'RESTRICT' => 'RESTRICT');
34 * Generate dropdown choices
36 * @param string Message to display
37 * @param string Name of the <select> field
38 * @param array Choices for dropdown
39 * @return string The existing value (for selected)
43 function PMA_generate_dropdown($dropdown_question,$radio_name,$choices,$selected_value) {
44 global $font_smallest;
46 echo $dropdown_question . ' ';
48 //echo '<select name="' . $radio_name . '" style="font-size: ' . $font_smallest . '">' . "\n";
49 //echo '<option value="nix" style="font-size: ' . $font_smallest . '" >--</option>' . "\n";
50 echo '<select name="' . $radio_name . '">' . "\n";
51 echo '<option value="nix">--</option>' . "\n";
53 foreach ($choices AS $one_value => $one_label) {
54 echo '<option value="' . $one_value . '"';
55 if ($selected_value == $one_value) {
56 echo ' selected="selected" ';
58 //echo ' style="font-size: ' . $font_smallest . '">'
59 echo '>' . $one_label . '</option>' . "\n";
61 echo '</select>' . "\n";
67 * Gets the relation settings
69 $cfgRelation = PMA_getRelationsParam();
76 // ensure we are positionned to our current db (since the previous reading
77 // of relations makes pmadb the current one, maybe depending on the MySQL version)
78 PMA_DBI_select_db($db);
80 if ($cfgRelation['relwork']) {
81 $existrel = PMA_getForeigners($db, $table, '', 'internal');
83 if ($tbl_type=='INNODB') {
84 $existrel_innodb = PMA_getForeigners($db, $table, '', 'innodb');
86 if ($cfgRelation['displaywork']) {
87 $disp = PMA_getDisplayField($db, $table);
90 // updates for internal relations or innodb?
91 if (isset($submit_rel) && $submit_rel == 'true') {
92 // 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
93 if ($cfgRelation['relwork']) {
95 foreach ($destination AS $master_field => $foreign_string) {
96 if ($foreign_string != 'nix') {
97 list($foreign_db, $foreign_table, $foreign_field) = explode('.', $foreign_string);
98 if (!isset($existrel[$master_field])) {
99 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['relation'])
100 . '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
102 . '\'' . PMA_sqlAddslashes($db) . '\', '
103 . '\'' . PMA_sqlAddslashes($table) . '\', '
104 . '\'' . PMA_sqlAddslashes($master_field) . '\', '
105 . '\'' . PMA_sqlAddslashes($foreign_db) . '\', '
106 . '\'' . PMA_sqlAddslashes($foreign_table) . '\','
107 . '\'' . PMA_sqlAddslashes($foreign_field) . '\')';
108 } else if ($existrel[$master_field]['foreign_db'] . '.' .$existrel[$master_field]['foreign_table'] . '.' . $existrel[$master_field]['foreign_field'] != $foreign_string) {
109 $upd_query = 'UPDATE ' . PMA_backquote($cfgRelation['relation']) . ' SET'
110 . ' foreign_db = \'' . PMA_sqlAddslashes($foreign_db) . '\', '
111 . ' foreign_table = \'' . PMA_sqlAddslashes($foreign_table) . '\', '
112 . ' foreign_field = \'' . PMA_sqlAddslashes($foreign_field) . '\' '
113 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
114 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
115 . ' AND master_field = \'' . PMA_sqlAddslashes($master_field) . '\'';
116 } // end if... else....
117 } else if (isset($existrel[$master_field])) {
118 $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['relation'])
119 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
120 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
121 . ' AND master_field = \'' . PMA_sqlAddslashes($master_field) . '\'';
122 } // end if... else....
123 if (isset($upd_query)) {
124 $upd_rs = PMA_query_as_cu($upd_query);
128 } // end if (updates for internal relations)
130 // u p d a t e s f o r I n n o D B
131 // ( for now, one index name only; we keep the definitions if the
132 // foreign db is not the same)
133 if (isset($destination_innodb)) {
134 foreach ($destination_innodb AS $master_field => $foreign_string) {
135 if ($foreign_string != 'nix') {
136 list($foreign_db, $foreign_table, $foreign_field) = explode('.', $foreign_string);
137 if (!isset($existrel_innodb[$master_field])) {
138 // no key defined for this field
140 // The next few lines are repeated below, so they
141 // could be put in an include file
142 // Note: I tried to enclose the db and table name with
143 // backquotes but MySQL 4.0.16 did not like the syntax
144 // (for example: `base2`.`table1` )
146 $upd_query = 'ALTER TABLE ' . PMA_backquote($table)
147 . ' ADD FOREIGN KEY ('
148 . PMA_backquote(PMA_sqlAddslashes($master_field)) . ')'
150 . PMA_backquote(PMA_sqlAddslashes($foreign_db) . '.'
151 . PMA_sqlAddslashes($foreign_table)) . '('
152 . PMA_backquote(PMA_sqlAddslashes($foreign_field)) . ')';
154 if ($
{$master_field . '_on_delete'} != 'nix') {
155 $upd_query .= ' ON DELETE ' . $options_array[$
{$master_field . '_on_delete'}];
157 if ($
{$master_field . '_on_update'} != 'nix') {
158 $upd_query .= ' ON UPDATE ' . $options_array[$
{$master_field . '_on_update'}];
163 } else if (($existrel_innodb[$master_field]['foreign_db'] . '.' .$existrel_innodb[$master_field]['foreign_table'] . '.' . $existrel_innodb[$master_field]['foreign_field'] != $foreign_string)
164 ||
( $
{$master_field . '_on_delete'} != (!empty($existrel_innodb[$master_field]['on_delete']) ?
$existrel_innodb[$master_field]['on_delete'] : ''))
165 ||
( $
{$master_field . '_on_update'} != (!empty($existrel_innodb[$master_field]['on_update']) ?
$existrel_innodb[$master_field]['on_update'] : ''))
167 // another foreign key is already defined for this field
169 // an option has been changed for ON DELETE or ON UPDATE
171 // remove existing key
172 if (PMA_MYSQL_INT_VERSION
>= 40013) {
173 $upd_query = 'ALTER TABLE ' . PMA_backquote($table)
174 . ' DROP FOREIGN KEY '
175 . PMA_backquote($existrel_innodb[$master_field]['constraint']);
177 // I tried to send both in one query but it failed
178 $upd_rs = PMA_DBI_query($upd_query);
182 $upd_query = 'ALTER TABLE ' . PMA_backquote($table)
183 . ' ADD FOREIGN KEY ('
184 . PMA_backquote(PMA_sqlAddslashes($master_field)) . ')'
186 . PMA_backquote(PMA_sqlAddslashes($foreign_db) . '.'
187 . PMA_sqlAddslashes($foreign_table)) . '('
188 . PMA_backquote(PMA_sqlAddslashes($foreign_field)) . ')';
190 if ($
{$master_field . '_on_delete'} != 'nix') {
191 $upd_query .= ' ON DELETE ' . $options_array[$
{$master_field . '_on_delete'}];
193 if ($
{$master_field . '_on_update'} != 'nix') {
194 $upd_query .= ' ON UPDATE ' . $options_array[$
{$master_field . '_on_update'}];
197 } // end if... else....
198 } else if (isset($existrel_innodb[$master_field])) {
199 if (PMA_MYSQL_INT_VERSION
>= 40013) {
200 $upd_query = 'ALTER TABLE ' . PMA_backquote($table)
201 . ' DROP FOREIGN KEY '
202 . PMA_backquote($existrel_innodb[$master_field]['constraint']);
204 } // end if... else....
206 if (isset($upd_query)) {
207 $upd_rs = PMA_DBI_try_query($upd_query);
208 $tmp_error = PMA_DBI_getError();
209 if (substr($tmp_error, 1, 4) == '1216') {
210 PMA_mysqlDie($tmp_error, $upd_query, FALSE, '', FALSE);
211 echo PMA_showMySQLDocu('manual_Table_types', 'InnoDB_foreign_key_constraints') . "\n";
213 if (substr($tmp_error, 1, 4) == '1005') {
214 echo '<p class="warning">' . $strNoIndex . ' (' . $master_field .')</p>' . PMA_showMySQLDocu('manual_Table_types', 'InnoDB_foreign_key_constraints') . "\n";
220 } // end if isset($destination_innodb)
222 } // end if (updates for internal relations or InnoDB)
225 // U p d a t e s f o r d i s p l a y f i e l d
227 if ($cfgRelation['displaywork']
228 && isset($submit_show) && $submit_show == 'true') {
231 if ($display_field != '') {
232 $upd_query = 'UPDATE ' . PMA_backquote($cfgRelation['table_info'])
233 . ' SET display_field = \'' . PMA_sqlAddslashes($display_field) . '\''
234 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
235 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
237 $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['table_info'])
238 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
239 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
241 } elseif ($display_field != '') {
242 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['table_info'])
243 . '(db_name, table_name, display_field) '
245 . '\'' . PMA_sqlAddslashes($db) . '\','
246 . '\'' . PMA_sqlAddslashes($table) . '\','
247 . '\'' . PMA_sqlAddslashes($display_field) . '\')';
250 if (isset($upd_query)) {
251 $upd_rs = PMA_query_as_cu($upd_query);
255 // If we did an update, refresh our data
256 if (isset($submit_rel) && $submit_rel == 'true') {
257 if ($cfgRelation['relwork']) {
258 $existrel = PMA_getForeigners($db, $table, '', 'internal');
260 if ($tbl_type=='INNODB') {
261 $existrel_innodb = PMA_getForeigners($db, $table, '', 'innodb');
264 if ($cfgRelation['displaywork']) {
265 $disp = PMA_getDisplayField($db, $table);
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
277 if ($tbl_type=='INNODB') {
278 $tab_query = 'SHOW TABLE STATUS FROM ' . PMA_backquote($db);
279 // [0] of the row is the name
282 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($db);
284 // [0] of the row is the name
286 $tab_rs = PMA_DBI_query($tab_query, NULL, PMA_DBI_QUERY_STORE
);
287 $selectboxall['nix'] = '--';
288 $selectboxall_innodb['nix'] = '--';
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);
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
307 // Please watch here, tbl_type is INNODB but the
308 // resulting value of SHOW KEYS is InnoDB
310 if ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
311 $selectboxall_innodb[$field_full] = $field_v;
314 } else if (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;
325 // for InnoDB, any index is allowed
326 } else if ($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;
332 } // end while over keys
333 } // end if (PMA_DBI_num_rows)
334 PMA_DBI_free_result($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)
339 else if ($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;
353 } // end if (PMA_DBI_num_rows)
354 PMA_DBI_free_result($fi_rs);
357 } // end while over tables
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)) {
370 $saved_row_cnt = count($save_row);
371 echo $tbl_type=='INNODB' ?
'' : '<table border="0" cellpadding="0" cellspacing="0">' . "\n"
372 . ' <tr><td valign="top">' . "\n\n";
375 <form method
="post" action
="tbl_relation.php">
376 <?php
echo PMA_generate_common_hidden_inputs($db, $table); ?
>
377 <input type
="hidden" name
="submit_rel" value
="true" />
379 <table cellpadding
="2" cellspacing
="1">
381 <th colspan
="<?php echo $tbl_type=='INNODB' ? '4' : '2'; ?>" align
="center" class="tblHeaders"><b
><?php
echo $strLinksTo; ?
></b
></th
>
386 if ($cfgRelation['relwork']) {
387 echo ' <th><b>' .$strInternalRelations;
388 if ($tbl_type=='INNODB') {
389 echo ' ' . PMA_showHint($strInternalNotNecessary);
393 if ($tbl_type=='INNODB') {
394 echo '<th colspan="2">InnoDB';
395 if (PMA_MYSQL_INT_VERSION
< 40013) {
403 for ($i = 0; $i < $saved_row_cnt; $i++
) {
404 $myfield = $save_row[$i]['Field'];
408 <td align
="center" bgcolor
="<?php echo ($i % 2) ? $GLOBALS['cfg']['BgcolorOne'] : $GLOBALS['cfg']['BgcolorTwo']; ?>"><b
><?php
echo $save_row[$i]['Field']; ?
></b
></td
>
410 if ($cfgRelation['relwork']) {
412 <td bgcolor
="<?php echo ($i % 2) ? $GLOBALS['cfg']['BgcolorOne'] : $GLOBALS['cfg']['BgcolorTwo']; ?>">
413 <select name
="destination[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
417 // PMA internal relations
418 if (isset($existrel[$myfield])) {
419 $foreign_field = $existrel[$myfield]['foreign_db'] . '.'
420 . $existrel[$myfield]['foreign_table'] . '.'
421 . $existrel[$myfield]['foreign_field'];
423 $foreign_field = FALSE;
426 foreach ($selectboxall AS $key => $value) {
428 . '<option value="' . htmlspecialchars($key) . '"';
429 if ($foreign_field && $key == $foreign_field) {
430 echo ' selected="selected"';
433 echo '>' . $value . '</option>'. "\n";
436 // if the link defined in relationtable points to a foreign field
437 // that is not a key in the foreign table, we show the link
438 // (will not be shown with an arrow)
439 if ($foreign_field && !$seen_key) {
441 . '<option value="' . htmlspecialchars($foreign_field) . '"';
442 echo ' selected="selected"';
443 echo '>' . $foreign_field . '</option>'. "\n";
449 } // end if (internal relations)
451 if ($tbl_type=='INNODB') {
452 if (!empty($save_row[$i]['Key'])) {
454 <td bgcolor
="<?php echo ($i % 2) ? $GLOBALS['cfg']['BgcolorOne'] : $GLOBALS['cfg']['BgcolorTwo']; ?>">
455 <select name
="destination_innodb[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
457 if (isset($existrel_innodb[$myfield])) {
458 $foreign_field = $existrel_innodb[$myfield]['foreign_db'] . '.'
459 . $existrel_innodb[$myfield]['foreign_table'] . '.'
460 . $existrel_innodb[$myfield]['foreign_field'];
462 $foreign_field = FALSE;
465 $found_foreign_field = FALSE;
466 foreach ($selectboxall_innodb AS $key => $value) {
468 . '<option value="' . htmlspecialchars($key) . '"';
469 if ($foreign_field && $key == $foreign_field) {
470 echo ' selected="selected"';
471 $found_foreign_field = TRUE;
473 echo '>' . $value . '</option>'. "\n";
476 // we did not find the foreign field in the tables of current db,
477 // must be defined in another db so show it to avoid erasing it
478 if (!$found_foreign_field && $foreign_field) {
480 . '<option value="' . htmlspecialchars($foreign_field) . '"';
481 echo ' selected="selected"';
482 echo '>' . $foreign_field . '</option>'. "\n";
488 <td bgcolor
="<?php echo ($i % 2) ? $GLOBALS['cfg']['BgcolorOne'] : $GLOBALS['cfg']['BgcolorTwo']; ?>">
490 PMA_generate_dropdown('ON DELETE',
491 htmlspecialchars($save_row[$i]['Field']) . '_on_delete',
493 (isset($existrel_innodb[$myfield]['on_delete']) ?
$existrel_innodb[$myfield]['on_delete']: '') );
495 echo ' ';
497 PMA_generate_dropdown('ON UPDATE',
498 htmlspecialchars($save_row[$i]['Field']) . '_on_update',
500 (isset($existrel_innodb[$myfield]['on_update']) ?
$existrel_innodb[$myfield]['on_update']: '') );
503 echo '<td>' . PMA_showHint($strNoIndex) . '</td>';
504 } // end if (a key exists)
515 <td colspan
="<?php echo $tbl_type=='INNODB' ? '4' : '2'; ?>" align
="center" class="tblFooters">
516 <input type
="submit" value
="<?php echo ' ' . $strGo . ' '; ?>" />
521 if ($tbl_type=='INNODB') {
522 if (PMA_MYSQL_INT_VERSION
< 40013) {
523 echo '** ' . sprintf($strUpgrade, 'MySQL', '4.0.13') . '<br />';
530 echo $tbl_type=='INNODB' ?
'' : "\n\n" . ' </td>' . "\n";
531 if ($cfgRelation['displaywork']) {
532 echo $tbl_type=='INNODB' ?
'' : ' <td> </td>' . "\n"
533 . ' <td valign="top" align="center">' . "\n\n";
534 // Get "display_field" infos
535 $disp = PMA_getDisplayField($db, $table);
539 <form method
="post" action
="tbl_relation.php">
540 <?php
echo PMA_generate_common_hidden_inputs($db, $table); ?
>
541 <input type
="hidden" name
="submit_show" value
="true" />
543 <p
><b
><?php
echo $strChangeDisplay . ': '; ?
></b
><br
/>
544 <select name
="display_field" onchange
="this.form.submit();" style
="vertical-align: middle">
545 <option value
="">---</option
>
548 foreach ($save_row AS $row) {
549 echo ' <option value="' . htmlspecialchars($row['Field']) . '"';
550 if (isset($disp) && $row['Field'] == $disp) {
551 echo ' selected="selected"';
553 echo '>' . htmlspecialchars($row['Field']) . '</option>'. "\n";
557 <script type
="text/javascript" language
="javascript">
559 // Fake js to allow the use of the <noscript> tag
563 <input type
="submit" value
="<?php echo $strGo; ?>" style
="vertical-align: middle" />
568 echo $tbl_type=='INNODB' ?
'' : "\n\n" . ' </td>' . "\n";
569 } // end if (displayworks)
571 // comments handling removed from this page; now only available on
572 // the field properties page
574 echo $tbl_type=='INNODB' ?
'' : "\n\n" . ' </td></tr>' . "\n"
575 . '</table>' . "\n\n";
576 } // end if (we have columns in this table)
580 * Displays the footer
582 require_once('./footer.inc.php');