2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * query by example the whole database
8 declare(strict_types
=1);
10 use PhpMyAdmin\Database\Qbe
;
11 use PhpMyAdmin\DatabaseInterface
;
12 use PhpMyAdmin\Di\Container
;
13 use PhpMyAdmin\Message
;
14 use PhpMyAdmin\Relation
;
15 use PhpMyAdmin\Response
;
16 use PhpMyAdmin\SavedSearches
;
18 use PhpMyAdmin\Template
;
22 if (! defined('ROOT_PATH')) {
23 define('ROOT_PATH', __DIR__
. DIRECTORY_SEPARATOR
);
26 global $db, $pmaThemeImage, $url_query;
28 require_once ROOT_PATH
. 'libraries/common.inc.php';
30 $container = Container
::getDefaultContainer();
31 $container->set(Response
::class, Response
::getInstance());
33 /** @var Response $response */
34 $response = $container->get(Response
::class);
36 /** @var DatabaseInterface $dbi */
37 $dbi = $container->get(DatabaseInterface
::class);
39 /** @var Relation $relation */
40 $relation = $containerBuilder->get('relation');
41 /** @var Template $template */
42 $template = $containerBuilder->get('template');
44 // Gets the relation settings
45 $cfgRelation = $relation->getRelationsParam();
47 $savedSearchList = [];
49 $currentSearchId = null;
50 if ($cfgRelation['savedsearcheswork']) {
51 $header = $response->getHeader();
52 $scripts = $header->getScripts();
53 $scripts->addFile('db_qbe.js');
55 //Get saved search list.
56 $savedSearch = new SavedSearches($GLOBALS, $relation);
57 $savedSearch->setUsername($GLOBALS['cfg']['Server']['user'])
60 if (! empty($_POST['searchId'])) {
61 $savedSearch->setId($_POST['searchId']);
64 //Action field is sent.
65 if (isset($_POST['action'])) {
66 $savedSearch->setSearchName($_POST['searchName']);
67 if ('create' === $_POST['action']) {
68 $saveResult = $savedSearch->setId(null)
69 ->setCriterias($_POST)
71 } elseif ('update' === $_POST['action']) {
72 $saveResult = $savedSearch->setCriterias($_POST)
74 } elseif ('delete' === $_POST['action']) {
75 $deleteResult = $savedSearch->delete();
76 //After deletion, reset search.
77 $savedSearch = new SavedSearches($GLOBALS, $relation);
78 $savedSearch->setUsername($GLOBALS['cfg']['Server']['user'])
81 } elseif ('load' === $_POST['action']) {
82 if (empty($_POST['searchId'])) {
83 //when not loading a search, reset the object.
84 $savedSearch = new SavedSearches($GLOBALS, $relation);
85 $savedSearch->setUsername($GLOBALS['cfg']['Server']['user'])
89 $loadResult = $savedSearch->load();
92 //Else, it's an "update query"
95 $savedSearchList = $savedSearch->getList();
96 $currentSearchId = $savedSearch->getId();
100 * A query has been submitted -> (maybe) execute it
102 $message_to_display = false;
103 if (isset($_POST['submit_sql']) && ! empty($sql_query)) {
104 if (0 !== stripos($sql_query, "SELECT")) {
105 $message_to_display = true;
107 $goto = 'db_sql.php';
109 $sql->executeQueryAndSendQueryResponse(
110 null, // analyzed_sql_results
111 false, // is_gotofile
114 false, // find_real_end
115 null, // sql_query_for_bookmark
117 null, // message_to_show
121 $pmaThemeImage, // pmaThemeImage
123 null, // disp_message
125 $sql_query, // sql_query
126 null, // selectedTables
127 null // complete_query
133 require ROOT_PATH
. 'libraries/db_common.inc.php';
134 $url_query .= '&goto=db_qbe.php';
135 $url_params['goto'] = 'db_qbe.php';
143 $db_is_system_schema,
147 ) = Util
::getDbInfo($db, is_null($sub_part) ?
'' : $sub_part);
149 if ($message_to_display) {
151 __('You have to choose at least one column to display!')
155 unset($message_to_display);
157 // create new qbe search instance
158 $db_qbe = new Qbe($relation, $template, $dbi, $db, $savedSearchList, $savedSearch);
162 'link' => 'db_multi_table_query.php',
163 'text' => __('Multi-table query'),
166 'link' => 'db_qbe.php',
167 'text' => __('Query by example'),
171 $template->render('secondary_tabs', [
172 'url_params' => $url_params,
173 'sub_tabs' => $secondaryTabs,
177 $url = 'db_designer.php' . Url
::getCommon(
186 __('Switch to %svisual builder%s'),
187 '<a href="' . $url . '">',
194 * Displays the Query by example form
196 $response->addHTML($db_qbe->getSelectionForm());