From e5167553dedef1e38a6324ab093e4aa206e7d704 Mon Sep 17 00:00:00 2001 From: "deb.monish" Date: Tue, 8 May 2018 12:06:26 +0530 Subject: [PATCH] revert some change and other fixes --- CRM/Contact/BAO/Query.php | 43 +++++++++++++-------------------------- 1 file changed, 14 insertions(+), 29 deletions(-) diff --git a/CRM/Contact/BAO/Query.php b/CRM/Contact/BAO/Query.php index 0ab46304d756..81a191cfa362 100644 --- a/CRM/Contact/BAO/Query.php +++ b/CRM/Contact/BAO/Query.php @@ -4734,7 +4734,7 @@ public static function appendAnyValueToSelect($selectClauses, $groupBy, $aggrega * on full_group_by mode, then append the those missing columns to GROUP BY clause * keyword to select fields not present in groupBy * - * @param string|array $groupBy - GROUP BY clause or array of columns where missing ORDER BY columns will be appended if not present + * @param string $groupBy - GROUP BY clause where missing ORDER BY columns will be appended if not present * @param array $orderBys - ORDER BY sub-clauses * */ @@ -4743,17 +4743,8 @@ public static function getGroupByFromOrderBy(&$groupBy, $orderBys) { foreach ($orderBys as $orderBy) { $orderBy = str_ireplace(array(' DESC', ' ASC', '`'), '', $orderBy); // remove sort syntax from ORDER BY clauses if present // if ORDER BY column is not present in GROUP BY then append it to end - if (preg_match('/(MAX|MIN)\(/i', trim($orderBy)) !== 1) { - if (is_array($groupBy)) { - // retrieve and add all the ORDER BY columns which are not in GROUP BY list of columns - $groupBy += array_filter($groupBy, - function($var) use ($orderBy) { - return !strstr($var, $orderBy); - }); - } - elseif (!strstr($groupBy, $orderBy)) { - $groupBy .= ", {$orderBy}"; - } + if (preg_match('/(MAX|MIN)\(/i', trim($orderBy)) !== 1 && !strstr($groupBy, $orderBy)) { + $groupBy .= ", {$orderBy}"; } } } @@ -4850,13 +4841,6 @@ public function searchQuery( $additionalFromClause = NULL, $skipOrderAndLimit = FALSE ) { - // In this case we are expecting the search query to return all the first single letter characters of contacts ONLY, - // but when FGB (FULL_GROUP_BY_MODE) is enabled MySQL expect the columns present in GROUP BY must be present in SELECT clause - // and that results into error and needless to have other columns. In order to resolve this disable FGB to fulfill both the cases - if ($sortByChar) { - CRM_Core_DAO::disableFullGroupByMode(); - } - if ($includeContactIds) { $this->_includeContactIds = TRUE; $this->_whereClause = $this->whereClause(); @@ -4904,6 +4888,16 @@ public function searchQuery( $limit = " LIMIT $offset, $rowCount "; } } + // Two cases where we are disabling FGB (FULL_GROUP_BY_MODE): + // 1. Expecting the search query to return all the first single letter characters of contacts ONLY, but when FGB is enabled + // MySQL expect the columns present in GROUP BY, must be present in SELECT clause and that results into error, needless to have other columns. + // 2. When GROUP BY columns are present then disable FGB otherwise it demands to add ORDER BY columns in GROUP BY and eventually in SELECT + // clause. This will impact the search query output. + $disableFullGroupByMode = ($sortByChar || !empty($groupByCols)); + + if ($disableFullGroupByMode) { + CRM_Core_DAO::disableFullGroupByMode(); + } // CRM-15231 $this->_sort = $sort; @@ -4914,15 +4908,6 @@ public function searchQuery( list($select, $from, $where, $having) = $this->query($count, $sortByChar, $groupContacts, $onlyDeleted); if (!empty($groupByCols)) { - if (!empty($order)) { - // retrieve order by columns from ORDER BY clause - $orderBys = explode(",", str_replace('ORDER BY ', '', $order)); - self::getGroupByFromOrderBy($groupByCols, $orderBys); - } - // It doesn't matter to include columns in SELECT clause, which are present in GROUP BY when we just want the contact IDs - if (!$groupContacts && !$sortByChar) { - $select = self::appendAnyValueToSelect($this->_select, $groupByCols, 'GROUP_CONCAT'); - } $groupBy = " GROUP BY " . implode(', ', $groupByCols); } @@ -4957,7 +4942,7 @@ public function searchQuery( $dao = CRM_Core_DAO::executeQuery($query); - if ($sortByChar) { + if ($disableFullGroupByMode) { CRM_Core_DAO::reenableFullGroupByMode(); }