Commit d122ac7c authored by catch's avatar catch

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

parent 1bffa612
......@@ -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';
}
......
......@@ -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(),
......
......@@ -27,19 +27,20 @@ public function countTableRows($table_name) {
* 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 @@ public function testTemporaryQuery() {
-- 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.');
}
......
......@@ -386,4 +386,16 @@ public function testDbTruncate() {
$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);
}
}
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