1
0
mirror of synced 2025-02-02 13:31:45 +03:00

Merge pull request #925 from deeky666/DDC-2310-2.4

[DDC-2310] [DDC-2675] [2.4] Fix SQL generation on table lock hint capable platforms
This commit is contained in:
Marco Pivetta 2014-11-12 00:31:13 +01:00
commit cfdef3bf19
4 changed files with 49 additions and 50 deletions

View File

@ -416,7 +416,7 @@ class BasicEntityPersister
break; break;
} }
$params[] = $value; $params[] = $value;
$set[] = $column . ' = ' . $placeholder; $set[] = $column . ' = ' . $placeholder;
$types[] = $this->columnTypes[$columnName]; $types[] = $this->columnTypes[$columnName];
@ -850,7 +850,7 @@ class BasicEntityPersister
$sql = $this->getSelectSQL($id, null, $lockMode); $sql = $this->getSelectSQL($id, null, $lockMode);
list($params, $types) = $this->expandParameters($id); list($params, $types) = $this->expandParameters($id);
$stmt = $this->conn->executeQuery($sql, $params, $types); $stmt = $this->conn->executeQuery($sql, $params, $types);
$hydrator = $this->em->newHydrator(Query::HYDRATE_OBJECT); $hydrator = $this->em->newHydrator(Query::HYDRATE_OBJECT);
$hydrator->hydrateAll($stmt, $this->rsm, array(Query::HINT_REFRESH => true)); $hydrator->hydrateAll($stmt, $this->rsm, array(Query::HINT_REFRESH => true));
} }
@ -1130,7 +1130,7 @@ class BasicEntityPersister
$tableName = $this->quoteStrategy->getTableName($this->class, $this->platform); $tableName = $this->quoteStrategy->getTableName($this->class, $this->platform);
if ('' !== $filterSql) { if ('' !== $filterSql) {
$conditionSql = $conditionSql $conditionSql = $conditionSql
? $conditionSql . ' AND ' . $filterSql ? $conditionSql . ' AND ' . $filterSql
: $filterSql; : $filterSql;
} }
@ -1283,7 +1283,7 @@ class BasicEntityPersister
if ($assoc['isOwningSide']) { if ($assoc['isOwningSide']) {
$tableAlias = $this->getSQLTableAlias($association['targetEntity'], $assocAlias); $tableAlias = $this->getSQLTableAlias($association['targetEntity'], $assocAlias);
$this->selectJoinSql .= ' ' . $this->getJoinSQLForJoinColumns($association['joinColumns']); $this->selectJoinSql .= ' ' . $this->getJoinSQLForJoinColumns($association['joinColumns']);
foreach ($association['joinColumns'] as $joinColumn) { foreach ($association['joinColumns'] as $joinColumn) {
$sourceCol = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->class, $this->platform); $sourceCol = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->class, $this->platform);
$targetCol = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $this->class, $this->platform); $targetCol = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $this->class, $this->platform);
@ -1415,7 +1415,7 @@ class BasicEntityPersister
foreach ($columns as $column) { foreach ($columns as $column) {
$placeholder = '?'; $placeholder = '?';
if (isset($this->class->fieldNames[$column]) if (isset($this->class->fieldNames[$column])
&& isset($this->columnTypes[$this->class->fieldNames[$column]]) && isset($this->columnTypes[$this->class->fieldNames[$column]])
&& isset($this->class->fieldMappings[$this->class->fieldNames[$column]]['requireSQLConversion'])) { && isset($this->class->fieldMappings[$this->class->fieldNames[$column]]['requireSQLConversion'])) {
@ -1488,7 +1488,7 @@ class BasicEntityPersister
$columnName = $this->quoteStrategy->getColumnName($field, $class, $this->platform); $columnName = $this->quoteStrategy->getColumnName($field, $class, $this->platform);
$sql = $tableAlias . '.' . $columnName; $sql = $tableAlias . '.' . $columnName;
$columnAlias = $this->getSQLColumnAlias($class->columnNames[$field]); $columnAlias = $this->getSQLColumnAlias($class->columnNames[$field]);
$this->rsm->addFieldResult($alias, $columnAlias, $field); $this->rsm->addFieldResult($alias, $columnAlias, $field);
if (isset($class->fieldMappings[$field]['requireSQLConversion'])) { if (isset($class->fieldMappings[$field]['requireSQLConversion'])) {
@ -1550,7 +1550,7 @@ class BasicEntityPersister
break; break;
} }
$lock = $this->platform->appendLockHint($this->getLockTablesSql(), $lockMode); $lock = $this->getLockTablesSql($lockMode);
$where = ($conditionSql ? ' WHERE ' . $conditionSql : '') . ' '; $where = ($conditionSql ? ' WHERE ' . $conditionSql : '') . ' ';
$sql = 'SELECT 1 ' $sql = 'SELECT 1 '
. $lock . $lock
@ -1565,13 +1565,18 @@ class BasicEntityPersister
/** /**
* Gets the FROM and optionally JOIN conditions to lock the entity managed by this persister. * Gets the FROM and optionally JOIN conditions to lock the entity managed by this persister.
* *
* @param integer $lockMode One of the Doctrine\DBAL\LockMode::* constants.
*
* @return string * @return string
*/ */
protected function getLockTablesSql() protected function getLockTablesSql($lockMode)
{ {
return 'FROM ' return $this->platform->appendLockHint(
. $this->quoteStrategy->getTableName($this->class, $this->platform) . ' ' 'FROM '
. $this->getSQLTableAlias($this->class->name); . $this->quoteStrategy->getTableName($this->class, $this->platform) . ' '
. $this->getSQLTableAlias($this->class->name),
$lockMode
);
} }
/** /**
@ -1910,7 +1915,7 @@ class BasicEntityPersister
$alias = $this->getSQLTableAlias($this->class->name); $alias = $this->getSQLTableAlias($this->class->name);
$sql = 'SELECT 1 ' $sql = 'SELECT 1 '
. $this->getLockTablesSql() . $this->getLockTablesSql(null)
. ' WHERE ' . $this->getSelectConditionSQL($criteria); . ' WHERE ' . $this->getSelectConditionSQL($criteria);
if ($filterSql = $this->generateFilterConditionSQL($this->class, $alias)) { if ($filterSql = $this->generateFilterConditionSQL($this->class, $alias)) {

View File

@ -288,7 +288,7 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister
foreach ($this->class->parentClasses as $parentClass) { foreach ($this->class->parentClasses as $parentClass) {
$parentMetadata = $this->em->getClassMetadata($parentClass); $parentMetadata = $this->em->getClassMetadata($parentClass);
$parentTable = $this->quoteStrategy->getTableName($parentMetadata, $this->platform); $parentTable = $this->quoteStrategy->getTableName($parentMetadata, $this->platform);
$this->conn->delete($parentTable, $id); $this->conn->delete($parentTable, $id);
} }
} }
@ -342,7 +342,7 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister
// If the current class in the root entity, add the filters // If the current class in the root entity, add the filters
if ($filterSql = $this->generateFilterConditionSQL($this->em->getClassMetadata($this->class->rootEntityName), $this->getSQLTableAlias($this->class->rootEntityName))) { if ($filterSql = $this->generateFilterConditionSQL($this->em->getClassMetadata($this->class->rootEntityName), $this->getSQLTableAlias($this->class->rootEntityName))) {
$conditionSql .= $conditionSql $conditionSql .= $conditionSql
? ' AND ' . $filterSql ? ' AND ' . $filterSql
: $filterSql; : $filterSql;
} }
@ -387,16 +387,13 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister
} }
/** /**
* Get the FROM and optionally JOIN conditions to lock the entity managed by this persister. * {@inheritdoc}
*
* @return string
*/ */
public function getLockTablesSql() protected function getLockTablesSql($lockMode)
{ {
$joinSql = ''; $joinSql = '';
$identifierColumns = $this->class->getIdentifierColumnNames(); $identifierColumns = $this->class->getIdentifierColumnNames();
$baseTableAlias = $this->getSQLTableAlias($this->class->name); $baseTableAlias = $this->getSQLTableAlias($this->class->name);
$quotedTableName = $this->quoteStrategy->getTableName($this->class, $this->platform);
// INNER JOIN parent tables // INNER JOIN parent tables
foreach ($this->class->parentClasses as $parentClassName) { foreach ($this->class->parentClasses as $parentClassName) {
@ -412,7 +409,7 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister
$joinSql .= implode(' AND ', $conditions); $joinSql .= implode(' AND ', $conditions);
} }
return 'FROM ' . $quotedTableName . ' ' . $baseTableAlias . $joinSql; return parent::getLockTablesSql($lockMode) . $joinSql;
} }
/** /**
@ -488,8 +485,8 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister
// Add join columns (foreign keys) // Add join columns (foreign keys)
foreach ($subClass->associationMappings as $mapping) { foreach ($subClass->associationMappings as $mapping) {
if ( ! $mapping['isOwningSide'] if ( ! $mapping['isOwningSide']
|| ! ($mapping['type'] & ClassMetadata::TO_ONE) || ! ($mapping['type'] & ClassMetadata::TO_ONE)
|| isset($mapping['inherited'])) { || isset($mapping['inherited'])) {
continue; continue;
} }
@ -505,17 +502,17 @@ class JoinedSubclassPersister extends AbstractEntityInheritancePersister
} }
$this->selectColumnListSql = implode(', ', $columnList); $this->selectColumnListSql = implode(', ', $columnList);
return $this->selectColumnListSql; return $this->selectColumnListSql;
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function getInsertColumnList() protected function getInsertColumnList()
{ {
// Identifier columns must always come first in the column list of subclasses. // Identifier columns must always come first in the column list of subclasses.
$columns = $this->class->parentClasses $columns = $this->class->parentClasses
? $this->class->getIdentifierColumnNames() ? $this->class->getIdentifierColumnNames()
: array(); : array();

View File

@ -456,7 +456,7 @@ class SqlWalker implements TreeWalker
} }
$sqlParts[] = (($this->useSqlTableAliases) ? $this->getSQLTableAlias($class->getTableName(), $dqlAlias) . '.' : '') $sqlParts[] = (($this->useSqlTableAliases) ? $this->getSQLTableAlias($class->getTableName(), $dqlAlias) . '.' : '')
. $class->discriminatorColumn['name'] . ' IN (' . implode(', ', $values) . ')'; . $class->discriminatorColumn['name'] . ' IN (' . implode(', ', $values) . ')';
} }
$sql = implode(' AND ', $sqlParts); $sql = implode(' AND ', $sqlParts);
@ -496,7 +496,7 @@ class SqlWalker implements TreeWalker
default: default:
//@todo: throw exception? //@todo: throw exception?
return ''; return '';
break; break;
} }
$filterClauses = array(); $filterClauses = array();
@ -574,7 +574,7 @@ class SqlWalker implements TreeWalker
$this->useSqlTableAliases = false; $this->useSqlTableAliases = false;
return $this->walkUpdateClause($AST->updateClause) return $this->walkUpdateClause($AST->updateClause)
. $this->walkWhereClause($AST->whereClause); . $this->walkWhereClause($AST->whereClause);
} }
/** /**
@ -585,7 +585,7 @@ class SqlWalker implements TreeWalker
$this->useSqlTableAliases = false; $this->useSqlTableAliases = false;
return $this->walkDeleteClause($AST->deleteClause) return $this->walkDeleteClause($AST->deleteClause)
. $this->walkWhereClause($AST->whereClause); . $this->walkWhereClause($AST->whereClause);
} }
/** /**
@ -700,10 +700,10 @@ class SqlWalker implements TreeWalker
} }
$addMetaColumns = ! $this->query->getHint(Query::HINT_FORCE_PARTIAL_LOAD) && $addMetaColumns = ! $this->query->getHint(Query::HINT_FORCE_PARTIAL_LOAD) &&
$this->query->getHydrationMode() == Query::HYDRATE_OBJECT $this->query->getHydrationMode() == Query::HYDRATE_OBJECT
|| ||
$this->query->getHydrationMode() != Query::HYDRATE_OBJECT && $this->query->getHydrationMode() != Query::HYDRATE_OBJECT &&
$this->query->getHint(Query::HINT_INCLUDE_META_COLUMNS); $this->query->getHint(Query::HINT_INCLUDE_META_COLUMNS);
foreach ($this->selectedClasses as $selectedClass) { foreach ($this->selectedClasses as $selectedClass) {
$class = $selectedClass['class']; $class = $selectedClass['class'];
@ -801,10 +801,7 @@ class SqlWalker implements TreeWalker
$sqlParts = array(); $sqlParts = array();
foreach ($identificationVarDecls as $identificationVariableDecl) { foreach ($identificationVarDecls as $identificationVariableDecl) {
$sql = $this->platform->appendLockHint( $sql = $this->walkRangeVariableDeclaration($identificationVariableDecl->rangeVariableDeclaration);
$this->walkRangeVariableDeclaration($identificationVariableDecl->rangeVariableDeclaration),
$this->query->getHint(Query::HINT_LOCK_MODE)
);
foreach ($identificationVariableDecl->joins as $join) { foreach ($identificationVariableDecl->joins as $join) {
$sql .= $this->walkJoin($join); $sql .= $this->walkJoin($join);
@ -846,8 +843,11 @@ class SqlWalker implements TreeWalker
$this->rootAliases[] = $dqlAlias; $this->rootAliases[] = $dqlAlias;
} }
$sql = $this->quoteStrategy->getTableName($class,$this->platform) . ' ' $sql = $this->platform->appendLockHint(
. $this->getSQLTableAlias($class->getTableName(), $dqlAlias); $this->quoteStrategy->getTableName($class, $this->platform) . ' ' .
$this->getSQLTableAlias($class->getTableName(), $dqlAlias),
$this->query->getHint(Query::HINT_LOCK_MODE)
);
if ($class->isInheritanceTypeJoined()) { if ($class->isInheritanceTypeJoined()) {
$sql .= $this->_generateClassTableInheritanceJoins($class, $dqlAlias); $sql .= $this->_generateClassTableInheritanceJoins($class, $dqlAlias);
@ -899,7 +899,7 @@ class SqlWalker implements TreeWalker
case ($assoc['type'] & ClassMetadata::TO_ONE): case ($assoc['type'] & ClassMetadata::TO_ONE):
$conditions = array(); $conditions = array();
foreach ($assoc['joinColumns'] as $joinColumn) { foreach ($assoc['joinColumns'] as $joinColumn) {
$quotedSourceColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $targetClass, $this->platform); $quotedSourceColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $targetClass, $this->platform);
$quotedTargetColumn = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $targetClass, $this->platform); $quotedTargetColumn = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $targetClass, $this->platform);
@ -1439,10 +1439,7 @@ class SqlWalker implements TreeWalker
$sqlParts = array (); $sqlParts = array ();
foreach ($identificationVarDecls as $subselectIdVarDecl) { foreach ($identificationVarDecls as $subselectIdVarDecl) {
$sql = $this->platform->appendLockHint( $sql = $this->walkRangeVariableDeclaration($subselectIdVarDecl->rangeVariableDeclaration);
$this->walkRangeVariableDeclaration($subselectIdVarDecl->rangeVariableDeclaration),
$this->query->getHint(Query::HINT_LOCK_MODE)
);
foreach ($subselectIdVarDecl->joins as $join) { foreach ($subselectIdVarDecl->joins as $join) {
$sql .= $this->walkJoin($join); $sql .= $this->walkJoin($join);
@ -1460,7 +1457,7 @@ class SqlWalker implements TreeWalker
public function walkSimpleSelectClause($simpleSelectClause) public function walkSimpleSelectClause($simpleSelectClause)
{ {
return 'SELECT' . ($simpleSelectClause->isDistinct ? ' DISTINCT' : '') return 'SELECT' . ($simpleSelectClause->isDistinct ? ' DISTINCT' : '')
. $this->walkSimpleSelectExpression($simpleSelectClause->simpleSelectExpression); . $this->walkSimpleSelectExpression($simpleSelectClause->simpleSelectExpression);
} }
/** /**
@ -1592,7 +1589,7 @@ class SqlWalker implements TreeWalker
public function walkAggregateExpression($aggExpression) public function walkAggregateExpression($aggExpression)
{ {
return $aggExpression->functionName . '(' . ($aggExpression->isDistinct ? 'DISTINCT ' : '') return $aggExpression->functionName . '(' . ($aggExpression->isDistinct ? 'DISTINCT ' : '')
. $this->walkSimpleArithmeticExpression($aggExpression->pathExpression) . ')'; . $this->walkSimpleArithmeticExpression($aggExpression->pathExpression) . ')';
} }
/** /**
@ -1887,7 +1884,7 @@ class SqlWalker implements TreeWalker
// join to target table // join to target table
$sql .= $this->quoteStrategy->getJoinTableName($owningAssoc, $targetClass, $this->platform) . ' ' . $joinTableAlias $sql .= $this->quoteStrategy->getJoinTableName($owningAssoc, $targetClass, $this->platform) . ' ' . $joinTableAlias
. ' INNER JOIN ' . $this->quoteStrategy->getTableName($targetClass, $this->platform) . ' ' . $targetTableAlias . ' ON '; . ' INNER JOIN ' . $this->quoteStrategy->getTableName($targetClass, $this->platform) . ' ' . $targetTableAlias . ' ON ';
// join conditions // join conditions
$joinColumns = $assoc['isOwningSide'] ? $joinTable['inverseJoinColumns'] : $joinTable['joinColumns']; $joinColumns = $assoc['isOwningSide'] ? $joinTable['inverseJoinColumns'] : $joinTable['joinColumns'];
@ -2067,7 +2064,7 @@ class SqlWalker implements TreeWalker
if ($betweenExpr->not) $sql .= ' NOT'; if ($betweenExpr->not) $sql .= ' NOT';
$sql .= ' BETWEEN ' . $this->walkArithmeticExpression($betweenExpr->leftBetweenExpression) $sql .= ' BETWEEN ' . $this->walkArithmeticExpression($betweenExpr->leftBetweenExpression)
. ' AND ' . $this->walkArithmeticExpression($betweenExpr->rightBetweenExpression); . ' AND ' . $this->walkArithmeticExpression($betweenExpr->rightBetweenExpression);
return $sql; return $sql;
} }

View File

@ -1990,7 +1990,7 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase
"SELECT c0_.id || c0_.name || c0_.status AS sclr0 FROM cms_users c0_ WHERE c0_.id = ?" "SELECT c0_.id || c0_.name || c0_.status AS sclr0 FROM cms_users c0_ WHERE c0_.id = ?"
); );
/*$connMock->setDatabasePlatform(new \Doctrine\DBAL\Platforms\SQLServerPlatform()); $connMock->setDatabasePlatform(new \Doctrine\DBAL\Platforms\SQLServerPlatform());
$this->assertSqlGeneration( $this->assertSqlGeneration(
"SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE CONCAT(u.name, u.status, 's') = ?1", "SELECT u.id FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE CONCAT(u.name, u.status, 's') = ?1",
"SELECT c0_.id AS id0 FROM cms_users c0_ WHERE (c0_.name + c0_.status + 's') = ?" "SELECT c0_.id AS id0 FROM cms_users c0_ WHERE (c0_.name + c0_.status + 's') = ?"
@ -1998,7 +1998,7 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase
$this->assertSqlGeneration( $this->assertSqlGeneration(
"SELECT CONCAT(u.id, u.name, u.status) FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1", "SELECT CONCAT(u.id, u.name, u.status) FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.id = ?1",
"SELECT (c0_.id + c0_.name + c0_.status) AS sclr0 FROM cms_users c0_ WHERE c0_.id = ?" "SELECT (c0_.id + c0_.name + c0_.status) AS sclr0 FROM cms_users c0_ WHERE c0_.id = ?"
);*/ );
$connMock->setDatabasePlatform($orgPlatform); $connMock->setDatabasePlatform($orgPlatform);
} }