diff --git a/lib/Doctrine/ORM/Persisters/AbstractEntityInheritancePersister.php b/lib/Doctrine/ORM/Persisters/AbstractEntityInheritancePersister.php index 670cf11e7..84540a337 100644 --- a/lib/Doctrine/ORM/Persisters/AbstractEntityInheritancePersister.php +++ b/lib/Doctrine/ORM/Persisters/AbstractEntityInheritancePersister.php @@ -62,7 +62,7 @@ abstract class AbstractEntityInheritancePersister extends BasicEntityPersister { $columnName = $class->columnNames[$field]; $sql = $this->_getSQLTableAlias($class->name, $alias == 'r' ? '' : $alias) . '.' . $class->getQuotedColumnName($field, $this->_platform); - $columnAlias = $this->_platform->getSQLResultCasing($columnName . $this->_sqlAliasCounter++); + $columnAlias = $this->getSQLColumnAlias($columnName); $this->_rsm->addFieldResult($alias, $columnAlias, $field, $class->name); return $sql . ' AS ' . $columnAlias; @@ -70,10 +70,9 @@ abstract class AbstractEntityInheritancePersister extends BasicEntityPersister protected function getSelectJoinColumnSQL($tableAlias, $joinColumnName, $className) { - $columnAlias = $joinColumnName . $this->_sqlAliasCounter++; - $resultColumnName = $this->_platform->getSQLResultCasing($columnAlias); - $this->_rsm->addMetaResult('r', $resultColumnName, $joinColumnName); + $columnAlias = $this->getSQLColumnAlias($joinColumnName); + $this->_rsm->addMetaResult('r', $columnAlias, $joinColumnName); return $tableAlias . '.' . $joinColumnName . ' AS ' . $columnAlias; } -} \ No newline at end of file +} diff --git a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php index 0379ccf56..9fa2e3dff 100644 --- a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php @@ -1060,8 +1060,7 @@ class BasicEntityPersister foreach ($assoc['targetToSourceKeyColumns'] as $srcColumn) { if ($columnList) $columnList .= ', '; - $columnAlias = $srcColumn . $this->_sqlAliasCounter++; - $resultColumnName = $this->_platform->getSQLResultCasing($columnAlias); + $resultColumnName = $this->getSQLColumnAlias($srcColumn); $columnList .= $this->_getSQLTableAlias($class->name, ($alias == 'r' ? '' : $alias) ) . '.' . $srcColumn . ' AS ' . $resultColumnName; $this->_rsm->addMetaResult($alias, $resultColumnName, $srcColumn, isset($assoc['id']) && $assoc['id'] === true); @@ -1180,11 +1179,10 @@ class BasicEntityPersister */ protected function _getSelectColumnSQL($field, ClassMetadata $class, $alias = 'r') { - $columnName = $class->columnNames[$field]; $sql = $this->_getSQLTableAlias($class->name, $alias == 'r' ? '' : $alias) . '.' . $class->getQuotedColumnName($field, $this->_platform); - $columnAlias = $this->_platform->getSQLResultCasing($columnName . $this->_sqlAliasCounter++); - + $columnAlias = $this->getSQLColumnAlias($class->columnNames[$field]); + $this->_rsm->addFieldResult($alias, $columnAlias, $field); return $sql . ' AS ' . $columnAlias; @@ -1500,4 +1498,19 @@ class BasicEntityPersister return (bool) $this->_conn->fetchColumn($sql, $params); } + + /** + * Gets an SQL column alias for a column name. + * + * @param string $columnName + * @return string + */ + public function getSQLColumnAlias($columnName) + { + // Trim the column alias to the maximum identifier length of the platform. + // If the alias is to long, characters are cut off from the beginning. + return $this->_platform->getSQLResultCasing( + substr($columnName . $this->_sqlAliasCounter++, -$this->_platform->getMaxIdentifierLength()) + ); + } } diff --git a/lib/Doctrine/ORM/Query/SqlWalker.php b/lib/Doctrine/ORM/Query/SqlWalker.php index 188350197..6dc662493 100644 --- a/lib/Doctrine/ORM/Query/SqlWalker.php +++ b/lib/Doctrine/ORM/Query/SqlWalker.php @@ -229,7 +229,11 @@ class SqlWalker implements TreeWalker */ public function getSQLColumnAlias($columnName) { - return $columnName . $this->_aliasCounter++; + // Trim the column alias to the maximum identifier length of the platform. + // If the alias is to long, characters are cut off from the beginning. + return $this->_platform->getSQLResultCasing( + substr($columnName . $this->_aliasCounter++, -$this->_platform->getMaxIdentifierLength()) + ); } /** @@ -535,7 +539,6 @@ class SqlWalker implements TreeWalker $sqlSelectExpressions[] = $tblAlias . '.' . $discrColumn['name'] . ' AS ' . $columnAlias; - $columnAlias = $this->_platform->getSQLResultCasing($columnAlias); $this->_rsm->setDiscriminatorColumn($dqlAlias, $columnAlias); $this->_rsm->addMetaResult($dqlAlias, $columnAlias, $discrColumn['fieldName']); @@ -556,8 +559,7 @@ class SqlWalker implements TreeWalker $sqlSelectExpressions[] = $sqlTableAlias . '.' . $srcColumn . ' AS ' . $columnAlias; - $columnAlias = $this->_platform->getSQLResultCasing($columnAlias); - $this->_rsm->addMetaResult($dqlAlias, $this->_platform->getSQLResultCasing($columnAlias), $srcColumn, (isset($assoc['id']) && $assoc['id'] === true)); + $this->_rsm->addMetaResult($dqlAlias, $columnAlias, $srcColumn, (isset($assoc['id']) && $assoc['id'] === true)); } } } @@ -574,8 +576,7 @@ class SqlWalker implements TreeWalker $sqlSelectExpressions[] = $sqlTableAlias . '.' . $srcColumn . ' AS ' . $columnAlias; - $columnAlias = $this->_platform->getSQLResultCasing($columnAlias); - $this->_rsm->addMetaResult($dqlAlias, $this->_platform->getSQLResultCasing($columnAlias), $srcColumn, (isset($assoc['id']) && $assoc['id'] === true)); + $this->_rsm->addMetaResult($dqlAlias, $columnAlias, $srcColumn, (isset($assoc['id']) && $assoc['id'] === true)); } } } @@ -1006,7 +1007,6 @@ class SqlWalker implements TreeWalker $columnAlias = $this->getSQLColumnAlias($columnName); $sql .= $sqlTableAlias . '.' . $columnName . ' AS ' . $columnAlias; - $columnAlias = $this->_platform->getSQLResultCasing($columnAlias); if ( ! $hidden) { $this->_rsm->addScalarResult($columnAlias, $resultAlias); @@ -1018,12 +1018,10 @@ class SqlWalker implements TreeWalker $resultAlias = $selectExpression->fieldIdentificationVariable; } - $columnAlias = 'sclr' . $this->_aliasCounter++; + $columnAlias = $this->getSQLColumnAlias('sclr'); $sql .= $this->walkAggregateExpression($expr) . ' AS ' . $columnAlias; $this->_scalarResultAliasMap[$resultAlias] = $columnAlias; - $columnAlias = $this->_platform->getSQLResultCasing($columnAlias); - if ( ! $hidden) { $this->_rsm->addScalarResult($columnAlias, $resultAlias); } @@ -1034,12 +1032,10 @@ class SqlWalker implements TreeWalker $resultAlias = $selectExpression->fieldIdentificationVariable; } - $columnAlias = 'sclr' . $this->_aliasCounter++; + $columnAlias = $this->getSQLColumnAlias('sclr'); $sql .= '(' . $this->walkSubselect($expr) . ') AS '.$columnAlias; $this->_scalarResultAliasMap[$resultAlias] = $columnAlias; - $columnAlias = $this->_platform->getSQLResultCasing($columnAlias); - if ( ! $hidden) { $this->_rsm->addScalarResult($columnAlias, $resultAlias); } @@ -1050,12 +1046,10 @@ class SqlWalker implements TreeWalker $resultAlias = $selectExpression->fieldIdentificationVariable; } - $columnAlias = 'sclr' . $this->_aliasCounter++; + $columnAlias = $this->getSQLColumnAlias('sclr'); $sql .= $this->walkFunction($expr) . ' AS ' . $columnAlias; $this->_scalarResultAliasMap[$resultAlias] = $columnAlias; - $columnAlias = $this->_platform->getSQLResultCasing($columnAlias); - if ( ! $hidden) { $this->_rsm->addScalarResult($columnAlias, $resultAlias); } @@ -1072,18 +1066,15 @@ class SqlWalker implements TreeWalker $resultAlias = $selectExpression->fieldIdentificationVariable; } - $columnAlias = 'sclr' . $this->_aliasCounter++; - + $columnAlias = $this->getSQLColumnAlias('sclr'); if ($expr instanceof AST\Literal) { $sql .= $this->walkLiteral($expr) . ' AS ' .$columnAlias; } else { $sql .= $this->walkSimpleArithmeticExpression($expr) . ' AS ' . $columnAlias; } - + $this->_scalarResultAliasMap[$resultAlias] = $columnAlias; - $columnAlias = $this->_platform->getSQLResultCasing($columnAlias); - if ( ! $hidden) { $this->_rsm->addScalarResult($columnAlias, $resultAlias); } @@ -1099,14 +1090,11 @@ class SqlWalker implements TreeWalker $resultAlias = $selectExpression->fieldIdentificationVariable; } - $columnAlias = 'sclr' . $this->_aliasCounter++; - + $columnAlias = $this->getSQLColumnAlias('sclr'); $sql .= $this->walkCaseExpression($expr) . ' AS ' . $columnAlias; - + $this->_scalarResultAliasMap[$resultAlias] = $columnAlias; - $columnAlias = $this->_platform->getSQLResultCasing($columnAlias); - if ( ! $hidden) { $this->_rsm->addScalarResult($columnAlias, $resultAlias); } @@ -1146,8 +1134,6 @@ class SqlWalker implements TreeWalker $sql .= $sqlTableAlias . '.' . $class->getQuotedColumnName($fieldName, $this->_platform) . ' AS ' . $columnAlias; - $columnAlias = $this->_platform->getSQLResultCasing($columnAlias); - $this->_rsm->addFieldResult($dqlAlias, $columnAlias, $fieldName, $class->name); } @@ -1171,7 +1157,6 @@ class SqlWalker implements TreeWalker $sql .= $sqlTableAlias . '.' . $subClass->getQuotedColumnName($fieldName, $this->_platform) . ' AS ' . $columnAlias; - $columnAlias = $this->_platform->getSQLResultCasing($columnAlias); $this->_rsm->addFieldResult($dqlAlias, $columnAlias, $fieldName, $subClassName); } @@ -1185,7 +1170,7 @@ class SqlWalker implements TreeWalker $columnAlias = $this->getSQLColumnAlias($srcColumn); $sql .= $sqlTableAlias . '.' . $srcColumn . ' AS ' . $columnAlias; - $this->_rsm->addMetaResult($dqlAlias, $this->_platform->getSQLResultCasing($columnAlias), $srcColumn); + $this->_rsm->addMetaResult($dqlAlias, $columnAlias, $srcColumn); } } } @@ -1312,7 +1297,7 @@ class SqlWalker implements TreeWalker $alias = $simpleSelectExpression->fieldIdentificationVariable; } - $columnAlias = 'sclr' . $this->_aliasCounter++; + $columnAlias = $this->getSQLColumnAlias('sclr'); $sql .= '(' . $this->walkSubselect($expr) . ') AS ' . $columnAlias; $this->_scalarResultAliasMap[$alias] = $columnAlias; } else if ($expr instanceof AST\Functions\FunctionNode) { @@ -1322,7 +1307,7 @@ class SqlWalker implements TreeWalker $alias = $simpleSelectExpression->fieldIdentificationVariable; } - $columnAlias = 'sclr' . $this->_aliasCounter++; + $columnAlias = $this->getSQLColumnAlias('sclr'); $sql .= $this->walkFunction($expr) . ' AS ' . $columnAlias; $this->_scalarResultAliasMap[$alias] = $columnAlias; } else if ( @@ -1337,7 +1322,7 @@ class SqlWalker implements TreeWalker $alias = $simpleSelectExpression->fieldIdentificationVariable; } - $columnAlias = 'sclr' . $this->_aliasCounter++; + $columnAlias = $this->getSQLColumnAlias('sclr'); $sql .= $this->walkSimpleArithmeticExpression($expr) . ' AS ' . $columnAlias; $this->_scalarResultAliasMap[$alias] = $columnAlias; } else if ( @@ -1352,7 +1337,7 @@ class SqlWalker implements TreeWalker $alias = $simpleSelectExpression->fieldIdentificationVariable; } - $columnAlias = 'sclr' . $this->_aliasCounter++; + $columnAlias = $this->getSQLColumnAlias('sclr'); $sql .= $this->walkCaseExpression($expr) . ' AS ' . $columnAlias; $this->_scalarResultAliasMap[$alias] = $columnAlias; diff --git a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php index 53002a8fc..e3ea9c004 100644 --- a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php +++ b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php @@ -729,12 +729,12 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase $this->assertSqlGeneration( "SELECT b FROM Doctrine\Tests\Models\Generic\BooleanModel b WHERE b.booleanField = true", - "SELECT b0_.id AS id0, b0_.booleanField AS booleanField1 FROM boolean_model b0_ WHERE b0_.booleanField = true" + "SELECT b0_.id AS id0, b0_.booleanField AS booleanfield1 FROM boolean_model b0_ WHERE b0_.booleanField = true" ); $this->assertSqlGeneration( "SELECT b FROM Doctrine\Tests\Models\Generic\BooleanModel b WHERE b.booleanField = false", - "SELECT b0_.id AS id0, b0_.booleanField AS booleanField1 FROM boolean_model b0_ WHERE b0_.booleanField = false" + "SELECT b0_.id AS id0, b0_.booleanField AS booleanfield1 FROM boolean_model b0_ WHERE b0_.booleanField = false" ); $this->_em->getConnection()->setDatabasePlatform($oldPlat); @@ -877,7 +877,7 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase $this->assertSqlGeneration( "SELECT u FROM Doctrine\Tests\Models\CMS\CmsUser u WHERE u.username = 'gblanco'", - "SELECT c0_.id AS id0, c0_.status AS status1, c0_.username AS username2, c0_.name AS name3 ". + "SELECT c0_.id AS ID0, c0_.status AS STATUS1, c0_.username AS USERNAME2, c0_.name AS NAME3 ". "FROM cms_users c0_ WHERE c0_.username = 'gblanco' FOR UPDATE", array(Query::HINT_LOCK_MODE => \Doctrine\DBAL\LockMode::PESSIMISTIC_READ) ); @@ -1251,6 +1251,17 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase array(Query::HINT_FORCE_PARTIAL_LOAD => false) ); } + + /** + * @group DDC-1384 + */ + function testAliasDoesNotExceedPlatformDefinedLength() + { + $this->assertSqlGeneration( + 'SELECT m FROM ' . __NAMESPACE__ . '\\DDC1384Model m', + "SELECT d0_.aVeryLongIdentifierThatShouldBeShortenedByTheSQLWalker_fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo AS fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo0 FROM DDC1384Model d0_" + ); + } } @@ -1281,3 +1292,15 @@ class MyAbsFunction extends \Doctrine\ORM\Query\AST\Functions\FunctionNode $parser->match(\Doctrine\ORM\Query\Lexer::T_CLOSE_PARENTHESIS); } } +/** + * @Entity + */ +class DDC1384Model +{ + /** + * @Id + * @Column(type="integer") + * @GeneratedValue + */ + protected $aVeryLongIdentifierThatShouldBeShortenedByTheSQLWalker_fooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo; +}