Skip to content

Commit

Permalink
drivers: do not convert DECIMAL/NUMERIC to floats (BC BREAK!)
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Apr 10, 2023
1 parent dabb07f commit b137ba3
Show file tree
Hide file tree
Showing 10 changed files with 11 additions and 47 deletions.
2 changes: 0 additions & 2 deletions src/Drivers/Mysqli/MysqliResultNormalizerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,6 @@ public function resolve(array $types): array
];

static $floats = [
MYSQLI_TYPE_DECIMAL => true,
MYSQLI_TYPE_NEWDECIMAL => true,
MYSQLI_TYPE_DOUBLE => true,
MYSQLI_TYPE_FLOAT => true,
];
Expand Down
2 changes: 0 additions & 2 deletions src/Drivers/PdoMysql/PdoMysqlResultNormalizerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ public function resolve(array $types): array
];

static $floats = [
'DECIMAL' => true,
'NEWDECIMAL' => true,
'DOUBLE' => true,
'FLOAT' => true,
];
Expand Down
1 change: 0 additions & 1 deletion src/Drivers/PdoPgsql/PdoPgsqlResultNormalizerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ public function resolve(array $types): array
];

static $floats = [
'numeric' => true,
'float4' => true,
'float8' => true,
];
Expand Down
15 changes: 0 additions & 15 deletions src/Drivers/PdoSqlsrv/PdoSqlsrvResultNormalizerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class PdoSqlsrvResultNormalizerFactory
private Closure $boolNormalizer;
private Closure $dateTimeNormalizer;
private Closure $offsetDateTimeNormalizer;
private Closure $moneyNormalizer;


public function __construct()
Expand Down Expand Up @@ -56,11 +55,6 @@ public function __construct()
if ($value === null) return null;
return new DateTimeImmutable($value);
};

$this->moneyNormalizer = static function ($value) {
if ($value === null) return null;
return !str_contains($value, '.') ? (int) $value : (float) $value;
};
}


Expand All @@ -85,13 +79,6 @@ public function resolve(array $types): array
'datetime2' => true,
];

static $money = [
'numeric' => true,
'decimal' => true,
'money' => true,
'smallmoney' => true,
];

$normalizers = [];
foreach ($types as $column => $type) {
if (str_ends_with((string) $type, ' identity')) { // strip " identity" suffix
Expand All @@ -110,8 +97,6 @@ public function resolve(array $types): array
$normalizers[$column] = $this->offsetDateTimeNormalizer;
} elseif ($type === 'bit') {
$normalizers[$column] = $this->boolNormalizer;
} elseif (isset($money[$type])) {
$normalizers[$column] = $this->moneyNormalizer;
}
}
return $normalizers;
Expand Down
1 change: 0 additions & 1 deletion src/Drivers/Pgsql/PgsqlResultNormalizerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ public function resolve(array $types): array
];

static $floats = [
'numeric' => true,
'float4' => true,
'float8' => true,
];
Expand Down
15 changes: 0 additions & 15 deletions src/Drivers/Sqlsrv/SqlsrvResultNormalizationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,11 @@ class SqlsrvResultNormalizationFactory
private const TYPE_DATE = 91;
private const TYPE_DATETIME_DATETIME2_SMALLDATETIME = 93;
private const TYPE_DATETIMEOFFSET = -155;
private const TYPE_NUMERIC = 2;
private const TYPE_DECIMAL_MONEY_SMALLMONEY = 3;

private Closure $intNormalizer;
private Closure $boolNormalizer;
private Closure $dateTimeNormalizer;
private Closure $offsetDateTimeNormalizer;
private Closure $moneyNormalizer;


public function __construct()
Expand All @@ -65,11 +62,6 @@ public function __construct()
if ($value === null) return null;
return new DateTimeImmutable($value);
};

$this->moneyNormalizer = static function ($value) {
if ($value === null) return null;
return !str_contains($value, '.') ? (int) $value : (float) $value;
};
}


Expand All @@ -94,11 +86,6 @@ public function resolve(array $types): array
self::TYPE_DATETIME_DATETIME2_SMALLDATETIME => true,
];

static $money = [
self::TYPE_NUMERIC => true,
self::TYPE_DECIMAL_MONEY_SMALLMONEY => true,
];

$normalizers = [];
foreach ($types as $column => $type) {
if (isset($ok[$type])) {
Expand All @@ -111,8 +98,6 @@ public function resolve(array $types): array
$normalizers[$column] = $this->offsetDateTimeNormalizer;
} elseif ($type === self::TYPE_BIT) {
$normalizers[$column] = $this->boolNormalizer;
} elseif (isset($money[$type])) {
$normalizers[$column] = $this->moneyNormalizer;
}
}
return $normalizers;
Expand Down
6 changes: 3 additions & 3 deletions tests/cases/integration/connection.sqlserver.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ class ConnectionSqlServerTest extends IntegrationTestCase

for ($i = 1; $i < 4; $i++) {
$this->connection->query('INSERT INTO autoi_1 DEFAULT VALUES');
Assert::same($i, $this->connection->getLastInsertedId());
Assert::same("$i", $this->connection->getLastInsertedId());
}

$this->connection->query('INSERT INTO autoi_2 DEFAULT VALUES');
Assert::same(1, $this->connection->getLastInsertedId());
Assert::same('1', $this->connection->getLastInsertedId());

$this->connection->query('CREATE TRIGGER autoi_2_ai ON autoi_2 AFTER INSERT AS INSERT INTO autoi_1 DEFAULT VALUES');
$this->connection->query('INSERT INTO autoi_2 DEFAULT VALUES');
Assert::same(2, $this->connection->getLastInsertedId());
Assert::same('2', $this->connection->getLastInsertedId());
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/cases/integration/types.mysql.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ class TypesMysqlTest extends IntegrationTestCase
Assert::same(1, $row->bigint);
Assert::same(2015, $row->year);

Assert::same(100.0, $row->decimal);
Assert::same(100.22, $row->decimal2);
Assert::same('100', $row->decimal);
Assert::same('100.22', $row->decimal2);
Assert::same(12.34, $row->float);
Assert::same(12.34, $row->double);

Expand Down
2 changes: 1 addition & 1 deletion tests/cases/integration/types.postgres.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class TypesPostgresTest extends IntegrationTestCase
Assert::same(2, $row->int4);
Assert::same(3, $row->int2);

Assert::same(12.04, $row->numeric);
Assert::same('12.04', $row->numeric);
Assert::same(12.05, $row->float4);
Assert::same(12.06, $row->float8);

Expand Down
10 changes: 5 additions & 5 deletions tests/cases/integration/types.sqlserver.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ class TypesSqlserverTest extends IntegrationTestCase
Assert::same(12.0, $row->float1);
Assert::same(12.0, $row->real1);

Assert::same(12.04, $row->numeric1);
Assert::same(12.00, $row->numeric2);
Assert::same(12, $row->numeric3);
Assert::same('12.04', $row->numeric1);
Assert::same('12.00', $row->numeric2);
Assert::same('12', $row->numeric3);

Assert::same(12.00, $row->money1);
Assert::same(12.00, $row->smallmoney1);
Assert::same('12.0000', $row->money1);
Assert::same('12.0000', $row->smallmoney1);

Assert::same(true, $row->boolean);
}
Expand Down

0 comments on commit b137ba3

Please sign in to comment.