Skip to content

Commit

Permalink
[fix] Conflict LIKE clause key #1111
Browse files Browse the repository at this point in the history
  • Loading branch information
Angelaon committed Feb 3, 2024
1 parent 55f2e23 commit 1f035fd
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Medoo.php
Original file line number Diff line number Diff line change
Expand Up @@ -942,14 +942,15 @@ protected function dataImplode(array $data, array &$map, string $conjunctor): st
$likeClauses = [];

foreach ($value as $index => $item) {
$likeKey = "{$mapKey}_{$index}_i";
$item = strval($item);

if (!preg_match('/((?<!\\\)\[.+(?<!\\\)\]|(?<!\\\)[\*\?\!\%#^_]|%.+|.+%)/', $item)) {
$item = '%' . $item . '%';
}

$likeClauses[] = $column . ($operator === '!~' ? ' NOT' : '') . " LIKE {$mapKey}L{$index}$";
$map["{$mapKey}L{$index}$"] = [$item, PDO::PARAM_STR];
$likeClauses[] = $column . ($operator === '!~' ? ' NOT' : '') . " LIKE {$likeKey}";
$map[$likeKey] = [$item, PDO::PARAM_STR];
}

$stack[] = '(' . implode($connector, $likeClauses) . ')';
Expand Down
2 changes: 2 additions & 0 deletions tests/WhereTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ public function testWildcardLikeWhere($type)

$this->database->select("account", "user_name", [
"city[~]" => "%stan",
"company[~]" => "Goo%",
"location[~]" => "Londo_",
"name[~]" => "[BCR]at",
"nickname[~]" => "[!BCR]at"
Expand All @@ -690,6 +691,7 @@ public function testWildcardLikeWhere($type)
FROM "account"
WHERE
("city" LIKE '%stan') AND
("company" LIKE 'Goo%') AND
("location" LIKE 'Londo_') AND
("name" LIKE '[BCR]at') AND
("nickname" LIKE '[!BCR]at')
Expand Down

0 comments on commit 1f035fd

Please sign in to comment.