From f198104cc0c947b7ca85a67e61c376ab77b3e8ba Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Tue, 9 Jun 2020 17:19:32 -0700 Subject: [PATCH] Deprecated platform-specific portability mode constants --- UPGRADE.md | 5 +++ lib/Doctrine/DBAL/Portability/Connection.php | 28 ++++-------- .../DBAL/Portability/OptimizeFlags.php | 44 +++++++++++++++++++ .../DBAL/Portability/OptimizeFlagsTest.php | 36 +++++++++++++++ 4 files changed, 94 insertions(+), 19 deletions(-) create mode 100644 lib/Doctrine/DBAL/Portability/OptimizeFlags.php create mode 100644 tests/Doctrine/Tests/DBAL/Portability/OptimizeFlagsTest.php diff --git a/UPGRADE.md b/UPGRADE.md index e69c41e563f..916603a0e46 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -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`. diff --git a/lib/Doctrine/DBAL/Portability/Connection.php b/lib/Doctrine/DBAL/Portability/Connection.php index 266b8ad095b..ad79f98e889 100644 --- a/lib/Doctrine/DBAL/Portability/Connection.php +++ b/lib/Doctrine/DBAL/Portability/Connection.php @@ -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; @@ -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; @@ -47,25 +52,10 @@ 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; - } - - $this->portability = $params['portability']; + $this->portability = $params['portability'] = (new OptimizeFlags())( + $this->getDatabasePlatform(), + $params['portability'] + ); } if (isset($params['fetch_case']) && $this->portability & self::PORTABILITY_FIX_CASE) { diff --git a/lib/Doctrine/DBAL/Portability/OptimizeFlags.php b/lib/Doctrine/DBAL/Portability/OptimizeFlags.php new file mode 100644 index 00000000000..7d8e55df457 --- /dev/null +++ b/lib/Doctrine/DBAL/Portability/OptimizeFlags.php @@ -0,0 +1,44 @@ + + */ + 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; + } +} diff --git a/tests/Doctrine/Tests/DBAL/Portability/OptimizeFlagsTest.php b/tests/Doctrine/Tests/DBAL/Portability/OptimizeFlagsTest.php new file mode 100644 index 00000000000..3d776de6e7b --- /dev/null +++ b/tests/Doctrine/Tests/DBAL/Portability/OptimizeFlagsTest.php @@ -0,0 +1,36 @@ +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); + } +}