3 // vim: expandtab sw=4 ts=4 sts=4:
6 * Gets some core libraries
8 require('./libraries/grab_globals.lib.php3');
9 require('./libraries/common.lib.php3');
10 require('./tbl_properties_common.php3');
11 $url_query .= '&goto=tbl_properties.php3';
13 // Note: in tbl_properties_table_info we get and display the table comment.
14 // For InnoDB, this comment contains the REFER information but any update
15 // has not been done yet (will be done in tbl_relation later).
16 $avoid_show_comment = TRUE;
17 require('./tbl_properties_table_info.php3');
18 require('./libraries/relation.lib.php3');
20 $options_array = array('CASCADE' => 'CASCADE', 'SET_NULL' => 'SET NULL', 'NO_ACTION' => 'NO ACTION', 'RESTRICT' => 'RESTRICT');
23 * Generate dropdown choices
25 * @param string Message to display
26 * @param string Name of the <select> field
27 * @param array Choices for dropdown
28 * @return string The existing value (for selected)
32 function PMA_generate_dropdown($dropdown_question,$radio_name,$choices,$selected_value) {
33 global $font_smallest;
35 echo $dropdown_question . ' ';
37 echo '<select name="' . $radio_name . '" style="font-size: ' . $font_smallest . '">' . "\n";
38 echo '<option value="nix" style="font-size: ' . $font_smallest . '" >--</option>' . "\n";
40 while (list($one_value, $one_label) = each($choices)) {
41 echo '<option value="' . $one_value . '"';
42 if ($selected_value == $one_value) {
43 echo ' selected="selected" ';
45 echo ' style="font-size: ' . $font_smallest . '">' . $one_label . '</option>' . "\n";
47 echo '</select>' . "\n";
53 * Gets the relation settings
55 $cfgRelation = PMA_getRelationsParam();
62 if ($cfgRelation['relwork']) {
63 $existrel = PMA_getForeigners($db, $table, '', 'internal');
64 if ($tbl_type=='INNODB') {
65 $existrel_innodb = PMA_getForeigners($db, $table, '', 'innodb');
68 if ($cfgRelation['displaywork']) {
69 $disp = PMA_getDisplayField($db, $table);
71 if ($cfgRelation['relwork']
72 && isset($submit_rel) && $submit_rel == 'true') {
74 // 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
76 while (list($master_field, $foreign_string) = each($destination)) {
77 if ($foreign_string != 'nix') {
78 list($foreign_db, $foreign_table, $foreign_field) = explode('.', $foreign_string);
79 if (!isset($existrel[$master_field])) {
80 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['relation'])
81 . '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
83 . '\'' . PMA_sqlAddslashes($db) . '\', '
84 . '\'' . PMA_sqlAddslashes($table) . '\', '
85 . '\'' . PMA_sqlAddslashes($master_field) . '\', '
86 . '\'' . PMA_sqlAddslashes($foreign_db) . '\', '
87 . '\'' . PMA_sqlAddslashes($foreign_table) . '\','
88 . '\'' . PMA_sqlAddslashes($foreign_field) . '\')';
89 } else if ($existrel[$master_field]['foreign_db'] . '.' .$existrel[$master_field]['foreign_table'] . '.' . $existrel[$master_field]['foreign_field'] != $foreign_string) {
90 $upd_query = 'UPDATE ' . PMA_backquote($cfgRelation['relation']) . ' SET'
91 . ' foreign_db = \'' . PMA_sqlAddslashes($foreign_db) . '\', '
92 . ' foreign_table = \'' . PMA_sqlAddslashes($foreign_table) . '\', '
93 . ' foreign_field = \'' . PMA_sqlAddslashes($foreign_field) . '\' '
94 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
95 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
96 . ' AND master_field = \'' . PMA_sqlAddslashes($master_field) . '\'';
97 } // end if... else....
98 } else if (isset($existrel[$master_field])) {
99 $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['relation'])
100 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
101 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
102 . ' AND master_field = \'' . PMA_sqlAddslashes($master_field) . '\'';
103 } // end if... else....
104 if (isset($upd_query)) {
105 $upd_rs = PMA_query_as_cu($upd_query);
111 // u p d a t e s f o r I n n o D B
112 // ( for now, same db only, and one index name)
113 if (isset($destination_innodb)) {
114 while (list($master_field, $foreign_string) = each($destination_innodb)) {
115 if ($foreign_string != 'nix') {
116 list($foreign_db, $foreign_table, $foreign_field) = explode('.', $foreign_string);
117 if (!isset($existrel_innodb[$master_field])) {
118 // no key defined for this field
120 // The next few lines are repeated below, so they
121 // could be put in an include file
122 $upd_query = 'ALTER TABLE ' . $table
123 . ' ADD FOREIGN KEY ('
124 . PMA_sqlAddslashes($master_field) . ')'
126 . PMA_sqlAddslashes($foreign_table) . '('
127 . PMA_sqlAddslashes($foreign_field) . ')';
129 if ($
{$master_field . '_on_delete'} != 'nix') {
130 $upd_query .= ' ON DELETE ' . $options_array[$
{$master_field . '_on_delete'}];
132 if ($
{$master_field . '_on_update'} != 'nix') {
133 $upd_query .= ' ON UPDATE ' . $options_array[$
{$master_field . '_on_update'}];
138 } else if (($existrel_innodb[$master_field]['foreign_db'] . '.' .$existrel_innodb[$master_field]['foreign_table'] . '.' . $existrel_innodb[$master_field]['foreign_field'] != $foreign_string)
139 ||
( $
{$master_field . '_on_delete'} != (!empty($existrel_innodb[$master_field]['on_delete']) ?
$existrel_innodb[$master_field]['on_delete'] : ''))
140 ||
( $
{$master_field . '_on_update'} != (!empty($existrel_innodb[$master_field]['on_update']) ?
$existrel_innodb[$master_field]['on_update'] : ''))
142 // another foreign key is already defined for this field
144 // an option has been changed for ON DELETE or ON UPDATE
146 // remove existing key
147 if (PMA_MYSQL_INT_VERSION
>= 40013) {
148 $upd_query = 'ALTER TABLE ' . $table
149 . ' DROP FOREIGN KEY '
150 . $existrel_innodb[$master_field]['constraint'];
152 // I tried to send both in one query but it failed
153 $upd_rs = PMA_mysql_query($upd_query);
157 $upd_query = 'ALTER TABLE ' . $table
158 . ' ADD FOREIGN KEY ('
159 . PMA_sqlAddslashes($master_field) . ')'
161 . PMA_sqlAddslashes($foreign_table) . '('
162 . PMA_sqlAddslashes($foreign_field) . ')';
164 if ($
{$master_field . '_on_delete'} != 'nix') {
165 $upd_query .= ' ON DELETE ' . $options_array[$
{$master_field . '_on_delete'}];
167 if ($
{$master_field . '_on_update'} != 'nix') {
168 $upd_query .= ' ON UPDATE ' . $options_array[$
{$master_field . '_on_update'}];
171 } // end if... else....
172 } else if (isset($existrel_innodb[$master_field])) {
173 if (PMA_MYSQL_INT_VERSION
>= 40013) {
174 $upd_query = 'ALTER TABLE ' . $table
175 . ' DROP FOREIGN KEY '
176 . $existrel_innodb[$master_field]['constraint'];
178 } // end if... else....
180 if (isset($upd_query)) {
181 $upd_rs = PMA_mysql_query($upd_query);
182 if (PMA_mysql_error() && mysql_errno() == 1005) {
183 echo '<p class="warning">' . $strNoIndex . ' (' . $master_field .')</p>' . PMA_showMySQLDocu('manual_Table_types', 'InnoDB_foreign_key_constraints') . "\n";
188 } // end if isset($destination_innodb)
193 // U p d a t e s f o r d i s p l a y f i e l d
195 if ($cfgRelation['displaywork']
196 && isset($submit_show) && $submit_show == 'true') {
199 if ($display_field != '') {
200 $upd_query = 'UPDATE ' . PMA_backquote($cfgRelation['table_info'])
201 . ' SET display_field = \'' . PMA_sqlAddslashes($display_field) . '\''
202 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
203 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
205 $upd_query = 'DELETE FROM ' . PMA_backquote($cfgRelation['table_info'])
206 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
207 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
209 } elseif ($display_field != '') {
210 $upd_query = 'INSERT INTO ' . PMA_backquote($cfgRelation['table_info'])
211 . '(db_name, table_name, display_field) '
213 . '\'' . PMA_sqlAddslashes($db) . '\','
214 . '\'' . PMA_sqlAddslashes($table) . '\','
215 . '\'' . PMA_sqlAddslashes($display_field) . '\')';
218 if (isset($upd_query)) {
219 $upd_rs = PMA_query_as_cu($upd_query);
223 if ($cfgRelation['commwork']
224 && isset($submit_comm) && $submit_comm == 'true') {
225 while (list($key, $value) = each($comment)) {
226 // garvin: I exported the snippet here to a function (relation.lib.php3) , so it can be used multiple times throughout other pages where you can set comments.
227 PMA_setComment($db, $table, $key, $value);
228 } // end while (transferred data)
229 } // end if (commwork)
231 // If we did an update, refresh our data
232 if ($cfgRelation['relwork']
233 && isset($submit_rel) && $submit_rel == 'true') {
234 $existrel = PMA_getForeigners($db, $table, '', 'internal');
235 if ($tbl_type=='INNODB') {
236 $existrel_innodb = PMA_getForeigners($db, $table, '', 'innodb');
239 if ($cfgRelation['displaywork']) {
240 $disp = PMA_getDisplayField($db, $table);
242 if ($cfgRelation['commwork']) {
243 $comments = PMA_getComments($db, $table);
250 if ($cfgRelation['relwork']) {
252 // To choose relations we first need all tables names in current db
253 // and if PMA version permits and the main table is innodb,
254 // we use SHOW TABLE STATUS because we need to find other InnoDB tables
256 if (PMA_MYSQL_INT_VERSION
>= 32303 && $tbl_type=='INNODB') {
257 $tab_query = 'SHOW TABLE STATUS FROM ' . PMA_backquote($db);
258 // [0] of the row is the name
261 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($db);
263 // [0] of the row is the name
265 $tab_rs = PMA_mysql_query($tab_query) or PMA_mysqlDie('', $tab_query, '', $err_url_0);
266 $selectboxall['nix'] = '--';
267 $selectboxall_innodb['nix'] = '--';
269 while ($curr_table = @PMA_mysql_fetch_array
($tab_rs)) {
270 if (($curr_table[0] != $table) && ($curr_table[0] != $cfg['Server']['relation'])) {
271 $fi_query = 'SHOW KEYS FROM ' . PMA_backquote($curr_table[0]);
272 $fi_rs = PMA_mysql_query($fi_query) or PMA_mysqlDie('', $fi_query, '', $err_url_0);
273 if ($fi_rs && mysql_num_rows($fi_rs) > 0) {
274 $seen_a_primary=FALSE;
275 while ($curr_field = PMA_mysql_fetch_array($fi_rs)) {
276 if (isset($curr_field['Key_name']) && $curr_field['Key_name'] == 'PRIMARY') {
277 $seen_a_primary=TRUE;
278 $field_full = $db . '.' .$curr_field['Table'] . '.' . $curr_field['Column_name'];
279 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
280 $selectboxall[$field_full] = $field_v;
281 // there could be more than one segment of the primary
284 if ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
285 $selectboxall_innodb[$field_full] = $field_v;
288 } else if (isset($curr_field['Non_unique']) && $curr_field['Non_unique'] == 0 && $seen_a_primary==FALSE) {
289 // if we can't find a primary key we take any unique one
290 // (in fact, we show all segments of unique keys
291 // and all unique keys)
292 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
293 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
294 $selectboxall[$field_full] = $field_v;
295 if ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
296 $selectboxall_innodb[$field_full] = $field_v;
299 } // end while over keys
300 } // end if (mysql_num_rows)
302 // Mike Beck - 24.07.02: i've been asked to add all keys of the
303 // current table (see bug report #574851)
305 else if ($curr_table[0] == $table) {
306 $fi_query = 'SHOW KEYS FROM ' . PMA_backquote($curr_table[0]);
307 $fi_rs = PMA_mysql_query($fi_query) or PMA_mysqlDie('', $fi_query, '', $err_url_0);
308 if ($fi_rs && mysql_num_rows($fi_rs) > 0) {
309 while ($curr_field = PMA_mysql_fetch_array($fi_rs)) {
310 $field_full = $db . '.' . $curr_field['Table'] . '.' . $curr_field['Column_name'];
311 $field_v = $curr_field['Table'] . '->' . $curr_field['Column_name'];
312 $selectboxall[$field_full] = $field_v;
313 if ($tbl_type=='INNODB' && isset($curr_table[1]) && $curr_table[1]=='InnoDB') {
314 $selectboxall_innodb[$field_full] = $field_v;
317 } // end if (mysql_num_rows)
319 } // end while over tables
324 // Now find out the columns of our $table
325 $col_query = 'SHOW COLUMNS FROM ' . PMA_backquote($table);
326 $col_rs = PMA_mysql_query($col_query) or PMA_mysqlDie('', $col_query, '', $err_url_0);
328 if ($col_rs && mysql_num_rows($col_rs) > 0) {
329 while ($row = PMA_mysql_fetch_array($col_rs)) {
332 $saved_row_cnt = count($save_row);
335 <form method
="post" action
="tbl_relation.php3">
336 <?php
echo PMA_generate_common_hidden_inputs($db, $table); ?
>
337 <input type
="hidden" name
="submit_rel" value
="true" />
341 <th colspan
="4" align
="center"><b
><?php
echo $strLinksTo; ?
></b
></th
>
344 <th
></th
><th
><b
><?php
echo $strInternalRelations;
345 if ($tbl_type=='INNODB') {
350 if ($tbl_type=='INNODB') {
351 echo '<th colspan="2">InnoDB';
352 if (PMA_MYSQL_INT_VERSION
< 40013) {
360 for ($i = 0; $i < $saved_row_cnt; $i++
) {
361 $myfield = $save_row[$i]['Field'];
365 <th
><?php
echo $save_row[$i]['Field']; ?
></th
>
367 <select name
="destination[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
371 // PMA internal relations
372 reset($selectboxall);
373 if (isset($existrel[$myfield])) {
374 $foreign_field = $existrel[$myfield]['foreign_db'] . '.'
375 . $existrel[$myfield]['foreign_table'] . '.'
376 . $existrel[$myfield]['foreign_field'];
378 $foreign_field = FALSE;
381 while (list($key, $value) = each($selectboxall)) {
383 . '<option value="' . htmlspecialchars($key) . '"';
384 if ($foreign_field && $key == $foreign_field) {
385 echo ' selected="selected"';
388 echo '>' . $value . '</option>'. "\n";
391 // if the link defined in relationtable points to a foreign field
392 // that is not a key in the foreign table, we show the link
393 // (will not be shown with an arrow)
394 if ($foreign_field && !$seen_key) {
396 . '<option value="' . htmlspecialchars($foreign_field) . '"';
397 echo ' selected="selected"';
398 echo '>' . $foreign_field . '</option>'. "\n";
404 if ($tbl_type=='INNODB') {
407 <select name
="destination_innodb[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
409 reset($selectboxall_innodb);
410 if (isset($existrel_innodb[$myfield])) {
411 $foreign_field = $existrel_innodb[$myfield]['foreign_db'] . '.'
412 . $existrel_innodb[$myfield]['foreign_table'] . '.'
413 . $existrel_innodb[$myfield]['foreign_field'];
415 $foreign_field = FALSE;
417 while (list($key, $value) = each($selectboxall_innodb)) {
419 . '<option value="' . htmlspecialchars($key) . '"';
420 if ($foreign_field && $key == $foreign_field) {
421 echo ' selected="selected"';
423 echo '>' . $value . '</option>'. "\n";
431 PMA_generate_dropdown('ON DELETE',
432 htmlspecialchars($save_row[$i]['Field']) . '_on_delete',
434 (isset($existrel_innodb[$myfield]['on_delete']) ?
$existrel_innodb[$myfield]['on_delete']: '') );
436 echo ' ';
438 PMA_generate_dropdown('ON UPDATE',
439 htmlspecialchars($save_row[$i]['Field']) . '_on_update',
441 (isset($existrel_innodb[$myfield]['on_update']) ?
$existrel_innodb[$myfield]['on_update']: '') );
453 <td colspan
="2" align
="center">
454 <input type
="submit" value
="<?php echo ' ' . $strGo . ' '; ?>" />
459 if ($tbl_type=='INNODB') {
460 echo $strInternalNotNecessary . '<br />';
461 if (PMA_MYSQL_INT_VERSION
< 40013) {
462 echo '** ' . sprintf($strUpgradeMySQL, '4.0.13') . '<br />';
469 if ($cfgRelation['displaywork']) {
470 // Get "display_field" infos
471 $disp = PMA_getDisplayField($db, $table);
475 <form method
="post" action
="tbl_relation.php3">
476 <?php
echo PMA_generate_common_hidden_inputs($db, $table); ?
>
477 <input type
="hidden" name
="submit_show" value
="true" />
479 <p
><?php
echo $strChangeDisplay . ': '; ?
>
480 <select name
="display_field" onchange
="this.form.submit();">
481 <option value
="">---</option
>
484 mysql_data_seek($col_rs, 0);
485 while ($row = @PMA_mysql_fetch_array
($col_rs)) {
486 echo ' <option value="' . htmlspecialchars($row['Field']) . '"';
487 if (isset($disp) && $row['Field'] == $disp) {
488 echo ' selected="selected"';
490 echo '>' . htmlspecialchars($row['Field']) . '</option>'. "\n";
494 <script type
="text/javascript" language
="javascript">
496 // Fake js to allow the use of the <noscript> tag
500 <input type
="submit" value
="<?php echo $strGo; ?>" />
505 } // end if (displayworks)
507 if ($cfgRelation['commwork']) {
511 <form method
="post" action
="tbl_relation.php3">
512 <?php
echo PMA_generate_common_hidden_inputs($db, $table); ?
>
513 <input type
="hidden" name
="submit_comm" value
="true" />
517 <th colspan
="2" align
="center"><b
><?php
echo $strComments; ?
></b
></th
>
520 for ($i = 0; $i < $saved_row_cnt; $i++
) {
521 $field = $save_row[$i]['Field'];
525 <th
><?php
echo $field; ?
></th
>
527 <input type
="text" name
="comment[<?php echo $field; ?>]" value
="<?php echo (isset($comments[$field]) ? htmlspecialchars($comments[$field]) : ''); ?>" />
536 <td colspan
="2" align
="center">
537 <input type
="submit" value
="<?php echo $strGo; ?>" />
543 } // end if (comments work)
544 } // end if (we have columns in this table)
548 * Displays the footer
551 require('./footer.inc.php3');