1
0
mirror of https://github.com/dg/dibi.git synced 2025-08-06 06:07:39 +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 3253a5b092
commit d31f2e3dd4
2 changed files with 24 additions and 13 deletions

View File

@@ -350,11 +350,15 @@ final class Translator
case 'u': // unsigned int, ignored
if ($value === null) {
return 'NULL';
} elseif (is_string($value) && preg_match('#[+-]?\d++(?:e\d+)?\z#A', $value)) {
return $value; // support for long numbers - keep them unchanged
} elseif (is_string($value) && substr($value, 1, 1) === 'x' && is_numeric($value)) {
trigger_error('Support for hex strings has been deprecated.', E_USER_DEPRECATED);
return (string) hexdec($value);
} elseif (is_string($value)) {
if (preg_match('#[+-]?\d++(?:e\d+)?\z#A', $value)) {
return $value; // support for long numbers - keep them unchanged
} elseif (substr($value, 1, 1) === 'x' && is_numeric($value)) {
trigger_error('Support for hex strings has been deprecated.', E_USER_DEPRECATED);
return (string) hexdec($value);
} else {
throw new Exception("Expected number, '$value' given.");
}
} else {
return (string) (int) $value;
}
@@ -362,8 +366,12 @@ final class Translator
case 'f': // float
if ($value === null) {
return 'NULL';
} elseif (is_string($value) && is_numeric($value) && substr($value, 1, 1) !== 'x') {
return $value; // support for extreme numbers - keep them unchanged
} elseif (is_string($value)) {
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 {
return rtrim(rtrim(number_format($value + 0, 10, '.', ''), '0'), '.');
}

View File

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