SystemDatabase.php 3.7 KB

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