db_qbe.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * query by example the whole database
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. use PhpMyAdmin\Database\Qbe;
  9. use PhpMyAdmin\Message;
  10. use PhpMyAdmin\Relation;
  11. use PhpMyAdmin\Response;
  12. use PhpMyAdmin\SavedSearches;
  13. use PhpMyAdmin\Sql;
  14. use PhpMyAdmin\Template;
  15. use PhpMyAdmin\Url;
  16. use PhpMyAdmin\Util;
  17. /**
  18. * requirements
  19. */
  20. require_once 'libraries/common.inc.php';
  21. $response = Response::getInstance();
  22. $relation = new Relation();
  23. // Gets the relation settings
  24. $cfgRelation = $relation->getRelationsParam();
  25. $savedSearchList = array();
  26. $savedSearch = null;
  27. $currentSearchId = null;
  28. if ($cfgRelation['savedsearcheswork']) {
  29. $header = $response->getHeader();
  30. $scripts = $header->getScripts();
  31. $scripts->addFile('db_qbe.js');
  32. //Get saved search list.
  33. $savedSearch = new SavedSearches($GLOBALS);
  34. $savedSearch->setUsername($GLOBALS['cfg']['Server']['user'])
  35. ->setDbname($GLOBALS['db']);
  36. if (!empty($_POST['searchId'])) {
  37. $savedSearch->setId($_POST['searchId']);
  38. }
  39. //Action field is sent.
  40. if (isset($_POST['action'])) {
  41. $savedSearch->setSearchName($_POST['searchName']);
  42. if ('create' === $_POST['action']) {
  43. $saveResult = $savedSearch->setId(null)
  44. ->setCriterias($_POST)
  45. ->save();
  46. } elseif ('update' === $_POST['action']) {
  47. $saveResult = $savedSearch->setCriterias($_POST)
  48. ->save();
  49. } elseif ('delete' === $_POST['action']) {
  50. $deleteResult = $savedSearch->delete();
  51. //After deletion, reset search.
  52. $savedSearch = new SavedSearches($GLOBALS);
  53. $savedSearch->setUsername($GLOBALS['cfg']['Server']['user'])
  54. ->setDbname($GLOBALS['db']);
  55. $_POST = array();
  56. } elseif ('load' === $_POST['action']) {
  57. if (empty($_POST['searchId'])) {
  58. //when not loading a search, reset the object.
  59. $savedSearch = new SavedSearches($GLOBALS);
  60. $savedSearch->setUsername($GLOBALS['cfg']['Server']['user'])
  61. ->setDbname($GLOBALS['db']);
  62. $_POST = array();
  63. } else {
  64. $loadResult = $savedSearch->load();
  65. }
  66. }
  67. //Else, it's an "update query"
  68. }
  69. $savedSearchList = $savedSearch->getList();
  70. $currentSearchId = $savedSearch->getId();
  71. }
  72. /**
  73. * A query has been submitted -> (maybe) execute it
  74. */
  75. $message_to_display = false;
  76. if (isset($_POST['submit_sql']) && ! empty($sql_query)) {
  77. if (! preg_match('@^SELECT@i', $sql_query)) {
  78. $message_to_display = true;
  79. } else {
  80. $goto = 'db_sql.php';
  81. $sql = new Sql();
  82. $sql->executeQueryAndSendQueryResponse(
  83. null, // analyzed_sql_results
  84. false, // is_gotofile
  85. $_POST['db'], // db
  86. null, // table
  87. false, // find_real_end
  88. null, // sql_query_for_bookmark
  89. null, // extra_data
  90. null, // message_to_show
  91. null, // message
  92. null, // sql_data
  93. $goto, // goto
  94. $pmaThemeImage, // pmaThemeImage
  95. null, // disp_query
  96. null, // disp_message
  97. null, // query_type
  98. $sql_query, // sql_query
  99. null, // selectedTables
  100. null // complete_query
  101. );
  102. }
  103. }
  104. $sub_part = '_qbe';
  105. require 'libraries/db_common.inc.php';
  106. $url_query .= '&amp;goto=db_qbe.php';
  107. $url_params['goto'] = 'db_qbe.php';
  108. list(
  109. $tables,
  110. $num_tables,
  111. $total_num_tables,
  112. $sub_part,
  113. $is_show_stats,
  114. $db_is_system_schema,
  115. $tooltip_truename,
  116. $tooltip_aliasname,
  117. $pos
  118. ) = Util::getDbInfo($db, isset($sub_part) ? $sub_part : '');
  119. if ($message_to_display) {
  120. Message::error(
  121. __('You have to choose at least one column to display!')
  122. )
  123. ->display();
  124. }
  125. unset($message_to_display);
  126. // create new qbe search instance
  127. $db_qbe = new Qbe($GLOBALS['db'], $savedSearchList, $savedSearch);
  128. $secondaryTabs = [
  129. 'multi' => [
  130. 'link' => 'db_multi_table_query.php',
  131. 'text' => __('Multi-table query'),
  132. ],
  133. 'qbe' => [
  134. 'link' => 'db_qbe.php',
  135. 'text' => __('Query by example'),
  136. ],
  137. ];
  138. $response->addHTML(
  139. Template::get('secondary_tabs')->render([
  140. 'url_params' => $url_params,
  141. 'sub_tabs' => $secondaryTabs,
  142. ])
  143. );
  144. $url = 'db_designer.php' . Url::getCommon(
  145. array_merge(
  146. $url_params,
  147. array('query' => 1)
  148. )
  149. );
  150. $response->addHTML(
  151. Message::notice(
  152. sprintf(
  153. __('Switch to %svisual builder%s'),
  154. '<a href="' . $url . '">',
  155. '</a>'
  156. )
  157. )
  158. );
  159. /**
  160. * Displays the Query by example form
  161. */
  162. $response->addHTML($db_qbe->getSelectionForm());