Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SmallFloat type #6471

Merged
merged 9 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/Platforms/AbstractMySQLPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,14 @@ public function getFloatDeclarationSQL(array $column): string
return 'DOUBLE PRECISION' . $this->getUnsignedDeclaration($column);
}

/**
* {@inheritDoc}
*/
public function getRealFloatDeclarationSQL(array $column): string
{
return 'FLOAT' . $this->getUnsignedDeclaration($column);
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -727,7 +735,7 @@ protected function initializeDoctrineTypeMappings(): void
'datetime' => Types::DATETIME_MUTABLE,
'decimal' => Types::DECIMAL,
'double' => Types::FLOAT,
'float' => Types::FLOAT,
'float' => Types::REAL,
'int' => Types::INTEGER,
'integer' => Types::INTEGER,
'json' => Types::JSON,
Expand Down
6 changes: 6 additions & 0 deletions src/Platforms/AbstractPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -1877,6 +1877,12 @@ public function getFloatDeclarationSQL(array $column): string
return 'DOUBLE PRECISION';
}

/** @param mixed[] $column */
public function getRealFloatDeclarationSQL(array $column): string
{
return 'REAL';
}

/**
* Gets the default transaction isolation level of the platform.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Platforms/DB2Platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected function initializeDoctrineTypeMappings(): void
'decimal' => Types::DECIMAL,
'double' => Types::FLOAT,
'integer' => Types::INTEGER,
'real' => Types::FLOAT,
'real' => Types::REAL,
'smallint' => Types::SMALLINT,
'time' => Types::TIME_MUTABLE,
'timestamp' => Types::DATETIME_MUTABLE,
Expand Down
1 change: 1 addition & 0 deletions src/Platforms/OraclePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,7 @@ protected function initializeDoctrineTypeMappings(): void
'nvarchar2' => Types::STRING,
'pls_integer' => Types::BOOLEAN,
'raw' => Types::BINARY,
'real' => Types::REAL,
'rowid' => Types::STRING,
'timestamp' => Types::DATETIME_MUTABLE,
'timestamptz' => Types::DATETIMETZ_MUTABLE,
Expand Down
4 changes: 2 additions & 2 deletions src/Platforms/PostgreSQLPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,7 @@ protected function initializeDoctrineTypeMappings(): void
'double' => Types::FLOAT,
'double precision' => Types::FLOAT,
'float' => Types::FLOAT,
'float4' => Types::FLOAT,
'float4' => Types::REAL,
'float8' => Types::FLOAT,
'inet' => Types::STRING,
'int' => Types::INTEGER,
Expand All @@ -717,7 +717,7 @@ protected function initializeDoctrineTypeMappings(): void
'serial' => Types::INTEGER,
'serial4' => Types::INTEGER,
'serial8' => Types::BIGINT,
'real' => Types::FLOAT,
'real' => Types::REAL,
'smallint' => Types::SMALLINT,
'text' => Types::TEXT,
'time' => Types::TIME_MUTABLE,
Expand Down
2 changes: 1 addition & 1 deletion src/Platforms/SQLServerPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,7 @@ protected function initializeDoctrineTypeMappings(): void
'ntext' => Types::TEXT,
'numeric' => Types::DECIMAL,
'nvarchar' => Types::STRING,
'real' => Types::FLOAT,
'real' => Types::REAL,
'smalldatetime' => Types::DATETIME_MUTABLE,
'smallint' => Types::SMALLINT,
'smallmoney' => Types::INTEGER,
Expand Down
2 changes: 1 addition & 1 deletion src/Platforms/SQLitePlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ protected function initializeDoctrineTypeMappings(): void
'ntext' => 'string',
'numeric' => 'decimal',
'nvarchar' => 'string',
'real' => 'float',
'real' => 'real',
'serial' => 'integer',
'smallint' => 'smallint',
'string' => 'string',
Expand Down
7 changes: 7 additions & 0 deletions src/Schema/OracleSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@

break;

case 'float':
if ($precision === 63) {
$type = 'real';

Check warning on line 155 in src/Schema/OracleSchemaManager.php

View check run for this annotation

Codecov / codecov/patch

src/Schema/OracleSchemaManager.php#L153-L155

Added lines #L153 - L155 were not covered by tests
}

break;

Check warning on line 158 in src/Schema/OracleSchemaManager.php

View check run for this annotation

Codecov / codecov/patch

src/Schema/OracleSchemaManager.php#L158

Added line #L158 was not covered by tests

case 'varchar':
case 'varchar2':
case 'nvarchar2':
Expand Down
30 changes: 30 additions & 0 deletions src/Types/RealFloatType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Types;

use Doctrine\DBAL\Platforms\AbstractPlatform;

final class RealFloatType extends Type
berkut1 marked this conversation as resolved.
Show resolved Hide resolved
{
/**
* {@inheritDoc}
*/
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
return $platform->getRealFloatDeclarationSQL($column);
}

