3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Core\KeyValueStorage\Type;
24 use Friendica\Core\PConfig\Util\ValueConversion;
25 use Friendica\Core\KeyValueStorage\Exceptions\KeyValueStoragePersistenceException;
26 use Friendica\Database\Database;
29 * A Key-Value storage provider with DB as persistence layer
31 class DBKeyValueStorage extends AbstractKeyValueStorage
33 const DB_KEY_VALUE_TABLE = 'key-value';
38 public function __construct(Database $database)
40 $this->database = $database;
44 public function offsetExists($offset): bool
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);
54 #[\ReturnTypeWillChange]
55 public function offsetGet($offset)
58 $result = $this->database->selectFirst(self::DB_KEY_VALUE_TABLE, ['v'], ['k' => $offset]);
60 if ($this->database->isResult($result)) {
61 $value = ValueConversion::toConfigValue($result['v']);
63 // just return it in case it is set
68 } catch (\Exception $exception) {
69 throw new KeyValueStoragePersistenceException(sprintf('Cannot get value for key %s', $offset), $exception);
76 #[\ReturnTypeWillChange]
77 public function offsetSet($offset, $value)
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);
86 if (isset($stored_value) && ($stored_value === $compare_value)) {
90 $dbValue = ValueConversion::toDbValue($value);
92 $return = $this->database->update(self::DB_KEY_VALUE_TABLE, [
94 'updated_at' => time()
95 ], ['k' => $offset], true);
98 throw new \Exception(sprintf('database update failed: %s', $this->database->errorMessage()));
100 } catch (\Exception $exception) {
101 throw new KeyValueStoragePersistenceException(sprintf('Cannot set value for %s for key %s', $value, $offset), $exception);
106 #[\ReturnTypeWillChange]
107 public function offsetUnset($offset)
110 if (!$this->database->delete(self::DB_KEY_VALUE_TABLE, ['k' => $offset])) {
111 throw new \Exception(sprintf('database deletion failed: %s', $this->database->errorMessage()));
113 } catch (\Exception $exception) {
114 throw new KeyValueStoragePersistenceException(sprintf('Cannot delete value with key %s', $offset), $exception);