Skip to content

Commit

Permalink
Merge pull request #7077 from lcobucci/fix-delete-bc-break
Browse files Browse the repository at this point in the history
Fix BC-break on delete without alias DQL
  • Loading branch information
Ocramius committed Feb 19, 2018
2 parents 44e82e2 + fc943b7 commit 1f82a20
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 23 deletions.
52 changes: 29 additions & 23 deletions lib/Doctrine/ORM/Query/Parser.php
Expand Up @@ -22,6 +22,7 @@
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Query;
use Doctrine\ORM\Query\AST\Functions;
use function strpos;

/**
* An LL(*) recursive-descent parser for the context-free grammar of the Doctrine Query Language.
Expand Down Expand Up @@ -303,21 +304,24 @@ public function match($token)
$lookaheadType = $this->lexer->lookahead['type'];

// Short-circuit on first condition, usually types match
if ($lookaheadType !== $token) {
// If parameter is not identifier (1-99) must be exact match
if ($token < Lexer::T_IDENTIFIER) {
$this->syntaxError($this->lexer->getLiteral($token));
}
if ($lookaheadType === $token) {
$this->lexer->moveNext();
return;
}

// If parameter is keyword (200+) must be exact match
if ($token > Lexer::T_IDENTIFIER) {
$this->syntaxError($this->lexer->getLiteral($token));
}
// If parameter is not identifier (1-99) must be exact match
if ($token < Lexer::T_IDENTIFIER) {
$this->syntaxError($this->lexer->getLiteral($token));
}

// If parameter is T_IDENTIFIER, then matches T_IDENTIFIER (100) and keywords (200+)
if ($token === Lexer::T_IDENTIFIER && $lookaheadType < Lexer::T_IDENTIFIER) {
$this->syntaxError($this->lexer->getLiteral($token));
}
// If parameter is keyword (200+) must be exact match
if ($token > Lexer::T_IDENTIFIER) {
$this->syntaxError($this->lexer->getLiteral($token));
}

// If parameter is T_IDENTIFIER, then matches T_IDENTIFIER (100) and keywords (200+)
if ($token === Lexer::T_IDENTIFIER && $lookaheadType < Lexer::T_IDENTIFIER) {
$this->syntaxError($this->lexer->getLiteral($token));
}

$this->lexer->moveNext();
Expand Down Expand Up @@ -958,20 +962,20 @@ public function AbstractSchemaName()
if ($this->lexer->isNextToken(Lexer::T_FULLY_QUALIFIED_NAME)) {
$this->match(Lexer::T_FULLY_QUALIFIED_NAME);

$schemaName = $this->lexer->token['value'];
} else if ($this->lexer->isNextToken(Lexer::T_IDENTIFIER)) {
return $this->lexer->token['value'];
}

if ($this->lexer->isNextToken(Lexer::T_IDENTIFIER)) {
$this->match(Lexer::T_IDENTIFIER);

$schemaName = $this->lexer->token['value'];
} else {
$this->match(Lexer::T_ALIASED_NAME);
return $this->lexer->token['value'];
}

list($namespaceAlias, $simpleClassName) = explode(':', $this->lexer->token['value']);
$this->match(Lexer::T_ALIASED_NAME);

$schemaName = $this->em->getConfiguration()->getEntityNamespace($namespaceAlias) . '\\' . $simpleClassName;
}
[$namespaceAlias, $simpleClassName] = explode(':', $this->lexer->token['value']);

return $schemaName;
return $this->em->getConfiguration()->getEntityNamespace($namespaceAlias) . '\\' . $simpleClassName;
}

/**
Expand Down Expand Up @@ -1280,7 +1284,9 @@ public function DeleteClause()
$this->match(Lexer::T_AS);
}

$aliasIdentificationVariable = $this->AliasIdentificationVariable();
$aliasIdentificationVariable = $this->lexer->isNextToken(Lexer::T_IDENTIFIER)
? $this->AliasIdentificationVariable()
: 'alias_should_have_been_set';

$deleteClause->aliasIdentificationVariable = $aliasIdentificationVariable;
$class = $this->em->getClassMetadata($deleteClause->abstractSchemaName);
Expand Down
8 changes: 8 additions & 0 deletions tests/Doctrine/Tests/ORM/Query/DeleteSqlGenerationTest.php
Expand Up @@ -36,6 +36,14 @@ public function assertSqlGeneration($dqlToBeTested, $sqlToBeConfirmed)
}
}

public function testSupportsDeleteWithoutWhereAndAlias() : void
{
$this->assertSqlGeneration(
'DELETE FROM Doctrine\Tests\Models\CMS\CmsUser',
'DELETE FROM cms_users'
);
}

public function testSupportsDeleteWithoutWhereAndFrom()
{
$this->assertSqlGeneration(
Expand Down

0 comments on commit 1f82a20

Please sign in to comment.