diff --git a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php index 00b00cfa2fa..a0cd444fd34 100644 --- a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php @@ -416,7 +416,7 @@ protected final function updateTable($entity, $quotedTableName, array $updateDat break; } - + $params[] = $value; $set[] = $column . ' = ' . $placeholder; $types[] = $this->columnTypes[$columnName]; @@ -850,7 +850,7 @@ public function refresh(array $id, $entity, $lockMode = 0) $sql = $this->getSelectSQL($id, null, $lockMode); list($params, $types) = $this->expandParameters($id); $stmt = $this->conn->executeQuery($sql, $params, $types); - + $hydrator = $this->em->newHydrator(Query::HYDRATE_OBJECT); $hydrator->hydrateAll($stmt, $this->rsm, array(Query::HINT_REFRESH => true)); } @@ -1130,7 +1130,7 @@ protected function getSelectSQL($criteria, $assoc = null, $lockMode = 0, $limit $tableName = $this->quoteStrategy->getTableName($this->class, $this->platform); if ('' !== $filterSql) { - $conditionSql = $conditionSql + $conditionSql = $conditionSql ? $conditionSql . ' AND ' . $filterSql : $filterSql; } @@ -1283,7 +1283,7 @@ protected function getSelectColumnsSQL() if ($assoc['isOwningSide']) { $tableAlias = $this->getSQLTableAlias($association['targetEntity'], $assocAlias); $this->selectJoinSql .= ' ' . $this->getJoinSQLForJoinColumns($association['joinColumns']); - + foreach ($association['joinColumns'] as $joinColumn) { $sourceCol = $this->quoteStrategy->getJoinColumnName($joinColumn, $this->class, $this->platform); $targetCol = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $this->class, $this->platform); @@ -1415,7 +1415,7 @@ protected function getInsertSQL() foreach ($columns as $column) { $placeholder = '?'; - if (isset($this->class->fieldNames[$column]) + if (isset($this->class->fieldNames[$column]) && isset($this->columnTypes[$this->class->fieldNames[$column]]) && isset($this->class->fieldMappings[$this->class->fieldNames[$column]]['requireSQLConversion'])) { @@ -1488,7 +1488,7 @@ protected function getSelectColumnSQL($field, ClassMetadata $class, $alias = 'r' $columnName = $this->quoteStrategy->getColumnName($field, $class, $this->platform); $sql = $tableAlias . '.' . $columnName; $columnAlias = $this->getSQLColumnAlias($class->columnNames[$field]); - + $this->rsm->addFieldResult($alias, $columnAlias, $field); if (isset($class->fieldMappings[$field]['requireSQLConversion'])) { @@ -1550,7 +1550,7 @@ public function lock(array $criteria, $lockMode) break; } - $lock = $this->platform->appendLockHint($this->getLockTablesSql(), $lockMode); + $lock = $this->getLockTablesSql($lockMode); $where = ($conditionSql ? ' WHERE ' . $conditionSql : '') . ' '; $sql = 'SELECT 1 ' . $lock @@ -1565,13 +1565,18 @@ public function lock(array $criteria, $lockMode) /** * 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 */ - protected function getLockTablesSql() + protected function getLockTablesSql($lockMode) { - return 'FROM ' - . $this->quoteStrategy->getTableName($this->class, $this->platform) . ' ' - . $this->getSQLTableAlias($this->class->name); + return $this->platform->appendLockHint( + 'FROM ' + . $this->quoteStrategy->getTableName($this->class, $this->platform) . ' ' + . $this->getSQLTableAlias($this->class->name), + $lockMode + ); } /** @@ -1910,7 +1915,7 @@ public function exists($entity, array $extraConditions = array()) $alias = $this->getSQLTableAlias($this->class->name); $sql = 'SELECT 1 ' - . $this->getLockTablesSql() + . $this->getLockTablesSql(null) . ' WHERE ' . $this->getSelectConditionSQL($criteria); if ($filterSql = $this->generateFilterConditionSQL($this->class, $alias)) { diff --git a/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php b/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php index 684d30571cc..0fbc5d0fd86 100644 --- a/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php +++ b/lib/Doctrine/ORM/Persisters/JoinedSubclassPersister.php @@ -288,7 +288,7 @@ public function delete($entity) foreach ($this->class->parentClasses as $parentClass) { $parentMetadata = $this->em->getClassMetadata($parentClass); $parentTable = $this->quoteStrategy->getTableName($parentMetadata, $this->platform); - + $this->conn->delete($parentTable, $id); } } @@ -342,7 +342,7 @@ protected function getSelectSQL($criteria, $assoc = null, $lockMode = 0, $limit // 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))) { - $conditionSql .= $conditionSql + $conditionSql .= $conditionSql ? ' AND ' . $filterSql : $filterSql; } @@ -387,16 +387,13 @@ protected function getSelectSQL($criteria, $assoc = null, $lockMode = 0, $limit } /** - * Get the FROM and optionally JOIN conditions to lock the entity managed by this persister. - * - * @return string + * {@inheritdoc} */ - public function getLockTablesSql() + protected function getLockTablesSql($lockMode) { $joinSql = ''; $identifierColumns = $this->class->getIdentifierColumnNames(); $baseTableAlias = $this->getSQLTableAlias($this->class->name); - $quotedTableName = $this->quoteStrategy->getTableName($this->class, $this->platform); // INNER JOIN parent tables foreach ($this->class->parentClasses as $parentClassName) { @@ -412,7 +409,7 @@ public function getLockTablesSql() $joinSql .= implode(' AND ', $conditions); } - return 'FROM ' . $quotedTableName . ' ' . $baseTableAlias . $joinSql; + return parent::getLockTablesSql($lockMode) . $joinSql; } /** @@ -488,8 +485,8 @@ protected function getSelectColumnsSQL() // Add join columns (foreign keys) foreach ($subClass->associationMappings as $mapping) { - if ( ! $mapping['isOwningSide'] - || ! ($mapping['type'] & ClassMetadata::TO_ONE) + if ( ! $mapping['isOwningSide'] + || ! ($mapping['type'] & ClassMetadata::TO_ONE) || isset($mapping['inherited'])) { continue; } @@ -505,17 +502,17 @@ protected function getSelectColumnsSQL() } $this->selectColumnListSql = implode(', ', $columnList); - + return $this->selectColumnListSql; } /** - * {@inheritdoc} + * {@inheritdoc} */ protected function getInsertColumnList() { // Identifier columns must always come first in the column list of subclasses. - $columns = $this->class->parentClasses + $columns = $this->class->parentClasses ? $this->class->getIdentifierColumnNames() : array(); diff --git a/lib/Doctrine/ORM/Query/SqlWalker.php b/lib/Doctrine/ORM/Query/SqlWalker.php index 4fcb20f771e..04f671c3f1b 100644 --- a/lib/Doctrine/ORM/Query/SqlWalker.php +++ b/lib/Doctrine/ORM/Query/SqlWalker.php @@ -456,7 +456,7 @@ private function _generateDiscriminatorColumnConditionSQL(array $dqlAliases) } $sqlParts[] = (($this->useSqlTableAliases) ? $this->getSQLTableAlias($class->getTableName(), $dqlAlias) . '.' : '') - . $class->discriminatorColumn['name'] . ' IN (' . implode(', ', $values) . ')'; + . $class->discriminatorColumn['name'] . ' IN (' . implode(', ', $values) . ')'; } $sql = implode(' AND ', $sqlParts); @@ -496,7 +496,7 @@ private function generateFilterConditionSQL(ClassMetadata $targetEntity, $target default: //@todo: throw exception? return ''; - break; + break; } $filterClauses = array(); @@ -574,7 +574,7 @@ public function walkUpdateStatement(AST\UpdateStatement $AST) $this->useSqlTableAliases = false; return $this->walkUpdateClause($AST->updateClause) - . $this->walkWhereClause($AST->whereClause); + . $this->walkWhereClause($AST->whereClause); } /** @@ -585,7 +585,7 @@ public function walkDeleteStatement(AST\DeleteStatement $AST) $this->useSqlTableAliases = false; return $this->walkDeleteClause($AST->deleteClause) - . $this->walkWhereClause($AST->whereClause); + . $this->walkWhereClause($AST->whereClause); } /** @@ -700,10 +700,10 @@ public function walkSelectClause($selectClause) } $addMetaColumns = ! $this->query->getHint(Query::HINT_FORCE_PARTIAL_LOAD) && - $this->query->getHydrationMode() == Query::HYDRATE_OBJECT - || - $this->query->getHydrationMode() != Query::HYDRATE_OBJECT && - $this->query->getHint(Query::HINT_INCLUDE_META_COLUMNS); + $this->query->getHydrationMode() == Query::HYDRATE_OBJECT + || + $this->query->getHydrationMode() != Query::HYDRATE_OBJECT && + $this->query->getHint(Query::HINT_INCLUDE_META_COLUMNS); foreach ($this->selectedClasses as $selectedClass) { $class = $selectedClass['class']; @@ -801,10 +801,7 @@ public function walkFromClause($fromClause) $sqlParts = array(); foreach ($identificationVarDecls as $identificationVariableDecl) { - $sql = $this->platform->appendLockHint( - $this->walkRangeVariableDeclaration($identificationVariableDecl->rangeVariableDeclaration), - $this->query->getHint(Query::HINT_LOCK_MODE) - ); + $sql = $this->walkRangeVariableDeclaration($identificationVariableDecl->rangeVariableDeclaration); foreach ($identificationVariableDecl->joins as $join) { $sql .= $this->walkJoin($join); @@ -846,8 +843,11 @@ public function walkRangeVariableDeclaration($rangeVariableDeclaration) $this->rootAliases[] = $dqlAlias; } - $sql = $this->quoteStrategy->getTableName($class,$this->platform) . ' ' - . $this->getSQLTableAlias($class->getTableName(), $dqlAlias); + $sql = $this->platform->appendLockHint( + $this->quoteStrategy->getTableName($class, $this->platform) . ' ' . + $this->getSQLTableAlias($class->getTableName(), $dqlAlias), + $this->query->getHint(Query::HINT_LOCK_MODE) + ); if ($class->isInheritanceTypeJoined()) { $sql .= $this->_generateClassTableInheritanceJoins($class, $dqlAlias); @@ -899,7 +899,7 @@ public function walkJoinAssociationDeclaration($joinAssociationDeclaration, $joi case ($assoc['type'] & ClassMetadata::TO_ONE): $conditions = array(); - foreach ($assoc['joinColumns'] as $joinColumn) { + foreach ($assoc['joinColumns'] as $joinColumn) { $quotedSourceColumn = $this->quoteStrategy->getJoinColumnName($joinColumn, $targetClass, $this->platform); $quotedTargetColumn = $this->quoteStrategy->getReferencedJoinColumnName($joinColumn, $targetClass, $this->platform); @@ -1439,10 +1439,7 @@ public function walkSubselectFromClause($subselectFromClause) $sqlParts = array (); foreach ($identificationVarDecls as $subselectIdVarDecl) { - $sql = $this->platform->appendLockHint( - $this->walkRangeVariableDeclaration($subselectIdVarDecl->rangeVariableDeclaration), - $this->query->getHint(Query::HINT_LOCK_MODE) - ); + $sql = $this->walkRangeVariableDeclaration($subselectIdVarDecl->rangeVariableDeclaration); foreach ($subselectIdVarDecl->joins as $join) { $sql .= $this->walkJoin($join); @@ -1460,7 +1457,7 @@ public function walkSubselectFromClause($subselectFromClause) public function walkSimpleSelectClause($simpleSelectClause) { return 'SELECT' . ($simpleSelectClause->isDistinct ? ' DISTINCT' : '') - . $this->walkSimpleSelectExpression($simpleSelectClause->simpleSelectExpression); + . $this->walkSimpleSelectExpression($simpleSelectClause->simpleSelectExpression); } /** @@ -1592,7 +1589,7 @@ public function walkSimpleSelectExpression($simpleSelectExpression) public function walkAggregateExpression($aggExpression) { return $aggExpression->functionName . '(' . ($aggExpression->isDistinct ? 'DISTINCT ' : '') - . $this->walkSimpleArithmeticExpression($aggExpression->pathExpression) . ')'; + . $this->walkSimpleArithmeticExpression($aggExpression->pathExpression) . ')'; } /** @@ -1887,7 +1884,7 @@ public function walkCollectionMemberExpression($collMemberExpr) // join to target table $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 $joinColumns = $assoc['isOwningSide'] ? $joinTable['inverseJoinColumns'] : $joinTable['joinColumns']; @@ -2067,7 +2064,7 @@ public function walkBetweenExpression($betweenExpr) if ($betweenExpr->not) $sql .= ' NOT'; $sql .= ' BETWEEN ' . $this->walkArithmeticExpression($betweenExpr->leftBetweenExpression) - . ' AND ' . $this->walkArithmeticExpression($betweenExpr->rightBetweenExpression); + . ' AND ' . $this->walkArithmeticExpression($betweenExpr->rightBetweenExpression); return $sql; } diff --git a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php index ceceb32891e..11c5e3aecc3 100644 --- a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php +++ b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php @@ -1990,7 +1990,7 @@ public function testSupportsMoreThanTwoParametersInConcatFunction() "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( "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') = ?" @@ -1998,7 +1998,7 @@ public function testSupportsMoreThanTwoParametersInConcatFunction() $this->assertSqlGeneration( "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 = ?" - );*/ + ); $connMock->setDatabasePlatform($orgPlatform); }