mirror of
https://github.com/dg/dibi.git
synced 2025-02-22 18:02:25 +01:00
Translator: %i %f throws exception when value is not numeric (BC break)
This commit is contained in:
parent
1b59801bed
commit
49e90517b9
@ -345,8 +345,12 @@ 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)) {
|
||||
if (preg_match('#[+-]?\d++(?:e\d+)?\z#A', $value)) {
|
||||
return $value; // support for long numbers - keep them unchanged
|
||||
} else {
|
||||
throw new Exception("Expected number, '$value' given.");
|
||||
}
|
||||
} else {
|
||||
return (string) (int) $value;
|
||||
}
|
||||
@ -354,8 +358,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'), '.');
|
||||
}
|
||||
|
@ -501,11 +501,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)
|
||||
);
|
||||
|
||||
@ -525,10 +525,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::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(
|
||||
|
Loading…
x
Reference in New Issue
Block a user