diff --git a/core/includes/database.inc b/core/includes/database.inc index 9f0f9939d74b5f99d668ecada1ec55f2b8581db5..bd6e32572c369d4c6a43a2588af3ab2f47361f1f 100644 --- a/core/includes/database.inc +++ b/core/includes/database.inc @@ -21,11 +21,12 @@ * Executes an arbitrary query string against the active database. * * Use this function for SELECT queries if it is just a simple query string. - * If the caller or other modules need to change the query, use db_select() - * instead. + * If the caller or other modules need to change the query, use + * \Drupal::database()->select() instead. * * Do not use this function for INSERT, UPDATE, or DELETE queries. Those should - * be handled via db_insert(), db_update() and db_delete() respectively. + * be handled via \Drupal::database()->insert(), \Drupal::database()->update(), + * \Drupal::database()->merge()and \Drupal::database()->delete(). * * @param string|\Drupal\Core\Database\StatementInterface $query * The prepared statement query to run. Although it will accept both named and @@ -382,14 +383,15 @@ function db_escape_field($field) { * a backslash. Use this to do a search for a verbatim string without any * wildcard behavior. * - * You must use a query builder like db_select() in order to use db_like() on - * all supported database systems. Using db_like() with db_query() or - * db_query_range() is not supported. + * You must use a query builder like \Drupal::database()->select() in order to + * use \Drupal::database()->escapeLike() on all supported database systems. Using + * \Drupal::database()->escapeLike() with \Drupal::database()->query() or + * \Drupal::database()->queryRange() is not supported. * * For example, the following does a case-insensitive query for all rows whose * name starts with $prefix: * @code - * $result = db_select('person', 'p') + * $result = \Drupal::database()->select('person', 'p') * ->fields('p') * ->condition('name', db_like($prefix) . '%', 'LIKE') * ->execute() @@ -1050,8 +1052,8 @@ function db_drop_index($table, $name) { * recreate all indices and primary keys that are using the changed field. * * That means that you have to drop all affected keys and indexes with - * db_drop_{primary_key,unique_key,index}() before calling - * \Drupal\Core\Database\Schema::changeField(). + * \Drupal::database()->schema()->drop{PrimaryKey,UniqueKey,Index}() before + * calling \Drupal\Core\Database\Schema::changeField(). * To recreate the keys and indices, pass the key definitions as the optional * $keys_new argument directly to \Drupal\Core\Database\Schema::changeField(). * @@ -1082,14 +1084,15 @@ function db_drop_index($table, $name) { * * On MySQL, all type 'serial' fields must be part of at least one key or index * as soon as they are created. You cannot use - * db_add_{primary_key,unique_key,index}() for this purpose because the ALTER - * TABLE command will fail to add the column without a key or index - * specification. The solution is to use the optional $keys_new argument to - * create the key or index at the same time as field. - * - * You could use db_add_{primary_key,unique_key,index}() in all cases unless you - * are converting a field to be type serial. You can use the $keys_new argument - * in all cases. + * \Drupal::database()->schema()->add{PrimaryKey,UniqueKey,Index}() for this + * purpose because the ALTER TABLE command will fail to add the column without + * a key or index specification. The solution is to use the optional $keys_new + * argument to create the key or index at the same time as field. + * + * You could use + * \Drupal::database()->schema()->add{PrimaryKey,UniqueKey,Index}() in all + * cases unless you are converting a field to be type serial. You can use the + * $keys_new argument in all cases. * * @param $table * Name of the table. diff --git a/core/includes/form.inc b/core/includes/form.inc index bd256ad16279f22675194e455ec64431be0cc8af..fefa9b3b872b575244af6c8195f6f786c83ab8ff 100644 --- a/core/includes/form.inc +++ b/core/includes/form.inc @@ -630,7 +630,7 @@ function template_preprocess_form_element_label(&$variables) { * ->fetchField(); * } * $limit = 5; - * $result = db_select('example') + * $result = \Drupal::database()->select('example') * ->fields('example', array('id')) * ->condition('id', $context['sandbox']['current_id'], '>') * ->orderBy('id') diff --git a/core/includes/pager.inc b/core/includes/pager.inc index 4c588419f66e07981c5ecd939d1971f64e87c178..81daa34c6e53d840bd9321b060206b339946a1cc 100644 --- a/core/includes/pager.inc +++ b/core/includes/pager.inc @@ -50,7 +50,7 @@ function pager_find_page($element = 0) { * can simply extend the query object with the 'PagerSelectExtender' extender * before executing it. For example: * @code - * $query = db_select('some_table') + * $query = \Drupal::database()->select('some_table') * ->extend('Drupal\Core\Database\Query\PagerSelectExtender'); * @endcode * diff --git a/core/lib/Drupal/Core/Database/Driver/pgsql/Select.php b/core/lib/Drupal/Core/Database/Driver/pgsql/Select.php index e76cb0f9522e15188bb96a545e744b5dfe2102a6..2df730d6139c7f05f8828f415c344abb92b517ee 100644 --- a/core/lib/Drupal/Core/Database/Driver/pgsql/Select.php +++ b/core/lib/Drupal/Core/Database/Driver/pgsql/Select.php @@ -30,7 +30,7 @@ public function orderRandom() { * yet selected. * * @code - * $query = db_select('example', 'e'); + * $query = \Drupal::database()->select('example', 'e'); * $query->join('example_revision', 'er', 'e.vid = er.vid'); * $query * ->distinct() diff --git a/core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php b/core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php index 51c8686c3831662542d6cbe0e4eb542118ef47db..c6f34c5c241f31e3f521839f1e68ddb2c67bc031 100644 --- a/core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php +++ b/core/lib/Drupal/Core/Database/Driver/sqlite/Schema.php @@ -157,7 +157,7 @@ protected function processField($field) { * Create an SQL string for a field to be used in table creation or alteration. * * Before passing a field out of a schema definition into this function it has - * to be processed by db_processField(). + * to be processed by self::processField(). * * @param $name * Name of the field. diff --git a/core/lib/Drupal/Core/Database/Query/ConditionInterface.php b/core/lib/Drupal/Core/Database/Query/ConditionInterface.php index 12e99682b6291d877f28fffc5e217087263742ed..fe5140c2af42b32fa70d1b6bbcecbb551f140172 100644 --- a/core/lib/Drupal/Core/Database/Query/ConditionInterface.php +++ b/core/lib/Drupal/Core/Database/Query/ConditionInterface.php @@ -30,7 +30,7 @@ interface ConditionInterface { * Drupal considers LIKE case insensitive and the following is often used * to tell the database that case insensitive equivalence is desired: * @code - * db_select('users') + * \Drupal::database()->select('users') * ->condition('name', $injected_connection->escapeLike($name), 'LIKE') * @endcode * Use 'LIKE BINARY' instead of 'LIKE' for case sensitive queries. diff --git a/core/lib/Drupal/Core/Database/Schema.php b/core/lib/Drupal/Core/Database/Schema.php index f32d9fb289e7df1ed9c5766fc41cef7c8c170cf9..ad5067789266f58ea84c403a96053a9f504aba6e 100644 --- a/core/lib/Drupal/Core/Database/Schema.php +++ b/core/lib/Drupal/Core/Database/Schema.php @@ -171,7 +171,7 @@ public function tableExists($table) { $condition->compile($this->connection, $this); // Normally, we would heartily discourage the use of string // concatenation for conditionals like this however, we - // couldn't use db_select() here because it would prefix + // couldn't use \Drupal::database()->select() here because it would prefix // information_schema.tables and the query would fail. // Don't use {} around information_schema.tables table. return (bool) $this->connection->query("SELECT 1 FROM information_schema.tables WHERE " . (string) $condition, $condition->arguments())->fetchField(); @@ -198,7 +198,7 @@ public function findTables($table_expression) { $tables = []; // Normally, we would heartily discourage the use of string // concatenation for conditionals like this however, we - // couldn't use db_select() here because it would prefix + // couldn't use \Drupal::database()->select() here because it would prefix // information_schema.tables and the query would fail. // Don't use {} around information_schema.tables table. $results = $this->connection->query("SELECT table_name as table_name FROM information_schema.tables WHERE " . (string) $condition, $condition->arguments()); @@ -253,7 +253,7 @@ public function fieldExists($table, $column) { $condition->compile($this->connection, $this); // Normally, we would heartily discourage the use of string // concatenation for conditionals like this however, we - // couldn't use db_select() here because it would prefix + // couldn't use \Drupal::database()->select() here because it would prefix // information_schema.tables and the query would fail. // Don't use {} around information_schema.columns table. return (bool) $this->connection->query("SELECT 1 FROM information_schema.columns WHERE " . (string) $condition, $condition->arguments())->fetchField(); diff --git a/core/lib/Drupal/Core/Database/database.api.php b/core/lib/Drupal/Core/Database/database.api.php index c39cf78cf4a9f9d7c15182fa4d952b189b78e985..82b6408669ae269846978bed5b5c35960f99ea1a 100644 --- a/core/lib/Drupal/Core/Database/database.api.php +++ b/core/lib/Drupal/Core/Database/database.api.php @@ -93,6 +93,11 @@ * fields (see the @link entity_api Entity API topic @endlink for more on * entity queries). * + * Note: \Drupal::database() is used here as a shorthand way to get a reference + * to the database connection object. In most classes, you should use dependency + * injection and inject the 'database' service to perform queries. See + * @ref sec_connection below for details. + * * The dynamic query API lets you build up a query dynamically using method * calls. As an illustration, the query example from @ref sec_simple above * would be: diff --git a/core/modules/node/tests/src/Kernel/NodeAccessLanguageAwareCombinationTest.php b/core/modules/node/tests/src/Kernel/NodeAccessLanguageAwareCombinationTest.php index b3a84d23ece0818553eff6a62843ab2a69d0abaf..6886f22db6d8a90240446037d0ebb9883978b192 100644 --- a/core/modules/node/tests/src/Kernel/NodeAccessLanguageAwareCombinationTest.php +++ b/core/modules/node/tests/src/Kernel/NodeAccessLanguageAwareCombinationTest.php @@ -262,7 +262,7 @@ public function testNodeAccessLanguageAwareCombination() { // Four nodes should be returned with public Hungarian translations or the // no language public node. - $this->assertEqual(count($nids), 4, 'db_select() returns 4 nodes when no langcode is specified.'); + $this->assertEqual(count($nids), 4, 'Query returns 4 nodes when no langcode is specified.'); $this->assertTrue(array_key_exists($this->nodes['public_both_public']->id(), $nids), 'Returned node ID is full public node.'); $this->assertTrue(array_key_exists($this->nodes['public_ca_private']->id(), $nids), 'Returned node ID is Hungarian public only node.'); $this->assertTrue(array_key_exists($this->nodes['private_both_public']->id(), $nids), 'Returned node ID is both public non-language-aware private only node.'); @@ -277,7 +277,7 @@ public function testNodeAccessLanguageAwareCombination() { $nids = $select->execute()->fetchAllAssoc('nid'); // Three nodes should be returned (with public Hungarian translations). - $this->assertEqual(count($nids), 3, 'db_select() returns 3 nodes.'); + $this->assertEqual(count($nids), 3, 'Query returns 3 nodes.'); $this->assertTrue(array_key_exists($this->nodes['public_both_public']->id(), $nids), 'Returned node ID is both public node.'); $this->assertTrue(array_key_exists($this->nodes['public_ca_private']->id(), $nids), 'Returned node ID is Hungarian public only node.'); $this->assertTrue(array_key_exists($this->nodes['private_both_public']->id(), $nids), 'Returned node ID is both public non-language-aware private only node.'); @@ -291,7 +291,7 @@ public function testNodeAccessLanguageAwareCombination() { $nids = $select->execute()->fetchAllAssoc('nid'); // Three nodes should be returned (with public Catalan translations). - $this->assertEqual(count($nids), 3, 'db_select() returns 3 nodes.'); + $this->assertEqual(count($nids), 3, 'Query returns 3 nodes.'); $this->assertTrue(array_key_exists($this->nodes['public_both_public']->id(), $nids), 'Returned node ID is both public node.'); $this->assertTrue(array_key_exists($this->nodes['public_hu_private']->id(), $nids), 'Returned node ID is Catalan public only node.'); $this->assertTrue(array_key_exists($this->nodes['private_both_public']->id(), $nids), 'Returned node ID is both public non-language-aware private only node.'); @@ -305,7 +305,7 @@ public function testNodeAccessLanguageAwareCombination() { $nids = $select->execute()->fetchAllAssoc('nid'); // There are no nodes with German translations, so no results are returned. - $this->assertTrue(empty($nids), 'db_select() returns an empty result.'); + $this->assertTrue(empty($nids), 'Query returns an empty result.'); // Query the nodes table as admin user (full access) with the node access // tag and no specific langcode. @@ -316,7 +316,7 @@ public function testNodeAccessLanguageAwareCombination() { $nids = $select->execute()->fetchAllAssoc('nid'); // All nodes are returned. - $this->assertEqual(count($nids), 10, 'db_select() returns all nodes.'); + $this->assertEqual(count($nids), 10, 'Query returns all nodes.'); // Query the nodes table as admin user (full access) with the node access // tag and langcode de. @@ -329,7 +329,7 @@ public function testNodeAccessLanguageAwareCombination() { // Even though there is no German translation, all nodes are returned // because node access filtering does not occur when the user is user 1. - $this->assertEqual(count($nids), 10, 'db_select() returns all nodes.'); + $this->assertEqual(count($nids), 10, 'Query returns all nodes.'); } } diff --git a/core/modules/node/tests/src/Kernel/NodeAccessLanguageAwareTest.php b/core/modules/node/tests/src/Kernel/NodeAccessLanguageAwareTest.php index 919ae2cd1e28c9d81c513cb3ede4057e87ea4747..2576060efe1e28dc032476499459ac05f2be7795 100644 --- a/core/modules/node/tests/src/Kernel/NodeAccessLanguageAwareTest.php +++ b/core/modules/node/tests/src/Kernel/NodeAccessLanguageAwareTest.php @@ -10,7 +10,7 @@ use Drupal\field\Entity\FieldStorageConfig; /** - * Tests node_access and db_select() with node_access tag functionality with + * Tests node_access and select queries with node_access tag functionality with * multiple languages with node_access_test_language which is language-aware. * * @group node @@ -220,7 +220,7 @@ public function testNodeAccessLanguageAware() { // Two nodes should be returned: the node with both translations public, and // the node with only the Catalan translation marked as private. - $this->assertEqual(count($nids), 2, 'db_select() returns 2 nodes when the hu langcode is specified.'); + $this->assertEqual(count($nids), 2, 'Query returns 2 nodes when the hu langcode is specified.'); $this->assertTrue(array_key_exists($this->nodes['both_public']->id(), $nids), 'The node with both translations public is returned.'); $this->assertTrue(array_key_exists($this->nodes['ca_private']->id(), $nids), 'The node with only the Catalan translation private is returned.'); @@ -234,7 +234,7 @@ public function testNodeAccessLanguageAware() { // Two nodes should be returned: the node with both translations public, and // the node with only the Hungarian translation marked as private. - $this->assertEqual(count($nids), 2, 'db_select() returns 2 nodes when the hu langcode is specified.'); + $this->assertEqual(count($nids), 2, 'Query returns 2 nodes when the hu langcode is specified.'); $this->assertTrue(array_key_exists($this->nodes['both_public']->id(), $nids), 'The node with both translations public is returned.'); $this->assertTrue(array_key_exists($this->nodes['hu_private']->id(), $nids), 'The node with only the Hungarian translation private is returned.'); @@ -247,7 +247,7 @@ public function testNodeAccessLanguageAware() { $nids = $select->execute()->fetchAllAssoc('nid'); // There are no nodes with German translations, so no results are returned. - $this->assertTrue(empty($nids), 'db_select() returns an empty result when the de langcode is specified.'); + $this->assertTrue(empty($nids), 'Query returns an empty result when the de langcode is specified.'); // Query the nodes table as admin user (full access) with the node access // tag and no specific langcode. @@ -258,7 +258,7 @@ public function testNodeAccessLanguageAware() { $nids = $select->execute()->fetchAllAssoc('nid'); // All nodes are returned. - $this->assertEqual(count($nids), 6, 'db_select() returns all nodes.'); + $this->assertEqual(count($nids), 6, 'Query returns all nodes.'); // Query the nodes table as admin user (full access) with the node access // tag and langcode de. @@ -271,7 +271,7 @@ public function testNodeAccessLanguageAware() { // Even though there is no German translation, all nodes are returned // because node access filtering does not occur when the user is user 1. - $this->assertEqual(count($nids), 6, 'db_select() returns all nodes.'); + $this->assertEqual(count($nids), 6, 'Query returns all nodes.'); } } diff --git a/core/modules/node/tests/src/Kernel/NodeAccessLanguageTest.php b/core/modules/node/tests/src/Kernel/NodeAccessLanguageTest.php index 06709021c628c2a5c2c0283c7a61ebd46ddf8e1a..269240670b9e19fd890e23b3e1e295f165a40dd7 100644 --- a/core/modules/node/tests/src/Kernel/NodeAccessLanguageTest.php +++ b/core/modules/node/tests/src/Kernel/NodeAccessLanguageTest.php @@ -9,7 +9,7 @@ use Drupal\user\Entity\User; /** - * Tests node_access and db_select() with node_access tag functionality with + * Tests node_access and select queries with node_access tag functionality with * multiple languages with a test node access module that is not language-aware. * * @group node @@ -175,7 +175,7 @@ public function testNodeAccessPrivate() { } /** - * Tests db_select() with a 'node_access' tag and langcode metadata. + * Tests select queries with a 'node_access' tag and langcode metadata. */ public function testNodeAccessQueryTag() { // Create a normal authenticated user. @@ -214,7 +214,7 @@ public function testNodeAccessQueryTag() { // The public node and no language node should be returned. Because no // langcode is given it will use the fallback node. - $this->assertEqual(count($nids), 2, 'db_select() returns 2 node'); + $this->assertEqual(count($nids), 2, 'Query returns 2 node'); $this->assertTrue(array_key_exists($node_public->id(), $nids), 'Returned node ID is public node.'); $this->assertTrue(array_key_exists($node_no_language->id(), $nids), 'Returned node ID is no language node.'); @@ -228,7 +228,7 @@ public function testNodeAccessQueryTag() { $nids = $select->execute()->fetchAllAssoc('nid'); // Because no nodes are created in German, no nodes are returned. - $this->assertTrue(empty($nids), 'db_select() returns an empty result.'); + $this->assertTrue(empty($nids), 'Query returns an empty result.'); // Query the nodes table as admin user (full access) with the node access // tag and no specific langcode. @@ -239,7 +239,7 @@ public function testNodeAccessQueryTag() { $nids = $select->execute()->fetchAllAssoc('nid'); // All nodes are returned. - $this->assertEqual(count($nids), 3, 'db_select() returns all three nodes.'); + $this->assertEqual(count($nids), 3, 'Query returns all three nodes.'); // Query the nodes table as admin user (full access) with the node access // tag and langcode de. @@ -252,7 +252,7 @@ public function testNodeAccessQueryTag() { // All nodes are returned because node access tag is not invoked when the // user is user 1. - $this->assertEqual(count($nids), 3, 'db_select() returns all three nodes.'); + $this->assertEqual(count($nids), 3, 'Query returns all three nodes.'); } } diff --git a/core/modules/system/tests/src/Functional/Database/TemporaryQueryTest.php b/core/modules/system/tests/src/Functional/Database/TemporaryQueryTest.php index 3e2cf5259b917a1cedba4d12b216f2bff0d0b5ee..f44073571a8ced01a1e523adc3a80b6f750d276c 100644 --- a/core/modules/system/tests/src/Functional/Database/TemporaryQueryTest.php +++ b/core/modules/system/tests/src/Functional/Database/TemporaryQueryTest.php @@ -38,7 +38,7 @@ public function testTemporaryQuery() { $this->fail('The creation of the temporary table failed.'); } - // Now try to run two db_query_temporary() in the same request. + // Now try to run two temporary queries in the same request. $table_name_test = $connection->queryTemporary('SELECT name FROM {test}', []); $table_name_task = $connection->queryTemporary('SELECT pid FROM {test_task}', []); diff --git a/core/modules/views/src/Plugin/views/display/DefaultDisplay.php b/core/modules/views/src/Plugin/views/display/DefaultDisplay.php index 2d14cfb16cf4289f1c80427e56b778f853b4f13e..2b5b3f291b6c1439f9418a8bc2431d339dbbe0b8 100644 --- a/core/modules/views/src/Plugin/views/display/DefaultDisplay.php +++ b/core/modules/views/src/Plugin/views/display/DefaultDisplay.php @@ -51,7 +51,7 @@ public function isDefaultDisplay() { * * If short circuited at any point, look in $view->build_info for * information about the query. After execute, look in $view->result - * for the array of objects returned from db_query. + * for the array of objects returned from \Drupal::database()->query(). * * You can also do: * @code diff --git a/core/tests/Drupal/KernelTests/Core/Database/RegressionTest.php b/core/tests/Drupal/KernelTests/Core/Database/RegressionTest.php index c5d05a2211a57f306ffc1b0357bb61d6613ef49e..5b501d95b47790ca91ab1117eb9277a3e2f4a5c0 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/RegressionTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/RegressionTest.php @@ -35,7 +35,7 @@ public function testRegression_310447() { } /** - * Tests the db_table_exists() function. + * Tests the Schema::tableExists() method. */ public function testDBTableExists() { $this->assertSame(TRUE, $this->connection->schema()->tableExists('test'), 'Returns true for existent table.');