View source
<?php
namespace Drupal\Core\KeyValueStore;
use Drupal\Component\Serialization\SerializationInterface;
use Drupal\Core\Database\Query\Merge;
use Drupal\Core\Database\Connection;
use Drupal\Core\Database\DatabaseException;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
class DatabaseStorage extends StorageBase {
use DependencySerializationTrait;
protected $serializer;
protected $connection;
protected $table;
public function __construct($collection, SerializationInterface $serializer, Connection $connection, $table = 'key_value') {
parent::__construct($collection);
$this->serializer = $serializer;
$this->connection = $connection;
$this->table = $table;
}
public function has($key) {
try {
return (bool) $this->connection
->query('SELECT 1 FROM {' . $this->connection
->escapeTable($this->table) . '} WHERE [collection] = :collection AND [name] = :key', [
':collection' => $this->collection,
':key' => $key,
])
->fetchField();
} catch (\Exception $e) {
$this
->catchException($e);
return FALSE;
}
}
public function getMultiple(array $keys) {
$values = [];
try {
$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');
foreach ($keys as $key) {
if (isset($result[$key])) {
$values[$key] = $this->serializer
->decode($result[$key]->value);
}
}
} catch (\Exception $e) {
}
return $values;
}
public function getAll() {
try {
$result = $this->connection
->query('SELECT [name], [value] FROM {' . $this->connection
->escapeTable($this->table) . '} WHERE [collection] = :collection', [
':collection' => $this->collection,
]);
} catch (\Exception $e) {
$this
->catchException($e);
$result = [];
}
$values = [];
foreach ($result as $item) {
if ($item) {
$values[$item->name] = $this->serializer
->decode($item->value);
}
}
return $values;
}
protected function doSet($key, $value) {
$this->connection
->merge($this->table)
->keys([
'name' => $key,
'collection' => $this->collection,
])
->fields([
'value' => $this->serializer
->encode($value),
])
->execute();
}
public function set($key, $value) {
try {
$this
->doSet($key, $value);
} catch (\Exception $e) {
if ($this
->ensureTableExists()) {
$this
->doSet($key, $value);
}
else {
throw $e;
}
}
}
public function doSetIfNotExists($key, $value) {
$result = $this->connection
->merge($this->table)
->insertFields([
'collection' => $this->collection,
'name' => $key,
'value' => $this->serializer
->encode($value),
])
->condition('collection', $this->collection)
->condition('name', $key)
->execute();
return $result == Merge::STATUS_INSERT;
}
public function setIfNotExists($key, $value) {
try {
return $this
->doSetIfNotExists($key, $value);
} catch (\Exception $e) {
if ($this
->ensureTableExists()) {
return $this
->doSetIfNotExists($key, $value);
}
else {
throw $e;
}
}
}
public function rename($key, $new_key) {
try {
$this->connection
->update($this->table)
->fields([
'name' => $new_key,
])
->condition('collection', $this->collection)
->condition('name', $key)
->execute();
} catch (\Exception $e) {
$this
->catchException($e);
}
}
public function deleteMultiple(array $keys) {
while ($keys) {
try {
$this->connection
->delete($this->table)
->condition('name', array_splice($keys, 0, 1000), 'IN')
->condition('collection', $this->collection)
->execute();
} catch (\Exception $e) {
$this
->catchException($e);
}
}
}
public function deleteAll() {
try {
$this->connection
->delete($this->table)
->condition('collection', $this->collection)
->execute();
} catch (\Exception $e) {
$this
->catchException($e);
}
}
protected function ensureTableExists() {
try {
$database_schema = $this->connection
->schema();
$database_schema
->createTable($this->table, $this
->schemaDefinition());
} catch (DatabaseException $e) {
} catch (\Exception $e) {
return FALSE;
}
return TRUE;
}
protected function catchException(\Exception $e) {
if (!$e instanceof DatabaseException && $this->connection
->schema()
->tableExists($this->table)) {
throw $e;
}
}
public static function schemaDefinition() {
return [
'description' => 'Generic key-value storage table. See the state system for an example.',
'fields' => [
'collection' => [
'description' => 'A named collection of key and value pairs.',
'type' => 'varchar_ascii',
'length' => 128,
'not null' => TRUE,
'default' => '',
],
'name' => [
'description' => 'The key of the key-value pair. As KEY is a SQL reserved keyword, name was chosen instead.',
'type' => 'varchar_ascii',
'length' => 128,
'not null' => TRUE,
'default' => '',
],
'value' => [
'description' => 'The value.',
'type' => 'blob',
'not null' => TRUE,
'size' => 'big',
],
],
'primary key' => [
'collection',
'name',
],
];
}
}