Permalink
Switch branches/tags
RELEASE_3_5_0 RELEASE_3_5_0RC2 RELEASE_3_5_0RC1 RELEASE_3_5_0BETA1 RELEASE_3_5_0ALPHA1 RELEASE_3_4_11 RELEASE_3_4_11RC1 RELEASE_3_4_10_2 RELEASE_3_4_10_1 RELEASE_3_4_10 RELEASE_3_4_10RC1 RELEASE_3_4_9 RELEASE_3_4_9RC1 RELEASE_3_4_8 RELEASE_3_4_8RC1 RELEASE_3_4_7_1 RELEASE_3_4_7 RELEASE_3_4_7RC1 RELEASE_3_4_6 RELEASE_3_4_6RC1 RELEASE_3_4_5 RELEASE_3_4_5RC1 RELEASE_3_4_4 RELEASE_3_4_4RC1 RELEASE_3_4_3_2 RELEASE_3_4_3_1 RELEASE_3_4_3 RELEASE_3_4_3RC1 RELEASE_3_4_2 RELEASE_3_4_2RC1 RELEASE_3_4_1 RELEASE_3_4_1RC1 RELEASE_3_4_0 RELEASE_3_4_0RC2 RELEASE_3_4_0RC1 RELEASE_3_4_0BETA4 RELEASE_3_4_0BETA3 RELEASE_3_4_0BETA2 RELEASE_3_4_0BETA1 RELEASE_3_4_0ALPHA2 RELEASE_3_4_0ALPHA1 RELEASE_3_3_10_5 RELEASE_3_3_10_4 RELEASE_3_3_10_3 RELEASE_3_3_10_2 RELEASE_3_3_10_1 RELEASE_3_3_10 RELEASE_3_3_10RC1 RELEASE_3_3_9_2 RELEASE_3_3_9_1 RELEASE_3_3_9 RELEASE_3_3_9RC1 RELEASE_3_3_8_1 RELEASE_3_3_8 RELEASE_3_3_8RC1 RELEASE_3_3_7 RELEASE_3_3_7RC1 RELEASE_3_3_6 RELEASE_3_3_6RC1 RELEASE_3_3_5_1 RELEASE_3_3_5 RELEASE_3_3_5RC1 RELEASE_3_3_4 RELEASE_3_3_4RC1 RELEASE_3_3_3 RELEASE_3_3_3RC1 RELEASE_3_3_2 RELEASE_3_3_2RC1 RELEASE_3_3_1 RELEASE_3_3_1RC1 RELEASE_3_3_0 RELEASE_3_3_0RC3 RELEASE_3_3_0RC2 RELEASE_3_3_0RC1 RELEASE_3_3_0BETA1 RELEASE_3_3_0ALPHA1 RELEASE_3_2_5 RELEASE_3_2_5RC2 RELEASE_3_2_5RC1 RELEASE_3_2_4 RELEASE_3_2_4RC1 RELEASE_3_2_3 RELEASE_3_2_3RC1 RELEASE_3_2_2_1 RELEASE_3_2_2 RELEASE_3_2_2RC1 RELEASE_3_2_0_1 RELEASE_3_2_0 RELEASE_3_2_0RC1 RELEASE_3_2_0BETA1 RELEASE_3_1_5 RELEASE_3_1_5RC1 RELEASE_3_1_4 RELEASE_3_1_4RC2 RELEASE_3_1_4RC1 RELEASE_3_1_3_2 RELEASE_3_1_3_1 RELEASE_3_1_3 RELEASE_3_1_3RC1 RELEASE_3_1_2
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
104 lines (93 sloc) 3.85 KB
<?php
/* vim: set expandtab sw=4 ts=4 sts=4: */
/**
*
* @package PhpMyAdmin-Designer
*/
/**
*
*/
require_once 'libraries/pmd_common.php';
$die_save_pos = 0;
require_once 'pmd_save_pos.php';
extract($_POST, EXTR_SKIP);
$tables = PMA_DBI_get_tables_full($db, $T1);
$type_T1 = strtoupper($tables[$T1]['ENGINE']);
$tables = PMA_DBI_get_tables_full($db, $T2);
$type_T2 = strtoupper($tables[$T2]['ENGINE']);
// native foreign key
if (PMA_foreignkey_supported($type_T1) && PMA_foreignkey_supported($type_T2) && $type_T1 == $type_T2) {
// relation exists?
$existrel_foreign = PMA_getForeigners($db, $T2, '', 'foreign');
if (isset($existrel_foreign[$F2])
&& isset($existrel_foreign[$F2]['constraint'])) {
PMD_return_new(0, __('Error: relation already exists.'));
}
// note: in InnoDB, the index does not requires to be on a PRIMARY
// or UNIQUE key
// improve: check all other requirements for InnoDB relations
$result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($T1) . ';');
$index_array1 = array(); // will be use to emphasis prim. keys in the table view
while ($row = PMA_DBI_fetch_assoc($result))
$index_array1[$row['Column_name']] = 1;
PMA_DBI_free_result($result);
$result = PMA_DBI_query('SHOW INDEX FROM ' . PMA_backquote($T2) . ';');
$index_array2 = array(); // will be used to emphasis prim. keys in the table view
while ($row = PMA_DBI_fetch_assoc($result))
$index_array2[$row['Column_name']] = 1;
PMA_DBI_free_result($result);
if (! empty($index_array1[$F1]) && ! empty($index_array2[$F2])) {
$upd_query = 'ALTER TABLE ' . PMA_backquote($T2)
. ' ADD FOREIGN KEY ('
. PMA_backquote($F2) . ')'
. ' REFERENCES '
. PMA_backquote($db) . '.'
. PMA_backquote($T1) . '('
. PMA_backquote($F1) . ')';
if ($on_delete != 'nix') {
$upd_query .= ' ON DELETE ' . $on_delete;
}
if ($on_update != 'nix') {
$upd_query .= ' ON UPDATE ' . $on_update;
}
PMA_DBI_try_query($upd_query) or PMD_return_new(0, __('Error: Relation not added.'));
PMD_return_new(1, __('FOREIGN KEY relation added'));
}
// internal (pmadb) relation
} else {
if ($GLOBALS['cfgRelation']['relwork'] == false) {
PMD_return_new(0, _('General relation features') . ':' . _('Disabled'));
} else {
// no need to recheck if the keys are primary or unique at this point,
// this was checked on the interface part
$q = 'INSERT INTO ' . PMA_backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_backquote($cfgRelation['relation'])
. '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
. ' values('
. '\'' . PMA_sqlAddSlashes($db) . '\', '
. '\'' . PMA_sqlAddSlashes($T2) . '\', '
. '\'' . PMA_sqlAddSlashes($F2) . '\', '
. '\'' . PMA_sqlAddSlashes($db) . '\', '
. '\'' . PMA_sqlAddSlashes($T1) . '\','
. '\'' . PMA_sqlAddSlashes($F1) . '\')';
if (PMA_query_as_controluser($q, false, PMA_DBI_QUERY_STORE)) {
PMD_return_new(1, __('Internal relation added'));
} else {
PMD_return_new(0, __('Error: Relation not added.'));
}
}
}
function PMD_return_new($b,$ret)
{
global $db,$T1,$F1,$T2,$F2;
header("Content-Type: text/xml; charset=utf-8");
header("Cache-Control: no-cache");
die('<root act="relation_new" return="'.$ret.'" b="'.$b.
'" DB1="'.urlencode($db).
'" T1="'.urlencode($T1).
'" F1="'.urlencode($F1).
'" DB2="'.urlencode($db).
'" T2="'.urlencode($T2).
'" F2="'.urlencode($F2).
'"></root>');
}
?>