Merge pull request #17349 from eileenmcnaughton/validate
[civicrm-core.git] / CRM / Utils / SQL / TempTable.php
index 6ca62379c66540acf0bb9d534f105f873fc61be4..f8c4e82d563f9907e988c98baf81c50ec000c4ee 100644 (file)
@@ -125,7 +125,7 @@ class CRM_Utils_SQL_TempTable {
     $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 @@ class CRM_Utils_SQL_TempTable {
     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.
    *
@@ -252,7 +275,7 @@ class CRM_Utils_SQL_TempTable {
    */
   public function setCategory($category) {
     if ($category && !preg_match(self::CATEGORY_REGEXP, $category) || strlen($category) > self::CATEGORY_LENGTH) {
-      throw new \RuntimeException("Malformed temp table category");
+      throw new \RuntimeException("Malformed temp table category $category");
     }
     $this->category = $category;
     return $this;