Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(dev/core#183) Temporary tables should follow consistent naming convention #12311

Merged
merged 9 commits into from
Jun 17, 2018
5 changes: 2 additions & 3 deletions CRM/Activity/BAO/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -920,8 +920,7 @@ public static function deprecatedGetActivities($input) {

$config = CRM_Core_Config::singleton();

$randomNum = md5(uniqid());
$activityTempTable = "civicrm_temp_activity_details_{$randomNum}";
$activityTempTable = CRM_Utils_SQL_TempTable::build()->setCategory('actdetail')->getName();

$tableFields = array(
'activity_id' => 'int unsigned',
Expand Down Expand Up @@ -1012,7 +1011,7 @@ public static function deprecatedGetActivities($input) {

// step 2: Get target and assignee contacts for above activities
// create temp table for target contacts
$activityContactTempTable = "civicrm_temp_activity_contact_{$randomNum}";
$activityContactTempTable = CRM_Utils_SQL_TempTable::build()->setCategory('actcontact')->getName();
$query = "CREATE TEMPORARY TABLE {$activityContactTempTable} (
activity_id int unsigned, contact_id int unsigned, record_type_id varchar(16),
contact_name varchar(255), is_deleted int unsigned, counter int unsigned, INDEX index_activity_id( activity_id ) )
Expand Down
6 changes: 2 additions & 4 deletions CRM/Contact/Form/Search/Custom/ContribSYBNT.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,8 @@ public function all(
";

if ($justIDs) {
CRM_Core_DAO::executeQuery("DROP TEMPORARY TABLE IF EXISTS CustomSearch_SYBNT_temp");
$query = "CREATE TEMPORARY TABLE CustomSearch_SYBNT_temp AS ({$sql})";
CRM_Core_DAO::executeQuery($query);
$sql = "SELECT contact_a.id as contact_id FROM CustomSearch_SYBNT_temp as contact_a";
$tempTable = CRM_Utils_SQL_TempTable::build()->createWithQuery($sql);
$sql = "SELECT contact_a.id as contact_id FROM {$tempTable->getName()} as contact_a";
}
return $sql;
}
Expand Down
59 changes: 31 additions & 28 deletions CRM/Contact/Form/Search/Custom/DateAdded.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class CRM_Contact_Form_Search_Custom_DateAdded extends CRM_Contact_Form_Search_C
protected $_aclFrom = NULL;
protected $_aclWhere = NULL;

protected $_datesTable = NULL, $_xgTable = NULL, $_igTable = NULL;

/**
* Class constructor.
*
Expand Down Expand Up @@ -177,11 +179,12 @@ public function all(
*/
public function from() {
//define table name
$randomNum = md5(uniqid());
$this->_tableName = "civicrm_temp_custom_{$randomNum}";
$this->_datesTable = CRM_Utils_SQL_TempTable::build()->setCategory('dates')->getName();
$this->_xgTable = CRM_Utils_SQL_TempTable::build()->setCategory('xg')->getName();
$this->_igTable = CRM_Utils_SQL_TempTable::build()->setCategory('ig')->getName();

//grab the contacts added in the date range first
$sql = "CREATE TEMPORARY TABLE dates_{$this->_tableName} ( id int primary key, date_added date ) ENGINE=HEAP";
$sql = "CREATE TEMPORARY TABLE {$this->_datesTable} ( id int primary key, date_added date ) ENGINE=HEAP";
if ($this->_debug > 0) {
print "-- Date range query: <pre>";
print "$sql;";
Expand All @@ -197,7 +200,7 @@ public function from() {
$endDateFix = "AND date_added <= '" . substr($endDate, 0, 10) . " 23:59:00'";
}

$dateRange = "INSERT INTO dates_{$this->_tableName} ( id, date_added )
$dateRange = "INSERT INTO {$this->_datesTable} ( id, date_added )
SELECT
civicrm_contact.id,
min(civicrm_log.modified_date) AS date_added
Expand Down Expand Up @@ -249,16 +252,16 @@ public function from() {
$xGroups = 0;
}

$sql = "DROP TEMPORARY TABLE IF EXISTS Xg_{$this->_tableName}";
$sql = "DROP TEMPORARY TABLE IF EXISTS {$this->_xgTable}";
CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
$sql = "CREATE TEMPORARY TABLE Xg_{$this->_tableName} ( contact_id int primary key) ENGINE=HEAP";
$sql = "CREATE TEMPORARY TABLE {$this->_xgTable} ( contact_id int primary key) ENGINE=HEAP";
CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);

//used only when exclude group is selected
if ($xGroups != 0) {
$excludeGroup = "INSERT INTO Xg_{$this->_tableName} ( contact_id )
$excludeGroup = "INSERT INTO {$this->_xgTable} ( contact_id )
SELECT DISTINCT civicrm_group_contact.contact_id
FROM civicrm_group_contact, dates_{$this->_tableName} AS d
FROM civicrm_group_contact, {$this->_datesTable} AS d
WHERE
d.id = civicrm_group_contact.contact_id AND
civicrm_group_contact.status = 'Added' AND
Expand All @@ -277,16 +280,16 @@ 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->_xgTable}(contact_id) $smartSql";

CRM_Core_DAO::executeQuery($smartGroupQuery, CRM_Core_DAO::$_nullArray);
}
}
}

$sql = "DROP TEMPORARY TABLE IF EXISTS Ig_{$this->_tableName}";
$sql = "DROP TEMPORARY TABLE IF EXISTS {$this->_igTable}";
CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
$sql = "CREATE TEMPORARY TABLE Ig_{$this->_tableName}
$sql = "CREATE TEMPORARY TABLE {$this->_igTable}
( id int PRIMARY KEY AUTO_INCREMENT,
contact_id int,
group_names varchar(64)) ENGINE=HEAP";
Expand All @@ -299,26 +302,26 @@ public function from() {

CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);

$includeGroup = "INSERT INTO Ig_{$this->_tableName} (contact_id, group_names)
$includeGroup = "INSERT INTO {$this->_igTable} (contact_id, group_names)
SELECT d.id as contact_id, civicrm_group.name as group_name
FROM dates_{$this->_tableName} AS d
FROM {$this->_datesTable} AS d
INNER JOIN civicrm_group_contact
ON civicrm_group_contact.contact_id = d.id
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 d.id = Xg_{$this->_tableName}.contact_id";
$includeGroup .= " LEFT JOIN {$this->_xgTable}
ON d.id = {$this->_xgTable}.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->_xgTable}.contact_id IS null";
}

