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