SystemDatabase.class.php 3.7 KB

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