diff --git a/CRM/Core/BAO/SchemaHandler.php b/CRM/Core/BAO/SchemaHandler.php index 591421c6f296..2f3f70241e07 100644 --- a/CRM/Core/BAO/SchemaHandler.php +++ b/CRM/Core/BAO/SchemaHandler.php @@ -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'); + } + } diff --git a/CRM/Utils/SQL/TempTable.php b/CRM/Utils/SQL/TempTable.php index 6ca62379c665..9ad78fcb0e39 100644 --- a/CRM/Utils/SQL/TempTable.php +++ b/CRM/Utils/SQL/TempTable.php @@ -125,7 +125,7 @@ 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); @@ -133,6 +133,29 @@ public function createWithQuery($selectQuery) { 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. *