Skip to content

Commit

Permalink
Support for DBAL 3's platform classes
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander M. Turek <me@derrabus.de>
  • Loading branch information
derrabus committed Aug 29, 2021
1 parent a06bbaf commit fdbc6b6
Show file tree
Hide file tree
Showing 26 changed files with 149 additions and 81 deletions.
15 changes: 9 additions & 6 deletions lib/Doctrine/ORM/Internal/SQLResultCasing.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
namespace Doctrine\ORM\Internal;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\DB2Platform;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;

use function method_exists;
use function strtolower;
Expand All @@ -17,13 +21,12 @@ trait SQLResultCasing
{
private function getSQLResultCasing(AbstractPlatform $platform, string $column): string
{
switch ($platform->getName()) {
case 'db2':
case 'oracle':
return strtoupper($column);
if ($platform instanceof DB2Platform || $platform instanceof OraclePlatform) {
return strtoupper($column);
}

case 'postgresql':
return strtolower($column);
if ($platform instanceof PostgreSQL94Platform || $platform instanceof PostgreSQLPlatform) {
return strtolower($column);
}

if (method_exists(AbstractPlatform::class, 'getSQLResultCasing')) {
Expand Down
6 changes: 5 additions & 1 deletion lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,11 @@ private function completeIdGeneratorMapping(ClassMetadataInfo $class): void

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

Expand Down
9 changes: 2 additions & 7 deletions lib/Doctrine/ORM/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use Doctrine\Common\Cache\Cache;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\LockMode;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\Internal\Hydration\IterableResult;
Expand All @@ -26,6 +25,7 @@
use function array_values;
use function assert;
use function count;
use function get_debug_type;
use function in_array;
use function ksort;
use function md5;
Expand Down Expand Up @@ -739,14 +739,9 @@ protected function getQueryCacheId(): string
{
ksort($this->_hints);

$platform = $this->getEntityManager()
->getConnection()
->getDatabasePlatform()
->getName();

return md5(
$this->getDQL() . serialize($this->_hints) .
'&platform=' . $platform .
'&platform=' . get_debug_type($this->getEntityManager()->getConnection()->getDatabasePlatform()) .
($this->_em->hasFilters() ? $this->_em->getFilters()->getHash() : '') .
'&firstResult=' . $this->firstResult . '&maxResult=' . $this->maxResults .
'&hydrationMode=' . $this->_hydrationMode . '&types=' . serialize($this->parsedTypes) . 'DOCTRINE_QUERY_CACHE_SALT'
Expand Down
4 changes: 3 additions & 1 deletion lib/Doctrine/ORM/Tools/Pagination/CountOutputWalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Doctrine\ORM\Tools\Pagination;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\SQLServer2012Platform;
use Doctrine\DBAL\Platforms\SQLServerPlatform;
use Doctrine\ORM\Query;
use Doctrine\ORM\Query\AST\SelectStatement;
use Doctrine\ORM\Query\ParserResult;
Expand Down Expand Up @@ -70,7 +72,7 @@ public function __construct($query, $parserResult, array $queryComponents)
*/
public function walkSelectStatement(SelectStatement $AST)
{
if ($this->platform->getName() === 'mssql') {
if ($this->platform instanceof SQLServer2012Platform || $this->platform instanceof SQLServerPlatform) {
$AST->orderByClause = null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\DB2Platform;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SQLAnywherePlatform;
use Doctrine\DBAL\Platforms\SQLServer2012Platform;
use Doctrine\DBAL\Platforms\SQLServerPlatform;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\QuoteStrategy;
Expand Down Expand Up @@ -115,7 +117,9 @@ public function __construct($query, $parserResult, array $queryComponents)
*/
private function platformSupportsRowNumber(): bool
{
return $this->platform instanceof PostgreSqlPlatform
return $this->platform instanceof PostgreSQL94Platform // DBAL 3.1 compatibility
|| $this->platform instanceof PostgreSQLPlatform
|| $this->platform instanceof SQLServer2012Platform // DBAL 3.1 compatibility
|| $this->platform instanceof SQLServerPlatform
|| $this->platform instanceof OraclePlatform
|| $this->platform instanceof SQLAnywherePlatform
Expand Down
2 changes: 2 additions & 0 deletions phpstan-dbal3.neon
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ parameters:
-
message: '/Application::add\(\) expects Symfony\\Component\\Console\\Command\\Command/'
path: lib/Doctrine/ORM/Tools/Console/ConsoleRunner.php

- '/^Class Doctrine\\DBAL\\Platforms\\(PostgreSQL|SQLServer|SQLAnywhere)Platform not found\.$/'
2 changes: 2 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ parameters:
- '/Variable \$offset in isset\(\) always exists and is not nullable\./'
# PHPStan doesn't understand our method_exists() safeguards.
- '/Call to an undefined method Doctrine\\DBAL\\Connection::createSchemaManager\(\)\./'
# Class name will change in DBAL 3.
- '/Class Doctrine\\DBAL\\Platforms\\PostgreSqlPlatform referenced with incorrect case: Doctrine\\DBAL\\Platforms\\PostgreSQLPlatform\./'
11 changes: 11 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,11 @@
<code>$class</code>
</PropertyNotSetInConstructor>
</file>
<file src="lib/Doctrine/ORM/Internal/SQLResultCasing.php">
<MissingDependency occurrences="1">
<code>PostgreSQL94Platform</code>
</MissingDependency>
</file>
<file src="lib/Doctrine/ORM/LazyCriteriaCollection.php">
<MoreSpecificImplementedParamType occurrences="1">
<code>$element</code>
Expand Down Expand Up @@ -869,6 +874,9 @@
<code>$driver</code>
<code>$evm</code>
</MissingConstructor>
<MissingDependency occurrences="1">
<code>Platforms\PostgreSQL94Platform</code>
</MissingDependency>
<NoInterfaceProperties occurrences="31">
<code>$class-&gt;cache</code>
<code>$class-&gt;changeTrackingPolicy</code>
Expand Down Expand Up @@ -3774,6 +3782,9 @@
</ImplementedReturnTypeMismatch>
</file>
<file src="lib/Doctrine/ORM/Tools/Pagination/LimitSubqueryOutputWalker.php">
<MissingDependency occurrences="1">
<code>PostgreSQL94Platform</code>
</MissingDependency>
<MoreSpecificImplementedParamType occurrences="1">
<code>$query</code>
</MoreSpecificImplementedParamType>
Expand Down
6 changes: 6 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
<file name="lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php"/>
</errorLevel>
</DocblockTypeContradiction>
<InvalidClass>
<errorLevel type="suppress">
<!-- Class name changes in DBAL 3. -->
<referencedClass name="Doctrine\DBAL\Platforms\PostgreSQLPlatform" />
</errorLevel>
</InvalidClass>
<ParadoxicalCondition>
<errorLevel type="suppress">
<!-- See https://github.com/vimeo/psalm/issues/3381 -->
Expand Down
7 changes: 2 additions & 5 deletions tests/Doctrine/Tests/Mocks/DatabasePlatformMock.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,9 @@ public function setSupportsSequences(bool $bool): void
$this->supportsSequences = $bool;
}

/**
* {@inheritdoc}
*/
public function getName()
public function getName(): string
{
return 'mock';
throw new BadMethodCallException('Call to deprecated method.');
}

/**
Expand Down
23 changes: 16 additions & 7 deletions tests/Doctrine/Tests/ORM/Functional/DatabaseDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

namespace Doctrine\Tests\ORM\Functional;

use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Platforms\SQLServer2012Platform;
use Doctrine\DBAL\Platforms\SQLServerPlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Table;
Expand Down Expand Up @@ -195,12 +198,7 @@ public function testLoadMetadataFromDatabaseDetail(): void
self::assertEquals('id', strtolower($metadata->fieldMappings['id']['columnName']));
self::assertEquals('integer', (string) $metadata->fieldMappings['id']['type']);

// FIXME: Condition here is fugly.
// NOTE: PostgreSQL and SQL SERVER do not support UNSIGNED integer
if (
! $this->_em->getConnection()->getDatabasePlatform() instanceof PostgreSqlPlatform &&
! $this->_em->getConnection()->getDatabasePlatform() instanceof SQLServerPlatform
) {
if (self::supportsUnsignedInteger($this->_em->getConnection()->getDatabasePlatform())) {
self::assertArrayHasKey('columnUnsigned', $metadata->fieldMappings);
self::assertTrue($metadata->fieldMappings['columnUnsigned']['options']['unsigned']);
}
Expand All @@ -227,4 +225,15 @@ public function testLoadMetadataFromDatabaseDetail(): void
$metadata->table['uniqueConstraints']['unique_index1']['columns']
);
}

private static function supportsUnsignedInteger(AbstractPlatform $platform): bool
{
// FIXME: Condition here is fugly.
// NOTE: PostgreSQL and SQL SERVER do not support UNSIGNED integer

return ! $platform instanceof SQLServer2012Platform
&& ! $platform instanceof SQLServerPlatform
&& ! $platform instanceof PostgreSQL94Platform
&& ! $platform instanceof PostgreSQLPlatform;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\Tests\ORM\Functional\SchemaTool;

use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\ORM\Tools;
use Doctrine\Tests\Models;
Expand Down Expand Up @@ -34,7 +35,7 @@ protected function setUp(): void

$conn = $this->_em->getConnection();

if ($conn->getDriver()->getDatabasePlatform()->getName() === 'sqlite') {
if ($conn->getDriver()->getDatabasePlatform() instanceof SqlitePlatform) {
self::markTestSkipped('SQLite does not support ALTER TABLE statements.');
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\Tests\ORM\Functional\SchemaTool;

use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
Expand All @@ -13,7 +14,6 @@
use Doctrine\Tests\Models;
use Doctrine\Tests\OrmFunctionalTestCase;

use function count;
use function method_exists;
use function sprintf;

Expand All @@ -22,7 +22,7 @@ class MySqlSchemaToolTest extends OrmFunctionalTestCase
protected function setUp(): void
{
parent::setUp();
if ($this->_em->getConnection()->getDatabasePlatform()->getName() !== 'mysql') {
if (! $this->_em->getConnection()->getDatabasePlatform() instanceof MySQLPlatform) {
self::markTestSkipped('The ' . self::class . ' requires the use of mysql.');
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Doctrine\Tests\ORM\Functional\SchemaTool;

use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
Expand All @@ -17,7 +19,6 @@

use function array_filter;
use function array_shift;
use function count;
use function implode;
use function strpos;

Expand All @@ -27,7 +28,8 @@ protected function setUp(): void
{
parent::setUp();

if ($this->_em->getConnection()->getDatabasePlatform()->getName() !== 'postgresql') {
$platform = $this->_em->getConnection()->getDatabasePlatform();
if (! $platform instanceof PostgreSQL94Platform && ! $platform instanceof PostgreSQLPlatform) {
self::markTestSkipped('The ' . self::class . ' requires the use of postgresql.');
}
}
Expand Down
5 changes: 4 additions & 1 deletion tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1151Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
Expand All @@ -20,7 +22,8 @@ class DDC1151Test extends OrmFunctionalTestCase
{
public function testQuoteForeignKey(): void
{
if ($this->_em->getConnection()->getDatabasePlatform()->getName() !== 'postgresql') {
$platform = $this->_em->getConnection()->getDatabasePlatform();
if (! $platform instanceof PostgreSQL94Platform && ! $platform instanceof PostgreSQLPlatform) {
self::markTestSkipped('This test is useful for all databases, but designed only for postgresql.');
}

Expand Down
5 changes: 4 additions & 1 deletion tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1360Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
Expand All @@ -18,7 +20,8 @@ class DDC1360Test extends OrmFunctionalTestCase
{
public function testSchemaDoubleQuotedCreate(): void
{
if ($this->_em->getConnection()->getDatabasePlatform()->getName() !== 'postgresql') {
$platform = $this->_em->getConnection()->getDatabasePlatform();
if (! $platform instanceof PostgreSQL94Platform && ! $platform instanceof PostgreSQLPlatform) {
self::markTestSkipped('PostgreSQL only test.');
}

Expand Down
3 changes: 2 additions & 1 deletion tests/Doctrine/Tests/ORM/Functional/Ticket/DDC1695Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\GeneratedValue;
Expand All @@ -18,7 +19,7 @@ class DDC1695Test extends OrmFunctionalTestCase
{
public function testIssue(): void
{
if ($this->_em->getConnection()->getDatabasePlatform()->getName() !== 'sqlite') {
if (! $this->_em->getConnection()->getDatabasePlatform() instanceof SqlitePlatform) {
self::markTestSkipped('Only with sqlite');
}

Expand Down
3 changes: 2 additions & 1 deletion tests/Doctrine/Tests/ORM/Functional/Ticket/DDC2182Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;
Expand All @@ -19,7 +20,7 @@ class DDC2182Test extends OrmFunctionalTestCase
{
public function testPassColumnOptionsToJoinColumns(): void
{
if ($this->_em->getConnection()->getDatabasePlatform()->getName() !== 'mysql') {
if (! $this->_em->getConnection()->getDatabasePlatform() instanceof MySQLPlatform) {
self::markTestSkipped('This test is useful for all databases, but designed only for mysql.');
}

Expand Down
5 changes: 2 additions & 3 deletions tests/Doctrine/Tests/ORM/Functional/Ticket/DDC832Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Doctrine\Tests\ORM\Functional\Ticket;

use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\DiscriminatorColumn;
use Doctrine\ORM\Mapping\DiscriminatorMap;
Expand All @@ -22,9 +23,7 @@ protected function setUp(): void
{
parent::setUp();

$platform = $this->_em->getConnection()->getDatabasePlatform();

if ($platform->getName() === 'oracle') {
if ($this->_em->getConnection()->getDatabasePlatform() instanceof OraclePlatform) {
self::markTestSkipped('Doesnt run on Oracle.');
}

Expand Down
Loading

0 comments on commit fdbc6b6

Please sign in to comment.