Skip to content
Permalink
Browse files

ResultSet: only format strings as floats (#228)

Because with native prepared statements in MySQL, floats are returned as floats, not strings like with emulated prepares

Fixes #227
  • Loading branch information...
spaze authored and dg committed Apr 15, 2019
1 parent a45e10a commit a418e3f1408b2d9be6387f68f2e6ca3cc5048581
Showing with 14 additions and 1 deletion.
  1. +1 −1 src/Database/ResultSet.php
  2. +13 −0 tests/Database/ResultSet.normalizeRow.mysql.phpt
@@ -140,7 +140,7 @@ public function normalizeRow(array $row): array
$row[$key] = is_float($tmp = $value * 1) ? $value : $tmp;
} elseif ($type === IStructure::FIELD_FLOAT) {
if (($pos = strpos($value, '.')) !== false) {
if (is_string($value) && ($pos = strpos($value, '.')) !== false) {
$value = rtrim(rtrim($pos === 0 ? "0$value" : $value, '0'), '.');
}
$float = (float) $value;
@@ -132,3 +132,16 @@ $avgTime->f = 0.5;
Assert::equal([
'avg_time' => $avgTime,
], (array) $res->fetch());
$connection->getPdo()->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$res = $connection->query('SELECT `int`, `decimal`, `decimal2`, `float`, `double` FROM types');
Assert::equal([
'int' => 1,
'decimal' => 1.0,
'decimal2' => 1.1,
'float' => 1.0,
'double' => 1.1,
], (array) $res->fetch());

0 comments on commit a418e3f

Please sign in to comment.
You can’t perform that action at this time.