Skip to content

Commit

Permalink
Merge pull request #15992 from eileenmcnaughton/utf_temp
Browse files Browse the repository at this point in the history
Update temp table handler to support utf8mb4 if that is the db collation
  • Loading branch information
mattwire authored Jan 17, 2020
2 parents 47b46fe + 25374ca commit 488fc5e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
18 changes: 18 additions & 0 deletions CRM/Core/BAO/SchemaHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -875,4 +875,22 @@ public static function migrateUtf8mb4($revert = FALSE) {
return TRUE;
}

/**
* Get the database collation.
*
* @return string
*/
public static function getDBCollation() {
return CRM_Core_DAO::singleValueQuery('SELECT @@collation_database');
}

/**
* Get the database collation.
*
* @return string
*/
public static function getDBCharset() {
return CRM_Core_DAO::singleValueQuery('SELECT @@character_set_database');
}

}
25 changes: 24 additions & 1 deletion CRM/Utils/SQL/TempTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,37 @@ public function createWithQuery($selectQuery) {
$sql = sprintf('%s %s %s AS %s',
$this->toSQL('CREATE'),
$this->memory ? self::MEMORY : self::INNODB,
$this->utf8 ? self::UTF8 : '',
$this->getUtf8String(),
($selectQuery instanceof CRM_Utils_SQL_Select ? $selectQuery->toSQL() : $selectQuery)
);
CRM_Core_DAO::executeQuery($sql, [], TRUE, NULL, TRUE, FALSE);
$this->createSql = $sql;
return $this;
}

/**
* Get the utf8 string for the table.
*
* If the db collation is already utf8 by default (either
* utf8 or utf84mb) then rely on that. Otherwise set to utf8.
*
* Respecting the DB collation supports utf8mb4 adopters, which is currently
* not the norm in civi installs.
*
* @return string
*/
public function getUtf8String() {
if (!$this->utf8) {
return '';
}
$dbUTF = CRM_Core_BAO_SchemaHandler::getDBCollation();
if (in_array($dbUTF, ['utf8_unicode_ci', 'utf8mb4_unicode_ci'])
&& in_array($dbUTF, ['utf8', 'utf8mb4'])) {
return '';
}
return self::UTF8;
}

/**
* Create the empty table.
*
Expand Down

0 comments on commit 488fc5e

Please sign in to comment.