PageRenderTime 21ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/classes/Controllers/Database/QueryByExampleController.php

http://github.com/phpmyadmin/phpmyadmin
PHP | 169 lines | 134 code | 24 blank | 11 comment | 11 complexity | d671501fcb1e1893e21c898c38da1cb2 MD5 | raw file
Possible License(s): GPL-2.0, MIT, LGPL-3.0
  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\ResponseRenderer;
  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. public function __construct(
  24. ResponseRenderer $response,
  25. Template $template,
  26. string $db,
  27. Relation $relation,
  28. DatabaseInterface $dbi
  29. ) {
  30. parent::__construct($response, $template, $db);
  31. $this->relation = $relation;
  32. $this->dbi = $dbi;
  33. }
  34. public function __invoke(): void
  35. {
  36. global $db, $savedSearchList, $savedSearch, $currentSearchId;
  37. global $sql_query, $goto, $sub_part, $tables, $num_tables, $total_num_tables;
  38. global $tooltip_truename, $tooltip_aliasname, $pos, $urlParams, $cfg, $errorUrl;
  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. $savedSearch->setId(null)
  58. ->setCriterias($_POST)
  59. ->save();
  60. } elseif ($_POST['action'] === 'update') {
  61. $savedSearch->setCriterias($_POST)
  62. ->save();
  63. } elseif ($_POST['action'] === 'delete') {
  64. $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. $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. null, // disp_query
  115. null, // disp_message
  116. $sql_query, // sql_query
  117. null // complete_query
  118. ));
  119. }
  120. }
  121. $sub_part = '_qbe';
  122. Util::checkParameters(['db']);
  123. $errorUrl = Util::getScriptNameForOption($cfg['DefaultTabDatabase'], 'database');
  124. $errorUrl .= Url::getCommon(['db' => $db], '&');
  125. if (! $this->hasDatabase()) {
  126. return;
  127. }
  128. $urlParams['goto'] = Url::getFromRoute('/database/qbe');
  129. [
  130. $tables,
  131. $num_tables,
  132. $total_num_tables,
  133. $sub_part,,,
  134. $tooltip_truename,
  135. $tooltip_aliasname,
  136. $pos,
  137. ] = Util::getDbInfo($db, $sub_part ?? '');
  138. $databaseQbe = new Qbe($this->relation, $this->template, $this->dbi, $db, $savedSearchList, $savedSearch);
  139. $this->render('database/qbe/index', [
  140. 'url_params' => $urlParams,
  141. 'has_message_to_display' => $hasMessageToDisplay,
  142. 'selection_form_html' => $databaseQbe->getSelectionForm(),
  143. ]);
  144. }
  145. }