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 private readonly Config
$config;
33 public function __construct(
34 private Template
$template,
35 private DatabaseInterface
$dbi,
36 private readonly BookmarkRepository
$bookmarkRepository,
38 $this->config
= Config
::getInstance();
42 * return HTML for the sql query boxes
44 * @param bool|string $query query to display in the textarea
45 * or true to display last executed
46 * @param bool|string $displayTab sql|full|false
47 * what part to display
48 * false if not inside querywindow
49 * @param string $delimiter delimiter
52 * @usedby /database/sql
54 * @usedby /table/structure
55 * @usedby /table/tracking
57 public function getHtml(
60 bool|
string $query = true,
61 bool|
string $displayTab = false,
62 string $delimiter = ';',
64 if ($displayTab === false ||
$displayTab === '') {
69 if ($query === true) {
70 $query = $GLOBALS['sql_query'];
71 if (empty($query) && (isset($_GET['show_query']) ||
isset($_POST['show_query']))) {
72 $query = $_GET['sql_query'] ??
$_POST['sql_query'] ??
'';
77 // prepare for server related
78 $goto = empty($GLOBALS['goto']) ? Url
::getFromRoute('/server/sql') : $GLOBALS['goto'];
79 } elseif ($table === '') {
80 // prepare for db related
81 $goto = empty($GLOBALS['goto']) ? Url
::getFromRoute('/database/sql') : $GLOBALS['goto'];
83 $goto = empty($GLOBALS['goto']) ? Url
::getFromRoute('/table/sql') : $GLOBALS['goto'];
86 if ($displayTab === 'full' ||
$displayTab === 'sql') {
87 [$legend, $query, $columnsList] = $this->init($query);
90 $relation = new Relation($this->dbi
);
91 $bookmarkFeature = $relation->getRelationParameters()->bookmarkFeature
;
94 if ($displayTab === 'full' && $bookmarkFeature !== null) {
95 $bookmarkList = $this->bookmarkRepository
->getList($this->config
->selectedServer
['user'], $db);
97 foreach ($bookmarkList as $bookmarkItem) {
99 'id' => $bookmarkItem->getId(),
100 'variable_count' => $bookmarkItem->getVariableCount(),
101 'label' => $bookmarkItem->getLabel(),
102 'is_shared' => $bookmarkItem->getUser() === '',
107 return $this->template
->render('sql/query', [
108 'legend' => $legend ??
'',
109 'textarea_cols' => $this->config
->settings
['TextareaCols'],
110 'textarea_rows' => $this->config
->settings
['TextareaRows'],
111 'textarea_auto_select' => $this->config
->settings
['TextareaAutoSelect'],
112 'columns_list' => $columnsList ??
[],
113 'codemirror_enable' => $this->config
->settings
['CodemirrorEnable'],
114 'has_bookmark' => $bookmarkFeature !== null,
115 'delimiter' => $delimiter,
116 'retain_query_box' => $this->config
->settings
['RetainQueryBox'] !== false,
117 'is_upload' => $this->config
->get('enable_upload'),
122 'display_tab' => $displayTab,
123 'bookmarks' => $bookmarks,
124 'can_convert_kanji' => Encoding
::canConvertKanji(),
125 'is_foreign_key_check' => ForeignKey
::isCheckEnabled(),
126 'allow_shared_bookmarks' => $this->config
->settings
['AllowSharedBookmarks'],
131 * Get initial values for Sql Query Form Insert
133 * @param string $query query to display in the textarea
135 * @return array{string, string, ColumnFull[]}
137 public function init(string $query): array
140 if (Current
::$database === '') {
141 // prepare for server related
143 __('Run SQL query/queries on server “%s”'),
145 ! empty($this->config
->settings
['Servers'][Current
::$server]['verbose'])
146 ?
$this->config
->settings
['Servers'][Current
::$server]['verbose']
147 : $this->config
->settings
['Servers'][Current
::$server]['host'],
150 } elseif (Current
::$table === '') {
151 // prepare for db related
152 $db = Current
::$database;
153 // if you want navigation:
154 $scriptName = Util
::getScriptNameForOption($this->config
->settings
['DefaultTabDatabase'], 'database');
155 $tmpDbLink = '<a href="' . $scriptName
156 . Url
::getCommon(['db' => $db], ! str_contains($scriptName, '?') ?
'?' : '&')
158 $tmpDbLink .= htmlspecialchars($db) . '</a>';
159 $legend = sprintf(__('Run SQL query/queries on database %s'), $tmpDbLink);
161 $query = Util
::expandUserString($this->config
->settings
['DefaultQueryDatabase'], Util
::backquote(...));
164 $db = Current
::$database;
165 $table = Current
::$table;
166 // Get the list and number of fields
167 // we do a try_query here, because we could be in the query window,
168 // trying to synchronize and the table has not yet been created
169 $columnsList = $this->dbi
->getColumns($db, Current
::$table, true);
171 $scriptName = Util
::getScriptNameForOption($this->config
->settings
['DefaultTabTable'], 'table');
172 $tmpTblLink = '<a href="' . $scriptName . Url
::getCommon(['db' => $db, 'table' => $table], '&') . '">';
173 $tmpTblLink .= htmlspecialchars($db) . '.' . htmlspecialchars($table) . '</a>';
174 $legend = sprintf(__('Run SQL query/queries on table %s'), $tmpTblLink);
176 $query = Util
::expandUserString($this->config
->settings
['DefaultQueryTable'], Util
::backquote(...));
180 $legend .= ': ' . MySQLDocumentation
::show('SELECT');
182 return [$legend, $query, $columnsList];