Skip to content

Commit

Permalink
Merge pull request #15876 from seamuslee001/dev_core_183_random_segement
Browse files Browse the repository at this point in the history
dev/core#183 Convert the generating of temporary tables within the ra…
  • Loading branch information
eileenmcnaughton authored Nov 21, 2019
2 parents 76f7a8b + 1cde537 commit 212f1cd
Showing 1 changed file with 26 additions and 35 deletions.
61 changes: 26 additions & 35 deletions CRM/Contact/Form/Search/Custom/RandomSegment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand Down Expand Up @@ -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
Expand All @@ -199,46 +200,35 @@ 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: <pre>";
print "$sql;";
print "</pre>";
}

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
ON civicrm_group_contact.group_id = civicrm_group.id";

//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
civicrm_group_contact.group_id IN($iGroups)";

//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) {
Expand All @@ -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);
}
}
Expand All @@ -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
Expand All @@ -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}";

Expand Down

0 comments on commit 212f1cd

Please sign in to comment.