QueryByExampleController.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Controllers\Database;
  4. use PhpMyAdmin\Database\Qbe;
  5. use PhpMyAdmin\DatabaseInterface;
  6. use PhpMyAdmin\Operations;
  7. use PhpMyAdmin\Relation;
  8. use PhpMyAdmin\RelationCleanup;
  9. use PhpMyAdmin\Response;
  10. use PhpMyAdmin\SavedSearches;
  11. use PhpMyAdmin\Sql;
  12. use PhpMyAdmin\Template;
  13. use PhpMyAdmin\Transformations;
  14. use PhpMyAdmin\Url;
  15. use PhpMyAdmin\Util;
  16. use function stripos;
  17. class QueryByExampleController extends AbstractController
  18. {
  19. /** @var Relation */
  20. private $relation;
  21. /** @var DatabaseInterface */
  22. private $dbi;
  23. /**
  24. * @param Response $response
  25. * @param string $db Database name
  26. * @param DatabaseInterface $dbi
  27. */
  28. public function __construct($response, Template $template, $db, Relation $relation, $dbi)
  29. {
  30. parent::__construct($response, $template, $db);
  31. $this->relation = $relation;
  32. $this->dbi = $dbi;
  33. }
  34. public function index(): void
  35. {
  36. global $db, $savedSearchList, $savedSearch, $currentSearchId, $PMA_Theme;
  37. global $sql_query, $goto, $sub_part, $tables, $num_tables, $total_num_tables;
  38. global $tooltip_truename, $tooltip_aliasname, $pos, $url_params, $cfg, $err_url;
  39. // Gets the relation settings
  40. $cfgRelation = $this->relation->getRelationsParam();
  41. $savedSearchList = [];
  42. $savedSearch = null;
  43. $currentSearchId = null;
  44. $this->addScriptFiles(['database/qbe.js']);
  45. if ($cfgRelation['savedsearcheswork']) {
  46. //Get saved search list.
  47. $savedSearch = new SavedSearches($GLOBALS, $this->relation);
  48. $savedSearch->setUsername($GLOBALS['cfg']['Server']['user'])
  49. ->setDbname($db);
  50. if (! empty($_POST['searchId'])) {
  51. $savedSearch->setId($_POST['searchId']);
  52. }
  53. //Action field is sent.
  54. if (isset($_POST['action'])) {
  55. $savedSearch->setSearchName($_POST['searchName']);
  56. if ($_POST['action'] === 'create') {
  57. $saveResult = $savedSearch->setId(null)
  58. ->setCriterias($_POST)
  59. ->save();
  60. } elseif ($_POST['action'] === 'update') {
  61. $saveResult = $savedSearch->setCriterias($_POST)
  62. ->save();
  63. } elseif ($_POST['action'] === 'delete') {
  64. $deleteResult = $savedSearch->delete();
  65. //After deletion, reset search.
  66. $savedSearch = new SavedSearches($GLOBALS, $this->relation);
  67. $savedSearch->setUsername($GLOBALS['cfg']['Server']['user'])
  68. ->setDbname($db);
  69. $_POST = [];
  70. } elseif ($_POST['action'] === 'load') {
  71. if (empty($_POST['searchId'])) {
  72. //when not loading a search, reset the object.
  73. $savedSearch = new SavedSearches($GLOBALS, $this->relation);
  74. $savedSearch->setUsername($GLOBALS['cfg']['Server']['user'])
  75. ->setDbname($db);
  76. $_POST = [];
  77. } else {
  78. $loadResult = $savedSearch->load();
  79. }
  80. }
  81. //Else, it's an "update query"
  82. }
  83. $savedSearchList = $savedSearch->getList();
  84. $currentSearchId = $savedSearch->getId();
  85. }
  86. /**
  87. * A query has been submitted -> (maybe) execute it
  88. */
  89. $hasMessageToDisplay = false;
  90. if (isset($_POST['submit_sql']) && ! empty($sql_query)) {
  91. if (stripos($sql_query, 'SELECT') !== 0) {
  92. $hasMessageToDisplay = true;
  93. } else {
  94. $goto = Url::getFromRoute('/database/sql');
  95. $sql = new Sql(
  96. $this->dbi,
  97. $this->relation,
  98. new RelationCleanup($this->dbi, $this->relation),
  99. new Operations($this->dbi, $this->relation),
  100. new Transformations(),
  101. $this->template
  102. );
  103. $this->response->addHTML($sql->executeQueryAndSendQueryResponse(
  104. null, // analyzed_sql_results
  105. false, // is_gotofile
  106. $_POST['db'], // db
  107. null, // table
  108. false, // find_real_end
  109. null, // sql_query_for_bookmark
  110. null, // extra_data
  111. null, // message_to_show
  112. null, // sql_data
  113. $goto, // goto
  114. $PMA_Theme->getImgPath(),
  115. null, // disp_query
  116. null, // disp_message
  117. $sql_query, // sql_query
  118. null // complete_query
  119. ));
  120. }
  121. }
  122. $sub_part = '_qbe';
  123. Util::checkParameters(['db']);
  124. $err_url = Util::getScriptNameForOption($cfg['DefaultTabDatabase'], 'database');
  125. $err_url .= Url::getCommon(['db' => $db], '&');
  126. if (! $this->hasDatabase()) {
  127. return;
  128. }
  129. $url_params['goto'] = Url::getFromRoute('/database/qbe');
  130. [
  131. $tables,
  132. $num_tables,
  133. $total_num_tables,
  134. $sub_part,,,
  135. $tooltip_truename,
  136. $tooltip_aliasname,
  137. $pos,
  138. ] = Util::getDbInfo($db, $sub_part ?? '');
  139. $databaseQbe = new Qbe($this->relation, $this->template, $this->dbi, $db, $savedSearchList, $savedSearch);
  140. $this->render('database/qbe/index', [
  141. 'url_params' => $url_params,
  142. 'has_message_to_display' => $hasMessageToDisplay,
  143. 'selection_form_html' => $databaseQbe->getSelectionForm(),
  144. ]);
  145. }
  146. }