Skip to content

Commit 08d1da8

Browse files
committed
SqlPreprocessor: recoginzes (SELECT) UNION (SELECT) as parameteric command [Closes #248]
1 parent b908881 commit 08d1da8

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/Database/SqlPreprocessor.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public function process(array $params, bool $useParams = false): array
9696
$this->arrayMode = null;
9797
$res[] = Nette\Utils\Strings::replace(
9898
$param,
99-
'~\'[^\']*+\'|"[^"]*+"|\?[a-z]*|^\s*+(?:SELECT|INSERT|UPDATE|DELETE|REPLACE|EXPLAIN)\b|\b(?:SET|WHERE|HAVING|ORDER BY|GROUP BY|KEY UPDATE)(?=\s*$|\s*\?)|/\*.*?\*/|--[^\n]*~Dsi',
99+
'~\'[^\']*+\'|"[^"]*+"|\?[a-z]*|^\s*+(?:\(?\s*SELECT|INSERT|UPDATE|DELETE|REPLACE|EXPLAIN)\b|\b(?:SET|WHERE|HAVING|ORDER BY|GROUP BY|KEY UPDATE)(?=\s*$|\s*\?)|/\*.*?\*/|--[^\n]*~Dsi',
100100
[$this, 'callback']
101101
);
102102
} else {
@@ -122,7 +122,7 @@ public function callback(array $m): string
122122
return $m;
123123

124124
} else { // command
125-
$cmd = ltrim(strtoupper($m));
125+
$cmd = ltrim(strtoupper($m), "\t\n\r (");
126126
$this->arrayMode = self::ARRAY_MODES[$cmd] ?? null;
127127
$this->useParams = isset(self::PARAMETRIC_COMMANDS[$cmd]) || $this->useParams;
128128
return $m;

tests/Database/SqlPreprocessor.phpt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ test(function () use ($preprocessor) { // no parameters
2929
});
3030

3131

32+
test(function () use ($preprocessor) { // recognizes command in braces
33+
[$sql, $params] = $preprocessor->process(['(SELECT ?) UNION (SELECT ?)', 1, 2]);
34+
Assert::same('(SELECT ?) UNION (SELECT ?)', $sql);
35+
Assert::same([1, 2], $params);
36+
});
37+
38+
3239
test(function () use ($preprocessor) { // arg without placeholder
3340
[$sql, $params] = $preprocessor->process(['SELECT id FROM author WHERE id =', 11]);
3441
Assert::same('SELECT id FROM author WHERE id = ?', $sql);

0 commit comments

Comments
 (0)