Skip to content

Commit

Permalink
Merge pull request #9802 from eileenmcnaughton/lower
Browse files Browse the repository at this point in the history
 CRM-19980, CRM-19811 - Fix slow queries due to LOWER on contact name…
  • Loading branch information
colemanw authored Mar 16, 2017
2 parents 6d4aa46 + 1809f3c commit 43e3832
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
3 changes: 1 addition & 2 deletions CRM/Contact/BAO/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -2273,8 +2273,7 @@ public function restWhere(&$values) {
}
else {
if ($tableName == 'civicrm_contact') {
// LOWER roughly translates to 'hurt my database without deriving any benefit' See CRM-19811.
$fieldName = "LOWER(contact_a.{$fieldName})";
$fieldName = "contact_a.{$fieldName}";
}
else {
if ($op != 'IN' && !is_numeric($value) && !is_array($value)) {
Expand Down
32 changes: 24 additions & 8 deletions tests/phpunit/CRM/Contact/BAO/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,14 +310,30 @@ public function testNumericPostal() {
*/
public function testCaseInsensitive() {
$orgID = $this->organizationCreate(array('organization_name' => 'BOb'));
$this->callAPISuccess('Contact', 'create', array('display_name' => 'Minnie Mouse', 'employer_id' => $orgID, 'contact_type' => 'Individual'));
$searchParams = array(array('current_employer', '=', 'bob', 0, 1));
$query = new CRM_Contact_BAO_Query($searchParams);
$result = $query->apiQuery($searchParams);
$this->assertEquals(1, count($result[0]));
$contact = reset($result[0]);
$this->assertEquals('Minnie Mouse', $contact['display_name']);
$this->assertEquals('BOb', $contact['current_employer']);
$params = array(
'display_name' => 'Minnie Mouse',
'first_name' => 'Minnie',
'last_name' => 'Mouse',
'employer_id' => $orgID,
'contact_type' => 'Individual',
'nick_name' => 'Mins',
);
$this->callAPISuccess('Contact', 'create', $params);
unset($params['contact_type']);
foreach ($params as $key => $value) {
if ($key == 'employer_id') {
$searchParams = array(array('current_employer', '=', 'bob', 0, 1));
}
else {
$searchParams = array(array($key, '=', strtolower($value), 0, 1));
}
$query = new CRM_Contact_BAO_Query($searchParams);
$result = $query->apiQuery($searchParams);
$this->assertEquals(1, count($result[0]), 'search for ' . $key);
$contact = reset($result[0]);
$this->assertEquals('Minnie Mouse', $contact['display_name']);
$this->assertEquals('BOb', $contact['current_employer']);
}
}

/**
Expand Down

0 comments on commit 43e3832

Please sign in to comment.