Skip to content

Commit a418e3f

Browse files
spazedg
authored andcommitted
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
1 parent a45e10a commit a418e3f

2 files changed

Lines changed: 14 additions & 1 deletion

File tree

src/Database/ResultSet.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ public function normalizeRow(array $row): array
140140
$row[$key] = is_float($tmp = $value * 1) ? $value : $tmp;
141141

142142
} elseif ($type === IStructure::FIELD_FLOAT) {
143-
if (($pos = strpos($value, '.')) !== false) {
143+
if (is_string($value) && ($pos = strpos($value, '.')) !== false) {
144144
$value = rtrim(rtrim($pos === 0 ? "0$value" : $value, '0'), '.');
145145
}
146146
$float = (float) $value;

tests/Database/ResultSet.normalizeRow.mysql.phpt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,16 @@ $avgTime->f = 0.5;
132132
Assert::equal([
133133
'avg_time' => $avgTime,
134134
], (array) $res->fetch());
135+
136+
137+
$connection->getPdo()->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
138+
139+
$res = $connection->query('SELECT `int`, `decimal`, `decimal2`, `float`, `double` FROM types');
140+
141+
Assert::equal([
142+
'int' => 1,
143+
'decimal' => 1.0,
144+
'decimal2' => 1.1,
145+
'float' => 1.0,
146+
'double' => 1.1,
147+
], (array) $res->fetch());

0 commit comments

Comments
 (0)