3 * Parse and analyse a SQL query
6 declare(strict_types
=1);
10 use PhpMyAdmin\SqlParser\Utils\Query
;
13 use function strcasecmp
;
16 * PhpMyAdmin\ParseAnalyze class
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);
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.
45 if (count($info['select_tables']) > 1) {
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)
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];