Skip to content

Commit

Permalink
Address issue doctrine#3631 by adding support for a float datatype
Browse files Browse the repository at this point in the history
Also try to add a new functional test per request.
  • Loading branch information
mmucklo committed Aug 7, 2019
1 parent 29486f0 commit c25890e
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 0 deletions.
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ after_script:
travis_retry php ocular.phar code-coverage:upload --format=php-clover clover.xml
fi
addons:
apt:
packages:
- language-pack-de

jobs:
allow_failures:
- php: 7.4snapshot
Expand Down Expand Up @@ -339,3 +344,15 @@ jobs:
install:
- composer config minimum-stability dev
- travis_retry composer update --prefer-dist

- stage: TestIntl
php: 7.2
env: LANGUAGE=de_DE.UTF-8 LC_ALL=de_DE.UTF-8 LANG=de_DE.UTF-8 DB=mysqli.docker MYSQL_VERSION=8.0
sudo: required
services:
- docker
before_script:
- bash ./tests/travis/install-mysql-8.0.sh
script:
- php -d"auto_prepend_file=$PWD/tests/travis/setlocale-de.php" ./vendor/bin/phpunit --configuration tests/travis/$DB.travis.xml

1 change: 1 addition & 0 deletions lib/Doctrine/DBAL/Driver/Mysqli/MysqliStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class MysqliStatement implements IteratorAggregate, Statement
ParameterType::NULL => 's',
ParameterType::INTEGER => 'i',
ParameterType::LARGE_OBJECT => 'b',
ParameterType::DOUBLE => 'd',
];

/** @var mysqli */
Expand Down
5 changes: 5 additions & 0 deletions lib/Doctrine/DBAL/ParameterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ final class ParameterType
*/
public const BINARY = 16;

/**
* Represents a double data type.
*/
public const DOUBLE = 17;

/**
* This class cannot be instantiated.
*/
Expand Down
9 changes: 9 additions & 0 deletions lib/Doctrine/DBAL/Types/FloatType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Doctrine\DBAL\Types;

use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Platforms\AbstractPlatform;

class FloatType extends Type
Expand Down Expand Up @@ -29,4 +30,12 @@ public function convertToPHPValue($value, AbstractPlatform $platform)
{
return $value === null ? null : (float) $value;
}

/**
* {@inheritdoc}
*/
public function getBindingType()
{
return ParameterType::DOUBLE;
}
}
102 changes: 102 additions & 0 deletions tests/Doctrine/Tests/DBAL/Functional/Types/DoubleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

declare(strict_types=1);

namespace Doctrine\Tests\DBAL\Functional\Types;

use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Schema\Table;
use Doctrine\Tests\DbalFunctionalTestCase;
use function is_float;
use function is_int;
use function localeconv;
use function microtime;
use function number_format;
use function strlen;
use function strpos;
use function substr;

class DoubleTest extends DbalFunctionalTestCase
{
protected function setUp() : void
{
parent::setUp();

$table = new Table('double_table');
$table->addColumn('id', 'integer');
$table->addColumn('val', 'float');
$table->setPrimaryKey(['id']);

$sm = $this->connection->getSchemaManager();
$sm->dropAndCreateTable($table);
}

public function testInsertAndSelect() : void
{
$value1 = 1.1;
$value2 = 77.99999999999;
$value3 = microtime(true);
$valStr = (string) $value3;
$localeInfo = localeconv();
$len = strlen(substr($valStr, strpos($valStr, $localeInfo['decimal_point'] ?? '.') + 1));

$this->insert(1, $value1);
$this->insert(2, $value2);
$this->insert(3, $value3);

$result1 = $this->select(1);
$result2 = $this->select(2);
$result3 = $this->select(3);

$this->assertSame(is_float($result1) ? $value1 : number_format($value1, 1, '.', ''), $result1);
$this->assertSame(is_float($result2) ? $value2 : number_format($value2, 11, '.', ''), $result2);
$this->assertSame(is_float($result3) ? $value3 : number_format($value3, $len, '.', ''), $result3);

$result1 = $this->selectDouble($value1);
$result2 = $this->selectDouble($value2);
$result3 = $this->selectDouble($value3);

$this->assertSame(is_int($result1) ? 1 : '1', $result1);
$this->assertSame(is_int($result2) ? 2 : '2', $result2);
$this->assertSame(is_int($result3) ? 3 : '3', $result3);
}

private function insert(int $id, float $value) : void
{
$result = $this->connection->insert('double_table', [
'id' => $id,
'val' => $value,
], [
ParameterType::INTEGER,
ParameterType::DOUBLE,
]);

self::assertSame(1, $result);
}

/**
* @return mixed
*/
private function select(int $id)
{
return $this->connection->fetchColumn(
'SELECT val FROM double_table WHERE id = ?',
[$id],
0,
[ParameterType::INTEGER]
);
}

/**
* @return mixed
*/
private function selectDouble(float $value)
{
return $this->connection->fetchColumn(
'SELECT id FROM double_table WHERE val = ?',
[$value],
0,
[ParameterType::DOUBLE]
);
}
}
3 changes: 3 additions & 0 deletions tests/travis/setlocale-de.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php

setlocale(LC_ALL, 'de_DE.UTF-8', 'de_DE', 'de', 'ge');

0 comments on commit c25890e

Please sign in to comment.