Skip to content
Open
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
6 changes: 0 additions & 6 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1548,12 +1548,6 @@ parameters:
count: 4
path: src/Statements/WithStatement.php

-
message: '#^Only booleans are allowed in a negated boolean, int\|false given\.$#'
identifier: booleanNot.exprNotBoolean
count: 1
path: src/Statements/WithStatement.php

-
message: '#^Only booleans are allowed in an if condition, PhpMyAdmin\\SqlParser\\Parser\|null given\.$#'
identifier: if.condNotBoolean
Expand Down
12 changes: 11 additions & 1 deletion src/Statements/WithStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use PhpMyAdmin\SqlParser\Parsers\Array2d;
use PhpMyAdmin\SqlParser\Parsers\OptionsArrays;
use PhpMyAdmin\SqlParser\Statement;
use PhpMyAdmin\SqlParser\Token;
use PhpMyAdmin\SqlParser\TokensList;
use PhpMyAdmin\SqlParser\TokenType;
use PhpMyAdmin\SqlParser\Translator;
Expand Down Expand Up @@ -108,7 +109,16 @@ public function parse(Parser $parser, TokensList $list): void
}

if ($state === 0) {
if ($token->type !== TokenType::None || ! preg_match('/^[a-zA-Z0-9_$]+$/', $token->token)) {
// A CTE name may be a plain identifier, a non-reserved keyword
// (e.g. `data`) or a backtick-quoted identifier. MySQL accepts
// all three, so none of them should be rejected here (see #662).
$isPlainName = $token->type === TokenType::None
&& preg_match('/^[a-zA-Z0-9_$]+$/', $token->token) === 1;
$isQuotedName = $token->type === TokenType::Symbol
&& ($token->flags & Token::FLAG_SYMBOL_BACKTICK) !== 0;
$isNonReservedKeyword = $token->type === TokenType::Keyword
&& ($token->flags & Token::FLAG_KEYWORD_RESERVED) === 0;
if (! ($isPlainName || $isQuotedName || $isNonReservedKeyword)) {
$parser->error('The name of the CTE was expected.', $token);
break;
}
Expand Down
30 changes: 30 additions & 0 deletions tests/Parser/WithStatementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpMyAdmin\SqlParser\Components\WithKeyword;
use PhpMyAdmin\SqlParser\Lexer;
use PhpMyAdmin\SqlParser\Parser;
use PhpMyAdmin\SqlParser\Statements\WithStatement;
use PhpMyAdmin\SqlParser\Tests\TestCase;
use PHPUnit\Framework\Attributes\DataProvider;

Expand Down Expand Up @@ -70,6 +71,35 @@ public function testWith(): void
$this->assertEquals($expected, $parser->statements[0]->build());
}

/** @return array<string, array{string, string}> */
public static function cteNameCases(): array
{
return [
'non-reserved keyword' => ['WITH data AS (SELECT 1) SELECT * FROM data', 'data'],
'backtick keyword' => ['WITH `data` AS (SELECT 1) SELECT * FROM `data`', 'data'],
'backtick identifier with space' => ['WITH `my cte` AS (SELECT 1) SELECT * FROM `my cte`', 'my cte'],
];
}

#[DataProvider('cteNameCases')]
public function testWithNonReservedOrQuotedName(string $sql, string $expectedName): void
{
// https://github.com/phpmyadmin/sql-parser/issues/662
// A CTE name may be a non-reserved keyword (e.g. "data") or a
// backtick-quoted identifier; both must be accepted instead of being
// reported as "The name of the CTE was expected."
$lexer = new Lexer($sql);
self::assertCount(0, $this->getErrorsAsArray($lexer));

$parser = new Parser($lexer->list);
self::assertCount(0, $this->getErrorsAsArray($parser));
self::assertCount(1, $parser->statements);

$statement = $parser->statements[0];
self::assertInstanceOf(WithStatement::class, $statement);
self::assertArrayHasKey($expectedName, $statement->withers);
}

public function testWithRecursive(): void
{
$sql = <<<'SQL'
Expand Down