diff --git a/core/includes/database.inc b/core/includes/database.inc index 0ed50818b4..f0b7ae5e04 100644 --- a/core/includes/database.inc +++ b/core/includes/database.inc @@ -157,6 +157,7 @@ function db_query_temporary($query, array $args = [], array $options = []) { * @see \Drupal\Core\Database\Connection::defaultOptions() */ function db_insert($table, array $options = []) { + @trigger_error('db_insert() 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 insert() on it. For example, $injected_database->insert($table, $options); See https://www.drupal.org/node/2993033', E_USER_DEPRECATED); if (empty($options['target']) || $options['target'] == 'replica') { $options['target'] = 'default'; } diff --git a/core/lib/Drupal/Core/Entity/entity.api.php b/core/lib/Drupal/Core/Entity/entity.api.php index ebbbf63bdb..c4e9b84e4f 100644 --- a/core/lib/Drupal/Core/Entity/entity.api.php +++ b/core/lib/Drupal/Core/Entity/entity.api.php @@ -1081,7 +1081,7 @@ function hook_ENTITY_TYPE_presave(Drupal\Core\Entity\EntityInterface $entity) { */ function hook_entity_insert(Drupal\Core\Entity\EntityInterface $entity) { // Insert the new entity into a fictional table of all entities. - db_insert('example_entity') + \Drupal::database()->insert('example_entity') ->fields([ 'type' => $entity->getEntityTypeId(), 'id' => $entity->id(), @@ -1105,7 +1105,7 @@ function hook_entity_insert(Drupal\Core\Entity\EntityInterface $entity) { */ function hook_ENTITY_TYPE_insert(Drupal\Core\Entity\EntityInterface $entity) { // Insert the new entity into a fictional table of this type of entity. - db_insert('example_entity') + \Drupal::database()->insert('example_entity') ->fields([ 'id' => $entity->id(), 'created' => REQUEST_TIME, diff --git a/core/modules/contact/tests/drupal-7.contact.database.php b/core/modules/contact/tests/drupal-7.contact.database.php index c427851305..8ace9362c8 100644 --- a/core/modules/contact/tests/drupal-7.contact.database.php +++ b/core/modules/contact/tests/drupal-7.contact.database.php @@ -17,7 +17,7 @@ ->execute(); // Add a custom contact category. -db_insert('contact')->fields([ +$connection->insert('contact')->fields([ 'category', 'recipients', 'reply', diff --git a/core/modules/file/tests/src/Kernel/UsageTest.php b/core/modules/file/tests/src/Kernel/UsageTest.php index b591a44c80..1e5473b5ee 100644 --- a/core/modules/file/tests/src/Kernel/UsageTest.php +++ b/core/modules/file/tests/src/Kernel/UsageTest.php @@ -22,7 +22,8 @@ class UsageTest extends FileManagedUnitTestBase { */ public function testGetUsage() { $file = $this->createFile(); - db_insert('file_usage') + $connection = Database::getConnection(); + $connection->insert('file_usage') ->fields([ 'fid' => $file->id(), 'module' => 'testing', @@ -31,7 +32,7 @@ public function testGetUsage() { 'count' => 1, ]) ->execute(); - db_insert('file_usage') + $connection->insert('file_usage') ->fields([ 'fid' => $file->id(), 'module' => 'testing', @@ -105,7 +106,8 @@ public function doTestRemoveUsage() { $file = $this->createFile(); $file->setPermanent(); $file_usage = $this->container->get('file.usage'); - db_insert('file_usage') + $connection = Database::getConnection(); + $connection->insert('file_usage') ->fields([ 'fid' => $file->id(), 'module' => 'testing', diff --git a/core/modules/history/tests/src/Kernel/Views/HistoryTimestampTest.php b/core/modules/history/tests/src/Kernel/Views/HistoryTimestampTest.php index 5aebaf43a1..f66ce52476 100644 --- a/core/modules/history/tests/src/Kernel/Views/HistoryTimestampTest.php +++ b/core/modules/history/tests/src/Kernel/Views/HistoryTimestampTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\history\Kernel\Views; +use Drupal\Core\Database\Database; use Drupal\node\Entity\Node; use Drupal\Tests\views\Kernel\ViewsKernelTestBase; use Drupal\user\Entity\User; @@ -67,14 +68,15 @@ public function testHandlers() { $account->save(); \Drupal::currentUser()->setAccount($account); - db_insert('history') + $connection = Database::getConnection(); + $connection->insert('history') ->fields([ 'uid' => $account->id(), 'nid' => $nodes[0]->id(), 'timestamp' => REQUEST_TIME - 100, ])->execute(); - db_insert('history') + $connection->insert('history') ->fields([ 'uid' => $account->id(), 'nid' => $nodes[1]->id(), diff --git a/core/modules/locale/tests/src/Functional/LocaleUpdateBase.php b/core/modules/locale/tests/src/Functional/LocaleUpdateBase.php index 1a5debf44a..387b05b0a0 100644 --- a/core/modules/locale/tests/src/Functional/LocaleUpdateBase.php +++ b/core/modules/locale/tests/src/Functional/LocaleUpdateBase.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\locale\Functional; +use Drupal\Core\Database\Database; use Drupal\Core\StreamWrapper\PublicStream; use Drupal\file\Entity\File; use Drupal\Tests\BrowserTestBase; @@ -278,9 +279,10 @@ protected function setCurrentTranslations() { 'filename' => 'custom_module_one.de.po', 'version' => '', ]; + $connection = Database::getConnection(); foreach ($data as $file) { $file = array_merge($default, $file); - db_insert('locale_file')->fields($file)->execute(); + $connection->insert('locale_file')->fields($file)->execute(); } } diff --git a/core/modules/node/node.api.php b/core/modules/node/node.api.php index e18cf66df5..bbe47626ea 100644 --- a/core/modules/node/node.api.php +++ b/core/modules/node/node.api.php @@ -51,7 +51,7 @@ * 'grant_update' => 0, * 'grant_delete' => 0, * ); - * db_insert('node_access')->fields($record)->execute(); + * \Drupal::database()->insert('node_access')->fields($record)->execute(); * @endcode * And then in its hook_node_grants() implementation, it would need to return: * @code diff --git a/core/modules/node/node.install b/core/modules/node/node.install index 0ccebb8b77..557153eb59 100644 --- a/core/modules/node/node.install +++ b/core/modules/node/node.install @@ -134,7 +134,7 @@ function node_install() { } // Populate the node access table. - db_insert('node_access') + Database::getConnection()->insert('node_access') ->fields([ 'nid' => 0, 'gid' => 0, diff --git a/core/modules/node/tests/src/Functional/NodeAccessGrantsCacheContextTest.php b/core/modules/node/tests/src/Functional/NodeAccessGrantsCacheContextTest.php index fa2116ab87..e0a466e21d 100644 --- a/core/modules/node/tests/src/Functional/NodeAccessGrantsCacheContextTest.php +++ b/core/modules/node/tests/src/Functional/NodeAccessGrantsCacheContextTest.php @@ -2,6 +2,8 @@ namespace Drupal\Tests\node\Functional; +use Drupal\Core\Database\Database; + /** * Tests the node access grants cache context service. * @@ -92,7 +94,7 @@ public function testCacheContext() { 'grant_update' => 0, 'grant_delete' => 0, ]; - db_insert('node_access')->fields($record)->execute(); + Database::getConnection()->insert('node_access')->fields($record)->execute(); // Put user accessUser (uid 0) in the realm. \Drupal::state()->set('node_access_test.no_access_uid', 0); diff --git a/core/modules/node/tests/src/Functional/NodeQueryAlterTest.php b/core/modules/node/tests/src/Functional/NodeQueryAlterTest.php index 362a27fc48..c26b8446c1 100644 --- a/core/modules/node/tests/src/Functional/NodeQueryAlterTest.php +++ b/core/modules/node/tests/src/Functional/NodeQueryAlterTest.php @@ -2,6 +2,8 @@ namespace Drupal\Tests\node\Functional; +use Drupal\Core\Database\Database; + /** * Tests that node access queries are properly altered by the node module. * @@ -151,7 +153,8 @@ public function testNodeQueryAlterOverride() { 'grant_update' => 0, 'grant_delete' => 0, ]; - db_insert('node_access')->fields($record)->execute(); + $connection = Database::getConnection(); + $connection->insert('node_access')->fields($record)->execute(); // Test that the noAccessUser still doesn't have the 'view' // privilege after adding the node_access record. diff --git a/core/modules/path/path.api.php b/core/modules/path/path.api.php index f41a37faf3..5df539b52c 100644 --- a/core/modules/path/path.api.php +++ b/core/modules/path/path.api.php @@ -20,7 +20,7 @@ * @see \Drupal\Core\Path\AliasStorageInterface::save() */ function hook_path_insert($path) { - db_insert('mytable') + \Drupal::database()->insert('mytable') ->fields([ 'alias' => $path['alias'], 'pid' => $path['pid'], diff --git a/core/modules/search/search.module b/core/modules/search/search.module index d86fe46495..52c5149c38 100644 --- a/core/modules/search/search.module +++ b/core/modules/search/search.module @@ -540,7 +540,7 @@ function search_index($type, $sid, $langcode, $text) { $connection = \Drupal::database(); // Insert cleaned up data into dataset - db_insert('search_dataset') + $connection->insert('search_dataset') ->fields([ 'sid' => $sid, 'langcode' => $langcode, diff --git a/core/modules/search/tests/src/Functional/SearchRankingTest.php b/core/modules/search/tests/src/Functional/SearchRankingTest.php index 4fafad1956..49bfc4c418 100644 --- a/core/modules/search/tests/src/Functional/SearchRankingTest.php +++ b/core/modules/search/tests/src/Functional/SearchRankingTest.php @@ -4,6 +4,7 @@ use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; use Drupal\comment\Tests\CommentTestTrait; +use Drupal\Core\Database\Database; use Drupal\Core\Url; use Drupal\filter\Entity\FilterFormat; use Drupal\search\Entity\SearchPage; @@ -104,7 +105,7 @@ public function testRankings() { // to the Statistics module. So instead go ahead and manually update the // counter for this node. $nid = $nodes['views'][1]->id(); - db_insert('node_counter') + Database::getConnection()->insert('node_counter') ->fields(['totalcount' => 5, 'daycount' => 5, 'timestamp' => REQUEST_TIME, 'nid' => $nid]) ->execute(); diff --git a/core/modules/simpletest/simpletest.module b/core/modules/simpletest/simpletest.module index 0814d882d7..1b603ab736 100644 --- a/core/modules/simpletest/simpletest.module +++ b/core/modules/simpletest/simpletest.module @@ -138,7 +138,7 @@ function simpletest_run_tests($test_list) { unset($test_list['phpunit']); } - $test_id = db_insert('simpletest_test_id') + $test_id = \Drupal::database()->insert('simpletest_test_id') ->useDefaults(['test_id']) ->execute(); diff --git a/core/modules/system/tests/modules/module_test/module_test.install b/core/modules/system/tests/modules/module_test/module_test.install index b76b7936f5..9d80d89605 100644 --- a/core/modules/system/tests/modules/module_test/module_test.install +++ b/core/modules/system/tests/modules/module_test/module_test.install @@ -5,6 +5,8 @@ * Install, update and uninstall functions for the module_test module. */ +use Drupal\Core\Database\Database; + /** * Implements hook_schema(). */ @@ -29,7 +31,7 @@ function module_test_schema() { */ function module_test_install() { $schema = drupal_get_module_schema('module_test', 'module_test'); - db_insert('module_test') + Database::getConnection()->insert('module_test') ->fields([ 'data' => $schema['fields']['data']['type'], ]) diff --git a/core/modules/tracker/tracker.module b/core/modules/tracker/tracker.module index 58e1a348de..60bf68c17d 100644 --- a/core/modules/tracker/tracker.module +++ b/core/modules/tracker/tracker.module @@ -70,7 +70,7 @@ function tracker_cron() { ->execute(); // Insert the node-level data. - db_insert('tracker_node') + $connection->insert('tracker_node') ->fields([ 'nid' => $nid, 'published' => (int) $node->isPublished(), @@ -79,7 +79,7 @@ function tracker_cron() { ->execute(); // Insert the user-level data for the node's author. - db_insert('tracker_user') + $connection->insert('tracker_user') ->fields([ 'nid' => $nid, 'published' => (int) $node->isPublished(), @@ -102,7 +102,7 @@ function tracker_cron() { ->groupBy('uid') ->execute(); if ($result) { - $query = db_insert('tracker_user'); + $query = $connection->insert('tracker_user'); foreach ($result as $row) { $query->fields([ 'uid' => $row['uid'], diff --git a/core/modules/user/user.api.php b/core/modules/user/user.api.php index 1abacda26f..f177088ae1 100644 --- a/core/modules/user/user.api.php +++ b/core/modules/user/user.api.php @@ -155,7 +155,7 @@ function hook_user_login($account) { * The user object on which the operation was just performed. */ function hook_user_logout($account) { - db_insert('logouts') + \Drupal::database()->insert('logouts') ->fields([ 'uid' => $account->id(), 'time' => time(), diff --git a/core/modules/views/src/Tests/ViewKernelTestBase.php b/core/modules/views/src/Tests/ViewKernelTestBase.php index 5a23285918..af306f0541 100644 --- a/core/modules/views/src/Tests/ViewKernelTestBase.php +++ b/core/modules/views/src/Tests/ViewKernelTestBase.php @@ -2,6 +2,7 @@ namespace Drupal\views\Tests; +use Drupal\Core\Database\Database; use Drupal\Core\Database\Query\SelectInterface; use Drupal\simpletest\KernelTestBase; @@ -71,7 +72,7 @@ protected function setUpFixtures() { // Load the test dataset. $data_set = $this->dataSet(); - $query = db_insert('views_test_data') + $query = Database::getConnection()->insert('views_test_data') ->fields(array_keys($data_set[0])); foreach ($data_set as $record) { $query->values($record); diff --git a/core/modules/views/src/Tests/ViewTestBase.php b/core/modules/views/src/Tests/ViewTestBase.php index e1e5190f59..1b799e1239 100644 --- a/core/modules/views/src/Tests/ViewTestBase.php +++ b/core/modules/views/src/Tests/ViewTestBase.php @@ -4,6 +4,7 @@ @trigger_error('\Drupal\views\Tests\ViewTestBase is deprecated in Drupal 8.4.0 and will be removed before Drupal 9.0.0. Instead, use \Drupal\Tests\views\Functional\ViewTestBase', E_USER_DEPRECATED); +use Drupal\Core\Database\Database; use Drupal\Core\Database\Query\SelectInterface; use Drupal\simpletest\WebTestBase; use Drupal\views\ViewExecutable; @@ -57,7 +58,7 @@ protected function enableViewsTestModule() { // Load the test dataset. $data_set = $this->dataSet(); - $query = db_insert('views_test_data') + $query = Database::getConnection()->insert('views_test_data') ->fields(array_keys($data_set[0])); foreach ($data_set as $record) { $query->values($record); diff --git a/core/modules/views/tests/src/Functional/Plugin/StyleTableTest.php b/core/modules/views/tests/src/Functional/Plugin/StyleTableTest.php index 5f1e642aa1..00d839da83 100644 --- a/core/modules/views/tests/src/Functional/Plugin/StyleTableTest.php +++ b/core/modules/views/tests/src/Functional/Plugin/StyleTableTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\views\Functional\Plugin; +use Drupal\Core\Database\Database; use Drupal\dynamic_page_cache\EventSubscriber\DynamicPageCacheSubscriber; use Drupal\Tests\views\Functional\ViewTestBase; use Drupal\views\Entity\View; @@ -115,7 +116,7 @@ public function testNumericFieldVisible() { // Adds a new datapoint in the views_test_data table to have a person with // an age of zero. $data_set = $this->dataSet(); - $query = db_insert('views_test_data') + $query = Database::getConnection()->insert('views_test_data') ->fields(array_keys($data_set[0])); $query->values([ 'name' => 'James McCartney', diff --git a/core/modules/views/tests/src/Functional/ViewTestBase.php b/core/modules/views/tests/src/Functional/ViewTestBase.php index 10bc5d48bc..186a8fea1e 100644 --- a/core/modules/views/tests/src/Functional/ViewTestBase.php +++ b/core/modules/views/tests/src/Functional/ViewTestBase.php @@ -3,6 +3,7 @@ namespace Drupal\Tests\views\Functional; use Behat\Mink\Exception\ElementNotFoundException; +use Drupal\Core\Database\Database; use Drupal\Core\Database\Query\SelectInterface; use Drupal\Tests\BrowserTestBase; use Drupal\views\Tests\ViewResultAssertionTrait; @@ -55,7 +56,7 @@ protected function enableViewsTestModule() { // Load the test dataset. $data_set = $this->dataSet(); - $query = db_insert('views_test_data') + $query = Database::getConnection()->insert('views_test_data') ->fields(array_keys($data_set[0])); foreach ($data_set as $record) { $query->values($record); diff --git a/core/modules/views/tests/src/Kernel/Plugin/CacheTest.php b/core/modules/views/tests/src/Kernel/Plugin/CacheTest.php index caa479b4a1..5a2a70f1a8 100644 --- a/core/modules/views/tests/src/Kernel/Plugin/CacheTest.php +++ b/core/modules/views/tests/src/Kernel/Plugin/CacheTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\views\Kernel\Plugin; +use Drupal\Core\Database\Database; use Drupal\Core\Render\RenderContext; use Drupal\node\Entity\Node; use Drupal\Tests\views\Kernel\ViewsKernelTestBase; @@ -88,7 +89,7 @@ public function testTimeResultCaching() { 'age' => 29, 'job' => 'Banjo', ]; - db_insert('views_test_data')->fields($record)->execute(); + Database::getConnection()->insert('views_test_data')->fields($record)->execute(); // The result should be the same as before, because of the caching. (Note // that views_test_data records don't have associated cache tags, and hence @@ -242,7 +243,7 @@ public function testNoneResultCaching() { 'age' => 29, 'job' => 'Banjo', ]; - db_insert('views_test_data')->fields($record)->execute(); + Database::getConnection()->insert('views_test_data')->fields($record)->execute(); // The Result changes, because the view is not cached. $view = Views::getView('test_cache'); diff --git a/core/tests/Drupal/KernelTests/Core/Config/Storage/DatabaseStorageTest.php b/core/tests/Drupal/KernelTests/Core/Config/Storage/DatabaseStorageTest.php index 288357c0fc..83cc87dbae 100644 --- a/core/tests/Drupal/KernelTests/Core/Config/Storage/DatabaseStorageTest.php +++ b/core/tests/Drupal/KernelTests/Core/Config/Storage/DatabaseStorageTest.php @@ -31,7 +31,7 @@ protected function read($name) { } protected function insert($name, $data) { - db_insert('config')->fields(['name' => $name, 'data' => $data])->execute(); + Database::getConnection()->insert('config')->fields(['name' => $name, 'data' => $data])->execute(); } protected function update($name, $data) { diff --git a/core/tests/Drupal/KernelTests/Core/Database/BasicSyntaxTest.php b/core/tests/Drupal/KernelTests/Core/Database/BasicSyntaxTest.php index 837b8ea036..ec12d05945 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/BasicSyntaxTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/BasicSyntaxTest.php @@ -76,7 +76,7 @@ public function testConcatWsFields() { * Tests escaping of LIKE wildcards. */ public function testLikeEscape() { - db_insert('test') + $this->connection->insert('test') ->fields([ 'name' => 'Ring_', ]) @@ -102,7 +102,7 @@ public function testLikeEscape() { * Tests a LIKE query containing a backslash. */ public function testLikeBackslash() { - db_insert('test') + $this->connection->insert('test') ->fields(['name']) ->values([ 'name' => 'abcde\f', diff --git a/core/tests/Drupal/KernelTests/Core/Database/CaseSensitivityTest.php b/core/tests/Drupal/KernelTests/Core/Database/CaseSensitivityTest.php index 37b876c8f8..1a2e90073a 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/CaseSensitivityTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/CaseSensitivityTest.php @@ -15,7 +15,7 @@ class CaseSensitivityTest extends DatabaseTestBase { public function testCaseSensitiveInsert() { $num_records_before = db_query('SELECT COUNT(*) FROM {test}')->fetchField(); - db_insert('test') + $this->connection->insert('test') ->fields([ // A record already exists with name 'John'. 'name' => 'john', diff --git a/core/tests/Drupal/KernelTests/Core/Database/DatabaseLegacyTest.php b/core/tests/Drupal/KernelTests/Core/Database/DatabaseLegacyTest.php index bbc8cb9ca8..73ab6bbe4c 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/DatabaseLegacyTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/DatabaseLegacyTest.php @@ -4,6 +4,7 @@ use Drupal\Core\Database\Query\Condition; use Drupal\Core\Database\Query\Delete; +use Drupal\Core\Database\Query\Insert; use Drupal\Core\Database\Query\Merge; use Drupal\Core\Database\Query\Truncate; use Drupal\Core\Database\Query\Update; @@ -417,4 +418,13 @@ public function testDbQueryRange() { $this->assertSame(3, $count); } + /** + * Tests the db_insert() function. + * + * @expectedDeprecation db_insert() 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 insert() on it. For example, $injected_database->insert($table, $options); See https://www.drupal.org/node/2993033 + */ + public function testDbInsert() { + $this->assertInstanceOf(Insert::class, db_insert('test')); + } + } diff --git a/core/tests/Drupal/KernelTests/Core/Database/InsertDefaultsTest.php b/core/tests/Drupal/KernelTests/Core/Database/InsertDefaultsTest.php index b5963cf420..13ef94e4cb 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/InsertDefaultsTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/InsertDefaultsTest.php @@ -15,7 +15,7 @@ class InsertDefaultsTest extends DatabaseTestBase { * Tests that we can run a query that uses default values for everything. */ public function testDefaultInsert() { - $query = db_insert('test')->useDefaults(['job']); + $query = $this->connection->insert('test')->useDefaults(['job']); $id = $query->execute(); $schema = drupal_get_module_schema('database_test', 'test'); @@ -31,7 +31,7 @@ public function testDefaultEmptyInsert() { $num_records_before = (int) db_query('SELECT COUNT(*) FROM {test}')->fetchField(); try { - db_insert('test')->execute(); + $this->connection->insert('test')->execute(); // This is only executed if no exception has been thrown. $this->fail('Expected exception NoFieldsException has not been thrown.'); } @@ -47,7 +47,7 @@ public function testDefaultEmptyInsert() { * Tests that we can insert fields with values and defaults in the same query. */ public function testDefaultInsertWithFields() { - $query = db_insert('test') + $query = $this->connection->insert('test') ->fields(['name' => 'Bob']) ->useDefaults(['job']); $id = $query->execute(); diff --git a/core/tests/Drupal/KernelTests/Core/Database/InsertLobTest.php b/core/tests/Drupal/KernelTests/Core/Database/InsertLobTest.php index e7a3107a87..10984385e2 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/InsertLobTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/InsertLobTest.php @@ -15,7 +15,7 @@ class InsertLobTest extends DatabaseTestBase { public function testInsertOneBlob() { $data = "This is\000a test."; $this->assertTrue(strlen($data) === 15, 'Test data contains a NULL.'); - $id = db_insert('test_one_blob') + $id = $this->connection->insert('test_one_blob') ->fields(['blob1' => $data]) ->execute(); $r = db_query('SELECT * FROM {test_one_blob} WHERE id = :id', [':id' => $id])->fetchAssoc(); @@ -26,7 +26,7 @@ public function testInsertOneBlob() { * Tests that we can insert multiple blob fields in the same query. */ public function testInsertMultipleBlob() { - $id = db_insert('test_two_blobs') + $id = $this->connection->insert('test_two_blobs') ->fields([ 'blob1' => 'This is', 'blob2' => 'a test', diff --git a/core/tests/Drupal/KernelTests/Core/Database/InsertTest.php b/core/tests/Drupal/KernelTests/Core/Database/InsertTest.php index d6b0f8dd80..cbc5c9d936 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/InsertTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/InsertTest.php @@ -15,7 +15,7 @@ class InsertTest extends DatabaseTestBase { public function testSimpleInsert() { $num_records_before = db_query('SELECT COUNT(*) FROM {test}')->fetchField(); - $query = db_insert('test'); + $query = $this->connection->insert('test'); $query->fields([ 'name' => 'Yoko', 'age' => '29', @@ -37,7 +37,7 @@ public function testSimpleInsert() { public function testMultiInsert() { $num_records_before = (int) db_query('SELECT COUNT(*) FROM {test}')->fetchField(); - $query = db_insert('test'); + $query = $this->connection->insert('test'); $query->fields([ 'name' => 'Larry', 'age' => '30', @@ -76,7 +76,7 @@ public function testMultiInsert() { public function testRepeatedInsert() { $num_records_before = db_query('SELECT COUNT(*) FROM {test}')->fetchField(); - $query = db_insert('test'); + $query = $this->connection->insert('test'); $query->fields([ 'name' => 'Larry', @@ -119,7 +119,7 @@ public function testRepeatedInsert() { public function testInsertFieldOnlyDefinition() { // This is useful for importers, when we want to create a query and define // its fields once, then loop over a multi-insert execution. - db_insert('test') + $this->connection->insert('test') ->fields(['name', 'age']) ->values(['Larry', '30']) ->values(['Curly', '31']) @@ -137,7 +137,7 @@ public function testInsertFieldOnlyDefinition() { * Tests that inserts return the proper auto-increment ID. */ public function testInsertLastInsertID() { - $id = db_insert('test') + $id = $this->connection->insert('test') ->fields([ 'name' => 'Larry', 'age' => '30', @@ -165,7 +165,7 @@ public function testInsertSelectFields() { // SELECT tp.age AS age, tp.name AS name, tp.job AS job // FROM test_people tp // WHERE tp.name = 'Meredith' - db_insert('test') + $this->connection->insert('test') ->from($query) ->execute(); @@ -186,7 +186,7 @@ public function testInsertSelectAll() { // SELECT * // FROM test_people tp // WHERE tp.name = 'Meredith' - db_insert('test_people_copy') + $this->connection->insert('test_people_copy') ->from($query) ->execute(); diff --git a/core/tests/Drupal/KernelTests/Core/Database/InvalidDataTest.php b/core/tests/Drupal/KernelTests/Core/Database/InvalidDataTest.php index f6af91fef8..f0885143c8 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/InvalidDataTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/InvalidDataTest.php @@ -18,7 +18,7 @@ class InvalidDataTest extends DatabaseTestBase { public function testInsertDuplicateData() { // Try to insert multiple records where at least one has bad data. try { - db_insert('test') + $this->connection->insert('test') ->fields(['name', 'age', 'job']) ->values([ 'name' => 'Elvis', @@ -75,7 +75,7 @@ public function testInsertDuplicateDataFromSelect() { // Insert multiple records in 'test_people' where one has bad data // (duplicate key). A 'Meredith' record has already been inserted // in ::setUp. - db_insert('test_people') + $this->connection->insert('test_people') ->fields(['name', 'age', 'job']) ->values([ 'name' => 'Elvis', @@ -109,7 +109,7 @@ public function testInsertDuplicateDataFromSelect() { ->orderBy('name'); // Try inserting from the subselect. - db_insert('test') + $this->connection->insert('test') ->from($query) ->execute(); diff --git a/core/tests/Drupal/KernelTests/Core/Database/QueryTest.php b/core/tests/Drupal/KernelTests/Core/Database/QueryTest.php index 76ed2e2987..bfe60b6c0f 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/QueryTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/QueryTest.php @@ -100,7 +100,7 @@ public function testConditionOperatorArgumentsSQLInjection() { // Attempt SQLi via union query with no unsafe characters. $this->enableModules(['user']); $this->installEntitySchema('user'); - db_insert('test') + $this->connection->insert('test') ->fields(['name' => '123456']) ->execute(); $injection = "= 1 UNION ALL SELECT password FROM user WHERE uid ="; @@ -117,7 +117,7 @@ public function testConditionOperatorArgumentsSQLInjection() { } // Attempt SQLi via union query - uppercase tablename. - db_insert('TEST_UPPERCASE') + $this->connection->insert('TEST_UPPERCASE') ->fields(['name' => 'secrets']) ->execute(); $injection = "IS NOT NULL) UNION ALL SELECT name FROM {TEST_UPPERCASE} -- "; diff --git a/core/tests/Drupal/KernelTests/Core/Database/RegressionTest.php b/core/tests/Drupal/KernelTests/Core/Database/RegressionTest.php index ddb2aa41b6..c035024fab 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/RegressionTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/RegressionTest.php @@ -22,7 +22,8 @@ class RegressionTest extends DatabaseTestBase { public function testRegression_310447() { // That's a 255 character UTF-8 string. $job = str_repeat("é", 255); - db_insert('test') + $this->connection + ->insert('test') ->fields([ 'name' => $this->randomMachineName(), 'age' => 20, diff --git a/core/tests/Drupal/KernelTests/Core/Database/SelectSubqueryTest.php b/core/tests/Drupal/KernelTests/Core/Database/SelectSubqueryTest.php index 58a66289e2..c035d35ea3 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/SelectSubqueryTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/SelectSubqueryTest.php @@ -209,7 +209,7 @@ public function testJoinSubquerySelect() { */ public function testExistsSubquerySelect() { // Put George into {test_people}. - db_insert('test_people') + $this->connection->insert('test_people') ->fields([ 'name' => 'George', 'age' => 27, @@ -239,7 +239,7 @@ public function testExistsSubquerySelect() { */ public function testNotExistsSubquerySelect() { // Put George into {test_people}. - db_insert('test_people') + $this->connection->insert('test_people') ->fields([ 'name' => 'George', 'age' => 27, diff --git a/core/tests/Drupal/KernelTests/Core/Database/SelectTest.php b/core/tests/Drupal/KernelTests/Core/Database/SelectTest.php index 8751a18fe3..a0ac960e79 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/SelectTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/SelectTest.php @@ -402,7 +402,7 @@ public function testRandomOrder() { // after shuffling it (in other words, nearly impossible). $number_of_items = 52; while (db_query("SELECT MAX(id) FROM {test}")->fetchField() < $number_of_items) { - db_insert('test')->fields(['name' => $this->randomMachineName()])->execute(); + $this->connection->insert('test')->fields(['name' => $this->randomMachineName()])->execute(); } // First select the items in order and make sure we get an ordered list. diff --git a/core/tests/Drupal/KernelTests/Core/Database/TransactionTest.php b/core/tests/Drupal/KernelTests/Core/Database/TransactionTest.php index 0e13a8793d..0d07bdec1e 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/TransactionTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/TransactionTest.php @@ -55,7 +55,7 @@ protected function transactionOuterLayer($suffix, $rollback = FALSE, $ddl_statem $txn = $this->connection->startTransaction(); // Insert a single row into the testing table. - db_insert('test') + $this->connection->insert('test') ->fields([ 'name' => 'David' . $suffix, 'age' => '24', @@ -103,7 +103,7 @@ protected function transactionInnerLayer($suffix, $rollback = FALSE, $ddl_statem $this->assertTrue($depth < $depth2, 'Transaction depth is has increased with new transaction.'); // Insert a single row into the testing table. - db_insert('test') + $this->connection->insert('test') ->fields([ 'name' => 'Daniel' . $suffix, 'age' => '19', @@ -308,7 +308,7 @@ public function testTransactionWithDdlStatement() { * Inserts a single row into the testing table. */ protected function insertRow($name) { - db_insert('test') + $this->connection->insert('test') ->fields([ 'name' => $name, ]) diff --git a/core/tests/Drupal/KernelTests/Core/Database/UpdateLobTest.php b/core/tests/Drupal/KernelTests/Core/Database/UpdateLobTest.php index 4b06c7ceea..a5f4df538c 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/UpdateLobTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/UpdateLobTest.php @@ -15,7 +15,7 @@ class UpdateLobTest extends DatabaseTestBase { public function testUpdateOneBlob() { $data = "This is\000a test."; $this->assertTrue(strlen($data) === 15, 'Test data contains a NULL.'); - $id = db_insert('test_one_blob') + $id = $this->connection->insert('test_one_blob') ->fields(['blob1' => $data]) ->execute(); @@ -33,7 +33,7 @@ public function testUpdateOneBlob() { * Confirms that we can update two blob columns in the same table. */ public function testUpdateMultipleBlob() { - $id = db_insert('test_two_blobs') + $id = $this->connection->insert('test_two_blobs') ->fields([ 'blob1' => 'This is', 'blob2' => 'a test', diff --git a/core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php b/core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php index 16f63db8d8..d57cd97ac8 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/FieldSqlStorageTest.php @@ -123,7 +123,8 @@ public function testFieldLoad() { // Generate values and insert them directly in the storage tables. $values = []; - $query = db_insert($this->revisionTable)->fields($columns); + $connection = Database::getConnection(); + $query = $connection->insert($this->revisionTable)->fields($columns); foreach ($revision_ids as $revision_id) { // Put one value too many. for ($delta = 0; $delta <= $this->fieldCardinality; $delta++) { @@ -133,7 +134,7 @@ public function testFieldLoad() { } $query->execute(); } - $query = db_insert($this->table)->fields($columns); + $query = $connection->insert($this->table)->fields($columns); foreach ($values[$revision_id] as $delta => $value) { $query->values([$bundle, 0, $entity->id(), $revision_id, $delta, $entity->language()->getId(), $value]); } @@ -167,8 +168,8 @@ public function testFieldLoad() { // loaded. $unavailable_langcode = 'xx'; $values = [$bundle, 0, $entity->id(), $entity->getRevisionId(), 0, $unavailable_langcode, mt_rand(1, 127)]; - db_insert($this->table)->fields($columns)->values($values)->execute(); - db_insert($this->revisionTable)->fields($columns)->values($values)->execute(); + $connection->insert($this->table)->fields($columns)->values($values)->execute(); + $connection->insert($this->revisionTable)->fields($columns)->values($values)->execute(); $entity = $storage->load($entity->id()); $this->assertFalse(array_key_exists($unavailable_langcode, $entity->{$this->fieldName})); }