Skip to content

Commit

Permalink
Deprecated platform-specific portability mode constants
Browse files Browse the repository at this point in the history
  • Loading branch information
morozov committed Jun 10, 2020
1 parent 7047798 commit 89c6f42
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 17 deletions.
5 changes: 5 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Upgrade to 2.11

## Deprecated `Portability\Connection::PORTABILITY_{PLATFORM}` constants`

The platform-specific portability mode flags are meant to be used only by the portability layer internally to optimize
the user-provided mode for the current database platform.

## Deprecated `MasterSlaveConnection` use `PrimaryReadReplicaConnection`

The `Doctrine\DBAL\Connections\MasterSlaveConnection` class is renamed to `Doctrine\DBAL\Connections\PrimaryReadReplicaConnection`.
Expand Down
25 changes: 8 additions & 17 deletions lib/Doctrine/DBAL/Portability/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ class Connection extends \Doctrine\DBAL\Connection
public const PORTABILITY_EMPTY_TO_NULL = 4;
public const PORTABILITY_FIX_CASE = 8;

/**#@+
*
* @deprecated Will be removed as internal implementation details.
*/
public const PORTABILITY_DB2 = 13;
public const PORTABILITY_ORACLE = 9;
public const PORTABILITY_POSTGRESQL = 13;
Expand All @@ -31,6 +35,7 @@ class Connection extends \Doctrine\DBAL\Connection
public const PORTABILITY_DRIZZLE = 13;
public const PORTABILITY_SQLANYWHERE = 13;
public const PORTABILITY_SQLSRV = 13;
/**#@-*/

/** @var int */
private $portability = self::PORTABILITY_NONE;
Expand All @@ -47,23 +52,9 @@ public function connect()
if ($ret) {
$params = $this->getParams();
if (isset($params['portability'])) {
if ($this->getDatabasePlatform()->getName() === 'oracle') {
$params['portability'] &= self::PORTABILITY_ORACLE;
} elseif ($this->getDatabasePlatform()->getName() === 'postgresql') {
$params['portability'] &= self::PORTABILITY_POSTGRESQL;
} elseif ($this->getDatabasePlatform()->getName() === 'sqlite') {
$params['portability'] &= self::PORTABILITY_SQLITE;
} elseif ($this->getDatabasePlatform()->getName() === 'drizzle') {
$params['portability'] &= self::PORTABILITY_DRIZZLE;
} elseif ($this->getDatabasePlatform()->getName() === 'sqlanywhere') {
$params['portability'] &= self::PORTABILITY_SQLANYWHERE;
} elseif ($this->getDatabasePlatform()->getName() === 'db2') {
$params['portability'] &= self::PORTABILITY_DB2;
} elseif ($this->getDatabasePlatform()->getName() === 'mssql') {
$params['portability'] &= self::PORTABILITY_SQLSRV;
} else {
$params['portability'] &= self::PORTABILITY_OTHERVENDORS;
}
$platform = $this->getDatabasePlatform();

$params['portability'] = (new OptimizeFlags())($platform, $params['portability']);

$this->portability = $params['portability'];
}
Expand Down
44 changes: 44 additions & 0 deletions lib/Doctrine/DBAL/Portability/OptimizeFlags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Portability;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\DB2Platform;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Platforms\PostgreSQL94Platform;
use Doctrine\DBAL\Platforms\SQLAnywhere16Platform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Platforms\SQLServer2012Platform;

final class OptimizeFlags
{
/**
* Platform-specific portability flags that need to be excluded from the user-provided mode
* since the platform already operates in this mode to avoid unnecessary conversion overhead.
*
* @var array<string,int>
*/
private static $platforms = [
DB2Platform::class => 0,
OraclePlatform::class => Connection::PORTABILITY_EMPTY_TO_NULL,
PostgreSQL94Platform::class => 0,
SQLAnywhere16Platform::class => 0,
SqlitePlatform::class => 0,
SQLServer2012Platform::class => 0,
];

public function __invoke(AbstractPlatform $platform, int $flags): int
{
foreach (self::$platforms as $class => $mask) {
if ($platform instanceof $class) {
$flags &= ~$mask;

break;
}
}

return $flags;
}
}
36 changes: 36 additions & 0 deletions tests/Doctrine/Tests/DBAL/Portability/OptimizeFlagsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\DBAL\Portability;

use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Platforms\SqlitePlatform;
use Doctrine\DBAL\Portability\Connection;
use Doctrine\DBAL\Portability\OptimizeFlags;
use PHPUnit\Framework\TestCase;

class OptimizeFlagsTest extends TestCase
{
/** @var OptimizeFlags */
private $optimizeFlags;

protected function setUp(): void
{
$this->optimizeFlags = new OptimizeFlags();
}

public function testOracle(): void
{
$flags = ($this->optimizeFlags)(new OraclePlatform(), Connection::PORTABILITY_ALL);

self::assertSame(0, $flags & Connection::PORTABILITY_EMPTY_TO_NULL);
}

public function testAnotherPlatform(): void
{
$flags = ($this->optimizeFlags)(new SqlitePlatform(), Connection::PORTABILITY_ALL);

self::assertSame(Connection::PORTABILITY_ALL, $flags);
}
}

0 comments on commit 89c6f42

Please sign in to comment.