Commit e57b4ac9 authored by xjm's avatar xjm

Issue #3139353 by daffie, andypost, mrinalini9, Lendude, xjm: Add public...

Issue #3139353 by daffie, andypost, mrinalini9, Lendude, xjm: Add public method Drupal\views\Plugin\views\query\Sql::getConnection()
parent a45c4857
......@@ -1248,6 +1248,24 @@ protected function compileFields($query) {
}
}
/**
* Gets the database connection to use for the view.
*
* The returned database connection does not have to be the default database
* connection. It can also be to another database connection when the view is
* to an external database or a replica database.
*
* @return \Drupal\Core\Database\Connection
* The database connection to be used for the query.
*/
public function getConnection() {
// Set the replica target if the replica option is set for the view.
$target = empty($this->options['replica']) ? 'default' : 'replica';
// Use an external database when the view configured to.
$key = $this->view->base_database ?? 'default';
return Database::getConnection($target, $key);
}
/**
* Generate a query and a countquery from all of the information supplied
* to the object.
......@@ -1282,22 +1300,9 @@ public function query($get_count = FALSE) {
$this->getCountOptimized = TRUE;
}
$options = [];
$target = 'default';
$key = 'default';
// Detect an external database and set the
if (isset($this->view->base_database)) {
$key = $this->view->base_database;
}
// Set the replica target if the replica option is set
if (!empty($this->options['replica'])) {
$target = 'replica';
}
// Go ahead and build the query.
$query = Database::getConnection($target, $key)
->select($this->view->storage->get('base_table'), $this->view->storage->get('base_table'), $options)
$query = $this->getConnection()
->select($this->view->storage->get('base_table'), $this->view->storage->get('base_table'))
->addTag('views')
->addTag('views_' . $this->view->storage->id());
......
......@@ -2,6 +2,7 @@
namespace Drupal\Tests\views\Kernel\Plugin;
use Drupal\Core\Database\Database;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\views\Views;
......@@ -79,4 +80,51 @@ public function testExecuteMetadata() {
}
}
/**
* Tests the method \Drupal\views\Plugin\views\query\Sql::getConnection().
*
* @covers \Drupal\views\Plugin\views\query\Sql::getConnection
*
* This needs to be a kernel test because the tested method uses the method
* \Drupal\Core\Database\Database::getConnection() which is a 'final' method
* and therefore cannot be mocked.
*/
public function testGetConnection() {
$view = Views::getView('test_view');
$view->setDisplay();
// Add 3 database connections for the different options that the method
// getConnection() supports.
$connection_info = Database::getConnectionInfo('default');
Database::addConnectionInfo('default', 'replica', $connection_info['default']);
Database::addConnectionInfo('corefake', 'default', $connection_info['default']);
Database::addConnectionInfo('corefake', 'replica', $connection_info['default']);
// Test the database connection with no special options set.
$this->assertSame('default', $view->getQuery()->getConnection()->getKey());
$this->assertSame('default', $view->getQuery()->getConnection()->getTarget());
// Test the database connection with the option 'replica' set to TRUE;
$view->getQuery()->options['replica'] = TRUE;
$this->assertSame('default', $view->getQuery()->getConnection()->getKey());
$this->assertSame('replica', $view->getQuery()->getConnection()->getTarget());
// Test the database connection with the view 'base_database' set.
$view->getQuery()->options['replica'] = FALSE;
$view->base_database = 'corefake';
$this->assertSame('corefake', $view->getQuery()->getConnection()->getKey());
$this->assertSame('default', $view->getQuery()->getConnection()->getTarget());
// Test the database connection with the view 'base_database' set and the
// option 'replica' set to TRUE.
$view->getQuery()->options['replica'] = TRUE;
$this->assertSame('corefake', $view->getQuery()->getConnection()->getKey());
$this->assertSame('replica', $view->getQuery()->getConnection()->getTarget());
// Clean up the created database connections.
Database::closeConnection('replica', 'default');
Database::closeConnection('default', 'corefake');
Database::closeConnection('replica', 'corefake');
}
}
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