From 01d3b5f2e1fd3eaa79ebb78f0795ef6f67094f3d Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Sat, 30 Jul 2022 08:51:39 -0700 Subject: [PATCH 1/5] Convert TrimMode to enum --- UPGRADE.md | 8 ++++++++ src/Platforms/AbstractPlatform.php | 14 ++------------ src/Platforms/SQLServerPlatform.php | 9 +-------- src/Platforms/SQLitePlatform.php | 9 +-------- src/Platforms/TrimMode.php | 20 +++++--------------- tests/Functional/DataAccessTest.php | 11 ++--------- 6 files changed, 19 insertions(+), 52 deletions(-) diff --git a/UPGRADE.md b/UPGRADE.md index f258faa9a07..d6f08670199 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -8,6 +8,14 @@ awareness about deprecated code. # Upgrade to 4.0 +## BC BREAK: converted enum-like classes to enums + +The following classes have been converted to enums: + +1. `Doctrine\DBAL\Platforms\TrimMode`. + +The corresponding class constants are now instances of their enum type. + ## BC BREAK: renamed SQLite platform classes 1. `SqlitePlatform` => `SQLitePlatform` diff --git a/src/Platforms/AbstractPlatform.php b/src/Platforms/AbstractPlatform.php index c4f54f2134e..5c623f05646 100644 --- a/src/Platforms/AbstractPlatform.php +++ b/src/Platforms/AbstractPlatform.php @@ -418,12 +418,10 @@ public function getModExpression(string $dividend, string $divisor): string * Returns the SQL snippet to trim a string. * * @param string $str The expression to apply the trim to. - * @param int $mode The position of the trim (leading/trailing/both). + * @param TrimMode $mode The position of the trim. * @param string|null $char The char to trim, has to be quoted already. Defaults to space. - * - * @throws InvalidArgumentException */ - public function getTrimExpression(string $str, int $mode = TrimMode::UNSPECIFIED, ?string $char = null): string + public function getTrimExpression(string $str, TrimMode $mode = TrimMode::UNSPECIFIED, ?string $char = null): string { $tokens = []; @@ -442,14 +440,6 @@ public function getTrimExpression(string $str, int $mode = TrimMode::UNSPECIFIED case TrimMode::BOTH: $tokens[] = 'BOTH'; break; - - default: - throw new InvalidArgumentException( - sprintf( - 'The value of $mode is expected to be one of the TrimMode constants, %d given.', - $mode - ) - ); } if ($char !== null) { diff --git a/src/Platforms/SQLServerPlatform.php b/src/Platforms/SQLServerPlatform.php index 416dc84558f..5feea298e4e 100644 --- a/src/Platforms/SQLServerPlatform.php +++ b/src/Platforms/SQLServerPlatform.php @@ -27,7 +27,6 @@ use function dechex; use function explode; use function implode; -use function in_array; use function is_array; use function is_bool; use function is_numeric; @@ -791,14 +790,8 @@ public function getModExpression(string $dividend, string $divisor): string return $dividend . ' % ' . $divisor; } - public function getTrimExpression(string $str, int $mode = TrimMode::UNSPECIFIED, ?string $char = null): string + public function getTrimExpression(string $str, TrimMode $mode = TrimMode::UNSPECIFIED, ?string $char = null): string { - if (! in_array($mode, [TrimMode::UNSPECIFIED, TrimMode::LEADING, TrimMode::TRAILING, TrimMode::BOTH], true)) { - throw new InvalidArgumentException( - sprintf('The value of $mode is expected to be one of the TrimMode constants, %d given', $mode) - ); - } - if ($char === null) { return match ($mode) { TrimMode::LEADING => 'LTRIM(' . $str . ')', diff --git a/src/Platforms/SQLitePlatform.php b/src/Platforms/SQLitePlatform.php index 9f4208b761d..6e15d173b1a 100644 --- a/src/Platforms/SQLitePlatform.php +++ b/src/Platforms/SQLitePlatform.php @@ -19,7 +19,6 @@ use Doctrine\DBAL\Schema\TableDiff; use Doctrine\DBAL\TransactionIsolationLevel; use Doctrine\DBAL\Types; -use InvalidArgumentException; use function array_combine; use function array_keys; @@ -60,19 +59,13 @@ public function getRegexpExpression(): string return 'REGEXP'; } - public function getTrimExpression(string $str, int $mode = TrimMode::UNSPECIFIED, ?string $char = null): string + public function getTrimExpression(string $str, TrimMode $mode = TrimMode::UNSPECIFIED, ?string $char = null): string { $trimFn = match ($mode) { TrimMode::UNSPECIFIED, TrimMode::BOTH => 'TRIM', TrimMode::LEADING => 'LTRIM', TrimMode::TRAILING => 'RTRIM', - default => throw new InvalidArgumentException( - sprintf( - 'The value of $mode is expected to be one of the TrimMode constants, %d given.', - $mode - ) - ), }; $arguments = [$str]; diff --git a/src/Platforms/TrimMode.php b/src/Platforms/TrimMode.php index eb499ee4621..31c237543fc 100644 --- a/src/Platforms/TrimMode.php +++ b/src/Platforms/TrimMode.php @@ -4,20 +4,10 @@ namespace Doctrine\DBAL\Platforms; -final class TrimMode +enum TrimMode { - public const UNSPECIFIED = 0; - - public const LEADING = 1; - - public const TRAILING = 2; - - public const BOTH = 3; - - /** - * @codeCoverageIgnore - */ - private function __construct() - { - } + case UNSPECIFIED; + case LEADING; + case TRAILING; + case BOTH; } diff --git a/tests/Functional/DataAccessTest.php b/tests/Functional/DataAccessTest.php index c85ff547511..c5f27f954df 100644 --- a/tests/Functional/DataAccessTest.php +++ b/tests/Functional/DataAccessTest.php @@ -14,7 +14,6 @@ use Doctrine\DBAL\Statement; use Doctrine\DBAL\Tests\FunctionalTestCase; use Doctrine\DBAL\Types\Types; -use InvalidArgumentException; use function array_change_key_case; use function date; @@ -330,10 +329,10 @@ public function testNativeArrayListSupport(): void /** * @dataProvider getTrimExpressionData */ - public function testTrimExpression(string $value, int $position, ?string $char, string $expectedResult): void + public function testTrimExpression(string $value, TrimMode $mode, ?string $char, string $expectedResult): void { $sql = 'SELECT ' . - $this->connection->getDatabasePlatform()->getTrimExpression($value, $position, $char) . ' AS trimmed ' . + $this->connection->getDatabasePlatform()->getTrimExpression($value, $mode, $char) . ' AS trimmed ' . 'FROM fetch_table'; $row = $this->connection->fetchAssociative($sql); @@ -388,12 +387,6 @@ public static function getTrimExpressionData(): iterable ]; } - public function testTrimExpressionInvalidMode(): void - { - $this->expectException(InvalidArgumentException::class); - $this->connection->getDatabasePlatform()->getTrimExpression('Trim me!', 0xBEEF); - } - /** * @dataProvider modeProvider */ From 17e5a212413fcbbfbabb00be815a7246458ee9ae Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Sat, 30 Jul 2022 09:02:02 -0700 Subject: [PATCH 2/5] Convert ColumnCase to enum --- UPGRADE.md | 3 ++- src/ColumnCase.php | 15 +++------------ src/Portability/Driver.php | 13 ++++++++----- src/Portability/Middleware.php | 3 ++- tests/Functional/PortabilityTest.php | 10 +++++----- 5 files changed, 20 insertions(+), 24 deletions(-) diff --git a/UPGRADE.md b/UPGRADE.md index d6f08670199..af60f9a7485 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -12,7 +12,8 @@ awareness about deprecated code. The following classes have been converted to enums: -1. `Doctrine\DBAL\Platforms\TrimMode`. +1. `Doctrine\DBAL\ColumnCase`, +2. `Doctrine\DBAL\Platforms\TrimMode`. The corresponding class constants are now instances of their enum type. diff --git a/src/ColumnCase.php b/src/ColumnCase.php index e3a19b05a47..687a04f80e6 100644 --- a/src/ColumnCase.php +++ b/src/ColumnCase.php @@ -7,24 +7,15 @@ /** * Contains portable column case conversions. */ -final class ColumnCase +enum ColumnCase { /** * Convert column names to upper case. */ - public const UPPER = 1; + case UPPER; /** * Convert column names to lower case. */ - public const LOWER = 2; - - /** - * This class cannot be instantiated. - * - * @codeCoverageIgnore - */ - private function __construct() - { - } + case LOWER; } diff --git a/src/Portability/Driver.php b/src/Portability/Driver.php index a90c64b0b89..831fd4a514f 100644 --- a/src/Portability/Driver.php +++ b/src/Portability/Driver.php @@ -15,7 +15,7 @@ final class Driver extends AbstractDriverMiddleware { - public function __construct(DriverInterface $driver, private readonly int $mode, private readonly int $case) + public function __construct(DriverInterface $driver, private readonly int $mode, private readonly ?ColumnCase $case) { parent::__construct($driver); } @@ -34,14 +34,17 @@ public function connect(array $params): ConnectionInterface $case = null; - if ($this->case !== 0 && ($portability & Connection::PORTABILITY_FIX_CASE) !== 0) { + if ($this->case !== null && ($portability & Connection::PORTABILITY_FIX_CASE) !== 0) { $nativeConnection = $connection->getNativeConnection(); + $case = match ($this->case) { + ColumnCase::LOWER => CASE_LOWER, + ColumnCase::UPPER => CASE_UPPER, + }; + if ($nativeConnection instanceof PDO) { $portability &= ~Connection::PORTABILITY_FIX_CASE; - $nativeConnection->setAttribute(PDO::ATTR_CASE, $this->case); - } else { - $case = $this->case === ColumnCase::LOWER ? CASE_LOWER : CASE_UPPER; + $nativeConnection->setAttribute(PDO::ATTR_CASE, $case); } } diff --git a/src/Portability/Middleware.php b/src/Portability/Middleware.php index c4053f60162..b97897c978d 100644 --- a/src/Portability/Middleware.php +++ b/src/Portability/Middleware.php @@ -4,12 +4,13 @@ namespace Doctrine\DBAL\Portability; +use Doctrine\DBAL\ColumnCase; use Doctrine\DBAL\Driver as DriverInterface; use Doctrine\DBAL\Driver\Middleware as MiddlewareInterface; final class Middleware implements MiddlewareInterface { - public function __construct(private readonly int $mode, private readonly int $case) + public function __construct(private readonly int $mode, private readonly ?ColumnCase $case) { } diff --git a/tests/Functional/PortabilityTest.php b/tests/Functional/PortabilityTest.php index 9fe1a9c6ed2..985a3d9bd67 100644 --- a/tests/Functional/PortabilityTest.php +++ b/tests/Functional/PortabilityTest.php @@ -52,7 +52,7 @@ public function testFullFetchMode(): void * * @dataProvider caseProvider */ - public function testCaseConversion(int $case, array $expected): void + public function testCaseConversion(ColumnCase $case, array $expected): void { $this->connectWithPortability(Connection::PORTABILITY_FIX_CASE, $case); $this->createTable(); @@ -64,7 +64,7 @@ public function testCaseConversion(int $case, array $expected): void } /** - * @return iterable}> + * @return iterable}> */ public static function caseProvider(): iterable { @@ -106,7 +106,7 @@ public function assertFetchResultRow(array $row): void */ public function testFetchColumn(string $column, array $expected): void { - $this->connectWithPortability(Connection::PORTABILITY_RTRIM, 0); + $this->connectWithPortability(Connection::PORTABILITY_RTRIM, null); $this->createTable(); $result = $this->connection->executeQuery('SELECT ' . $column . ' FROM portability_table'); @@ -133,7 +133,7 @@ public static function fetchColumnProvider(): iterable public function testFetchAllNullColumn(): void { - $this->connectWithPortability(Connection::PORTABILITY_EMPTY_TO_NULL, 0); + $this->connectWithPortability(Connection::PORTABILITY_EMPTY_TO_NULL, null); $this->createTable(); $column = $this->connection->fetchFirstColumn('SELECT Test_Null FROM portability_table'); @@ -141,7 +141,7 @@ public function testFetchAllNullColumn(): void self::assertSame([null, null], $column); } - private function connectWithPortability(int $mode, int $case): void + private function connectWithPortability(int $mode, ?ColumnCase $case): void { // closing the default connection prior to 4.0.0 to prevent connection leak $this->connection->close(); From feded321200410dd27d409b1dadd04c18b6f81ed Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Sat, 30 Jul 2022 09:07:12 -0700 Subject: [PATCH 3/5] Convert LockMode to enum --- UPGRADE.md | 3 ++- src/Exception/InvalidLockMode.php | 26 ------------------- src/LockMode.php | 21 +++++---------- src/Platforms/AbstractPlatform.php | 13 ++-------- src/Platforms/SQLServerPlatform.php | 4 +-- tests/Platforms/AbstractPlatformTestCase.php | 7 ----- tests/Platforms/SQLServerPlatformTestCase.php | 6 ++--- 7 files changed, 13 insertions(+), 67 deletions(-) delete mode 100644 src/Exception/InvalidLockMode.php diff --git a/UPGRADE.md b/UPGRADE.md index af60f9a7485..697722f71bb 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -13,7 +13,8 @@ awareness about deprecated code. The following classes have been converted to enums: 1. `Doctrine\DBAL\ColumnCase`, -2. `Doctrine\DBAL\Platforms\TrimMode`. +2. `Doctrine\DBAL\LockMode`, +3. `Doctrine\DBAL\Platforms\TrimMode`. The corresponding class constants are now instances of their enum type. diff --git a/src/Exception/InvalidLockMode.php b/src/Exception/InvalidLockMode.php deleted file mode 100644 index 64e96201ab7..00000000000 --- a/src/Exception/InvalidLockMode.php +++ /dev/null @@ -1,26 +0,0 @@ - $fromClause, - default => throw InvalidLockMode::fromLockMode($lockMode), - }; + return $fromClause; } /** diff --git a/src/Platforms/SQLServerPlatform.php b/src/Platforms/SQLServerPlatform.php index 5feea298e4e..d07a4c2255e 100644 --- a/src/Platforms/SQLServerPlatform.php +++ b/src/Platforms/SQLServerPlatform.php @@ -6,7 +6,6 @@ use Doctrine\DBAL\Connection; use Doctrine\DBAL\Exception\ColumnLengthRequired; -use Doctrine\DBAL\Exception\InvalidLockMode; use Doctrine\DBAL\LockMode; use Doctrine\DBAL\Platforms\Keywords\KeywordList; use Doctrine\DBAL\Platforms\Keywords\SQLServerKeywords; @@ -1118,14 +1117,13 @@ public function getForeignKeyReferentialActionSQL(string $action): string return parent::getForeignKeyReferentialActionSQL($action); } - public function appendLockHint(string $fromClause, int $lockMode): string + public function appendLockHint(string $fromClause, LockMode $lockMode): string { return match ($lockMode) { LockMode::NONE, LockMode::OPTIMISTIC => $fromClause, LockMode::PESSIMISTIC_READ => $fromClause . ' WITH (HOLDLOCK, ROWLOCK)', LockMode::PESSIMISTIC_WRITE => $fromClause . ' WITH (UPDLOCK, ROWLOCK)', - default => throw InvalidLockMode::fromLockMode($lockMode), }; } diff --git a/tests/Platforms/AbstractPlatformTestCase.php b/tests/Platforms/AbstractPlatformTestCase.php index 250111e858f..e48ca40b4f1 100644 --- a/tests/Platforms/AbstractPlatformTestCase.php +++ b/tests/Platforms/AbstractPlatformTestCase.php @@ -7,7 +7,6 @@ use Doctrine\Common\EventManager; use Doctrine\DBAL\Events; use Doctrine\DBAL\Exception; -use Doctrine\DBAL\Exception\InvalidLockMode; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\ColumnDiff; @@ -1368,12 +1367,6 @@ public function asciiStringSqlDeclarationDataProvider(): array ['CHAR(12)', ['length' => 12, 'fixed' => true]], ]; } - - public function testInvalidLockMode(): void - { - $this->expectException(InvalidLockMode::class); - $this->platform->appendLockHint('TABLE', 128); - } } interface GetCreateTableSqlDispatchEventListener diff --git a/tests/Platforms/SQLServerPlatformTestCase.php b/tests/Platforms/SQLServerPlatformTestCase.php index 5798a1c9358..b2884c505c1 100644 --- a/tests/Platforms/SQLServerPlatformTestCase.php +++ b/tests/Platforms/SQLServerPlatformTestCase.php @@ -1683,11 +1683,9 @@ public function testAlterTableWithSchemaSameColumnComments(): void } /** - * @psalm-param LockMode::* $lockMode - * * @dataProvider getLockHints */ - public function testAppendsLockHint(int $lockMode, string $lockHint): void + public function testAppendsLockHint(LockMode $lockMode, string $lockHint): void { $fromClause = 'FROM users'; $expectedResult = $fromClause . $lockHint; @@ -1696,7 +1694,7 @@ public function testAppendsLockHint(int $lockMode, string $lockHint): void } /** - * @return mixed[][] + * @return iterable */ public static function getLockHints(): iterable { From d9efe0ecac1de6e68e358c68b48b23fee3f1280f Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Sat, 30 Jul 2022 09:32:02 -0700 Subject: [PATCH 4/5] Convert DateIntervalUnit to enum --- UPGRADE.md | 3 ++- src/Platforms/AbstractMySQLPlatform.php | 4 ++-- src/Platforms/AbstractPlatform.php | 12 +++++----- src/Platforms/DB2Platform.php | 4 ++-- src/Platforms/DateIntervalUnit.php | 32 +++++++------------------ src/Platforms/OraclePlatform.php | 2 +- src/Platforms/PostgreSQLPlatform.php | 4 ++-- src/Platforms/SQLServerPlatform.php | 4 ++-- src/Platforms/SQLitePlatform.php | 4 ++-- 9 files changed, 28 insertions(+), 41 deletions(-) diff --git a/UPGRADE.md b/UPGRADE.md index 697722f71bb..e9d68e932aa 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -14,7 +14,8 @@ The following classes have been converted to enums: 1. `Doctrine\DBAL\ColumnCase`, 2. `Doctrine\DBAL\LockMode`, -3. `Doctrine\DBAL\Platforms\TrimMode`. +3. `Doctrine\DBAL\Platforms\DateIntervalUnit`, +4. `Doctrine\DBAL\Platforms\TrimMode`. The corresponding class constants are now instances of their enum type. diff --git a/src/Platforms/AbstractMySQLPlatform.php b/src/Platforms/AbstractMySQLPlatform.php index 3f364b42f5c..4b8b4534e9d 100644 --- a/src/Platforms/AbstractMySQLPlatform.php +++ b/src/Platforms/AbstractMySQLPlatform.php @@ -85,11 +85,11 @@ protected function getDateArithmeticIntervalExpression( string $date, string $operator, string $interval, - string $unit + DateIntervalUnit $unit ): string { $function = $operator === '+' ? 'DATE_ADD' : 'DATE_SUB'; - return $function . '(' . $date . ', INTERVAL ' . $interval . ' ' . $unit . ')'; + return $function . '(' . $date . ', INTERVAL ' . $interval . ' ' . $unit->value . ')'; } public function getDateDiffExpression(string $date1, string $date2): string diff --git a/src/Platforms/AbstractPlatform.php b/src/Platforms/AbstractPlatform.php index 8172e2a78db..03bb9549cdb 100644 --- a/src/Platforms/AbstractPlatform.php +++ b/src/Platforms/AbstractPlatform.php @@ -677,18 +677,18 @@ public function getDateSubYearsExpression(string $date, string $years): string /** * Returns the SQL for a date arithmetic expression. * - * @param string $date SQL expression representing a date to perform the arithmetic operation on. - * @param string $operator The arithmetic operator (+ or -). - * @param string $interval SQL expression representing the value of the interval that shall be calculated - * into the date. - * @param string $unit The unit of the interval that shall be calculated into the date. + * @param string $date SQL expression representing a date to perform the arithmetic operation on. + * @param string $operator The arithmetic operator (+ or -). + * @param string $interval SQL expression representing the value of the interval that shall be calculated + * into the date. + * @param DateIntervalUnit $unit The unit of the interval that shall be calculated into the date. * One of the DATE_INTERVAL_UNIT_* constants. */ abstract protected function getDateArithmeticIntervalExpression( string $date, string $operator, string $interval, - string $unit + DateIntervalUnit $unit ): string; /** diff --git a/src/Platforms/DB2Platform.php b/src/Platforms/DB2Platform.php index 7ee0f41466a..3488a9b5f21 100644 --- a/src/Platforms/DB2Platform.php +++ b/src/Platforms/DB2Platform.php @@ -133,7 +133,7 @@ protected function getDateArithmeticIntervalExpression( string $date, string $operator, string $interval, - string $unit + DateIntervalUnit $unit ): string { switch ($unit) { case DateIntervalUnit::WEEK: @@ -147,7 +147,7 @@ protected function getDateArithmeticIntervalExpression( break; } - return $date . ' ' . $operator . ' ' . $interval . ' ' . $unit; + return $date . ' ' . $operator . ' ' . $interval . ' ' . $unit->value; } public function getDateDiffExpression(string $date1, string $date2): string diff --git a/src/Platforms/DateIntervalUnit.php b/src/Platforms/DateIntervalUnit.php index c1f3ca59218..ba783f37223 100644 --- a/src/Platforms/DateIntervalUnit.php +++ b/src/Platforms/DateIntervalUnit.php @@ -4,28 +4,14 @@ namespace Doctrine\DBAL\Platforms; -final class DateIntervalUnit +enum DateIntervalUnit: string { - public const SECOND = 'SECOND'; - - public const MINUTE = 'MINUTE'; - - public const HOUR = 'HOUR'; - - public const DAY = 'DAY'; - - public const WEEK = 'WEEK'; - - public const MONTH = 'MONTH'; - - public const QUARTER = 'QUARTER'; - - public const YEAR = 'YEAR'; - - /** - * @codeCoverageIgnore - */ - private function __construct() - { - } + case SECOND = 'SECOND'; + case MINUTE = 'MINUTE'; + case HOUR = 'HOUR'; + case DAY = 'DAY'; + case WEEK = 'WEEK'; + case MONTH = 'MONTH'; + case QUARTER = 'QUARTER'; + case YEAR = 'YEAR'; } diff --git a/src/Platforms/OraclePlatform.php b/src/Platforms/OraclePlatform.php index e9ad5b78cfa..bd7ad85d8bf 100644 --- a/src/Platforms/OraclePlatform.php +++ b/src/Platforms/OraclePlatform.php @@ -55,7 +55,7 @@ protected function getDateArithmeticIntervalExpression( string $date, string $operator, string $interval, - string $unit + DateIntervalUnit $unit ): string { switch ($unit) { case DateIntervalUnit::MONTH: diff --git a/src/Platforms/PostgreSQLPlatform.php b/src/Platforms/PostgreSQLPlatform.php index 3610cee8476..c5c9e535a9b 100644 --- a/src/Platforms/PostgreSQLPlatform.php +++ b/src/Platforms/PostgreSQLPlatform.php @@ -94,14 +94,14 @@ protected function getDateArithmeticIntervalExpression( string $date, string $operator, string $interval, - string $unit + DateIntervalUnit $unit ): string { if ($unit === DateIntervalUnit::QUARTER) { $interval = $this->multiplyInterval($interval, 3); $unit = DateIntervalUnit::MONTH; } - return '(' . $date . ' ' . $operator . ' (' . $interval . " || ' " . $unit . "')::interval)"; + return '(' . $date . ' ' . $operator . ' (' . $interval . " || ' " . $unit->value . "')::interval)"; } public function getDateDiffExpression(string $date1, string $date2): string diff --git a/src/Platforms/SQLServerPlatform.php b/src/Platforms/SQLServerPlatform.php index d07a4c2255e..6a2f68c6f7c 100644 --- a/src/Platforms/SQLServerPlatform.php +++ b/src/Platforms/SQLServerPlatform.php @@ -70,7 +70,7 @@ protected function getDateArithmeticIntervalExpression( string $date, string $operator, string $interval, - string $unit + DateIntervalUnit $unit ): string { $factorClause = ''; @@ -78,7 +78,7 @@ protected function getDateArithmeticIntervalExpression( $factorClause = '-1 * '; } - return 'DATEADD(' . $unit . ', ' . $factorClause . $interval . ', ' . $date . ')'; + return 'DATEADD(' . $unit->value . ', ' . $factorClause . $interval . ', ' . $date . ')'; } public function getDateDiffExpression(string $date1, string $date2): string diff --git a/src/Platforms/SQLitePlatform.php b/src/Platforms/SQLitePlatform.php index 6e15d173b1a..18880c5cd93 100644 --- a/src/Platforms/SQLitePlatform.php +++ b/src/Platforms/SQLitePlatform.php @@ -99,7 +99,7 @@ protected function getDateArithmeticIntervalExpression( string $date, string $operator, string $interval, - string $unit + DateIntervalUnit $unit ): string { switch ($unit) { case DateIntervalUnit::WEEK: @@ -116,7 +116,7 @@ protected function getDateArithmeticIntervalExpression( return 'DATETIME(' . $date . ',' . $this->getConcatExpression( $this->quoteStringLiteral($operator), $interval, - $this->quoteStringLiteral(' ' . $unit) + $this->quoteStringLiteral(' ' . $unit->value) ) . ')'; } From d34d5a92629cb691a57c86e9e860023e79ff07fd Mon Sep 17 00:00:00 2001 From: Sergei Morozov Date: Sat, 30 Jul 2022 09:39:29 -0700 Subject: [PATCH 5/5] Convert TransactionIsolationLevel to enum --- UPGRADE.md | 5 ++-- src/Connection.php | 12 ++++------ src/Platforms/AbstractMySQLPlatform.php | 4 ++-- src/Platforms/AbstractPlatform.php | 13 ++++------ src/Platforms/DB2Platform.php | 3 ++- src/Platforms/OraclePlatform.php | 8 +++---- src/Platforms/PostgreSQLPlatform.php | 3 ++- src/Platforms/SQLServerPlatform.php | 3 ++- src/Platforms/SQLitePlatform.php | 5 ++-- src/TransactionIsolationLevel.php | 32 ++++--------------------- 10 files changed, 31 insertions(+), 57 deletions(-) diff --git a/UPGRADE.md b/UPGRADE.md index e9d68e932aa..1c1c17fb04e 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -14,8 +14,9 @@ The following classes have been converted to enums: 1. `Doctrine\DBAL\ColumnCase`, 2. `Doctrine\DBAL\LockMode`, -3. `Doctrine\DBAL\Platforms\DateIntervalUnit`, -4. `Doctrine\DBAL\Platforms\TrimMode`. +3. `Doctrine\DBAL\TransactionIsolationLevel`, +4. `Doctrine\DBAL\Platforms\DateIntervalUnit`, +5. `Doctrine\DBAL\Platforms\TrimMode`. The corresponding class constants are now instances of their enum type. diff --git a/src/Connection.php b/src/Connection.php index faebaeeb2f6..7c313068ae6 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -96,10 +96,8 @@ class Connection implements ServerVersionProvider /** * The currently active transaction isolation level or NULL before it has been determined. - * - * @var TransactionIsolationLevel::*|null */ - private ?int $transactionIsolationLevel = null; + private ?TransactionIsolationLevel $transactionIsolationLevel = null; /** * The parameters used during creation of the Connection instance. @@ -501,11 +499,11 @@ public function close(): void /** * Sets the transaction isolation level. * - * @param TransactionIsolationLevel::* $level The level to set. + * @param TransactionIsolationLevel $level The level to set. * * @throws Exception */ - public function setTransactionIsolation(int $level): void + public function setTransactionIsolation(TransactionIsolationLevel $level): void { $this->transactionIsolationLevel = $level; @@ -515,11 +513,11 @@ public function setTransactionIsolation(int $level): void /** * Gets the currently active transaction isolation level. * - * @return TransactionIsolationLevel::* The current transaction isolation level. + * @return TransactionIsolationLevel The current transaction isolation level. * * @throws Exception */ - public function getTransactionIsolation(): int + public function getTransactionIsolation(): TransactionIsolationLevel { return $this->transactionIsolationLevel ??= $this->getDatabasePlatform()->getDefaultTransactionIsolationLevel(); } diff --git a/src/Platforms/AbstractMySQLPlatform.php b/src/Platforms/AbstractMySQLPlatform.php index 4b8b4534e9d..30c25805c07 100644 --- a/src/Platforms/AbstractMySQLPlatform.php +++ b/src/Platforms/AbstractMySQLPlatform.php @@ -677,7 +677,7 @@ public function getDropUniqueConstraintSQL(string $name, string $tableName): str return $this->getDropIndexSQL($name, $tableName); } - public function getSetTransactionIsolationSQL(int $level): string + public function getSetTransactionIsolationSQL(TransactionIsolationLevel $level): string { return 'SET SESSION TRANSACTION ISOLATION LEVEL ' . $this->_getTransactionIsolationLevelSQL($level); } @@ -778,7 +778,7 @@ public function quoteStringLiteral(string $str): string return parent::quoteStringLiteral($str); } - public function getDefaultTransactionIsolationLevel(): int + public function getDefaultTransactionIsolationLevel(): TransactionIsolationLevel { return TransactionIsolationLevel::REPEATABLE_READ; } diff --git a/src/Platforms/AbstractPlatform.php b/src/Platforms/AbstractPlatform.php index 03bb9549cdb..9afde034342 100644 --- a/src/Platforms/AbstractPlatform.php +++ b/src/Platforms/AbstractPlatform.php @@ -1973,17 +1973,14 @@ public function getCurrentTimestampSQL(): string /** * Returns the SQL for a given transaction isolation level Connection constant. - * - * @throws InvalidArgumentException */ - protected function _getTransactionIsolationLevelSQL(int $level): string + protected function _getTransactionIsolationLevelSQL(TransactionIsolationLevel $level): string { return match ($level) { TransactionIsolationLevel::READ_UNCOMMITTED => 'READ UNCOMMITTED', TransactionIsolationLevel::READ_COMMITTED => 'READ COMMITTED', TransactionIsolationLevel::REPEATABLE_READ => 'REPEATABLE READ', TransactionIsolationLevel::SERIALIZABLE => 'SERIALIZABLE', - default => throw new InvalidArgumentException(sprintf('Invalid isolation level "%s".', $level)), }; } @@ -2049,7 +2046,7 @@ public function getDropDatabaseSQL(string $name): string /** * Returns the SQL to set the transaction isolation level. */ - abstract public function getSetTransactionIsolationSQL(int $level): string; + abstract public function getSetTransactionIsolationSQL(TransactionIsolationLevel $level): string; /** * Obtains DBMS specific SQL to be used to create datetime columns in @@ -2096,11 +2093,9 @@ public function getFloatDeclarationSQL(array $column): string /** * Gets the default transaction isolation level of the platform. * - * @see TransactionIsolationLevel - * - * @return TransactionIsolationLevel::* The default isolation level. + * @return TransactionIsolationLevel The default isolation level. */ - public function getDefaultTransactionIsolationLevel(): int + public function getDefaultTransactionIsolationLevel(): TransactionIsolationLevel { return TransactionIsolationLevel::READ_COMMITTED; } diff --git a/src/Platforms/DB2Platform.php b/src/Platforms/DB2Platform.php index 3488a9b5f21..c0ac78b6ea4 100644 --- a/src/Platforms/DB2Platform.php +++ b/src/Platforms/DB2Platform.php @@ -14,6 +14,7 @@ use Doctrine\DBAL\Schema\Identifier; use Doctrine\DBAL\Schema\Index; use Doctrine\DBAL\Schema\TableDiff; +use Doctrine\DBAL\TransactionIsolationLevel; use function array_merge; use function count; @@ -195,7 +196,7 @@ public function getTruncateTableSQL(string $tableName, bool $cascade = false): s * * @throws Exception */ - public function getSetTransactionIsolationSQL(int $level): string + public function getSetTransactionIsolationSQL(TransactionIsolationLevel $level): string { throw NotSupported::new(__METHOD__); } diff --git a/src/Platforms/OraclePlatform.php b/src/Platforms/OraclePlatform.php index bd7ad85d8bf..acb0b00cd31 100644 --- a/src/Platforms/OraclePlatform.php +++ b/src/Platforms/OraclePlatform.php @@ -174,18 +174,18 @@ public function getSequenceNextValSQL(string $sequence): string return 'SELECT ' . $sequence . '.nextval FROM DUAL'; } - public function getSetTransactionIsolationSQL(int $level): string + public function getSetTransactionIsolationSQL(TransactionIsolationLevel $level): string { return 'SET TRANSACTION ISOLATION LEVEL ' . $this->_getTransactionIsolationLevelSQL($level); } - protected function _getTransactionIsolationLevelSQL(int $level): string + protected function _getTransactionIsolationLevelSQL(TransactionIsolationLevel $level): string { return match ($level) { TransactionIsolationLevel::READ_UNCOMMITTED => 'READ UNCOMMITTED', TransactionIsolationLevel::READ_COMMITTED => 'READ COMMITTED', - TransactionIsolationLevel::REPEATABLE_READ, TransactionIsolationLevel::SERIALIZABLE => 'SERIALIZABLE', - default => parent::_getTransactionIsolationLevelSQL($level), + TransactionIsolationLevel::REPEATABLE_READ, + TransactionIsolationLevel::SERIALIZABLE => 'SERIALIZABLE', }; } diff --git a/src/Platforms/PostgreSQLPlatform.php b/src/Platforms/PostgreSQLPlatform.php index c5c9e535a9b..b1740a77e1f 100644 --- a/src/Platforms/PostgreSQLPlatform.php +++ b/src/Platforms/PostgreSQLPlatform.php @@ -14,6 +14,7 @@ use Doctrine\DBAL\Schema\PostgreSQLSchemaManager; use Doctrine\DBAL\Schema\Sequence; use Doctrine\DBAL\Schema\TableDiff; +use Doctrine\DBAL\TransactionIsolationLevel; use Doctrine\DBAL\Types\BinaryType; use Doctrine\DBAL\Types\BlobType; use UnexpectedValueException; @@ -608,7 +609,7 @@ public function getSequenceNextValSQL(string $sequence): string return "SELECT NEXTVAL('" . $sequence . "')"; } - public function getSetTransactionIsolationSQL(int $level): string + public function getSetTransactionIsolationSQL(TransactionIsolationLevel $level): string { return 'SET SESSION CHARACTERISTICS AS TRANSACTION ISOLATION LEVEL ' . $this->_getTransactionIsolationLevelSQL($level); diff --git a/src/Platforms/SQLServerPlatform.php b/src/Platforms/SQLServerPlatform.php index 6a2f68c6f7c..f858e6d580e 100644 --- a/src/Platforms/SQLServerPlatform.php +++ b/src/Platforms/SQLServerPlatform.php @@ -16,6 +16,7 @@ use Doctrine\DBAL\Schema\Sequence; use Doctrine\DBAL\Schema\SQLServerSchemaManager; use Doctrine\DBAL\Schema\TableDiff; +use Doctrine\DBAL\TransactionIsolationLevel; use InvalidArgumentException; use function array_merge; @@ -844,7 +845,7 @@ public function getCurrentDatabaseExpression(): string return 'DB_NAME()'; } - public function getSetTransactionIsolationSQL(int $level): string + public function getSetTransactionIsolationSQL(TransactionIsolationLevel $level): string { return 'SET TRANSACTION ISOLATION LEVEL ' . $this->_getTransactionIsolationLevelSQL($level); } diff --git a/src/Platforms/SQLitePlatform.php b/src/Platforms/SQLitePlatform.php index 18880c5cd93..c962ef8c41c 100644 --- a/src/Platforms/SQLitePlatform.php +++ b/src/Platforms/SQLitePlatform.php @@ -138,18 +138,17 @@ public function getCurrentDatabaseExpression(): string return "''"; } - protected function _getTransactionIsolationLevelSQL(int $level): string + protected function _getTransactionIsolationLevelSQL(TransactionIsolationLevel $level): string { return match ($level) { TransactionIsolationLevel::READ_UNCOMMITTED => '0', TransactionIsolationLevel::READ_COMMITTED, TransactionIsolationLevel::REPEATABLE_READ, TransactionIsolationLevel::SERIALIZABLE => '1', - default => parent::_getTransactionIsolationLevelSQL($level), }; } - public function getSetTransactionIsolationSQL(int $level): string + public function getSetTransactionIsolationSQL(TransactionIsolationLevel $level): string { return 'PRAGMA read_uncommitted = ' . $this->_getTransactionIsolationLevelSQL($level); } diff --git a/src/TransactionIsolationLevel.php b/src/TransactionIsolationLevel.php index e8dc5d91814..2b094dbe84e 100644 --- a/src/TransactionIsolationLevel.php +++ b/src/TransactionIsolationLevel.php @@ -4,32 +4,10 @@ namespace Doctrine\DBAL; -final class TransactionIsolationLevel +enum TransactionIsolationLevel { - /** - * Transaction isolation level READ UNCOMMITTED. - */ - public const READ_UNCOMMITTED = 1; - - /** - * Transaction isolation level READ COMMITTED. - */ - public const READ_COMMITTED = 2; - - /** - * Transaction isolation level REPEATABLE READ. - */ - public const REPEATABLE_READ = 3; - - /** - * Transaction isolation level SERIALIZABLE. - */ - public const SERIALIZABLE = 4; - - /** - * @codeCoverageIgnore - */ - private function __construct() - { - } + case READ_UNCOMMITTED; + case READ_COMMITTED; + case REPEATABLE_READ; + case SERIALIZABLE; }