Skip to content

Commit

Permalink
Remove references to and noisly deprecated CRM_Core_DAO::createTempTa…
Browse files Browse the repository at this point in the history
…bleName
  • Loading branch information
seamuslee001 committed Nov 10, 2019
1 parent 80c8288 commit 9d62b52
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
13 changes: 5 additions & 8 deletions CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ";
}
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions CRM/Core/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
19 changes: 19 additions & 0 deletions CRM/Utils/SQL/TempTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -96,6 +97,8 @@ class CRM_Utils_SQL_TempTable {

protected $createSql;

protected $myisam;

/**
* @return CRM_Utils_SQL_TempTable
*/
Expand All @@ -108,6 +111,7 @@ public static function build() {
$t->utf8 = TRUE;
$t->autodrop = FALSE;
$t->memory = FALSE;
$t->myisam = FALSE;
return $t;
}

Expand Down Expand Up @@ -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.
*
Expand Down
15 changes: 4 additions & 11 deletions tests/phpunit/CRM/Core/DAOTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

/**
Expand Down

0 comments on commit 9d62b52

Please sign in to comment.