mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
BUGFIX Case insensitive search filters for PostgreSQL (fixes #6548)
This commit is contained in:
parent
19772f3c04
commit
2facc31e1f
@ -25,7 +25,12 @@ class EndsWithFilter extends SearchFilter {
|
|||||||
*/
|
*/
|
||||||
public function apply(DataQuery $query) {
|
public function apply(DataQuery $query) {
|
||||||
$this->model = $query->applyRelation($this->relation);
|
$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() {
|
public function isEmpty() {
|
||||||
|
@ -15,13 +15,14 @@ class PartialMatchFilter extends SearchFilter {
|
|||||||
public function apply(DataQuery $query) {
|
public function apply(DataQuery $query) {
|
||||||
$this->model = $query->applyRelation($this->relation);
|
$this->model = $query->applyRelation($this->relation);
|
||||||
$where = array();
|
$where = array();
|
||||||
|
$comparison = (DB::getConn() instanceof PostgreSQLDatabase) ? 'ILIKE' : 'LIKE';
|
||||||
if(is_array($this->getValue())) {
|
if(is_array($this->getValue())) {
|
||||||
foreach($this->getValue() as $value) {
|
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 {
|
} 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));
|
return $query->where(implode(' OR ', $where));
|
||||||
|
@ -25,7 +25,12 @@ class StartsWithFilter extends SearchFilter {
|
|||||||
*/
|
*/
|
||||||
public function apply(DataQuery $query) {
|
public function apply(DataQuery $query) {
|
||||||
$this->model = $query->applyRelation($this->relation);
|
$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() {
|
public function isEmpty() {
|
||||||
|
@ -136,6 +136,32 @@ class SearchContextTest extends SapphireTest {
|
|||||||
$this->assertEquals("Filtered value", $results->First()->HiddenValue);
|
$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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
class SearchContextTest_Person extends DataObject implements TestOnly {
|
class SearchContextTest_Person extends DataObject implements TestOnly {
|
||||||
|
@ -63,6 +63,6 @@ SearchContextTest_AllFilterTypes:
|
|||||||
Negation: Shouldnt match me
|
Negation: Shouldnt match me
|
||||||
HiddenValue: Filtered value
|
HiddenValue: Filtered value
|
||||||
CollectionMatch: ExistingCollectionValue
|
CollectionMatch: ExistingCollectionValue
|
||||||
StartsWith: 12345-6789
|
StartsWith: 12345-6789 CamelCase
|
||||||
EndsWith: abcd-efgh-ijkl
|
EndsWith: abcd-efgh-ijkl
|
||||||
FulltextField: one two three
|
FulltextField: one two three
|
Loading…
Reference in New Issue
Block a user