SystemDatabase.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * hold PhpMyAdmin\SystemDatabase class
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. namespace PhpMyAdmin;
  9. use PhpMyAdmin\DatabaseInterface;
  10. use PhpMyAdmin\Relation;
  11. use PhpMyAdmin\Util;
  12. /**
  13. * Class SystemDatabase
  14. *
  15. * @package PhpMyAdmin
  16. */
  17. class SystemDatabase
  18. {
  19. /**
  20. * @var DatabaseInterface
  21. */
  22. protected $dbi;
  23. /**
  24. * @var Relation $relation
  25. */
  26. private $relation;
  27. /**
  28. * Get instance of SystemDatabase
  29. *
  30. * @param DatabaseInterface $dbi Database interface for the system database
  31. *
  32. */
  33. function __construct(DatabaseInterface $dbi)
  34. {
  35. $this->dbi = $dbi;
  36. $this->relation = new Relation();
  37. }
  38. /**
  39. * Get existing data on transformations applied for
  40. * columns in a particular table
  41. *
  42. * @param string $db Database name looking for
  43. *
  44. * @return \mysqli_result Result of executed SQL query
  45. */
  46. public function getExistingTransformationData($db)
  47. {
  48. $cfgRelation = $this->relation->getRelationsParam();
  49. // Get the existing transformation details of the same database
  50. // from pma__column_info table
  51. $pma_transformation_sql = sprintf(
  52. "SELECT * FROM %s.%s WHERE `db_name` = '%s'",
  53. Util::backquote($cfgRelation['db']),
  54. Util::backquote($cfgRelation['column_info']),
  55. $GLOBALS['dbi']->escapeString($db)
  56. );
  57. return $this->dbi->tryQuery($pma_transformation_sql);
  58. }
  59. /**
  60. * Get SQL query for store new transformation details of a VIEW
  61. *
  62. * @param object $pma_transformation_data Result set of SQL execution
  63. * @param array $column_map Details of VIEW columns
  64. * @param string $view_name Name of the VIEW
  65. * @param string $db Database name of the VIEW
  66. *
  67. * @return string $new_transformations_sql SQL query for new transformations
  68. */
  69. function getNewTransformationDataSql(
  70. $pma_transformation_data, array $column_map, $view_name, $db
  71. ) {
  72. $cfgRelation = $this->relation->getRelationsParam();
  73. // Need to store new transformation details for VIEW
  74. $new_transformations_sql = sprintf(
  75. "INSERT INTO %s.%s ("
  76. . "`db_name`, `table_name`, `column_name`, "
  77. . "`comment`, `mimetype`, `transformation`, "
  78. . "`transformation_options`) VALUES",
  79. Util::backquote($cfgRelation['db']),
  80. Util::backquote($cfgRelation['column_info'])
  81. );
  82. $column_count = 0;
  83. $add_comma = false;
  84. while ($data_row = $this->dbi->fetchAssoc($pma_transformation_data)) {
  85. foreach ($column_map as $column) {
  86. if ($data_row['table_name'] != $column['table_name']
  87. || $data_row['column_name'] != $column['refering_column']
  88. ) {
  89. continue;
  90. }
  91. $new_transformations_sql .= sprintf(
  92. "%s ('%s', '%s', '%s', '%s', '%s', '%s', '%s')",
  93. $add_comma ? ', ' : '',
  94. $db,
  95. $view_name,
  96. isset($column['real_column'])
  97. ? $column['real_column']
  98. : $column['refering_column'],
  99. $data_row['comment'],
  100. $data_row['mimetype'],
  101. $data_row['transformation'],
  102. $GLOBALS['dbi']->escapeString(
  103. $data_row['transformation_options']
  104. )
  105. );
  106. $add_comma = true;
  107. $column_count++;
  108. break;
  109. }
  110. if ($column_count == count($column_map)) {
  111. break;
  112. }
  113. }
  114. return ($column_count > 0) ? $new_transformations_sql : '';
  115. }
  116. }