Skip to content
This repository
Browse code

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

  • Loading branch information...
commit 2facc31e1f82bf9c6146844157425e26eb9f9579 1 parent 19772f3
Ingo Schommer authored March 14, 2012
7  search/filters/EndsWithFilter.php
@@ -25,7 +25,12 @@ class EndsWithFilter extends SearchFilter {
25 25
 	 */
26 26
 	public function apply(DataQuery $query) {
27 27
 		$this->model = $query->applyRelation($this->relation);
28  
-		return $query->where($this->getDbName() . " LIKE '%" . Convert::raw2sql($this->getValue()) . "'");
  28
+		return $query->where(sprintf(
  29
+			"%s %s '%%%s'",
  30
+			$this->getDbName(),
  31
+			(DB::getConn() instanceof PostgreSQLDatabase) ? 'ILIKE' : 'LIKE',
  32
+			Convert::raw2sql($this->getValue())
  33
+		));
29 34
 	}
30 35
 	
31 36
 	public function isEmpty() {
5  search/filters/PartialMatchFilter.php
@@ -15,13 +15,14 @@ class PartialMatchFilter extends SearchFilter {
15 15
 	public function apply(DataQuery $query) {
16 16
 		$this->model = $query->applyRelation($this->relation);
17 17
 		$where = array();
  18
+		$comparison = (DB::getConn() instanceof PostgreSQLDatabase) ? 'ILIKE' : 'LIKE';
18 19
 		if(is_array($this->getValue())) {
19 20
 			foreach($this->getValue() as $value) {
20  
-				$where[]= sprintf("%s LIKE '%%%s%%'", $this->getDbName(), Convert::raw2sql($value));
  21
+				$where[]= sprintf("%s %s '%%%s%%'", $this->getDbName(), $comparison, Convert::raw2sql($value));
21 22
 			}
22 23
 
23 24
 		} else {
24  
-			$where[] = sprintf("%s LIKE '%%%s%%'", $this->getDbName(), Convert::raw2sql($this->getValue()));
  25
+			$where[] = sprintf("%s %s '%%%s%%'", $this->getDbName(), $comparison, Convert::raw2sql($this->getValue()));
25 26
 		}
26 27
 
27 28
 		return $query->where(implode(' OR ', $where));
7  search/filters/StartsWithFilter.php
@@ -25,7 +25,12 @@ class StartsWithFilter extends SearchFilter {
25 25
 	 */
26 26
 	public function apply(DataQuery $query) {
27 27
 		$this->model = $query->applyRelation($this->relation);
28  
-		return $query->where($this->getDbName() . " LIKE '" . Convert::raw2sql($this->getValue()) . "%'");
  28
+		return $query->where(sprintf(
  29
+			"%s %s '%s%%'",
  30
+			$this->getDbName(),
  31
+			(DB::getConn() instanceof PostgreSQLDatabase) ? 'ILIKE' : 'LIKE',
  32
+			Convert::raw2sql($this->getValue())
  33
+		));
29 34
 	}
30 35
 	
31 36
 	public function isEmpty() {
26  tests/search/SearchContextTest.php
@@ -135,6 +135,32 @@ function testCanGenerateQueryUsingAllFilterTypes() {
135 135
 		$this->assertEquals(1, $results->Count());
136 136
 		$this->assertEquals("Filtered value", $results->First()->HiddenValue);
137 137
 	}
  138
+
  139
+	function testStartsWithFilterCaseInsensitive() {
  140
+		$all = singleton("SearchContextTest_AllFilterTypes");
  141
+		$context = $all->getDefaultSearchContext();
  142
+		$params = array(
  143
+			"StartsWith" => "12345-6789 camelcase", // spelled lowercase
  144
+		);
  145
+
  146
+		$results = $context->getResults($params);
  147
+		$this->assertEquals(1, $results->Count());
  148
+		$this->assertEquals("Filtered value", $results->First()->HiddenValue);
  149
+	}
  150
+
  151
+	function testEndsWithFilterCaseInsensitive() {
  152
+		$all = singleton("SearchContextTest_AllFilterTypes");
  153
+		$context = $all->getDefaultSearchContext();
  154
+		$params = array(
  155
+			"EndsWith" => "IJKL", // spelled uppercase
  156
+		);
  157
+
  158
+		$results = $context->getResults($params);
  159
+		$this->assertEquals(1, $results->Count());
  160
+		$this->assertEquals("Filtered value", $results->First()->HiddenValue);
  161
+	}
  162
+
  163
+
138 164
 	
139 165
 }
140 166
 
2  tests/search/SearchContextTest.yml
@@ -63,6 +63,6 @@ SearchContextTest_AllFilterTypes:
63 63
         Negation: Shouldnt match me
64 64
         HiddenValue: Filtered value
65 65
         CollectionMatch: ExistingCollectionValue
66  
-        StartsWith: 12345-6789
  66
+        StartsWith: 12345-6789 CamelCase
67 67
         EndsWith: abcd-efgh-ijkl
68 68
         FulltextField: one two three

0 notes on commit 2facc31

Please sign in to comment.
Something went wrong with that request. Please try again.