From f30aa754776f3460d76b7c8c875d00ef01b68ba3 Mon Sep 17 00:00:00 2001 From: Jitendra Purohit Date: Tue, 13 Nov 2018 15:45:23 +0530 Subject: [PATCH] Remove gids appended to table alias --- CRM/Contact/BAO/Query.php | 16 +++++----------- .../CRM/Contact/BAO/GroupContactCacheTest.php | 8 ++++---- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 861a2f2ca4..19d6ceb8b2 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -1427,7 +1427,7 @@ class CRM_Contact_BAO_Query { $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 @@ -3020,7 +3020,7 @@ class CRM_Contact_BAO_Query { $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) { @@ -3043,14 +3043,12 @@ class CRM_Contact_BAO_Query { //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) { @@ -3103,7 +3101,7 @@ class CRM_Contact_BAO_Query { * * @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; @@ -3144,10 +3142,6 @@ WHERE $smartGroupClause 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') { diff --git a/tests/phpunit/CRM/Contact/BAO/GroupContactCacheTest.php b/tests/phpunit/CRM/Contact/BAO/GroupContactCacheTest.php index 4fc0348fca..67509eb182 100644 --- a/tests/phpunit/CRM/Contact/BAO/GroupContactCacheTest.php +++ b/tests/phpunit/CRM/Contact/BAO/GroupContactCacheTest.php @@ -441,7 +441,7 @@ class CRM_Contact_BAO_GroupContactCacheTest extends CiviUnitTestCase { 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}"); @@ -453,7 +453,7 @@ class CRM_Contact_BAO_GroupContactCacheTest extends CiviUnitTestCase { 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}"); @@ -464,7 +464,7 @@ class CRM_Contact_BAO_GroupContactCacheTest extends CiviUnitTestCase { 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})"); @@ -475,7 +475,7 @@ class CRM_Contact_BAO_GroupContactCacheTest extends CiviUnitTestCase { 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})"); } -- 2.25.1