From d49a968d554e5ec464305b704b6be62db8ecd9fb Mon Sep 17 00:00:00 2001 From: "Fabio B. Silva" Date: Thu, 31 May 2012 18:37:51 -0300 Subject: [PATCH] tests for DDC-1719 --- lib/Doctrine/ORM/EntityManager.php | 11 ++-- .../ORM/Mapping/DefaultQuoteStrategy.php | 38 +++--------- lib/Doctrine/ORM/Mapping/QuoteStrategy.php | 59 +++++++++++++++++++ .../ORM/Persisters/BasicEntityPersister.php | 22 ++++--- lib/Doctrine/ORM/Query/SqlWalker.php | 6 +- .../BasicEntityPersisterTypeValueSqlTest.php | 12 ++++ .../ORM/Query/SelectSqlGenerationTest.php | 21 +++++++ 7 files changed, 120 insertions(+), 49 deletions(-) diff --git a/lib/Doctrine/ORM/EntityManager.php b/lib/Doctrine/ORM/EntityManager.php index 28ddd5695..de81cdc2f 100644 --- a/lib/Doctrine/ORM/EntityManager.php +++ b/lib/Doctrine/ORM/EntityManager.php @@ -139,11 +139,9 @@ class EntityManager implements ObjectManager $this->config = $config; $this->eventManager = $eventManager; - $quoteStrategyClassName = $config->getQuoteStrategyClassName(); - $metadataFactoryClassName = $config->getClassMetadataFactoryName(); + $metadataFactoryClassName = $config->getClassMetadataFactoryName(); - $this->quoteStrategy = new $quoteStrategyClassName($conn->getDatabasePlatform()); - $this->metadataFactory = new $metadataFactoryClassName; + $this->metadataFactory = new $metadataFactoryClassName; $this->metadataFactory->setEntityManager($this); $this->metadataFactory->setCacheDriver($this->config->getMetadataCacheImpl()); @@ -183,6 +181,11 @@ class EntityManager implements ObjectManager */ public function getQuoteStrategy() { + if ($this->quoteStrategy === null) { + $className = $this->getConfiguration()->getQuoteStrategyClassName(); + $this->quoteStrategy = new $className($this->getConnection()->getDatabasePlatform()); + } + return $this->quoteStrategy; } diff --git a/lib/Doctrine/ORM/Mapping/DefaultQuoteStrategy.php b/lib/Doctrine/ORM/Mapping/DefaultQuoteStrategy.php index a2a49f1b4..07b74f0a5 100644 --- a/lib/Doctrine/ORM/Mapping/DefaultQuoteStrategy.php +++ b/lib/Doctrine/ORM/Mapping/DefaultQuoteStrategy.php @@ -33,10 +33,7 @@ class DefaultQuoteStrategy extends QuoteStrategy { /** - * Checks if the given identifier is quoted - * - * @param string $identifier - * @return string + * {@inheritdoc} */ public function isQuotedIdentifier($identifier) { @@ -44,10 +41,7 @@ class DefaultQuoteStrategy extends QuoteStrategy } /** - * Gets the uquoted column name. - * - * @param string $identifier - * @return string + * {@inheritdoc} */ public function getUnquotedIdentifier($identifier) { @@ -55,11 +49,7 @@ class DefaultQuoteStrategy extends QuoteStrategy } /** - * Gets the (possibly quoted) column name for safe use in an SQL statement. - * - * @param string $fieldName - * @param ClassMetadata $class - * @return string + * {@inheritdoc} */ public function getColumnName($fieldName, ClassMetadata $class) { @@ -69,10 +59,7 @@ class DefaultQuoteStrategy extends QuoteStrategy } /** - * Gets the (possibly quoted) primary table name for safe use in an SQL statement. - * - * @param ClassMetadata $class - * @return string + * {@inheritdoc} */ public function getTableName(ClassMetadata $class) { @@ -82,10 +69,7 @@ class DefaultQuoteStrategy extends QuoteStrategy } /** - * Gets the (possibly quoted) name of the join table. - * - * @param ClassMetadata $class - * @return string + * {@inheritdoc} */ public function getJoinTableName($relation, ClassMetadata $class) { @@ -96,10 +80,7 @@ class DefaultQuoteStrategy extends QuoteStrategy } /** - * Gets the (possibly quoted) identifier column names for safe use in an SQL statement. - * - * @param ClassMetadata $class - * @return array + * {@inheritdoc} */ public function getIdentifierColumnNames(ClassMetadata $class) { @@ -131,12 +112,7 @@ class DefaultQuoteStrategy extends QuoteStrategy } /** - * Gets the column alias. - * - * @param string $columnName - * @param integer $counter - * @param ClassMetadata $class - * @return string + * {@inheritdoc} */ public function getColumnAlias($columnName, $counter, ClassMetadata $class = null) { diff --git a/lib/Doctrine/ORM/Mapping/QuoteStrategy.php b/lib/Doctrine/ORM/Mapping/QuoteStrategy.php index 366efb877..ee7c24819 100644 --- a/lib/Doctrine/ORM/Mapping/QuoteStrategy.php +++ b/lib/Doctrine/ORM/Mapping/QuoteStrategy.php @@ -46,4 +46,63 @@ abstract class QuoteStrategy $this->platform = $platform; } + /** + * Checks if the given identifier is quoted + * + * @param string $identifier + * @return string + */ + abstract public function isQuotedIdentifier($identifier); + + /** + * Gets the uquoted column name. + * + * @param string $identifier + * @return string + */ + abstract public function getUnquotedIdentifier($identifier); + + /** + * Gets the (possibly quoted) column name for safe use in an SQL statement. + * + * @param string $fieldName + * @param ClassMetadata $class + * @return string + */ + abstract public function getColumnName($fieldName, ClassMetadata $class); + + /** + * Gets the (possibly quoted) primary table name for safe use in an SQL statement. + * + * @param ClassMetadata $class + * @return string + */ + abstract public function getTableName(ClassMetadata $class); + + /** + * Gets the (possibly quoted) name of the join table. + * + * @param ClassMetadata $class + * @return string + */ + abstract public function getJoinTableName($relation, ClassMetadata $class); + + /** + * Gets the (possibly quoted) identifier column names for safe use in an SQL statement. + * + * @param ClassMetadata $class + * @return array + */ + abstract public function getIdentifierColumnNames(ClassMetadata $class); + + /** + * Gets the column alias. + * + * @param string $columnName + * @param integer $counter + * @param ClassMetadata $class + * @return string + */ + abstract public function getColumnAlias($columnName, $counter, ClassMetadata $class = null); + } \ No newline at end of file diff --git a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php index d51e2fe51..b94ecfad7 100644 --- a/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php +++ b/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php @@ -170,6 +170,13 @@ class BasicEntityPersister */ protected $_sqlTableAliases = array(); + /** + * The quote strategy. + * + * @var \Doctrine\ORM\Mapping\QuoteStrategy + */ + private $quoteStrategy; + /** * Initializes a new BasicEntityPersister that uses the given EntityManager * and persists instances of the class described by the given ClassMetadata descriptor. @@ -179,10 +186,11 @@ class BasicEntityPersister */ public function __construct(EntityManager $em, ClassMetadata $class) { - $this->_em = $em; - $this->_class = $class; - $this->_conn = $em->getConnection(); - $this->_platform = $this->_conn->getDatabasePlatform(); + $this->_em = $em; + $this->_class = $class; + $this->_conn = $em->getConnection(); + $this->quoteStrategy = $em->getQuoteStrategy(); + $this->_platform = $this->_conn->getDatabasePlatform(); } /** @@ -1573,11 +1581,7 @@ class BasicEntityPersister */ 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()) - ); + return $this->quoteStrategy->getColumnAlias($columnName, $this->_sqlAliasCounter++); } /** diff --git a/lib/Doctrine/ORM/Query/SqlWalker.php b/lib/Doctrine/ORM/Query/SqlWalker.php index 920703149..06f0c4abe 100644 --- a/lib/Doctrine/ORM/Query/SqlWalker.php +++ b/lib/Doctrine/ORM/Query/SqlWalker.php @@ -286,11 +286,7 @@ class SqlWalker implements TreeWalker */ 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->aliasCounter++, -$this->platform->getMaxIdentifierLength()) - ); + return $this->quoteStrategy->getColumnAlias($columnName, $this->aliasCounter++); } /** diff --git a/tests/Doctrine/Tests/ORM/Persisters/BasicEntityPersisterTypeValueSqlTest.php b/tests/Doctrine/Tests/ORM/Persisters/BasicEntityPersisterTypeValueSqlTest.php index f31fb2b2f..344ca12f9 100644 --- a/tests/Doctrine/Tests/ORM/Persisters/BasicEntityPersisterTypeValueSqlTest.php +++ b/tests/Doctrine/Tests/ORM/Persisters/BasicEntityPersisterTypeValueSqlTest.php @@ -76,4 +76,16 @@ class BasicEntityPersisterTypeValueSqlTest extends \Doctrine\Tests\OrmTestCase $this->assertEquals('t0.customInteger = ABS(?) AND t0.child_id = ?', $sql); } + + /** + * @group DDC-1719 + */ + public function testStripNonAlphanumericCharactersFromSelectColumnListSQL() + { + $persister = new BasicEntityPersister($this->_em, $this->_em->getClassMetadata('Doctrine\Tests\Models\DDC1719\DDC1719Entity')); + $method = new \ReflectionMethod($persister, '_getSelectColumnListSQL'); + $method->setAccessible(true); + + $this->assertEquals('t0."entity-id" AS entityid1, t0."entity-value" AS entityvalue2', $method->invoke($persister)); + } } diff --git a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php index 09fc39b9d..ae7cd7a52 100644 --- a/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php +++ b/tests/Doctrine/Tests/ORM/Query/SelectSqlGenerationTest.php @@ -1641,6 +1641,27 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase ); } + /** + * @group DDC-1719 + */ + public function testStripNonAlphanumericCharactersFromAlias() + { + $this->assertSqlGeneration( + 'SELECT e FROM Doctrine\Tests\Models\DDC1719\DDC1719Entity e', + 'SELECT d0_."entity-id" AS entityid0, d0_."entity-value" AS entityvalue1 FROM "ddc-1719-entity" d0_' + ); + + $this->assertSqlGeneration( + 'SELECT e.value FROM Doctrine\Tests\Models\DDC1719\DDC1719Entity e ORDER BY e.value', + 'SELECT d0_."entity-value" AS entityvalue0 FROM "ddc-1719-entity" d0_ ORDER BY d0_."entity-value" ASC' + ); + + $this->assertSqlGeneration( + 'SELECT TRIM(e.value) FROM Doctrine\Tests\Models\DDC1719\DDC1719Entity e ORDER BY e.value', + 'SELECT TRIM(d0_."entity-value") AS sclr0 FROM "ddc-1719-entity" d0_ ORDER BY d0_."entity-value" ASC' + ); + } + }