mirror of
https://github.com/silverstripe/silverstripe-mssql
synced 2024-10-22 08:05:53 +02:00
BUG Support for case sensitive searches
Through newly added Database->comparisonClause() API
This commit is contained in:
parent
334c2634ac
commit
d17ae37411
@ -1559,6 +1559,50 @@ class MSSQLDatabase extends SS_Database {
|
||||
return "N'" . Convert::raw2sql($string) . "'";
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a WHERE clause for text matching.
|
||||
*
|
||||
* @param String $field Quoted field name
|
||||
* @param String $value Escaped search. Can include percentage wildcards.
|
||||
* @param boolean $exact Exact matches or wildcard support.
|
||||
* @param boolean $negate Negate the clause.
|
||||
* @param boolean $caseSensitive Enforce case sensitivity if TRUE or FALSE.
|
||||
* Stick with default collation if set to NULL.
|
||||
* @return String SQL
|
||||
*/
|
||||
public function comparisonClause($field, $value, $exact = false, $negate = false, $caseSensitive = null) {
|
||||
if($exact) {
|
||||
$comp = ($negate) ? '!=' : '=';
|
||||
} else {
|
||||
$comp = 'LIKE';
|
||||
if($negate) $comp = 'NOT ' . $comp;
|
||||
}
|
||||
|
||||
// Field definitions are case insensitive by default,
|
||||
// change used collation for case sensitive searches.
|
||||
$collateClause = '';
|
||||
if($caseSensitive === true) {
|
||||
if(self::$collation) {
|
||||
$collation = preg_replace('/_CI_/', '_CS_', self::$collation);
|
||||
} else {
|
||||
$collation = 'Latin1_General_CS_AS';
|
||||
}
|
||||
$collateClause = ' COLLATE ' . $collation;
|
||||
} elseif($caseSensitive === false) {
|
||||
if(self::$collation) {
|
||||
$collation = preg_replace('/_CS_/', '_CI_', self::$collation);
|
||||
} else {
|
||||
$collation = 'Latin1_General_CI_AS';
|
||||
}
|
||||
$collateClause = ' COLLATE ' . $collation;
|
||||
}
|
||||
|
||||
$clause = sprintf("%s %s '%s'", $field, $comp, $value);
|
||||
if($collateClause) $clause .= $collateClause;
|
||||
|
||||
return $clause;
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to return an SQL datetime expression for MSSQL
|
||||
* used for querying a datetime in a certain format
|
||||
|
Loading…
Reference in New Issue
Block a user