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

dev/core#748 Switch alphabetQuery to use new getSearchSQLParts() function #13772

Merged
merged 1 commit into from
Mar 21, 2019
Merged
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
45 changes: 22 additions & 23 deletions CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -1384,6 +1384,8 @@ public function query($count = FALSE, $sortByChar = FALSE, $groupContacts = FALS
}
}
elseif ($sortByChar) {
// @fixme add the deprecated warning back in (it breaks CRM_Contact_SelectorTest::testSelectorQuery)
// 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 @@ -4948,18 +4950,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 @@ -6270,16 +6273,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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

additionalFromClause is no longer required in this signature - I haven't removed to keep this from going stale but we should do as a follow up

$orderByArray = [];
$orderBy = '';

Expand Down Expand Up @@ -6315,9 +6316,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 @@ -6397,10 +6395,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 @@ -6663,6 +6657,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 @@ -6749,14 +6748,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