Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

BUGFIX Case insensitive search filters for PostgreSQL (fixes #6548)

  • Loading branch information...
commit 2facc31e1f82bf9c6146844157425e26eb9f9579 1 parent 19772f3
@chillu chillu authored
View
7 search/filters/EndsWithFilter.php
@@ -25,7 +25,12 @@ class EndsWithFilter extends SearchFilter {
*/
public function apply(DataQuery $query) {
$this->model = $query->applyRelation($this->relation);
- return $query->where($this->getDbName() . " LIKE '%" . Convert::raw2sql($this->getValue()) . "'");
+ return $query->where(sprintf(
+ "%s %s '%%%s'",
+ $this->getDbName(),
+ (DB::getConn() instanceof PostgreSQLDatabase) ? 'ILIKE' : 'LIKE',
+ Convert::raw2sql($this->getValue())
+ ));
}
public function isEmpty() {
View
5 search/filters/PartialMatchFilter.php
@@ -15,13 +15,14 @@ class PartialMatchFilter extends SearchFilter {
public function apply(DataQuery $query) {
$this->model = $query->applyRelation($this->relation);
$where = array();
+ $comparison = (DB::getConn() instanceof PostgreSQLDatabase) ? 'ILIKE' : 'LIKE';
if(is_array($this->getValue())) {
foreach($this->getValue() as $value) {
- $where[]= sprintf("%s LIKE '%%%s%%'", $this->getDbName(), Convert::raw2sql($value));
+ $where[]= sprintf("%s %s '%%%s%%'", $this->getDbName(), $comparison, Convert::raw2sql($value));
}
} else {
- $where[] = sprintf("%s LIKE '%%%s%%'", $this->getDbName(), Convert::raw2sql($this->getValue()));
+ $where[] = sprintf("%s %s '%%%s%%'", $this->getDbName(), $comparison, Convert::raw2sql($this->getValue()));
}
return $query->where(implode(' OR ', $where));
View
7 search/filters/StartsWithFilter.php
@@ -25,7 +25,12 @@ class StartsWithFilter extends SearchFilter {
*/
public function apply(DataQuery $query) {
$this->model = $query->applyRelation($this->relation);
- return $query->where($this->getDbName() . " LIKE '" . Convert::raw2sql($this->getValue()) . "%'");
+ return $query->where(sprintf(
+ "%s %s '%s%%'",
+ $this->getDbName(),
+ (DB::getConn() instanceof PostgreSQLDatabase) ? 'ILIKE' : 'LIKE',
+ Convert::raw2sql($this->getValue())
+ ));
}
public function isEmpty() {
View
26 tests/search/SearchContextTest.php
@@ -135,6 +135,32 @@ function testCanGenerateQueryUsingAllFilterTypes() {
$this->assertEquals(1, $results->Count());
$this->assertEquals("Filtered value", $results->First()->HiddenValue);
}
+
+ function testStartsWithFilterCaseInsensitive() {
+ $all = singleton("SearchContextTest_AllFilterTypes");
+ $context = $all->getDefaultSearchContext();
+ $params = array(
+ "StartsWith" => "12345-6789 camelcase", // spelled lowercase
+ );
+
+ $results = $context->getResults($params);
+ $this->assertEquals(1, $results->Count());
+ $this->assertEquals("Filtered value", $results->First()->HiddenValue);
+ }
+
+ function testEndsWithFilterCaseInsensitive() {
+ $all = singleton("SearchContextTest_AllFilterTypes");
+ $context = $all->getDefaultSearchContext();
+ $params = array(
+ "EndsWith" => "IJKL", // spelled uppercase
+ );
+
+ $results = $context->getResults($params);
+ $this->assertEquals(1, $results->Count());
+ $this->assertEquals("Filtered value", $results->First()->HiddenValue);
+ }
+
+
}
View
2  tests/search/SearchContextTest.yml
@@ -63,6 +63,6 @@ SearchContextTest_AllFilterTypes:
Negation: Shouldnt match me
HiddenValue: Filtered value
CollectionMatch: ExistingCollectionValue
- StartsWith: 12345-6789
+ StartsWith: 12345-6789 CamelCase
EndsWith: abcd-efgh-ijkl
FulltextField: one two three
Please sign in to comment.
Something went wrong with that request. Please try again.