3 declare(strict_types
=1);
7 use PhpMyAdmin\SqlParser\Utils\Query
;
8 use PhpMyAdmin\SqlParser\Utils\StatementInfo
;
11 use function strcasecmp
;
14 * Parse and analyse a SQL query
19 * Calls the parser on a query
21 * @param string $sqlQuery the query to parse
22 * @param string $db the current database
24 * @return array<int, StatementInfo|string>
25 * @psalm-return array{StatementInfo, string, string}
27 public static function sqlQuery(string $sqlQuery, string $db): array
29 $info = Query
::getAll($sqlQuery);
33 // If the targeted table (and database) are different than the ones that is
34 // currently browsed, edit `$db` and `$table` to match them so other elements
35 // (page headers, links, navigation panel) can be updated properly.
36 if ($info->selectTables
!== []) {
37 // Previous table and database name is stored to check if it changed.
40 if (count($info->selectTables
) > 1) {
42 * @todo if there are more than one table name in the Select:
43 * - do not extract the first table name
44 * - do not show a table name in the page header
45 * - do not display the sub-pages links)
49 $table = $info->selectTables
[0][0] ??
'';
50 if (isset($info->selectTables
[0][1])) {
51 $db = $info->selectTables
[0][1];
55 // There is no point checking if a reloading is required if we already decided
56 // to reload. Also, no reload is required for AJAX requests.
57 $response = ResponseRenderer
::getInstance();
58 if (! $info->flags
->reload
&& ! $response->isAjax()) {
59 // NOTE: Database names are case-insensitive.
60 $info->flags
->reload
= strcasecmp($db, $previousDb) !== 0;
64 return [$info, $db, $table];