1
0
Fork 0
mirror of synced 2025-04-03 13:23:37 +03:00

tests for quoted columns metadata

This commit is contained in:
Fabio B. Silva 2012-06-04 16:56:46 -03:00
parent 8fec73673d
commit f335f23145
13 changed files with 270 additions and 70 deletions

View file

@ -1373,17 +1373,25 @@ class ClassMetadataInfo implements ClassMetadata
$uniqueContraintColumns[] = $joinColumn['name']; $uniqueContraintColumns[] = $joinColumn['name'];
} }
} }
if (empty($joinColumn['name'])) { if (empty($joinColumn['name'])) {
$joinColumn['name'] = $this->namingStrategy->joinColumnName($mapping['fieldName']); $joinColumn['name'] = $this->namingStrategy->joinColumnName($mapping['fieldName']);
} }
if (empty($joinColumn['referencedColumnName'])) { if (empty($joinColumn['referencedColumnName'])) {
$joinColumn['referencedColumnName'] = $this->namingStrategy->referenceColumnName(); $joinColumn['referencedColumnName'] = $this->namingStrategy->referenceColumnName();
} }
if ($joinColumn['name'][0] == '`') { if ($joinColumn['name'][0] == '`') {
$joinColumn['name'] = trim($joinColumn['name'], '`'); $joinColumn['name'] = trim($joinColumn['name'], '`');
$joinColumn['quoted'] = true; $joinColumn['quoted'] = true;
} }
if ($joinColumn['referencedColumnName'][0] == '`') {
$joinColumn['referencedColumnName'] = trim($joinColumn['referencedColumnName'], '`');
$joinColumn['quoted'] = true;
}
$mapping['sourceToTargetKeyColumns'][$joinColumn['name']] = $joinColumn['referencedColumnName']; $mapping['sourceToTargetKeyColumns'][$joinColumn['name']] = $joinColumn['referencedColumnName'];
$mapping['joinColumnFieldNames'][$joinColumn['name']] = isset($joinColumn['fieldName']) $mapping['joinColumnFieldNames'][$joinColumn['name']] = isset($joinColumn['fieldName'])
? $joinColumn['fieldName'] : $joinColumn['name']; ? $joinColumn['fieldName'] : $joinColumn['name'];
@ -1464,12 +1472,25 @@ class ClassMetadataInfo implements ClassMetadata
if (empty($joinColumn['name'])) { if (empty($joinColumn['name'])) {
$joinColumn['name'] = $this->namingStrategy->joinKeyColumnName($mapping['sourceEntity'], $joinColumn['referencedColumnName']); $joinColumn['name'] = $this->namingStrategy->joinKeyColumnName($mapping['sourceEntity'], $joinColumn['referencedColumnName']);
} }
if (empty($joinColumn['referencedColumnName'])) { if (empty($joinColumn['referencedColumnName'])) {
$joinColumn['referencedColumnName'] = $this->namingStrategy->referenceColumnName(); $joinColumn['referencedColumnName'] = $this->namingStrategy->referenceColumnName();
} }
if ($joinColumn['name'][0] == '`') {
$joinColumn['name'] = trim($joinColumn['name'], '`');
$joinColumn['quoted'] = true;
}
if ($joinColumn['referencedColumnName'][0] == '`') {
$joinColumn['referencedColumnName'] = trim($joinColumn['referencedColumnName'], '`');
$joinColumn['quoted'] = true;
}
if (isset($joinColumn['onDelete']) && strtolower($joinColumn['onDelete']) == 'cascade') { if (isset($joinColumn['onDelete']) && strtolower($joinColumn['onDelete']) == 'cascade') {
$mapping['isOnDeleteCascade'] = true; $mapping['isOnDeleteCascade'] = true;
} }
$mapping['relationToSourceKeyColumns'][$joinColumn['name']] = $joinColumn['referencedColumnName']; $mapping['relationToSourceKeyColumns'][$joinColumn['name']] = $joinColumn['referencedColumnName'];
$mapping['joinTableColumns'][] = $joinColumn['name']; $mapping['joinTableColumns'][] = $joinColumn['name'];
} }
@ -1478,12 +1499,25 @@ class ClassMetadataInfo implements ClassMetadata
if (empty($inverseJoinColumn['name'])) { if (empty($inverseJoinColumn['name'])) {
$inverseJoinColumn['name'] = $this->namingStrategy->joinKeyColumnName($mapping['targetEntity'], $inverseJoinColumn['referencedColumnName']); $inverseJoinColumn['name'] = $this->namingStrategy->joinKeyColumnName($mapping['targetEntity'], $inverseJoinColumn['referencedColumnName']);
} }
if (empty($inverseJoinColumn['referencedColumnName'])) { if (empty($inverseJoinColumn['referencedColumnName'])) {
$inverseJoinColumn['referencedColumnName'] = $this->namingStrategy->referenceColumnName(); $inverseJoinColumn['referencedColumnName'] = $this->namingStrategy->referenceColumnName();
} }
if ($inverseJoinColumn['name'][0] == '`') {
$inverseJoinColumn['name'] = trim($inverseJoinColumn['name'], '`');
$inverseJoinColumn['quoted'] = true;
}
if ($inverseJoinColumn['referencedColumnName'][0] == '`') {
$inverseJoinColumn['referencedColumnName'] = trim($inverseJoinColumn['referencedColumnName'], '`');
$inverseJoinColumn['quoted'] = true;
}
if (isset($inverseJoinColumn['onDelete']) && strtolower($inverseJoinColumn['onDelete']) == 'cascade') { if (isset($inverseJoinColumn['onDelete']) && strtolower($inverseJoinColumn['onDelete']) == 'cascade') {
$mapping['isOnDeleteCascade'] = true; $mapping['isOnDeleteCascade'] = true;
} }
$mapping['relationToTargetKeyColumns'][$inverseJoinColumn['name']] = $inverseJoinColumn['referencedColumnName']; $mapping['relationToTargetKeyColumns'][$inverseJoinColumn['name']] = $inverseJoinColumn['referencedColumnName'];
$mapping['joinTableColumns'][] = $inverseJoinColumn['name']; $mapping['joinTableColumns'][] = $inverseJoinColumn['name'];
} }

