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

This commit is contained in:
Ingo Schommer 2012-03-14 16:05:28 +01:00
parent 19772f3c04
commit 2facc31e1f
5 changed files with 42 additions and 5 deletions

View File

@ -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 File

@ -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 File

@ -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 File

@ -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);
}
}

View File

@ -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