From 16aa558292d8513c27add2dd7ed0574b3d6820db Mon Sep 17 00:00:00 2001 From: jsor Date: Mon, 21 Nov 2011 15:08:36 +0100 Subject: [PATCH] Remove sql conversion from where clauses and update statements --- lib/Doctrine/ORM/Query/SqlWalker.php | 52 ++----------------- .../DbalTypes/NegativeToPositiveType.php | 2 +- .../Tests/ORM/Functional/TypeValueSqlTest.php | 5 +- .../ORM/Query/SelectSqlGenerationTest.php | 18 ++++++- .../ORM/Query/UpdateSqlGenerationTest.php | 12 +---- 5 files changed, 27 insertions(+), 62 deletions(-) diff --git a/lib/Doctrine/ORM/Query/SqlWalker.php b/lib/Doctrine/ORM/Query/SqlWalker.php index ccf6f2ca8..3b58dfba2 100644 --- a/lib/Doctrine/ORM/Query/SqlWalker.php +++ b/lib/Doctrine/ORM/Query/SqlWalker.php @@ -97,17 +97,6 @@ class SqlWalker implements TreeWalker * These should only be generated for SELECT queries, not for UPDATE/DELETE. */ private $_useSqlTableAliases = true; - - /** - * Flag that indicates whether to pass columns through Type::convertToPHPValueSQL(). - * These should only be done for SELECT queries, not for UPDATE. - */ - private $_useDbalTypeValueSql = true; - - /** - * Holds the current columns type. - */ - private $_currentColumnType; /** * The database platform abstraction. @@ -421,7 +410,6 @@ class SqlWalker implements TreeWalker public function walkUpdateStatement(AST\UpdateStatement $AST) { $this->_useSqlTableAliases = false; - $this->_useDbalTypeValueSql = false; return $this->walkUpdateClause($AST->updateClause) . $this->walkWhereClause($AST->whereClause); } @@ -477,20 +465,11 @@ class SqlWalker implements TreeWalker $dqlAlias = $pathExpr->identificationVariable; $class = $this->_queryComponents[$dqlAlias]['metadata']; - $column = ''; - if ($this->_useSqlTableAliases) { - $column .= $this->walkIdentificationVariable($dqlAlias, $fieldName) . '.'; + $sql .= $this->walkIdentificationVariable($dqlAlias, $fieldName) . '.'; } - $column .= $class->getQuotedColumnName($fieldName, $this->_platform); - - if ($this->_useDbalTypeValueSql && isset($class->fieldMappings[$fieldName]['requireSQLConversion'])) { - $type = Type::getType($class->getTypeOfField($fieldName)); - $column = $type->convertToPHPValueSQL($column, $this->_conn->getDatabasePlatform()); - } - - $sql .= $column; + $sql .= $class->getQuotedColumnName($fieldName, $this->_platform); break; case AST\PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION: @@ -1429,18 +1408,7 @@ class SqlWalker implements TreeWalker switch (true) { case ($newValue instanceof AST\Node): - $currentColumnTypeBefore = $this->_currentColumnType; - $this->_currentColumnType = null; - - if ($updateItem->pathExpression->type == AST\PathExpression::TYPE_STATE_FIELD) { - $class = $this->_queryComponents[$updateItem->pathExpression->identificationVariable]['metadata']; - if (isset($class->fieldMappings[$updateItem->pathExpression->field]['requireSQLConversion'])) { - $this->_currentColumnType = $class->getTypeOfField($updateItem->pathExpression->field); - } - } - $sql .= $newValue->dispatch($this); - $this->_currentColumnType = $currentColumnTypeBefore; break; case ($newValue === null): @@ -1813,30 +1781,20 @@ class SqlWalker implements TreeWalker { switch ($literal->type) { case AST\Literal::STRING: - $value = $this->_conn->quote($literal->value); - break; + return $this->_conn->quote($literal->value); case AST\Literal::BOOLEAN: $bool = strtolower($literal->value) == 'true' ? true : false; $boolVal = $this->_conn->getDatabasePlatform()->convertBooleans($bool); - $value = $boolVal; - break; + return $boolVal; case AST\Literal::NUMERIC: - $value = $literal->value; - break; + return $literal->value; default: throw QueryException::invalidLiteral($literal); } - - if ($this->_currentColumnType !== null) { - $type = Type::getType($this->_currentColumnType); - $value = $type->convertToDatabaseValueSQL($value, $this->_conn->getDatabasePlatform()); - } - - return $value; } /** diff --git a/tests/Doctrine/Tests/DbalTypes/NegativeToPositiveType.php b/tests/Doctrine/Tests/DbalTypes/NegativeToPositiveType.php index e477ecd3c..ae704f8bd 100644 --- a/tests/Doctrine/Tests/DbalTypes/NegativeToPositiveType.php +++ b/tests/Doctrine/Tests/DbalTypes/NegativeToPositiveType.php @@ -29,6 +29,6 @@ class NegativeToPositiveType extends Type public function convertToPHPValueSQL($sqlExpr, $platform) { - return '((' . $sqlExpr . ') * -1)'; + return '-(' . $sqlExpr . ')'; } } diff --git a/tests/Doctrine/Tests/ORM/Functional/TypeValueSqlTest.php b/tests/Doctrine/Tests/ORM/Functional/TypeValueSqlTest.php index 6102812d7..5a05d76d2 100644 --- a/tests/Doctrine/Tests/ORM/Functional/TypeValueSqlTest.php +++ b/tests/Doctrine/Tests/ORM/Functional/TypeValueSqlTest.php @@ -46,7 +46,7 @@ class TypeValueSqlTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->assertEquals('foo', $entity->lowerCaseString, 'Entity holds lowercase string'); $this->assertEquals('FOO', $this->_em->getConnection()->fetchColumn("select lowerCaseString from customtype_uppercases where id=".$entity->id.""), 'Database holds uppercase string'); } - + public function testTypeValueSqlWithAssociations() { $parent = new CustomTypeParent(); @@ -90,12 +90,13 @@ class TypeValueSqlTest extends \Doctrine\Tests\OrmFunctionalTestCase $this->_em->clear(); - $query = $this->_em->createQuery("SELECT p, p.customInteger, c from Doctrine\Tests\Models\CustomType\CustomTypeParent p JOIN p.child c where p.id = " . $parentId . " AND p.customInteger = -1"); + $query = $this->_em->createQuery("SELECT p, p.customInteger, c from Doctrine\Tests\Models\CustomType\CustomTypeParent p JOIN p.child c where p.id = " . $parentId); $result = $query->getResult(); $this->assertEquals(1, count($result)); $this->assertInstanceOf('Doctrine\Tests\Models\CustomType\CustomTypeParent', $result[0][0]); + $this->assertEquals(-1, $result[0][0]->customInteger); $this->assertEquals(-1, $result[0]['customInteger']); diff --git a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php index 028406068..ceb1a3729 100644 --- a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php +++ b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php @@ -1345,7 +1345,7 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase $this->assertSqlGeneration( 'SELECT p.customInteger FROM Doctrine\Tests\Models\CustomType\CustomTypeParent p WHERE p.id = 1', - 'SELECT ((c0_.customInteger) * -1) AS customInteger0 FROM customtype_parents c0_ WHERE c0_.id = 1' + 'SELECT -(c0_.customInteger) AS customInteger0 FROM customtype_parents c0_ WHERE c0_.id = 1' ); } @@ -1373,7 +1373,21 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase $this->assertSqlGeneration( 'SELECT p FROM Doctrine\Tests\Models\CustomType\CustomTypeParent p', - 'SELECT c0_.id AS id0, ((c0_.customInteger) * -1) AS customInteger1 FROM customtype_parents c0_' + 'SELECT c0_.id AS id0, -(c0_.customInteger) AS customInteger1 FROM customtype_parents c0_' + ); + } + + public function testCustomTypeValueSqlForPartialObject() + { + if (DBALType::hasType('negative_to_positive')) { + DBALType::overrideType('negative_to_positive', 'Doctrine\Tests\DbalTypes\NegativeToPositiveType'); + } else { + DBALType::addType('negative_to_positive', 'Doctrine\Tests\DbalTypes\NegativeToPositiveType'); + } + + $this->assertSqlGeneration( + 'SELECT partial p.{id, customInteger} FROM Doctrine\Tests\Models\CustomType\CustomTypeParent p', + 'SELECT c0_.id AS id0, -(c0_.customInteger) AS customInteger1 FROM customtype_parents c0_' ); } } diff --git a/tests/Doctrine/Tests/ORM/Query/UpdateSqlGenerationTest.php b/tests/Doctrine/Tests/ORM/Query/UpdateSqlGenerationTest.php index 34658c95a..a65efe079 100644 --- a/tests/Doctrine/Tests/ORM/Query/UpdateSqlGenerationTest.php +++ b/tests/Doctrine/Tests/ORM/Query/UpdateSqlGenerationTest.php @@ -195,19 +195,11 @@ class UpdateSqlGenerationTest extends \Doctrine\Tests\OrmTestCase ); } - public function testCustomTypeValueSql() + public function testCustomTypeValueSqlCompletelyIgnoredInUpdateStatements() { $this->assertSqlGeneration( 'UPDATE Doctrine\Tests\Models\CustomType\CustomTypeParent p SET p.customInteger = 1 WHERE p.id = 1', - 'UPDATE customtype_parents SET customInteger = ABS(1) WHERE id = 1' - ); - } - - public function testCustomTypeValueSqlIgnoresIdentifierColumns() - { - $this->assertSqlGeneration( - 'UPDATE Doctrine\Tests\Models\CustomType\CustomTypeParent p SET p.id = 2 WHERE p.id = 1', - 'UPDATE customtype_parents SET id = 2 WHERE id = 1' + 'UPDATE customtype_parents SET customInteger = 1 WHERE id = 1' ); } }