Translated using Weblate (Portuguese)
[phpmyadmin.git] / src / ParseAnalyze.php
blob61e88e3b6594e879f01940c546d96df624ddbbfb
1 <?php
3 declare(strict_types=1);
5 namespace PhpMyAdmin;
7 use PhpMyAdmin\SqlParser\Utils\Query;
8 use PhpMyAdmin\SqlParser\Utils\StatementInfo;
10 use function count;
11 use function strcasecmp;
13 /**
14 * Parse and analyse a SQL query
16 class ParseAnalyze
18 /**
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);
31 $table = '';
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.
38 $previousDb = $db;
40 if (count($info->selectTables) > 1) {
41 /**
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)
47 $table = '';
48 } else {
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];