if ($this->_debug > 0) {
Expand All @@ -339,19 +342,19 @@ public function from() {

$smartSql .= " AND contact_a.id IN (
SELECT id AS contact_id
FROM dates_{$this->_tableName} )";
FROM {$this->_datesTable} )";

$smartSql .= " AND contact_a.id NOT IN (
SELECT contact_id FROM civicrm_group_contact
WHERE civicrm_group_contact.group_id = {$values} AND civicrm_group_contact.status = 'Removed')";

//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->_xgTable})";
}

$smartGroupQuery = " INSERT IGNORE INTO
Ig_{$this->_tableName}(contact_id)
{$this->_igTable}(contact_id)
$smartSql";

CRM_Core_DAO::executeQuery($smartGroupQuery, CRM_Core_DAO::$_nullArray);
Expand All @@ -360,11 +363,11 @@ public function from() {
print "$smartGroupQuery;";
print "</pre>";
}
$insertGroupNameQuery = "UPDATE IGNORE Ig_{$this->_tableName}
$insertGroupNameQuery = "UPDATE IGNORE {$this->_igTable}
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->_igTable}.contact_id IS NOT NULL
AND {$this->_igTable}.group_names IS NULL";
CRM_Core_DAO::executeQuery($insertGroupNameQuery, CRM_Core_DAO::$_nullArray);
if ($this->_debug > 0) {
print "-- Smart group query: <pre>";
Expand All @@ -380,12 +383,12 @@ public function from() {

/* We need to join to this again to get the date_added value */

$from .= " INNER JOIN dates_{$this->_tableName} d ON (contact_a.id = d.id) {$this->_aclFrom}";
$from .= " INNER JOIN {$this->_datesTable} d ON (contact_a.id = d.id) {$this->_aclFrom}";

// Only include groups in the search query of one or more Include OR Exclude groups has been selected.
// CRM-6356
if ($this->_groups) {
$from .= " INNER JOIN Ig_{$this->_tableName} temptable1 ON (contact_a.id = temptable1.contact_id)";
$from .= " INNER JOIN {$this->_igTable} temptable1 ON (contact_a.id = temptable1.contact_id)";
}

return $from;
Expand Down Expand Up @@ -437,13 +440,13 @@ public function count() {

public function __destruct() {
//drop the temp. tables if they exist
if (!empty($this->_includeGroups)) {
$sql = "DROP TEMPORARY TABLE IF EXISTS Ig_{$this->_tableName}";
if ($this->_igTable && !empty($this->_includeGroups)) {
$sql = "DROP TEMPORARY TABLE IF EXISTS {$this->_igTable}";
CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
}

if (!empty($this->_excludeGroups)) {
$sql = "DROP TEMPORARY TABLE IF EXISTS Xg_{$this->_tableName}";
if ($this->_xgTable && !empty($this->_excludeGroups)) {
$sql = "DROP TEMPORARY TABLE IF EXISTS {$this->_xgTable}";
CRM_Core_DAO::executeQuery($sql, CRM_Core_DAO::$_nullArray);
}
}
Expand Down
2 changes: 1 addition & 1 deletion CRM/Contact/Form/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public static function preProcessCommon(&$form, $useTable = FALSE) {
$form->assign('taskName', CRM_Utils_Array::value($form->_task, $crmContactTaskTasks));

if ($useTable) {
$form->_componentTable = CRM_Core_DAO::createTempTableName('civicrm_task_action', TRUE, $qfKey);
$form->_componentTable = CRM_Utils_SQL_TempTable::build()->setCategory('tskact')->setDurable()->setId($qfKey)->getName();
$sql = " DROP TABLE IF EXISTS {$form->_componentTable}";
CRM_Core_DAO::executeQuery($sql);

Expand Down
5 changes: 3 additions & 2 deletions CRM/Core/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,11 +389,12 @@ public static function clearTempTables($timeInterval = FALSE) {
WHERE TABLE_SCHEMA = %1
AND (
TABLE_NAME LIKE 'civicrm_import_job_%'
OR TABLE_NAME LIKE 'civicrm_export_temp%'
OR TABLE_NAME LIKE 'civicrm_task_action_temp%'
OR TABLE_NAME LIKE 'civicrm_report_temp%'
OR TABLE_NAME LIKE 'civicrm_tmp_d%'
)
";
// NOTE: Cannot find use-cases where "civicrm_report_temp" would be durable. Could probably remove.

if ($timeInterval) {
$query .= " AND CREATE_TIME < DATE_SUB(NOW(), INTERVAL {$timeInterval})";
}
Expand Down
2 changes: 2 additions & 0 deletions CRM/Core/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -2018,6 +2018,8 @@ public static function setCreateDefaults(&$params, $defaults) {
* @param null $string
*
* @return string
* @deprecated
* @see CRM_Utils_SQL_TempTable
*/
public static function createTempTableName($prefix = 'civicrm', $addRandomString = TRUE, $string = NULL) {
$tableName = $prefix . "_temp";
Expand Down
2 changes: 1 addition & 1 deletion CRM/Export/BAO/Export.php
Original file line number Diff line number Diff line change
Expand Up @@ -1259,7 +1259,7 @@ public static function writeDetailsToTable($tableName, &$details, &$sqlColumns)
*/
public static function createTempTable(&$sqlColumns) {
//creating a temporary table for the search result that need be exported
$exportTempTable = CRM_Core_DAO::createTempTableName('civicrm_export', TRUE);
$exportTempTable = CRM_Utils_SQL_TempTable::build()->setDurable()->setCategory('export')->getName();

// also create the sql table
$sql = "DROP TABLE IF EXISTS {$exportTempTable}";
Expand Down
2 changes: 1 addition & 1 deletion CRM/Report/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -3689,7 +3689,7 @@ public function buildGroupTempTable() {
WHERE smartgroup_contact.group_id IN ({$smartGroups}) ";
}

$this->groupTempTable = 'civicrm_report_temp_group_' . date('Ymd_') . uniqid();
$this->groupTempTable = CRM_Utils_SQL_TempTable::build()->setCategory('rptgrp')->setId(date('Ymd_') . uniqid())->getName();
$this->executeReportQuery("
CREATE TEMPORARY TABLE $this->groupTempTable $this->_databaseAttributes
$query
Expand Down
2 changes: 1 addition & 1 deletion CRM/Report/Form/Contribute/Lybunt.php
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ public function beginPostProcessCommon() {
// @todo this acl has no test coverage and is very hard to test manually so could be fragile.
$this->resetFormSqlAndWhereHavingClauses();

$this->contactTempTable = 'civicrm_report_temp_lybunt_c_' . date('Ymd_') . uniqid();
$this->contactTempTable = CRM_Utils_SQL_TempTable::build()->setCategory('rptlybunt')->setId(date('Ymd_') . uniqid())->getName();
$this->limit();
$getContacts = "
CREATE TEMPORARY TABLE $this->contactTempTable {$this->_databaseAttributes}
Expand Down
Loading