Skip to content

Commit

Permalink
Switch alphabetQuery to use new getSearchSQLParts() function
Browse files Browse the repository at this point in the history
  • Loading branch information
mattwire committed Mar 19, 2019
1 parent c0dbb51 commit fa38b06
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -1384,6 +1384,7 @@ public function query($count = FALSE, $sortByChar = FALSE, $groupContacts = FALS
}
}
elseif ($sortByChar) {
CRM_Core_Error::deprecatedFunctionWarning('sort by char is deprecated - use alphabetQuery method');
$select = 'SELECT DISTINCT LEFT(contact_a.sort_name, 1) as sort_name';
$from = $this->_simpleFromClause;
}
Expand Down Expand Up @@ -4935,18 +4936,19 @@ public function searchQuery(
}

/**
* Create and query the db for a contact search.
* Create and query the db for the list of all first letters used by contacts
*
* @return CRM_Core_DAO
*/
public function alphabetQuery() {
$query = $this->getSearchSQL(NULL, NULL, NULL, FALSE, FALSE, TRUE);

$sqlParts = $this->getSearchSQLParts(NULL, NULL, NULL, FALSE, FALSE, TRUE);
$query = "SELECT DISTINCT LEFT(contact_a.sort_name, 1) as sort_name
{$this->_simpleFromClause}
{$sqlParts['where']}
{$sqlParts['having']}
GROUP BY sort_name
ORDER BY sort_name asc";
$dao = CRM_Core_DAO::executeQuery($query);

// We can always call this - it will only re-enable if it was originally enabled.
CRM_Core_DAO::reenableFullGroupByMode();

return $dao;
}

Expand Down Expand Up @@ -6257,16 +6259,14 @@ public static function processSpecialFormValue(&$formValues, $specialFields, $ch
*
* @param string|CRM_Utils_Sort $sort
* The order by string.
* @param bool $sortByChar
* If true returns the distinct array of first characters for search results.
* @param null $sortOrder
* Who knows? Hu knows. He who knows Hu knows who.
* @param string $additionalFromClause
* Should be clause with proper joins, effective to reduce where clause load.
* @return array
* list(string $orderByClause, string $additionalFromClause).
*/
protected function prepareOrderBy($sort, $sortByChar, $sortOrder, $additionalFromClause) {
protected function prepareOrderBy($sort, $sortOrder, $additionalFromClause) {
$orderByArray = [];
$orderBy = '';

Expand Down Expand Up @@ -6302,9 +6302,6 @@ protected function prepareOrderBy($sort, $sortByChar, $sortOrder, $additionalFro
}
}
}
elseif ($sortByChar) {
$orderBy = " sort_name asc";
}
else {
$orderBy = " contact_a.sort_name ASC, contact_a.id";
}
Expand Down Expand Up @@ -6393,10 +6390,6 @@ protected function prepareOrderBy($sort, $sortByChar, $sortOrder, $additionalFro

// The above code relies on crazy brittle string manipulation of a peculiarly-encoded ORDER BY
// clause. But this magic helper which forgivingly reescapes ORDER BY.
// Note: $sortByChar implies that $order was hard-coded/trusted, so it can do funky things.
if ($sortByChar) {
return array(' ORDER BY ' . $order, $additionalFromClause);
}
if ($order) {
$order = CRM_Utils_Type::escape($order, 'MysqlOrderBy');
return array(' ORDER BY ' . $order, $additionalFromClause);
Expand Down Expand Up @@ -6659,6 +6652,11 @@ public function getSearchSQL(

$sqlParts = $this->getSearchSQLParts($offset, $rowCount, $sort, $count, $includeContactIds, $sortByChar, $groupContacts, $additionalWhereClause, $sortOrder, $additionalFromClause);

if ($sortByChar) {
CRM_Core_Error::deprecatedFunctionWarning('sort by char is deprecated - use alphabetQuery method');
$sqlParts['order_by'] = 'ORDER BY sort_name asc';
}

if ($skipOrderAndLimit) {
CRM_Core_Error::deprecatedFunctionWarning('skipOrderAndLimit is deprected - call getSearchSQLParts & construct it in the calling function');
$query = "{$sqlParts['select']} {$sqlParts['from']} {$sqlParts['where']} {$sqlParts['having']} {$sqlParts['group_by']}";
Expand Down Expand Up @@ -6745,14 +6743,14 @@ public function getSearchSQLParts($offset = 0, $rowCount = 0, $sort = NULL,

$order = $orderBy = '';
if (!$count) {
list($order, $additionalFromClause) = $this->prepareOrderBy($sort, $sortByChar, $sortOrder, $additionalFromClause);
if (!$sortByChar) {
list($order, $additionalFromClause) = $this->prepareOrderBy($sort, $sortOrder, $additionalFromClause);
}
}
// 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
// Cases where we are disabling FGB (FULL_GROUP_BY_MODE):
// 1. 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($groupBy) || $groupContacts);
$disableFullGroupByMode = (!empty($groupBy) || $groupContacts);

if ($disableFullGroupByMode) {
CRM_Core_DAO::disableFullGroupByMode();
Expand Down

0 comments on commit fa38b06

Please sign in to comment.