diff --git a/src/Platforms/PostgreSQLPlatform.php b/src/Platforms/PostgreSQLPlatform.php index 3364838488b..e5a953247d7 100644 --- a/src/Platforms/PostgreSQLPlatform.php +++ b/src/Platforms/PostgreSQLPlatform.php @@ -1088,7 +1088,14 @@ public function getGuidTypeDeclarationSQL(array $column) */ public function getDateTimeTypeDeclarationSQL(array $column) { - return 'TIMESTAMP(0) WITHOUT TIME ZONE'; + $precision = 0; + if (isset($column['precision'])) { + if ($column['precision'] >= 0 && $column['precision'] <= 6) { + $precision = $column['precision']; + } + } + + return sprintf('TIMESTAMP(%d) WITHOUT TIME ZONE', $precision); } /** @@ -1096,7 +1103,14 @@ public function getDateTimeTypeDeclarationSQL(array $column) */ public function getDateTimeTzTypeDeclarationSQL(array $column) { - return 'TIMESTAMP(0) WITH TIME ZONE'; + $precision = 0; + if (isset($column['precision'])) { + if ($column['precision'] >= 0 && $column['precision'] <= 6) { + $precision = $column['precision']; + } + } + + return sprintf('TIMESTAMP(%d) WITH TIME ZONE', $precision); } /** @@ -1112,7 +1126,14 @@ public function getDateTypeDeclarationSQL(array $column) */ public function getTimeTypeDeclarationSQL(array $column) { - return 'TIME(0) WITHOUT TIME ZONE'; + $precision = 0; + if (isset($column['precision'])) { + if ($column['precision'] >= 0 && $column['precision'] <= 6) { + $precision = $column['precision']; + } + } + + return sprintf('TIME(%d) WITHOUT TIME ZONE', $precision); } /** @@ -1167,7 +1188,15 @@ public function getName() */ public function getDateTimeTzFormatString() { - return 'Y-m-d H:i:sO'; + return 'Y-m-d H:i:s.uO'; + } + + /** + * {@inheritDoc} + */ + public function getDateTimeFormatString() + { + return 'Y-m-d H:i:s.u'; } /** diff --git a/tests/Platforms/PostgreSQLPlatformTest.php b/tests/Platforms/PostgreSQLPlatformTest.php index 614eb45f8f7..45bd32ac7a8 100644 --- a/tests/Platforms/PostgreSQLPlatformTest.php +++ b/tests/Platforms/PostgreSQLPlatformTest.php @@ -2,6 +2,7 @@ namespace Doctrine\DBAL\Tests\Platforms; +use Doctrine\DBAL\Exception; use Doctrine\DBAL\Platforms\AbstractPlatform; use Doctrine\DBAL\Platforms\PostgreSQLPlatform; use Doctrine\DBAL\Schema\Column; @@ -14,6 +15,7 @@ use Doctrine\DBAL\TransactionIsolationLevel; use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Types; +use Generator; use UnexpectedValueException; use function sprintf; @@ -1069,4 +1071,66 @@ public function testInitializesJsonTypeMapping(): void self::assertTrue($this->platform->hasDoctrineTypeMappingFor('jsonb')); self::assertEquals(Types::JSON, $this->platform->getDoctrineTypeMapping('jsonb')); } + + /** + * @param array{precision?: int} $column + * + * @dataProvider dataProviderGetDateTimeTz + */ + public function testGetDateTimeTzTypeDeclarationSQL(string $assert, array $column): void + { + $this->assertSame($assert, $this->platform->getDateTimeTzTypeDeclarationSQL($column)); + } + + public function dataProviderGetDateTimeTz(): Generator + { + yield ['TIMESTAMP(0) WITH TIME ZONE', []]; + yield ['TIMESTAMP(0) WITH TIME ZONE', ['precision' => -5]]; + yield ['TIMESTAMP(0) WITH TIME ZONE', ['precision' => 15]]; + yield ['TIMESTAMP(0) WITH TIME ZONE', ['precision' => 0]]; + yield ['TIMESTAMP(3) WITH TIME ZONE', ['precision' => 3]]; + yield ['TIMESTAMP(6) WITH TIME ZONE', ['precision' => 6]]; + } + + /** + * @param array{precision?: int} $column + * + * @dataProvider dataProviderGetDateTime + */ + public function testGetDateTimeTypeDeclarationSQL(string $assert, array $column): void + { + $this->assertSame($assert, $this->platform->getDateTimeTypeDeclarationSQL($column)); + } + + public function dataProviderGetDateTime(): Generator + { + yield ['TIMESTAMP(0) WITHOUT TIME ZONE', []]; + yield ['TIMESTAMP(0) WITHOUT TIME ZONE', ['precision' => -5]]; + yield ['TIMESTAMP(0) WITHOUT TIME ZONE', ['precision' => 15]]; + yield ['TIMESTAMP(0) WITHOUT TIME ZONE', ['precision' => 0]]; + yield ['TIMESTAMP(3) WITHOUT TIME ZONE', ['precision' => 3]]; + yield ['TIMESTAMP(6) WITHOUT TIME ZONE', ['precision' => 6]]; + } + + /** + * @param array{precision?: int} $column + * + * @throws Exception + * + * @dataProvider dataProviderGetTime + */ + public function testFetTimeTypeDeclarationSQL(string $assert, array $column): void + { + $this->assertSame($assert, $this->platform->getTimeTypeDeclarationSQL($column)); + } + + public function dataProviderGetTime(): Generator + { + yield ['TIME(0) WITHOUT TIME ZONE', []]; + yield ['TIME(0) WITHOUT TIME ZONE', ['precision' => -5]]; + yield ['TIME(0) WITHOUT TIME ZONE', ['precision' => 15]]; + yield ['TIME(0) WITHOUT TIME ZONE', ['precision' => 0]]; + yield ['TIME(3) WITHOUT TIME ZONE', ['precision' => 3]]; + yield ['TIME(6) WITHOUT TIME ZONE', ['precision' => 6]]; + } }