Commit 383b06f2 authored by catch's avatar catch
Browse files

Issue #2849745 by voleger, yogeshmpawar, gaurav.kapoor, Berdir,...

Issue #2849745 by voleger, yogeshmpawar, gaurav.kapoor, Berdir, Munavijayalakshmi, vacho, cilefen, daffie: Replace documentation recommending db_*() wrappers
parent c848fb6b
......@@ -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.
......
......@@ -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')
......
......@@ -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
*
......
......@@ -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()
......
......@@ -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.
......
......@@ -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.
......
......@@ -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();
......
......@@ -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:
......
......@@ -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.');
}
}
......@@ -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.');
}
}
......@@ -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.');
}
}
......@@ -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}', []);
......
......@@ -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
......
......@@ -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.');
......
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