3 * functions for displaying the sql query form
6 * @usedby /database/sql
8 * @usedby /table/structure
9 * @usedby /table/tracking
12 declare(strict_types
=1);
16 use PhpMyAdmin\Bookmarks\BookmarkRepository
;
17 use PhpMyAdmin\ConfigStorage\Relation
;
18 use PhpMyAdmin\Html\MySQLDocumentation
;
19 use PhpMyAdmin\Utils\ForeignKey
;
22 use function htmlspecialchars
;
24 use function str_contains
;
27 * PhpMyAdmin\SqlQueryForm class
31 public function __construct(
32 private Template
$template,
33 private DatabaseInterface
$dbi,
34 private readonly BookmarkRepository
$bookmarkRepository,
39 * return HTML for the sql query boxes
41 * @param bool|string $query query to display in the textarea
42 * or true to display last executed
43 * @param bool|string $displayTab sql|full|false
44 * what part to display
45 * false if not inside querywindow
46 * @param string $delimiter delimiter
49 * @usedby /database/sql
51 * @usedby /table/structure
52 * @usedby /table/tracking
54 public function getHtml(
57 bool|
string $query = true,
58 bool|
string $displayTab = false,
59 string $delimiter = ';',
61 if ($displayTab === false ||
$displayTab === '') {
66 if ($query === true) {
67 $query = $GLOBALS['sql_query'];
68 if (empty($query) && (isset($_GET['show_query']) ||
isset($_POST['show_query']))) {
69 $query = $_GET['sql_query'] ??
$_POST['sql_query'] ??
'';
74 // prepare for server related
75 $goto = empty($GLOBALS['goto']) ? Url
::getFromRoute('/server/sql') : $GLOBALS['goto'];
76 } elseif ($table === '') {
77 // prepare for db related
78 $goto = empty($GLOBALS['goto']) ? Url
::getFromRoute('/database/sql') : $GLOBALS['goto'];
80 $goto = empty($GLOBALS['goto']) ? Url
::getFromRoute('/table/sql') : $GLOBALS['goto'];
83 if ($displayTab === 'full' ||
$displayTab === 'sql') {
84 [$legend, $query, $columnsList] = $this->init($query);
87 $relation = new Relation($this->dbi
);
88 $bookmarkFeature = $relation->getRelationParameters()->bookmarkFeature
;
91 $config = Config
::getInstance();
92 if ($displayTab === 'full' && $bookmarkFeature !== null) {
93 $bookmarkList = $this->bookmarkRepository
->getList($config->selectedServer
['user'], $db);
95 foreach ($bookmarkList as $bookmarkItem) {
97 'id' => $bookmarkItem->getId(),
98 'variable_count' => $bookmarkItem->getVariableCount(),
99 'label' => $bookmarkItem->getLabel(),
100 'is_shared' => $bookmarkItem->getUser() === '',
105 return $this->template
->render('sql/query', [
106 'legend' => $legend ??
'',
107 'textarea_cols' => $config->settings
['TextareaCols'],
108 'textarea_rows' => $config->settings
['TextareaRows'],
109 'textarea_auto_select' => $config->settings
['TextareaAutoSelect'],
110 'columns_list' => $columnsList ??
[],
111 'codemirror_enable' => $config->settings
['CodemirrorEnable'],
112 'has_bookmark' => $bookmarkFeature !== null,
113 'delimiter' => $delimiter,
114 'retain_query_box' => $config->settings
['RetainQueryBox'] !== false,
115 'is_upload' => $config->get('enable_upload'),
120 'display_tab' => $displayTab,
121 'bookmarks' => $bookmarks,
122 'can_convert_kanji' => Encoding
::canConvertKanji(),
123 'is_foreign_key_check' => ForeignKey
::isCheckEnabled(),
124 'allow_shared_bookmarks' => $config->settings
['AllowSharedBookmarks'],
129 * Get initial values for Sql Query Form Insert
131 * @param string $query query to display in the textarea
133 * @return array{string, string, ColumnFull[]}
135 public function init(string $query): array
138 $config = Config
::getInstance();
139 if ($GLOBALS['db'] === '') {
140 // prepare for server related
142 __('Run SQL query/queries on server “%s”'),
144 ! empty($config->settings
['Servers'][$GLOBALS['server']]['verbose'])
145 ?
$config->settings
['Servers'][$GLOBALS['server']]['verbose']
146 : $config->settings
['Servers'][$GLOBALS['server']]['host'],
149 } elseif ($GLOBALS['table'] === '') {
150 // prepare for db related
151 $db = $GLOBALS['db'];
152 // if you want navigation:
153 $scriptName = Util
::getScriptNameForOption($config->settings
['DefaultTabDatabase'], 'database');
154 $tmpDbLink = '<a href="' . $scriptName
155 . Url
::getCommon(['db' => $db], ! str_contains($scriptName, '?') ?
'?' : '&')
157 $tmpDbLink .= htmlspecialchars($db) . '</a>';
158 $legend = sprintf(__('Run SQL query/queries on database %s'), $tmpDbLink);
160 $query = Util
::expandUserString($config->settings
['DefaultQueryDatabase'], Util
::backquote(...));
163 $db = $GLOBALS['db'];
164 $table = $GLOBALS['table'];
165 // Get the list and number of fields
166 // we do a try_query here, because we could be in the query window,
167 // trying to synchronize and the table has not yet been created
168 $columnsList = $this->dbi
->getColumns($db, $GLOBALS['table'], true);
170 $scriptName = Util
::getScriptNameForOption($config->settings
['DefaultTabTable'], 'table');
171 $tmpTblLink = '<a href="' . $scriptName . Url
::getCommon(['db' => $db, 'table' => $table], '&') . '">';
172 $tmpTblLink .= htmlspecialchars($db) . '.' . htmlspecialchars($table) . '</a>';
173 $legend = sprintf(__('Run SQL query/queries on table %s'), $tmpTblLink);
175 $query = Util
::expandUserString($config->settings
['DefaultQueryTable'], Util
::backquote(...));
179 $legend .= ': ' . MySQLDocumentation
::show('SELECT');
181 return [$legend, $query, $columnsList];