3 * @copyright Copyright (C) 2010-2021, 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\Model\Storage;
24 use Friendica\Core\L10n;
25 use Psr\Log\LoggerInterface;
26 use Friendica\Database\Database as DBA;
29 * Database based storage system
31 * This class manage data stored in database table.
33 class Database extends AbstractStorage
35 const NAME = 'Database';
42 * @param LoggerInterface $logger
45 public function __construct(DBA $dba, LoggerInterface $logger, L10n $l10n)
47 parent::__construct($l10n, $logger);
55 public function get(string $reference)
57 $result = $this->dba->selectFirst('storage', ['data'], ['id' => $reference]);
58 if (!$this->dba->isResult($result)) {
62 return $result['data'];
68 public function put(string $data, string $reference = '')
70 if ($reference !== '') {
71 $result = $this->dba->update('storage', ['data' => $data], ['id' => $reference]);
72 if ($result === false) {
73 $this->logger->warning('Failed to update data.', ['id' => $reference, 'errorCode' => $this->dba->errorNo(), 'errorMessage' => $this->dba->errorMessage()]);
74 throw new StorageException($this->l10n->t('Database storage failed to update %s', $reference));
79 $result = $this->dba->insert('storage', ['data' => $data]);
80 if ($result === false) {
81 $this->logger->warning('Failed to insert data.', ['errorCode' => $this->dba->errorNo(), 'errorMessage' => $this->dba->errorMessage()]);
82 throw new StorageException($this->l10n->t('Database storage failed to insert data'));
85 return $this->dba->lastInsertId();
92 public function delete(string $reference)
94 return $this->dba->delete('storage', ['id' => $reference]);
100 public function getOptions()
108 public function saveOptions(array $data)
116 public static function getName()