From 83068a645aafca03d6f04e36b737ddee208459d7 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 29 Jun 2018 17:34:07 -0700 Subject: [PATCH] (dev/core#217) CRM_Contact_Selector::rebuildPreNextCache - Use PrevNextCache::fillWithArray() --- CRM/Contact/Selector.php | 17 ++++++++--------- CRM/Core/PrevNextCache/Interface.php | 15 +++++++++++++++ CRM/Core/PrevNextCache/Sql.php | 22 ++++++++++++++++++++++ 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/CRM/Contact/Selector.php b/CRM/Contact/Selector.php index f0ee4584a4..a3c270231c 100644 --- a/CRM/Contact/Selector.php +++ b/CRM/Contact/Selector.php @@ -1083,18 +1083,17 @@ class CRM_Contact_Selector extends CRM_Core_Selector_Base implements CRM_Core_Se $dao = CRM_Core_DAO::executeQuery($sql); // build insert query, note that currently we build cache for 500 (self::CACHE_SIZE) contact records at a time, hence below approach - $insertValues = array(); + $rows = []; while ($dao->fetch()) { - $insertValues[] = "('civicrm_contact', {$dao->contact_id}, {$dao->contact_id}, '{$cacheKey}', '" . CRM_Core_DAO::escapeString($dao->sort_name) . "')"; + $rows[] = [ + 'entity_table' => 'civicrm_contact', + 'entity_id1' => $dao->contact_id, + 'entity_id2' => $dao->contact_id, + 'data' => $dao->sort_name, + ]; } - //update pre/next cache using single insert query - if (!empty($insertValues)) { - $sql = 'INSERT INTO civicrm_prevnext_cache ( entity_table, entity_id1, entity_id2, cacheKey, data ) VALUES -' . implode(',', $insertValues); - - $result = CRM_Core_DAO::executeQuery($sql); - } + Civi::service('prevnext')->fillWithArray($cacheKey, $rows); } /** diff --git a/CRM/Core/PrevNextCache/Interface.php b/CRM/Core/PrevNextCache/Interface.php index 06e725bb72..5220ca205c 100644 --- a/CRM/Core/PrevNextCache/Interface.php +++ b/CRM/Core/PrevNextCache/Interface.php @@ -36,6 +36,7 @@ interface CRM_Core_PrevNextCache_Interface { /** * Store the results of a SQL query in the cache. * + * @param string $cacheKey * @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 @@ -43,4 +44,18 @@ interface CRM_Core_PrevNextCache_Interface { */ public function fillWithSql($cacheKey, $sql); + /** + * Store the contents of an array in the cache. + * + * @param string $cacheKey + * @param array $rows + * A list of cache records. Each record should have keys: + * - entity_table + * - entity_id1 + * - entity_id2 + * - data + * @return bool + */ + public function fillWithArray($cacheKey, $rows); + } diff --git a/CRM/Core/PrevNextCache/Sql.php b/CRM/Core/PrevNextCache/Sql.php index 980af48f82..7882e9ad83 100644 --- a/CRM/Core/PrevNextCache/Sql.php +++ b/CRM/Core/PrevNextCache/Sql.php @@ -52,4 +52,26 @@ INSERT INTO civicrm_prevnext_cache ( entity_table, entity_id1, entity_id2, cache return TRUE; } + public function fillWithArray($cacheKey, $rows) { + if (empty($rows)) { + return; + } + + $insert = CRM_Utils_SQL_Insert::into('civicrm_prevnext_cache') + ->columns([ + 'entity_table', + 'entity_id1', + 'entity_id2', + 'cacheKey', + 'data' + ]); + + foreach ($rows as &$row) { + $insert->row($row + ['cacheKey' => $cacheKey]); + } + + CRM_Core_DAO::executeQuery($insert->toSQL()); + return TRUE; + } + } -- 2.25.1