1
0
mirror of synced 2025-02-06 15:29:26 +03:00

Remove sql conversion from where clauses and update statements

This commit is contained in:
jsor 2011-11-21 15:08:36 +01:00
parent 4042bc53ce
commit 16aa558292
5 changed files with 27 additions and 62 deletions

View File

@ -98,17 +98,6 @@ class SqlWalker implements TreeWalker
*/ */
private $_useSqlTableAliases = true; 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. * The database platform abstraction.
* *
@ -421,7 +410,6 @@ class SqlWalker implements TreeWalker
public function walkUpdateStatement(AST\UpdateStatement $AST) public function walkUpdateStatement(AST\UpdateStatement $AST)
{ {
$this->_useSqlTableAliases = false; $this->_useSqlTableAliases = false;
$this->_useDbalTypeValueSql = false;
return $this->walkUpdateClause($AST->updateClause) . $this->walkWhereClause($AST->whereClause); return $this->walkUpdateClause($AST->updateClause) . $this->walkWhereClause($AST->whereClause);
} }
@ -477,20 +465,11 @@ class SqlWalker implements TreeWalker
$dqlAlias = $pathExpr->identificationVariable; $dqlAlias = $pathExpr->identificationVariable;
$class = $this->_queryComponents[$dqlAlias]['metadata']; $class = $this->_queryComponents[$dqlAlias]['metadata'];
$column = '';
if ($this->_useSqlTableAliases) { if ($this->_useSqlTableAliases) {
$column .= $this->walkIdentificationVariable($dqlAlias, $fieldName) . '.'; $sql .= $this->walkIdentificationVariable($dqlAlias, $fieldName) . '.';
} }
$column .= $class->getQuotedColumnName($fieldName, $this->_platform); $sql .= $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;
break; break;
case AST\PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION: case AST\PathExpression::TYPE_SINGLE_VALUED_ASSOCIATION:
@ -1429,18 +1408,7 @@ class SqlWalker implements TreeWalker
switch (true) { switch (true) {
case ($newValue instanceof AST\Node): 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); $sql .= $newValue->dispatch($this);
$this->_currentColumnType = $currentColumnTypeBefore;
break; break;
case ($newValue === null): case ($newValue === null):
@ -1813,30 +1781,20 @@ class SqlWalker implements TreeWalker
{ {
switch ($literal->type) { switch ($literal->type) {
case AST\Literal::STRING: case AST\Literal::STRING:
$value = $this->_conn->quote($literal->value); return $this->_conn->quote($literal->value);
break;
case AST\Literal::BOOLEAN: case AST\Literal::BOOLEAN:
$bool = strtolower($literal->value) == 'true' ? true : false; $bool = strtolower($literal->value) == 'true' ? true : false;
$boolVal = $this->_conn->getDatabasePlatform()->convertBooleans($bool); $boolVal = $this->_conn->getDatabasePlatform()->convertBooleans($bool);
$value = $boolVal; return $boolVal;
break;
case AST\Literal::NUMERIC: case AST\Literal::NUMERIC:
$value = $literal->value; return $literal->value;
break;
default: default:
throw QueryException::invalidLiteral($literal); throw QueryException::invalidLiteral($literal);
} }
if ($this->_currentColumnType !== null) {
$type = Type::getType($this->_currentColumnType);
$value = $type->convertToDatabaseValueSQL($value, $this->_conn->getDatabasePlatform());
}
return $value;
} }
/** /**

View File

@ -29,6 +29,6 @@ class NegativeToPositiveType extends Type
public function convertToPHPValueSQL($sqlExpr, $platform) public function convertToPHPValueSQL($sqlExpr, $platform)
{ {
return '((' . $sqlExpr . ') * -1)'; return '-(' . $sqlExpr . ')';
} }
} }

View File

@ -90,12 +90,13 @@ class TypeValueSqlTest extends \Doctrine\Tests\OrmFunctionalTestCase
$this->_em->clear(); $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(); $result = $query->getResult();
$this->assertEquals(1, count($result)); $this->assertEquals(1, count($result));
$this->assertInstanceOf('Doctrine\Tests\Models\CustomType\CustomTypeParent', $result[0][0]); $this->assertInstanceOf('Doctrine\Tests\Models\CustomType\CustomTypeParent', $result[0][0]);
$this->assertEquals(-1, $result[0][0]->customInteger);
$this->assertEquals(-1, $result[0]['customInteger']); $this->assertEquals(-1, $result[0]['customInteger']);

View File

@ -1345,7 +1345,7 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase
$this->assertSqlGeneration( $this->assertSqlGeneration(
'SELECT p.customInteger FROM Doctrine\Tests\Models\CustomType\CustomTypeParent p WHERE p.id = 1', '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( $this->assertSqlGeneration(
'SELECT p FROM Doctrine\Tests\Models\CustomType\CustomTypeParent p', '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_'
); );
} }
} }

View File

@ -195,19 +195,11 @@ class UpdateSqlGenerationTest extends \Doctrine\Tests\OrmTestCase
); );
} }
public function testCustomTypeValueSql() public function testCustomTypeValueSqlCompletelyIgnoredInUpdateStatements()
{ {
$this->assertSqlGeneration( $this->assertSqlGeneration(
'UPDATE Doctrine\Tests\Models\CustomType\CustomTypeParent p SET p.customInteger = 1 WHERE p.id = 1', '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' 'UPDATE customtype_parents SET customInteger = 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'
); );
} }
} }