From 32a2c0243df59b344c56aeec334ae4f4199b2a4a Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 29 Jun 2018 15:05:31 -0700 Subject: [PATCH] (dev/core#217) CRM_Contact_Selector::fillupPrevNextCache - Use PrevNextCache::fillWithSql() --- CRM/Contact/Selector.php | 12 +++--------- CRM/Core/PrevNextCache/Interface.php | 10 ++++++++++ CRM/Core/PrevNextCache/Sql.php | 21 +++++++++++++++++++++ 3 files changed, 34 insertions(+), 9 deletions(-) diff --git a/CRM/Contact/Selector.php b/CRM/Contact/Selector.php index c19789fa53..f0ee4584a4 100644 --- a/CRM/Contact/Selector.php +++ b/CRM/Contact/Selector.php @@ -1039,17 +1039,11 @@ class CRM_Contact_Selector extends CRM_Core_Selector_Base implements CRM_Core_Se // the other alternative of running the FULL query will just be incredibly inefficient // and slow things down way too much on large data sets / complex queries - $insertSQL = " -INSERT INTO civicrm_prevnext_cache ( entity_table, entity_id1, entity_id2, cacheKey, data ) -SELECT DISTINCT 'civicrm_contact', contact_a.id, contact_a.id, '$cacheKey', contact_a.sort_name -"; + $selectSQL = "SELECT DISTINCT 'civicrm_contact', contact_a.id, contact_a.id, '$cacheKey', contact_a.sort_name"; - $sql = str_replace(array("SELECT contact_a.id as contact_id", "SELECT contact_a.id as id"), $insertSQL, $sql); + $sql = str_replace(array("SELECT contact_a.id as contact_id", "SELECT contact_a.id as id"), $selectSQL, $sql); try { - $result = CRM_Core_DAO::executeQuery($sql, [], FALSE, NULL, FALSE, TRUE, TRUE); - if (is_a($result, 'DB_Error')) { - throw new CRM_Core_Exception($result->message); - } + Civi::service('prevnext')->fillWithSql($cacheKey, $sql); } catch (CRM_Core_Exception $e) { if ($coreSearch) { diff --git a/CRM/Core/PrevNextCache/Interface.php b/CRM/Core/PrevNextCache/Interface.php index 3c8867838a..06e725bb72 100644 --- a/CRM/Core/PrevNextCache/Interface.php +++ b/CRM/Core/PrevNextCache/Interface.php @@ -33,4 +33,14 @@ */ interface CRM_Core_PrevNextCache_Interface { + /** + * Store the results of a SQL query in the cache. + * + * @param string $sql + * A SQL query. The query *MUST* be a SELECT statement which yields + * the following columns (in order): entity_table, entity_id1, entity_id2, cacheKey, data + * @return bool + */ + public function fillWithSql($cacheKey, $sql); + } diff --git a/CRM/Core/PrevNextCache/Sql.php b/CRM/Core/PrevNextCache/Sql.php index b51704e85a..980af48f82 100644 --- a/CRM/Core/PrevNextCache/Sql.php +++ b/CRM/Core/PrevNextCache/Sql.php @@ -31,4 +31,25 @@ * Store the previous/next cache in a special-purpose SQL table. */ class CRM_Core_PrevNextCache_Sql implements CRM_Core_PrevNextCache_Interface { + + /** + * Store the results of a SQL query in the cache. + * + * @param string $sql + * A SQL query. The query *MUST* be a SELECT statement which yields + * the following columns (in order): entity_table, entity_id1, entity_id2, cacheKey, data + * @return bool + * @throws CRM_Core_Exception + */ + public function fillWithSql($cacheKey, $sql) { + $insertSQL = " +INSERT INTO civicrm_prevnext_cache ( entity_table, entity_id1, entity_id2, cacheKey, data ) +"; + $result = CRM_Core_DAO::executeQuery($insertSQL . $sql, [], FALSE, NULL, FALSE, TRUE, TRUE); + if (is_a($result, 'DB_Error')) { + throw new CRM_Core_Exception($result->message); + } + return TRUE; + } + } -- 2.25.1