From f1e68af10610a6e50481e142c179d2c311ab17b9 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/Memory.php | 4 ++++ CRM/Core/PrevNextCache/Sql.php | 21 +++++++++++++++++++++ 4 files changed, 48 insertions(+), 9 deletions(-) diff --git a/CRM/Contact/Selector.php b/CRM/Contact/Selector.php index 006ca1cdf04..ed20119051c 100644 --- a/CRM/Contact/Selector.php +++ b/CRM/Contact/Selector.php @@ -1085,18 +1085,17 @@ public function rebuildPreNextCache($start, $end, $sort, $cacheKey) { $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 e26d0e51d59..ff74d60f374 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,6 +44,20 @@ 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); + /** * Fetch a list of contacts from the prev/next cache for displaying a search results page * diff --git a/CRM/Core/PrevNextCache/Memory.php b/CRM/Core/PrevNextCache/Memory.php index ad4cd7094fe..42d01e85444 100644 --- a/CRM/Core/PrevNextCache/Memory.php +++ b/CRM/Core/PrevNextCache/Memory.php @@ -49,6 +49,10 @@ public function fillWithSql($cacheKey, $sql) { throw new \RuntimeException("Not implemented: " . __CLASS__ . '::' . __FUNCTION__); } + public function fillWithArray($cacheKey, $rows) { + throw new \RuntimeException("Not implemented: " . __CLASS__ . '::' . __FUNCTION__); + } + public function fetch($cacheKey, $offset, $rowCount, $includeContactIds, $queryBao) { throw new \RuntimeException("Not implemented: " . __CLASS__ . '::' . __FUNCTION__); } diff --git a/CRM/Core/PrevNextCache/Sql.php b/CRM/Core/PrevNextCache/Sql.php index a371af4984c..3b24741dd40 100644 --- a/CRM/Core/PrevNextCache/Sql.php +++ b/CRM/Core/PrevNextCache/Sql.php @@ -52,6 +52,27 @@ public function fillWithSql($cacheKey, $sql) { 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()); + } + /** * Fetch a list of contacts from the prev/next cache for displaying a search results page *