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 19, 2019
1 parent 5394a55 commit 8d282c1
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 49 deletions.
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
19 changes: 14 additions & 5 deletions CRM/Report/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -1147,17 +1147,26 @@ public function cleanUpTemporaryTables() {
*
* @param string $identifier
* @param $sql
* @param bool $columns
* @param bool $memory
*
* @return string
*/
public function createTemporaryTable($identifier, $sql) {
$tempTable = CRM_Utils_SQL_TempTable::build()->setUtf8(TRUE)->createWithQuery($sql);
public function createTemporaryTable($identifier, $sql, $columns = FALSE, $memory = FALSE) {
$tempTable = CRM_Utils_SQL_TempTable::build()->setUtf8();
if ($memory) {
$tempTable->setMemory();
}
if ($columns) {
$tempTable->createWithColumns($sql);
}
else {
$tempTable->createWithQuery($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
18 changes: 6 additions & 12 deletions CRM/Report/Form/ActivitySummary.php
Original file line number Diff line number Diff line change
Expand Up @@ -532,20 +532,16 @@ 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');

// build temporary table column names base on column headers of result
$dbColumns = array();
foreach ($this->_columnHeaders as $fieldName => $dontCare) {
$dbColumns[] = $fieldName . ' VARCHAR(128)';
}

// 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);
$this->_tempTableName = $this->createTemporaryTable('tempTable', "
id int unsigned NOT NULL AUTO_INCREMENT, " . implode(', ', $dbColumns) . ' , PRIMARY KEY (id)',
TRUE);

// build main report query
$sql = "{$this->_select} {$this->_from} {$this->_where} {$this->_groupBy} {$this->_having} {$this->_orderBy} {$this->_limit}";
Expand All @@ -565,11 +561,9 @@ 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);
$this->_tempDurationSumTableName = $this->createTemporaryTable('tempDurationSumTable', "
id int unsigned NOT NULL AUTO_INCREMENT, civicrm_activity_duration_total VARCHAR(128), PRIMARY KEY (id)",
TRUE);

// store the result in temporary table
$insertQuery = "INSERT INTO {$this->_tempDurationSumTableName} (civicrm_activity_duration_total)
Expand Down
14 changes: 4 additions & 10 deletions CRM/Report/Form/Contribute/Repeat.php
Original file line number Diff line number Diff line change
Expand Up @@ -1030,31 +1030,25 @@ protected function buildTempTables() {
{$from}
{$subWhere}
GROUP BY contribution2.{$this->contributionJoinTableColumn}, currency";
$this->tempTableRepeat1 = 'civicrm_temp_civireport_repeat1' . uniqid();
$sql = "
CREATE TEMPORARY TABLE $this->tempTableRepeat1 (
$this->tempTableRepeat1 = $this->createTemporaryTable('tempTableRepeat1', "
{$create}
{$this->contributionJoinTableColumn} int unsigned,
total_amount_sum decimal(20,2),
total_amount_count int
) ENGINE=HEAP {$this->_databaseAttributes}";
$this->executeReportQuery($sql);
", TRUE, TRUE);
$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 (
$this->tempTableRepeat2 = $this->createTemporaryTable('tempTableRepeat2', "
{$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);
", TRUE, TRUE);
$sql = "INSERT INTO $this->tempTableRepeat2 {$subContributionQuery2}";
$this->executeReportQuery($sql);

Expand Down
22 changes: 10 additions & 12 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,12 @@ 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);

$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);
$this->tableName = $this->createTemporaryTable('table', '
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)', TRUE, TRUE);

$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
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 8d282c1

Please sign in to comment.