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

Feature: Add Real/Float4 type #6467

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -1108,6 +1108,14 @@ public function getFloatDeclarationSQL(array $column)
return 'DOUBLE PRECISION' . $this->getUnsignedDeclaration($column);
}

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

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

/**
* @param mixed[] $column
*
* @return string
*/
public function getRealFloatDeclarationSQL(array $column)
{
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 @@ -136,7 +136,7 @@ protected function initializeDoctrineTypeMappings()
'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 @@ -1258,6 +1258,7 @@ protected function initializeDoctrineTypeMappings()
'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 @@ -1220,7 +1220,7 @@ protected function initializeDoctrineTypeMappings()
'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 @@ -1236,7 +1236,7 @@ protected function initializeDoctrineTypeMappings()
'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 @@ -1555,7 +1555,7 @@ protected function initializeDoctrineTypeMappings()
'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 @@ -796,7 +796,7 @@ protected function initializeDoctrineTypeMappings()
'ntext' => Types\Types::STRING,
'numeric' => Types\Types::DECIMAL,
'nvarchar' => Types\Types::STRING,
'real' => Types\Types::FLOAT,
'real' => Types\Types::REAL,
'serial' => Types\Types::INTEGER,
'smallint' => Types\Types::SMALLINT,
'text' => Types\Types::TEXT,
Expand Down
6 changes: 6 additions & 0 deletions src/Schema/OracleSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@

break;

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

Check failure on line 204 in src/Schema/OracleSchemaManager.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.3)

Expected 1 line after "if", found 0.
$type = 'real';

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

View check run for this annotation

Codecov / codecov/patch

src/Schema/OracleSchemaManager.php#L203-L205

Added lines #L203 - L205 were not covered by tests
}
break;

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

View check run for this annotation

Codecov / codecov/patch

src/Schema/OracleSchemaManager.php#L207

Added line #L207 was not covered by tests

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

namespace Doctrine\DBAL\Types;

use Doctrine\DBAL\Platforms\AbstractPlatform;

class RealFloatType extends Type
{
/**
* {@inheritDoc}
*/
public function getName()
{
return Types::REAL;
}

/**
* {@inheritDoc}
*/
public function getSQLDeclaration(array $column, AbstractPlatform $platform)
{
return $platform->getRealFloatDeclarationSQL($column);
}

/**
* {@inheritDoc}
*
* @param T $value
*
* @return (T is null ? null : float)
*
* @template T
*/
public function convertToPHPValue($value, AbstractPlatform $platform)
{
return $value === null ? null : (float) $value;
}
}

Check failure on line 38 in src/Types/RealFloatType.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.3)

Expected 1 newline at end of file; 0 found
1 change: 1 addition & 0 deletions src/Types/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ abstract class Type
Types::DATETIMETZ_IMMUTABLE => DateTimeTzImmutableType::class,
Types::DECIMAL => DecimalType::class,
Types::FLOAT => FloatType::class,
Types::REAL => RealFloatType::class,
Types::GUID => GuidType::class,
Types::INTEGER => IntegerType::class,
Types::JSON => JsonType::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 DATETIMETZ_IMMUTABLE = 'datetimetz_immutable';
public const DECIMAL = 'decimal';
public const FLOAT = 'float';
public const REAL = 'real';
public const GUID = 'guid';
public const INTEGER = 'integer';
public const JSON = 'json';
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 @@ -161,8 +161,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 @@ -396,6 +396,24 @@ public function testListFloatTypeColumns(): void
self::assertTrue($columns['col_unsigned']->getUnsigned());
}

public function testListRealFloatTypeColumns(): void
{
$tableName = 'test_list_real_float_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
2 changes: 2 additions & 0 deletions tests/Functional/Schema/PostgreSQLSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,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, ['default' => -1.1]);
$table->addColumn('col_string', Types::STRING, ['default' => '(-1)']);

Expand All @@ -501,6 +502,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
22 changes: 22 additions & 0 deletions tests/Functional/TypeConversionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ protected function setUp(): void
$table->addColumn('test_json', Types::JSON, ['notnull' => false]);
$table->addColumn('test_object', Types::OBJECT, ['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 @@ -104,6 +105,27 @@ public static function floatProvider(): iterable
];
}

/**
* @param mixed $originalValue
*
* @dataProvider realFloatProvider
*/
public function testIdempotentConversionToRealFloat(string $type, $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],
];
}

/**
* @param mixed $originalValue
*
Expand Down
12 changes: 12 additions & 0 deletions tests/Platforms/AbstractMySQLPlatformTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,18 @@
];
}

public static function getGeneratesRealFloatDeclarationSQL(): iterable

Check failure on line 871 in tests/Platforms/AbstractMySQLPlatformTestCase.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.3)

Method \Doctrine\DBAL\Tests\Platforms\AbstractMySQLPlatformTestCase::getGeneratesRealFloatDeclarationSQL() does not have @return annotation for its traversable return value.
{
return [
[[], 'FLOAT'],
[['unsigned' => true], 'FLOAT UNSIGNED'],
[['unsigned' => false], 'FLOAT'],
[['precision' => 5], 'FLOAT'],
[['scale' => 5], 'FLOAT'],
[['precision' => 4, 'scale' => 2], 'FLOAT'],
];
}

public function testQuotesTableNameInListTableIndexesSQL(): void
{
self::assertStringContainsStringIgnoringCase(
Expand Down
23 changes: 23 additions & 0 deletions tests/Platforms/AbstractPlatformTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1353,6 +1353,29 @@ 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 @@ -513,6 +513,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'));
}

protected function getBinaryMaxLength(): int
Expand Down
2 changes: 1 addition & 1 deletion tests/Platforms/SQLServerPlatformTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1102,7 +1102,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

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
{
/** @var AbstractPlatform&MockObject */
private AbstractPlatform $platform;

private RealFloatType $type;

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

Check failure on line 20 in tests/Types/RealFloatTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.3)

Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space
}

public function testFloatConvertsToPHPValue(): void
{
self::assertIsFloat($this->type->convertToPHPValue('5.5', $this->platform));
}

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));
}
}

Check failure on line 42 in tests/Types/RealFloatTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.3)

Expected 1 newline at end of file; 0 found
Loading