Issue #2216579 by sun: Add a KeyValueStoreInterface::rename() method.

8.0.x
webchick 2014-03-25 14:47:00 -07:00
parent 0935e4c9ab
commit add1e1ae2d
5 changed files with 49 additions and 0 deletions

View File

@ -121,6 +121,17 @@ class DatabaseStorage extends StorageBase {
return $result == Merge::STATUS_INSERT;
}
/**
* {@inheritdoc}
*/
public function rename($key, $new_key) {
$this->connection->update($this->table)
->fields(array('name' => $new_key))
->condition('collection', $this->collection)
->condition('name', $key)
->execute();
}
/**
* Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::deleteMultiple().
*/

View File

@ -96,6 +96,16 @@ interface KeyValueStoreInterface {
*/
public function setMultiple(array $data);
/**
* Renames a key.
*
* @param string $key
* The key to rename.
* @param string $new_key
* The new key name.
*/
public function rename($key, $new_key);
/**
* Deletes an item from the key/value store.
*

View File

@ -72,6 +72,14 @@ class MemoryStorage extends StorageBase {
$this->data = $data + $this->data;
}
/**
* {@inheritdoc}
*/
public function rename($key, $new_key) {
$this->data[$new_key] = $this->data[$key];
unset($this->data[$key]);
}
/**
* Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::delete().
*/

View File

@ -76,6 +76,12 @@ class NullStorageExpirable implements KeyValueStoreExpirableInterface {
*/
public function setMultiple(array $data) { }
/**
* {@inheritdoc}
*/
public function rename($key, $new_key) {
}
/**
* Implements Drupal\Core\KeyValueStore\KeyValueStoreInterface::delete().
*/

View File

@ -205,6 +205,20 @@ abstract class StorageTestBase extends UnitTestBase {
$this->assertFalse($stores[1]->get($key));
}
/**
* Tests the rename operation.
*/
public function testRename() {
$stores = $this->createStorage();
$store = $stores[0];
$store->set('old', 'thing');
$this->assertIdentical($store->get('old'), 'thing');
$store->rename('old', 'new');
$this->assertIdentical($store->get('new'), 'thing');
$this->assertNull($store->get('old'));
}
/**
* Creates storage objects for each collection defined for this class.
*