Translated using Weblate (Portuguese (Brazil))
[phpmyadmin.git] / src / ParseAnalyze.php
blob083ceae8934589c36144cc90b9152a62d745fa38
1 <?php
2 /**
3 * Parse and analyse a SQL query
4 */
6 declare(strict_types=1);
8 namespace PhpMyAdmin;
10 use PhpMyAdmin\SqlParser\Utils\Query;
12 use function count;
13 use function strcasecmp;
15 /**
16 * PhpMyAdmin\ParseAnalyze class
18 class ParseAnalyze
20 /**
21 * Calls the parser on a query
23 * @param string $sqlQuery the query to parse
24 * @param string $db the current database
26 * @return array<int, StatementInfo|string>
27 * @psalm-return array{StatementInfo, string, string}
29 public static function sqlQuery(string $sqlQuery, string $db): array
31 // @todo: move to returned results (also in all the calling chain)
32 $GLOBALS['unparsed_sql'] = $sqlQuery;
34 $info = Query::getAll($sqlQuery);
36 $table = '';
38 // If the targeted table (and database) are different than the ones that is
39 // currently browsed, edit `$db` and `$table` to match them so other elements
40 // (page headers, links, navigation panel) can be updated properly.
41 if (! empty($info['select_tables'])) {
42 // Previous table and database name is stored to check if it changed.
43 $previousDb = $db;
45 if (count($info['select_tables']) > 1) {
47 /**
48 * @todo if there are more than one table name in the Select:
49 * - do not extract the first table name
50 * - do not show a table name in the page header
51 * - do not display the sub-pages links)
53 $table = '';
54 } else {
55 $table = $info['select_tables'][0][0] ?? '';
56 if (isset($info['select_tables'][0][1])) {
57 $db = $info['select_tables'][0][1];
61 // There is no point checking if a reloading is required if we already decided
62 // to reload. Also, no reload is required for AJAX requests.
63 $response = ResponseRenderer::getInstance();
64 if (empty($info['reload']) && ! $response->isAjax()) {
65 // NOTE: Database names are case-insensitive.
66 $info['reload'] = strcasecmp($db, $previousDb) !== 0;
70 return [StatementInfo::fromArray($info), $db, $table];