remove control M
[phpmyadmin/crack.git] / tbl_relation.php3
blob658e81d0b9c257ba6ed608c59b9b1aa10f6c5a91
1 <?php
2 /* $Id$ */
3 // vim: expandtab sw=4 ts=4 sts=4:
5 /**
6 * Gets some core libraries
7 */
8 require('./libraries/grab_globals.lib.php3');
9 require('./libraries/common.lib.php3');
10 require('./tbl_properties_common.php3');
11 $url_query .= '&amp;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');
22 /**
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)
30 * @access public
32 function PMA_generate_dropdown($dropdown_question,$radio_name,$choices,$selected_value) {
33 global $font_smallest;
35 echo $dropdown_question . '&nbsp;&nbsp;';
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";
48 echo "\n";
52 /**
53 * Gets the relation settings
55 $cfgRelation = PMA_getRelationsParam();
58 /**
59 * Updates
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)'
82 . ' values('
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);
106 unset($upd_query);
108 } // end while
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) . ')'
125 . ' REFERENCES '
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'}];
136 // end repeated code
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
143 // or
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);
156 // add another
157 $upd_query = 'ALTER TABLE ' . $table
158 . ' ADD FOREIGN KEY ('
159 . PMA_sqlAddslashes($master_field) . ')'
160 . ' REFERENCES '
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";
185 unset($upd_query);
187 } // end while
188 } // end if isset($destination_innodb)
190 } // end if
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') {
198 if ($disp) {
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) . '\'';
204 } else {
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) '
212 . ' VALUES('
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);
221 } // end if
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);
248 * Dialog
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
259 // [1] is the type
260 } else {
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
282 // so do not break
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;
298 } // end if
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;
316 } // end while
317 } // end if (mysql_num_rows)
319 } // end while over tables
321 } // end if
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)) {
330 $save_row[] = $row;
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" />
339 <table>
340 <tr>
341 <th colspan="4" align="center"><b><?php echo $strLinksTo; ?></b></th>
342 </tr>
343 <tr>
344 <th></th><th><b><?php echo $strInternalRelations;
345 if ($tbl_type=='INNODB') {
346 echo '&nbsp;(*)';
348 ?></b></th>
349 <?php
350 if ($tbl_type=='INNODB') {
351 echo '<th colspan="2">InnoDB';
352 if (PMA_MYSQL_INT_VERSION < 40013) {
353 echo '&nbsp;(**)';
355 echo '</th>';
358 </tr>
359 <?php
360 for ($i = 0; $i < $saved_row_cnt; $i++) {
361 $myfield = $save_row[$i]['Field'];
362 echo "\n";
364 <tr>
365 <th><?php echo $save_row[$i]['Field']; ?></th>
366 <td>
367 <select name="destination[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
368 <?php
369 echo "\n";
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'];
377 } else {
378 $foreign_field = FALSE;
380 $seen_key = FALSE;
381 while (list($key, $value) = each($selectboxall)) {
382 echo ' '
383 . '<option value="' . htmlspecialchars($key) . '"';
384 if ($foreign_field && $key == $foreign_field) {
385 echo ' selected="selected"';
386 $seen_key = TRUE;
388 echo '>' . $value . '</option>'. "\n";
389 } // end while
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) {
395 echo ' '
396 . '<option value="' . htmlspecialchars($foreign_field) . '"';
397 echo ' selected="selected"';
398 echo '>' . $foreign_field . '</option>'. "\n";
401 </select>
402 </td>
403 <?php
404 if ($tbl_type=='INNODB') {
406 <td>
407 <select name="destination_innodb[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
408 <?php
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'];
414 } else {
415 $foreign_field = FALSE;
417 while (list($key, $value) = each($selectboxall_innodb)) {
418 echo ' '
419 . '<option value="' . htmlspecialchars($key) . '"';
420 if ($foreign_field && $key == $foreign_field) {
421 echo ' selected="selected"';
423 echo '>' . $value . '</option>'. "\n";
424 } // end while
427 </select>
428 </td>
429 <td>
430 <?php
431 PMA_generate_dropdown('ON DELETE',
432 htmlspecialchars($save_row[$i]['Field']) . '_on_delete',
433 $options_array,
434 (isset($existrel_innodb[$myfield]['on_delete']) ? $existrel_innodb[$myfield]['on_delete']: '') );
436 echo '&nbsp;&nbsp;&nbsp;';
438 PMA_generate_dropdown('ON UPDATE',
439 htmlspecialchars($save_row[$i]['Field']) . '_on_update',
440 $options_array,
441 (isset($existrel_innodb[$myfield]['on_update']) ? $existrel_innodb[$myfield]['on_update']: '') );
445 </td>
446 </tr>
447 <?php
448 } // end for
450 echo "\n";
452 <tr>
453 <td colspan="2" align="center">
454 <input type="submit" value="<?php echo ' ' . $strGo . ' '; ?>" />
455 </td>
456 </tr>
457 </table>
458 <?php
459 if ($tbl_type=='INNODB') {
460 echo $strInternalNotNecessary . '<br />';
461 if (PMA_MYSQL_INT_VERSION < 40013) {
462 echo '** ' . sprintf($strUpgradeMySQL, '4.0.13') . '<br />';
466 </form>
468 <?php
469 if ($cfgRelation['displaywork']) {
470 // Get "display_field" infos
471 $disp = PMA_getDisplayField($db, $table);
473 echo "\n";
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>
482 <?php
483 echo "\n";
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";
491 } // end while
493 </select>
494 <script type="text/javascript" language="javascript">
495 <!--
496 // Fake js to allow the use of the <noscript> tag
497 //-->
498 </script>
499 <noscript>
500 <input type="submit" value="<?php echo $strGo; ?>" />
501 </noscript>
502 </form>
503 </p>
504 <?php
505 } // end if (displayworks)
507 if ($cfgRelation['commwork']) {
509 echo "\n";
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" />
515 <table>
516 <tr>
517 <th colspan="2" align="center"><b><?php echo $strComments; ?></b></th>
518 </tr>
519 <?php
520 for ($i = 0; $i < $saved_row_cnt; $i++) {
521 $field = $save_row[$i]['Field'];
522 echo "\n";
524 <tr>
525 <th><?php echo $field; ?></th>
526 <td>
527 <input type="text" name="comment[<?php echo $field; ?>]" value="<?php echo (isset($comments[$field]) ? htmlspecialchars($comments[$field]) : ''); ?>" />
528 </td>
529 </tr>
530 <?php
531 } // end for
533 echo "\n";
535 <tr>
536 <td colspan="2" align="center">
537 <input type="submit" value="<?php echo $strGo; ?>" />
538 </td>
539 </tr>
540 </table>
541 </form>
542 <?php
543 } // end if (comments work)
544 } // end if (we have columns in this table)
548 * Displays the footer
550 echo "\n";
551 require('./footer.inc.php3');