diff --git a/search/filters/EndsWithFilter.php b/search/filters/EndsWithFilter.php index f33da6099..74539da3e 100644 --- a/search/filters/EndsWithFilter.php +++ b/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() { diff --git a/search/filters/PartialMatchFilter.php b/search/filters/PartialMatchFilter.php index a37775cce..418e1975c 100644 --- a/search/filters/PartialMatchFilter.php +++ b/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)); diff --git a/search/filters/StartsWithFilter.php b/search/filters/StartsWithFilter.php index d8d3f9527..e81dbcbd8 100644 --- a/search/filters/StartsWithFilter.php +++ b/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() { diff --git a/tests/search/SearchContextTest.php b/tests/search/SearchContextTest.php index 2e3156e85..74eb684f2 100644 --- a/tests/search/SearchContextTest.php +++ b/tests/search/SearchContextTest.php @@ -135,6 +135,32 @@ class SearchContextTest extends SapphireTest { $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); + } + + } diff --git a/tests/search/SearchContextTest.yml b/tests/search/SearchContextTest.yml index d89505991..a807625c5 100644 --- a/tests/search/SearchContextTest.yml +++ b/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 \ No newline at end of file