3 namespace Drupal\Core\Config;
5 use Drupal\Core\Database\Database;
6 use Drupal\Core\Database\Connection;
7 use Drupal\Core\Database\SchemaObjectExistsException;
8 use Drupal\Core\DependencyInjection\DependencySerializationTrait;
11 * Defines the Database storage.
13 class DatabaseStorage implements StorageInterface {
14 use DependencySerializationTrait;
17 * The database connection.
19 * @var \Drupal\Core\Database\Connection
21 protected $connection;
24 * The database table name.
31 * Additional database connection options to use in queries.
35 protected $options = [];
38 * The storage collection.
42 protected $collection = StorageInterface::DEFAULT_COLLECTION;
45 * Constructs a new DatabaseStorage.
47 * @param \Drupal\Core\Database\Connection $connection
48 * A Database connection to use for reading and writing configuration data.
49 * @param string $table
50 * A database table name to store configuration data in.
51 * @param array $options
52 * (optional) Any additional database connection options to use in queries.
53 * @param string $collection
54 * (optional) The collection to store configuration in. Defaults to the
57 public function __construct(Connection $connection, $table, array $options = [], $collection = StorageInterface::DEFAULT_COLLECTION) {
58 $this->connection = $connection;
59 $this->table = $table;
60 $this->options = $options;
61 $this->collection = $collection;
67 public function exists($name) {
69 return (bool) $this->connection->queryRange('SELECT 1 FROM {' . $this->connection->escapeTable($this->table) . '} WHERE collection = :collection AND name = :name', 0, 1, [
70 ':collection' => $this->collection,
72 ], $this->options)->fetchField();
74 catch (\Exception $e) {
75 // If we attempt a read without actually having the database or the table
76 // available, just return FALSE so the caller can handle it.
84 public function read($name) {
87 $raw = $this->connection->query('SELECT data FROM {' . $this->connection->escapeTable($this->table) . '} WHERE collection = :collection AND name = :name', [':collection' => $this->collection, ':name' => $name], $this->options)->fetchField();
89 $data = $this->decode($raw);
92 catch (\Exception $e) {
93 // If we attempt a read without actually having the database or the table
94 // available, just return FALSE so the caller can handle it.
102 public function readMultiple(array $names) {
105 $list = $this->connection->query('SELECT name, data FROM {' . $this->connection->escapeTable($this->table) . '} WHERE collection = :collection AND name IN ( :names[] )', [':collection' => $this->collection, ':names[]' => $names], $this->options)->fetchAllKeyed();
106 foreach ($list as &$data) {
107 $data = $this->decode($data);
110 catch (\Exception $e) {
111 // If we attempt a read without actually having the database or the table
112 // available, just return an empty array so the caller can handle it.
120 public function write($name, array $data) {
121 $data = $this->encode($data);
123 return $this->doWrite($name, $data);
125 catch (\Exception $e) {
126 // If there was an exception, try to create the table.
127 if ($this->ensureTableExists()) {
128 return $this->doWrite($name, $data);
130 // Some other failure that we can not recover from.
136 * Helper method so we can re-try a write.
138 * @param string $name
140 * @param string $data
141 * The config data, already dumped to a string.
145 protected function doWrite($name, $data) {
146 $options = ['return' => Database::RETURN_AFFECTED] + $this->options;
147 return (bool) $this->connection->merge($this->table, $options)
148 ->keys(['collection', 'name'], [$this->collection, $name])
149 ->fields(['data' => $data])
154 * Check if the config table exists and create it if not.
157 * TRUE if the table was created, FALSE otherwise.
159 * @throws \Drupal\Core\Config\StorageException
160 * If a database error occurs.
162 protected function ensureTableExists() {
164 if (!$this->connection->schema()->tableExists($this->table)) {
165 $this->connection->schema()->createTable($this->table, static::schemaDefinition());
169 // If another process has already created the config table, attempting to
170 // recreate it will throw an exception. In this case just catch the
171 // exception and do nothing.
172 catch (SchemaObjectExistsException $e) {
175 catch (\Exception $e) {
176 throw new StorageException($e->getMessage(), NULL, $e);
182 * Defines the schema for the configuration table.
186 protected static function schemaDefinition() {
188 'description' => 'The base table for configuration data.',
191 'description' => 'Primary Key: Config object collection.',
192 'type' => 'varchar_ascii',
198 'description' => 'Primary Key: Config object name.',
199 'type' => 'varchar_ascii',
205 'description' => 'A serialized configuration object data.',
211 'primary key' => ['collection', 'name'],
217 * Implements Drupal\Core\Config\StorageInterface::delete().
219 * @throws PDOException
221 * @todo Ignore replica targets for data manipulation operations.
223 public function delete($name) {
224 $options = ['return' => Database::RETURN_AFFECTED] + $this->options;
225 return (bool) $this->connection->delete($this->table, $options)
226 ->condition('collection', $this->collection)
227 ->condition('name', $name)
233 * Implements Drupal\Core\Config\StorageInterface::rename().
235 * @throws PDOException
237 public function rename($name, $new_name) {
238 $options = ['return' => Database::RETURN_AFFECTED] + $this->options;
239 return (bool) $this->connection->update($this->table, $options)
240 ->fields(['name' => $new_name])
241 ->condition('name', $name)
242 ->condition('collection', $this->collection)
249 public function encode($data) {
250 return serialize($data);
254 * Implements Drupal\Core\Config\StorageInterface::decode().
256 * @throws ErrorException
257 * The unserialize() call will trigger E_NOTICE if the string cannot
260 public function decode($raw) {
261 $data = @unserialize($raw);
262 return is_array($data) ? $data : FALSE;
268 public function listAll($prefix = '') {
270 $query = $this->connection->select($this->table);
271 $query->fields($this->table, ['name']);
272 $query->condition('collection', $this->collection, '=');
273 $query->condition('name', $prefix . '%', 'LIKE');
274 $query->orderBy('collection')->orderBy('name');
275 return $query->execute()->fetchCol();
277 catch (\Exception $e) {
285 public function deleteAll($prefix = '') {
287 $options = ['return' => Database::RETURN_AFFECTED] + $this->options;
288 return (bool) $this->connection->delete($this->table, $options)
289 ->condition('name', $prefix . '%', 'LIKE')
290 ->condition('collection', $this->collection)
293 catch (\Exception $e) {
301 public function createCollection($collection) {
313 public function getCollectionName() {
314 return $this->collection;
320 public function getAllCollectionNames() {
322 return $this->connection->query('SELECT DISTINCT collection FROM {' . $this->connection->escapeTable($this->table) . '} WHERE collection <> :collection ORDER by collection', [
323 ':collection' => StorageInterface::DEFAULT_COLLECTION,
327 catch (\Exception $e) {