Fixed descriptions of geometry types - these are column types, not OpenGIS objects
[phpmyadmin.git] / pmd_relation_new.php
blob231e4a8a5e3c54ead774a2ebe6d3985251ec7277
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';
12 require_once 'libraries/pmd_common.php';
13 $die_save_pos = 0;
14 require_once 'pmd_save_pos.php';
15 extract($_POST, EXTR_SKIP);
17 $tables = PMA_DBI_get_tables_full($db, $T1);
18 $type_T1 = strtoupper($tables[$T1]['ENGINE']);
19 $tables = PMA_DBI_get_tables_full($db, $T2);
20 $type_T2 = strtoupper($tables[$T2]['ENGINE']);
22 // native foreign key
23 if (PMA_foreignkey_supported($type_T1) && PMA_foreignkey_supported($type_T2) && $type_T1 == $type_T2) {
24 // relation exists?
25 $existrel_foreign = PMA_getForeigners($db, $T2, '', 'foreign');
26 if (isset($existrel_foreign[$F2])
27 && isset($existrel_foreign[$F2]['constraint'])
28 ) {
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;
39 PMA_DBI_free_result($result);
41 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($T2) . ';');
42 $index_array2 = array(); // will be used to emphasis prim. keys in the table view
43 while ($row = PMA_DBI_fetch_assoc($result)) {
44 $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 $upd_query .= ';';
64 PMA_DBI_try_query($upd_query) or PMD_return_new(0, __('Error: Relation not added.'));
65 PMD_return_new(1, __('FOREIGN KEY relation added'));
68 // internal (pmadb) relation
69 } else {
70 if ($GLOBALS['cfgRelation']['relwork'] == false) {
71 PMD_return_new(0, _('General relation features') . ':' . _('Disabled'));
72 } else {
73 // no need to recheck if the keys are primary or unique at this point,
74 // this was checked on the interface part
76 $q = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
77 . '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
78 . ' values('
79 . '\'' . PMA_sqlAddSlashes($db) . '\', '
80 . '\'' . PMA_sqlAddSlashes($T2) . '\', '
81 . '\'' . PMA_sqlAddSlashes($F2) . '\', '
82 . '\'' . PMA_sqlAddSlashes($db) . '\', '
83 . '\'' . PMA_sqlAddSlashes($T1) . '\','
84 . '\'' . PMA_sqlAddSlashes($F1) . '\')';
86 if (PMA_query_as_controluser($q, false, PMA_DBI_QUERY_STORE)) {
87 PMD_return_new(1, __('Internal relation added'));
88 } else {
89 PMD_return_new(0, __('Error: Relation not added.'));
94 function PMD_return_new($b,$ret)
96 global $db,$T1,$F1,$T2,$F2;
97 header("Content-Type: text/xml; charset=utf-8");
98 header("Cache-Control: no-cache");
99 die('<root act="relation_new" return="'.$ret.'" b="'.$b.
100 '" DB1="'.urlencode($db).
101 '" T1="'.urlencode($T1).
102 '" F1="'.urlencode($F1).
103 '" DB2="'.urlencode($db).
104 '" T2="'.urlencode($T2).
105 '" F2="'.urlencode($F2).
106 '"></root>');