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