Cookie does not have to be set here.
[phpmyadmin/crack.git] / tbl_relation.php
blob32fb38dd6a9b158b38c6aada4c540e3f91c1707b
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, $select_name, $choices, $selected_value)
60 echo htmlspecialchars($dropdown_question) . '&nbsp;&nbsp;';
62 echo '<select name="' . htmlspecialchars($select_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 (PMA_foreignkey_supported($tbl_type)) {
88 $existrel_foreign = PMA_getForeigners($db, $table, '', 'foreign');
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 f o r e i g n k e y s
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_foreign'])) {
141 $display_query = '';
142 $seen_error = false;
143 foreach ($_REQUEST['destination_foreign'] 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_foreign[$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_foreign[$master_field]['foreign_db'] . '.' .$existrel_foreign[$master_field]['foreign_table'] . '.' . $existrel_foreign[$master_field]['foreign_field'] != $foreign_string)
176 || ($_REQUEST['on_delete'][$master_field] != (!empty($existrel_foreign[$master_field]['on_delete']) ? $existrel_foreign[$master_field]['on_delete'] : ''))
177 || ($_REQUEST['on_update'][$master_field] != (!empty($existrel_foreign[$master_field]['on_update']) ? $existrel_foreign[$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_foreign[$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 (! empty($_REQUEST['on_delete'][$master_field])) {
202 $sql_query .= ' ON DELETE '
203 . $options_array[$_REQUEST['on_delete'][$master_field]];
205 if (! empty($_REQUEST['on_update'][$master_field])) {
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_foreign[$master_field])) {
214 $sql_query = 'ALTER TABLE ' . PMA_backquote($table)
215 . ' DROP FOREIGN KEY '
216 . PMA_backquote($existrel_foreign[$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_foreign)
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_foreign) && PMA_foreignkey_supported($tbl_type)) {
286 $existrel_foreign = PMA_getForeigners($db, $table, '', 'foreign');
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'] || PMA_foreignkey_supported($tbl_type)) {
306 // To choose relations we first need all tables names in current db
307 // and if the main table supports foreign keys
308 // we use SHOW TABLE STATUS because we need to find other tables of the
309 // same engine.
311 if (PMA_foreignkey_supported($tbl_type)) {
312 $tab_query = 'SHOW TABLE STATUS FROM ' . PMA_backquote($db);
313 // [0] of the row is the name
314 // [1] is the type
315 } else {
316 $tab_query = 'SHOW TABLES FROM ' . PMA_backquote($db);
317 // [0] of the row is the name
320 $tab_rs = PMA_DBI_query($tab_query, null, PMA_DBI_QUERY_STORE);
321 $selectboxall[] = '';
322 $selectboxall_foreign[] = '';
324 while ($curr_table = PMA_DBI_fetch_row($tab_rs)) {
325 $current_table = new PMA_Table($curr_table[0], $db);
327 // explicitely ask for non-quoted list of indexed columns
328 $selectboxall = array_merge($selectboxall, $current_table->getUniqueColumns(false));
330 // if foreign keys are supported, collect all keys from other
331 // tables of the same engine
332 if (PMA_foreignkey_supported($tbl_type)
333 && isset($curr_table[1])
334 && strtoupper($curr_table[1]) == $tbl_type) {
335 // explicitely ask for non-quoted list of indexed columns
336 $selectboxall_foreign = array_merge($selectboxall_foreign, $current_table->getIndexedColumns(false));
338 } // end while over tables
339 } // end if
341 // Now find out the columns of our $table
342 // need to use PMA_DBI_QUERY_STORE with PMA_DBI_num_rows() in mysqli
343 $col_rs = PMA_DBI_try_query('SHOW COLUMNS FROM ' . PMA_backquote($table) . ';', null, PMA_DBI_QUERY_STORE);
345 if ($col_rs && PMA_DBI_num_rows($col_rs) > 0) {
346 while ($row = PMA_DBI_fetch_assoc($col_rs)) {
347 $save_row[] = $row;
349 $saved_row_cnt = count($save_row);
351 <fieldset>
352 <legend><?php echo $strLinksTo; ?></legend>
354 <table>
355 <tr><th></th>
356 <?php
357 if ($cfgRelation['relwork']) {
358 echo '<th>' . $strInternalRelations;
359 if (PMA_foreignkey_supported($tbl_type)) {
360 echo PMA_showHint($strInternalAndForeign);
362 echo '</th>';
364 if (PMA_foreignkey_supported($tbl_type)) {
365 // this does not have to be translated, it's part of the MySQL syntax
366 echo '<th colspan="2">FOREIGN KEY (' . $tbl_type . ')';
367 echo '</th>';
370 </tr>
371 <?php
372 $odd_row = true;
373 for ($i = 0; $i < $saved_row_cnt; $i++) {
374 $myfield = $save_row[$i]['Field'];
376 <tr class="<?php echo $odd_row ? 'odd' : 'even'; $odd_row = ! $odd_row; ?>">
377 <td align="center">
378 <strong><?php echo $save_row[$i]['Field']; ?></strong></td>
379 <?php
380 if ($cfgRelation['relwork']) {
382 <td><select name="destination[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
383 <?php
384 // PMA internal relations
385 if (isset($existrel[$myfield])) {
386 $foreign_field = $existrel[$myfield]['foreign_db'] . '.'
387 . $existrel[$myfield]['foreign_table'] . '.'
388 . $existrel[$myfield]['foreign_field'];
389 } else {
390 $foreign_field = FALSE;
392 $seen_key = FALSE;
393 foreach ($selectboxall as $value) {
394 echo ' '
395 . '<option value="' . htmlspecialchars($value) . '"';
396 if ($foreign_field && $value == $foreign_field) {
397 echo ' selected="selected"';
398 $seen_key = TRUE;
400 echo '>' . htmlspecialchars($value) . '</option>'. "\n";
401 } // end while
403 // if the link defined in relationtable points to a foreign field
404 // that is not a key in the foreign table, we show the link
405 // (will not be shown with an arrow)
406 if ($foreign_field && !$seen_key) {
407 echo ' '
408 .'<option value="' . htmlspecialchars($foreign_field) . '"'
409 .' selected="selected"'
410 .'>' . $foreign_field . '</option>'. "\n";
413 </select>
414 </td>
415 <?php
416 } // end if (internal relations)
418 if (PMA_foreignkey_supported($tbl_type)) {
419 echo '<td>';
420 if (!empty($save_row[$i]['Key'])) {
422 <span class="formelement">
423 <select name="destination_foreign[<?php echo htmlspecialchars($save_row[$i]['Field']); ?>]">
424 <?php
425 if (isset($existrel_foreign[$myfield])) {
426 $foreign_field = $existrel_foreign[$myfield]['foreign_db'] . '.'
427 . $existrel_foreign[$myfield]['foreign_table'] . '.'
428 . $existrel_foreign[$myfield]['foreign_field'];
429 } else {
430 $foreign_field = FALSE;
433 $found_foreign_field = FALSE;
434 foreach ($selectboxall_foreign as $value) {
435 echo ' '
436 . '<option value="' . htmlspecialchars($value) . '"';
437 if ($foreign_field && $value == $foreign_field) {
438 echo ' selected="selected"';
439 $found_foreign_field = TRUE;
441 echo '>' . htmlspecialchars($value) . '</option>'. "\n";
442 } // end while
444 // we did not find the foreign field in the tables of current db,
445 // must be defined in another db so show it to avoid erasing it
446 if (!$found_foreign_field && $foreign_field) {
447 echo ' '
448 . '<option value="' . htmlspecialchars($foreign_field) . '"';
449 echo ' selected="selected"';
450 echo '>' . $foreign_field . '</option>' . "\n";
454 </select>
455 </span>
456 <span class="formelement">
457 <?php
458 PMA_generate_dropdown('ON DELETE',
459 'on_delete[' . $save_row[$i]['Field'] . ']',
460 $options_array,
461 isset($existrel_foreign[$myfield]['on_delete']) ? $existrel_foreign[$myfield]['on_delete']: '');
463 echo '</span>' . "\n"
464 .'<span class="formelement">' . "\n";
466 PMA_generate_dropdown('ON UPDATE',
467 'on_update[' . $save_row[$i]['Field'] . ']',
468 $options_array,
469 isset($existrel_foreign[$myfield]['on_update']) ? $existrel_foreign[$myfield]['on_update']: '');
470 echo '</span>' . "\n";
471 } else {
472 echo $strNoIndex;
473 } // end if (a key exists)
474 echo ' </td>';
475 } // end if (InnoDB)
477 </tr>
478 <?php
479 } // end for
481 echo ' </table>' . "\n";
482 echo '</fieldset>' . "\n";
484 if ($cfgRelation['displaywork']) {
485 // Get "display_field" infos
486 $disp = PMA_getDisplayField($db, $table);
488 <fieldset>
489 <label><?php echo $strChangeDisplay . ': '; ?></label>
490 <select name="display_field" style="vertical-align: middle">
491 <option value="">---</option>
492 <?php
493 foreach ($save_row AS $row) {
494 echo ' <option value="' . htmlspecialchars($row['Field']) . '"';
495 if (isset($disp) && $row['Field'] == $disp) {
496 echo ' selected="selected"';
498 echo '>' . htmlspecialchars($row['Field']) . '</option>'. "\n";
499 } // end while
501 </select>
502 </fieldset>
503 <?php
504 } // end if (displayworks)
506 <fieldset class="tblFooters">
507 <input type="submit" value="<?php echo $strSave; ?>" />
508 </fieldset>
509 </form>
510 <?php
511 } // end if (we have columns in this table)
514 * Displays the footer
516 require_once './libraries/footer.inc.php';