2018-11-29 08:36:39 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2021-03-29 08:40:20 +02:00
|
|
|
* @copyright Copyright (C) 2010-2021, the Friendica project
|
2020-02-09 15:45:36 +01:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
2018-11-29 08:36:39 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Friendica\Model\Storage;
|
|
|
|
|
2021-08-01 13:06:19 +02:00
|
|
|
use Exception;
|
2020-01-06 17:42:28 +01:00
|
|
|
use Friendica\Database\Database as DBA;
|
2018-11-29 08:36:39 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-19 07:05:23 +01:00
|
|
|
* Database based storage system
|
2018-11-29 08:36:39 +01:00
|
|
|
*
|
|
|
|
* This class manage data stored in database table.
|
|
|
|
*/
|
2021-08-10 22:07:52 +02:00
|
|
|
class Database implements IWritableStorage
|
2018-11-29 08:36:39 +01:00
|
|
|
{
|
2020-01-05 01:58:49 +01:00
|
|
|
const NAME = 'Database';
|
|
|
|
|
2020-01-06 17:42:28 +01:00
|
|
|
/** @var DBA */
|
2020-01-05 01:58:49 +01:00
|
|
|
private $dba;
|
|
|
|
|
|
|
|
/**
|
2020-01-06 17:42:28 +01:00
|
|
|
* @param DBA $dba
|
2020-01-05 01:58:49 +01:00
|
|
|
*/
|
2021-08-01 13:06:19 +02:00
|
|
|
public function __construct(DBA $dba)
|
2020-01-05 01:58:49 +01:00
|
|
|
{
|
2020-01-06 17:42:28 +01:00
|
|
|
$this->dba = $dba;
|
2020-01-05 01:58:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2021-08-01 13:06:19 +02:00
|
|
|
public function get(string $reference): string
|
2018-11-29 08:36:39 +01:00
|
|
|
{
|
2021-08-01 13:06:19 +02:00
|
|
|
try {
|
|
|
|
$result = $this->dba->selectFirst('storage', ['data'], ['id' => $reference]);
|
|
|
|
if (!$this->dba->isResult($result)) {
|
|
|
|
throw new ReferenceStorageException(sprintf('Database storage cannot find data for reference %s', $reference));
|
|
|
|
}
|
2018-11-29 08:36:39 +01:00
|
|
|
|
2021-08-01 13:06:19 +02:00
|
|
|
return $result['data'];
|
|
|
|
} catch (Exception $exception) {
|
2021-08-01 14:31:57 +02:00
|
|
|
if ($exception instanceof ReferenceStorageException) {
|
|
|
|
throw $exception;
|
|
|
|
} else {
|
|
|
|
throw new StorageException(sprintf('Database storage failed to get %s', $reference), $exception->getCode(), $exception);
|
|
|
|
}
|
2021-08-01 13:06:19 +02:00
|
|
|
}
|
2018-11-29 08:36:39 +01:00
|
|
|
}
|
|
|
|
|
2020-01-05 01:58:49 +01:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2021-08-01 13:06:19 +02:00
|
|
|
public function put(string $data, string $reference = ''): string
|
2018-11-29 08:36:39 +01:00
|
|
|
{
|
2020-01-05 01:58:49 +01:00
|
|
|
if ($reference !== '') {
|
2021-08-01 13:06:19 +02:00
|
|
|
try {
|
|
|
|
$result = $this->dba->update('storage', ['data' => $data], ['id' => $reference]);
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
throw new StorageException(sprintf('Database storage failed to update %s', $reference), $exception->getCode(), $exception);
|
|
|
|
}
|
2020-01-05 01:58:49 +01:00
|
|
|
if ($result === false) {
|
2021-08-01 13:06:19 +02:00
|
|
|
throw new StorageException(sprintf('Database storage failed to update %s', $reference), 500, new Exception($this->dba->errorMessage(), $this->dba->errorNo()));
|
2020-01-06 17:42:28 +01:00
|
|
|
}
|
2020-01-05 01:58:49 +01:00
|
|
|
|
|
|
|
return $reference;
|
2018-11-29 08:36:39 +01:00
|
|
|
} else {
|
2021-08-01 13:06:19 +02:00
|
|
|
try {
|
|
|
|
$result = $this->dba->insert('storage', ['data' => $data]);
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
throw new StorageException(sprintf('Database storage failed to insert %s', $reference), $exception->getCode(), $exception);
|
|
|
|
}
|
2020-01-05 01:58:49 +01:00
|
|
|
if ($result === false) {
|
2021-08-01 13:06:19 +02:00
|
|
|
throw new StorageException(sprintf('Database storage failed to update %s', $reference), 500, new Exception($this->dba->errorMessage(), $this->dba->errorNo()));
|
2018-11-29 08:36:39 +01:00
|
|
|
}
|
2020-01-05 01:58:49 +01:00
|
|
|
|
|
|
|
return $this->dba->lastInsertId();
|
2018-11-29 08:36:39 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-05 01:58:49 +01:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
public function delete(string $reference)
|
2018-11-29 08:36:39 +01:00
|
|
|
{
|
2021-08-01 13:06:19 +02:00
|
|
|
try {
|
2021-08-07 22:07:47 +02:00
|
|
|
if (!$this->dba->delete('storage', ['id' => $reference]) || $this->dba->affectedRows() === 0) {
|
2021-08-01 13:06:19 +02:00
|
|
|
throw new ReferenceStorageException(sprintf('Database storage failed to delete %s', $reference));
|
|
|
|
}
|
|
|
|
} catch (Exception $exception) {
|
2021-08-01 14:31:57 +02:00
|
|
|
if ($exception instanceof ReferenceStorageException) {
|
|
|
|
throw $exception;
|
|
|
|
} else {
|
|
|
|
throw new StorageException(sprintf('Database storage failed to delete %s', $reference), $exception->getCode(), $exception);
|
|
|
|
}
|
2021-08-01 13:06:19 +02:00
|
|
|
}
|
2018-11-29 08:36:39 +01:00
|
|
|
}
|
2019-02-19 01:56:41 +01:00
|
|
|
|
2020-01-05 01:58:49 +01:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2021-08-01 13:06:19 +02:00
|
|
|
public function getOptions(): array
|
2019-02-19 01:56:41 +01:00
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2020-01-05 01:58:49 +01:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2021-08-01 13:06:19 +02:00
|
|
|
public function saveOptions(array $data): array
|
2019-02-19 01:56:41 +01:00
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
2020-01-05 01:58:49 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
2021-08-01 13:06:19 +02:00
|
|
|
public static function getName(): string
|
2020-01-05 01:58:49 +01:00
|
|
|
{
|
|
|
|
return self::NAME;
|
|
|
|
}
|
2021-08-01 13:06:19 +02:00
|
|
|
|
|
|
|
public function __toString()
|
|
|
|
{
|
|
|
|
return self::getName();
|
|
|
|
}
|
2018-12-08 18:49:16 +01:00
|
|
|
}
|