Skip to content

Commit

Permalink
Merge pull request #7 from CarbonPHP/feature/maximum-per-platform
Browse files Browse the repository at this point in the history
Cap precision depending on maximum per platform
  • Loading branch information
kylekatarnls authored Dec 10, 2023
2 parents 49856fb + 1cb72be commit a31d335
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
composer.lock
composer.phar
/vendor/
Expand Down
26 changes: 25 additions & 1 deletion src/Carbon/Doctrine/CarbonTypeConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
use Carbon\CarbonInterface;
use DateTimeInterface;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\DB2Platform;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Platforms\SQLitePlatform;
use Doctrine\DBAL\Platforms\SQLServerPlatform;
use Doctrine\DBAL\Types\Exception\InvalidType;
use Doctrine\DBAL\Types\Exception\ValueNotConvertible;
use Exception;
Expand Down Expand Up @@ -35,7 +39,10 @@ protected function getCarbonClassName(): string

public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform): string
{
$precision = $fieldDeclaration['precision'] ?? DateTimeDefaultPrecision::get();
$precision = min(
$fieldDeclaration['precision'] ?? DateTimeDefaultPrecision::get(),
$this->getMaximumPrecision($platform),
);

$type = parent::getSQLDeclaration($fieldDeclaration, $platform);

Expand Down Expand Up @@ -104,4 +111,21 @@ private function doConvertToPHPValue(mixed $value)

return $date;
}

private function getMaximumPrecision(AbstractPlatform $platform): int
{
if ($platform instanceof DB2Platform) {
return 12;
}

if ($platform instanceof OraclePlatform) {
return 9;
}

if ($platform instanceof SQLServerPlatform || $platform instanceof SQLitePlatform) {
return 3;
}

return 6;
}
}
40 changes: 37 additions & 3 deletions tests/Doctrine/CarbonImmutableTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
use Carbon\CarbonImmutable;
use Carbon\Doctrine\CarbonImmutableType;
use DateTimeImmutable;
use Doctrine\DBAL\Platforms\DB2Platform;
use Doctrine\DBAL\Platforms\MySQLPlatform;
use Doctrine\DBAL\Platforms\OraclePlatform;
use Doctrine\DBAL\Platforms\SQLitePlatform;
use Doctrine\DBAL\Types\Type;
use PHPUnit\Framework\TestCase;

Expand All @@ -24,9 +27,7 @@ protected function setUp(): void

public function testCarbonImmutableType(): void
{
Type::addType('carbon_immutable', CarbonImmutableType::class);
/** @var CarbonImmutableType $type */
$type = Type::getType('carbon_immutable');
$type = self::getType();
$platformClass = class_exists(MySQLPlatform::class)
? MySQLPlatform::class
: \Doctrine\DBAL\Platforms\MySqlPlatform::class;
Expand All @@ -49,4 +50,37 @@ public function testCarbonImmutableType(): void
self::assertInstanceOf(CarbonImmutable::class, $date);
self::assertSame('2000-01-01 12:00:00 UTC', $date->format('Y-m-d H:i:s e'));
}

public function testPrecisionPerPlatform(): void
{
$type = self::getType();

self::assertSame('DATETIME(3)', $type->getSQLDeclaration([
'precision' => 99,
], new SQLitePlatform()));

self::assertSame('DATETIME(6)', $type->getSQLDeclaration([
'precision' => 99,
], new MySQLPlatform()));

self::assertSame('TIMESTAMP(9)', $type->getSQLDeclaration([
'precision' => 99,
], new OraclePlatform()));

self::assertSame('TIMESTAMP(12)', $type->getSQLDeclaration([
'precision' => 99,
], new DB2Platform()));
}

private static function getType(): CarbonImmutableType
{
if (!Type::hasType('carbon_immutable')) {
Type::addType('carbon_immutable', CarbonImmutableType::class);
}

/** @var CarbonImmutableType $type */
$type = Type::getType('carbon_immutable');

return $type;
}
}

0 comments on commit a31d335

Please sign in to comment.