Translated using Weblate (Estonian)
[phpmyadmin.git] / tbl_relation.php
blobb78e0877f54040c3dace266370fd789997ff5c24
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 * for internal relations (but foreign keys relations are correct)
10 * @todo foreign key constraints require both fields being of equal type and size
11 * @todo check foreign fields to be from same type and size, all other makes no sense
12 * @todo if above todos are fullfilled we can add all fields meet requirements
13 * in the select dropdown
14 * @package PhpMyAdmin
17 /**
18 * Gets some core libraries
20 require_once 'libraries/common.inc.php';
21 require_once 'libraries/index.lib.php';
22 require_once 'libraries/tbl_relation.lib.php';
24 $response = PMA_Response::getInstance();
26 // Send table of column names to populate corresponding dropdowns depending
27 // on the current selection
28 if (isset($_REQUEST['getDropdownValues'])
29 && $_REQUEST['getDropdownValues'] === 'true'
30 ) {
31 $foreignDb = $_REQUEST['foreignDb'];
33 if (isset($_REQUEST['foreignTable'])) { // if both db and table are selected
34 $foreignTable = $_REQUEST['foreignTable'];
35 $table_obj = new PMA_Table($foreignTable, $foreignDb);
36 $columns = array();
37 foreach ($table_obj->getUniqueColumns(false, false) as $column) {
38 $columns[] = htmlspecialchars($column);
40 $response->addJSON('columns', $columns);
42 } else { // if only the db is selected
43 $foreign = isset($_REQUEST['foreign']) && $_REQUEST['foreign'] === 'true';
44 if ($foreign) {
45 $query = 'SHOW TABLE STATUS FROM ' . PMA_Util::backquote($foreignDb);
46 $tbl_storage_engine = strtoupper(
47 PMA_Table::sGetStatusInfo(
48 $_REQUEST['db'],
49 $_REQUEST['table'],
50 'Engine'
53 } else {
54 $query = 'SHOW TABLES FROM ' . PMA_Util::backquote($foreignDb);
56 $tables_rs = $GLOBALS['dbi']->query(
57 $query,
58 null,
59 PMA_DatabaseInterface::QUERY_STORE
61 $tables = array();
62 while ($row = $GLOBALS['dbi']->fetchRow($tables_rs)) {
63 if ($foreign) {
64 if (isset($row[1])
65 && strtoupper($row[1]) == $tbl_storage_engine
66 ) {
67 $tables[] = htmlspecialchars($row[0]);
69 } else {
70 $tables[] = htmlspecialchars($row[0]);
73 $response->addJSON('tables', $tables);
75 exit;
78 $header = $response->getHeader();
79 $scripts = $header->getScripts();
80 $scripts->addFile('tbl_relation.js');
81 $scripts->addFile('indexes.js');
83 /**
84 * Sets globals from $_POST
86 $post_params = array(
87 'destination_db',
88 'destination_table',
89 'destination_column',
90 'destination_foreign_db',
91 'destination_foreign_table',
92 'destination_foreign_column',
93 'display_field',
94 'fields_name',
95 'on_delete',
96 'on_update'
99 foreach ($post_params as $one_post_param) {
100 if (isset($_POST[$one_post_param])) {
101 $GLOBALS[$one_post_param] = $_POST[$one_post_param];
106 * Gets tables informations
108 require_once 'libraries/tbl_info.inc.php';
110 $options_array = array(
111 'CASCADE' => 'CASCADE',
112 'SET_NULL' => 'SET NULL',
113 'NO_ACTION' => 'NO ACTION',
114 'RESTRICT' => 'RESTRICT',
118 * Gets the relation settings
120 $cfgRelation = PMA_getRelationsParam();
123 * Updates
125 if ($cfgRelation['relwork']) {
126 $existrel = PMA_getForeigners($db, $table, '', 'internal');
128 if (PMA_Util::isForeignKeySupported($tbl_storage_engine)) {
129 $existrel_foreign = PMA_getForeigners($db, $table, '', 'foreign');
131 if ($cfgRelation['displaywork']) {
132 $disp = PMA_getDisplayField($db, $table);
135 // will be used in the logic for internal relations and foreign keys:
136 $multi_edit_columns_name = isset($_REQUEST['fields_name'])
137 ? $_REQUEST['fields_name']
138 : null;
140 $html_output = '';
142 // 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
143 if (isset($destination_db) && $cfgRelation['relwork']) {
145 foreach ($destination_db as $master_field_md5 => $foreign_db) {
146 $upd_query = false;
148 // Map the fieldname's md5 back to its real name
149 $master_field = $multi_edit_columns_name[$master_field_md5];
151 $foreign_table = $destination_table[$master_field_md5];
152 $foreign_field = $destination_column[$master_field_md5];
153 if (! empty($foreign_db)
154 && ! empty($foreign_table)
155 && ! empty($foreign_field)
157 if (! isset($existrel[$master_field])) {
158 $upd_query = 'INSERT INTO '
159 . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
160 . '.' . PMA_Util::backquote($cfgRelation['relation'])
161 . '(master_db, master_table, master_field, foreign_db,'
162 . ' foreign_table, foreign_field)'
163 . ' values('
164 . '\'' . PMA_Util::sqlAddSlashes($db) . '\', '
165 . '\'' . PMA_Util::sqlAddSlashes($table) . '\', '
166 . '\'' . PMA_Util::sqlAddSlashes($master_field) . '\', '
167 . '\'' . PMA_Util::sqlAddSlashes($foreign_db) . '\', '
168 . '\'' . PMA_Util::sqlAddSlashes($foreign_table) . '\','
169 . '\'' . PMA_Util::sqlAddSlashes($foreign_field) . '\')';
171 } elseif ($existrel[$master_field]['foreign_db'] != $foreign_db
172 || $existrel[$master_field]['foreign_table'] != $foreign_table
173 || $existrel[$master_field]['foreign_field'] != $foreign_field
175 $upd_query = 'UPDATE '
176 . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
177 . '.' . PMA_Util::backquote($cfgRelation['relation']) . ' SET'
178 . ' foreign_db = \''
179 . PMA_Util::sqlAddSlashes($foreign_db) . '\', '
180 . ' foreign_table = \''
181 . PMA_Util::sqlAddSlashes($foreign_table) . '\', '
182 . ' foreign_field = \''
183 . PMA_Util::sqlAddSlashes($foreign_field) . '\' '
184 . ' WHERE master_db = \''
185 . PMA_Util::sqlAddSlashes($db) . '\''
186 . ' AND master_table = \''
187 . PMA_Util::sqlAddSlashes($table) . '\''
188 . ' AND master_field = \''
189 . PMA_Util::sqlAddSlashes($master_field) . '\'';
190 } // end if... else....
191 } elseif (isset($existrel[$master_field])) {
192 $upd_query = 'DELETE FROM '
193 . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
194 . '.' . PMA_Util::backquote($cfgRelation['relation'])
195 . ' WHERE master_db = \'' . PMA_Util::sqlAddSlashes($db) . '\''
196 . ' AND master_table = \'' . PMA_Util::sqlAddSlashes($table) . '\''
197 . ' AND master_field = \'' . PMA_Util::sqlAddSlashes($master_field)
198 . '\'';
199 } // end if... else....
200 if ($upd_query) {
201 PMA_queryAsControlUser($upd_query);
203 } // end while
204 } // end if (updates for internal relations)
206 // u p d a t e s f o r f o r e i g n k e y s
207 // (for now, one index name only; we keep the definitions if the
208 // foreign db is not the same)
210 if (isset($destination_foreign_db)) {
211 $display_query = '';
212 $seen_error = false;
213 foreach ($destination_foreign_db as $master_field_md5 => $foreign_db) {
214 $create = false;
215 $drop = false;
217 // Map the fieldname's md5 back to it's real name
218 $master_field = $multi_edit_columns_name[$master_field_md5];
220 $foreign_table = $destination_foreign_table[$master_field_md5];
221 $foreign_field = $destination_foreign_column[$master_field_md5];
222 if (! empty($foreign_db)
223 && ! empty($foreign_table)
224 && ! empty($foreign_field)
226 if (! isset($existrel_foreign[$master_field])) {
227 // no key defined for this field
228 $create = true;
229 } elseif ($existrel_foreign[$master_field]['foreign_db'] != $foreign_db
230 || $existrel_foreign[$master_field]['foreign_table'] != $foreign_table
231 || $existrel_foreign[$master_field]['foreign_field'] != $foreign_field
232 || $_REQUEST['constraint_name'][$master_field_md5] != $existrel_foreign[$master_field]['constraint']
233 || ($_REQUEST['on_delete'][$master_field_md5] != (! empty($existrel_foreign[$master_field]['on_delete']) ? $existrel_foreign[$master_field]['on_delete'] : 'RESTRICT'))
234 || ($_REQUEST['on_update'][$master_field_md5] != (! empty($existrel_foreign[$master_field]['on_update']) ? $existrel_foreign[$master_field]['on_update'] : 'RESTRICT'))
236 // another foreign key is already defined for this field
237 // or an option has been changed for ON DELETE or ON UPDATE
238 $drop = true;
239 $create = true;
240 } // end if... else....
241 } elseif (isset($existrel_foreign[$master_field])) {
242 $drop = true;
243 } // end if... else....
245 $tmp_error_drop = false;
246 if ($drop) {
247 $drop_query = PMA_getSQLToDropForeignKey(
248 $table, $existrel_foreign[$master_field]['constraint']
250 $display_query .= $drop_query . "\n";
251 $GLOBALS['dbi']->tryQuery($drop_query);
252 $tmp_error_drop = $GLOBALS['dbi']->getError();
254 if (! empty($tmp_error_drop)) {
255 $seen_error = true;
256 $html_output .= PMA_Util::mysqlDie(
257 $tmp_error_drop, $drop_query, false, '', false
259 continue;
262 $tmp_error_create = false;
263 if ($create) {
264 $create_query = PMA_getSQLToCreateForeignKey(
265 $table, $master_field, $foreign_db, $foreign_table, $foreign_field,
266 $_REQUEST['constraint_name'][$master_field_md5],
267 $options_array[$_REQUEST['on_delete'][$master_field_md5]],
268 $options_array[$_REQUEST['on_update'][$master_field_md5]]
271 $display_query .= $create_query . "\n";
272 $GLOBALS['dbi']->tryQuery($create_query);
273 $tmp_error_create = $GLOBALS['dbi']->getError();
274 if (! empty($tmp_error_create)) {
275 $seen_error = true;
277 if (substr($tmp_error_create, 1, 4) == '1005') {
278 $message = PMA_Message::error(
279 __('Error creating foreign key on %1$s (check data types)')
281 $message->addParam($master_field);
282 $message->display();
283 } else {
284 $html_output .= PMA_Util::mysqlDie(
285 $tmp_error_create, $create_query, false, '', false
288 $html_output .= PMA_Util::showMySQLDocu(
289 'InnoDB_foreign_key_constraints'
290 ) . "\n";
293 // this is an alteration and the old constraint has been dropped
294 // without creation of a new one
295 if ($drop && $create && empty($tmp_error_drop)
296 && ! empty($tmp_error_create)
298 // a rollback may be better here
299 $sql_query_recreate = '# Restoring the dropped constraint...' . "\n";
300 $sql_query_recreate .= PMA_getSQLToCreateForeignKey(
301 $table,
302 $master_field,
303 $existrel_foreign[$master_field]['foreign_db'],
304 $existrel_foreign[$master_field]['foreign_table'],
305 $existrel_foreign[$master_field]['foreign_field'],
306 $existrel_foreign[$master_field]['constraint'],
307 $options_array[$existrel_foreign[$master_field]['on_delete']],
308 $options_array[$existrel_foreign[$master_field]['on_update']]
310 $display_query .= $sql_query_recreate . "\n";
311 $GLOBALS['dbi']->tryQuery($sql_query_recreate);
314 } // end foreach
315 if (! empty($display_query) && ! $seen_error) {
316 $html_output .= PMA_Util::getMessage(
317 __('Your SQL query has been executed successfully'),
318 null, 'success'
321 } // end if isset($destination_foreign)
324 // U p d a t e s f o r d i s p l a y f i e l d
326 if ($cfgRelation['displaywork'] && isset($display_field)) {
327 $upd_query = false;
328 if ($disp) {
329 if ($display_field != '') {
330 $upd_query = 'UPDATE '
331 . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
332 . '.' . PMA_Util::backquote($cfgRelation['table_info'])
333 . ' SET display_field = \''
334 . PMA_Util::sqlAddSlashes($display_field) . '\''
335 . ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\''
336 . ' AND table_name = \'' . PMA_Util::sqlAddSlashes($table) . '\'';
337 } else {
338 $upd_query = 'DELETE FROM '
339 . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
340 . '.' . PMA_Util::backquote($cfgRelation['table_info'])
341 . ' WHERE db_name = \'' . PMA_Util::sqlAddSlashes($db) . '\''
342 . ' AND table_name = \'' . PMA_Util::sqlAddSlashes($table) . '\'';
344 } elseif ($display_field != '') {
345 $upd_query = 'INSERT INTO '
346 . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
347 . '.' . PMA_Util::backquote($cfgRelation['table_info'])
348 . '(db_name, table_name, display_field) VALUES('
349 . '\'' . PMA_Util::sqlAddSlashes($db) . '\','
350 . '\'' . PMA_Util::sqlAddSlashes($table) . '\','
351 . '\'' . PMA_Util::sqlAddSlashes($display_field) . '\')';
354 if ($upd_query) {
355 PMA_queryAsControlUser($upd_query);
357 } // end if
359 // If we did an update, refresh our data
360 if (isset($destination_db) && $cfgRelation['relwork']) {
361 $existrel = PMA_getForeigners($db, $table, '', 'internal');
363 if (isset($destination_foreign_db)
364 && PMA_Util::isForeignKeySupported($tbl_storage_engine)
366 $existrel_foreign = PMA_getForeigners($db, $table, '', 'foreign');
369 if ($cfgRelation['displaywork']) {
370 $disp = PMA_getDisplayField($db, $table);
375 * Dialog
378 // common form
379 $html_output .= '<form method="post" action="tbl_relation.php">' . "\n"
380 . PMA_URL_getHiddenInputs($db, $table);
382 // Now find out the columns of our $table
383 // need to use PMA_DatabaseInterface::QUERY_STORE with $GLOBALS['dbi']->numRows() in mysqli
384 $columns = $GLOBALS['dbi']->getColumns($db, $table);
386 if (count($columns) > 0) {
388 foreach ($columns as $row) {
389 $save_row[] = $row;
392 $saved_row_cnt = count($save_row);
393 $html_output .= '<fieldset>'
394 . '<legend>' . __('Relations'). '</legend>'
395 . '<table id="relationalTable">'
396 . '<tr><th>' . __('Column') . '</th>';
398 if ($cfgRelation['relwork']) {
399 $html_output .= '<th>' . __('Internal relation');
400 if (PMA_Util::isForeignKeySupported($tbl_storage_engine)) {
401 $html_output .= PMA_Util::showHint(
403 'An internal relation is not necessary when a corresponding'
404 . ' FOREIGN KEY relation exists.'
408 $html_output .= '</th>';
411 if (PMA_Util::isForeignKeySupported($tbl_storage_engine)) {
412 // this does not have to be translated, it's part of the MySQL syntax
413 $html_output .= '<th colspan="2">' . __('Foreign key constraint')
414 . ' (' . $tbl_storage_engine . ')';
415 $html_output .= '</th>';
417 $html_output .= '</tr>';
419 $odd_row = true;
420 for ($i = 0; $i < $saved_row_cnt; $i++) {
421 $myfield = $save_row[$i]['Field'];
422 // Use an md5 as array index to avoid having special characters
423 // in the name atttibure (see bug #1746964 )
424 $myfield_md5 = md5($myfield);
425 $myfield_html = htmlspecialchars($myfield);
427 $html_output .= '<tr class="' . ($odd_row ? 'odd' : 'even') . '">'
428 . '<td class="center">'
429 . '<strong>' . $myfield_html . '</strong>'
430 . '<input type="hidden" name="fields_name[' . $myfield_md5 . ']"'
431 . ' value="' . $myfield_html . '"/>'
432 . '</td>';
433 $odd_row = ! $odd_row;
435 if ($cfgRelation['relwork']) {
436 $html_output .= '<td>';
438 $foreign_db = false;
439 $foreign_table = false;
440 $foreign_column = false;
442 // database dropdown
443 if (isset($existrel[$myfield])) {
444 $foreign_db = $existrel[$myfield]['foreign_db'];
445 } else {
446 $foreign_db = $db;
448 $html_output .= PMA_generateRelationalDropdown(
449 'destination_db[' . $myfield_md5 . ']',
450 $GLOBALS['pma']->databases,
451 $foreign_db,
452 __('Database')
454 // end of database dropdown
456 // table dropdown
457 $tables = array();
458 if ($foreign_db) {
459 if (isset($existrel[$myfield])) {
460 $foreign_table = $existrel[$myfield]['foreign_table'];
462 $tables_rs = $GLOBALS['dbi']->query(
463 'SHOW TABLES FROM ' . PMA_Util::backquote($foreign_db),
464 null,
465 PMA_DatabaseInterface::QUERY_STORE
467 while ($row = $GLOBALS['dbi']->fetchRow($tables_rs)) {
468 $tables[] = $row[0];
471 $html_output .= PMA_generateRelationalDropdown(
472 'destination_table[' . $myfield_md5 . ']',
473 $tables,
474 $foreign_table,
475 __('Table')
477 // end of table dropdown
479 // column dropdown
480 $columns = array();
481 if ($foreign_db && $foreign_table) {
482 if (isset($existrel[$myfield])) {
483 $foreign_column = $existrel[$myfield]['foreign_field'];
485 $table_obj = new PMA_Table($foreign_table, $foreign_db);
486 $columns = $table_obj->getUniqueColumns(false, false);
488 $html_output .= PMA_generateRelationalDropdown(
489 'destination_column[' . $myfield_md5 . ']',
490 $columns,
491 $foreign_column,
492 __('Column')
494 // end of column dropdown
496 $html_output .= '</td>';
497 } // end if (internal relations)
499 if (PMA_Util::isForeignKeySupported($tbl_storage_engine)) {
500 $html_output .= '<td>';
501 if (! empty($save_row[$i]['Key'])) {
503 $foreign_db = false;
504 $foreign_table = false;
505 $foreign_column = false;
507 // foreign database dropdown
508 if (isset($existrel_foreign[$myfield])) {
509 $foreign_db = $existrel_foreign[$myfield]['foreign_db'];
510 } else {
511 $foreign_db = $db;
513 $html_output .= '<span class="formelement clearfloat">';
514 $html_output .= PMA_generateRelationalDropdown(
515 'destination_foreign_db[' . $myfield_md5 . ']',
516 $GLOBALS['pma']->databases,
517 $foreign_db,
518 __('Database')
520 // end of foreign database dropdown
522 // foreign table dropdown
523 $tables = array();
524 if ($foreign_db) {
525 if (isset($existrel_foreign[$myfield])) {
526 $foreign_table = $existrel_foreign[$myfield]['foreign_table'];
528 $tables_rs = $GLOBALS['dbi']->query(
529 'SHOW TABLE STATUS FROM ' . PMA_Util::backquote($foreign_db),
530 null,
531 PMA_DatabaseInterface::QUERY_STORE
533 while ($row = $GLOBALS['dbi']->fetchRow($tables_rs)) {
534 if (isset($row[1])
535 && strtoupper($row[1]) == $tbl_storage_engine
537 $tables[] = $row[0];
541 $html_output .= PMA_generateRelationalDropdown(
542 'destination_foreign_table[' . $myfield_md5 . ']',
543 $tables,
544 $foreign_table,
545 __('Table')
547 // end of foreign table dropdown
549 // foreign column dropdown
550 $columns = array();
551 if ($foreign_db && $foreign_table) {
552 if (isset($existrel_foreign[$myfield])) {
553 $foreign_column = $existrel_foreign[$myfield]['foreign_field'];
555 $table_obj = new PMA_Table($foreign_table, $foreign_db);
556 $columns = $table_obj->getUniqueColumns(false, false);
558 $html_output .= PMA_generateRelationalDropdown(
559 'destination_foreign_column[' . $myfield_md5 . ']',
560 $columns,
561 $foreign_column,
562 __('Column')
564 $html_output .= '</span>';
565 // end of foreign column dropdown
567 // For constraint name
568 $html_output .= '<span class="formelement clearfloat">';
569 $constraint_name = isset($existrel_foreign[$myfield]['constraint'])
570 ? $existrel_foreign[$myfield]['constraint'] : '';
571 $html_output .= __('Constraint name');
572 $html_output .= '<input type="text" name="constraint_name['
573 . $myfield_md5 . ']"'
574 . ' value="' . $constraint_name . '"/>';
575 $html_output .= '</span>' . "\n";
577 $html_output .= '<span class="formelement clearfloat">';
578 // For ON DELETE and ON UPDATE, the default action
579 // is RESTRICT as per MySQL doc; however, a SHOW CREATE TABLE
580 // won't display the clause if it's set as RESTRICT.
581 $on_delete = isset($existrel_foreign[$myfield]['on_delete'])
582 ? $existrel_foreign[$myfield]['on_delete'] : 'RESTRICT';
583 $html_output .= PMA_generateDropdown(
584 'ON DELETE',
585 'on_delete[' . $myfield_md5 . ']',
586 $options_array,
587 $on_delete
589 $html_output .= '</span>' . "\n";
591 $html_output .= '<span class="formelement clearfloat">' . "\n";
592 $on_update = isset($existrel_foreign[$myfield]['on_update'])
593 ? $existrel_foreign[$myfield]['on_update'] : 'RESTRICT';
594 $html_output .= PMA_generateDropdown(
595 'ON UPDATE',
596 'on_update[' . $myfield_md5 . ']',
597 $options_array,
598 $on_update
600 $html_output .= '</span>' . "\n";
601 } else {
602 $html_output .= __('No index defined! Create one below');
603 } // end if (a key exists)
604 $html_output .= '</td>';
605 } // end if (InnoDB)
606 $html_output .= '</tr>';
607 } // end for
609 unset( $myfield, $myfield_md5, $myfield_html);
610 $html_output .= '</table>' . "\n"
611 . '</fieldset>' . "\n";
613 if ($cfgRelation['displaywork']) {
614 // Get "display_field" infos
615 $disp = PMA_getDisplayField($db, $table);
616 $html_output .= '<fieldset>'
617 . '<label>' . __('Choose column to display:') . '</label>'
618 . '<select name="display_field">'
619 . '<option value="">---</option>';
621 foreach ($save_row as $row) {
622 $html_output .= '<option value="'
623 . htmlspecialchars($row['Field']) . '"';
624 if (isset($disp) && $row['Field'] == $disp) {
625 $html_output .= ' selected="selected"';
627 $html_output .= '>' . htmlspecialchars($row['Field'])
628 . '</option>'. "\n";
629 } // end while
631 $html_output .= '</select>'
632 . '</fieldset>';
633 } // end if (displayworks)
635 $html_output .= '<fieldset class="tblFooters">'
636 . '<input type="submit" value="' . __('Save') . '" />'
637 . '</fieldset>'
638 . '</form>';
639 } // end if (we have columns in this table)
641 if (PMA_Util::isForeignKeySupported($tbl_storage_engine)) {
642 $html_output .= '<div id="index_div" class="ajax" >'
643 . PMA_getHtmlForDisplayIndexes();
645 // Render HTML output
646 PMA_Response::getInstance()->addHTML($html_output);