Commit b07fb6b0 authored by catch's avatar catch

Issue #1774104 by sun: Replace all trivial drupal_write_record() calls with db_merge().

parent dd4931b0
...@@ -161,9 +161,11 @@ function createFile($filepath = NULL, $contents = NULL, $scheme = NULL) { ...@@ -161,9 +161,11 @@ function createFile($filepath = NULL, $contents = NULL, $scheme = NULL) {
$file->status = 0; $file->status = 0;
// Write the record directly rather than using the API so we don't invoke // Write the record directly rather than using the API so we don't invoke
// the hooks. // the hooks.
$this->assertNotIdentical(drupal_write_record('file_managed', $file), FALSE, 'The file was added to the database.', 'Create test file'); $file = (array) $file;
$file['fid'] = db_insert('file_managed')
return entity_create('file', (array) $file); ->fields($file)
->execute();
return entity_create('file', $file);
} }
/** /**
......
...@@ -24,19 +24,19 @@ function setUp() { ...@@ -24,19 +24,19 @@ function setUp() {
// Create records for a couple of users with different sizes. // Create records for a couple of users with different sizes.
$file = array('uid' => 2, 'uri' => 'public://example1.txt', 'filesize' => 50, 'status' => FILE_STATUS_PERMANENT); $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); $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); $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); $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. // Now create some non-permanent files.
$file = array('uid' => 2, 'uri' => 'public://example5.txt', 'filesize' => 1, 'status' => 0); $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); $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();
} }
/** /**
......
...@@ -270,8 +270,8 @@ protected function setCurrentTranslations() { ...@@ -270,8 +270,8 @@ protected function setCurrentTranslations() {
'version' => '', 'version' => '',
); );
foreach ($data as $file) { foreach ($data as $file) {
$file = (object) array_merge($default, $file); $file = array_merge($default, $file);
drupal_write_record('locale_file', $file); db_insert('locale_file')->fields($file)->execute();
} }
} }
......
...@@ -866,22 +866,23 @@ function locale_translation_get_file_history() { ...@@ -866,22 +866,23 @@ function locale_translation_get_file_history() {
* *
* @return integer * @return integer
* FALSE on failure. Otherwise SAVED_NEW or SAVED_UPDATED. * FALSE on failure. Otherwise SAVED_NEW or SAVED_UPDATED.
*
* @see drupal_write_record()
*/ */
function locale_translation_update_file_history($file) { function locale_translation_update_file_history($file) {
// Update or write new record. $status = db_merge('locale_file')
if (db_query("SELECT project FROM {locale_file} WHERE project = :project AND langcode = :langcode", array(':project' => $file->project, ':langcode' => $file->langcode))->fetchField()) { ->key(array(
$update = array('project', 'langcode'); 'project' => $file->project,
} 'langcode' => $file->langcode,
else { ))
$update = array(); ->fields(array(
} 'version' => $file->version,
$result = drupal_write_record('locale_file', $file, $update); 'timestamp' => $file->timestamp,
'last_checked' => $file->last_checked,
))
->execute();
// The file history has changed, flush the static cache now. // The file history has changed, flush the static cache now.
// @todo Can we make this more fine grained? // @todo Can we make this more fine grained?
drupal_static_reset('locale_translation_get_file_history'); drupal_static_reset('locale_translation_get_file_history');
return $result; return $status;
} }
/** /**
......
...@@ -111,7 +111,7 @@ function hook_menu_link_insert(\Drupal\menu_link\Entity\MenuLink $menu_link) { ...@@ -111,7 +111,7 @@ function hook_menu_link_insert(\Drupal\menu_link\Entity\MenuLink $menu_link) {
$record['mlid'] = $menu_link->id(); $record['mlid'] = $menu_link->id();
$record['menu_name'] = $menu_link->menu_name; $record['menu_name'] = $menu_link->menu_name;
$record['status'] = 0; $record['status'] = 0;
drupal_write_record('menu_example', $record); db_insert('menu_example')->fields($record)->execute();
} }
/** /**
......
...@@ -144,7 +144,7 @@ function testNodeQueryAlterOverride() { ...@@ -144,7 +144,7 @@ function testNodeQueryAlterOverride() {
'grant_update' => 0, 'grant_update' => 0,
'grant_delete' => 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' // Test that the noAccessUser still doesn't have the 'view'
// privilege after adding the node_access record. // privilege after adding the node_access record.
......
...@@ -150,7 +150,7 @@ ...@@ -150,7 +150,7 @@
* 'grant_update' => 0, * 'grant_update' => 0,
* 'grant_delete' => 0, * 'grant_delete' => 0,
* ); * );
* drupal_write_record('node_access', $record); * db_insert('node_access')->fields($record)->execute();
* @endcode * @endcode
* And then in its hook_node_grants() implementation, it would need to return: * And then in its hook_node_grants() implementation, it would need to return:
* @code * @code
......
...@@ -381,12 +381,10 @@ function hook_entity_predelete(Drupal\Core\Entity\EntityInterface $entity) { ...@@ -381,12 +381,10 @@ function hook_entity_predelete(Drupal\Core\Entity\EntityInterface $entity) {
->fetchField(); ->fetchField();
// Log the count in a table that records this statistic for deleted entities. // Log the count in a table that records this statistic for deleted entities.
$ref_count_record = (object) array( db_merge('example_deleted_entity_statistics')
'count' => $count, ->key(array('type' => $type, 'id' => $id))
'type' => $type, ->fields(array('count' => $count))
'id' => $id, ->execute();
);
drupal_write_record('example_deleted_entity_statistics', $ref_count_record);
} }
/** /**
......
...@@ -31,22 +31,12 @@ public static function getInfo() { ...@@ -31,22 +31,12 @@ public static function getInfo() {
} }
/** /**
* Test that calls to drupal_write_record() work during module installation. * Verify that drupal_get_schema() can be used 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.
*/ */
public function testDrupalWriteRecord() { public function testGetSchemaAtInstallTime() {
// Check for data that was inserted using drupal_write_record() while the // @see module_test_install()
// 'module_test' module was being installed and enabled. $value = db_query("SELECT data FROM {module_test}")->fetchField();
$data = db_query("SELECT data FROM {module_test}")->fetchCol(); $this->assertIdentical($value, 'varchar');
$this->assertTrue(in_array('Data inserted in hook_install()', $data), 'Data inserted using drupal_write_record() in hook_install() is correctly saved.');
} }
/** /**
......
...@@ -28,6 +28,10 @@ function module_test_schema() { ...@@ -28,6 +28,10 @@ function module_test_schema() {
* Implements hook_install(). * Implements hook_install().
*/ */
function module_test_install() { function module_test_install() {
$record = array('data' => 'Data inserted in hook_install()'); $schema = drupal_get_schema('module_test');
drupal_write_record('module_test', $record); db_insert('module_test')
->fields(array(
'data' => $schema['fields']['data']['type'],
))
->execute();
} }
...@@ -64,7 +64,7 @@ public function testTimeCaching() { ...@@ -64,7 +64,7 @@ public function testTimeCaching() {
'age' => 29, 'age' => 29,
'job' => 'Banjo', '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. // The Result should be the same as before, because of the caching.
$view = views_get_view('test_cache'); $view = views_get_view('test_cache');
...@@ -106,8 +106,7 @@ function testNoneCaching() { ...@@ -106,8 +106,7 @@ function testNoneCaching() {
'age' => 29, 'age' => 29,
'job' => 'Banjo', 'job' => 'Banjo',
); );
db_insert('views_test_data')->fields($record)->execute();
drupal_write_record('views_test_data', $record);
// The Result changes, because the view is not cached. // The Result changes, because the view is not cached.
$view = views_get_view('test_cache'); $view = views_get_view('test_cache');
......
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