Skip to content

Commit

Permalink
Remove gids appended to table alias
Browse files Browse the repository at this point in the history
  • Loading branch information
Jitendra Purohit committed Dec 3, 2018
1 parent 9f6a155 commit a9e20b9
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 15 deletions.
16 changes: 5 additions & 11 deletions CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -1427,7 +1427,7 @@ public function query($count = FALSE, $sortByChar = FALSE, $groupContacts = FALS
$group->find(TRUE);

if (!isset($group->saved_search_id)) {
$tbName = "`civicrm_group_contact-{$groupId}`";
$tbName = "civicrm_group_contact";
// CRM-17254 don't retrieve extra fields if contact_id is specifically requested
// as this will add load to an intentionally light query.
// ideally this code would be removed as it appears to be to support CRM-1203
Expand Down Expand Up @@ -3020,7 +3020,7 @@ public function group($values) {
$op = strpos($op, 'IN') ? $op : ($op == '!=') ? 'NOT IN' : 'IN';
}
$groupIds = implode(',', (array) $regularGroupIDs);
$gcTable = "`civicrm_group_contact-{$groupIds}`";
$gcTable = "civicrm_group_contact-" . uniqid();
$joinClause = array("contact_a.id = {$gcTable}.contact_id");

if (strpos($op, 'IN') !== FALSE) {
Expand All @@ -3043,14 +3043,12 @@ public function group($values) {
//CRM-19589: contact(s) removed from a Smart Group, resides in civicrm_group_contact table
$groupContactCacheClause = '';
if (count($smartGroupIDs) || empty($value)) {
$isNullOp = (strpos($op, 'NULL') !== FALSE);
$gccTableAlias = "`civicrm_group_contact_cache_";
$gccTableAlias .= ($isNullOp) ? "a`" : implode(',', $smartGroupIDs) . "`";
$gccTableAlias = "civicrm_group_contact_cache";
$groupContactCacheClause = $this->addGroupContactCache($smartGroupIDs, $gccTableAlias, "contact_a", $op);
if (!empty($groupContactCacheClause)) {
if ($isNotOp) {
$groupIds = implode(',', (array) $smartGroupIDs);
$gcTable = "`civicrm_group_contact-{$groupIds}`";
$gcTable = "civicrm_group_contact";
$joinClause = array("contact_a.id = {$gcTable}.contact_id");
$this->_tables[$gcTable] = $this->_whereTables[$gcTable] = " LEFT JOIN civicrm_group_contact {$gcTable} ON (" . implode(' AND ', $joinClause) . ")";
if (strpos($op, 'IN') !== FALSE) {
Expand Down Expand Up @@ -3103,7 +3101,7 @@ public function getGroupsFromTypeCriteria($value) {
*
* @return string WHERE clause component for smart group criteria.
*/
public function addGroupContactCache($groups, $tableAlias = NULL, $joinTable = "contact_a", $op, $joinColumn = 'id') {
public function addGroupContactCache($groups, $tableAlias, $joinTable = "contact_a", $op, $joinColumn = 'id') {
$isNullOp = (strpos($op, 'NULL') !== FALSE);
$groupsIds = $groups;

Expand Down Expand Up @@ -3144,10 +3142,6 @@ public function addGroupContactCache($groups, $tableAlias = NULL, $joinTable = "
return NULL;
}

if (!$tableAlias) {
$tableAlias = "`civicrm_group_contact_cache_";
$tableAlias .= ($isNullOp) ? "a`" : implode(',', (array) $groupsIds) . "`";
}
$this->_tables[$tableAlias] = $this->_whereTables[$tableAlias] = " LEFT JOIN civicrm_group_contact_cache {$tableAlias} ON {$joinTable}.{$joinColumn} = {$tableAlias}.contact_id ";

if ($op == 'NOT IN') {
Expand Down
8 changes: 4 additions & 4 deletions tests/phpunit/CRM/Contact/BAO/GroupContactCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ public function testSmartGroupSearchBuilder() {
FALSE, FALSE, FALSE,
TRUE, FALSE
);
$expectedWhere = "`civicrm_group_contact_cache_{$group2->id}`.group_id IN (\"{$group2->id}\")";
$expectedWhere = "civicrm_group_contact_cache.group_id IN (\"{$group2->id}\")";
$this->assertContains($expectedWhere, $query->_whereClause);
$this->_assertContactIds($query, "group_id = {$group2->id}");

Expand All @@ -453,7 +453,7 @@ public function testSmartGroupSearchBuilder() {
FALSE, FALSE
);
//Assert if proper where clause is present.
$expectedWhere = "`civicrm_group_contact-{$group->id}`.group_id != {$group->id} AND `civicrm_group_contact_cache_{$group->id}`.group_id IS NULL OR ( `civicrm_group_contact_cache_{$group->id}`.contact_id NOT IN (SELECT contact_id FROM civicrm_group_contact_cache cgcc WHERE cgcc.group_id IN ( {$group->id} ) ) )";
$expectedWhere = "civicrm_group_contact.group_id != {$group->id} AND civicrm_group_contact_cache.group_id IS NULL OR ( civicrm_group_contact_cache.contact_id NOT IN (SELECT contact_id FROM civicrm_group_contact_cache cgcc WHERE cgcc.group_id IN ( {$group->id} ) ) )";
$this->assertContains($expectedWhere, $query->_whereClause);
$this->_assertContactIds($query, "group_id != {$group->id}");

Expand All @@ -464,7 +464,7 @@ public function testSmartGroupSearchBuilder() {
FALSE,
FALSE, FALSE
);
$expectedWhere = "`civicrm_group_contact_cache_{$group->id},{$group2->id}`.group_id IN (\"{$group->id}\", \"{$group2->id}\")";
$expectedWhere = "civicrm_group_contact_cache.group_id IN (\"{$group->id}\", \"{$group2->id}\")";
$this->assertContains($expectedWhere, $query->_whereClause);
$this->_assertContactIds($query, "group_id IN ({$group->id}, {$group2->id})");

Expand All @@ -475,7 +475,7 @@ public function testSmartGroupSearchBuilder() {
FALSE,
FALSE, FALSE
);
$expectedWhere = "`civicrm_group_contact-{$group->id}`.group_id NOT IN ( {$group->id} ) AND `civicrm_group_contact_cache_{$group->id}`.group_id IS NULL OR ( `civicrm_group_contact_cache_{$group->id}`.contact_id NOT IN (SELECT contact_id FROM civicrm_group_contact_cache cgcc WHERE cgcc.group_id IN ( {$group->id} ) ) )";
$expectedWhere = "civicrm_group_contact.group_id NOT IN ( {$group->id} ) AND civicrm_group_contact_cache.group_id IS NULL OR ( civicrm_group_contact_cache.contact_id NOT IN (SELECT contact_id FROM civicrm_group_contact_cache cgcc WHERE cgcc.group_id IN ( {$group->id} ) ) )";
$this->assertContains($expectedWhere, $query->_whereClause);
$this->_assertContactIds($query, "group_id NOT IN ({$group->id})");
}
Expand Down

0 comments on commit a9e20b9

Please sign in to comment.