1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-07 14:46:50 +02:00

Translator: %i %f throws exception when value is not numeric (BC break)

This commit is contained in:
David Grudl
2017-07-22 00:11:23 +02:00
parent 1b59801bed
commit 49e90517b9
2 changed files with 21 additions and 10 deletions

View File

@@ -345,8 +345,12 @@ final class Translator
case 'u': // unsigned int, ignored case 'u': // unsigned int, ignored
if ($value === null) { if ($value === null) {
return 'NULL'; return 'NULL';
} elseif (is_string($value) && preg_match('#[+-]?\d++(?:e\d+)?\z#A', $value)) { } elseif (is_string($value)) {
if (preg_match('#[+-]?\d++(?:e\d+)?\z#A', $value)) {
return $value; // support for long numbers - keep them unchanged return $value; // support for long numbers - keep them unchanged
} else {
throw new Exception("Expected number, '$value' given.");
}
} else { } else {
return (string) (int) $value; return (string) (int) $value;
} }
@@ -354,8 +358,12 @@ final class Translator
case 'f': // float case 'f': // float
if ($value === null) { if ($value === null) {
return 'NULL'; return 'NULL';
} elseif (is_string($value) && is_numeric($value) && substr($value, 1, 1) !== 'x') { } elseif (is_string($value)) {
return $value; // support for extreme numbers - keep them unchanged if (is_numeric($value) && substr($value, 1, 1) !== 'x') {
return $value; // support for long numbers - keep them unchanged
} else {
throw new Exception("Expected number, '$value' given.");
}
} else { } else {
return rtrim(rtrim(number_format($value + 0, 10, '.', ''), '0'), '.'); return rtrim(rtrim(number_format($value + 0, 10, '.', ''), '0'), '.');
} }

View File

@@ -501,11 +501,11 @@ Assert::same('INSERT INTO test **Multi-insert array "num%i" is different**', $e-
$array6 = [ $array6 = [
'id' => [1, 2, 3, 4], 'id' => [1, 2, 3, 4],
'text' => ['ahoj', 'jak', 'se', ['SUM(%i)', '5']], 'text' => ['ahoj', 'jak', 'se', ['SUM(%i)', '5']],
'num%i' => ['1', '', 10.3, 1], 'num%i' => ['1', '-1', 10.3, 1],
]; ];
Assert::same( Assert::same(
reformat('INSERT INTO test ([id], [text], [num]) VALUES (1, \'ahoj\', 1), (2, \'jak\', 0), (3, \'se\', 10), (4, SUM(5), 1)'), reformat('INSERT INTO test ([id], [text], [num]) VALUES (1, \'ahoj\', 1), (2, \'jak\', -1), (3, \'se\', 10), (4, SUM(5), 1)'),
$conn->translate('INSERT INTO test %m', $array6) $conn->translate('INSERT INTO test %m', $array6)
); );
@@ -525,10 +525,13 @@ Assert::same(
$conn->translate('INSERT INTO [test.*]') $conn->translate('INSERT INTO [test.*]')
); );
Assert::same( Assert::exception(function () use ($conn) {
reformat('INSERT INTO 0'), $conn->translate('INSERT INTO %i', 'ahoj');
@$conn->translate('INSERT INTO %f', 'ahoj') // triggers warning in PHP 7.1 }, Dibi\Exception::class, "Expected number, 'ahoj' given.");
);
Assert::exception(function () use ($conn) {
$conn->translate('INSERT INTO %f', 'ahoj');
}, Dibi\Exception::class, "Expected number, 'ahoj' given.");
Assert::same( Assert::same(