Skip to content

Commit

Permalink
Merge pull request #8870 from derrabus/improvement/prefers-sequences
Browse files Browse the repository at this point in the history
Remove calls to prefersSequences()
  • Loading branch information
greg0ire authored Aug 10, 2021
2 parents f980682 + edaa05a commit 7c15937
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 34 deletions.
25 changes: 18 additions & 7 deletions lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,13 +548,7 @@ private function completeIdGeneratorMapping(ClassMetadataInfo $class): void
{
$idGenType = $class->generatorType;
if ($idGenType === ClassMetadata::GENERATOR_TYPE_AUTO) {
if ($this->getTargetPlatform()->prefersSequences()) {
$class->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_SEQUENCE);
} elseif ($this->getTargetPlatform()->prefersIdentityColumns()) {
$class->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_IDENTITY);
} else {
$class->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_TABLE);
}
$class->setIdGeneratorType($this->determineIdGeneratorStrategy($this->getTargetPlatform()));
}

// Create & assign an appropriate ID generator instance
Expand Down Expand Up @@ -658,6 +652,23 @@ private function completeIdGeneratorMapping(ClassMetadataInfo $class): void
}
}

private function determineIdGeneratorStrategy(AbstractPlatform $platform): int
{
if ($platform->getName() === 'oracle' || $platform->getName() === 'postgresql') {
return ClassMetadata::GENERATOR_TYPE_SEQUENCE;
}

if ($platform->supportsIdentityColumns()) {
return ClassMetadata::GENERATOR_TYPE_IDENTITY;
}

if ($platform->supportsSequences()) {
return ClassMetadata::GENERATOR_TYPE_SEQUENCE;
}

return ClassMetadata::GENERATOR_TYPE_TABLE;
}

/**
* Inherits the ID generator mapping from a parent class.
*/
Expand Down
48 changes: 23 additions & 25 deletions tests/Doctrine/Tests/Mocks/DatabasePlatformMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,47 @@

namespace Doctrine\Tests\Mocks;

use BadMethodCallException;
use Doctrine\DBAL\Exception as DBALException;
use Doctrine\DBAL\Platforms\AbstractPlatform;

/**
* Mock class for DatabasePlatform.
*/
class DatabasePlatformMock extends AbstractPlatform
{
/** @var string */
private $_sequenceNextValSql = '';

/** @var bool */
private $_prefersIdentityColumns = true;
private $supportsIdentityColumns = true;

/** @var bool */
private $_prefersSequences = false;
private $supportsSequences = false;

/**
* {@inheritdoc}
*/
public function prefersIdentityColumns()
public function prefersIdentityColumns(): bool
{
return $this->_prefersIdentityColumns;
throw new BadMethodCallException('Call to deprecated method.');
}

/**
* {@inheritdoc}
*/
public function prefersSequences()
public function supportsIdentityColumns(): bool
{
return $this->supportsIdentityColumns;
}

public function prefersSequences(): bool
{
throw new BadMethodCallException('Call to deprecated method.');
}

public function supportsSequences(): bool
{
return $this->_prefersSequences;
return $this->supportsSequences;
}

/**
* {@inheritdoc}
*/
public function getSequenceNextValSQL($sequenceName)
{
return $this->_sequenceNextValSql;
return '';
}

/**
Expand Down Expand Up @@ -95,19 +98,14 @@ public function getClobTypeDeclarationSQL(array $field)

/* MOCK API */

public function setPrefersIdentityColumns(bool $bool): void
{
$this->_prefersIdentityColumns = $bool;
}

public function setPrefersSequences(bool $bool): void
public function setSupportsIdentityColumns(bool $bool): void
{
$this->_prefersSequences = $bool;
$this->supportsIdentityColumns = $bool;
}

public function setSequenceNextValSql(string $sql): void
public function setSupportsSequences(bool $bool): void
{
$this->_sequenceNextValSql = $sql;
$this->supportsSequences = $bool;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/Doctrine/Tests/ORM/Mapping/ClassMetadataFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public function testGetMetadataForSingleClass(): void

$conn = $entityManager->getConnection();
$mockPlatform = $conn->getDatabasePlatform();
$mockPlatform->setPrefersSequences(true);
$mockPlatform->setPrefersIdentityColumns(false);
$mockPlatform->setSupportsSequences(true);
$mockPlatform->setSupportsIdentityColumns(false);

$cm1 = $this->createValidClassMetadata();

Expand Down

0 comments on commit 7c15937

Please sign in to comment.