diff --git a/lib/Doctrine/ORM/Mapping/AnsiQuoteStrategy.php b/lib/Doctrine/ORM/Mapping/AnsiQuoteStrategy.php new file mode 100644 index 000000000..440c3909f --- /dev/null +++ b/lib/Doctrine/ORM/Mapping/AnsiQuoteStrategy.php @@ -0,0 +1,97 @@ +. + */ + +namespace Doctrine\ORM\Mapping; + +use Doctrine\ORM\Mapping\ClassMetadata; +use Doctrine\DBAL\Platforms\AbstractPlatform; + +/** + * ANSI compliant quote strategy, this strategy does not apply any quote. + * To use this strategy all mapped tables and columns should be ANSI compliant. + * + * @since 2.5 + * @author Fabio B. Silva + */ +class AnsiQuoteStrategy implements QuoteStrategy +{ + /** + * {@inheritdoc} + */ + public function getColumnName($fieldName, ClassMetadata $class, AbstractPlatform $platform) + { + return $class->fieldMappings[$fieldName]['columnName']; + } + + /** + * {@inheritdoc} + */ + public function getTableName(ClassMetadata $class, AbstractPlatform $platform) + { + return $class->table['name']; + } + + /** + * {@inheritdoc} + */ + public function getSequenceName(array $definition, ClassMetadata $class, AbstractPlatform $platform) + { + return $definition['sequenceName']; + } + + /** + * {@inheritdoc} + */ + public function getJoinColumnName(array $joinColumn, ClassMetadata $class, AbstractPlatform $platform) + { + return $joinColumn['name']; + } + + /** + * {@inheritdoc} + */ + public function getReferencedJoinColumnName(array $joinColumn, ClassMetadata $class, AbstractPlatform $platform) + { + return $joinColumn['referencedColumnName']; + } + + /** + * {@inheritdoc} + */ + public function getJoinTableName(array $association, ClassMetadata $class, AbstractPlatform $platform) + { + return $association['joinTable']['name']; + } + + /** + * {@inheritdoc} + */ + public function getIdentifierColumnNames(ClassMetadata $class, AbstractPlatform $platform) + { + return $class->identifier; + } + + /** + * {@inheritdoc} + */ + public function getColumnAlias($columnName, $counter, AbstractPlatform $platform, ClassMetadata $class = null) + { + return $platform->getSQLResultCasing($columnName . $counter); + } +} diff --git a/tests/Doctrine/Tests/ORM/Mapping/AnsiQuoteStrategyTest.php b/tests/Doctrine/Tests/ORM/Mapping/AnsiQuoteStrategyTest.php new file mode 100644 index 000000000..5c51eb034 --- /dev/null +++ b/tests/Doctrine/Tests/ORM/Mapping/AnsiQuoteStrategyTest.php @@ -0,0 +1,148 @@ +_getTestEntityManager(); + $this->platform = $em->getConnection()->getDatabasePlatform(); + $this->strategy = new AnsiQuoteStrategy(); + } + + /** + * @param string $className + * @return \Doctrine\ORM\Mapping\ClassMetadata + */ + private function createClassMetadata($className) + { + $class = new ClassMetadata($className); + $class->initializeReflection(new \Doctrine\Common\Persistence\Mapping\RuntimeReflectionService); + + return $class; + } + + public function testGetColumnName() + { + $class = $this->createClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'); + $class->mapField(array('fieldName' => 'name', 'columnName' => 'name')); + $class->mapField(array('fieldName' => 'id', 'columnName' => 'id', 'id' => true)); + + $this->assertEquals('id' ,$this->strategy->getColumnName('id', $class, $this->platform)); + $this->assertEquals('name' ,$this->strategy->getColumnName('name', $class, $this->platform)); + } + + public function testGetTableName() + { + $class = $this->createClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'); + + $class->setPrimaryTable(array('name'=>'cms_user')); + $this->assertEquals('cms_user' ,$this->strategy->getTableName($class, $this->platform)); + } + + public function testJoinTableName() + { + $class = $this->createClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress'); + + $class->mapManyToMany(array( + 'fieldName' => 'user', + 'targetEntity' => 'CmsUser', + 'inversedBy' => 'users', + 'joinTable' => array( + 'name' => 'cmsaddress_cmsuser' + ) + )); + + $this->assertEquals('cmsaddress_cmsuser', $this->strategy->getJoinTableName($class->associationMappings['user'], $class, $this->platform)); + + } + + public function testIdentifierColumnNames() + { + $class = $this->createClassMetadata('Doctrine\Tests\Models\CMS\CmsAddress'); + + $class->mapField(array( + 'id' => true, + 'fieldName' => 'id', + 'columnName' => 'id', + )); + + $this->assertEquals(array('id'), $this->strategy->getIdentifierColumnNames($class, $this->platform)); + } + + + public function testColumnAlias() + { + $this->assertEquals('columnName1', $this->strategy->getColumnAlias('columnName', 1, $this->platform)); + } + + public function testJoinColumnName() + { + $class = $this->createClassMetadata('Doctrine\Tests\Models\DDC117\DDC117ArticleDetails'); + + $class->mapOneToOne(array( + 'id' => true, + 'fieldName' => 'article', + 'targetEntity' => 'Doctrine\Tests\Models\DDC117\DDC117Article', + 'joinColumns' => array(array( + 'name' => 'article' + )), + )); + + $joinColumn = $class->associationMappings['article']['joinColumns'][0]; + $this->assertEquals('article',$this->strategy->getJoinColumnName($joinColumn, $class, $this->platform)); + } + + public function testReferencedJoinColumnName() + { + $cm = $this->createClassMetadata('Doctrine\Tests\Models\DDC117\DDC117ArticleDetails'); + + $cm->mapOneToOne(array( + 'id' => true, + 'fieldName' => 'article', + 'targetEntity' => 'Doctrine\Tests\Models\DDC117\DDC117Article', + 'joinColumns' => array(array( + 'name' => 'article' + )), + )); + + $joinColumn = $cm->associationMappings['article']['joinColumns'][0]; + $this->assertEquals('id',$this->strategy->getReferencedJoinColumnName($joinColumn, $cm, $this->platform)); + } + + public function testGetSequenceName() + { + $class = $this->createClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'); + $definition = array( + 'sequenceName' => 'user_id_seq', + 'allocationSize' => 1, + 'initialValue' => 2 + ); + + $class->setSequenceGeneratorDefinition($definition); + + $this->assertEquals('user_id_seq',$this->strategy->getSequenceName($definition, $class, $this->platform)); + } +} \ No newline at end of file