Translated using Weblate (Czech)
[phpmyadmin.git] / pmd_relation_new.php
blob60247edf1acbcd66a9504af42704942da60068b2
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * PMD handler for creating new relation
6 * @package PhpMyAdmin-Designer
7 */
9 /**
12 require_once './libraries/common.inc.php';
14 PMA_Response::getInstance()->disable();
16 require_once 'libraries/pmd_common.php';
17 $die_save_pos = 0;
18 require_once 'pmd_save_pos.php';
19 extract($_POST, EXTR_SKIP);
21 $tables = $GLOBALS['dbi']->getTablesFull($db, $T1);
22 $type_T1 = strtoupper($tables[$T1]['ENGINE']);
23 $tables = $GLOBALS['dbi']->getTablesFull($db, $T2);
24 $type_T2 = strtoupper($tables[$T2]['ENGINE']);
26 // native foreign key
27 if (PMA_Util::isForeignKeySupported($type_T1)
28 && PMA_Util::isForeignKeySupported($type_T2)
29 && $type_T1 == $type_T2
30 ) {
31 // relation exists?
32 $existrel_foreign = PMA_getForeigners($db, $T2, '', 'foreign');
33 if (isset($existrel_foreign[$F2])
34 && isset($existrel_foreign[$F2]['constraint'])
35 ) {
36 PMD_return_new(0, __('Error: relation already exists.'));
38 // note: in InnoDB, the index does not requires to be on a PRIMARY
39 // or UNIQUE key
40 // improve: check all other requirements for InnoDB relations
41 $result = $GLOBALS['dbi']->query(
42 'SHOW INDEX FROM ' . PMA_Util::backquote($db)
43 . '.' . PMA_Util::backquote($T1) . ';'
45 $index_array1 = array(); // will be use to emphasis prim. keys in the table view
46 while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
47 $index_array1[$row['Column_name']] = 1;
49 $GLOBALS['dbi']->freeResult($result);
51 $result = $GLOBALS['dbi']->query(
52 'SHOW INDEX FROM ' . PMA_Util::backquote($db)
53 . '.' . PMA_Util::backquote($T2) . ';'
55 // will be used to emphasis prim. keys in the table view
56 $index_array2 = array();
57 while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
58 $index_array2[$row['Column_name']] = 1;
60 $GLOBALS['dbi']->freeResult($result);
62 if (! empty($index_array1[$F1]) && ! empty($index_array2[$F2])) {
63 $upd_query = 'ALTER TABLE ' . PMA_Util::backquote($db)
64 . '.' . PMA_Util::backquote($T2)
65 . ' ADD FOREIGN KEY ('
66 . PMA_Util::backquote($F2) . ')'
67 . ' REFERENCES '
68 . PMA_Util::backquote($db) . '.'
69 . PMA_Util::backquote($T1) . '('
70 . PMA_Util::backquote($F1) . ')';
72 if ($on_delete != 'nix') {
73 $upd_query .= ' ON DELETE ' . $on_delete;
75 if ($on_update != 'nix') {
76 $upd_query .= ' ON UPDATE ' . $on_update;
78 $upd_query .= ';';
79 $GLOBALS['dbi']->tryQuery($upd_query) or PMD_return_new(0, __('Error: Relation not added.'));
80 PMD_return_new(1, __('FOREIGN KEY relation added'));
82 } else { // internal (pmadb) relation
83 if ($GLOBALS['cfgRelation']['relwork'] == false) {
84 PMD_return_new(0, __('Error: Relational features are disabled!'));
85 } else {
86 // no need to recheck if the keys are primary or unique at this point,
87 // this was checked on the interface part
89 $q = 'INSERT INTO ' . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_Util::backquote($cfgRelation['relation'])
90 . '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
91 . ' values('
92 . '\'' . PMA_Util::sqlAddSlashes($db) . '\', '
93 . '\'' . PMA_Util::sqlAddSlashes($T2) . '\', '
94 . '\'' . PMA_Util::sqlAddSlashes($F2) . '\', '
95 . '\'' . PMA_Util::sqlAddSlashes($db) . '\', '
96 . '\'' . PMA_Util::sqlAddSlashes($T1) . '\','
97 . '\'' . PMA_Util::sqlAddSlashes($F1) . '\')';
99 if (PMA_queryAsControlUser($q, false, PMA_DatabaseInterface::QUERY_STORE)) {
100 PMD_return_new(1, __('Internal relation added'));
101 } else {
102 PMD_return_new(0, __('Error: Relation not added.'));
107 function PMD_return_new($b,$ret)
109 global $db,$T1,$F1,$T2,$F2;
110 header("Content-Type: text/xml; charset=utf-8");
111 header("Cache-Control: no-cache");
112 die('<root act="relation_new" return="'.$ret.'" b="'.$b.
113 '" DB1="'.urlencode($db).
114 '" T1="'.urlencode($T1).
115 '" F1="'.urlencode($F1).
116 '" DB2="'.urlencode($db).
117 '" T2="'.urlencode($T2).
118 '" F2="'.urlencode($F2).
119 '"></root>');