Skip to content

Commit e254b10

Browse files
committed
Поддержка условия "или" при биндинге
1 parent 83b390c commit e254b10

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

Parser.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ private function simplifyParams($params)
128128
private function parseSql()
129129
{
130130
// Разбор многострочных комментариев
131-
if (preg_match_all('#/\*(\w+)(.+?)\*/#s', $this->sql, $matches)) {
131+
if (preg_match_all('#/\*([\w|]+)(.+?)\*/#s', $this->sql, $matches)) {
132132
$count = count($matches[0]);
133133
for ($i = 0; $i < $count; $i++) {
134134
$this->replaceComment($matches[0][$i], $matches[2][$i], $matches[1][$i]);
@@ -137,7 +137,7 @@ private function parseSql()
137137

138138
// Многоитерационный разбор однострчных комментариев
139139
while (true) {
140-
if (preg_match_all('#--\*(\w+)(.+)#', $this->sql, $matches)) {
140+
if (preg_match_all('#--\*([\w|]+)(.+)#', $this->sql, $matches)) {
141141
$count = count($matches[0]);
142142
for ($i = 0; $i < $count; $i++) {
143143
$this->replaceComment($matches[0][$i], $matches[2][$i], $matches[1][$i]);
@@ -159,7 +159,21 @@ private function parseSql()
159159
private function replaceComment($comment, $queryInComment, $paramName)
160160
{
161161
$param = $this->getParam($paramName);
162-
if ($param) {
162+
163+
if (strpos($paramName, '|')) {
164+
$found = false;
165+
166+
foreach (explode('|', $paramName) as $param) {
167+
if (array_key_exists($param, $this->params)) {
168+
$found = true;
169+
break;
170+
}
171+
}
172+
173+
if (!$found) {
174+
$queryInComment = '';
175+
}
176+
} elseif ($param) {
163177
$paramName = $param[0];
164178
$paramValue = $param[1];
165179
if (is_array($paramValue)) {

tests/SqlParserTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ public function sqlData()
1414
/*param4 --*param5 sql5 */
1515
/*param8 --*param9 --*param10 sql10 */
1616
--*param11 :@param11
17+
--*param12|param13 test multiple
18+
/*param14|param15 test multiple*/
1719
';
1820

1921
return [
@@ -29,7 +31,8 @@ public function sqlData()
2931
["-- test\n".$query, [], '/^-- test$/'],
3032
[$query, ['param11' => [['v1', 'v2']]], "/^:param11_0,:param11_1$/"],
3133
["--*param order by param", ['param' => ['v1', 'bind' => 'text']], "/^order by v1$/"],
32-
34+
[$query, ['param12' => 'test'], "/^test multiple$/"],
35+
[$query, ['param14' => 'test'], "/^test multiple$/"],
3336
];
3437
}
3538

0 commit comments

Comments
 (0)