2
/* $Id: pmd_relation_new.php 9828 2007-01-05 17:30:36Z lem9 $ */
3
// vim: expandtab sw=4 ts=4 sts=4:
5
include_once 'pmd_common.php';
7
include_once 'pmd_save_pos.php';
8
require_once './libraries/relation.lib.php';
11
$tables = PMA_DBI_get_tables_full($db, $T1);
12
$type_T1 = strtoupper($tables[$T1]['ENGINE']);
13
$tables = PMA_DBI_get_tables_full($db, $T2);
16
$type_T2 = strtoupper($tables[$T2]['ENGINE']);
19
if ($type_T1 == 'INNODB' and $type_T2 == 'INNODB') {
21
$existrel_innodb = PMA_getForeigners($db, $T2, '', 'innodb');
22
if (isset($existrel_innodb[$F2])
23
&& isset($existrel_innodb[$F2]['constraint'])) {
24
PMD_return(0,'strErrorRelationExists');
26
// note: in InnoDB, the index does not requires to be on a PRIMARY
28
// improve: check all other requirements for InnoDB relations
29
$result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($T1) . ';');
30
$index_array1 = array(); // will be use to emphasis prim. keys in the table view
31
while ($row = PMA_DBI_fetch_assoc($result))
32
$index_array1[$row['Column_name']] = 1;
33
PMA_DBI_free_result($result);
35
$result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($T2) . ';');
36
$index_array2 = array(); // will be used to emphasis prim. keys in the table view
37
while ($row = PMA_DBI_fetch_assoc($result))
38
$index_array2[$row['Column_name']] = 1;
39
PMA_DBI_free_result($result);
41
if (! empty($index_array1[$F1]) && ! empty($index_array2[$F2])) {
42
$upd_query = 'ALTER TABLE ' . PMA_backquote($T2)
43
. ' ADD FOREIGN KEY ('
44
. PMA_backquote($F2) . ')'
46
. PMA_backquote($db) . '.'
47
. PMA_backquote($T1) . '('
48
. PMA_backquote($F1) . ')';
50
if ($on_delete != 'nix') {
51
$upd_query .= ' ON DELETE ' . $on_delete;
53
if ($on_update != 'nix') {
54
$upd_query .= ' ON UPDATE ' . $on_update;
56
PMA_DBI_try_query($upd_query) or PMD_return(0,'strErrorRelationAdded');
57
PMD_return(1,'strInnoDBRelationAdded');
60
// n o n - I n n o D B
62
if ($GLOBALS['cfgRelation']['relwork'] == false) {
63
PMD_return(0, 'strGeneralRelationFeat:strDisabled');
65
// no need to recheck if the keys are primary or unique at this point,
66
// this was checked on the interface part
68
$q = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
69
. '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
71
. '\'' . PMA_sqlAddslashes($db) . '\', '
72
. '\'' . PMA_sqlAddslashes($T2) . '\', '
73
. '\'' . PMA_sqlAddslashes($F2) . '\', '
74
. '\'' . PMA_sqlAddslashes($db) . '\', '
75
. '\'' . PMA_sqlAddslashes($T1) . '\','
76
. '\'' . PMA_sqlAddslashes($F1) . '\')';
78
if (PMA_query_as_cu( $q , false, PMA_DBI_QUERY_STORE)) {
79
PMD_return(1, 'strInternalRelationAdded');
81
PMD_return(0, 'strErrorRelationAdded');
86
function PMD_return($b,$ret)
88
global $db,$T1,$F1,$T2,$F2;
89
header("Content-Type: text/xml; charset=utf-8");//utf-8 .$_GLOBALS['charset']
90
header("Cache-Control: no-cache");
91
die('<root act="relation_new" return="'.$ret.'" b="'.$b.
92
'" DB1="'.urlencode($db).
93
'" T1="'.urlencode($T1).
94
'" F1="'.urlencode($F1).
95
'" DB2="'.urlencode($db).
96
'" T2="'.urlencode($T2).
97
'" F2="'.urlencode($F2).