]> git.mxchange.org Git - friendica.git/blob - src/Core/KeyValueStorage/Type/DBKeyValueStorage.php
Change key-value table
[friendica.git] / src / Core / KeyValueStorage / Type / DBKeyValueStorage.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Core\KeyValueStorage\Type;
23
24 use Friendica\Core\Config\Util\ValueConversion;
25 use Friendica\Core\KeyValueStorage\Exceptions\KeyValueStoragePersistenceException;
26 use Friendica\Database\Database;
27
28 /**
29  * A Key-Value storage provider with DB as persistence layer
30  */
31 class DBKeyValueStorage extends AbstractKeyValueStorage
32 {
33         const DB_KEY_VALUE_TABLE = 'key-value';
34
35         /** @var Database */
36         protected $database;
37
38         public function __construct(Database $database)
39         {
40                 $this->database = $database;
41         }
42
43         /** {@inheritDoc} */
44         public function offsetExists($offset): bool
45         {
46                 try {
47                         return $this->database->exists(self::DB_KEY_VALUE_TABLE, ['k' => $offset]);
48                 } catch (\Exception $exception) {
49                         throw new KeyValueStoragePersistenceException(sprintf('Cannot check storage with key %s', $offset), $exception);
50                 }
51         }
52
53         /** {@inheritDoc} */
54         #[\ReturnTypeWillChange]
55         public function offsetGet($offset)
56         {
57                 try {
58                         $result = $this->database->selectFirst(self::DB_KEY_VALUE_TABLE, ['v'], ['k' => $offset]);
59
60                         if ($this->database->isResult($result)) {
61                                 $value = ValueConversion::toConfigValue($result['v']);
62
63                                 // just return it in case it is set
64                                 if (isset($value)) {
65                                         return $value;
66                                 }
67                         }
68                 } catch (\Exception $exception) {
69                         throw new KeyValueStoragePersistenceException(sprintf('Cannot get value for key %s', $offset), $exception);
70                 }
71
72                 return null;
73         }
74
75         /** {@inheritDoc} */
76         #[\ReturnTypeWillChange]
77         public function offsetSet($offset, $value)
78         {
79                 try {
80                         // We store our setting values in a string variable.
81                         // So we have to do the conversion here so that the compare below works.
82                         // The exception are array values.
83                         $compare_value = (!is_array($value) ? (string)$value : $value);
84                         $stored_value  = $this->get($offset);
85
86                         if (isset($stored_value) && ($stored_value === $compare_value)) {
87                                 return;
88                         }
89
90                         $dbValue = ValueConversion::toDbValue($value);
91
92                         $return = $this->database->update(self::DB_KEY_VALUE_TABLE, [
93                                 'v' => $dbValue,
94                                 'updated_at' => time()
95                         ], ['k' => $offset], true);
96
97                         if (!$return) {
98                                 throw new \Exception(sprintf('database update failed: %s', $this->database->errorMessage()));
99                         }
100                 } catch (\Exception $exception) {
101                         throw new KeyValueStoragePersistenceException(sprintf('Cannot set value for %s for key %s', $value, $offset), $exception);
102                 }
103         }
104
105         /** {@inheritDoc} */
106         #[\ReturnTypeWillChange]
107         public function offsetUnset($offset)
108         {
109                 try {
110                         if (!$this->database->delete(self::DB_KEY_VALUE_TABLE, ['k' => $offset])) {
111                                 throw new \Exception(sprintf('database deletion failed: %s', $this->database->errorMessage()));
112                         }
113                 } catch (\Exception $exception) {
114                         throw new KeyValueStoragePersistenceException(sprintf('Cannot delete value with key %s', $offset), $exception);
115                 }
116         }
117 }