class DatabaseStorage in Zircon Profile 8
Same name in this branch
- 8 core/lib/Drupal/Core/Config/DatabaseStorage.php \Drupal\Core\Config\DatabaseStorage
- 8 core/lib/Drupal/Core/KeyValueStore/DatabaseStorage.php \Drupal\Core\KeyValueStore\DatabaseStorage
Same name and namespace in other branches
- 8.0 core/lib/Drupal/Core/KeyValueStore/DatabaseStorage.php \Drupal\Core\KeyValueStore\DatabaseStorage
Defines a default key/value store implementation.
This is Drupal's default key/value store implementation. It uses the database to store key/value data.
Hierarchy
- class \Drupal\Core\KeyValueStore\StorageBase implements KeyValueStoreInterface
- class \Drupal\Core\KeyValueStore\DatabaseStorage uses DependencySerializationTrait
Expanded class hierarchy of DatabaseStorage
File
- core/
lib/ Drupal/ Core/ KeyValueStore/ DatabaseStorage.php, line 21 - Contains \Drupal\Core\KeyValueStore\DatabaseStorage.
Namespace
Drupal\Core\KeyValueStoreView source
class DatabaseStorage extends StorageBase {
use DependencySerializationTrait;
/**
* The serialization class to use.
*
* @var \Drupal\Component\Serialization\SerializationInterface
*/
protected $serializer;
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;
/**
* The name of the SQL table to use.
*
* @var string
*/
protected $table;
/**
* Overrides Drupal\Core\KeyValueStore\StorageBase::__construct().
*
* @param string $collection
* The name of the collection holding key and value pairs.
* @param \Drupal\Component\Serialization\SerializationInterface $serializer
* The serialization class to use.
* @param \Drupal\Core\Database\Connection $connection
* The database connection to use.
* @param string $table
* The name of the SQL table to use, defaults to key_value.
*/
public function __construct($collection, SerializationInterface $serializer, Connection $connection, $table = 'key_value') {
parent::__construct($collection);
$this->serializer = $serializer;
$this->connection = $connection;
$this->table = $table;
}
/**
* {@inheritdoc}
*/
public function has($key) {
return (bool) $this->connection
->query('SELECT 1 FROM {' . $this->connection
->escapeTable($this->table) . '} WHERE collection = :collection AND name = :key', array(
':collection' => $this->collection,
':key' => $key,
))
->fetchField();
}
/**
* {@inheritdoc}
*/
public function getMultiple(array $keys) {
$values = array();
try {
$result = $this->connection
->query('SELECT name, value FROM {' . $this->connection
->escapeTable($this->table) . '} WHERE name IN ( :keys[] ) AND collection = :collection', array(
':keys[]' => $keys,
':collection' => $this->collection,
))
->fetchAllAssoc('name');
foreach ($keys as $key) {
if (isset($result[$key])) {
$values[$key] = $this->serializer
->decode($result[$key]->value);
}
}
} catch (\Exception $e) {
// @todo: Perhaps if the database is never going to be available,
// key/value requests should return FALSE in order to allow exception
// handling to occur but for now, keep it an array, always.
}
return $values;
}
/**
* {@inheritdoc}
*/
public function getAll() {
$result = $this->connection
->query('SELECT name, value FROM {' . $this->connection
->escapeTable($this->table) . '} WHERE collection = :collection', array(
':collection' => $this->collection,
));
$values = array();
foreach ($result as $item) {
if ($item) {
$values[$item->name] = $this->serializer
->decode($item->value);
}
}
return $values;
}
/**
* {@inheritdoc}
*/
public function set($key, $value) {
$this->connection
->merge($this->table)
->keys(array(
'name' => $key,
'collection' => $this->collection,
))
->fields(array(
'value' => $this->serializer
->encode($value),
))
->execute();
}
/**
* {@inheritdoc}
*/
public function setIfNotExists($key, $value) {
$result = $this->connection
->merge($this->table)
->insertFields(array(
'collection' => $this->collection,
'name' => $key,
'value' => $this->serializer
->encode($value),
))
->condition('collection', $this->collection)
->condition('name', $key)
->execute();
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();
}
/**
* {@inheritdoc}
*/
public function deleteMultiple(array $keys) {
// Delete in chunks when a large array is passed.
while ($keys) {
$this->connection
->delete($this->table)
->condition('name', array_splice($keys, 0, 1000), 'IN')
->condition('collection', $this->collection)
->execute();
}
}
/**
* {@inheritdoc}
*/
public function deleteAll() {
$this->connection
->delete($this->table)
->condition('collection', $this->collection)
->execute();
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DatabaseStorage:: |
protected | property | The database connection. | |
DatabaseStorage:: |
protected | property | The serialization class to use. | |
DatabaseStorage:: |
protected | property | The name of the SQL table to use. | |
DatabaseStorage:: |
public | function |
Deletes all items from the key/value store. Overrides KeyValueStoreInterface:: |
|
DatabaseStorage:: |
public | function |
Deletes multiple items from the key/value store. Overrides KeyValueStoreInterface:: |
1 |
DatabaseStorage:: |
public | function |
Returns all stored key/value pairs in the collection. Overrides KeyValueStoreInterface:: |
1 |
DatabaseStorage:: |
public | function |
Returns the stored key/value pairs for a given set of keys. Overrides KeyValueStoreInterface:: |
1 |
DatabaseStorage:: |
public | function |
Returns whether a given key exists in the store. Overrides KeyValueStoreInterface:: |
1 |
DatabaseStorage:: |
public | function |
Renames a key. Overrides KeyValueStoreInterface:: |
|
DatabaseStorage:: |
public | function |
Saves a value for a given key. Overrides KeyValueStoreInterface:: |
|
DatabaseStorage:: |
public | function |
Saves a value for a given key if it does not exist yet. Overrides KeyValueStoreInterface:: |
|
DatabaseStorage:: |
public | function |
Overrides Drupal\Core\KeyValueStore\StorageBase::__construct(). Overrides StorageBase:: |
1 |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
StorageBase:: |
protected | property | The name of the collection holding key and value pairs. | |
StorageBase:: |
public | function |
Deletes an item from the key/value store. Overrides KeyValueStoreInterface:: |
1 |
StorageBase:: |
public | function |
Returns the stored value for a given key. Overrides KeyValueStoreInterface:: |
1 |
StorageBase:: |
public | function |
Returns the name of this collection. Overrides KeyValueStoreInterface:: |
|
StorageBase:: |
public | function |
Saves key/value pairs. Overrides KeyValueStoreInterface:: |
1 |