Skip to content

Commit

Permalink
Use TempTable methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
mfb authored and eileenmcnaughton committed Mar 22, 2019
1 parent adf139c commit 0384322
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
25 changes: 19 additions & 6 deletions CRM/Report/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -1146,18 +1146,31 @@ public function cleanUpTemporaryTables() {
* This function creates a table AND adds the details to the developer tab & $this->>temporary tables.
*
* @param string $identifier
* @param $sql
* This is the key that will be used for the table in the temporaryTables property.
* @param string $sql
* Sql select statement or column description (the latter requires the columns flag)
* @param bool $isColumns
* Is the sql describing columns to create (rather than using a select query).
* @param bool $isMemory
* Create a memory table rather than a normal INNODB table.
*
* @return string
*/
public function createTemporaryTable($identifier, $sql) {
$tempTable = CRM_Utils_SQL_TempTable::build()->setUtf8(TRUE)->createWithQuery($sql);
public function createTemporaryTable($identifier, $sql, $isColumns = FALSE, $isMemory = FALSE) {
$tempTable = CRM_Utils_SQL_TempTable::build()->setUtf8();
if ($isMemory) {
$tempTable->setMemory();
}
if ($isColumns) {
$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
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
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 0384322

Please sign in to comment.