Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,12 @@ parameters:

-
message: "#^Cannot access property \\$idx on PhpMyAdmin\\\\SqlParser\\\\TokensList\\|null\\.$#"
count: 8
count: 12
path: src/Parser.php

-
message: "#^Cannot access property \\$keyword on PhpMyAdmin\\\\SqlParser\\\\Token\\|null\\.$#"
count: 2
path: src/Parser.php

-
Expand All @@ -542,7 +547,7 @@ parameters:

-
message: "#^Cannot call method getNextOfType\\(\\) on PhpMyAdmin\\\\SqlParser\\\\TokensList\\|null\\.$#"
count: 2
count: 4
path: src/Parser.php

-
Expand Down Expand Up @@ -960,11 +965,6 @@ parameters:
count: 1
path: src/Utils/Query.php

-
message: "#^Method PhpMyAdmin\\\\SqlParser\\\\Utils\\\\Query\\:\\:getFlags\\(\\) should return array\\{distinct\\?\\: bool, drop_database\\?\\: bool, group\\?\\: bool, having\\?\\: bool, is_affected\\?\\: bool, is_analyse\\?\\: bool, is_count\\?\\: bool, is_delete\\?\\: bool, \\.\\.\\.\\} but returns non\\-empty\\-array\\<literal\\-string&non\\-falsy\\-string, bool\\|\\(literal\\-string&non\\-falsy\\-string\\)\\>\\.$#"
count: 1
path: src/Utils/Query.php

-
message: "#^Parameter \\#1 \\$component of static method PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\:\\:build\\(\\) expects array\\<PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression\\>\\|PhpMyAdmin\\\\SqlParser\\\\Components\\\\Expression, object given\\.$#"
count: 1
Expand Down
45 changes: 33 additions & 12 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,27 +466,48 @@ public function parse()
continue;
}

// Checking if it is a known statement that can be parsed.
if (empty(static::$STATEMENT_PARSERS[$token->keyword])) {
if (! isset(static::$STATEMENT_PARSERS[$token->keyword])) {
// A statement is considered recognized if the parser
// is aware that it is a statement, but it does not have
// a parser for it yet.
$this->error('Unrecognized statement type.', $token);
$lastIdx = $list->idx;
$statementName = null;

if ($token->keyword === 'ANALYZE') {
++$list->idx; // Skip ANALYZE

$first = $list->getNextOfType(Token::TYPE_KEYWORD);
$second = $list->getNextOfType(Token::TYPE_KEYWORD);

// ANALYZE keyword can be an indication of two cases:
// 1 - ANALYZE TABLE statements, in both MariaDB and MySQL
// 2 - Explain statement, in case of MariaDB https://mariadb.com/kb/en/explain-analyze/
// We need to point case 2 to use the EXPLAIN Parser.
$statementName = 'EXPLAIN';
if ($first->keyword === 'TABLE' || $second->keyword === 'TABLE') {
$statementName = 'ANALYZE';
}

// Skipping to the end of this statement.
$list->getNextOfType(Token::TYPE_DELIMITER);
$prevLastIdx = $list->idx;
continue;
$list->idx = $lastIdx;
} else {
// Checking if it is a known statement that can be parsed.
if (empty(static::$STATEMENT_PARSERS[$token->keyword])) {
if (! isset(static::$STATEMENT_PARSERS[$token->keyword])) {
// A statement is considered recognized if the parser
// is aware that it is a statement, but it does not have
// a parser for it yet.
$this->error('Unrecognized statement type.', $token);
}

// Skipping to the end of this statement.
$list->getNextOfType(Token::TYPE_DELIMITER);
$prevLastIdx = $list->idx;
continue;
}
}

/**
* The name of the class that is used for parsing.
*
* @var string
*/
$class = static::$STATEMENT_PARSERS[$token->keyword];
$class = static::$STATEMENT_PARSERS[$statementName ?? $token->keyword];

/**
* Processed statement.
Expand Down
29 changes: 29 additions & 0 deletions tests/Parser/AnalyzeStatementTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace PhpMyAdmin\SqlParser\Tests\Parser;

use PhpMyAdmin\SqlParser\Tests\TestCase;

class AnalyzeStatementTest extends TestCase
{
/**
* @dataProvider analyzeProvider
*/
public function testAnalyze(string $test): void
{
$this->runParserTest($test);
}

/**
* @return string[][]
*/
public function analyzeProvider(): array
{
return [
['parser/parseAnalyzeTable'],
['parser/parseAnalyzeTable1'],
];
}
}
1 change: 1 addition & 0 deletions tests/Parser/ExplainStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function explainProvider(): array
{
return [
['parser/parseExplain'],
['parser/parseExplain1'],
];
}
}
1 change: 1 addition & 0 deletions tests/data/parser/parseAnalyzeTable.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ANALYZE TABLE tbl
Loading