Issue #2996441 by andypost, longwave: Replace all calls to db_query_temporary, which is deprecated

8.7.x
Nathaniel Catchpole 2018-09-14 08:40:42 +01:00
parent 1bffa61218
commit d122ac7cfa
4 changed files with 51 additions and 8 deletions

View File

@ -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';
}

View File

@ -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(),

View File

@ -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.');
}

View File

@ -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);
}
}