Skip to content

Commit b2067a2

Browse files
committed
SqlPreprocessor: scalar are always passed via bindValue()
1 parent a01c1e1 commit b2067a2

7 files changed

+106
-110
lines changed

src/Database/SqlPreprocessor.php

+6-22
Original file line numberDiff line numberDiff line change
@@ -113,29 +113,16 @@ public function callback($m)
113113
private function formatValue($value, $mode = NULL)
114114
{
115115
if (!$mode || $mode === 'auto') {
116-
if (is_string($value)) {
117-
if (strlen($value) > 20) {
118-
$this->remaining[] = $value;
119-
return '?';
120-
121-
} else {
122-
return $this->connection->quote($value);
123-
}
124-
125-
} elseif (is_int($value)) {
126-
return (string) $value;
127-
128-
} elseif (is_float($value)) {
129-
return rtrim(rtrim(number_format($value, 10, '.', ''), '0'), '.');
130-
131-
} elseif (is_bool($value)) {
132-
return $this->driver->formatBool($value);
116+
if (is_scalar($value) || is_resource($value)) {
117+
$this->remaining[] = $value;
118+
return '?';
133119

134120
} elseif ($value === NULL) {
135121
return 'NULL';
136122

137123
} elseif ($value instanceof Table\IRow) {
138-
return $this->formatValue($value->getPrimary());
124+
$this->remaining[] = $value->getPrimary();
125+
return '?';
139126

140127
} elseif ($value instanceof SqlLiteral) {
141128
$prep = clone $this;
@@ -150,10 +137,7 @@ private function formatValue($value, $mode = NULL)
150137
return $this->driver->formatDateInterval($value);
151138

152139
} elseif (is_object($value) && method_exists($value, '__toString')) {
153-
return $this->formatValue((string) $value);
154-
155-
} elseif (is_resource($value)) {
156-
$this->remaining[] = $value;
140+
$this->remaining[] = (string) $value;
157141
return '?';
158142
}
159143

tests/Database/Connection.preprocess.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ require __DIR__ . '/connect.inc.php'; // create $connection
1212

1313
Assert::same(['SELECT name FROM author', []], $connection->preprocess('SELECT name FROM author'));
1414

15-
Assert::same(["SELECT 'string'", []], $connection->preprocess('SELECT ?', 'string'));
15+
Assert::same(["SELECT ?", ['string']], $connection->preprocess('SELECT ?', 'string'));

tests/Database/Connection.query.phpt

+6-3
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,20 @@ Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/files/{$driverName
1515
test(function () use ($connection) {
1616
$res = $connection->query('SELECT id FROM author WHERE id = ?', 11);
1717
Assert::type(Nette\Database\ResultSet::class, $res);
18-
Assert::same('SELECT id FROM author WHERE id = 11', $res->getQueryString());
18+
Assert::same('SELECT id FROM author WHERE id = ?', $res->getQueryString());
19+
Assert::same([11], $res->getParameters());
1920
});
2021

2122

2223
test(function () use ($connection) {
2324
$res = $connection->query('SELECT id FROM author WHERE id = ? OR id = ?', 11, 12);
24-
Assert::same('SELECT id FROM author WHERE id = 11 OR id = 12', $res->getQueryString());
25+
Assert::same('SELECT id FROM author WHERE id = ? OR id = ?', $res->getQueryString());
26+
Assert::same([11, 12], $res->getParameters());
2527
});
2628

2729

2830
test(function () use ($connection) {
2931
$res = $connection->queryArgs('SELECT id FROM author WHERE id = ? OR id = ?', [11, 12]);
30-
Assert::same('SELECT id FROM author WHERE id = 11 OR id = 12', $res->getQueryString());
32+
Assert::same('SELECT id FROM author WHERE id = ? OR id = ?', $res->getQueryString());
33+
Assert::same([11, 12], $res->getParameters());
3134
});

tests/Database/Context.query.phpt

+6-3
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,20 @@ Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/files/{$driverName
1515
test(function () use ($context) {
1616
$res = $context->query('SELECT id FROM author WHERE id = ?', 11);
1717
Assert::type(Nette\Database\ResultSet::class, $res);
18-
Assert::same('SELECT id FROM author WHERE id = 11', $res->getQueryString());
18+
Assert::same('SELECT id FROM author WHERE id = ?', $res->getQueryString());
19+
Assert::same([11], $res->getParameters());
1920
});
2021

2122

2223
test(function () use ($context) {
2324
$res = $context->query('SELECT id FROM author WHERE id = ? OR id = ?', 11, 12);
24-
Assert::same('SELECT id FROM author WHERE id = 11 OR id = 12', $res->getQueryString());
25+
Assert::same('SELECT id FROM author WHERE id = ? OR id = ?', $res->getQueryString());
26+
Assert::same([11, 12], $res->getParameters());
2527
});
2628

2729

2830
test(function () use ($context) {
2931
$res = $context->queryArgs('SELECT id FROM author WHERE id = ? OR id = ?', [11, 12]);
30-
Assert::same('SELECT id FROM author WHERE id = 11 OR id = 12', $res->getQueryString());
32+
Assert::same('SELECT id FROM author WHERE id = ? OR id = ?', $res->getQueryString());
33+
Assert::same([11, 12], $res->getParameters());
3134
});

0 commit comments

Comments
 (0)