diff --git a/core/includes/database.inc b/core/includes/database.inc index 46bba23a498..0c165061c32 100644 --- a/core/includes/database.inc +++ b/core/includes/database.inc @@ -116,7 +116,7 @@ function db_query_range($query, $from, $count, array $args = [], array $options * @param array $options * An array of options to control how the query operates. * - * @return + * @return string * The name of the temporary table. * * @deprecated as of Drupal 8.0.x, will be removed in Drupal 9.0.0. Instead, get @@ -128,6 +128,7 @@ function db_query_range($query, $from, $count, array $args = [], array $options * @see \Drupal\Core\Database\Connection::defaultOptions() */ function db_query_temporary($query, array $args = [], array $options = []) { + @trigger_error('db_query_temporary() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Instead, get a database connection injected into your service from the container and call queryTemporary() on it. For example, $injected_database->queryTemporary($query, $args, $options). See https://www.drupal.org/node/2993033', E_USER_DEPRECATED); if (empty($options['target'])) { $options['target'] = 'default'; } diff --git a/core/modules/system/tests/modules/database_test/src/Controller/DatabaseTestController.php b/core/modules/system/tests/modules/database_test/src/Controller/DatabaseTestController.php index 56ea9d140d6..809225909a0 100644 --- a/core/modules/system/tests/modules/database_test/src/Controller/DatabaseTestController.php +++ b/core/modules/system/tests/modules/database_test/src/Controller/DatabaseTestController.php @@ -2,15 +2,44 @@ namespace Drupal\database_test\Controller; +use Drupal\Core\Controller\ControllerBase; +use Drupal\Core\Database\Connection; +use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\JsonResponse; /** * Controller routines for database_test routes. */ -class DatabaseTestController { +class DatabaseTestController extends ControllerBase { /** - * Runs db_query_temporary() and outputs the table name and its number of rows. + * The database connection. + * + * @var \Drupal\Core\Database\Connection + */ + protected $connection; + + /** + * Constructs a DatabaseTestController object. + * + * @param \Drupal\Core\Database\Connection $connection + * A database connection. + */ + public function __construct(Connection $connection) { + $this->connection = $connection; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('database') + ); + } + + /** + * Creates temporary table and outputs the table name and its number of rows. * * We need to test that the table created is temporary, so we run it here, in a * separate menu callback request; After this request is done, the temporary @@ -19,7 +48,7 @@ class DatabaseTestController { * @return \Symfony\Component\HttpFoundation\JsonResponse */ public function dbQueryTemporary() { - $table_name = db_query_temporary('SELECT age FROM {test}', []); + $table_name = $this->connection->queryTemporary('SELECT age FROM {test}', []); return new JsonResponse([ 'table_name' => $table_name, 'row_count' => db_select($table_name)->countQuery()->execute()->fetchField(), diff --git a/core/modules/system/tests/src/Functional/Database/TemporaryQueryTest.php b/core/modules/system/tests/src/Functional/Database/TemporaryQueryTest.php index e7d08502f35..e06cf8cc6c1 100644 --- a/core/modules/system/tests/src/Functional/Database/TemporaryQueryTest.php +++ b/core/modules/system/tests/src/Functional/Database/TemporaryQueryTest.php @@ -27,19 +27,20 @@ class TemporaryQueryTest extends DatabaseTestBase { * Confirms that temporary tables work and are limited to one request. */ public function testTemporaryQuery() { + $connection = Database::getConnection(); $this->drupalGet('database_test/db_query_temporary'); $data = json_decode($this->getSession()->getPage()->getContent()); if ($data) { $this->assertEqual($this->countTableRows('test'), $data->row_count, 'The temporary table contains the correct amount of rows.'); - $this->assertFalse(Database::getConnection()->schema()->tableExists($data->table_name), 'The temporary table is, indeed, temporary.'); + $this->assertFalse($connection->schema()->tableExists($data->table_name), 'The temporary table is, indeed, temporary.'); } else { $this->fail('The creation of the temporary table failed.'); } // Now try to run two db_query_temporary() in the same request. - $table_name_test = db_query_temporary('SELECT name FROM {test}', []); - $table_name_task = db_query_temporary('SELECT pid FROM {test_task}', []); + $table_name_test = $connection->queryTemporary('SELECT name FROM {test}', []); + $table_name_task = $connection->queryTemporary('SELECT pid FROM {test_task}', []); $this->assertEqual($this->countTableRows($table_name_test), $this->countTableRows('test'), 'A temporary table was created successfully in this request.'); $this->assertEqual($this->countTableRows($table_name_task), $this->countTableRows('test_task'), 'A second temporary table was created successfully in this request.'); @@ -50,7 +51,7 @@ class TemporaryQueryTest extends DatabaseTestBase { -- Let's select some rows into a temporary table SELECT name FROM {test} "; - $table_name_test = db_query_temporary($sql, []); + $table_name_test = $connection->queryTemporary($sql, []); $this->assertEqual($this->countTableRows($table_name_test), $this->countTableRows('test'), 'Leading white space and comments do not interfere with temporary table creation.'); } diff --git a/core/tests/Drupal/KernelTests/Core/Database/DatabaseLegacyTest.php b/core/tests/Drupal/KernelTests/Core/Database/DatabaseLegacyTest.php index 64c348c632e..b6774d66722 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/DatabaseLegacyTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/DatabaseLegacyTest.php @@ -386,4 +386,16 @@ class DatabaseLegacyTest extends DatabaseTestBase { $this->assertInstanceOf(Truncate::class, db_truncate('test')); } + /** + * Tests deprecation of the db_query_temporary() function. + * + * @expectedDeprecation db_query_temporary() is deprecated in Drupal 8.0.x and will be removed before Drupal 9.0.0. Instead, get a database connection injected into your service from the container and call queryTemporary() on it. For example, $injected_database->queryTemporary($query, $args, $options). See https://www.drupal.org/node/2993033 + */ + public function testDbQueryTemporary() { + $expected = $this->connection->select('test')->countQuery()->execute()->fetchField(); + $name = db_query_temporary('SELECT name FROM {test}'); + $count = $this->connection->select($name)->countQuery()->execute()->fetchField(); + $this->assertSame($expected, $count); + } + }