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

Improve performance of financial ACL where clause #31976

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions CRM/Contribute/BAO/Contribution.php
Original file line number Diff line number Diff line change
Expand Up @@ -4226,13 +4226,7 @@ public static function getAnnualQuery($contactIDs) {
$endDate = "$nextYear$monthDay";
$havingClause = 'contribution_status_id = ' . (int) CRM_Core_PseudoConstant::getKey('CRM_Contribute_BAO_Contribution', 'contribution_status_id', 'Completed');

$contributionBAO = new CRM_Contribute_BAO_Contribution();
$whereClauses = $contributionBAO->addSelectWhereClause();

$clauses = [];
foreach ($whereClauses as $key => $clause) {
$clauses[] = 'b.' . $key . ' ' . implode(' AND b.' . $key . ' ', (array) $clause);
}
$clauses = CRM_Contribute_BAO_Contribution::getSelectWhereClause('b');
$clauses[] = 'b.contact_id IN (' . $contactIDs . ')';
$clauses[] = 'b.is_test = 0';
$clauses[] = 'b.receive_date >=' . $startDate . ' AND b.receive_date < ' . $endDate;
Expand Down
16 changes: 9 additions & 7 deletions CRM/Utils/SQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,23 @@ public static function mergeSubquery($entityName, $joinColumn = 'id') {
$baoName = CRM_Core_DAO_AllCoreTables::getBAOClassName(CRM_Core_DAO_AllCoreTables::getDAONameForEntity($entityName));
$bao = new $baoName();
$fields = $bao::getSupportedFields();
$fieldNames = array_keys($fields);
$mergeClauses = $subClauses = [];
foreach ((array) $bao->addSelectWhereClause($entityName) as $fieldName => $fieldClauses) {
foreach ($bao->addSelectWhereClause($entityName) as $fieldName => $fieldClauses) {
if ($fieldClauses) {
foreach ((array) $fieldClauses as $fieldClause) {
$formattedClause = CRM_Utils_SQL::prefixFieldNames($fieldClause, array_keys($fields), $bao->tableName());
$originalClause = $fieldClause;
CRM_Utils_SQL::prefixFieldNames($fieldClause, $fieldNames, $bao->tableName());
// Same as join column with no additional fields - can be added directly
if ($fieldName === $joinColumn && $fieldClause === $formattedClause) {
$mergeClauses[] = $formattedClause;
if ($fieldName === $joinColumn && $originalClause === $fieldClause) {
$mergeClauses[] = $fieldClause;
}
// Arrays of arrays get joined with OR (similar to CRM_Core_Permission::check)
elseif (is_array($formattedClause)) {
$subClauses[] = "(($fieldName " . implode(") OR ($fieldName ", $formattedClause) . '))';
elseif (is_array($fieldClause)) {
$subClauses[] = "(($fieldName " . implode(") OR ($fieldName ", $fieldClause) . '))';
}
else {
$subClauses[] = "$fieldName $formattedClause";
$subClauses[] = "$fieldName $fieldClause";
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion ext/financialacls/financialacls.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function financialacls_civicrm_selectWhereClause($entity, &$clauses) {
if ($entity === 'Contribution') {
$unavailableTypes = _financialacls_civicrm_get_inaccessible_financial_types();
if (!empty($unavailableTypes)) {
$clauses['id'][] = 'NOT IN (SELECT contribution_id FROM civicrm_line_item WHERE contribution_id IS NOT NULL AND financial_type_id IN (' . implode(',', $unavailableTypes) . '))';
$clauses['id'][] = 'AND NOT EXISTS (SELECT 1 FROM civicrm_line_item WHERE contribution_id = {id} AND financial_type_id IN (' . implode(',', $unavailableTypes) . '))';
}
}
break;
Expand Down
7 changes: 4 additions & 3 deletions tests/phpunit/CRM/Contribute/BAO/ContributionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ public function testAnnualQueryWithFinancialACLsEnabled(): void {
$sql = CRM_Contribute_BAO_Contribution::getAnnualQuery([1, 2, 3]);
$this->assertStringContainsString('SUM(total_amount) as amount,', $sql);
$this->assertStringContainsString('b.contact_id IN (1,2,3)', $sql);
$this->assertStringContainsString('b.financial_type_id IN (' . $permittedFinancialType . ')', $sql);
$this->assertStringContainsString('`b`.`financial_type_id` IN (' . $permittedFinancialType . ')', $sql);

// Run it to make sure it's not bad sql.
CRM_Core_DAO::executeQuery($sql);
Expand Down Expand Up @@ -260,8 +260,9 @@ public function testAnnualQueryWithFinancialHook(): void {
$sql = CRM_Contribute_BAO_Contribution::getAnnualQuery([1, 2, 3]);
$this->assertStringContainsString('SUM(total_amount) as amount,', $sql);
$this->assertStringContainsString('b.contact_id IN (1,2,3)', $sql);
$this->assertStringContainsString('WHERE b.id NOT IN (0)', $sql);
$this->assertStringContainsString('`b`.`id` NOT IN (0)', $sql);
$this->assertStringNotContainsString('b.financial_type_id', $sql);
$this->assertStringNotContainsString('`b`.`financial_type_id`', $sql);
CRM_Core_DAO::executeQuery($sql);
}

Expand All @@ -275,7 +276,7 @@ public function aclNoZero(string $entity, array &$clauses): void {
if ($entity !== 'Contribution') {
return;
}
$clauses['id'] = 'NOT IN (0)';
$clauses['id'] = ['NOT IN (0)'];
}

/**
Expand Down