Skip to content

Commit

Permalink
Fix using regex when string value containing a placeholder name
Browse files Browse the repository at this point in the history
  • Loading branch information
Tigrov committed Mar 28, 2024
1 parent 766fc54 commit e9660dc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/Expression/ExpressionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private function appendParams(string $sql, array &$expressionParams, array &$par
$patterns[] = $this->getPattern($name);
$uniqueName = $this->getUniqueName($name, $params);

$replacements[] = $uniqueName[0] !== ':' ? ":$uniqueName" : $uniqueName;
$replacements[] = '$1' . ($uniqueName[0] !== ':' ? ":$uniqueName" : $uniqueName);

$params[$uniqueName] = $value;
$expressionParams[$uniqueName] = $value;
Expand Down Expand Up @@ -104,7 +104,9 @@ private function getPattern(string $name): string
$name = ":$name";
}

return '/' . preg_quote($name, '/') . '\b/';
$skipQuotedStrings = '((?:([\'"`])(?:.*?(?:\\\\)*(?:\\\2)?)*\2.*?)*)';

return '/' . $skipQuotedStrings . preg_quote($name, '/') . '\b/';
}

private function getUniqueName(string $name, array $params): string
Expand Down
18 changes: 17 additions & 1 deletion tests/Provider/QueryBuilderProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -1262,7 +1262,7 @@ public static function update(): array
'val1' => 'Banana',
],
],
[
'Expressions with nested Expressions' => [
'{{table}}',
['name' => new Expression(
':val || :val_0',
Expand Down Expand Up @@ -1305,6 +1305,22 @@ public static function update(): array
// Wrong order of params
['Banana', 'Apple'],
],
'Expressions with a string value containing a placeholder name' => [
'{{product}}',
['price' => 10],
':val',
[':val' => new Expression("label=':val\\\'a\\\'' label1=':val\\\'a\\\'' AND name=:val", [':val' => 'Apple'])],
DbHelper::replaceQuotes(
<<<SQL
UPDATE [[product]] SET [[price]]=:qp1 WHERE label=':val\'a\'' AND name=:val_0
SQL,
static::$driverName,
),
[
':qp1' => 10,
':val_0' => 'Apple',
],
],
];
}

Expand Down

0 comments on commit e9660dc

Please sign in to comment.