Commit 82e7ad80 authored by catch's avatar catch

Issue #2999612 by voleger, andypost: Properly deprecate db_query_range

parent 384f184f
......@@ -92,6 +92,7 @@ function db_query($query, array $args = [], array $options = []) {
* @see \Drupal\Core\Database\Connection::defaultOptions()
*/
function db_query_range($query, $from, $count, array $args = [], array $options = []) {
@trigger_error('db_query_range() 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 queryRange() on it. For example, $injected_database->queryRange($query, $from, $count, $args, $options). See https://www.drupal.org/node/2993033', E_USER_DEPRECATED);
if (empty($options['target'])) {
$options['target'] = 'default';
}
......
......@@ -4,6 +4,7 @@
use Drupal\aggregator\Entity\Feed;
use Drupal\Component\Utility\Html;
use Drupal\Core\Database\Database;
use Drupal\simpletest\WebTestBase;
use Drupal\aggregator\FeedInterface;
......@@ -150,7 +151,7 @@ public function getFeedEditObject($feed_url = NULL, array $values = []) {
*/
public function getDefaultFeedItemCount() {
// Our tests are based off of rss.xml, so let's find out how many elements should be related.
$feed_count = db_query_range('SELECT COUNT(DISTINCT nid) FROM {node_field_data} n WHERE n.promote = 1 AND n.status = 1', 0, $this->config('system.rss')->get('items.limit'))->fetchField();
$feed_count = Database::getConnection()->queryRange('SELECT COUNT(DISTINCT nid) FROM {node_field_data} n WHERE n.promote = 1 AND n.status = 1', 0, $this->config('system.rss')->get('items.limit'))->fetchField();
return $feed_count > 10 ? 10 : $feed_count;
}
......
......@@ -4,6 +4,7 @@
use Drupal\aggregator\Entity\Feed;
use Drupal\Component\Utility\Html;
use Drupal\Core\Database\Database;
use Drupal\Tests\BrowserTestBase;
use Drupal\aggregator\FeedInterface;
......@@ -147,7 +148,7 @@ public function getFeedEditObject($feed_url = NULL, array $values = []) {
*/
public function getDefaultFeedItemCount() {
// Our tests are based off of rss.xml, so let's find out how many elements should be related.
$feed_count = db_query_range('SELECT COUNT(DISTINCT nid) FROM {node_field_data} n WHERE n.promote = 1 AND n.status = 1', 0, $this->config('system.rss')->get('items.limit'))->fetchField();
$feed_count = Database::getConnection()->queryRange('SELECT COUNT(DISTINCT nid) FROM {node_field_data} n WHERE n.promote = 1 AND n.status = 1', 0, $this->config('system.rss')->get('items.limit'))->fetchField();
return $feed_count > 10 ? 10 : $feed_count;
}
......
......@@ -3,6 +3,7 @@
namespace Drupal\Tests\dblog\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Database\Database;
use Drupal\Core\Url;
use Drupal\Tests\rest\Functional\CookieResourceTestTrait;
use Drupal\Tests\rest\Functional\ResourceTestBase;
......@@ -58,7 +59,7 @@ public function testWatchdog() {
// Write a log message to the DB.
$this->container->get('logger.channel.rest')->notice('Test message');
// Get the ID of the written message.
$id = db_query_range("SELECT wid FROM {watchdog} WHERE type = :type ORDER BY wid DESC", 0, 1, [':type' => 'rest'])
$id = Database::getConnection()->queryRange("SELECT wid FROM {watchdog} WHERE type = :type ORDER BY wid DESC", 0, 1, [':type' => 'rest'])
->fetchField();
$this->initAuthentication();
......
......@@ -265,7 +265,7 @@ private function verifyLinkEscaping() {
'link' => $link,
]);
$result = db_query_range('SELECT wid FROM {watchdog} ORDER BY wid DESC', 0, 1);
$result = Database::getConnection()->queryRange('SELECT wid FROM {watchdog} ORDER BY wid DESC', 0, 1);
$this->drupalGet('admin/reports/dblog/event/' . $result->fetchField());
// Check if the link exists (unescaped).
......
......@@ -2,6 +2,7 @@
namespace Drupal\system\Tests\Form;
use Drupal\Core\Database\Database;
use Drupal\simpletest\WebTestBase;
/**
......@@ -174,7 +175,7 @@ public function testImmutableFormLegacyProtection() {
// Assert that a watchdog message was logged by
// \Drupal::formBuilder()->setCache().
$status = (bool) db_query_range('SELECT 1 FROM {watchdog} WHERE message = :message', 0, 1, [':message' => 'Form build-id mismatch detected while attempting to store a form in the cache.']);
$status = (bool) Database::getConnection()->queryRange('SELECT 1 FROM {watchdog} WHERE message = :message', 0, 1, [':message' => 'Form build-id mismatch detected while attempting to store a form in the cache.']);
$this->assert($status, 'A watchdog message was logged by \Drupal::formBuilder()->setCache');
// Ensure that the form state was not poisoned by the preceding call.
......
......@@ -398,4 +398,14 @@ public function testDbQueryTemporary() {
$this->assertSame($expected, $count);
}
/**
* Tests deprecation of the db_query_range() function.
*
* @expectedDeprecation db_query_range() 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 queryRange() on it. For example, $injected_database->queryRange($query, $from, $count, $args, $options). See https://www.drupal.org/node/2993033
*/
public function testDbQueryRange() {
$count = count(db_query_range('SELECT name FROM {test}', 1, 3)->fetchAll());
$this->assertSame(3, $count);
}
}
......@@ -14,7 +14,7 @@ class RangeQueryTest extends DatabaseTestBase {
*/
public function testRangeQuery() {
// Test if return correct number of rows.
$range_rows = db_query_range("SELECT name FROM {test} ORDER BY name", 1, 3)->fetchAll();
$range_rows = $this->connection->queryRange("SELECT name FROM {test} ORDER BY name", 1, 3)->fetchAll();
$this->assertEqual(count($range_rows), 3, 'Range query work and return correct number of rows.');
// Test if return target data.
......
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