Czech translation update.
[phpmyadmin/crack.git] / tbl_relation.php
blobc1cc1889edbc5f6af4df629dd12906abba2bcd4f
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Display table relations for viewing and editing
6 * includes phpMyAdmin relations and InnoDB relations
8 * @todo fix name handling: currently names with dots (.) are not properly handled
9 * @todo foreign key constraints require both fields being of equal type and size
10 * @todo check foreign fields to be from same type and size, all other makes no sense
11 * @todo add an link to create an index required for constraints, or an option to do automatically
12 * @todo if above todos are fullfilled we can add all fields meet requirements in the select dropdown
13 * @version $Id$
16 /**
17 * Gets some core libraries
19 require_once './libraries/common.inc.php';
20 require_once './libraries/tbl_common.php';
21 $url_query .= '&amp;goto=tbl_sql.php';
24 /**
25 * Gets tables informations
27 require_once './libraries/tbl_info.inc.php';
29 // Note: in libraries/tbl_links.inc.php we get and display the table comment.
30 // For InnoDB, this comment contains the REFER information but any update
31 // has not been done yet (will be done in tbl_relation.php later).
32 $avoid_show_comment = TRUE;
34 /**
35 * Displays top menu links
37 require_once './libraries/tbl_links.inc.php';
39 require_once './libraries/relation.lib.php';
41 $options_array = array(
42 'CASCADE' => 'CASCADE',
43 'SET_NULL' => 'SET NULL',
44 'NO_ACTION' => 'NO ACTION',
45 'RESTRICT' => 'RESTRICT',
48 /**
49 * Generate dropdown choices
51 * @param string Message to display
52 * @param string Name of the <select> field
53 * @param array Choices for dropdown
54 * @return string The existing value (for selected)
56 * @access public
58 function PMA_generate_dropdown($dropdown_question, $radio_name, $choices, $selected_value)
60 echo htmlspecialchars($dropdown_question) . '&nbsp;&nbsp;';
62 echo '<select name="' . htmlspecialchars($radio_name) . '">' . "\n";
63 echo '<option value=""></option>' . "\n";
65 foreach ($choices as $one_value => $one_label) {
66 echo '<option value="' . htmlspecialchars($one_value) . '"';
67 if ($selected_value == $one_value) {
68 echo ' selected="selected" ';
70 echo '>' . htmlspecialchars($one_label) . '</option>' . "\n";
72 echo '</select>' . "\n";
75 /**
76 * Gets the relation settings
78 $cfgRelation = PMA_getRelationsParam();
81 /**
82 * Updates
84 if ($cfgRelation['relwork']) {
85 $existrel = PMA_getForeigners($db, $table, '', 'internal');
87 if ($tbl_type == 'INNODB') {
88 $existrel_innodb = PMA_getForeigners($db, $table, '', 'innodb');
90 if ($cfgRelation['displaywork']) {
91 $disp = PMA_getDisplayField($db, $table);
94 // 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
95 if (isset($destination) && $cfgRelation['relwork']) {
97 foreach ($destination as $master_field => $foreign_string) {
98 $upd_query = false;
99 if (! empty($foreign_string)) {
100 $foreign_string = trim($foreign_string, '`');
101 list($foreign_db, $foreign_table, $foreign_field) =
102 explode('`.`', $foreign_string);
103 if (! isset($existrel[$master_field])) {
104 $upd_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
105 . '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
106 . ' values('
107 . '\'' . PMA_sqlAddslashes($db) . '\', '
108 . '\'' . PMA_sqlAddslashes($table) . '\', '
109 . '\'' . PMA_sqlAddslashes($master_field) . '\', '
110 . '\'' . PMA_sqlAddslashes($foreign_db) . '\', '
111 . '\'' . PMA_sqlAddslashes($foreign_table) . '\','
112 . '\'' . PMA_sqlAddslashes($foreign_field) . '\')';
113 } elseif ($existrel[$master_field]['foreign_db'] . '.' .$existrel[$master_field]['foreign_table'] . '.' . $existrel[$master_field]['foreign_field'] != $foreign_string) {
114 $upd_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation']) . ' SET'
115 . ' foreign_db = \'' . PMA_sqlAddslashes($foreign_db) . '\', '
116 . ' foreign_table = \'' . PMA_sqlAddslashes($foreign_table) . '\', '
117 . ' foreign_field = \'' . PMA_sqlAddslashes($foreign_field) . '\' '
118 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
119 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
120 . ' AND master_field = \'' . PMA_sqlAddslashes($master_field) . '\'';
121 } // end if... else....
122 } elseif (isset($existrel[$master_field])) {
123 $upd_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
124 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
125 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
126 . ' AND master_field = \'' . PMA_sqlAddslashes($master_field) . '\'';
127 } // end if... else....
128 if ($upd_query) {
129 PMA_query_as_cu($upd_query);
131 } // end while
132 } // end if (updates for internal relations)
134 // u p d a t e s f o r I n n o D B
135 // (for now, one index name only; we keep the definitions if the
136 // foreign db is not the same)
137 // I use $sql_query to be able to display directly the query via
138 // PMA_showMessage()
140 if (isset($_REQUEST['destination_innodb'])) {
141 $display_query = '';
142 $seen_error = false;
143 foreach ($_REQUEST['destination_innodb'] as $master_field => $foreign_string) {
144 if (! empty($foreign_string)) {
145 $foreign_string = trim($foreign_string, '`');
146 list($foreign_db, $foreign_table, $foreign_field) =
147 explode('`.`', $foreign_string);
148 if (!isset($existrel_innodb[$master_field])) {
149 // no key defined for this field
151 // The next few lines are repeated below, so they
152 // could be put in an include file
153 // Note: I tried to enclose the db and table name with
154 // backquotes but MySQL 4.0.16 did not like the syntax
155 // (for example: `base2`.`table1`)
157 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
158 . ' ADD FOREIGN KEY ('
159 . PMA_backquote($master_field) . ')'
160 . ' REFERENCES '
161 . PMA_backquote($foreign_db) . '.'
162 . PMA_backquote($foreign_table) . '('
163 . PMA_backquote($foreign_field) . ')';
165 if (! empty($_REQUEST['on_delete'][$master_field])) {
166 $sql_query .= ' ON DELETE ' . $options_array[$_REQUEST['on_delete'][$master_field]];
168 if (! empty($_REQUEST['on_update'][$master_field])) {
169 $sql_query .= ' ON UPDATE ' . $options_array[$_REQUEST['on_update'][$master_field]];
171 $sql_query .= ';';
172 $display_query .= $sql_query . "\n";
173 // end repeated code
175 } elseif (($existrel_innodb[$master_field]['foreign_db'] . '.' .$existrel_innodb[$master_field]['foreign_table'] . '.' . $existrel_innodb[$master_field]['foreign_field'] != $foreign_string)
176 || ($_REQUEST['on_delete'][$master_field] != (!empty($existrel_innodb[$master_field]['on_delete']) ? $existrel_innodb[$master_field]['on_delete'] : ''))
177 || ($_REQUEST['on_update'][$master_field] != (!empty($existrel_innodb[$master_field]['on_update']) ? $existrel_innodb[$master_field]['on_update'] : ''))
179 // another foreign key is already defined for this field
180 // or
181 // an option has been changed for ON DELETE or ON UPDATE
183 // remove existing key
184 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
185 . ' DROP FOREIGN KEY '
186 . PMA_backquote($existrel_innodb[$master_field]['constraint']) . ';';
188 // I tried to send both in one query but it failed
189 PMA_DBI_query($sql_query);
190 $display_query .= $sql_query . "\n";
192 // add another
193 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
194 . ' ADD FOREIGN KEY ('
195 . PMA_backquote($master_field) . ')'
196 . ' REFERENCES '
197 . PMA_backquote($foreign_db) . '.'
198 . PMA_backquote($foreign_table) . '('
199 . PMA_backquote($foreign_field) . ')';
201 if ($_REQUEST['on_delete'][$master_field] != 'nix') {
202 $sql_query .= ' ON DELETE '
203 . $options_array[$_REQUEST['on_delete'][$master_field]];
205 if ($_REQUEST['on_update'][$master_field] != 'nix') {
206 $sql_query .= ' ON UPDATE '
207 . $options_array[$_REQUEST['on_update'][$master_field]];
209 $sql_query .= ';';
210 $display_query .= $sql_query . "\n";
212 } // end if... else....
213 } elseif (isset($existrel_innodb[$master_field])) {
214 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
215 . ' DROP FOREIGN KEY '
216 . PMA_backquote($existrel_innodb[$master_field]['constraint']);
217 $sql_query .= ';';
218 $display_query .= $sql_query . "\n";
219 } // end if... else....
221 if (! empty($sql_query)) {
222 PMA_DBI_try_query($sql_query);
223 $tmp_error = PMA_DBI_getError();
224 if (! empty($tmp_error)) {
225 $seen_error = true;
227 if (substr($tmp_error, 1, 4) == '1216'
228 || substr($tmp_error, 1, 4) == '1452') {
229 PMA_mysqlDie($tmp_error, $sql_query, FALSE, '', FALSE);
230 echo PMA_showMySQLDocu('manual_Table_types', 'InnoDB_foreign_key_constraints') . "\n";
232 if (substr($tmp_error, 1, 4) == '1005') {
233 $message = PMA_Message::warning('strForeignKeyError');
234 $message->addParam($master_field);
235 $message->display();
236 echo PMA_showMySQLDocu('manual_Table_types', 'InnoDB_foreign_key_constraints') . "\n";
238 unset($tmp_error);
239 $sql_query = '';
241 } // end foreach
242 if (!empty($display_query)) {
243 if ($seen_error) {
244 PMA_showMessage($strError, null, 'error');
245 } else {
246 PMA_showMessage($strSuccess, null, 'success');
249 } // end if isset($destination_innodb)
252 // U p d a t e s f o r d i s p l a y f i e l d
254 if ($cfgRelation['displaywork'] && isset($display_field)) {
255 $upd_query = false;
256 if ($disp) {
257 if ($display_field != '') {
258 $upd_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
259 . ' SET display_field = \'' . PMA_sqlAddslashes($display_field) . '\''
260 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
261 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
262 } else {
263 $upd_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
264 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
265 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
267 } elseif ($display_field != '') {
268 $upd_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
269 . '(db_name, table_name, display_field) '
270 . ' VALUES('
271 . '\'' . PMA_sqlAddslashes($db) . '\','
272 . '\'' . PMA_sqlAddslashes($table) . '\','
273 . '\'' . PMA_sqlAddslashes($display_field) . '\')';
276 if ($upd_query) {
277 PMA_query_as_cu($upd_query);
279 } // end if
281 // If we did an update, refresh our data
282 if (isset($destination) && $cfgRelation['relwork']) {
283 $existrel = PMA_getForeigners($db, $table, '', 'internal');
285 if (isset($destination_innodb) && $tbl_type=='INNODB') {
286 $existrel_innodb = PMA_getForeigners($db, $table, '', 'innodb');
289 if ($cfgRelation['displaywork']) {
290 $disp = PMA_getDisplayField($db, $table);
295 * Dialog
298 // common form
299 echo '<form method="post" action="tbl_relation.php">' . "\n";
300 echo PMA_generate_common_hidden_inputs($db, $table);
303 // relations
305 if ($cfgRelation['relwork'] || $tbl_type == 'INNODB') {
306 // To choose relations we first need all tables names in current db
307 // and if PMA version permits and the main table is innodb,
308 // we use SHOW TABLE STATUS because we need to find other InnoDB tables
310 if ($tbl_type == 'INNODB') {
311 $tab_query = 'SHOW TABLE STATUS FROM ' . PMA_backquote($db);
312 // [0] of the row is the name
313 // [1] is the type
314 } else {
315 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($db);
316 // [0] of the row is the name
319 $tab_rs = PMA_DBI_query($tab_query, null, PMA_DBI_QUERY_STORE);
320 $selectboxall[] = '';
321 $selectboxall_innodb[] = '';
323 while ($curr_table = PMA_DBI_fetch_row($tab_rs)) {
324 $current_table = new PMA_Table($curr_table[0], $db);
326 $selectboxall = array_merge($selectboxall, $current_table->getUniqueColumns());
328 if ($tbl_type == 'INNODB'
329 && isset($curr_table[1])
330 && $curr_table[1] == 'InnoDB') {
331 $selectboxall_innodb = array_merge($selectboxall_innodb, $current_table->getIndexedColumns());
333 } // end while over tables
334 } // end if
337 // Now find out the columns of our $table
338 // need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
339 $col_rs = PMA_DBI_try_query('SHOW COLUMNS FROM ' . PMA_backquote($table) . ';', null, PMA_DBI_QUERY_STORE);
341 if ($col_rs && PMA_DBI_num_rows($col_rs) > 0) {
342 while ($row = PMA_DBI_fetch_assoc($col_rs)) {
343 $save_row[] = $row;
345 $saved_row_cnt = count($save_row);
347 <fieldset>
348 <legend><?php echo $strLinksTo; ?></legend>
350 <table>
351 <tr><th></th>
352 <?php
353 if ($cfgRelation['relwork']) {
354 echo '<th>' . $strInternalRelations;
355 if ($tbl_type=='INNODB') {
356 echo PMA_showHint($strInternalNotNecessary);
358 echo '</th>';
360 if ($tbl_type == 'INNODB') {
361 echo '<th colspan="2">InnoDB';
362 echo '</th>';
365 </tr>
366 <?php
367 $odd_row = true;
368 for ($i = 0; $i < $saved_row_cnt; $i++) {
369 $myfield = $save_row[$i]['Field'];
371 <tr class="<?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
372 <td align="center">
373 <b><?php echo $save_row[$i]['Field']; ?></b></td>
374 <?php
375 if ($cfgRelation['relwork']) {
377 <td><select name="destination[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
378 <?php
379 // PMA internal relations
380 if (isset($existrel[$myfield])) {
381 $foreign_field = $existrel[$myfield]['foreign_db'] . '.'
382 . $existrel[$myfield]['foreign_table'] . '.'
383 . $existrel[$myfield]['foreign_field'];
384 } else {
385 $foreign_field = FALSE;
387 $seen_key = FALSE;
388 foreach ($selectboxall as $value) {
389 echo ' '
390 . '<option value="' . htmlspecialchars($value) . '"';
391 if ($foreign_field && $value == $foreign_field) {
392 echo ' selected="selected"';
393 $seen_key = TRUE;
395 echo '>' . htmlspecialchars($value) . '</option>'. "\n";
396 } // end while
398 // if the link defined in relationtable points to a foreign field
399 // that is not a key in the foreign table, we show the link
400 // (will not be shown with an arrow)
401 if ($foreign_field && !$seen_key) {
402 echo ' '
403 .'<option value="' . htmlspecialchars($foreign_field) . '"'
404 .' selected="selected"'
405 .'>' . $foreign_field . '</option>'. "\n";
408 </select>
409 </td>
410 <?php
411 } // end if (internal relations)
413 if ($tbl_type=='INNODB') {
414 echo '<td>';
415 if (!empty($save_row[$i]['Key'])) {
417 <span class="formelement">
418 <select name="destination_innodb[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
419 <?php
420 if (isset($existrel_innodb[$myfield])) {
421 $foreign_field = $existrel_innodb[$myfield]['foreign_db'] . '.'
422 . $existrel_innodb[$myfield]['foreign_table'] . '.'
423 . $existrel_innodb[$myfield]['foreign_field'];
424 } else {
425 $foreign_field = FALSE;
428 $found_foreign_field = FALSE;
429 foreach ($selectboxall_innodb as $value) {
430 echo ' '
431 . '<option value="' . htmlspecialchars($value) . '"';
432 if ($foreign_field && $value == $foreign_field) {
433 echo ' selected="selected"';
434 $found_foreign_field = TRUE;
436 echo '>' . htmlspecialchars($value) . '</option>'. "\n";
437 } // end while
439 // we did not find the foreign field in the tables of current db,
440 // must be defined in another db so show it to avoid erasing it
441 if (!$found_foreign_field && $foreign_field) {
442 echo ' '
443 . '<option value="' . htmlspecialchars($foreign_field) . '"';
444 echo ' selected="selected"';
445 echo '>' . $foreign_field . '</option>' . "\n";
449 </select>
450 </span>
451 <span class="formelement">
452 <?php
453 PMA_generate_dropdown('ON DELETE',
454 'on_delete[' . $save_row[$i]['Field'] . ']',
455 $options_array,
456 isset($existrel_innodb[$myfield]['on_delete']) ? $existrel_innodb[$myfield]['on_delete']: '');
458 echo '</span>' . "\n"
459 .'<span class="formelement">' . "\n";
461 PMA_generate_dropdown('ON UPDATE',
462 'on_update[' . $save_row[$i]['Field'] . ']',
463 $options_array,
464 isset($existrel_innodb[$myfield]['on_update']) ? $existrel_innodb[$myfield]['on_update']: '');
465 echo '</span>' . "\n";
466 } else {
467 echo $strNoIndex;
468 } // end if (a key exists)
469 echo ' </td>';
470 } // end if (InnoDB)
472 </tr>
473 <?php
474 } // end for
476 echo ' </table>' . "\n";
477 echo '</fieldset>' . "\n";
479 if ($cfgRelation['displaywork']) {
480 // Get "display_field" infos
481 $disp = PMA_getDisplayField($db, $table);
483 <fieldset>
484 <label><?php echo $strChangeDisplay . ': '; ?></label>
485 <select name="display_field" style="vertical-align: middle">
486 <option value="">---</option>
487 <?php
488 foreach ($save_row AS $row) {
489 echo ' <option value="' . htmlspecialchars($row['Field']) . '"';
490 if (isset($disp) && $row['Field'] == $disp) {
491 echo ' selected="selected"';
493 echo '>' . htmlspecialchars($row['Field']) . '</option>'. "\n";
494 } // end while
496 </select>
497 </fieldset>
498 <?php
499 } // end if (displayworks)
501 <fieldset class="tblFooters">
502 <input type="submit" value="<?php echo $strSave; ?>" />
503 </fieldset>
504 </form>
505 <?php
506 } // end if (we have columns in this table)
509 * Displays the footer
511 require_once './libraries/footer.inc.php';