3 // vim: expandtab sw=4 ts=4 sts=4:
5 include_once 'pmd_session.php';
7 include_once 'pmd_save_pos.php';
8 require_once './libraries/relation.lib.php';
10 PMA_getRelationsParam();
12 $tables = PMA_DBI_get_tables_full($db, $T1);
13 $type_T1 = strtoupper($tables[$T1]['ENGINE']);
14 $tables = PMA_DBI_get_tables_full($db, $T2);
15 $type_T2 = strtoupper($tables[$T2]['ENGINE']);
18 if ($type_T1 == 'INNODB' and $type_T2 == 'INNODB') {
20 $existrel_innodb = PMA_getForeigners($db, $T2, '', 'innodb');
21 if (isset($existrel_innodb[$F2])
22 && isset($existrel_innodb[$F2]['constraint'])) {
23 PMD_return(0,'ERROR : Relation already exists!');
25 // note: in InnoDB, the index does not requires to be on a PRIMARY
27 // improve: check all other requirements for InnoDB relations
28 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($T1) . ';');
29 $index_array1 = array(); // will be use to emphasis prim. keys in the table view
30 while ($row = PMA_DBI_fetch_assoc($result))
31 $index_array1[$row['Column_name']] = 1;
32 PMA_DBI_free_result($result);
34 $result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($T2) . ';');
35 $index_array2 = array(); // will be used to emphasis prim. keys in the table view
36 while ($row = PMA_DBI_fetch_assoc($result))
37 $index_array2[$row['Column_name']] = 1;
38 PMA_DBI_free_result($result);
40 if (! empty($index_array1[$F1]) && ! empty($index_array2[$F2])) {
41 $upd_query = 'ALTER TABLE ' . PMA_backquote($T2)
42 . ' ADD FOREIGN KEY ('
43 . PMA_backquote($F2) . ')'
45 . PMA_backquote($db) . '.'
46 . PMA_backquote($T1) . '('
47 . PMA_backquote($F1) . ')';
49 if ($on_delete != 'nix') {
50 $upd_query .= ' ON DELETE ' . $on_delete;
52 if ($on_update != 'nix') {
53 $upd_query .= ' ON UPDATE ' . $on_update;
55 PMA_DBI_try_query($upd_query) or PMD_return(0,'ERROR : Relation not added!!!');
56 PMD_return(1,$strInnoDBRelationAdded);
59 // n o n - I n n o D B
61 // no need to recheck if the keys are primary or unique at this point,
62 // this was checked on the interface part
64 $q = "INSERT INTO ".PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])."
65 (master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)
66 VALUES ('".$db."','".$T2."','$F2','".
67 $db."','".$T1."','$F1')";
68 PMA_query_as_cu( $q , true, PMA_DBI_QUERY_STORE
);// or PMD_return(0,'ERROR : Relation not added!');
70 PMD_return(1, $strInternalRelationAdded);
73 function PMD_return($b,$ret)
75 global $db,$T1,$F1,$T2,$F2;
76 die('<root act="relation_new" return="'.$ret.'" b="'.$b.'" DB1="'.$db.'" T1="'.$T1.'" F1="'.$F1.'" DB2="'.$db.'" T2="'.$T2.'" F2="'.$F2.'"></root>');