is*() methods now return boolean values;
[phpmyadmin.git] / tbl_relation.php
blob31844dde4e37ce1d81fa9737b8bfe5b8b5d6ca6a
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$
14 * @package phpMyAdmin
17 /**
18 * Gets some core libraries
20 require_once './libraries/common.inc.php';
21 require_once './libraries/tbl_common.php';
22 $url_query .= '&amp;goto=tbl_sql.php';
25 /**
26 * Gets tables informations
28 require_once './libraries/tbl_info.inc.php';
30 // Note: in libraries/tbl_links.inc.php we get and display the table comment.
31 // For InnoDB, this comment contains the REFER information but any update
32 // has not been done yet (will be done in tbl_relation.php later).
33 $avoid_show_comment = TRUE;
35 /**
36 * Displays top menu links
38 require_once './libraries/tbl_links.inc.php';
40 require_once './libraries/relation.lib.php';
42 $options_array = array(
43 'CASCADE' => 'CASCADE',
44 'SET_NULL' => 'SET NULL',
45 'NO_ACTION' => 'NO ACTION',
46 'RESTRICT' => 'RESTRICT',
49 /**
50 * Generate dropdown choices
52 * @param string Message to display
53 * @param string Name of the <select> field
54 * @param array Choices for dropdown
55 * @return string The existing value (for selected)
57 * @access public
59 function PMA_generate_dropdown($dropdown_question, $select_name, $choices, $selected_value)
61 echo htmlspecialchars($dropdown_question) . '&nbsp;&nbsp;';
63 echo '<select name="' . htmlspecialchars($select_name) . '">' . "\n";
64 echo '<option value=""></option>' . "\n";
66 foreach ($choices as $one_value => $one_label) {
67 echo '<option value="' . htmlspecialchars($one_value) . '"';
68 if ($selected_value == $one_value) {
69 echo ' selected="selected" ';
71 echo '>' . htmlspecialchars($one_label) . '</option>' . "\n";
73 echo '</select>' . "\n";
76 /**
77 * Gets the relation settings
79 $cfgRelation = PMA_getRelationsParam();
82 /**
83 * Updates
85 if ($cfgRelation['relwork']) {
86 $existrel = PMA_getForeigners($db, $table, '', 'internal');
88 if (PMA_foreignkey_supported($tbl_type)) {
89 $existrel_foreign = PMA_getForeigners($db, $table, '', 'foreign');
91 if ($cfgRelation['displaywork']) {
92 $disp = PMA_getDisplayField($db, $table);
95 // 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
96 if (isset($destination) && $cfgRelation['relwork']) {
98 foreach ($destination as $master_field => $foreign_string) {
99 $upd_query = false;
100 if (! empty($foreign_string)) {
101 $foreign_string = trim($foreign_string, '`');
102 list($foreign_db, $foreign_table, $foreign_field) =
103 explode('.', $foreign_string);
104 if (! isset($existrel[$master_field])) {
105 $upd_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
106 . '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
107 . ' values('
108 . '\'' . PMA_sqlAddslashes($db) . '\', '
109 . '\'' . PMA_sqlAddslashes($table) . '\', '
110 . '\'' . PMA_sqlAddslashes($master_field) . '\', '
111 . '\'' . PMA_sqlAddslashes($foreign_db) . '\', '
112 . '\'' . PMA_sqlAddslashes($foreign_table) . '\','
113 . '\'' . PMA_sqlAddslashes($foreign_field) . '\')';
114 } elseif ($existrel[$master_field]['foreign_db'] . '.' .$existrel[$master_field]['foreign_table'] . '.' . $existrel[$master_field]['foreign_field'] != $foreign_string) {
115 $upd_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation']) . ' SET'
116 . ' foreign_db = \'' . PMA_sqlAddslashes($foreign_db) . '\', '
117 . ' foreign_table = \'' . PMA_sqlAddslashes($foreign_table) . '\', '
118 . ' foreign_field = \'' . PMA_sqlAddslashes($foreign_field) . '\' '
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 } elseif (isset($existrel[$master_field])) {
124 $upd_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
125 . ' WHERE master_db = \'' . PMA_sqlAddslashes($db) . '\''
126 . ' AND master_table = \'' . PMA_sqlAddslashes($table) . '\''
127 . ' AND master_field = \'' . PMA_sqlAddslashes($master_field) . '\'';
128 } // end if... else....
129 if ($upd_query) {
130 PMA_query_as_cu($upd_query);
132 } // end while
133 } // end if (updates for internal relations)
135 // u p d a t e s f o r f o r e i g n k e y s
136 // (for now, one index name only; we keep the definitions if the
137 // foreign db is not the same)
138 // I use $sql_query to be able to display directly the query via
139 // PMA_showMessage()
141 if (isset($_REQUEST['destination_foreign'])) {
142 $display_query = '';
143 $seen_error = false;
144 foreach ($_REQUEST['destination_foreign'] as $master_field => $foreign_string) {
145 if (! empty($foreign_string)) {
146 $foreign_string = trim($foreign_string, '`');
147 list($foreign_db, $foreign_table, $foreign_field) =
148 explode('.', $foreign_string);
149 if (!isset($existrel_foreign[$master_field])) {
150 // no key defined for this field
152 // The next few lines are repeated below, so they
153 // could be put in an include file
154 // Note: I tried to enclose the db and table name with
155 // backquotes but MySQL 4.0.16 did not like the syntax
156 // (for example: `base2`.`table1`)
158 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
159 . ' ADD FOREIGN KEY ('
160 . PMA_backquote($master_field) . ')'
161 . ' REFERENCES '
162 . PMA_backquote($foreign_db) . '.'
163 . PMA_backquote($foreign_table) . '('
164 . PMA_backquote($foreign_field) . ')';
166 if (! empty($_REQUEST['on_delete'][$master_field])) {
167 $sql_query .= ' ON DELETE ' . $options_array[$_REQUEST['on_delete'][$master_field]];
169 if (! empty($_REQUEST['on_update'][$master_field])) {
170 $sql_query .= ' ON UPDATE ' . $options_array[$_REQUEST['on_update'][$master_field]];
172 $sql_query .= ';';
173 $display_query .= $sql_query . "\n";
174 // end repeated code
176 } elseif (($existrel_foreign[$master_field]['foreign_db'] . '.' .$existrel_foreign[$master_field]['foreign_table'] . '.' . $existrel_foreign[$master_field]['foreign_field'] != $foreign_string)
177 || ($_REQUEST['on_delete'][$master_field] != (!empty($existrel_foreign[$master_field]['on_delete']) ? $existrel_foreign[$master_field]['on_delete'] : ''))
178 || ($_REQUEST['on_update'][$master_field] != (!empty($existrel_foreign[$master_field]['on_update']) ? $existrel_foreign[$master_field]['on_update'] : ''))
180 // another foreign key is already defined for this field
181 // or
182 // an option has been changed for ON DELETE or ON UPDATE
184 // remove existing key
185 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
186 . ' DROP FOREIGN KEY '
187 . PMA_backquote($existrel_foreign[$master_field]['constraint']) . ';';
189 // I tried to send both in one query but it failed
190 PMA_DBI_query($sql_query);
191 $display_query .= $sql_query . "\n";
193 // add another
194 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
195 . ' ADD FOREIGN KEY ('
196 . PMA_backquote($master_field) . ')'
197 . ' REFERENCES '
198 . PMA_backquote($foreign_db) . '.'
199 . PMA_backquote($foreign_table) . '('
200 . PMA_backquote($foreign_field) . ')';
202 if (! empty($_REQUEST['on_delete'][$master_field])) {
203 $sql_query .= ' ON DELETE '
204 . $options_array[$_REQUEST['on_delete'][$master_field]];
206 if (! empty($_REQUEST['on_update'][$master_field])) {
207 $sql_query .= ' ON UPDATE '
208 . $options_array[$_REQUEST['on_update'][$master_field]];
210 $sql_query .= ';';
211 $display_query .= $sql_query . "\n";
213 } // end if... else....
214 } elseif (isset($existrel_foreign[$master_field])) {
215 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
216 . ' DROP FOREIGN KEY '
217 . PMA_backquote($existrel_foreign[$master_field]['constraint']);
218 $sql_query .= ';';
219 $display_query .= $sql_query . "\n";
220 } // end if... else....
222 if (! empty($sql_query)) {
223 PMA_DBI_try_query($sql_query);
224 $tmp_error = PMA_DBI_getError();
225 if (! empty($tmp_error)) {
226 $seen_error = true;
228 if (substr($tmp_error, 1, 4) == '1216'
229 || substr($tmp_error, 1, 4) == '1452') {
230 PMA_mysqlDie($tmp_error, $sql_query, FALSE, '', FALSE);
231 echo PMA_showMySQLDocu('manual_Table_types', 'InnoDB_foreign_key_constraints') . "\n";
233 if (substr($tmp_error, 1, 4) == '1005') {
234 $message = PMA_Message::warning('strForeignKeyError');
235 $message->addParam($master_field);
236 $message->display();
237 echo PMA_showMySQLDocu('manual_Table_types', 'InnoDB_foreign_key_constraints') . "\n";
239 unset($tmp_error);
240 $sql_query = '';
242 } // end foreach
243 if (!empty($display_query)) {
244 if ($seen_error) {
245 PMA_showMessage($strError, null, 'error');
246 } else {
247 PMA_showMessage($strSuccess, null, 'success');
250 } // end if isset($destination_foreign)
253 // U p d a t e s f o r d i s p l a y f i e l d
255 if ($cfgRelation['displaywork'] && isset($display_field)) {
256 $upd_query = false;
257 if ($disp) {
258 if ($display_field != '') {
259 $upd_query = 'UPDATE ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
260 . ' SET display_field = \'' . PMA_sqlAddslashes($display_field) . '\''
261 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
262 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
263 } else {
264 $upd_query = 'DELETE FROM ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
265 . ' WHERE db_name = \'' . PMA_sqlAddslashes($db) . '\''
266 . ' AND table_name = \'' . PMA_sqlAddslashes($table) . '\'';
268 } elseif ($display_field != '') {
269 $upd_query = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['table_info'])
270 . '(db_name, table_name, display_field) '
271 . ' VALUES('
272 . '\'' . PMA_sqlAddslashes($db) . '\','
273 . '\'' . PMA_sqlAddslashes($table) . '\','
274 . '\'' . PMA_sqlAddslashes($display_field) . '\')';
277 if ($upd_query) {
278 PMA_query_as_cu($upd_query);
280 } // end if
282 // If we did an update, refresh our data
283 if (isset($destination) && $cfgRelation['relwork']) {
284 $existrel = PMA_getForeigners($db, $table, '', 'internal');
286 if (isset($destination_foreign) && PMA_foreignkey_supported($tbl_type)) {
287 $existrel_foreign = PMA_getForeigners($db, $table, '', 'foreign');
290 if ($cfgRelation['displaywork']) {
291 $disp = PMA_getDisplayField($db, $table);
296 * Dialog
299 // common form
300 echo '<form method="post" action="tbl_relation.php">' . "\n";
301 echo PMA_generate_common_hidden_inputs($db, $table);
304 // relations
306 if ($cfgRelation['relwork'] || PMA_foreignkey_supported($tbl_type)) {
307 // To choose relations we first need all tables names in current db
308 // and if the main table supports foreign keys
309 // we use SHOW TABLE STATUS because we need to find other tables of the
310 // same engine.
312 if (PMA_foreignkey_supported($tbl_type)) {
313 $tab_query = 'SHOW TABLE STATUS FROM ' . PMA_backquote($db);
314 // [0] of the row is the name
315 // [1] is the type
316 } else {
317 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($db);
318 // [0] of the row is the name
321 $tab_rs = PMA_DBI_query($tab_query, null, PMA_DBI_QUERY_STORE);
322 $selectboxall[] = '';
323 $selectboxall_foreign[] = '';
325 while ($curr_table = PMA_DBI_fetch_row($tab_rs)) {
326 $current_table = new PMA_Table($curr_table[0], $db);
328 // explicitely ask for non-quoted list of indexed columns
329 $selectboxall = array_merge($selectboxall, $current_table->getUniqueColumns(false));
331 // if foreign keys are supported, collect all keys from other
332 // tables of the same engine
333 if (PMA_foreignkey_supported($tbl_type)
334 && isset($curr_table[1])
335 && strtoupper($curr_table[1]) == $tbl_type) {
336 // explicitely ask for non-quoted list of indexed columns
337 $selectboxall_foreign = array_merge($selectboxall_foreign, $current_table->getIndexedColumns(false));
339 } // end while over tables
340 } // end if
342 // Now find out the columns of our $table
343 // need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
344 $col_rs = PMA_DBI_try_query('SHOW COLUMNS FROM ' . PMA_backquote($table) . ';', null, PMA_DBI_QUERY_STORE);
346 if ($col_rs && PMA_DBI_num_rows($col_rs) > 0) {
347 while ($row = PMA_DBI_fetch_assoc($col_rs)) {
348 $save_row[] = $row;
350 $saved_row_cnt = count($save_row);
352 <fieldset>
353 <legend><?php echo $strLinksTo; ?></legend>
355 <table>
356 <tr><th></th>
357 <?php
358 if ($cfgRelation['relwork']) {
359 echo '<th>' . $strInternalRelations;
360 if (PMA_foreignkey_supported($tbl_type)) {
361 echo PMA_showHint($strInternalAndForeign);
363 echo '</th>';
365 if (PMA_foreignkey_supported($tbl_type)) {
366 // this does not have to be translated, it's part of the MySQL syntax
367 echo '<th colspan="2">FOREIGN KEY (' . $tbl_type . ')';
368 echo '</th>';
371 </tr>
372 <?php
373 $odd_row = true;
374 for ($i = 0; $i < $saved_row_cnt; $i++) {
375 $myfield = $save_row[$i]['Field'];
377 <tr class="<?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
378 <td align="center">
379 <strong><?php echo $save_row[$i]['Field']; ?></strong></td>
380 <?php
381 if ($cfgRelation['relwork']) {
383 <td><select name="destination[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
384 <?php
385 // PMA internal relations
386 if (isset($existrel[$myfield])) {
387 $foreign_field = $existrel[$myfield]['foreign_db'] . '.'
388 . $existrel[$myfield]['foreign_table'] . '.'
389 . $existrel[$myfield]['foreign_field'];
390 } else {
391 $foreign_field = FALSE;
393 $seen_key = FALSE;
394 foreach ($selectboxall as $value) {
395 echo ' '
396 . '<option value="' . htmlspecialchars($value) . '"';
397 if ($foreign_field && $value == $foreign_field) {
398 echo ' selected="selected"';
399 $seen_key = TRUE;
401 echo '>' . htmlspecialchars($value) . '</option>'. "\n";
402 } // end while
404 // if the link defined in relationtable points to a foreign field
405 // that is not a key in the foreign table, we show the link
406 // (will not be shown with an arrow)
407 if ($foreign_field && !$seen_key) {
408 echo ' '
409 .'<option value="' . htmlspecialchars($foreign_field) . '"'
410 .' selected="selected"'
411 .'>' . $foreign_field . '</option>'. "\n";
414 </select>
415 </td>
416 <?php
417 } // end if (internal relations)
419 if (PMA_foreignkey_supported($tbl_type)) {
420 echo '<td>';
421 if (!empty($save_row[$i]['Key'])) {
423 <span class="formelement">
424 <select name="destination_foreign[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
425 <?php
426 if (isset($existrel_foreign[$myfield])) {
427 $foreign_field = $existrel_foreign[$myfield]['foreign_db'] . '.'
428 . $existrel_foreign[$myfield]['foreign_table'] . '.'
429 . $existrel_foreign[$myfield]['foreign_field'];
430 } else {
431 $foreign_field = FALSE;
434 $found_foreign_field = FALSE;
435 foreach ($selectboxall_foreign as $value) {
436 echo ' '
437 . '<option value="' . htmlspecialchars($value) . '"';
438 if ($foreign_field && $value == $foreign_field) {
439 echo ' selected="selected"';
440 $found_foreign_field = TRUE;
442 echo '>' . htmlspecialchars($value) . '</option>'. "\n";
443 } // end while
445 // we did not find the foreign field in the tables of current db,
446 // must be defined in another db so show it to avoid erasing it
447 if (!$found_foreign_field && $foreign_field) {
448 echo ' '
449 . '<option value="' . htmlspecialchars($foreign_field) . '"';
450 echo ' selected="selected"';
451 echo '>' . $foreign_field . '</option>' . "\n";
455 </select>
456 </span>
457 <span class="formelement">
458 <?php
459 PMA_generate_dropdown('ON DELETE',
460 'on_delete[' . $save_row[$i]['Field'] . ']',
461 $options_array,
462 isset($existrel_foreign[$myfield]['on_delete']) ? $existrel_foreign[$myfield]['on_delete']: '');
464 echo '</span>' . "\n"
465 .'<span class="formelement">' . "\n";
467 PMA_generate_dropdown('ON UPDATE',
468 'on_update[' . $save_row[$i]['Field'] . ']',
469 $options_array,
470 isset($existrel_foreign[$myfield]['on_update']) ? $existrel_foreign[$myfield]['on_update']: '');
471 echo '</span>' . "\n";
472 } else {
473 echo $strNoIndex;
474 } // end if (a key exists)
475 echo ' </td>';
476 } // end if (InnoDB)
478 </tr>
479 <?php
480 } // end for
482 echo ' </table>' . "\n";
483 echo '</fieldset>' . "\n";
485 if ($cfgRelation['displaywork']) {
486 // Get "display_field" infos
487 $disp = PMA_getDisplayField($db, $table);
489 <fieldset>
490 <label><?php echo $strChangeDisplay . ': '; ?></label>
491 <select name="display_field" style="vertical-align: middle">
492 <option value="">---</option>
493 <?php
494 foreach ($save_row AS $row) {
495 echo ' <option value="' . htmlspecialchars($row['Field']) . '"';
496 if (isset($disp) && $row['Field'] == $disp) {
497 echo ' selected="selected"';
499 echo '>' . htmlspecialchars($row['Field']) . '</option>'. "\n";
500 } // end while
502 </select>
503 </fieldset>
504 <?php
505 } // end if (displayworks)
507 <fieldset class="tblFooters">
508 <input type="submit" value="<?php echo $strSave; ?>" />
509 </fieldset>
510 </form>
511 <?php
512 } // end if (we have columns in this table)
515 * Displays the footer
517 require_once './libraries/footer.inc.php';