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;
25 use Friendica\Database\Database as DBA;
28 * Database based storage system
30 * This class manage data stored in database table.
32 class Database implements ISelectableStorage
34 const NAME = 'Database';
42 public function __construct(DBA $dba)
50 public function get(string $reference): string
53 $result = $this->dba->selectFirst('storage', ['data'], ['id' => $reference]);
54 if (!$this->dba->isResult($result)) {
55 throw new ReferenceStorageException(sprintf('Database storage cannot find data for reference %s', $reference));
58 return $result['data'];
59 } catch (Exception $exception) {
60 throw new StorageException(sprintf('Database storage failed to get %s', $reference), $exception->getCode(), $exception);
67 public function put(string $data, string $reference = ''): string
69 if ($reference !== '') {
71 $result = $this->dba->update('storage', ['data' => $data], ['id' => $reference]);
72 } catch (Exception $exception) {
73 throw new StorageException(sprintf('Database storage failed to update %s', $reference), $exception->getCode(), $exception);
75 if ($result === false) {
76 throw new StorageException(sprintf('Database storage failed to update %s', $reference), 500, new Exception($this->dba->errorMessage(), $this->dba->errorNo()));
82 $result = $this->dba->insert('storage', ['data' => $data]);
83 } catch (Exception $exception) {
84 throw new StorageException(sprintf('Database storage failed to insert %s', $reference), $exception->getCode(), $exception);
86 if ($result === false) {
87 throw new StorageException(sprintf('Database storage failed to update %s', $reference), 500, new Exception($this->dba->errorMessage(), $this->dba->errorNo()));
90 return $this->dba->lastInsertId();
97 public function delete(string $reference)
100 if (!$this->dba->delete('storage', ['id' => $reference])) {
101 throw new ReferenceStorageException(sprintf('Database storage failed to delete %s', $reference));
103 } catch (Exception $exception) {
104 throw new StorageException(sprintf('Database storage failed to delete %s', $reference), $exception->getCode(), $exception);
111 public function getOptions(): array
119 public function saveOptions(array $data): array
127 public static function getName(): string
132 public function __toString()
134 return self::getName();