Skip to content

Commit b908881

Browse files
committed
SqlPreprocessor: correctly escapes scalars when parameters are not used
1 parent 6132b72 commit b908881

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/Database/SqlPreprocessor.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,17 @@ private function formatValue($value, string $mode = null): string
137137
if ($this->useParams) {
138138
$this->remaining[] = $value;
139139
return '?';
140+
141+
} elseif (is_int($value) || is_bool($value)) {
142+
return (string) (int) $value;
143+
144+
} elseif (is_float($value)) {
145+
return rtrim(rtrim(number_format($value, 10, '.', ''), '0'), '.');
146+
147+
} elseif (is_resource($value)) {
148+
return $this->connection->quote(stream_get_contents($value));
149+
140150
} else {
141-
if (is_resource($value)) {
142-
$value = stream_get_contents($value);
143-
}
144151
return $this->connection->quote((string) $value);
145152
}
146153

tests/Database/SqlPreprocessor.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ test(function () use ($preprocessor) { // basic
2222
});
2323

2424

25+
test(function () use ($preprocessor) { // no parameters
26+
[$sql, $params] = $preprocessor->process(['UNKNOWN a = ?, b = ?, c = ?, d = ?, e = ?', 123, 'abc', true, false, null]);
27+
Assert::same("UNKNOWN a = 123, b = 'abc', c = 1, d = 0, e = NULL", $sql);
28+
Assert::same([], $params);
29+
});
30+
31+
2532
test(function () use ($preprocessor) { // arg without placeholder
2633
[$sql, $params] = $preprocessor->process(['SELECT id FROM author WHERE id =', 11]);
2734
Assert::same('SELECT id FROM author WHERE id = ?', $sql);

0 commit comments

Comments
 (0)