Skip to content

Commit

Permalink
feat: add time and datetime precision
Browse files Browse the repository at this point in the history
  • Loading branch information
vencakrecl committed Jun 13, 2024
1 parent 0e3536b commit 36992cd
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src/Platforms/PostgreSQLPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -1088,15 +1088,29 @@ 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);
}

/**
* {@inheritDoc}
*/
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);
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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';

Check warning on line 1191 in src/Platforms/PostgreSQLPlatform.php

View check run for this annotation

Codecov / codecov/patch

src/Platforms/PostgreSQLPlatform.php#L1191

Added line #L1191 was not covered by tests
}

/**
* {@inheritDoc}
*/
public function getDateTimeFormatString()

Check warning on line 1197 in src/Platforms/PostgreSQLPlatform.php

View check run for this annotation

Codecov / codecov/patch

src/Platforms/PostgreSQLPlatform.php#L1197

Added line #L1197 was not covered by tests
{
return 'Y-m-d H:i:s.u';

Check warning on line 1199 in src/Platforms/PostgreSQLPlatform.php

View check run for this annotation

Codecov / codecov/patch

src/Platforms/PostgreSQLPlatform.php#L1199

Added line #L1199 was not covered by tests
}

/**
Expand Down
64 changes: 64 additions & 0 deletions tests/Platforms/PostgreSQLPlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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]];
}
}

0 comments on commit 36992cd

Please sign in to comment.