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

Conversation

mattwire
Copy link
Contributor

@mattwire mattwire commented Mar 6, 2019

Overview

This replaces #13733 following merge of #13735. Partial based on #13713

Before

getSearchSQL is only called from one place in with $sortByChar set to TRUE and each part of the query building functions have special handling for this parameter. We disable full group by for this query as it's not supported.

After

Some of the specific handling for $sortByChar query is pulled out of the generic functions, simplifying them. We generate the specific lines of query in the one place they are actually used. We also generate a GROUP BY clause that is compatible with fullgroupby.

Technical Details

$sortByChar is used only for a single, very specific query to generate the list of characters which should be displayed as dynamic filters at the top of search results. The code which builds the search queries is over-complex and this helps simplify it.

Comments

@eileenmcnaughton Replacement for #13733 based on your work in #13735

@civibot
Copy link

civibot bot commented Mar 6, 2019

(Standard links)

@eileenmcnaughton
Copy link
Contributor

This looks good! The craziness of this legacy code is starting to dissapate

I'll pull it down & give it a spin once jenkins has passed judgement

@eileenmcnaughton
Copy link
Contributor

Just noting the alphabet query does have test coverage

@seamuslee001
Copy link
Contributor

Jenkins re test this please

@eileenmcnaughton
Copy link
Contributor

eileenmcnaughton commented Mar 6, 2019

@mattwire I went through & did the same refactor as you & here is a patch of mine against yours

--- a/CRM/Contact/BAO/Query.php
+++ b/CRM/Contact/BAO/Query.php
@@ -4924,11 +4924,15 @@ civicrm_relationship.start_date > {$today}
    * @return CRM_Core_DAO
    */
   public function alphabetQuery() {
-    $sqlParts = $this->getSearchSQLParts(NULL, NULL, NULL, FALSE, FALSE, TRUE);
-    $sqlParts['group_by'] = 'GROUP BY sort_name';
-    $sqlParts['order_by'] = 'ORDER BY sort_name asc';
-    $query = "{$sqlParts['select']} {$sqlParts['from']} {$sqlParts['where']} {$sqlParts['having']} {$sqlParts['group_by']} {$sqlParts['order_by']}";
+    CRM_Core_DAO::disableFullGroupByMode();
 
+    $sqlParts = $this->getSearchSQLParts(NULL, NULL, NULL, FALSE, FALSE, TRUE);
+    $query = "SELECT DISTINCT LEFT(contact_a.sort_name, 1) as sort_name
+     {$sqlParts['from']}
+     {$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.
@@ -6636,6 +6640,10 @@ AND   displayRelType.is_active = 1
     $additionalFromClause = NULL, $skipOrderAndLimit = FALSE) {
 
     $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';
+    }
 

The important parts are

  1. ensuring CRM_Core_DAO::disableFullGroupByMode(); is called from alphabet query
  2. supporting but deprecating the $sortByChar function further up the chain - I'm worried about custom searches outside of core

Also note we can probably simplify the prepareOrderBy & general web further is we agree this can be merged #13748 - that $additionalFromClause is a pain from a code POV but I think it actually BREAKS stuff from a usability pov

@mattwire
Copy link
Contributor Author

mattwire commented Mar 6, 2019

@eileenmcnaughton With the GROUP BY change we don't need to call disableFullGroupByMode() at all as the query is now compatible with full group by

@eileenmcnaughton
Copy link
Contributor

@mattwire ah yeah - you are right re FGB - so we should remove the reenable as well I think. I do think the other part I suggested - the deprecation rather than remove from parent could be needed for custom searches

@mattwire mattwire force-pushed the alphabetquery_groupby branch from 3d5198a to dbcf34c Compare March 12, 2019 10:11
@mattwire
Copy link
Contributor Author

@eileenmcnaughton I think I've addressed your comments. If tests pass this is ready for review again

@mattwire mattwire force-pushed the alphabetquery_groupby branch from dbcf34c to ae644bc Compare March 16, 2019 10:58
@mattwire
Copy link
Contributor Author

@eileenmcnaughton Ok I've addressed your comments - I think this is now ok to merge?

@eileenmcnaughton
Copy link
Contributor

@mattwire those deprecated lines are hit... :-(

@mattwire
Copy link
Contributor Author

It's catch 22. Leave the $sortByChar ifs' in and you do some of the work twice because if you don't pass in $sortByChar=TRUE you trigger the query functions to do a load of other work. If you take out $sortByChar then you risk breaking custom searches that may have been implemented by third-parties IF they call getSearchSQL with sortByChar=TRUE. Which was only called once by core via PagerAToZ.php in getDynamicCharacters().

Could we just put a deprecated warning into getSearchSQL() and return a NULL query? The worst thing I think it's going to break is that you'll only have the standard letters across the top of the results instead of all "extra" letters. Which is not likely to affect English and most European names which start with a standard letter even if they then use accents etc. later on. And provide an upgrader message to the effect that you should check your custom searches if relying on this feature.

* @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

@mattwire mattwire force-pushed the alphabetquery_groupby branch from fa38b06 to caefed7 Compare March 20, 2019 23:02
@eileenmcnaughton
Copy link
Contributor

unrelated fail

@eileenmcnaughton eileenmcnaughton merged commit d721765 into civicrm:master Mar 21, 2019
@mattwire mattwire deleted the alphabetquery_groupby branch March 22, 2020 22:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants