From b07fb6b076d202da6629e21ef3cdde6bbf7c587b Mon Sep 17 00:00:00 2001 From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org> Date: Mon, 10 Mar 2014 11:17:13 +0000 Subject: [PATCH] Issue #1774104 by sun: Replace all trivial drupal_write_record() calls with db_merge(). --- .../Drupal/file/Tests/FileManagedTestBase.php | 8 ++++--- .../lib/Drupal/file/Tests/SpaceUsedTest.php | 12 +++++----- .../Drupal/locale/Tests/LocaleUpdateBase.php | 4 ++-- core/modules/locale/locale.module | 23 ++++++++++--------- core/modules/menu_link/menu_link.api.php | 2 +- .../Drupal/node/Tests/NodeQueryAlterTest.php | 2 +- core/modules/node/node.api.php | 2 +- core/modules/system/entity.api.php | 10 ++++---- .../system/Tests/Module/InstallTest.php | 20 ++++------------ .../modules/module_test/module_test.install | 8 +++++-- .../Drupal/views/Tests/Plugin/CacheTest.php | 5 ++-- 11 files changed, 45 insertions(+), 51 deletions(-) diff --git a/core/modules/file/lib/Drupal/file/Tests/FileManagedTestBase.php b/core/modules/file/lib/Drupal/file/Tests/FileManagedTestBase.php index b4c70c324900..c77769133909 100644 --- a/core/modules/file/lib/Drupal/file/Tests/FileManagedTestBase.php +++ b/core/modules/file/lib/Drupal/file/Tests/FileManagedTestBase.php @@ -161,9 +161,11 @@ function createFile($filepath = NULL, $contents = NULL, $scheme = NULL) { $file->status = 0; // Write the record directly rather than using the API so we don't invoke // the hooks. - $this->assertNotIdentical(drupal_write_record('file_managed', $file), FALSE, 'The file was added to the database.', 'Create test file'); - - return entity_create('file', (array) $file); + $file = (array) $file; + $file['fid'] = db_insert('file_managed') + ->fields($file) + ->execute(); + return entity_create('file', $file); } /** diff --git a/core/modules/file/lib/Drupal/file/Tests/SpaceUsedTest.php b/core/modules/file/lib/Drupal/file/Tests/SpaceUsedTest.php index 49e57f8b721f..325990ed40f3 100644 --- a/core/modules/file/lib/Drupal/file/Tests/SpaceUsedTest.php +++ b/core/modules/file/lib/Drupal/file/Tests/SpaceUsedTest.php @@ -24,19 +24,19 @@ function setUp() { // Create records for a couple of users with different sizes. $file = array('uid' => 2, 'uri' => 'public://example1.txt', 'filesize' => 50, 'status' => FILE_STATUS_PERMANENT); - drupal_write_record('file_managed', $file); + db_insert('file_managed')->fields($file)->execute(); $file = array('uid' => 2, 'uri' => 'public://example2.txt', 'filesize' => 20, 'status' => FILE_STATUS_PERMANENT); - drupal_write_record('file_managed', $file); + db_insert('file_managed')->fields($file)->execute(); $file = array('uid' => 3, 'uri' => 'public://example3.txt', 'filesize' => 100, 'status' => FILE_STATUS_PERMANENT); - drupal_write_record('file_managed', $file); + db_insert('file_managed')->fields($file)->execute(); $file = array('uid' => 3, 'uri' => 'public://example4.txt', 'filesize' => 200, 'status' => FILE_STATUS_PERMANENT); - drupal_write_record('file_managed', $file); + db_insert('file_managed')->fields($file)->execute(); // Now create some non-permanent files. $file = array('uid' => 2, 'uri' => 'public://example5.txt', 'filesize' => 1, 'status' => 0); - drupal_write_record('file_managed', $file); + db_insert('file_managed')->fields($file)->execute(); $file = array('uid' => 3, 'uri' => 'public://example6.txt', 'filesize' => 3, 'status' => 0); - drupal_write_record('file_managed', $file); + db_insert('file_managed')->fields($file)->execute(); } /** diff --git a/core/modules/locale/lib/Drupal/locale/Tests/LocaleUpdateBase.php b/core/modules/locale/lib/Drupal/locale/Tests/LocaleUpdateBase.php index 8ae5a8ed18a3..2e29d98f51f6 100644 --- a/core/modules/locale/lib/Drupal/locale/Tests/LocaleUpdateBase.php +++ b/core/modules/locale/lib/Drupal/locale/Tests/LocaleUpdateBase.php @@ -270,8 +270,8 @@ protected function setCurrentTranslations() { 'version' => '', ); foreach ($data as $file) { - $file = (object) array_merge($default, $file); - drupal_write_record('locale_file', $file); + $file = array_merge($default, $file); + db_insert('locale_file')->fields($file)->execute(); } } diff --git a/core/modules/locale/locale.module b/core/modules/locale/locale.module index 316013554d26..324be33e7dc0 100644 --- a/core/modules/locale/locale.module +++ b/core/modules/locale/locale.module @@ -866,22 +866,23 @@ function locale_translation_get_file_history() { * * @return integer * FALSE on failure. Otherwise SAVED_NEW or SAVED_UPDATED. - * - * @see drupal_write_record() */ function locale_translation_update_file_history($file) { - // Update or write new record. - if (db_query("SELECT project FROM {locale_file} WHERE project = :project AND langcode = :langcode", array(':project' => $file->project, ':langcode' => $file->langcode))->fetchField()) { - $update = array('project', 'langcode'); - } - else { - $update = array(); - } - $result = drupal_write_record('locale_file', $file, $update); + $status = db_merge('locale_file') + ->key(array( + 'project' => $file->project, + 'langcode' => $file->langcode, + )) + ->fields(array( + 'version' => $file->version, + 'timestamp' => $file->timestamp, + 'last_checked' => $file->last_checked, + )) + ->execute(); // The file history has changed, flush the static cache now. // @todo Can we make this more fine grained? drupal_static_reset('locale_translation_get_file_history'); - return $result; + return $status; } /** diff --git a/core/modules/menu_link/menu_link.api.php b/core/modules/menu_link/menu_link.api.php index 2541eb54086d..cf8ee6dfa385 100644 --- a/core/modules/menu_link/menu_link.api.php +++ b/core/modules/menu_link/menu_link.api.php @@ -111,7 +111,7 @@ function hook_menu_link_insert(\Drupal\menu_link\Entity\MenuLink $menu_link) { $record['mlid'] = $menu_link->id(); $record['menu_name'] = $menu_link->menu_name; $record['status'] = 0; - drupal_write_record('menu_example', $record); + db_insert('menu_example')->fields($record)->execute(); } /** diff --git a/core/modules/node/lib/Drupal/node/Tests/NodeQueryAlterTest.php b/core/modules/node/lib/Drupal/node/Tests/NodeQueryAlterTest.php index 6a6bce7b0249..ad80d73f56d1 100644 --- a/core/modules/node/lib/Drupal/node/Tests/NodeQueryAlterTest.php +++ b/core/modules/node/lib/Drupal/node/Tests/NodeQueryAlterTest.php @@ -144,7 +144,7 @@ function testNodeQueryAlterOverride() { 'grant_update' => 0, 'grant_delete' => 0, ); - drupal_write_record('node_access', $record); + db_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/node/node.api.php b/core/modules/node/node.api.php index 20f62576b158..844e1a3decca 100644 --- a/core/modules/node/node.api.php +++ b/core/modules/node/node.api.php @@ -150,7 +150,7 @@ * 'grant_update' => 0, * 'grant_delete' => 0, * ); - * drupal_write_record('node_access', $record); + * db_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/system/entity.api.php b/core/modules/system/entity.api.php index aefb73b95277..c0203bdf7be7 100644 --- a/core/modules/system/entity.api.php +++ b/core/modules/system/entity.api.php @@ -381,12 +381,10 @@ function hook_entity_predelete(Drupal\Core\Entity\EntityInterface $entity) { ->fetchField(); // Log the count in a table that records this statistic for deleted entities. - $ref_count_record = (object) array( - 'count' => $count, - 'type' => $type, - 'id' => $id, - ); - drupal_write_record('example_deleted_entity_statistics', $ref_count_record); + db_merge('example_deleted_entity_statistics') + ->key(array('type' => $type, 'id' => $id)) + ->fields(array('count' => $count)) + ->execute(); } /** diff --git a/core/modules/system/lib/Drupal/system/Tests/Module/InstallTest.php b/core/modules/system/lib/Drupal/system/Tests/Module/InstallTest.php index c668fe95b6c0..eaa2fc912ee0 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Module/InstallTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Module/InstallTest.php @@ -31,22 +31,12 @@ public static function getInfo() { } /** - * Test that calls to drupal_write_record() work during module installation. - * - * This is a useful function to test because modules often use it to insert - * initial data in their database tables when they are being installed or - * enabled. Furthermore, drupal_write_record() relies on the module schema - * information being available, so this also checks that the data from one of - * the module's hook implementations, in particular hook_schema(), is - * properly available during this time. Therefore, this test helps ensure - * that modules are fully functional while Drupal is installing and enabling - * them. + * Verify that drupal_get_schema() can be used during module installation. */ - public function testDrupalWriteRecord() { - // Check for data that was inserted using drupal_write_record() while the - // 'module_test' module was being installed and enabled. - $data = db_query("SELECT data FROM {module_test}")->fetchCol(); - $this->assertTrue(in_array('Data inserted in hook_install()', $data), 'Data inserted using drupal_write_record() in hook_install() is correctly saved.'); + public function testGetSchemaAtInstallTime() { + // @see module_test_install() + $value = db_query("SELECT data FROM {module_test}")->fetchField(); + $this->assertIdentical($value, 'varchar'); } /** 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 f3b274a7d551..53dfa33d57a5 100644 --- a/core/modules/system/tests/modules/module_test/module_test.install +++ b/core/modules/system/tests/modules/module_test/module_test.install @@ -28,6 +28,10 @@ function module_test_schema() { * Implements hook_install(). */ function module_test_install() { - $record = array('data' => 'Data inserted in hook_install()'); - drupal_write_record('module_test', $record); + $schema = drupal_get_schema('module_test'); + db_insert('module_test') + ->fields(array( + 'data' => $schema['fields']['data']['type'], + )) + ->execute(); } diff --git a/core/modules/views/lib/Drupal/views/Tests/Plugin/CacheTest.php b/core/modules/views/lib/Drupal/views/Tests/Plugin/CacheTest.php index dca282f237ac..2fef5115cda9 100644 --- a/core/modules/views/lib/Drupal/views/Tests/Plugin/CacheTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/Plugin/CacheTest.php @@ -64,7 +64,7 @@ public function testTimeCaching() { 'age' => 29, 'job' => 'Banjo', ); - drupal_write_record('views_test_data', $record); + db_insert('views_test_data')->fields($record)->execute(); // The Result should be the same as before, because of the caching. $view = views_get_view('test_cache'); @@ -106,8 +106,7 @@ function testNoneCaching() { 'age' => 29, 'job' => 'Banjo', ); - - drupal_write_record('views_test_data', $record); + db_insert('views_test_data')->fields($record)->execute(); // The Result changes, because the view is not cached. $view = views_get_view('test_cache'); -- GitLab