Skip to content

Commit 5dbe4ba

Browse files
committed
Escape like pattern % and _ so that search containing these chars work like expected
This fixes issue #1075
1 parent 377feaf commit 5dbe4ba

File tree

3 files changed

+15
-7
lines changed

3 files changed

+15
-7
lines changed

src/DataTables/Filters/Constraints/TextConstraint.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,15 @@ public function apply(QueryBuilder $queryBuilder): void
9696

9797
//The CONTAINS, LIKE, STARTS and ENDS operators use the LIKE operator, but we have to build the value string differently
9898
$like_value = null;
99+
$escaped_value = str_replace(['%', '_'], ['\%', '\_'], $this->value);
99100
if ($this->operator === 'LIKE') {
100-
$like_value = $this->value;
101+
$like_value = $this->value; //Here we do not escape anything, as the user may provide % and _ wildcards
101102
} elseif ($this->operator === 'STARTS') {
102-
$like_value = $this->value . '%';
103+
$like_value = $escaped_value . '%';
103104
} elseif ($this->operator === 'ENDS') {
104-
$like_value = '%' . $this->value;
105+
$like_value = '%' . $escaped_value;
105106
} elseif ($this->operator === 'CONTAINS') {
106-
$like_value = '%' . $this->value . '%';
107+
$like_value = '%' . $escaped_value . '%';
107108
}
108109

109110
if ($like_value !== null) {

src/DataTables/Filters/PartSearchFilter.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ public function apply(QueryBuilder $queryBuilder): void
144144
if ($this->regex) {
145145
$queryBuilder->setParameter('search_query', $this->keyword);
146146
} else {
147+
//Escape % and _ characters in the keyword
148+
$this->keyword = str_replace(['%', '_'], ['\%', '\_'], $this->keyword);
147149
$queryBuilder->setParameter('search_query', '%' . $this->keyword . '%');
148150
}
149151
}

src/Doctrine/Functions/ILike.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ public function getSql(SqlWalker $sqlWalker): string
5656
{
5757
$platform = $sqlWalker->getConnection()->getDatabasePlatform();
5858

59-
//
6059
if ($platform instanceof AbstractMySQLPlatform || $platform instanceof SQLitePlatform) {
6160
$operator = 'LIKE';
6261
} elseif ($platform instanceof PostgreSQLPlatform) {
@@ -66,6 +65,12 @@ public function getSql(SqlWalker $sqlWalker): string
6665
throw new \RuntimeException('Platform ' . gettype($platform) . ' does not support case insensitive like expressions.');
6766
}
6867

69-
return '(' . $this->value->dispatch($sqlWalker) . ' ' . $operator . ' ' . $this->expr->dispatch($sqlWalker) . ')';
68+
$escape = "";
69+
if ($platform instanceof SQLitePlatform) {
70+
//SQLite needs ESCAPE explicitly defined backslash as escape character
71+
$escape = " ESCAPE '\\'";
72+
}
73+
74+
return '(' . $this->value->dispatch($sqlWalker) . ' ' . $operator . ' ' . $this->expr->dispatch($sqlWalker) . $escape . ')';
7075
}
71-
}
76+
}

0 commit comments

Comments
 (0)