pmd_relation_new.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * PMD handler for creating new relation
  5. *
  6. * @package PhpMyAdmin-Designer
  7. */
  8. /**
  9. *
  10. */
  11. require_once './libraries/common.inc.php';
  12. PMA_Response::getInstance()->disable();
  13. require_once 'libraries/pmd_common.php';
  14. $die_save_pos = 0;
  15. require_once 'pmd_save_pos.php';
  16. extract($_POST, EXTR_SKIP);
  17. $tables = $GLOBALS['dbi']->getTablesFull($db, $T1);
  18. $type_T1 = strtoupper($tables[$T1]['ENGINE']);
  19. $tables = $GLOBALS['dbi']->getTablesFull($db, $T2);
  20. $type_T2 = strtoupper($tables[$T2]['ENGINE']);
  21. // native foreign key
  22. if (PMA_Util::isForeignKeySupported($type_T1)
  23. && PMA_Util::isForeignKeySupported($type_T2)
  24. && $type_T1 == $type_T2
  25. ) {
  26. // relation exists?
  27. $existrel_foreign = PMA_getForeigners($db, $T2, '', 'foreign');
  28. if (isset($existrel_foreign[$F2])
  29. && isset($existrel_foreign[$F2]['constraint'])
  30. ) {
  31. PMD_Return_new(0, __('Error: relation already exists.'));
  32. }
  33. // note: in InnoDB, the index does not requires to be on a PRIMARY
  34. // or UNIQUE key
  35. // improve: check all other requirements for InnoDB relations
  36. $result = $GLOBALS['dbi']->query(
  37. 'SHOW INDEX FROM ' . PMA_Util::backquote($db)
  38. . '.' . PMA_Util::backquote($T1) . ';'
  39. );
  40. $index_array1 = array(); // will be use to emphasis prim. keys in the table view
  41. while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
  42. $index_array1[$row['Column_name']] = 1;
  43. }
  44. $GLOBALS['dbi']->freeResult($result);
  45. $result = $GLOBALS['dbi']->query(
  46. 'SHOW INDEX FROM ' . PMA_Util::backquote($db)
  47. . '.' . PMA_Util::backquote($T2) . ';'
  48. );
  49. // will be used to emphasis prim. keys in the table view
  50. $index_array2 = array();
  51. while ($row = $GLOBALS['dbi']->fetchAssoc($result)) {
  52. $index_array2[$row['Column_name']] = 1;
  53. }
  54. $GLOBALS['dbi']->freeResult($result);
  55. if (! empty($index_array1[$F1]) && ! empty($index_array2[$F2])) {
  56. $upd_query = 'ALTER TABLE ' . PMA_Util::backquote($db)
  57. . '.' . PMA_Util::backquote($T2)
  58. . ' ADD FOREIGN KEY ('
  59. . PMA_Util::backquote($F2) . ')'
  60. . ' REFERENCES '
  61. . PMA_Util::backquote($db) . '.'
  62. . PMA_Util::backquote($T1) . '('
  63. . PMA_Util::backquote($F1) . ')';
  64. if ($on_delete != 'nix') {
  65. $upd_query .= ' ON DELETE ' . $on_delete;
  66. }
  67. if ($on_update != 'nix') {
  68. $upd_query .= ' ON UPDATE ' . $on_update;
  69. }
  70. $upd_query .= ';';
  71. $GLOBALS['dbi']->tryQuery($upd_query)
  72. || PMD_Return_new(0, __('Error: Relation could not be added!'));
  73. PMD_Return_new(1, __('FOREIGN KEY relation has been added.'));
  74. }
  75. } else { // internal (pmadb) relation
  76. if ($GLOBALS['cfgRelation']['relwork'] == false) {
  77. PMD_Return_new(0, __('Error: Relational features are disabled!'));
  78. } else {
  79. // no need to recheck if the keys are primary or unique at this point,
  80. // this was checked on the interface part
  81. $q = 'INSERT INTO '
  82. . PMA_Util::backquote($GLOBALS['cfgRelation']['db'])
  83. . '.' . PMA_Util::backquote($cfgRelation['relation'])
  84. . '(master_db, master_table, master_field,'
  85. . 'foreign_db, foreign_table, foreign_field)'
  86. . ' values('
  87. . '\'' . PMA_Util::sqlAddSlashes($db) . '\', '
  88. . '\'' . PMA_Util::sqlAddSlashes($T2) . '\', '
  89. . '\'' . PMA_Util::sqlAddSlashes($F2) . '\', '
  90. . '\'' . PMA_Util::sqlAddSlashes($db) . '\', '
  91. . '\'' . PMA_Util::sqlAddSlashes($T1) . '\','
  92. . '\'' . PMA_Util::sqlAddSlashes($F1) . '\')';
  93. if (PMA_queryAsControlUser($q, false, PMA_DatabaseInterface::QUERY_STORE)) {
  94. PMD_Return_new(1, __('Internal relation has been added.'));
  95. } else {
  96. PMD_Return_new(0, __('Error: Relation could not be added!'));
  97. }
  98. }
  99. }
  100. /**
  101. * Send xml
  102. *
  103. * @param string $b Value of attribute "b"
  104. * @param string $ret Value of attribute "return"
  105. *
  106. * @return void
  107. */
  108. function PMD_Return_new($b,$ret)
  109. {
  110. global $db,$T1,$F1,$T2,$F2;
  111. header("Content-Type: text/xml; charset=utf-8");
  112. header("Cache-Control: no-cache");
  113. die('<root act="relation_new" return="' . $ret . '" b="' . $b .
  114. '" DB1="' . urlencode($db) .
  115. '" T1="' . urlencode($T1) .
  116. '" F1="' . urlencode($F1) .
  117. '" DB2="' . urlencode($db) .
  118. '" T2="' . urlencode($T2) .
  119. '" F2="' . urlencode($F2) .
  120. '"></root>');
  121. }
  122. ?>