Commit cff48a22 authored by catch's avatar catch

Issue #3001216 by longwave, mondrake, voleger, tstoeckler, Berdir, andypost:...

Issue #3001216 by longwave, mondrake, voleger, tstoeckler, Berdir, andypost: Use the database.replica service where appropriate
parent 1aee6103
......@@ -354,6 +354,10 @@ services:
class: Drupal\Core\Database\Connection
factory: Drupal\Core\Database\Database::getConnection
arguments: [default]
database.replica:
class: Drupal\Core\Database\Connection
factory: Drupal\Core\Database\Database::getConnection
arguments: [replica]
datetime.time:
class: Drupal\Component\Datetime\Time
arguments: ['@request_stack']
......@@ -791,10 +795,6 @@ services:
arguments: ['@language.default']
tags:
- { name: service_collector, tag: string_translator, call: addTranslator }
database.replica:
class: Drupal\Core\Database\Connection
factory: Drupal\Core\Database\Database::getConnection
arguments: [replica]
typed_data_manager:
class: Drupal\Core\TypedData\TypedDataManager
arguments: ['@container.namespaces', '@cache.discovery', '@module_handler', '@class_resolver']
......
......@@ -11,7 +11,7 @@ services:
comment.statistics:
class: Drupal\comment\CommentStatistics
arguments: ['@database', '@current_user', '@entity.manager', '@state']
arguments: ['@database', '@current_user', '@entity.manager', '@state', '@database.replica']
tags:
- { name: backend_overridable }
......
......@@ -20,6 +20,13 @@ class CommentStatistics implements CommentStatisticsInterface {
*/
protected $database;
/**
* The replica database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $databaseReplica;
/**
* The current logged in user.
*
......@@ -52,9 +59,12 @@ class CommentStatistics implements CommentStatisticsInterface {
* The entity manager service.
* @param \Drupal\Core\State\StateInterface $state
* The state service.
* @param \Drupal\Core\Database\Connection|null $database_replica
* (Optional) the replica database connection.
*/
public function __construct(Connection $database, AccountInterface $current_user, EntityManagerInterface $entity_manager, StateInterface $state) {
public function __construct(Connection $database, AccountInterface $current_user, EntityManagerInterface $entity_manager, StateInterface $state, Connection $database_replica = NULL) {
$this->database = $database;
$this->databaseReplica = $database_replica ?: $database;
$this->currentUser = $current_user;
$this->entityManager = $entity_manager;
$this->state = $state;
......@@ -64,8 +74,8 @@ public function __construct(Connection $database, AccountInterface $current_user
* {@inheritdoc}
*/
public function read($entities, $entity_type, $accurate = TRUE) {
$options = $accurate ? [] : ['target' => 'replica'];
$stats = $this->database->select('comment_entity_statistics', 'ces', $options)
$connection = $accurate ? $this->database : $this->databaseReplica;
$stats = $connection->select('comment_entity_statistics', 'ces')
->fields('ces')
->condition('ces.entity_id', array_keys($entities), 'IN')
->condition('ces.entity_type', $entity_type)
......
......@@ -82,7 +82,7 @@ protected function setUp() {
->method('select')
->will($this->returnValue($this->select));
$this->commentStatistics = new CommentStatistics($this->database, $this->getMock('Drupal\Core\Session\AccountInterface'), $this->getMock('Drupal\Core\Entity\EntityManagerInterface'), $this->getMock('Drupal\Core\State\StateInterface'));
$this->commentStatistics = new CommentStatistics($this->database, $this->getMock('Drupal\Core\Session\AccountInterface'), $this->getMock('Drupal\Core\Entity\EntityManagerInterface'), $this->getMock('Drupal\Core\State\StateInterface'), $this->database);
}
/**
......
......@@ -36,12 +36,19 @@
class NodeSearch extends ConfigurableSearchPluginBase implements AccessibleInterface, SearchIndexingInterface {
/**
* A database connection object.
* The current database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;
/**
* The replica database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $databaseReplica;
/**
* An entity manager object.
*
......@@ -138,7 +145,8 @@ public static function create(ContainerInterface $container, array $configuratio
$container->get('language_manager'),
$container->get('renderer'),
$container->get('messenger'),
$container->get('current_user')
$container->get('current_user'),
$container->get('database.replica')
);
}
......@@ -152,7 +160,7 @@ public static function create(ContainerInterface $container, array $configuratio
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Database\Connection $database
* A database connection object.
* The current database connection.
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* An entity manager object.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
......@@ -167,9 +175,12 @@ public static function create(ContainerInterface $container, array $configuratio
* The messenger.
* @param \Drupal\Core\Session\AccountInterface $account
* The $account object to use for checking for access to advanced search.
* @param \Drupal\Core\Database\Connection|null $database_replica
* (Optional) the replica database connection.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, Config $search_settings, LanguageManagerInterface $language_manager, RendererInterface $renderer, MessengerInterface $messenger, AccountInterface $account = NULL) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, Connection $database, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, Config $search_settings, LanguageManagerInterface $language_manager, RendererInterface $renderer, MessengerInterface $messenger, AccountInterface $account = NULL, Connection $database_replica = NULL) {
$this->database = $database;
$this->databaseReplica = $database_replica ?: $database;
$this->entityManager = $entity_manager;
$this->moduleHandler = $module_handler;
$this->searchSettings = $search_settings;
......@@ -237,8 +248,8 @@ protected function findResults() {
$keys = $this->keywords;
// Build matching conditions.
$query = $this->database
->select('search_index', 'i', ['target' => 'replica'])
$query = $this->databaseReplica
->select('search_index', 'i')
->extend('Drupal\search\SearchQuery')
->extend('Drupal\Core\Database\Query\PagerSelectExtender');
$query->join('node_field_data', 'n', 'n.nid = i.sid AND n.langcode = i.langcode');
......@@ -439,7 +450,7 @@ public function updateIndex() {
// per cron run.
$limit = (int) $this->searchSettings->get('index.cron_limit');
$query = Database::getConnection('replica')->select('node', 'n');
$query = $this->databaseReplica->select('node', 'n');
$query->addField('n', 'nid');
$query->leftJoin('search_dataset', 'sd', 'sd.sid = n.nid AND sd.type = :type', [':type' => $this->getPluginId()]);
$query->addExpression('CASE MAX(sd.reindex) WHEN NULL THEN 0 ELSE 1 END', 'ex');
......
......@@ -2,7 +2,6 @@
namespace Drupal\search\Plugin\views\argument;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\Query\Condition;
use Drupal\search\ViewsSearchQuery;
use Drupal\views\Plugin\views\argument\ArgumentPluginBase;
......@@ -50,7 +49,7 @@ public function init(ViewExecutable $view, DisplayPluginBase $display, array &$o
*/
protected function queryParseSearchExpression($input) {
if (!isset($this->searchQuery)) {
$this->searchQuery = Database::getConnection('replica')->select('search_index', 'i')->extend(ViewsSearchQuery::class);
$this->searchQuery = \Drupal::service('database.replica')->select('search_index', 'i')->extend(ViewsSearchQuery::class);
$this->searchQuery->searchExpression($input, $this->searchType);
$this->searchQuery->publicParseSearchExpression();
}
......
......@@ -2,7 +2,6 @@
namespace Drupal\search\Plugin\views\filter;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Form\FormStateInterface;
use Drupal\search\ViewsSearchQuery;
......@@ -122,7 +121,7 @@ public function validateExposed(&$form, FormStateInterface $form_state) {
protected function queryParseSearchExpression($input) {
if (!isset($this->searchQuery)) {
$this->parsed = TRUE;
$this->searchQuery = Database::getConnection('replica')->select('search_index', 'i')->extend(ViewsSearchQuery::class);
$this->searchQuery = \Drupal::service('database.replica')->select('search_index', 'i')->extend(ViewsSearchQuery::class);
$this->searchQuery->searchExpression($input, $this->searchType);
$this->searchQuery->publicParseSearchExpression();
}
......
......@@ -2,7 +2,6 @@
namespace Drupal\search;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Database\Query\SelectExtender;
use Drupal\Core\Database\Query\SelectInterface;
......@@ -622,7 +621,7 @@ public function countQuery() {
$expressions = [];
// Add sid as the only field and count them as a subquery.
$count = Database::getConnection('replica')->select($inner->fields('i', ['sid']), NULL);
$count = $this->connection->select($inner->fields('i', ['sid']), NULL);
// Add the COUNT() expression.
$count->addExpression('COUNT(*)');
......
......@@ -6,7 +6,6 @@
*/
use Drupal\Core\Cache\Cache;
use Drupal\Core\Database\Database;
use Drupal\node\Entity\Node;
/**
......@@ -23,13 +22,13 @@
*/
function tracker_page($account = NULL) {
if ($account) {
$query = Database::getConnection()->select('tracker_user', 't')
$query = \Drupal::database()->select('tracker_user', 't')
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
->addMetaData('base_table', 'tracker_user')
->condition('t.uid', $account->id());
}
else {
$query = Database::getConnection('replica')->select('tracker_node', 't')
$query = \Drupal::service('database.replica')->select('tracker_node', 't')
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
->addMetaData('base_table', 'tracker_node');
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment