diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 6e73904a0c9e..8138278a0bfd 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -4200,7 +4200,10 @@ public function relationship(&$values) { $relationshipTempTable = NULL; if (self::$_relType == 'reciprocal') { $where = []; - self::$_relationshipTempTable = $relationshipTempTable = CRM_Core_DAO::createTempTableName('civicrm_rel'); + self::$_relationshipTempTable = $relationshipTempTable = CRM_Utils_SQL_TempTable::build() + ->setCategory('civicrm_rel') + ->createWithColumns("`contact_id` int(10) unsigned NOT NULL DEFAULT '0'", "`contact_id_alt` int(10) unsigned NOT NULL DEFAULT '0'", "KEY `contact_id` (`contact_id`)", "KEY `contact_id_alt` (`contact_id_alt`)") + ->getName(); if ($nameClause) { $where[$grouping][] = " sort_name $nameClause "; } @@ -4323,13 +4326,7 @@ public function relationship(&$values) { $whereClause = str_replace('contact_b', 'c', $whereClause); } $sql = " - CREATE TEMPORARY TABLE {$relationshipTempTable} - ( - `contact_id` int(10) unsigned NOT NULL DEFAULT '0', - `contact_id_alt` int(10) unsigned NOT NULL DEFAULT '0', - KEY `contact_id` (`contact_id`), - KEY `contact_id_alt` (`contact_id_alt`) - ) + INSERT INTO {$relationshipTempTable} (contact_id, contact_id_alt) (SELECT contact_id_b as contact_id, contact_id_a as contact_id_alt, civicrm_relationship.id FROM civicrm_relationship INNER JOIN civicrm_contact c ON civicrm_relationship.contact_id_a = c.id diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index e5971188cba7..72714e73ec21 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -2128,6 +2128,7 @@ public static function setCreateDefaults(&$params, $defaults) { * @see CRM_Utils_SQL_TempTable */ public static function createTempTableName($prefix = 'civicrm', $addRandomString = TRUE, $string = NULL) { + CRM_Core_Error::deprecatedFunctionWarning('Use CRM_Utils_SQL_TempTable interface to create temporary tables'); $tableName = $prefix . "_temp"; if ($addRandomString) { diff --git a/CRM/Utils/SQL/TempTable.php b/CRM/Utils/SQL/TempTable.php index fee6fdfdfc4d..d637e16af4e0 100644 --- a/CRM/Utils/SQL/TempTable.php +++ b/CRM/Utils/SQL/TempTable.php @@ -75,6 +75,7 @@ class CRM_Utils_SQL_TempTable { const ID_REGEXP = ';^[a-zA-Z0-9_]+$;'; const INNODB = 'ENGINE=InnoDB'; const MEMORY = 'ENGINE=MEMORY'; + const MYISAM = 'ENGINE=MYISAM'; /** * @var bool @@ -96,6 +97,8 @@ class CRM_Utils_SQL_TempTable { protected $createSql; + protected $myisam; + /** * @return CRM_Utils_SQL_TempTable */ @@ -108,6 +111,7 @@ public static function build() { $t->utf8 = TRUE; $t->autodrop = FALSE; $t->memory = FALSE; + $t->myisam = FALSE; return $t; } @@ -315,6 +319,21 @@ public function setMemory($value = TRUE) { return $this; } + /** + * Set table engine to MEMORY. + * + * @param bool $value + * + * @return $this + */ + public function setMyisam($value = TRUE) { + if (CIVICRM_UF !== 'UnitTests') { + throw new \CRM_Core_Exception(ts('MySIAM Engine temporary tables can only be set within unit tests')); + } + $this->myisam = $value; + return $this; + } + /** * Set table collation to UTF8. * diff --git a/tests/phpunit/CRM/Core/DAOTest.php b/tests/phpunit/CRM/Core/DAOTest.php index 32e8e38babb3..f2be3339d229 100644 --- a/tests/phpunit/CRM/Core/DAOTest.php +++ b/tests/phpunit/CRM/Core/DAOTest.php @@ -296,21 +296,14 @@ public function testMyISAMCheck() { $this->assertEquals(1, CRM_Core_DAO::isDBMyISAM()); CRM_Core_DAO::executeQuery('DROP TABLE civicrm_my_isam'); - // A temp table should not raise flag (static naming). - $tempName = CRM_Core_DAO::createTempTableName('civicrm', FALSE); - $this->assertEquals(0, CRM_Core_DAO::isDBMyISAM()); - CRM_Core_DAO::executeQuery("CREATE TABLE $tempName (`id` int(10) unsigned NOT NULL) ENGINE = MyISAM"); - // Ignore temp tables - $this->assertEquals(0, CRM_Core_DAO::isDBMyISAM()); - CRM_Core_DAO::executeQuery("DROP TABLE $tempName"); - + // A temp table should not raise flag. + $tempTableName = CRM_Utils_SQL_TempTable::build()->setCategory('myisam')->setMyisam()->getName(); // A temp table should not raise flag (randomized naming). - $tempName = CRM_Core_DAO::createTempTableName('civicrm', TRUE); $this->assertEquals(0, CRM_Core_DAO::isDBMyISAM()); - CRM_Core_DAO::executeQuery("CREATE TABLE $tempName (`id` int(10) unsigned NOT NULL) ENGINE = MyISAM"); + CRM_Core_DAO::executeQuery("CREATE TABLE $tempTableName (`id` int(10) unsigned NOT NULL) ENGINE = MyISAM"); // Ignore temp tables $this->assertEquals(0, CRM_Core_DAO::isDBMyISAM()); - CRM_Core_DAO::executeQuery("DROP TABLE $tempName"); + CRM_Core_DAO::executeQuery("DROP TABLE $tempTableName"); } /**