/**
* @param T $value
*
* @return (T is null ? null : float)
*
* @template T
*/
public function convertToPHPValue(mixed $value, AbstractPlatform $platform): ?float
{
return $value === null ? null : (float) $value;
}
}
1 change: 1 addition & 0 deletions src/Types/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ abstract class Type
Types::GUID => GuidType::class,
Types::INTEGER => IntegerType::class,
Types::JSON => JsonType::class,
Types::REAL => RealFloatType::class,
Types::SIMPLE_ARRAY => SimpleArrayType::class,
Types::SMALLINT => SmallIntType::class,
Types::STRING => StringType::class,
Expand Down
1 change: 1 addition & 0 deletions src/Types/Types.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ final class Types
public const GUID = 'guid';
public const INTEGER = 'integer';
public const JSON = 'json';
public const REAL = 'real';
public const SIMPLE_ARRAY = 'simple_array';
public const SMALLINT = 'smallint';
public const STRING = 'string';
Expand Down
6 changes: 4 additions & 2 deletions tests/Functional/Driver/PgSQL/ResultTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,10 @@ public static function typedValueProvider(): Generator
yield 'text' => ['TEXT', 'some value', Types::STRING];
yield 'boolean true' => ['BOOLEAN', true, Types::BOOLEAN];
yield 'boolean false' => ['BOOLEAN', false, Types::BOOLEAN];
yield 'float' => ['REAL', 47.11, Types::FLOAT];
yield 'negative float with exponent' => ['REAL', -8.15e10, Types::FLOAT];
yield 'float' => ['DOUBLE PRECISION', 47.11, Types::FLOAT];
yield 'real' => ['REAL', 47.11, Types::REAL];
yield 'negative float with exponent' => ['DOUBLE PRECISION', -8.15e10, Types::FLOAT];
yield 'negative real with exponent' => ['REAL', -8.15e5, Types::REAL];
yield 'double' => ['DOUBLE PRECISION', 47.11, Types::FLOAT];
yield 'decimal' => ['NUMERIC (6, 2)', '47.11', Types::DECIMAL];
yield 'binary' => ['BYTEA', chr(0x8b), Types::BINARY];
Expand Down
18 changes: 18 additions & 0 deletions tests/Functional/Schema/MySQLSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,24 @@ public function testListFloatTypeColumns(): void
self::assertTrue($columns['col_unsigned']->getUnsigned());
}

public function testListRealFloatTypeColumns(): void
{
$tableName = 'test_list_real_columns';
$table = new Table($tableName);

$table->addColumn('col', Types::REAL);
$table->addColumn('col_unsigned', Types::REAL, ['unsigned' => true]);

$this->dropAndCreateTable($table);

$columns = $this->schemaManager->listTableColumns($tableName);

self::assertArrayHasKey('col', $columns);
self::assertArrayHasKey('col_unsigned', $columns);
self::assertFalse($columns['col']->getUnsigned());
self::assertTrue($columns['col_unsigned']->getUnsigned());
}

