Add missing operator
[phpmyadmin-regexreplace.git] / pmd_relation_new.php
blob6ea4485d667b34b314ba7015bdba8c1fe6439766
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @package phpMyAdmin-Designer
6 */
8 /**
11 include_once 'pmd_common.php';
12 $die_save_pos = 0;
13 include_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 //print_r($tables);
20 //die();
21 $type_T2 = strtoupper($tables[$T2]['ENGINE']);
23 // native foreign key
24 if (PMA_foreignkey_supported($type_T1) && PMA_foreignkey_supported($type_T2) && $type_T1 == $type_T2) {
25 // relation exists?
26 $existrel_foreign = PMA_getForeigners($db, $T2, '', 'foreign');
27 if (isset($existrel_foreign[$F2])
28 && isset($existrel_foreign[$F2]['constraint'])) {
29 PMD_return_new(0,__('Error: relation already exists.'));
31 // note: in InnoDB, the index does not requires to be on a PRIMARY
32 // or UNIQUE key
33 // improve: check all other requirements for InnoDB relations
34 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($T1) . ';');
35 $index_array1 = array(); // will be use to emphasis prim. keys in the table view
36 while ($row = PMA_DBI_fetch_assoc($result))
37 $index_array1[$row['Column_name']] = 1;
38 PMA_DBI_free_result($result);
40 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($T2) . ';');
41 $index_array2 = array(); // will be used to emphasis prim. keys in the table view
42 while ($row = PMA_DBI_fetch_assoc($result))
43 $index_array2[$row['Column_name']] = 1;
44 PMA_DBI_free_result($result);
46 if (! empty($index_array1[$F1]) && ! empty($index_array2[$F2])) {
47 $upd_query = 'ALTER TABLE ' . PMA_backquote($T2)
48 . ' ADD FOREIGN KEY ('
49 . PMA_backquote($F2) . ')'
50 . ' REFERENCES '
51 . PMA_backquote($db) . '.'
52 . PMA_backquote($T1) . '('
53 . PMA_backquote($F1) . ')';
55 if ($on_delete != 'nix') {
56 $upd_query .= ' ON DELETE ' . $on_delete;
58 if ($on_update != 'nix') {
59 $upd_query .= ' ON UPDATE ' . $on_update;
61 PMA_DBI_try_query($upd_query) or PMD_return_new(0,__('Error: Relation not added.'));
62 PMD_return_new(1,__('FOREIGN KEY relation added'));
65 // internal (pmadb) relation
66 } else {
67 if ($GLOBALS['cfgRelation']['relwork'] == false) {
68 PMD_return_new(0, _('General relation features') . ':' . _('Disabled'));
69 } else {
70 // no need to recheck if the keys are primary or unique at this point,
71 // this was checked on the interface part
73 $q = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
74 . '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
75 . ' values('
76 . '\'' . PMA_sqlAddslashes($db) . '\', '
77 . '\'' . PMA_sqlAddslashes($T2) . '\', '
78 . '\'' . PMA_sqlAddslashes($F2) . '\', '
79 . '\'' . PMA_sqlAddslashes($db) . '\', '
80 . '\'' . PMA_sqlAddslashes($T1) . '\','
81 . '\'' . PMA_sqlAddslashes($F1) . '\')';
83 if (PMA_query_as_controluser($q , false, PMA_DBI_QUERY_STORE)) {
84 PMD_return_new(1, __('Internal relation added'));
85 } else {
86 PMD_return_new(0, __('Error: Relation not added.'));
91 function PMD_return_new($b,$ret)
93 global $db,$T1,$F1,$T2,$F2;
94 header("Content-Type: text/xml; charset=utf-8");//utf-8 .$_GLOBALS['charset']
95 header("Cache-Control: no-cache");
96 die('<root act="relation_new" return="'.$ret.'" b="'.$b.
97 '" DB1="'.urlencode($db).
98 '" T1="'.urlencode($T1).
99 '" F1="'.urlencode($F1).
100 '" DB2="'.urlencode($db).
101 '" T2="'.urlencode($T2).
102 '" F2="'.urlencode($F2).
103 '"></root>');