From 078e19d1c7038e2094cea2acfb498c5151d32d1f Mon Sep 17 00:00:00 2001 From: Benjamin Eberlei Date: Thu, 13 Jan 2011 21:16:08 +0100 Subject: [PATCH] DDC-980 - Fix Update and Delete statements reference of the root table when doing subselects. --- lib/Doctrine/ORM/Query/SqlWalker.php | 14 +++++----- .../ManyToManyBasicAssociationTest.php | 9 +++++++ .../ORM/Query/DeleteSqlGenerationTest.php | 11 ++++++++ .../Tests/ORM/Query/ParserResultTest.php | 26 +++++++++---------- .../ORM/Query/UpdateSqlGenerationTest.php | 11 ++++++++ 5 files changed, 51 insertions(+), 20 deletions(-) diff --git a/lib/Doctrine/ORM/Query/SqlWalker.php b/lib/Doctrine/ORM/Query/SqlWalker.php index 3cf472000..827ebe4ab 100644 --- a/lib/Doctrine/ORM/Query/SqlWalker.php +++ b/lib/Doctrine/ORM/Query/SqlWalker.php @@ -209,9 +209,13 @@ class SqlWalker implements TreeWalker * * @param string $tableName * @param string $alias + * @param string $dqlAlias + * @return string */ - public function setSqlTableAlias($tableName, $alias) + public function setSqlTableAlias($tableName, $alias, $dqlAlias = '') { + $tableName .= $dqlAlias; + $this->_tableAliasMap[$tableName] = $alias; return $alias; @@ -1260,9 +1264,7 @@ class SqlWalker implements TreeWalker $class = $this->_em->getClassMetadata($deleteClause->abstractSchemaName); $sql .= $class->getQuotedTableName($this->_platform); - if ($this->_useSqlTableAliases) { - $sql .= ' ' . $this->getSqlTableAlias($class->getTableName()); - } + $this->setSqlTableAlias($class->getTableName(), $class->getTableName(), $deleteClause->aliasIdentificationVariable); $this->_rootAliases[] = $deleteClause->aliasIdentificationVariable; @@ -1281,9 +1283,7 @@ class SqlWalker implements TreeWalker $class = $this->_em->getClassMetadata($updateClause->abstractSchemaName); $sql .= $class->getQuotedTableName($this->_platform); - if ($this->_useSqlTableAliases) { - $sql .= ' ' . $this->getSqlTableAlias($class->getTableName()); - } + $this->setSqlTableAlias($class->getTableName(), $class->getTableName(), $updateClause->aliasIdentificationVariable); $this->_rootAliases[] = $updateClause->aliasIdentificationVariable; diff --git a/tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php b/tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php index eef376baf..c2df6c467 100644 --- a/tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/ManyToManyBasicAssociationTest.php @@ -299,4 +299,13 @@ class ManyToManyBasicAssociationTest extends \Doctrine\Tests\OrmFunctionalTestCa return $user; } + + /** + * @group DDC-980 + */ + public function testUpdateDeleteSizeSubselectQueries() + { + $this->_em->createQuery("DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE SIZE(u.groups) = 10")->execute(); + $this->_em->createQuery("UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.status = 'inactive' WHERE SIZE(u.groups) = 10")->execute(); + } } diff --git a/tests/Doctrine/Tests/ORM/Query/DeleteSqlGenerationTest.php b/tests/Doctrine/Tests/ORM/Query/DeleteSqlGenerationTest.php index 8d1597629..1b2cd245e 100644 --- a/tests/Doctrine/Tests/ORM/Query/DeleteSqlGenerationTest.php +++ b/tests/Doctrine/Tests/ORM/Query/DeleteSqlGenerationTest.php @@ -273,4 +273,15 @@ class DeleteSqlGenerationTest extends \Doctrine\Tests\OrmTestCase 'DELETE FROM cms_users WHERE id NOT IN (?, ?)' ); } + + /** + * @group DDC-980 + */ + public function testSubselectTableAliasReferencing() + { + $this->assertSqlGeneration( + 'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE SIZE(u.groups) = 10', + 'DELETE FROM cms_users WHERE (SELECT COUNT(*) FROM cms_users_groups c0_ WHERE c0_.user_id = cms_users.id) = 10' + ); + } } diff --git a/tests/Doctrine/Tests/ORM/Query/ParserResultTest.php b/tests/Doctrine/Tests/ORM/Query/ParserResultTest.php index d9449720d..d4dc95f6a 100644 --- a/tests/Doctrine/Tests/ORM/Query/ParserResultTest.php +++ b/tests/Doctrine/Tests/ORM/Query/ParserResultTest.php @@ -6,43 +6,43 @@ use Doctrine\ORM\Query\ParserResult; class ParserResultTest extends \PHPUnit_Framework_TestCase { - public $result; + public $parserResult; public function setUp() { - $this->result = new ParserResult(); + $this->parserResult = new ParserResult(); } public function testGetRsm() { $this->assertType( 'Doctrine\ORM\Query\ResultSetMapping', - $this->result->getResultSetMapping() + $this->parserResult->getResultSetMapping() ); } public function testSetGetSqlExecutor() { - $this->assertNull($this->result->getSqlExecutor()); + $this->assertNull($this->parserResult->getSqlExecutor()); $executor = $this->getMock('Doctrine\ORM\Query\Exec\AbstractSqlExecutor', array('execute')); - $this->result->setSqlExecutor($executor); - $this->assertSame($executor, $this->result->getSqlExecutor()); + $this->parserResult->setSqlExecutor($executor); + $this->assertSame($executor, $this->parserResult->getSqlExecutor()); } public function testGetSqlParameterPosition() { - $this->result->addParameterMapping(1, 1); - $this->result->addParameterMapping(1, 2); - $this->assertEquals(array(1, 2), $this->result->getSqlParameterPositions(1)); + $this->parserResult->addParameterMapping(1, 1); + $this->parserResult->addParameterMapping(1, 2); + $this->assertEquals(array(1, 2), $this->parserResult->getSqlParameterPositions(1)); } public function testGetParameterMappings() { - $this->assertType('array', $this->result->getParameterMappings()); + $this->assertType('array', $this->parserResult->getParameterMappings()); - $this->result->addParameterMapping(1, 1); - $this->result->addParameterMapping(1, 2); - $this->assertEquals(array(1 => array(1, 2)), $this->result->getParameterMappings()); + $this->parserResult->addParameterMapping(1, 1); + $this->parserResult->addParameterMapping(1, 2); + $this->assertEquals(array(1 => array(1, 2)), $this->parserResult->getParameterMappings()); } } \ No newline at end of file diff --git a/tests/Doctrine/Tests/ORM/Query/UpdateSqlGenerationTest.php b/tests/Doctrine/Tests/ORM/Query/UpdateSqlGenerationTest.php index 725e2aeb6..a8a59ff63 100644 --- a/tests/Doctrine/Tests/ORM/Query/UpdateSqlGenerationTest.php +++ b/tests/Doctrine/Tests/ORM/Query/UpdateSqlGenerationTest.php @@ -175,4 +175,15 @@ class UpdateSqlGenerationTest extends \Doctrine\Tests\OrmTestCase "UPDATE cms_comments SET article_id = NULL WHERE article_id = ?" ); } + + /** + * @group DDC-980 + */ + public function testSubselectTableAliasReferencing() + { + $this->assertSqlGeneration( + "UPDATE Doctrine\Tests\Models\CMS\CmsUser u SET u.status = 'inactive' WHERE SIZE(u.groups) = 10", + "UPDATE cms_users SET status = 'inactive' WHERE (SELECT COUNT(*) FROM cms_users_groups c0_ WHERE c0_.user_id = cms_users.id) = 10" + ); + } }