Issue #2454733 by amateescu: Add a user-space case-insensitive collation to the SQLite driver

8.0.x
webchick 2015-03-29 11:38:44 -07:00
parent 3142e86d7c
commit ed6b8823bb
3 changed files with 27 additions and 2 deletions

View File

@ -541,6 +541,22 @@ EOD;
return $string;
}
/**
* Compares UTF-8-encoded strings in a binary safe case-insensitive manner.
*
* @param string $str1
* The first string.
* @param string $str2
* The second string.
*
* @return int
* Returns < 0 if $str1 is less than $str2; > 0 if $str1 is greater than
* $str2, and 0 if they are equal.
*/
public static function strCaseCmp($str1 , $str2) {
return strcmp(mb_strtoupper($str1, 'utf-8'), mb_strtoupper($str2, 'utf-8'));
}
/**
* Encodes MIME/HTTP headers that contain incorrectly encoded characters.
*

View File

@ -124,6 +124,9 @@ class Connection extends DatabaseConnection {
$pdo->sqliteCreateFunction('rand', array(__CLASS__, 'sqlFunctionRand'));
$pdo->sqliteCreateFunction('regexp', array(__CLASS__, 'sqlFunctionRegexp'));
// Create a user-space case-insensitive collation with UTF-8 support.
$pdo->sqliteCreateCollation('NOCASE_UTF8', array('Drupal\Component\Utility\Unicode', 'strCaseCmp'));
// Execute sqlite init_commands.
if (isset($connection_options['init_commands'])) {
$pdo->exec(implode('; ', $connection_options['init_commands']));

View File

@ -160,10 +160,16 @@ class Schema extends DatabaseSchema {
else {
$sql = $name . ' ' . $spec['sqlite_type'];
if (in_array($spec['sqlite_type'], array('VARCHAR', 'TEXT')) && isset($spec['length'])) {
if (in_array($spec['sqlite_type'], array('VARCHAR', 'TEXT'))) {
if (isset($spec['length'])) {
$sql .= '(' . $spec['length'] . ')';
}
if (isset($spec['binary']) && $spec['binary'] === FALSE) {
$sql .= ' COLLATE NOCASE_UTF8';
}
}
if (isset($spec['not null'])) {
if ($spec['not null']) {
$sql .= ' NOT NULL';