3 namespace Drupal\Core\KeyValueStore;
5 use Drupal\Component\Serialization\SerializationInterface;
6 use Drupal\Core\Database\Query\Merge;
7 use Drupal\Core\Database\Connection;
8 use Drupal\Core\DependencyInjection\DependencySerializationTrait;
11 * Defines a default key/value store implementation.
13 * This is Drupal's default key/value store implementation. It uses the database
14 * to store key/value data.
16 class DatabaseStorage extends StorageBase {
18 use DependencySerializationTrait;
21 * The serialization class to use.
23 * @var \Drupal\Component\Serialization\SerializationInterface
25 protected $serializer;
28 * The database connection.
30 * @var \Drupal\Core\Database\Connection
32 protected $connection;
35 * The name of the SQL table to use.
42 * Overrides Drupal\Core\KeyValueStore\StorageBase::__construct().
44 * @param string $collection
45 * The name of the collection holding key and value pairs.
46 * @param \Drupal\Component\Serialization\SerializationInterface $serializer
47 * The serialization class to use.
48 * @param \Drupal\Core\Database\Connection $connection
49 * The database connection to use.
50 * @param string $table
51 * The name of the SQL table to use, defaults to key_value.
53 public function __construct($collection, SerializationInterface $serializer, Connection $connection, $table = 'key_value') {
54 parent::__construct($collection);
55 $this->serializer = $serializer;
56 $this->connection = $connection;
57 $this->table = $table;
63 public function has($key) {
64 return (bool) $this->connection->query('SELECT 1 FROM {' . $this->connection->escapeTable($this->table) . '} WHERE collection = :collection AND name = :key', [
65 ':collection' => $this->collection,
73 public function getMultiple(array $keys) {
76 $result = $this->connection->query('SELECT name, value FROM {' . $this->connection->escapeTable($this->table) . '} WHERE name IN ( :keys[] ) AND collection = :collection', [':keys[]' => $keys, ':collection' => $this->collection])->fetchAllAssoc('name');
77 foreach ($keys as $key) {
78 if (isset($result[$key])) {
79 $values[$key] = $this->serializer->decode($result[$key]->value);
83 catch (\Exception $e) {
84 // @todo: Perhaps if the database is never going to be available,
85 // key/value requests should return FALSE in order to allow exception
86 // handling to occur but for now, keep it an array, always.
94 public function getAll() {
95 $result = $this->connection->query('SELECT name, value FROM {' . $this->connection->escapeTable($this->table) . '} WHERE collection = :collection', [':collection' => $this->collection]);
98 foreach ($result as $item) {
100 $values[$item->name] = $this->serializer->decode($item->value);
109 public function set($key, $value) {
110 $this->connection->merge($this->table)
113 'collection' => $this->collection,
115 ->fields(['value' => $this->serializer->encode($value)])
122 public function setIfNotExists($key, $value) {
123 $result = $this->connection->merge($this->table)
125 'collection' => $this->collection,
127 'value' => $this->serializer->encode($value),
129 ->condition('collection', $this->collection)
130 ->condition('name', $key)
132 return $result == Merge::STATUS_INSERT;
138 public function rename($key, $new_key) {
139 $this->connection->update($this->table)
140 ->fields(['name' => $new_key])
141 ->condition('collection', $this->collection)
142 ->condition('name', $key)
149 public function deleteMultiple(array $keys) {
150 // Delete in chunks when a large array is passed.
152 $this->connection->delete($this->table)
153 ->condition('name', array_splice($keys, 0, 1000), 'IN')
154 ->condition('collection', $this->collection)
162 public function deleteAll() {
163 $this->connection->delete($this->table)
164 ->condition('collection', $this->collection)