Skip to content

Commit

Permalink
Use TempTable methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
mfb committed Mar 14, 2019
1 parent be28d3e commit df84458
Show file tree
Hide file tree
Showing 12 changed files with 64 additions and 83 deletions.
9 changes: 4 additions & 5 deletions CRM/Activity/BAO/Activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,8 @@ public static function deprecatedGetActivities($input) {

$config = CRM_Core_Config::singleton();

$activityTempTable = CRM_Utils_SQL_TempTable::build()->setCategory('actdetail')->getName();
$tempTable = CRM_Utils_SQL_TempTable::build()->setCategory('actdetail')->setMemory()->setUtf8();
$activityTempTable = $tempTable->getName();

$tableFields = array(
'activity_id' => 'int unsigned',
Expand All @@ -911,12 +912,11 @@ public static function deprecatedGetActivities($input) {
'campaign_id' => 'int unsigned',
);

$sql = "CREATE TEMPORARY TABLE {$activityTempTable} ( ";
$insertValueSQL = $selectColumns = array();
// The activityTempTable contains the sorted rows
// so in order to maintain the sort order as-is we add an auto_increment
// field; we can sort by this later to ensure the sort order stays correct.
$sql .= " fixed_sort_order INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,";
$sql = " fixed_sort_order INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,";
foreach ($tableFields as $name => $desc) {
$sql .= "$name $desc,\n";
$insertValueSQL[] = $name;
Expand All @@ -933,10 +933,9 @@ public static function deprecatedGetActivities($input) {
// fixed_sort_order field
$sql .= "
UNIQUE KEY ( activity_id )
) ENGINE=HEAP DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci
";

CRM_Core_DAO::executeQuery($sql);
$tempTable->createWithColumns($sql);

$insertSQL = "INSERT IGNORE INTO {$activityTempTable} (" . implode(',', $insertValueSQL) . " ) ";

Expand Down
15 changes: 6 additions & 9 deletions CRM/Contact/Form/Search/Custom/FullText.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ public function initialize() {
}

public function buildTempTable() {
$randomNum = md5(uniqid());
$this->_tableName = "civicrm_temp_custom_details_{$randomNum}";
$table = CRM_Utils_SQL_TempTable::build()->setCategory('custom')->setMemory()->setUtf8();
$this->_tableName = $table->getName();

$this->_tableFields = array(
'id' => 'int unsigned NOT NULL AUTO_INCREMENT',
Expand Down Expand Up @@ -200,7 +200,6 @@ public function buildTempTable() {
);

$sql = "
CREATE TEMPORARY TABLE {$this->_tableName} (
";

foreach ($this->_tableFields as $name => $desc) {
Expand All @@ -209,21 +208,19 @@ public function buildTempTable() {

$sql .= "
PRIMARY KEY ( id )
) ENGINE=HEAP DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci
";
CRM_Core_DAO::executeQuery($sql);
$table->createWithColumns($sql);

$this->_entityIDTableName = "civicrm_temp_custom_entityID_{$randomNum}";
$entityIdTable = CRM_Utils_SQL_TempTable::build()->setCategory('custom')->setMemory()->setUtf8();
$this->_entityIDTableName = $entityIdTable->getName();
$sql = "
CREATE TEMPORARY TABLE {$this->_entityIDTableName} (
id int unsigned NOT NULL AUTO_INCREMENT,
entity_id int unsigned NOT NULL,
UNIQUE INDEX unique_entity_id ( entity_id ),
PRIMARY KEY ( id )
) ENGINE=HEAP DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci
";
CRM_Core_DAO::executeQuery($sql);
$entityIdTable->createWithColumns($sql);

if (!empty($this->_formValues['is_unit_test'])) {
$this->_tableNameForTest = $this->_tableName;
Expand Down
4 changes: 1 addition & 3 deletions CRM/Report/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -1155,9 +1155,7 @@ public function createTemporaryTable($identifier, $sql) {
$name = $tempTable->getName();
// Developers may force tables to be durable to assist in debugging so lets check.
$isNotTrueTemporary = $tempTable->isDurable();
// The TempTable build routine adds the next line - we output it to help developers see what has happened.
$sql = 'CREATE ' . ($isNotTrueTemporary ? '' : 'TEMPORARY ') . "TABLE $name DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci " . $sql;
$this->addToDeveloperTab($sql);
$this->addToDeveloperTab($tempTable->getCreateSql());
$this->temporaryTables[$identifier] = ['temporary' => !$isNotTrueTemporary, 'name' => $name];
return $name;
}
Expand Down
20 changes: 10 additions & 10 deletions CRM/Report/Form/ActivitySummary.php
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,8 @@ public function postProcess() {
CRM_Utils_Hook::alterReportVar('sql', $this, $this);

// store the duration count in temp table
$this->_tempTableName = CRM_Core_DAO::createTempTableName('civicrm_activity');
$tempTable = CRM_Utils_SQL_TempTable::build()->setCategory('activity')->setUtf8();
$this->_tempTableName = $tempTable->getName();

// build temporary table column names base on column headers of result
$dbColumns = array();
Expand All @@ -542,10 +543,9 @@ public function postProcess() {
}

// create temp table to store main result
$tempQuery = "CREATE TEMPORARY TABLE {$this->_tempTableName} (
id int unsigned NOT NULL AUTO_INCREMENT, " . implode(', ', $dbColumns) . ' , PRIMARY KEY (id))'
. $this->_databaseAttributes;
CRM_Core_DAO::executeQuery($tempQuery);
$tempTable->createWithColumns("
id int unsigned NOT NULL AUTO_INCREMENT, " . implode(', ', $dbColumns) . ' , PRIMARY KEY (id)'
);

// build main report query
$sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy} {$this->_limit}";
Expand All @@ -565,11 +565,11 @@ public function postProcess() {
$sql = "SELECT SUM(activity_civireport.duration) as civicrm_activity_duration_total {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy} {$this->_limit}";

// create temp table to store duration
$this->_tempDurationSumTableName = CRM_Core_DAO::createTempTableName('civicrm_activity');
$tempQuery = "CREATE TEMPORARY TABLE {$this->_tempDurationSumTableName} (
id int unsigned NOT NULL AUTO_INCREMENT, civicrm_activity_duration_total VARCHAR(128), PRIMARY KEY (id))"
. $this->_databaseAttributes;
CRM_Core_DAO::executeQuery($tempQuery);
$tempTable = CRM_Utils_SQL_TempTable::build()->setCategory('activity')->setUtf8();
$this->_tempDurationSumTableName = $tempTable->getName();
$tempTable->createWithColumns("
id int unsigned NOT NULL AUTO_INCREMENT, civicrm_activity_duration_total VARCHAR(128), PRIMARY KEY (id)"
);

// store the result in temporary table
$insertQuery = "INSERT INTO {$this->_tempDurationSumTableName} (civicrm_activity_duration_total)
Expand Down
8 changes: 4 additions & 4 deletions CRM/Report/Form/Contribute/Bookkeeping.php
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,8 @@ public function groupBy() {
*/
public function statistics(&$rows) {
$statistics = parent::statistics($rows);
$tempTableName = CRM_Core_DAO::createTempTableName('civicrm_contribution');
$tempTable = CRM_Utils_SQL_TempTable::build()->setCategory('contribution')->setUtf8();
$tempTableName = $tempTable->getName();
$financialSelect = "CASE WHEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.entity_id IS NOT NULL
THEN {$this->_aliases['civicrm_entity_financial_trxn']}_item.amount
ELSE {$this->_aliases['civicrm_entity_financial_trxn']}.amount
Expand All @@ -618,9 +619,8 @@ public function statistics(&$rows) {

$this->groupBy();

$tempQuery = "CREATE TEMPORARY TABLE {$tempTableName} {$this->_databaseAttributes} AS
{$select} {$this->_from} {$this->_where} {$this->_groupBy} ";
CRM_Core_DAO::executeQuery($tempQuery);
$tempTable->createWithQuery("
{$select} {$this->_from} {$this->_where} {$this->_groupBy} ");

$sql = "SELECT COUNT(trxnID) as count, SUM(amount) as amount, currency
FROM {$tempTableName}
Expand Down
28 changes: 15 additions & 13 deletions CRM/Report/Form/Contribute/Detail.php
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ public function statistics(&$rows) {
public function buildQuery($applyLimit = FALSE) {
if ($this->isTempTableBuilt) {
$this->limit();
return "SELECT SQL_CALC_FOUND_ROWS * FROM civireport_contribution_detail_temp3 $this->_orderBy $this->_limit";
return "SELECT SQL_CALC_FOUND_ROWS * FROM {$this->temporaryTables['civireport_contribution_detail_temp3']['name']} $this->_orderBy $this->_limit";
}
return parent::buildQuery($applyLimit);
}
Expand Down Expand Up @@ -544,9 +544,10 @@ public function beginPostProcessCommon() {
// we inner join with temp1 to restrict soft contributions to those in temp1 table.
// no group by here as we want to display as many soft credit rows as actually exist.
$sql = "{$select} {$this->_from} {$this->_where} $this->_groupBy";
$tempQuery = "CREATE TEMPORARY TABLE civireport_contribution_detail_temp2 {$this->_databaseAttributes} AS {$sql}";
$this->executeReportQuery($tempQuery);
$this->temporaryTables['civireport_contribution_detail_temp2'] = ['name' => 'civireport_contribution_detail_temp2', 'temporary' => TRUE];
$tempTable = CRM_Utils_SQL_TempTable::build()->setCategory('civireport')->setUtf8();
$tempTable->createWithQuery($sql);
$this->addToDeveloperTab($tempTable->getCreateSql());
$this->temporaryTables['civireport_contribution_detail_temp2'] = ['name' => $tempTable->getName(), 'temporary' => TRUE];

if (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) ==
'soft_credits_only'
Expand All @@ -568,24 +569,25 @@ public function beginPostProcessCommon() {
// 3. Decide where to populate temp3 table from
if ($this->isContributionBaseMode
) {
$this->executeReportQuery(
"CREATE TEMPORARY TABLE civireport_contribution_detail_temp3 {$this->_databaseAttributes} AS (SELECT * FROM {$this->temporaryTables['civireport_contribution_detail_temp1']['name']})"
$tempTable = CRM_Utils_SQL_TempTable::build()->setCategory('civireport')->setUtf8()->createWithQuery(
"(SELECT * FROM {$this->temporaryTables['civireport_contribution_detail_temp1']['name']})"
);
}
elseif (CRM_Utils_Array::value('contribution_or_soft_value', $this->_params) ==
'soft_credits_only'
) {
$this->executeReportQuery(
"CREATE TEMPORARY TABLE civireport_contribution_detail_temp3 {$this->_databaseAttributes} AS (SELECT * FROM civireport_contribution_detail_temp2)"
$tempTable = CRM_Utils_SQL_TempTable::build()->setCategory('civireport')->setUtf8()->createWithQuery(
"(SELECT * FROM {$this->temporaryTables['civireport_contribution_detail_temp2']['name']})"
);
}
else {
$this->executeReportQuery("CREATE TEMPORARY TABLE civireport_contribution_detail_temp3 {$this->_databaseAttributes}
$tempTable = CRM_Utils_SQL_TempTable::build()->setCategory('civireport')->setUtf8()->createWithQuery("
(SELECT * FROM {$this->temporaryTables['civireport_contribution_detail_temp1']['name']})
UNION ALL
(SELECT * FROM civireport_contribution_detail_temp2)");
(SELECT * FROM {$this->temporaryTables['civireport_contribution_detail_temp2']['name']})");
}
$this->temporaryTables['civireport_contribution_detail_temp3'] = ['name' => 'civireport_contribution_detail_temp3', 'temporary' => TRUE];
$this->addToDeveloperTab($tempTable->getCreateSql());
$this->temporaryTables['civireport_contribution_detail_temp3'] = ['name' => $tempTable->getName(), 'temporary' => TRUE];
$this->isTempTableBuilt = TRUE;
}

Expand Down Expand Up @@ -730,7 +732,7 @@ public function alterDisplay(&$rows) {
) {
$query = "
SELECT civicrm_contact_id, civicrm_contact_sort_name, civicrm_contribution_total_amount_sum, civicrm_contribution_currency
FROM civireport_contribution_detail_temp2
FROM {$this->temporaryTables['civireport_contribution_detail_temp2']['name']}
WHERE civicrm_contribution_contribution_id={$row['civicrm_contribution_contribution_id']}";
$dao = CRM_Core_DAO::executeQuery($query);
$string = '';
Expand Down Expand Up @@ -834,7 +836,7 @@ public function sectionTotals() {
}

$query = $this->_select .
"$addtotals, count(*) as ct from civireport_contribution_detail_temp3 group by " .
"$addtotals, count(*) as ct from {$this->temporaryTables['civireport_contribution_detail_temp3']['name']} group by " .
implode(", ", $sectionAliases);
// initialize array of total counts
$sumcontribs = $totals = array();
Expand Down
20 changes: 10 additions & 10 deletions CRM/Report/Form/Contribute/Repeat.php
Original file line number Diff line number Diff line change
Expand Up @@ -1030,31 +1030,31 @@ protected function buildTempTables() {
{$from}
{$subWhere}
GROUP BY contribution2.{$this->contributionJoinTableColumn}, currency";
$this->tempTableRepeat1 = 'civicrm_temp_civireport_repeat1' . uniqid();
$sql = "
CREATE TEMPORARY TABLE $this->tempTableRepeat1 (
$tempTable = CRM_Utils_SQL_TempTable::build()->setCategory('civireport')->setMemory()->setUtf8();
$this->tempTableRepeat1 = $tempTable->getName();
$tempTable->createWithColumns("
{$create}
{$this->contributionJoinTableColumn} int unsigned,
total_amount_sum decimal(20,2),
total_amount_count int
) ENGINE=HEAP {$this->_databaseAttributes}";
$this->executeReportQuery($sql);
");
$this->addToDeveloperTab($tempTable->getCreateSql());
$this->executeReportQuery("INSERT INTO $this->tempTableRepeat1 {$subContributionQuery1}");

$this->executeReportQuery("
ALTER TABLE $this->tempTableRepeat1 ADD INDEX ({$this->contributionJoinTableColumn})
");

$this->tempTableRepeat2 = 'civicrm_temp_civireport_repeat2' . uniqid();
$sql = "
CREATE TEMPORARY TABLE $this->tempTableRepeat2 (
$tempTable = CRM_Utils_SQL_TempTable::build()->setCategory('civireport')->setMemory()->setUtf8();
$this->tempTableRepeat2 = $tempTable->getName();
$tempTable->createWithColumns("
{$create}
{$this->contributionJoinTableColumn} int unsigned,
total_amount_sum decimal(20,2),
total_amount_count int,
currency varchar(3)
) ENGINE=HEAP {$this->_databaseAttributes}";
$this->executeReportQuery($sql);
");
$this->addToDeveloperTab($tempTable->getCreateSql());
$sql = "INSERT INTO $this->tempTableRepeat2 {$subContributionQuery2}";
$this->executeReportQuery($sql);

Expand Down
24 changes: 13 additions & 11 deletions CRM/Report/Form/Member/ContributionDetail.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class CRM_Report_Form_Member_ContributionDetail extends CRM_Report_Form {
*/
protected $groupFilterNotOptimised = TRUE;

protected $tableName;

/**
* Class constructor.
*/
Expand Down Expand Up @@ -441,13 +443,13 @@ public function select() {

public function from() {
$this->_from = "
FROM civireport_membership_contribution_detail
FROM {$this->tableName}
INNER JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
ON (civireport_membership_contribution_detail.contribution_id = {$this->_aliases['civicrm_contribution']}.id)
ON ({$this->tableName}.contribution_id = {$this->_aliases['civicrm_contribution']}.id)
LEFT JOIN civicrm_membership {$this->_aliases['civicrm_membership']}
ON (civireport_membership_contribution_detail.membership_id = {$this->_aliases['civicrm_membership']}.id)
ON ({$this->tableName}.membership_id = {$this->_aliases['civicrm_membership']}.id)
INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
ON (civireport_membership_contribution_detail.contact_id = {$this->_aliases['civicrm_contact']}.id)
ON ({$this->tableName}.contact_id = {$this->_aliases['civicrm_contact']}.id)
LEFT JOIN civicrm_membership_status {$this->_aliases['civicrm_membership_status']}
ON {$this->_aliases['civicrm_membership_status']}.id =
{$this->_aliases['civicrm_membership']}.status_id
Expand Down Expand Up @@ -505,16 +507,16 @@ public function from() {
*/
public function tempTable($applyLimit = TRUE) {
// create temp table with contact ids,contribtuion id,membership id
$dropTempTable = 'DROP TEMPORARY TABLE IF EXISTS civireport_membership_contribution_detail';
CRM_Core_DAO::executeQuery($dropTempTable);
$tempTable = CRM_Utils_SQL_TempTable::build()->setMemory()->setCategory('civireport')->setUtf8();
$this->tableName = $tempTable->getName();
$tempTable->drop();

$sql = 'CREATE TEMPORARY TABLE civireport_membership_contribution_detail
(contribution_id int, INDEX USING HASH(contribution_id), contact_id int, INDEX USING HASH(contact_id),
membership_id int, INDEX USING HASH(membership_id), payment_id int, INDEX USING HASH(payment_id)) ENGINE=MEMORY' . $this->_databaseAttributes;
CRM_Core_DAO::executeQuery($sql);
$tempTable->createWithColumns('
contribution_id int, INDEX USING HASH(contribution_id), contact_id int, INDEX USING HASH(contact_id),
membership_id int, INDEX USING HASH(membership_id), payment_id int, INDEX USING HASH(payment_id)');

$fillTemp = "
INSERT INTO civireport_membership_contribution_detail (contribution_id, contact_id, membership_id)
INSERT INTO {$this->tableName} (contribution_id, contact_id, membership_id)
SELECT contribution.id, {$this->_aliases['civicrm_contact']}.id, m.id
FROM civicrm_contribution contribution
INNER JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
Expand Down
3 changes: 0 additions & 3 deletions tests/phpunit/CRM/Report/Form/ActivityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ public function setUp() {

public function tearDown() {
parent::tearDown();
CRM_Core_DAO::executeQuery('DROP TEMPORARY TABLE IF EXISTS civireport_contribution_detail_temp1');
CRM_Core_DAO::executeQuery('DROP TEMPORARY TABLE IF EXISTS civireport_contribution_detail_temp2');
CRM_Core_DAO::executeQuery('DROP TEMPORARY TABLE IF EXISTS civireport_contribution_detail_temp3');
CRM_Core_DAO::executeQuery('DROP TEMPORARY TABLE IF EXISTS civireport_activity_temp_target');
}

Expand Down
7 changes: 0 additions & 7 deletions tests/phpunit/CRM/Report/Form/Contribute/DetailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,6 @@ public function setUp() {
$this->quickCleanup($this->_tablesToTruncate);
}

public function tearDown() {
parent::tearDown();
CRM_Core_DAO::executeQuery('DROP TEMPORARY TABLE IF EXISTS civireport_contribution_detail_temp1');
CRM_Core_DAO::executeQuery('DROP TEMPORARY TABLE IF EXISTS civireport_contribution_detail_temp2');
CRM_Core_DAO::executeQuery('DROP TEMPORARY TABLE IF EXISTS civireport_contribution_detail_temp3');
}

/**
* @dataProvider dataProvider
* @param $reportClass
Expand Down
7 changes: 0 additions & 7 deletions tests/phpunit/CRM/Report/Form/TestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,6 @@ public function setUp() {
$this->quickCleanup($this->_tablesToTruncate);
}

public function tearDown() {
parent::tearDown();
CRM_Core_DAO::executeQuery('DROP TEMPORARY TABLE IF EXISTS civireport_contribution_detail_temp1');
CRM_Core_DAO::executeQuery('DROP TEMPORARY TABLE IF EXISTS civireport_contribution_detail_temp2');
CRM_Core_DAO::executeQuery('DROP TEMPORARY TABLE IF EXISTS civireport_contribution_detail_temp3');
}

/**
* @dataProvider dataProvider
* @param $reportClass
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/api/v3/ReportTemplateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ public function testLybuntReportWithFYData() {

$this->assertEquals(2, $rows['count'], "Report failed - the sql used to generate the results was " . print_r($rows['metadata']['sql'], TRUE));

$expected = preg_replace('/\s+/', ' ', 'DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci
$expected = preg_replace('/\s+/', ' ', 'DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci AS
SELECT SQL_CALC_FOUND_ROWS contact_civireport.id as cid FROM civicrm_contact contact_civireport INNER JOIN civicrm_contribution contribution_civireport USE index (received_date) ON contribution_civireport.contact_id = contact_civireport.id
AND contribution_civireport.is_test = 0
AND contribution_civireport.receive_date BETWEEN \'20140701000000\' AND \'20150630235959\'
Expand Down

0 comments on commit df84458

Please sign in to comment.