MultiTableQuery.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * Handles DB Multi-table query
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Database;
  7. use PhpMyAdmin\DatabaseInterface;
  8. use PhpMyAdmin\Operations;
  9. use PhpMyAdmin\ParseAnalyze;
  10. use PhpMyAdmin\Relation;
  11. use PhpMyAdmin\RelationCleanup;
  12. use PhpMyAdmin\Sql;
  13. use PhpMyAdmin\Template;
  14. use PhpMyAdmin\Transformations;
  15. use PhpMyAdmin\Url;
  16. use function array_keys;
  17. use function md5;
  18. /**
  19. * Class to handle database Multi-table querying
  20. */
  21. class MultiTableQuery
  22. {
  23. /**
  24. * DatabaseInterface instance
  25. *
  26. * @access private
  27. * @var DatabaseInterface
  28. */
  29. private $dbi;
  30. /**
  31. * Database name
  32. *
  33. * @access private
  34. * @var string
  35. */
  36. private $db;
  37. /**
  38. * Default number of columns
  39. *
  40. * @access private
  41. * @var int
  42. */
  43. private $defaultNoOfColumns;
  44. /**
  45. * Table names
  46. *
  47. * @access private
  48. * @var array
  49. */
  50. private $tables;
  51. /** @var Template */
  52. public $template;
  53. /**
  54. * @param DatabaseInterface $dbi DatabaseInterface instance
  55. * @param Template $template Template instance
  56. * @param string $dbName Database name
  57. * @param int $defaultNoOfColumns Default number of columns
  58. */
  59. public function __construct(
  60. DatabaseInterface $dbi,
  61. Template $template,
  62. $dbName,
  63. $defaultNoOfColumns = 3
  64. ) {
  65. $this->dbi = $dbi;
  66. $this->db = $dbName;
  67. $this->defaultNoOfColumns = $defaultNoOfColumns;
  68. $this->template = $template;
  69. $this->tables = $this->dbi->getTables($this->db);
  70. }
  71. /**
  72. * Get Multi-Table query page HTML
  73. *
  74. * @return string Multi-Table query page HTML
  75. */
  76. public function getFormHtml()
  77. {
  78. $tables = [];
  79. foreach ($this->tables as $table) {
  80. $tables[$table]['hash'] = md5($table);
  81. $tables[$table]['columns'] = array_keys(
  82. $this->dbi->getColumns($this->db, $table)
  83. );
  84. }
  85. return $this->template->render('database/multi_table_query/form', [
  86. 'db' => $this->db,
  87. 'tables' => $tables,
  88. 'default_no_of_columns' => $this->defaultNoOfColumns,
  89. ]);
  90. }
  91. /**
  92. * Displays multi-table query results
  93. *
  94. * @param string $sqlQuery The query to parse
  95. * @param string $db The current database
  96. * @param string $themeImagePath Uri of the PMA theme image
  97. */
  98. public static function displayResults($sqlQuery, $db, $themeImagePath): string
  99. {
  100. global $dbi;
  101. [, $db] = ParseAnalyze::sqlQuery($sqlQuery, $db);
  102. $goto = Url::getFromRoute('/database/multi-table-query');
  103. $relation = new Relation($dbi);
  104. $sql = new Sql(
  105. $dbi,
  106. $relation,
  107. new RelationCleanup($dbi, $relation),
  108. new Operations($dbi, $relation),
  109. new Transformations(),
  110. new Template()
  111. );
  112. return $sql->executeQueryAndSendQueryResponse(
  113. null, // analyzed_sql_results
  114. false, // is_gotofile
  115. $db, // db
  116. null, // table
  117. null, // find_real_end
  118. null, // sql_query_for_bookmark - see below
  119. null, // extra_data
  120. null, // message_to_show
  121. null, // sql_data
  122. $goto, // goto
  123. $themeImagePath,
  124. null, // disp_query
  125. null, // disp_message
  126. $sqlQuery, // sql_query
  127. null // complete_query
  128. );
  129. }
  130. }