SystemDatabase.php 3.9 KB

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