Skip to content

Commit

Permalink
Address issue #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.

In addition:
- Fix IBM DB2 test issue due to untrusted update
- Try to fix flakey Mysql tests
  • Loading branch information
mmucklo committed Aug 8, 2019
1 parent 29486f0 commit e2a4e80
Show file tree
Hide file tree
Showing 14 changed files with 182 additions and 2 deletions.
13 changes: 13 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -339,3 +339,16 @@ 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-language.sh
- 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

7 changes: 6 additions & 1 deletion lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,13 @@ public function convertException($message, DriverException $exception)
case '1429':
case '2002':
case '2005':
case '2054':
return new Exception\ConnectionException($message, $exception);

case '2006':
if ($exception instanceof Driver\Mysqli\MysqliConnectionException || $exception instanceof PDOConnectionException) {
return new Exception\ConnectionException($message, $exception);
}
break;
case '1048':
case '1121':
case '1138':
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function __construct(array $params, $username, $password, array $driverOp
});
try {
if (! $this->conn->real_connect($params['host'], $username, $password, $dbname, $port, $socket, $flags)) {
throw new MysqliException($this->conn->connect_error, $this->conn->sqlstate ?? 'HY000', $this->conn->connect_errno);
throw new MysqliConnectionException($this->conn->connect_error, $this->conn->sqlstate ?? 'HY000', $this->conn->connect_errno);
}
} finally {
restore_error_handler();
Expand Down
10 changes: 10 additions & 0 deletions lib/Doctrine/DBAL/Driver/Mysqli/MysqliConnectionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Doctrine\DBAL\Driver\Mysqli;

/**
* Exception thrown in case the mysqli driver errors while connecting.
*/
class MysqliConnectionException extends MysqliException
{
}
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
10 changes: 10 additions & 0 deletions lib/Doctrine/DBAL/Driver/PDOConnectionException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Doctrine\DBAL\Driver;

/**
* PDOConnectionException is used to distinguish connection failures from regular PDOExceptions.
*/
class PDOConnectionException extends PDOException
{
}
1 change: 1 addition & 0 deletions lib/Doctrine/DBAL/Driver/PDOStatement.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class PDOStatement extends \PDOStatement implements Statement
ParameterType::BINARY => PDO::PARAM_LOB,
ParameterType::LARGE_OBJECT => PDO::PARAM_LOB,
ParameterType::BOOLEAN => PDO::PARAM_BOOL,
ParameterType::DOUBLE => PDO::PARAM_STR,
];

private const FETCH_MODE_MAP = [
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;
}
}
112 changes: 112 additions & 0 deletions tests/Doctrine/Tests/DBAL/Functional/Types/DoubleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?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 abs;
use function floatval;
use function is_int;
use function is_string;
use function localeconv;
use function microtime;
use function str_replace;

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);
$localeInfo = localeconv();

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

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

if (is_string($result1)) {
$result1 = str_replace('.', $localeInfo['decimal_point'] ?? '.', $result1);
$result2 = str_replace('.', $localeInfo['decimal_point'] ?? '.', $result2);
$result3 = str_replace('.', $localeInfo['decimal_point'] ?? '.', $result3);
$result1 = floatval($result1);
$result2 = floatval($result2);
$result3 = floatval($result3);
}

$diff1 = abs($result1 - $value1);
$diff2 = abs($result2 - $value2);
$diff3 = abs($result3 - $value3);

$this->assertLessThanOrEqual(0.0001, $diff1);
$this->assertLessThanOrEqual(0.0001, $diff2);
$this->assertLessThanOrEqual(0.0001, $diff3);

$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]
);
}
}
1 change: 1 addition & 0 deletions tests/travis/install-db2-ibm_db2.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ set -ex
echo "Installing extension"
(
# updating APT packages as per support recommendation
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 6B05F25D762E3157
sudo apt -y -q update
sudo apt install ksh

Expand Down
9 changes: 9 additions & 0 deletions tests/travis/install-language.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

set -ex

echo "Installing language pack..."

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 6B05F25D762E3157
sudo apt -y -q update
sudo apt install language-pack-de
1 change: 1 addition & 0 deletions tests/travis/install-mysql-8.0.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ sudo docker run \
-p 33306:3306 \
--name mysql80 \
mysql:8.0 \
mysqld \
--default-authentication-plugin=mysql_native_password

sudo docker exec -i mysql80 bash <<< 'until echo \\q | mysql doctrine_tests > /dev/null 2>&1 ; do sleep 1; done'
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 e2a4e80

Please sign in to comment.