View file

@ -23,30 +23,12 @@ namespace Doctrine\ORM\Mapping;
/** /**
* A set of rules for determining the physical column, alias and table quotes * A set of rules for determining the physical column, alias and table quotes
* *
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL * @since 2.3
* @link www.doctrine-project.org
* @since 2.4
* @author Fabio B. Silva <fabio.bat.silva@gmail.com> * @author Fabio B. Silva <fabio.bat.silva@gmail.com>
*/ */
class DefaultQuoteStrategy extends QuoteStrategy class DefaultQuoteStrategy extends QuoteStrategy
{ {
/**
* {@inheritdoc}
*/
public function isQuotedIdentifier($identifier)
{
return strlen($identifier) > 0 && $identifier[0] === '`';
}
/**
* {@inheritdoc}
*/
public function getUnquotedIdentifier($identifier)
{
return trim($identifier, '`');
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View file

@ -25,8 +25,6 @@ use Doctrine\DBAL\Platforms\AbstractPlatform;
/** /**
* A set of rules for determining the physical column, alias and table quotes * A set of rules for determining the physical column, alias and table quotes
* *
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.doctrine-project.org
* @since 2.3 * @since 2.3
* @author Fabio B. Silva <fabio.bat.silva@gmail.com> * @author Fabio B. Silva <fabio.bat.silva@gmail.com>
*/ */
@ -45,22 +43,6 @@ abstract class QuoteStrategy
$this->platform = $platform; $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. * Gets the (possibly quoted) column name for safe use in an SQL statement.
* *

View file

@ -0,0 +1,30 @@
<?php
namespace Doctrine\Tests\Models\Quote;
/**
* @Entity
* @Table(name="`quote-address`")
*/
class Address
{
/**
* @Id
* @GeneratedValue
* @Column(type="integer", name="`address-id`")
*/
public $id;
/**
* @Column(name="`address-zip`")
*/
public $zip;
/**
* @OneToOne(targetEntity="User", inversedBy="address")
* @JoinColumn(name="`user-id`", referencedColumnName="`user-id`")
*/
public $user;
}

View file

@ -0,0 +1,29 @@
<?php
namespace Doctrine\Tests\Models\Quote;
/**
* @Entity
* @Table(name="`quote-group`")
*/
class Group
{
/**
* @Id
* @GeneratedValue
* @Column(type="integer", name="`group-id`")
*/
public $id;
/**
* @Column(name="`group-name`")
*/
public $name;
/**
* @ManyToMany(targetEntity="User", mappedBy="groups")
*/
public $users;
}

View file

@ -0,0 +1,24 @@
<?php
namespace Doctrine\Tests\Models\Quote;
/**
* @Entity
* @Table(name="`quote-phone`")
*/
class Phone
{
/**
* @Id
* @Column(name="`phone-number`")
*/
public $number;
/**
* @ManyToOne(targetEntity="User", inversedBy="phones")
* @JoinColumn(name="`user-id`", referencedColumnName="`user-id`")
*/
public $user;
}

View file

@ -1,23 +1,23 @@
<?php <?php
namespace Doctrine\Tests\Models\DDC1719; namespace Doctrine\Tests\Models\Quote;
/** /**
* @Entity * @Entity
* @Table(name="`ddc-1719-entity`") * @Table(name="`ddc-1719-simple-entity`")
*/ */
class DDC1719Entity class SimpleEntity
{ {
/** /**
* @Id * @Id
* @Column(type="integer", name="`entity-id`") * @Column(type="integer", name="`simple-entity-id`")
* @GeneratedValue(strategy="AUTO") * @GeneratedValue(strategy="AUTO")
*/ */
public $id; public $id;
/** /**
* @Column(type="string", name="`entity-value`") * @Column(type="string", name="`simple-entity-value`")
*/ */
public $value; public $value;

View file

@ -0,0 +1,61 @@
<?php
namespace Doctrine\Tests\Models\Quote;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @Entity
* @Table(name="`quote-user`")
*/
class User
{
/**
* @Id
* @GeneratedValue
* @Column(type="integer", name="`user-id`")
*/
public $id;
/**
* @Column(type="string", name="`user-name`")
*/
public $name;
/**
* @OneToMany(targetEntity="Phone", mappedBy="user", cascade={"persist"})
*/
public $phones;
/**
* @JoinColumn(name="`address-id`", referencedColumnName="`address-id`")
* @OneToOne(targetEntity="Address", mappedBy="user", cascade={"persist"})
*/
public $address;
/**
* @ManyToMany(targetEntity="Group", inversedBy="users", cascade={"persist"})
* @JoinTable(name="`quote-users-groups`",
* joinColumns={
* @JoinColumn(
* name="`user-id`",
* referencedColumnName="`user-id`")
* },
* inverseJoinColumns={
* @JoinColumn(
* name="`group-id`",
* referencedColumnName="`group-id`"
* )
* }
* )
*/
public $groups;
public function __construct()
{
$this->phones = new ArrayCollection;
$this->groups = new ArrayCollection;
}
}

View file

@ -2,7 +2,7 @@
namespace Doctrine\Tests\ORM\Functional\Ticket; namespace Doctrine\Tests\ORM\Functional\Ticket;
use Doctrine\Tests\Models\DDC1719\DDC1719Entity; use Doctrine\Tests\Models\Quote\SimpleEntity;
require_once __DIR__ . '/../../../TestInit.php'; require_once __DIR__ . '/../../../TestInit.php';
@ -12,7 +12,7 @@ require_once __DIR__ . '/../../../TestInit.php';
class DDC1719Test extends \Doctrine\Tests\OrmFunctionalTestCase class DDC1719Test extends \Doctrine\Tests\OrmFunctionalTestCase
{ {
const CLASS_NAME = '\Doctrine\Tests\Models\DDC1719\DDC1719Entity'; const CLASS_NAME = '\Doctrine\Tests\Models\Quote\SimpleEntity';
protected function setUp() protected function setUp()
{ {
@ -27,8 +27,8 @@ class DDC1719Test extends \Doctrine\Tests\OrmFunctionalTestCase
public function testCreateRetreaveUpdateDelete() public function testCreateRetreaveUpdateDelete()
{ {
$e1 = new DDC1719Entity('Bar 1'); $e1 = new SimpleEntity('Bar 1');
$e2 = new DDC1719Entity('Foo 1'); $e2 = new SimpleEntity('Foo 1');
// Create // Create
$this->_em->persist($e1); $this->_em->persist($e1);

View file

@ -243,6 +243,80 @@ class ClassMetadataFactoryTest extends \Doctrine\Tests\OrmTestCase
$cm1->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_AUTO); $cm1->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_AUTO);
return $cm1; return $cm1;
} }
/**
* @group DDC-1845
*/
public function testQuoteMetadata()
{
$cmf = new ClassMetadataFactory();
$driver = $this->createAnnotationDriver(array(__DIR__ . '/../../Models/Quote/'));
$em = $this->_createEntityManager($driver);
$cmf->setEntityManager($em);
$userMetadata = $cmf->getMetadataFor('Doctrine\Tests\Models\Quote\User');
$phoneMetadata = $cmf->getMetadataFor('Doctrine\Tests\Models\Quote\Phone');
$groupMetadata = $cmf->getMetadataFor('Doctrine\Tests\Models\Quote\Group');
$addressMetadata = $cmf->getMetadataFor('Doctrine\Tests\Models\Quote\Address');
// Phone Class Metadata
$this->assertTrue($phoneMetadata->fieldMappings['number']['quoted']);
$this->assertEquals('phone-number', $phoneMetadata->fieldMappings['number']['columnName']);
$user = $phoneMetadata->associationMappings['user'];
$this->assertTrue($user['joinColumns'][0]['quoted']);
$this->assertEquals('user-id', $user['joinColumns'][0]['name']);
$this->assertEquals('user-id', $user['joinColumns'][0]['referencedColumnName']);
// User Class Metadata
$this->assertTrue($groupMetadata->fieldMappings['id']['quoted']);
$this->assertTrue($groupMetadata->fieldMappings['name']['quoted']);
$this->assertEquals('user-id', $userMetadata->fieldMappings['id']['columnName']);
$this->assertEquals('user-name', $userMetadata->fieldMappings['name']['columnName']);
// Address Class Metadata
$this->assertTrue($addressMetadata->fieldMappings['id']['quoted']);
$this->assertTrue($addressMetadata->fieldMappings['zip']['quoted']);
$this->assertEquals('address-id', $addressMetadata->fieldMappings['id']['columnName']);
$this->assertEquals('address-zip', $addressMetadata->fieldMappings['zip']['columnName']);
$user = $addressMetadata->associationMappings['user'];
$this->assertTrue($user['joinColumns'][0]['quoted']);
$this->assertEquals('user-id', $user['joinColumns'][0]['name']);
$this->assertEquals('user-id', $user['joinColumns'][0]['referencedColumnName']);
// User Class Metadata
$this->assertTrue($userMetadata->fieldMappings['id']['quoted']);
$this->assertTrue($userMetadata->fieldMappings['name']['quoted']);
$this->assertEquals('user-id', $userMetadata->fieldMappings['id']['columnName']);
$this->assertEquals('user-name', $userMetadata->fieldMappings['name']['columnName']);
$address = $userMetadata->associationMappings['address'];
$this->assertTrue($address['joinColumns'][0]['quoted']);
$this->assertEquals('address-id', $address['joinColumns'][0]['name']);
$this->assertEquals('address-id', $address['joinColumns'][0]['referencedColumnName']);
$groups = $userMetadata->associationMappings['groups'];
$this->assertTrue($groups['joinTable']['joinColumns'][0]['quoted']);
$this->assertEquals('user-id', $groups['joinTable']['joinColumns'][0]['name']);
$this->assertEquals('user-id', $groups['joinTable']['joinColumns'][0]['referencedColumnName']);
$this->assertTrue($groups['joinTable']['inverseJoinColumns'][0]['quoted']);
$this->assertEquals('group-id', $groups['joinTable']['inverseJoinColumns'][0]['name']);
$this->assertEquals('group-id', $groups['joinTable']['inverseJoinColumns'][0]['referencedColumnName']);
}
} }
/* Test subject class with overriden factory method for mocking purposes */ /* Test subject class with overriden factory method for mocking purposes */

View file

@ -26,7 +26,6 @@ class QuoteStrategyTest extends \Doctrine\Tests\OrmTestCase
$this->strategy = new DefaultQuoteStrategy($em->getConnection()->getDatabasePlatform()); $this->strategy = new DefaultQuoteStrategy($em->getConnection()->getDatabasePlatform());
} }
/** /**
* @param string $className * @param string $className
* @return \Doctrine\ORM\Mapping\ClassMetadata * @return \Doctrine\ORM\Mapping\ClassMetadata
@ -39,21 +38,6 @@ class QuoteStrategyTest extends \Doctrine\Tests\OrmTestCase
return $cm; return $cm;
} }
public function testIsQuotedIdentifier()
{
$this->assertTrue($this->strategy->isQuotedIdentifier('`table_name`'));
$this->assertFalse($this->strategy->isQuotedIdentifier('table_name'));
$this->assertFalse($this->strategy->isQuotedIdentifier(null));
$this->assertFalse($this->strategy->isQuotedIdentifier(''));
}
public function testGetUnquotedIdentifier()
{
$this->assertEquals('table_name' ,$this->strategy->getUnquotedIdentifier('`table_name`'));
$this->assertEquals('table_name' ,$this->strategy->getUnquotedIdentifier('table_name'));
}
public function testGetColumnName() public function testGetColumnName()
{ {
$cm = $this->createClassMetadata('Doctrine\Tests\Models\CMS\CmsUser'); $cm = $this->createClassMetadata('Doctrine\Tests\Models\CMS\CmsUser');

View file

@ -82,10 +82,10 @@ class BasicEntityPersisterTypeValueSqlTest extends \Doctrine\Tests\OrmTestCase
*/ */
public function testStripNonAlphanumericCharactersFromSelectColumnListSQL() public function testStripNonAlphanumericCharactersFromSelectColumnListSQL()
{ {
$persister = new BasicEntityPersister($this->_em, $this->_em->getClassMetadata('Doctrine\Tests\Models\DDC1719\DDC1719Entity')); $persister = new BasicEntityPersister($this->_em, $this->_em->getClassMetadata('Doctrine\Tests\Models\Quote\SimpleEntity'));
$method = new \ReflectionMethod($persister, '_getSelectColumnListSQL'); $method = new \ReflectionMethod($persister, '_getSelectColumnListSQL');
$method->setAccessible(true); $method->setAccessible(true);
$this->assertEquals('t0."entity-id" AS entityid1, t0."entity-value" AS entityvalue2', $method->invoke($persister)); $this->assertEquals('t0."simple-entity-id" AS simpleentityid1, t0."simple-entity-value" AS simpleentityvalue2', $method->invoke($persister));
} }
} }

View file

@ -1647,18 +1647,18 @@ class SelectSqlGenerationTest extends \Doctrine\Tests\OrmTestCase
public function testStripNonAlphanumericCharactersFromAlias() public function testStripNonAlphanumericCharactersFromAlias()
{ {
$this->assertSqlGeneration( $this->assertSqlGeneration(
'SELECT e FROM Doctrine\Tests\Models\DDC1719\DDC1719Entity e', 'SELECT e FROM Doctrine\Tests\Models\Quote\SimpleEntity e',
'SELECT d0_."entity-id" AS entityid0, d0_."entity-value" AS entityvalue1 FROM "ddc-1719-entity" d0_' 'SELECT d0_."simple-entity-id" AS simpleentityid0, d0_."simple-entity-value" AS simpleentityvalue1 FROM "ddc-1719-simple-entity" d0_'
); );
$this->assertSqlGeneration( $this->assertSqlGeneration(
'SELECT e.value FROM Doctrine\Tests\Models\DDC1719\DDC1719Entity e ORDER BY e.value', 'SELECT e.value FROM Doctrine\Tests\Models\Quote\SimpleEntity e ORDER BY e.value',
'SELECT d0_."entity-value" AS entityvalue0 FROM "ddc-1719-entity" d0_ ORDER BY d0_."entity-value" ASC' 'SELECT d0_."simple-entity-value" AS simpleentityvalue0 FROM "ddc-1719-simple-entity" d0_ ORDER BY d0_."simple-entity-value" ASC'
); );
$this->assertSqlGeneration( $this->assertSqlGeneration(
'SELECT TRIM(e.value) FROM Doctrine\Tests\Models\DDC1719\DDC1719Entity e ORDER BY e.value', 'SELECT TRIM(e.value) FROM Doctrine\Tests\Models\Quote\SimpleEntity e ORDER BY e.value',
'SELECT TRIM(d0_."entity-value") AS sclr0 FROM "ddc-1719-entity" d0_ ORDER BY d0_."entity-value" ASC' 'SELECT TRIM(d0_."simple-entity-value") AS sclr0 FROM "ddc-1719-simple-entity" d0_ ORDER BY d0_."simple-entity-value" ASC'
); );
} }