pmd_relation_new.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. *
  5. * @package PhpMyAdmin-Designer
  6. */
  7. /**
  8. *
  9. */
  10. require_once './libraries/common.inc.php';
  11. PMA_Response::getInstance()->disable();
  12. require_once 'libraries/pmd_common.php';
  13. $die_save_pos = 0;
  14. require_once 'pmd_save_pos.php';
  15. extract($_POST, EXTR_SKIP);
  16. $tables = PMA_DBI_get_tables_full($db, $T1);
  17. $type_T1 = strtoupper($tables[$T1]['ENGINE']);
  18. $tables = PMA_DBI_get_tables_full($db, $T2);
  19. $type_T2 = strtoupper($tables[$T2]['ENGINE']);
  20. // native foreign key
  21. if (PMA_Util::isForeignKeySupported($type_T1)
  22. && PMA_Util::isForeignKeySupported($type_T2)
  23. && $type_T1 == $type_T2
  24. ) {
  25. // relation exists?
  26. $existrel_foreign = PMA_getForeigners($db, $T2, '', 'foreign');
  27. if (isset($existrel_foreign[$F2])
  28. && isset($existrel_foreign[$F2]['constraint'])
  29. ) {
  30. PMD_return_new(0, __('Error: relation already exists.'));
  31. }
  32. // note: in InnoDB, the index does not requires to be on a PRIMARY
  33. // or UNIQUE key
  34. // improve: check all other requirements for InnoDB relations
  35. $result = PMA_DBI_query(
  36. 'SHOW INDEX FROM ' . PMA_Util::backquote($db)
  37. . '.' . PMA_Util::backquote($T1) . ';'
  38. );
  39. $index_array1 = array(); // will be use to emphasis prim. keys in the table view
  40. while ($row = PMA_DBI_fetch_assoc($result)) {
  41. $index_array1[$row['Column_name']] = 1;
  42. }
  43. PMA_DBI_free_result($result);
  44. $result = PMA_DBI_query(
  45. 'SHOW INDEX FROM ' . PMA_Util::backquote($db)
  46. . '.' . PMA_Util::backquote($T2) . ';'
  47. );
  48. $index_array2 = array(); // will be used to emphasis prim. keys in the table view
  49. while ($row = PMA_DBI_fetch_assoc($result)) {
  50. $index_array2[$row['Column_name']] = 1;
  51. }
  52. PMA_DBI_free_result($result);
  53. if (! empty($index_array1[$F1]) && ! empty($index_array2[$F2])) {
  54. $upd_query = 'ALTER TABLE ' . PMA_Util::backquote($db)
  55. . '.' . PMA_Util::backquote($T2)
  56. . ' ADD FOREIGN KEY ('
  57. . PMA_Util::backquote($F2) . ')'
  58. . ' REFERENCES '
  59. . PMA_Util::backquote($db) . '.'
  60. . PMA_Util::backquote($T1) . '('
  61. . PMA_Util::backquote($F1) . ')';
  62. if ($on_delete != 'nix') {
  63. $upd_query .= ' ON DELETE ' . $on_delete;
  64. }
  65. if ($on_update != 'nix') {
  66. $upd_query .= ' ON UPDATE ' . $on_update;
  67. }
  68. $upd_query .= ';';
  69. PMA_DBI_try_query($upd_query) or PMD_return_new(0, __('Error: Relation not added.'));
  70. PMD_return_new(1, __('FOREIGN KEY relation added'));
  71. }
  72. } else { // internal (pmadb) relation
  73. if ($GLOBALS['cfgRelation']['relwork'] == false) {
  74. PMD_return_new(0, __('General relation features') . ':' . __('Disabled'));
  75. } else {
  76. // no need to recheck if the keys are primary or unique at this point,
  77. // this was checked on the interface part
  78. $q = 'INSERT INTO ' . PMA_Util::backquote($GLOBALS['cfgRelation']['db']) . '.' . PMA_Util::backquote($cfgRelation['relation'])
  79. . '(master_db, master_table, master_field, foreign_db, foreign_table, foreign_field)'
  80. . ' values('
  81. . '\'' . PMA_Util::sqlAddSlashes($db) . '\', '
  82. . '\'' . PMA_Util::sqlAddSlashes($T2) . '\', '
  83. . '\'' . PMA_Util::sqlAddSlashes($F2) . '\', '
  84. . '\'' . PMA_Util::sqlAddSlashes($db) . '\', '
  85. . '\'' . PMA_Util::sqlAddSlashes($T1) . '\','
  86. . '\'' . PMA_Util::sqlAddSlashes($F1) . '\')';
  87. if (PMA_queryAsControlUser($q, false, PMA_DBI_QUERY_STORE)) {
  88. PMD_return_new(1, __('Internal relation added'));
  89. } else {
  90. PMD_return_new(0, __('Error: Relation not added.'));
  91. }
  92. }
  93. }
  94. function PMD_return_new($b,$ret)
  95. {
  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>');
  107. }
  108. ?>