Skip to content

Commit 6dd5dda

Browse files
committed
SqlPreprocessor: detects modes inside SQL string, before ?
1 parent 9f4b829 commit 6dd5dda

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/Database/SqlPreprocessor.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function process($params)
6666
$this->arrayMode = NULL;
6767
$res[] = Nette\Utils\Strings::replace(
6868
$param,
69-
'~\'[^\']*+\'|"[^"]*+"|\?[a-z]*|^\s*+(?:INSERT|REPLACE)\b|\b(?:SET|WHERE|HAVING|ORDER BY|GROUP BY|KEY UPDATE)(?=[\s?]*+\z)|/\*.*?\*/|--[^\n]*~si',
69+
'~\'[^\']*+\'|"[^"]*+"|\?[a-z]*|^\s*+(?:INSERT|REPLACE)\b|\b(?:SET|WHERE|HAVING|ORDER BY|GROUP BY|KEY UPDATE)(?=\s*\z|\s*\?)|/\*.*?\*/|--[^\n]*~si',
7070
[$this, 'callback']
7171
);
7272
} else {

tests/Database/SqlPreprocessor.phpt

+13
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,19 @@ test(function () use ($preprocessor) { // ?order
143143
});
144144

145145

146+
test(function () use ($preprocessor) { // mix of where & order
147+
list($sql, $params) = $preprocessor->process(['SELECT id FROM author WHERE ? ORDER BY ?', [
148+
'id' => 1,
149+
'web' => 'web',
150+
], [
151+
'name' => FALSE,
152+
]]);
153+
154+
Assert::same(reformat("SELECT id FROM author WHERE ([id] = 1) AND ([web] = 'web') ORDER BY [name] DESC"), $sql);
155+
Assert::same([], $params);
156+
});
157+
158+
146159
test(function () use ($preprocessor) { // missing parameters
147160
Assert::exception(function () use ($preprocessor) {
148161
$preprocessor->process(['SELECT id FROM author WHERE id =? OR id = ?', 11]);

0 commit comments

Comments
 (0)