2.11.11.2 release
[phpmyadmin/tyronm.git] / pmd_relation_new.php
blob27c171813c9e7ddb096e0c009ea98ffd146fa28b
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @version $Id$
6 * @package phpMyAdmin-Designer
7 */
9 /**
12 include_once 'pmd_common.php';
13 $die_save_pos = 0;
14 include_once 'pmd_save_pos.php';
15 require_once './libraries/relation.lib.php';
16 extract($_POST, EXTR_SKIP);
18 $tables = PMA_DBI_get_tables_full($db, $T1);
19 $type_T1 = strtoupper($tables[$T1]['ENGINE']);
20 $tables = PMA_DBI_get_tables_full($db, $T2);
21 //print_r($tables);
22 //die();
23 $type_T2 = strtoupper($tables[$T2]['ENGINE']);
25 // I n n o D B
26 if ($type_T1 == 'INNODB' and $type_T2 == 'INNODB') {
27 // relation exists?
28 $existrel_innodb = PMA_getForeigners($db, $T2, '', 'innodb');
29 if (isset($existrel_innodb[$F2])
30 && isset($existrel_innodb[$F2]['constraint'])) {
31 PMD_return(0,'strErrorRelationExists');
33 // note: in InnoDB, the index does not requires to be on a PRIMARY
34 // or UNIQUE key
35 // improve: check all other requirements for InnoDB relations
36 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($T1) . ';');
37 $index_array1 = array(); // will be use to emphasis prim. keys in the table view
38 while ($row = PMA_DBI_fetch_assoc($result))
39 $index_array1[$row['Column_name']] = 1;
40 PMA_DBI_free_result($result);
42 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($T2) . ';');
43 $index_array2 = array(); // will be used to emphasis prim. keys in the table view
44 while ($row = PMA_DBI_fetch_assoc($result))
45 $index_array2[$row['Column_name']] = 1;
46 PMA_DBI_free_result($result);
48 if (! empty($index_array1[$F1]) && ! empty($index_array2[$F2])) {
49 $upd_query = 'ALTER TABLE ' . PMA_backquote($T2)
50 . ' ADD FOREIGN KEY ('
51 . PMA_backquote($F2) . ')'
52 . ' REFERENCES '
53 . PMA_backquote($db) . '.'
54 . PMA_backquote($T1) . '('
55 . PMA_backquote($F1) . ')';
57 if ($on_delete != 'nix') {
58 $upd_query .= ' ON DELETE ' . $on_delete;
60 if ($on_update != 'nix') {
61 $upd_query .= ' ON UPDATE ' . $on_update;
63 PMA_DBI_try_query($upd_query) or PMD_return(0,'strErrorRelationAdded');
64 PMD_return(1,'strInnoDBRelationAdded');
67 // n o n - I n n o D B
68 } else {
69 if ($GLOBALS['cfgRelation']['relwork'] == false) {
70 PMD_return(0, 'strGeneralRelationFeat:strDisabled');
71 } else {
72 // no need to recheck if the keys are primary or unique at this point,
73 // this was checked on the interface part
75 $q = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
76 . '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
77 . ' values('
78 . '\'' . PMA_sqlAddslashes($db) . '\', '
79 . '\'' . PMA_sqlAddslashes($T2) . '\', '
80 . '\'' . PMA_sqlAddslashes($F2) . '\', '
81 . '\'' . PMA_sqlAddslashes($db) . '\', '
82 . '\'' . PMA_sqlAddslashes($T1) . '\','
83 . '\'' . PMA_sqlAddslashes($F1) . '\')';
85 if (PMA_query_as_cu($q , false, PMA_DBI_QUERY_STORE)) {
86 PMD_return(1, 'strInternalRelationAdded');
87 } else {
88 PMD_return(0, 'strErrorRelationAdded');
93 function PMD_return($b,$ret)
95 global $db,$T1,$F1,$T2,$F2;
96 header("Content-Type: text/xml; charset=utf-8");//utf-8 .$_GLOBALS['charset']
97 header("Cache-Control: no-cache");
98 die('<root act="relation_new" return="'.$ret.'" b="'.$b.
99 '" DB1="'.urlencode($db).
100 '" T1="'.urlencode($T1).
101 '" F1="'.urlencode($F1).
102 '" DB2="'.urlencode($db).
103 '" T2="'.urlencode($T2).
104 '" F2="'.urlencode($F2).
105 '"></root>');