diff --git a/lib/Doctrine/ORM/Query/SqlWalker.php b/lib/Doctrine/ORM/Query/SqlWalker.php index 8e9b8b566..6fe4abc56 100644 --- a/lib/Doctrine/ORM/Query/SqlWalker.php +++ b/lib/Doctrine/ORM/Query/SqlWalker.php @@ -307,7 +307,20 @@ class SqlWalker public function walkDeleteStatement(AST\DeleteStatement $AST) { + $sql = $this->walkDeleteClause($AST->getDeleteClause()); + $sql .= $AST->getWhereClause() ? $this->walkWhereClause($AST->getWhereClause()) : ''; + return $sql; + } + public function walkDeleteClause(AST\DeleteClause $deleteClause) + { + $sql = 'DELETE FROM '; + $class = $this->_em->getClassMetadata($deleteClause->getAbstractSchemaName()); + $sql .= $class->getTableName(); + if ($deleteClause->getAliasIdentificationVariable()) { + $sql .= ' ' . $this->_dqlToSqlAliasMap[$deleteClause->getAliasIdentificationVariable()]; + } + return $sql; } public function walkWhereClause($whereClause) @@ -328,7 +341,7 @@ class SqlWalker public function walkConditionalFactor($factor) { $sql = ''; - if ($factor->isNot()) $sql .= ' NOT '; + if ($factor->isNot()) $sql .= 'NOT '; $primary = $factor->getConditionalPrimary(); if ($primary->isSimpleConditionalExpression()) { $simpleCond = $primary->getSimpleConditionalExpression(); diff --git a/tests/Doctrine/Tests/ORM/Query/DqlGenerationTest.php b/tests/Doctrine/Tests/ORM/Criteria/DqlGenerationTest.php similarity index 97% rename from tests/Doctrine/Tests/ORM/Query/DqlGenerationTest.php rename to tests/Doctrine/Tests/ORM/Criteria/DqlGenerationTest.php index 31e91a033..4d4a082ce 100644 --- a/tests/Doctrine/Tests/ORM/Query/DqlGenerationTest.php +++ b/tests/Doctrine/Tests/ORM/Criteria/DqlGenerationTest.php @@ -16,31 +16,31 @@ * * This software consists of voluntary contributions made by many individuals * and is licensed under the LGPL. For more information, see - * . + * . */ +namespace Doctrine\Tests\ORM\Query; + +require_once __DIR__ . '/../../TestInit.php'; + /** * Test case for testing the saving and referencing of query identifiers. * - * @package Doctrine - * @subpackage Query * @author Guilherme Blanco * @author Janne Vanhala * @author Konsta Vesterinen * @license http://www.opensource.org/licenses/lgpl-license.php LGPL - * @link http://www.phpdoctrine.org + * @link http://www.doctrine-project.org * @since 2.0 * @version $Revision$ */ -class Orm_Query_DqlGenerationTest extends Doctrine_OrmTestCase +class DqlGenerationTest extends \Doctrine\Tests\OrmTestCase { protected function createQuery() { - $entityManager = $this->_em; - return $entityManager->createQuery(); + return $this->_em->createQuery(); } - public function testSelect() { $query = $this->createQuery(); diff --git a/tests/Doctrine/Tests/ORM/Query/AllTests.php b/tests/Doctrine/Tests/ORM/Query/AllTests.php index a07fc93b8..b19712fd7 100644 --- a/tests/Doctrine/Tests/ORM/Query/AllTests.php +++ b/tests/Doctrine/Tests/ORM/Query/AllTests.php @@ -23,11 +23,9 @@ class AllTests $suite->addTestSuite('Doctrine\Tests\ORM\Query\SelectSqlGenerationTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Query\LanguageRecognitionTest'); $suite->addTestSuite('Doctrine\Tests\ORM\Query\LexerTest'); + $suite->addTestSuite('Doctrine\Tests\ORM\Query\DeleteSqlGenerationTest'); /* - - $suite->addTestSuite('Orm_Query_DqlGenerationTest'); - $suite->addTestSuite('Orm_Query_DeleteSqlGenerationTest'); $suite->addTestSuite('Orm_Query_UpdateSqlGenerationTest');*/ return $suite; diff --git a/tests/Doctrine/Tests/ORM/Query/DeleteSqlGenerationTest.php b/tests/Doctrine/Tests/ORM/Query/DeleteSqlGenerationTest.php index 8c123a558..da037872a 100644 --- a/tests/Doctrine/Tests/ORM/Query/DeleteSqlGenerationTest.php +++ b/tests/Doctrine/Tests/ORM/Query/DeleteSqlGenerationTest.php @@ -16,14 +16,16 @@ * * This software consists of voluntary contributions made by many individuals * and is licensed under the LGPL. For more information, see - * . + * . */ -require_once 'lib/DoctrineTestInit.php'; + +namespace Doctrine\Tests\ORM\Query; + +require_once __DIR__ . '/../../TestInit.php'; + /** * Test case for testing the saving and referencing of query identifiers. * - * @package Doctrine - * @subpackage Query * @author Guilherme Blanco * @author Janne Vanhala * @author Konsta Vesterinen @@ -35,166 +37,146 @@ require_once 'lib/DoctrineTestInit.php'; * testcases later since we'll have a lot of them and we might want to have special SQL * generation tests for some dbms specific SQL syntaxes. */ -class Orm_Query_DeleteSqlGenerationTest extends Doctrine_OrmTestCase +class DeleteSqlGenerationTest extends \Doctrine\Tests\OrmTestCase { + private $_em; + + protected function setUp() { + $this->_em = $this->_getTestEntityManager(); + } + public function assertSqlGeneration($dqlToBeTested, $sqlToBeConfirmed) { try { - $entityManager = $this->_em; - $query = $entityManager->createQuery($dqlToBeTested); - + $query = $this->_em->createQuery($dqlToBeTested); parent::assertEquals($sqlToBeConfirmed, $query->getSql()); - $query->free(); - } catch (Doctrine_Exception $e) { + } catch (\Exception $e) { $this->fail($e->getMessage()); } } - public function testWithoutWhere() { - // NO WhereClause $this->assertSqlGeneration( - 'DELETE CmsUser u', - 'DELETE FROM cms_user cu WHERE 1 = 1' + 'DELETE Doctrine\Tests\Models\CMS\CmsUser u', + 'DELETE FROM cms_users c0' ); - $this->assertSqlGeneration( - 'DELETE FROM CmsUser u', - 'DELETE FROM cms_user cu WHERE 1 = 1' + 'DELETE FROM Doctrine\Tests\Models\CMS\CmsUser u', + 'DELETE FROM cms_users c0' ); } - public function testWithWhere() { - // "WHERE" ConditionalExpression - // ConditionalExpression = ConditionalTerm {"OR" ConditionalTerm} - // ConditionalTerm = ConditionalFactor {"AND" ConditionalFactor} - // ConditionalFactor = ["NOT"] ConditionalPrimary - // ConditionalPrimary = SimpleConditionalExpression | "(" ConditionalExpression ")" - // SimpleConditionalExpression - // = Expression (ComparisonExpression | BetweenExpression | LikeExpression - // | InExpression | NullComparisonExpression) | ExistsExpression - - // If this one test fail, all others will fail too. That's the simplest case possible $this->assertSqlGeneration( - 'DELETE CmsUser u WHERE id = ?', - 'DELETE FROM cms_user cu WHERE cu.id = ?' + 'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1', + 'DELETE FROM cms_users c0 WHERE c0.id = ?' ); } - public function testWithConditionalExpressions() { $this->assertSqlGeneration( - 'DELETE CmsUser u WHERE u.username = ? OR u.name = ?', - 'DELETE FROM cms_user cu WHERE cu.username = ? OR cu.name = ?' + 'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = ?1 OR u.name = ?2', + 'DELETE FROM cms_users c0 WHERE c0.username = ? OR c0.name = ?' ); $this->assertSqlGeneration( - 'DELETE CmsUser u WHERE u.id = ? OR ( u.username = ? OR u.name = ? )', - 'DELETE FROM cms_user cu WHERE cu.id = ? OR (cu.username = ? OR cu.name = ?)' + 'DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1 OR ( u.username = ?2 OR u.name = ?3)', + 'DELETE FROM cms_users c0 WHERE c0.id = ? OR (c0.username = ? OR c0.name = ?)' ); - $this->assertSqlGeneration( - 'DELETE FROM CmsUser WHERE id = ?', - 'DELETE FROM cms_user cu WHERE cu.id = ?' - ); + /*$this->assertSqlGeneration( + 'DELETE FROM Doctrine\Tests\Models\CMS\CmsUser WHERE id = ?1', + 'DELETE FROM cms_users WHERE id = ?' + );*/ } - public function testParserIsCaseAgnostic() { $this->assertSqlGeneration( - "delete from CmsUser u where u.username = ?", - "DELETE FROM cms_user cu WHERE cu.username = ?" + "delete from Doctrine\Tests\Models\CMS\CmsUser u where u.username = ?1", + "DELETE FROM cms_users c0 WHERE c0.username = ?" ); } - public function testWithConditionalTerms() { $this->assertSqlGeneration( - "DELETE CmsUser u WHERE u.username = ? AND u.name = ?", - "DELETE FROM cms_user cu WHERE cu.username = ? AND cu.name = ?" + "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = ?1 AND u.name = ?2", + "DELETE FROM cms_users c0 WHERE c0.username = ? AND c0.name = ?" ); } - public function testWithConditionalFactors() { $this->assertSqlGeneration( - "DELETE CmsUser u WHERE NOT id != ?", - "DELETE FROM cms_user cu WHERE NOT cu.id <> ?" + "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE NOT u.id != ?1", + "DELETE FROM cms_users c0 WHERE NOT c0.id <> ?" ); $this->assertSqlGeneration( - "DELETE CmsUser u WHERE NOT ( id != ? )", - "DELETE FROM cms_user cu WHERE NOT (cu.id <> ?)" + "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE NOT ( u.id != ?1 )", + "DELETE FROM cms_users c0 WHERE NOT (c0.id <> ?)" ); $this->assertSqlGeneration( - "DELETE CmsUser u WHERE NOT ( id != ? AND username = ? )", - "DELETE FROM cms_user cu WHERE NOT (cu.id <> ? AND cu.username = ?)" + "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE NOT ( u.id != ?1 AND u.username = ?2 )", + "DELETE FROM cms_users c0 WHERE NOT (c0.id <> ? AND c0.username = ?)" ); } - // ConditionalPrimary was already tested (see testDeleteWithWhere() and testDeleteWithConditionalFactors()) - public function testWithExprAndComparison() { // id = ? was already tested (see testDeleteWithWhere()) $this->assertSqlGeneration( - "DELETE CmsUser u WHERE id > ?", - "DELETE FROM cms_user cu WHERE cu.id > ?" + "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id > ?1", + "DELETE FROM cms_users c0 WHERE c0.id > ?" ); $this->assertSqlGeneration( - "DELETE CmsUser u WHERE id >= ?", - "DELETE FROM cms_user cu WHERE cu.id >= ?" + "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id >= ?1", + "DELETE FROM cms_users c0 WHERE c0.id >= ?" ); $this->assertSqlGeneration( - "DELETE CmsUser u WHERE id < ?", - "DELETE FROM cms_user cu WHERE cu.id < ?" + "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id < ?1", + "DELETE FROM cms_users c0 WHERE c0.id < ?" ); $this->assertSqlGeneration( - "DELETE CmsUser u WHERE id <= ?", - "DELETE FROM cms_user cu WHERE cu.id <= ?" + "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id <= ?1", + "DELETE FROM cms_users c0 WHERE c0.id <= ?" ); $this->assertSqlGeneration( - "DELETE CmsUser u WHERE id <> ?", - "DELETE FROM cms_user cu WHERE cu.id <> ?" + "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id <> ?1", + "DELETE FROM cms_users c0 WHERE c0.id <> ?" ); $this->assertSqlGeneration( - "DELETE CmsUser u WHERE id != ?", - "DELETE FROM cms_user cu WHERE cu.id <> ?" + "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id != ?1", + "DELETE FROM cms_users c0 WHERE c0.id <> ?" ); } - - +/* public function testWithExprAndBetween() { - // "WHERE" Expression BetweenExpression $this->assertSqlGeneration( - "DELETE CmsUser u WHERE u.id NOT BETWEEN ? AND ?", - "DELETE FROM cms_user cu WHERE cu.id NOT BETWEEN ? AND ?" + "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id NOT BETWEEN ?1 AND ?2", + "DELETE FROM cms_users c0 WHERE c0.id NOT BETWEEN ? AND ?" ); $this->assertSqlGeneration( - "DELETE CmsUser u WHERE u.id BETWEEN ? AND ? AND u.username != ?", - "DELETE FROM cms_user cu WHERE cu.id BETWEEN ? AND ? AND cu.username <> ?" + "DELETE Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id BETWEEN ?1 AND ?2 AND u.username != ?3", + "DELETE FROM cms_users c0 WHERE c0.id BETWEEN ? AND ? AND c0.username <> ?" ); } - public function testWithExprAndLike() { // "WHERE" Expression LikeExpression @@ -255,4 +237,5 @@ class Orm_Query_DeleteSqlGenerationTest extends Doctrine_OrmTestCase 'DELETE FROM cms_user cu WHERE ? = 1' ); } + */ }