Skip to content

Commit 644b587

Browse files
committed
SqlPreprocessor: scalar are always passed via bindValue() (BC break)
1 parent ffe2c09 commit 644b587

File tree

8 files changed

+108
-110
lines changed

8 files changed

+108
-110
lines changed

src/Database/SqlPreprocessor.php

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

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

138124
} elseif ($value instanceof Table\IRow) {
139-
return $this->formatValue($value->getPrimary());
125+
$this->remaining[] = $value->getPrimary();
126+
return '?';
140127

141128
} elseif ($value instanceof SqlLiteral) {
142129
$prep = clone $this;
@@ -151,10 +138,7 @@ private function formatValue($value, $mode = NULL)
151138
return $this->driver->formatDateInterval($value);
152139

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

tests/Database/Connection.preprocess.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@ require __DIR__ . '/connect.inc.php'; // create $connection
1414

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

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

tests/Database/Connection.query.phpt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,20 @@ Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/files/{$driverName
1717
test(function () use ($connection) {
1818
$res = $connection->query('SELECT id FROM author WHERE id = ?', 11);
1919
Assert::type(Nette\Database\ResultSet::class, $res);
20-
Assert::same('SELECT id FROM author WHERE id = 11', $res->getQueryString());
20+
Assert::same('SELECT id FROM author WHERE id = ?', $res->getQueryString());
21+
Assert::same([11], $res->getParameters());
2122
});
2223

2324

2425
test(function () use ($connection) {
2526
$res = $connection->query('SELECT id FROM author WHERE id = ? OR id = ?', 11, 12);
26-
Assert::same('SELECT id FROM author WHERE id = 11 OR id = 12', $res->getQueryString());
27+
Assert::same('SELECT id FROM author WHERE id = ? OR id = ?', $res->getQueryString());
28+
Assert::same([11, 12], $res->getParameters());
2729
});
2830

2931

3032
test(function () use ($connection) {
3133
$res = $connection->queryArgs('SELECT id FROM author WHERE id = ? OR id = ?', [11, 12]);
32-
Assert::same('SELECT id FROM author WHERE id = 11 OR id = 12', $res->getQueryString());
34+
Assert::same('SELECT id FROM author WHERE id = ? OR id = ?', $res->getQueryString());
35+
Assert::same([11, 12], $res->getParameters());
3336
});

tests/Database/Context.query.phpt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,20 @@ Nette\Database\Helpers::loadFromFile($connection, __DIR__ . "/files/{$driverName
1717
test(function () use ($context) {
1818
$res = $context->query('SELECT id FROM author WHERE id = ?', 11);
1919
Assert::type(Nette\Database\ResultSet::class, $res);
20-
Assert::same('SELECT id FROM author WHERE id = 11', $res->getQueryString());
20+
Assert::same('SELECT id FROM author WHERE id = ?', $res->getQueryString());
21+
Assert::same([11], $res->getParameters());
2122
});
2223

2324

2425
test(function () use ($context) {
2526
$res = $context->query('SELECT id FROM author WHERE id = ? OR id = ?', 11, 12);
26-
Assert::same('SELECT id FROM author WHERE id = 11 OR id = 12', $res->getQueryString());
27+
Assert::same('SELECT id FROM author WHERE id = ? OR id = ?', $res->getQueryString());
28+
Assert::same([11, 12], $res->getParameters());
2729
});
2830

2931

3032
test(function () use ($context) {
3133
$res = $context->queryArgs('SELECT id FROM author WHERE id = ? OR id = ?', [11, 12]);
32-
Assert::same('SELECT id FROM author WHERE id = 11 OR id = 12', $res->getQueryString());
34+
Assert::same('SELECT id FROM author WHERE id = ? OR id = ?', $res->getQueryString());
35+
Assert::same([11, 12], $res->getParameters());
3336
});

tests/Database/ResultSet.parameters.postgre.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use Nette\Utils\DateTime;
1212

1313
require __DIR__ . '/connect.inc.php'; // create $connection
1414

15-
$res = $connection->fetch('SELECT ? AS c1, ? AS c2, ? AS c3', TRUE, NULL, 123);
15+
$res = $connection->fetch('SELECT ?::bool AS c1, ? AS c2, ?::int AS c3', TRUE, NULL, 123);
1616

1717
Assert::same(
1818
['c1' => TRUE, 'c2' => NULL, 'c3' => 123],

0 commit comments

Comments
 (0)