public function testJsonColumnType(): void
{
$table = new Table('test_mysql_json');
Expand Down
16 changes: 16 additions & 0 deletions tests/Functional/Schema/OracleSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Doctrine\DBAL\Types\DateTimeType;
use Doctrine\DBAL\Types\DateTimeTzType;
use Doctrine\DBAL\Types\DateType;
use Doctrine\DBAL\Types\FloatType;
use Doctrine\DBAL\Types\RealFloatType;
use Doctrine\DBAL\Types\Types;

use function array_map;
Expand Down Expand Up @@ -241,6 +243,20 @@ public function testListTableDateTypeColumns(): void
self::assertInstanceOf(DateTimeTzType::class, $columns['col_datetimetz']->getType());
}

public function testListTableFloatTypeColumns(): void
{
$table = new Table('tbl_float');
$table->addColumn('col_float', Types::FLOAT);
$table->addColumn('col_real_float', Types::REAL);

$this->dropAndCreateTable($table);

$columns = $this->schemaManager->listTableColumns('tbl_float');

self::assertInstanceOf(FloatType::class, $columns['col_float']->getType());
self::assertInstanceOf(RealFloatType::class, $columns['col_real_float']->getType());
}
berkut1 marked this conversation as resolved.
Show resolved Hide resolved

public function testCreateAndListSequences(): void
{
self::markTestSkipped(
Expand Down
2 changes: 2 additions & 0 deletions tests/Functional/Schema/PostgreSQLSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ public function testListNegativeColumnDefaultValue(): void
$table->addColumn('col_integer', Types::INTEGER, ['default' => -1]);
$table->addColumn('col_bigint', Types::BIGINT, ['default' => -1]);
$table->addColumn('col_float', Types::FLOAT, ['default' => -1.1]);
$table->addColumn('col_real', Types::REAL, ['default' => -1.1]);
$table->addColumn('col_decimal', Types::DECIMAL, [
'precision' => 2,
'scale' => 1,
Expand All @@ -430,6 +431,7 @@ public function testListNegativeColumnDefaultValue(): void
self::assertEquals(-1, $columns['col_integer']->getDefault());
self::assertEquals(-1, $columns['col_bigint']->getDefault());
self::assertEquals(-1.1, $columns['col_float']->getDefault());
self::assertEquals(-1.1, $columns['col_real']->getDefault());
self::assertEquals(-1.1, $columns['col_decimal']->getDefault());
self::assertEquals('(-1)', $columns['col_string']->getDefault());
}
Expand Down
18 changes: 18 additions & 0 deletions tests/Functional/TypeConversionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ protected function setUp(): void
$table->addColumn('test_text', Types::TEXT, ['notnull' => false]);
$table->addColumn('test_json', Types::JSON, ['notnull' => false]);
$table->addColumn('test_float', Types::FLOAT, ['notnull' => false]);
$table->addColumn('test_real', Types::REAL, ['notnull' => false]);
$table->addColumn('test_decimal', Types::DECIMAL, ['notnull' => false, 'scale' => 2, 'precision' => 10]);
$table->setPrimaryKey(['id']);

Expand Down Expand Up @@ -94,6 +95,23 @@ public static function floatProvider(): iterable
];
}

#[DataProvider('realFloatProvider')]
public function testIdempotentConversionToRealFloat(string $type, mixed $originalValue): void
{
$dbValue = $this->processValue($type, $originalValue);

self::assertIsFloat($dbValue);
self::assertEquals($originalValue, $dbValue);
}

/** @return mixed[][] */
public static function realFloatProvider(): iterable
{
return [
'real' => [Types::REAL, 1.5],
];
}

#[DataProvider('toStringProvider')]
public function testIdempotentConversionToString(string $type, mixed $originalValue): void
{
Expand Down
15 changes: 15 additions & 0 deletions tests/Platforms/AbstractMySQLPlatformTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,21 @@ public static function getGeneratesFloatDeclarationSQL(): iterable
];
}

