Unverified Commit 258fd5ed authored by alexpott's avatar alexpott
Browse files

Issue #3124354 by daffie, abhisekmazumdar, alexpott, johnwebdev, catch,...

Issue #3124354 by daffie, abhisekmazumdar, alexpott, johnwebdev, catch, Beakerboy: Remove unnecessary classes from the database drivers

(cherry picked from commit a11ed7af)
parent acfa2e5d
......@@ -4,6 +4,13 @@
use Drupal\Component\Assertion\Inspector;
use Drupal\Core\Database\Query\Condition;
use Drupal\Core\Database\Query\Delete;
use Drupal\Core\Database\Query\Insert;
use Drupal\Core\Database\Query\Merge;
use Drupal\Core\Database\Query\Select;
use Drupal\Core\Database\Query\Truncate;
use Drupal\Core\Database\Query\Update;
use Drupal\Core\Database\Query\Upsert;
/**
* Base Database API class.
......@@ -873,11 +880,44 @@ protected function expandArguments(&$query, &$args) {
public function getDriverClass($class) {
if (empty($this->driverClasses[$class])) {
$driver_class = $this->connectionOptions['namespace'] . '\\' . $class;
$this->driverClasses[$class] = class_exists($driver_class) ? $driver_class : $class;
if ($this->driverClasses[$class] === 'Condition') {
// @todo Deprecate the fallback for contrib and custom drivers in 9.1.x
// in https://www.drupal.org/project/drupal/issues/3120036.
$this->driverClasses[$class] = Condition::class;
if (class_exists($driver_class)) {
$this->driverClasses[$class] = $driver_class;
}
else {
switch ($class) {
case 'Condition':
$this->driverClasses[$class] = Condition::class;
break;
case 'Delete':
$this->driverClasses[$class] = Delete::class;
break;
case 'Insert':
$this->driverClasses[$class] = Insert::class;
break;
case 'Merge':
$this->driverClasses[$class] = Merge::class;
break;
case 'Schema':
$this->driverClasses[$class] = Schema::class;
break;
case 'Select':
$this->driverClasses[$class] = Select::class;
break;
case 'Transaction':
$this->driverClasses[$class] = Transaction::class;
break;
case 'Truncate':
$this->driverClasses[$class] = Truncate::class;
break;
case 'Update':
$this->driverClasses[$class] = Update::class;
break;
case 'Upsert':
$this->driverClasses[$class] = Upsert::class;
break;
default:
$this->driverClasses[$class] = $class;
}
}
}
return $this->driverClasses[$class];
......
<?php
namespace Drupal\Core\Database\Driver\mysql;
use Drupal\Core\Database\Query\Delete as QueryDelete;
/**
* MySQL implementation of \Drupal\Core\Database\Query\Delete.
*/
class Delete extends QueryDelete {}
<?php
namespace Drupal\Core\Database\Driver\mysql;
use Drupal\Core\Database\Query\Merge as QueryMerge;
/**
* MySQL implementation of \Drupal\Core\Database\Query\Merge.
*/
class Merge extends QueryMerge {}
<?php
namespace Drupal\Core\Database\Driver\mysql;
use Drupal\Core\Database\Query\Select as QuerySelect;
/**
* MySQL implementation of \Drupal\Core\Database\Query\Select.
*/
class Select extends QuerySelect {}
<?php
namespace Drupal\Core\Database\Driver\mysql;
use Drupal\Core\Database\Transaction as DatabaseTransaction;
/**
* MySQL implementation of \Drupal\Core\Database\Transaction.
*/
class Transaction extends DatabaseTransaction {}
<?php
namespace Drupal\Core\Database\Driver\mysql;
use Drupal\Core\Database\Query\Truncate as QueryTruncate;
/**
* MySQL implementation of \Drupal\Core\Database\Query\Truncate.
*/
class Truncate extends QueryTruncate {}
<?php
namespace Drupal\Core\Database\Driver\mysql;
use Drupal\Core\Database\Query\Update as QueryUpdate;
/**
* MySQL implementation of \Drupal\Core\Database\Query\Update.
*/
class Update extends QueryUpdate {}
<?php
namespace Drupal\Core\Database\Driver\pgsql;
use Drupal\Core\Database\Query\Merge as QueryMerge;
/**
* PostgreSQL implementation of \Drupal\Core\Database\Query\Merge.
*/
class Merge extends QueryMerge {}
<?php
namespace Drupal\Core\Database\Driver\pgsql;
use Drupal\Core\Database\Transaction as DatabaseTransaction;
/**
* PostgreSQL implementation of \Drupal\Core\Database\Transaction.
*/
class Transaction extends DatabaseTransaction {}
<?php
namespace Drupal\Core\Database\Driver\sqlite;
use Drupal\Core\Database\Query\Delete as QueryDelete;
/**
* SQLite implementation of \Drupal\Core\Database\Query\Delete.
*/
class Delete extends QueryDelete {}
<?php
namespace Drupal\Core\Database\Driver\sqlite;
use Drupal\Core\Database\Query\Merge as QueryMerge;
/**
* SQLite implementation of \Drupal\Core\Database\Query\Merge.
*/
class Merge extends QueryMerge {}
<?php
namespace Drupal\Core\Database\Driver\sqlite;
use Drupal\Core\Database\Transaction as DatabaseTransaction;
/**
* SQLite implementation of \Drupal\Core\Database\Transaction.
*/
class Transaction extends DatabaseTransaction {}
<?php
namespace Drupal\Core\Database\Driver\sqlite;
use Drupal\Core\Database\Query\Update as QueryUpdate;
/**
* SQLite implementation of \Drupal\Core\Database\Query\Update.
*/
class Update extends QueryUpdate {}
......@@ -2,8 +2,6 @@
namespace Drupal\database_statement_monitoring_test;
use Drupal\Core\Database\Query\Condition;
/**
* Trait for Connection classes that can store logged statements.
*/
......@@ -46,18 +44,16 @@ public function resetLoggedStatements() {
* {@inheritdoc}
*/
public function getDriverClass($class) {
// Override because the database driver copies in the
// database_statement_monitoring_test module don't contain all the necessary
// classes.
$namespace = (new \ReflectionClass(get_parent_class($this)))->getNamespaceName();
$driver_class = $namespace . '\\' . $class;
if (class_exists($driver_class)) {
return $driver_class;
}
elseif ($class == 'Condition') {
return Condition::class;
static $fixed_namespace;
if (!$fixed_namespace) {
// Override because we've altered the namespace in
// \Drupal\KernelTests\Core\Cache\EndOfTransactionQueriesTest::getDatabaseConnectionInfo()
// to use the logging Connection classes. Set to a proper database driver
// namespace.
$this->connectionOptions['namespace'] = (new \ReflectionClass(get_parent_class($this)))->getNamespaceName();
$fixed_namespace = TRUE;
}
return $class;
return parent::getDriverClass($class);
}
/**
......
<?php
namespace Drupal\driver_test\Driver\Database\DrivertestMysql;
use Drupal\Core\Database\Driver\mysql\Delete as CoreDelete;
/**
* MySQL test implementation of \Drupal\Core\Database\Query\Delete.
*/
class Delete extends CoreDelete {}
<?php
namespace Drupal\driver_test\Driver\Database\DrivertestMysql;
use Drupal\Core\Database\Driver\mysql\Merge as CoreMerge;
/**
* MySQL test implementation of \Drupal\Core\Database\Query\Merge.
*/
class Merge extends CoreMerge {}
<?php
namespace Drupal\driver_test\Driver\Database\DrivertestMysql;
use Drupal\Core\Database\Driver\mysql\Select as CoreSelect;
/**
* MySQL test implementation of \Drupal\Core\Database\Query\Select.
*/
class Select extends CoreSelect {}
<?php
namespace Drupal\driver_test\Driver\Database\DrivertestMysql;
use Drupal\Core\Database\Driver\mysql\Transaction as CoreTransaction;
/**
* MySQL test implementation of \Drupal\Core\Database\Transaction.
*/
class Transaction extends CoreTransaction {}
<?php
namespace Drupal\driver_test\Driver\Database\DrivertestMysql;
use Drupal\Core\Database\Driver\mysql\Truncate as CoreTruncate;
/**
* MySQL test implementation of \Drupal\Core\Database\Query\Truncate.
*/
class Truncate extends CoreTruncate {}
<?php
namespace Drupal\driver_test\Driver\Database\DrivertestMysql;
use Drupal\Core\Database\Driver\mysql\Update as CoreUpdate;
/**
* MySQL test implementation of \Drupal\Core\Database\Query\Update.
*/
class Update extends CoreUpdate {}
<?php
namespace Drupal\driver_test\Driver\Database\DrivertestPgsql;
use Drupal\Core\Database\Driver\pgsql\Merge as CoreMerge;
/**
* PostgreSQL implementation of \Drupal\Core\Database\Query\Merge.
*/
class Merge extends CoreMerge {}
<?php
namespace Drupal\driver_test\Driver\Database\DrivertestPgsql;
use Drupal\Core\Database\Driver\pgsql\Transaction as CoreTransaction;
/**
* PostgreSQL implementation of \Drupal\Core\Database\Transaction.
*/
class Transaction extends CoreTransaction {}
......@@ -99,7 +99,7 @@ public function testConnectionOptions() {
Database::addConnectionInfo('default', 'replica', $connection_info['default']);
$db2 = Database::getConnection('replica', 'default');
// Getting a driver class ensures the namespace option is set.
$this->assertEquals($db->getDriverClass('select'), $db2->getDriverClass('select'));
$this->assertEquals($db->getDriverClass('Select'), $db2->getDriverClass('Select'));
$connectionOptions2 = $db2->getConnectionOptions();
// Get a fresh copy of the default connection options.
......
......@@ -2,6 +2,7 @@
namespace Drupal\Tests\Core\Database;
use Composer\Autoload\ClassLoader;
use Drupal\Tests\Core\Database\Stub\StubConnection;
use Drupal\Tests\Core\Database\Stub\StubPDO;
use Drupal\Tests\UnitTestCase;
......@@ -118,7 +119,6 @@ public function testPrefixTables($expected, $prefix_info, $query, array $quote_i
* @return array
* Array of arrays with the following elements:
* - Expected namespaced class name.
* - Driver.
* - Namespace.
* - Class name without namespace.
*/
......@@ -134,23 +134,155 @@ public function providerGetDriverClass() {
NULL,
'Select',
],
// Tests with the corefake database driver. This driver has no custom
// driver classes.
[
'Drupal\\Tests\\Core\\Database\\Stub\\Driver\\Schema',
'Drupal\\Tests\\Core\\Database\\Stub\\Driver',
'Drupal\Core\Database\Query\Condition',
'Drupal\corefake\Driver\Database\corefake',
'Condition',
],
[
'Drupal\Core\Database\Query\Delete',
'Drupal\corefake\Driver\Database\corefake',
'Delete',
],
[
'Drupal\Core\Database\Query\Insert',
'Drupal\corefake\Driver\Database\corefake',
'Insert',
],
[
'Drupal\Core\Database\Query\Merge',
'Drupal\corefake\Driver\Database\corefake',
'Merge',
],
[
'Drupal\Core\Database\Schema',
'Drupal\corefake\Driver\Database\corefake',
'Schema',
],
[
'Drupal\Core\Database\Query\Select',
'Drupal\corefake\Driver\Database\corefake',
'Select',
],
[
'Drupal\Core\Database\Transaction',
'Drupal\corefake\Driver\Database\corefake',
'Transaction',
],
[
'Drupal\Core\Database\Query\Truncate',
'Drupal\corefake\Driver\Database\corefake',
'Truncate',
],
[
'Drupal\Core\Database\Query\Update',
'Drupal\corefake\Driver\Database\corefake',
'Update',
],
[
'Drupal\Core\Database\Query\Upsert',
'Drupal\corefake\Driver\Database\corefake',
'Upsert',
],
// Tests with the corefakeWithAllCustomClasses database driver. This
// driver has custom driver classes for all classes.
[
'Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses\Condition',
'Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses',
'Condition',
],
[
'Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses\Delete',
'Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses',
'Delete',
],
[
'Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses\Insert',
'Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses',
'Insert',
],
[
'Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses\Merge',
'Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses',
'Merge',
],
[
'Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses\Schema',
'Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses',
'Schema',
],
[
'Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses\Select',
'Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses',
'Select',
],
[
'Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses\Transaction',
'Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses',
'Transaction',
],
[
'Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses\Truncate',
'Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses',
'Truncate',
],
[
'Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses\Update',
'Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses',
'Update',
],
[
'Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses\Upsert',
'Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses',
'Upsert',
],
[
'Drupal\Core\Database\Query\PagerSelectExtender',
'Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses',
'Drupal\Core\Database\Query\PagerSelectExtender',
],
[
'\Drupal\Core\Database\Query\PagerSelectExtender',
'Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses',
'\Drupal\Core\Database\Query\PagerSelectExtender',
],
[
'Drupal\Core\Database\Query\TableSortExtender',
'Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses',
'Drupal\Core\Database\Query\TableSortExtender',
],
[
'\Drupal\Core\Database\Query\TableSortExtender',
'Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses',
'\Drupal\Core\Database\Query\TableSortExtender',
],
[
'Drupal\search\SearchQuery',
'Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses',
'Drupal\search\SearchQuery',
],
[
'\Drupal\search\SearchQuery',
'Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses',
'\Drupal\search\SearchQuery',
],
];
}
/**
* Test getDriverClass().
*
* @covers ::getDriverClass
* @dataProvider providerGetDriverClass
*/
public function testGetDriverClass($expected, $namespace, $class) {
$additional_class_loader = new ClassLoader();
$additional_class_loader->addPsr4("Drupal\\corefake\\Driver\\Database\\corefake\\", __DIR__ . "/../../../../../tests/fixtures/database_drivers/module/corefake/src/Driver/Database/corefake");
$additional_class_loader->addPsr4("Drupal\\corefake\\Driver\\Database\\corefakeWithAllCustomClasses\\", __DIR__ . "/../../../../../tests/fixtures/database_drivers/module/corefake/src/Driver/Database/corefakeWithAllCustomClasses");
$additional_class_loader->register(TRUE);
$mock_pdo = $this->createMock('Drupal\Tests\Core\Database\Stub\StubPDO');
$connection = new StubConnection($mock_pdo, ['namespace' => $namespace]);
// Set the driver using our stub class' public property.
$this->assertEquals($expected, $connection->getDriverClass($class));
}
......
......@@ -2,7 +2,7 @@
namespace Drupal\Tests\Core\Database\Stub;
use Drupal\Core\Database\Driver\mysql\Select as QuerySelect;
use Drupal\Core\Database\Query\Select as QuerySelect;
class Select extends QuerySelect {
......
......@@ -4,6 +4,9 @@
use Drupal\Driver\Database\fake\Connection as BaseConnection;
/**
* Corefake implementation of \Drupal\Core\Database\Connection.
*/
class Connection extends BaseConnection {
/**
......
......@@ -4,6 +4,9 @@
use Drupal\Core\Database\Install\Tasks as InstallTasks;
/**
* Specifies installation tasks for corefake.
*/
class Tasks extends InstallTasks {
/**
......
<?php
namespace Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses;
use Drupal\Core\Database\Query\Condition as QueryCondition;
/**
* CorefakeWithAllCustomClasses implementation of \Drupal\Core\Database\Condition.
*/
class Condition extends QueryCondition {
}
<?php
namespace Drupal\corefake\Driver\Database\corefakeWithAllCustomClasses;
use Drupal\Driver\Database\fake\Connection as BaseConnection;
/**
* CorefakeWithAllCustomClasses implementation of \Drupal\Core\Database\Connection.
*/
class Connection extends BaseConnection {
/**
* {@inheritdoc}
*/
public $driver = 'corefakeWithAllCustomClasses';