Translated using Weblate.
[phpmyadmin.git] / pmd_relation_new.php
blobf0577ca4ea3011e8a6b987f66b79470c778fdc28
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @package PhpMyAdmin-Designer
6 */
8 /**
11 require_once './libraries/pmd_common.php';
12 $die_save_pos = 0;
13 require_once 'pmd_save_pos.php';
14 extract($_POST, EXTR_SKIP);
16 $tables = PMA_DBI_get_tables_full($db, $T1);
17 $type_T1 = strtoupper($tables[$T1]['ENGINE']);
18 $tables = PMA_DBI_get_tables_full($db, $T2);
19 $type_T2 = strtoupper($tables[$T2]['ENGINE']);
21 // native foreign key
22 if (PMA_foreignkey_supported($type_T1) && PMA_foreignkey_supported($type_T2) && $type_T1 == $type_T2) {
23 // relation exists?
24 $existrel_foreign = PMA_getForeigners($db, $T2, '', 'foreign');
25 if (isset($existrel_foreign[$F2])
26 && isset($existrel_foreign[$F2]['constraint'])) {
27 PMD_return_new(0, __('Error: relation already exists.'));
29 // note: in InnoDB, the index does not requires to be on a PRIMARY
30 // or UNIQUE key
31 // improve: check all other requirements for InnoDB relations
32 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($T1) . ';');
33 $index_array1 = array(); // will be use to emphasis prim. keys in the table view
34 while ($row = PMA_DBI_fetch_assoc($result))
35 $index_array1[$row['Column_name']] = 1;
36 PMA_DBI_free_result($result);
38 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($T2) . ';');
39 $index_array2 = array(); // will be used to emphasis prim. keys in the table view
40 while ($row = PMA_DBI_fetch_assoc($result))
41 $index_array2[$row['Column_name']] = 1;
42 PMA_DBI_free_result($result);
44 if (! empty($index_array1[$F1]) && ! empty($index_array2[$F2])) {
45 $upd_query = 'ALTER TABLE ' . PMA_backquote($T2)
46 . ' ADD FOREIGN KEY ('
47 . PMA_backquote($F2) . ')'
48 . ' REFERENCES '
49 . PMA_backquote($db) . '.'
50 . PMA_backquote($T1) . '('
51 . PMA_backquote($F1) . ')';
53 if ($on_delete != 'nix') {
54 $upd_query .= ' ON DELETE ' . $on_delete;
56 if ($on_update != 'nix') {
57 $upd_query .= ' ON UPDATE ' . $on_update;
59 PMA_DBI_try_query($upd_query) or PMD_return_new(0, __('Error: Relation not added.'));
60 PMD_return_new(1, __('FOREIGN KEY relation added'));
63 // internal (pmadb) relation
64 } else {
65 if ($GLOBALS['cfgRelation']['relwork'] == false) {
66 PMD_return_new(0, _('General relation features') . ':' . _('Disabled'));
67 } else {
68 // no need to recheck if the keys are primary or unique at this point,
69 // this was checked on the interface part
71 $q = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
72 . '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
73 . ' values('
74 . '\'' . PMA_sqlAddSlashes($db) . '\', '
75 . '\'' . PMA_sqlAddSlashes($T2) . '\', '
76 . '\'' . PMA_sqlAddSlashes($F2) . '\', '
77 . '\'' . PMA_sqlAddSlashes($db) . '\', '
78 . '\'' . PMA_sqlAddSlashes($T1) . '\','
79 . '\'' . PMA_sqlAddSlashes($F1) . '\')';
81 if (PMA_query_as_controluser($q, false, PMA_DBI_QUERY_STORE)) {
82 PMD_return_new(1, __('Internal relation added'));
83 } else {
84 PMD_return_new(0, __('Error: Relation not added.'));
89 function PMD_return_new($b,$ret)
91 global $db,$T1,$F1,$T2,$F2;
92 header("Content-Type: text/xml; charset=utf-8");
93 header("Cache-Control: no-cache");
94 die('<root act="relation_new" return="'.$ret.'" b="'.$b.
95 '" DB1="'.urlencode($db).
96 '" T1="'.urlencode($T1).
97 '" F1="'.urlencode($F1).
98 '" DB2="'.urlencode($db).
99 '" T2="'.urlencode($T2).
100 '" F2="'.urlencode($F2).
101 '"></root>');