From 1cde537607b0c59021e6d17ba37db0603a077d01 Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Mon, 18 Nov 2019 06:54:46 +1100 Subject: [PATCH] dev/core#183 Convert the generating of temporary tables within the random segment custom search to using standard CRM_Utils_SQL_TemporaryTable method --- .../Form/Search/Custom/RandomSegment.php | 61 ++++++++----------- 1 file changed, 26 insertions(+), 35 deletions(-) diff --git a/CRM/Contact/Form/Search/Custom/RandomSegment.php b/CRM/Contact/Form/Search/Custom/RandomSegment.php index f52371cb7510..127d0c3eb52c 100644 --- a/CRM/Contact/Form/Search/Custom/RandomSegment.php +++ b/CRM/Contact/Form/Search/Custom/RandomSegment.php @@ -142,8 +142,10 @@ public function all( */ public function from() { //define table name - $randomNum = md5(uniqid()); - $this->_tableName = "civicrm_temp_custom_{$randomNum}"; + $this->_Xg_table = CRM_Utils_SQL_TempTable::build()->setMemory()->setCategory('xgSegment'); + $this->_Xg_tableName = $this->_Xg_table->getName(); + $this->_Ig_table = CRM_Utils_SQL_TempTable::build()->setMemory()->setCategory('IgSegment'); + $this->_Ig_tableName = $this->_Ig_table->getName(); //block for Group search $smartGroup = []; @@ -172,14 +174,13 @@ public function from() { $xGroups = 0; } - $sql = "DROP TEMPORARY TABLE IF EXISTS Xg_{$this->_tableName}"; - CRM_Core_DAO::executeQuery($sql); - $sql = "CREATE TEMPORARY TABLE Xg_{$this->_tableName} ( contact_id int primary key) ENGINE=HEAP"; + $sql = "DROP TEMPORARY TABLE IF EXISTS {$this->_Xg_tableName}"; CRM_Core_DAO::executeQuery($sql); + $this->_Xg_table->createWithColumns('contact_id int PRIMARY KEY'); //used only when exclude group is selected if ($xGroups != 0) { - $excludeGroup = "INSERT INTO Xg_{$this->_tableName} ( contact_id ) + $excludeGroup = "INSERT INTO {$this->_Xg_tableName} ( contact_id ) SELECT DISTINCT civicrm_group_contact.contact_id FROM civicrm_group_contact WHERE @@ -199,29 +200,18 @@ public function from() { SELECT contact_id FROM civicrm_group_contact WHERE civicrm_group_contact.group_id = {$values} AND civicrm_group_contact.status = 'Removed')"; - $smartGroupQuery = " INSERT IGNORE INTO Xg_{$this->_tableName}(contact_id) $smartSql"; + $smartGroupQuery = " INSERT IGNORE INTO {$this->_Xg_tableName}(contact_id) $smartSql"; CRM_Core_DAO::executeQuery($smartGroupQuery); } } } - $sql = "DROP TEMPORARY TABLE IF EXISTS Ig_{$this->_tableName}"; + $sql = "DROP TEMPORARY TABLE IF EXISTS {$this->_Ig_tableName}"; CRM_Core_DAO::executeQuery($sql); - $sql = "CREATE TEMPORARY TABLE Ig_{$this->_tableName} - ( id int PRIMARY KEY AUTO_INCREMENT, - contact_id int, - group_names varchar(64)) ENGINE=HEAP"; + $this->_Ig_table->createWithColumns("id int PRIMARY KEY AUTO_INCREMENT, contact_id int, group_names varchar(64)"); - if ($this->_debug > 0) { - print "-- Include groups query:
";
-      print "$sql;";
-      print "
"; - } - - CRM_Core_DAO::executeQuery($sql); - - $includeGroup = "INSERT INTO Ig_{$this->_tableName} (contact_id, group_names) + $includeGroup = "INSERT INTO {$this->_Ig_tableName} (contact_id, group_names) SELECT civicrm_group_contact.contact_id, civicrm_group.name as group_name FROM civicrm_group_contact LEFT JOIN civicrm_group @@ -229,8 +219,8 @@ public function from() { //used only when exclude group is selected if ($xGroups != 0) { - $includeGroup .= " LEFT JOIN Xg_{$this->_tableName} - ON civicrm_group_contact.contact_id = Xg_{$this->_tableName}.contact_id"; + $includeGroup .= " LEFT JOIN {$this->_Xg_tableName} + ON civicrm_group_contact.contact_id = {$this->_Xg_tableName}.contact_id"; } $includeGroup .= " WHERE civicrm_group_contact.status = 'Added' AND @@ -238,7 +228,7 @@ public function from() { //used only when exclude group is selected if ($xGroups != 0) { - $includeGroup .= " AND Xg_{$this->_tableName}.contact_id IS null"; + $includeGroup .= " AND {$this->_Xg_tableName}.contact_id IS null"; } if ($this->_debug > 0) { @@ -263,18 +253,18 @@ public function from() { //used only when exclude group is selected if ($xGroups != 0) { - $smartSql .= " AND contact_a.id NOT IN (SELECT contact_id FROM Xg_{$this->_tableName})"; + $smartSql .= " AND contact_a.id NOT IN (SELECT contact_id FROM {$this->_Xg_tableName})"; } - $smartGroupQuery = " INSERT IGNORE INTO Ig_{$this->_tableName}(contact_id) + $smartGroupQuery = " INSERT IGNORE INTO {$this->_Ig_tableName} (contact_id) $smartSql"; CRM_Core_DAO::executeQuery($smartGroupQuery); - $insertGroupNameQuery = "UPDATE IGNORE Ig_{$this->_tableName} + $insertGroupNameQuery = "UPDATE IGNORE {$this->_Ig_tableName} SET group_names = (SELECT title FROM civicrm_group WHERE civicrm_group.id = $values) - WHERE Ig_{$this->_tableName}.contact_id IS NOT NULL - AND Ig_{$this->_tableName}.group_names IS NULL"; + WHERE {$this->_Ig_tableName}.contact_id IS NOT NULL + AND {$this->_Ig_tableName}.group_names IS NULL"; CRM_Core_DAO::executeQuery($insertGroupNameQuery); } } @@ -284,13 +274,14 @@ public function from() { $fromTail = "LEFT JOIN civicrm_email ON ( contact_a.id = civicrm_email.contact_id AND civicrm_email.is_primary = 1 )"; - $fromTail .= " INNER JOIN Ig_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; + $fromTail .= " INNER JOIN {$this->_Ig_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)"; // now create a temp table to store the randomized contacts - $sql = "DROP TEMPORARY TABLE IF EXISTS random_{$this->_tableName}"; - CRM_Core_DAO::executeQuery($sql); - $sql = "CREATE TEMPORARY TABLE random_{$this->_tableName} ( id int primary key ) ENGINE=HEAP"; + $this->_rand_table = CRM_Utils_SQL_TempTable::build()->setMemory()->setCategory('randSegment'); + $this->_rand_tableName = $this->_rand_table->getName(); + $sql = "DROP TEMPORARY TABLE IF EXISTS {$this->_rand_tableName}"; CRM_Core_DAO::executeQuery($sql); + $this->_rand_table->createWithColumns("id int PRIMARY KEY"); if (substr($this->_segmentSize, -1) == '%') { $countSql = "SELECT DISTINCT contact_a.id $from $fromTail @@ -302,14 +293,14 @@ public function from() { $this->_segmentSize = round($totalSize * $multiplier); } - $sql = "INSERT INTO random_{$this->_tableName} ( id ) + $sql = "INSERT INTO {$this->_rand_tableName} ( id ) SELECT DISTINCT contact_a.id $from $fromTail WHERE " . $this->where() . " ORDER BY RAND() LIMIT {$this->_segmentSize}"; CRM_Core_DAO::executeQuery($sql); - $from = "FROM random_{$this->_tableName} random"; + $from = "FROM {$this->_rand_tableName} random"; $from .= " INNER JOIN civicrm_contact contact_a ON random.id = contact_a.id {$this->_aclFrom}";