DatabaseStorage.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. namespace Drupal\Core\Config;
  3. use Drupal\Core\Database\Database;
  4. use Drupal\Core\Database\Connection;
  5. use Drupal\Core\Database\SchemaObjectExistsException;
  6. use Drupal\Core\DependencyInjection\DependencySerializationTrait;
  7. /**
  8. * Defines the Database storage.
  9. */
  10. class DatabaseStorage implements StorageInterface {
  11. use DependencySerializationTrait;
  12. /**
  13. * The database connection.
  14. *
  15. * @var \Drupal\Core\Database\Connection
  16. */
  17. protected $connection;
  18. /**
  19. * The database table name.
  20. *
  21. * @var string
  22. */
  23. protected $table;
  24. /**
  25. * Additional database connection options to use in queries.
  26. *
  27. * @var array
  28. */
  29. protected $options = [];
  30. /**
  31. * The storage collection.
  32. *
  33. * @var string
  34. */
  35. protected $collection = StorageInterface::DEFAULT_COLLECTION;
  36. /**
  37. * Constructs a new DatabaseStorage.
  38. *
  39. * @param \Drupal\Core\Database\Connection $connection
  40. * A Database connection to use for reading and writing configuration data.
  41. * @param string $table
  42. * A database table name to store configuration data in.
  43. * @param array $options
  44. * (optional) Any additional database connection options to use in queries.
  45. * @param string $collection
  46. * (optional) The collection to store configuration in. Defaults to the
  47. * default collection.
  48. */
  49. public function __construct(Connection $connection, $table, array $options = [], $collection = StorageInterface::DEFAULT_COLLECTION) {
  50. $this->connection = $connection;
  51. $this->table = $table;
  52. $this->options = $options;
  53. $this->collection = $collection;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function exists($name) {
  59. try {
  60. return (bool) $this->connection->queryRange('SELECT 1 FROM {' . $this->connection->escapeTable($this->table) . '} WHERE collection = :collection AND name = :name', 0, 1, [
  61. ':collection' => $this->collection,
  62. ':name' => $name,
  63. ], $this->options)->fetchField();
  64. }
  65. catch (\Exception $e) {
  66. // If we attempt a read without actually having the database or the table
  67. // available, just return FALSE so the caller can handle it.
  68. return FALSE;
  69. }
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function read($name) {
  75. $data = FALSE;
  76. try {
  77. $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();
  78. if ($raw !== FALSE) {
  79. $data = $this->decode($raw);
  80. }
  81. }
  82. catch (\Exception $e) {
  83. // If we attempt a read without actually having the database or the table
  84. // available, just return FALSE so the caller can handle it.
  85. }
  86. return $data;
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function readMultiple(array $names) {
  92. $list = [];
  93. try {
  94. $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();
  95. foreach ($list as &$data) {
  96. $data = $this->decode($data);
  97. }
  98. }
  99. catch (\Exception $e) {
  100. // If we attempt a read without actually having the database or the table
  101. // available, just return an empty array so the caller can handle it.
  102. }
  103. return $list;
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function write($name, array $data) {
  109. $data = $this->encode($data);
  110. try {
  111. return $this->doWrite($name, $data);
  112. }
  113. catch (\Exception $e) {
  114. // If there was an exception, try to create the table.
  115. if ($this->ensureTableExists()) {
  116. return $this->doWrite($name, $data);
  117. }
  118. // Some other failure that we can not recover from.
  119. throw $e;
  120. }
  121. }
  122. /**
  123. * Helper method so we can re-try a write.
  124. *
  125. * @param string $name
  126. * The config name.
  127. * @param string $data
  128. * The config data, already dumped to a string.
  129. *
  130. * @return bool
  131. */
  132. protected function doWrite($name, $data) {
  133. $options = ['return' => Database::RETURN_AFFECTED] + $this->options;
  134. return (bool) $this->connection->merge($this->table, $options)
  135. ->keys(['collection', 'name'], [$this->collection, $name])
  136. ->fields(['data' => $data])
  137. ->execute();
  138. }
  139. /**
  140. * Check if the config table exists and create it if not.
  141. *
  142. * @return bool
  143. * TRUE if the table was created, FALSE otherwise.
  144. *
  145. * @throws \Drupal\Core\Config\StorageException
  146. * If a database error occurs.
  147. */
  148. protected function ensureTableExists() {
  149. try {
  150. if (!$this->connection->schema()->tableExists($this->table)) {
  151. $this->connection->schema()->createTable($this->table, static::schemaDefinition());
  152. return TRUE;
  153. }
  154. }
  155. // If another process has already created the config table, attempting to
  156. // recreate it will throw an exception. In this case just catch the
  157. // exception and do nothing.
  158. catch (SchemaObjectExistsException $e) {
  159. return TRUE;
  160. }
  161. catch (\Exception $e) {
  162. throw new StorageException($e->getMessage(), NULL, $e);
  163. }
  164. return FALSE;
  165. }
  166. /**
  167. * Defines the schema for the configuration table.
  168. *
  169. * @internal
  170. */
  171. protected static function schemaDefinition() {
  172. $schema = [
  173. 'description' => 'The base table for configuration data.',
  174. 'fields' => [
  175. 'collection' => [
  176. 'description' => 'Primary Key: Config object collection.',
  177. 'type' => 'varchar_ascii',
  178. 'length' => 255,
  179. 'not null' => TRUE,
  180. 'default' => '',
  181. ],
  182. 'name' => [
  183. 'description' => 'Primary Key: Config object name.',
  184. 'type' => 'varchar_ascii',
  185. 'length' => 255,
  186. 'not null' => TRUE,
  187. 'default' => '',
  188. ],
  189. 'data' => [
  190. 'description' => 'A serialized configuration object data.',
  191. 'type' => 'blob',
  192. 'not null' => FALSE,
  193. 'size' => 'big',
  194. ],
  195. ],
  196. 'primary key' => ['collection', 'name'],
  197. ];
  198. return $schema;
  199. }
  200. /**
  201. * Implements Drupal\Core\Config\StorageInterface::delete().
  202. *
  203. * @throws PDOException
  204. *
  205. * @todo Ignore replica targets for data manipulation operations.
  206. */
  207. public function delete($name) {
  208. $options = ['return' => Database::RETURN_AFFECTED] + $this->options;
  209. return (bool) $this->connection->delete($this->table, $options)
  210. ->condition('collection', $this->collection)
  211. ->condition('name', $name)
  212. ->execute();
  213. }
  214. /**
  215. * Implements Drupal\Core\Config\StorageInterface::rename().
  216. *
  217. * @throws PDOException
  218. */
  219. public function rename($name, $new_name) {
  220. $options = ['return' => Database::RETURN_AFFECTED] + $this->options;
  221. return (bool) $this->connection->update($this->table, $options)
  222. ->fields(['name' => $new_name])
  223. ->condition('name', $name)
  224. ->condition('collection', $this->collection)
  225. ->execute();
  226. }
  227. /**
  228. * {@inheritdoc}
  229. */
  230. public function encode($data) {
  231. return serialize($data);
  232. }
  233. /**
  234. * Implements Drupal\Core\Config\StorageInterface::decode().
  235. *
  236. * @throws ErrorException
  237. * The unserialize() call will trigger E_NOTICE if the string cannot
  238. * be unserialized.
  239. */
  240. public function decode($raw) {
  241. $data = @unserialize($raw);
  242. return is_array($data) ? $data : FALSE;
  243. }
  244. /**
  245. * {@inheritdoc}
  246. */
  247. public function listAll($prefix = '') {
  248. try {
  249. $query = $this->connection->select($this->table);
  250. $query->fields($this->table, ['name']);
  251. $query->condition('collection', $this->collection, '=');
  252. $query->condition('name', $prefix . '%', 'LIKE');
  253. $query->orderBy('collection')->orderBy('name');
  254. return $query->execute()->fetchCol();
  255. }
  256. catch (\Exception $e) {
  257. return [];
  258. }
  259. }
  260. /**
  261. * {@inheritdoc}
  262. */
  263. public function deleteAll($prefix = '') {
  264. try {
  265. $options = ['return' => Database::RETURN_AFFECTED] + $this->options;
  266. return (bool) $this->connection->delete($this->table, $options)
  267. ->condition('name', $prefix . '%', 'LIKE')
  268. ->condition('collection', $this->collection)
  269. ->execute();
  270. }
  271. catch (\Exception $e) {
  272. return FALSE;
  273. }
  274. }
  275. /**
  276. * {@inheritdoc}
  277. */
  278. public function createCollection($collection) {
  279. return new static(
  280. $this->connection,
  281. $this->table,
  282. $this->options,
  283. $collection
  284. );
  285. }
  286. /**
  287. * {@inheritdoc}
  288. */
  289. public function getCollectionName() {
  290. return $this->collection;
  291. }
  292. /**
  293. * {@inheritdoc}
  294. */
  295. public function getAllCollectionNames() {
  296. try {
  297. return $this->connection->query('SELECT DISTINCT collection FROM {' . $this->connection->escapeTable($this->table) . '} WHERE collection <> :collection ORDER by collection', [
  298. ':collection' => StorageInterface::DEFAULT_COLLECTION,
  299. ]
  300. )->fetchCol();
  301. }
  302. catch (\Exception $e) {
  303. return [];
  304. }
  305. }
  306. }