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