/**
* {@inheritDoc}
*/
public static function getGeneratesRealFloatDeclarationSQL(): iterable
{
return [
[[], 'FLOAT'],
[['unsigned' => true], 'FLOAT UNSIGNED'],
[['unsigned' => false], 'FLOAT'],
[['precision' => 5], 'FLOAT'],
[['scale' => 5], 'FLOAT'],
[['precision' => 4, 'scale' => 2], 'FLOAT'],
];
}

public function testQuotesDatabaseNameInListViewsSQL(): void
{
self::assertStringContainsStringIgnoringCase(
Expand Down
20 changes: 20 additions & 0 deletions tests/Platforms/AbstractPlatformTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,26 @@ public static function getGeneratesFloatDeclarationSQL(): iterable
];
}

/** @param mixed[] $column */
#[DataProvider('getGeneratesRealFloatDeclarationSQL')]
public function testGeneratesRealFloatDeclarationSQL(array $column, string $expectedSql): void
{
self::assertSame($expectedSql, $this->platform->getRealFloatDeclarationSQL($column));
}

/** @return mixed[][] */
public static function getGeneratesRealFloatDeclarationSQL(): iterable
{
return [
[[], 'REAL'],
[['unsigned' => true], 'REAL'],
[['unsigned' => false], 'REAL'],
[['precision' => 5], 'REAL'],
[['scale' => 5], 'REAL'],
[['precision' => 4, 'scale' => 2], 'REAL'],
];
}

public function testItEscapesStringsForLike(): void
{
self::assertSame(
Expand Down
3 changes: 3 additions & 0 deletions tests/Platforms/OraclePlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,9 @@ public function testInitializesDoctrineTypeMappings(): void

self::assertTrue($this->platform->hasDoctrineTypeMappingFor('date'));
self::assertSame(Types::DATE_MUTABLE, $this->platform->getDoctrineTypeMapping('date'));

self::assertTrue($this->platform->hasDoctrineTypeMappingFor('real'));
self::assertSame(Types::REAL, $this->platform->getDoctrineTypeMapping('real'));
}

public function testGetVariableLengthStringTypeDeclarationSQLNoLength(): void
Expand Down
2 changes: 1 addition & 1 deletion tests/Platforms/SQLServerPlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ public function testInitializesDoctrineTypeMappings(): void
self::assertSame(Types::FLOAT, $this->platform->getDoctrineTypeMapping('float'));

self::assertTrue($this->platform->hasDoctrineTypeMappingFor('real'));
self::assertSame(Types::FLOAT, $this->platform->getDoctrineTypeMapping('real'));
self::assertSame(Types::REAL, $this->platform->getDoctrineTypeMapping('real'));

self::assertTrue($this->platform->hasDoctrineTypeMappingFor('double'));
self::assertSame(Types::FLOAT, $this->platform->getDoctrineTypeMapping('double'));
Expand Down
42 changes: 42 additions & 0 deletions tests/Types/RealFloatTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Tests\Types;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\RealFloatType;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class RealFloatTest extends TestCase
{
private AbstractPlatform&MockObject $platform;
private RealFloatType $type;

protected function setUp(): void
{
$this->platform = $this->createMock(AbstractPlatform::class);
$this->type = new RealFloatType();
}

public function testFloatConvertsToPHPValue(): void
{
self::assertIsFloat($this->type->convertToPHPValue('5.5', $this->platform));
berkut1 marked this conversation as resolved.
Show resolved Hide resolved
}

public function testFloatNullConvertsToPHPValue(): void
{
self::assertNull($this->type->convertToPHPValue(null, $this->platform));
}

public function testFloatConvertToDatabaseValue(): void
{
self::assertIsFloat($this->type->convertToDatabaseValue(5.5, $this->platform));
}

public function testFloatNullConvertToDatabaseValue(): void
{
self::assertNull($this->type->convertToDatabaseValue(null, $this->platform));
}
}
Loading