Various updates
This commit is contained in:
@ -3,11 +3,11 @@ namespace ProVM\Common\Implement;
|
||||
|
||||
use PDO;
|
||||
use PDOException;
|
||||
use ProVM\Common\Exception\Database\BlankResult;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use ProVM\Common\Define\Model as ModelInterface;
|
||||
use ProVM\Common\Exception\Database\BlankResult;
|
||||
use ProVM\Common\Define;
|
||||
|
||||
abstract class Repository
|
||||
abstract class Repository implements Define\Repository
|
||||
{
|
||||
public function __construct(PDO $connection, LoggerInterface $logger)
|
||||
{
|
||||
@ -32,33 +32,32 @@ abstract class Repository
|
||||
return $this->logger;
|
||||
}
|
||||
|
||||
public function setConnection(PDO $pdo): Repository
|
||||
public function setConnection(PDO $pdo): Define\Repository
|
||||
{
|
||||
$this->connection = $pdo;
|
||||
return $this;
|
||||
}
|
||||
public function setTable(string $table): Repository
|
||||
public function setTable(string $table): Define\Repository
|
||||
{
|
||||
$this->table = $table;
|
||||
return $this;
|
||||
}
|
||||
public function setLogger(LoggerInterface $logger): Repository
|
||||
public function setLogger(LoggerInterface $logger): Define\Repository
|
||||
{
|
||||
$this->logger = $logger;
|
||||
return $this;
|
||||
}
|
||||
|
||||
abstract protected function fieldsForUpdate(): array;
|
||||
abstract protected function valuesForUpdate(ModelInterface $model): array;
|
||||
protected function idProperty(): string
|
||||
public function isInstalled(): bool
|
||||
{
|
||||
return 'getId';
|
||||
$query = "SHOW TABLES LIKE '{$this->getTable()}'";
|
||||
$st = $this->getConnection()->query($query);
|
||||
if ($st === false) {
|
||||
throw new PDOException("Could not run query {$query}");
|
||||
}
|
||||
return $st->rowCount() > 0;
|
||||
}
|
||||
protected function idField(): string
|
||||
{
|
||||
return 'id';
|
||||
}
|
||||
public function update(ModelInterface $model, ModelInterface $old): void
|
||||
public function update(Define\Model $model, Define\Model $old): void
|
||||
{
|
||||
$query = "UPDATE `{$this->getTable()}` SET ";
|
||||
$model_values = $this->valuesForUpdate($model);
|
||||
@ -79,22 +78,7 @@ abstract class Repository
|
||||
$st = $this->getConnection()->prepare($query);
|
||||
$st->execute($values);
|
||||
}
|
||||
abstract protected function fieldsForInsert(): array;
|
||||
abstract protected function valuesForInsert(ModelInterface $model): array;
|
||||
protected function insert(ModelInterface $model): void
|
||||
{
|
||||
$fields = $this->fieldsForInsert();
|
||||
$fields_string = implode(', ', array_map(function($field) {
|
||||
return "`{$field}`";
|
||||
}, $fields));
|
||||
$fields_questions = implode(', ', array_fill(0, count($fields), '?'));
|
||||
$query = "INSERT INTO `{$this->getTable()}` ({$fields_string}) VALUES ({$fields_questions})";
|
||||
$values = $this->valuesForInsert($model);
|
||||
$st = $this->getConnection()->prepare($query);
|
||||
$st->execute($values);
|
||||
}
|
||||
abstract protected function defaultFind(ModelInterface $model): ModelInterface;
|
||||
public function save(ModelInterface &$model): void
|
||||
public function save(Define\Model &$model): void
|
||||
{
|
||||
try {
|
||||
$old = $this->defaultFind($model);
|
||||
@ -107,12 +91,7 @@ abstract class Repository
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
abstract public function load(array $row): ModelInterface;
|
||||
|
||||
abstract protected function fieldsForCreate(): array;
|
||||
abstract protected function valuesForCreate(array $data): array;
|
||||
abstract protected function defaultSearch(array $data): ModelInterface;
|
||||
public function create(array $data): ModelInterface
|
||||
public function create(array $data): Define\Model
|
||||
{
|
||||
try {
|
||||
return $this->defaultSearch($data);
|
||||
@ -121,11 +100,6 @@ abstract class Repository
|
||||
return $this->load($data);
|
||||
}
|
||||
}
|
||||
|
||||
protected function getId(ModelInterface $model): int
|
||||
{
|
||||
return $model->getId();
|
||||
}
|
||||
public function resetIndex(): void
|
||||
{
|
||||
$query = "ALTER TABLE `{$this->getTable()}` AUTO_INCREMENT = 1";
|
||||
@ -136,7 +110,7 @@ abstract class Repository
|
||||
$query = "OPTIMIZE TABLE `{$this->getTable()}`";
|
||||
$this->getConnection()->query($query);
|
||||
}
|
||||
public function delete(ModelInterface $model): void
|
||||
public function delete(Define\Model $model): void
|
||||
{
|
||||
$query = "DELETE FROM `{$this->getTable()}` WHERE `{$this->idField()}` = ?";
|
||||
$st = $this->getConnection()->prepare($query);
|
||||
@ -144,8 +118,42 @@ abstract class Repository
|
||||
$this->resetIndex();
|
||||
$this->optimize();
|
||||
}
|
||||
public function fetchAll(): array
|
||||
{
|
||||
$query = "SELECT * FROM `{$this->getTable()}`";
|
||||
return $this->fetchMany($query);
|
||||
}
|
||||
public function fetchById(int $id): Define\Model
|
||||
{
|
||||
$query = "SELECT * FROM `{$this->getTable()}` WHERE `{$this->idField()}` = ?";
|
||||
return $this->fetchOne($query, [$id]);
|
||||
}
|
||||
|
||||
protected function fetchOne(string $query, ?array $values = null): ModelInterface
|
||||
protected function idProperty(): string
|
||||
{
|
||||
return 'getId';
|
||||
}
|
||||
protected function idField(): string
|
||||
{
|
||||
return 'id';
|
||||
}
|
||||
protected function insert(Define\Model $model): void
|
||||
{
|
||||
$fields = $this->fieldsForInsert();
|
||||
$fields_string = implode(', ', array_map(function($field) {
|
||||
return "`{$field}`";
|
||||
}, $fields));
|
||||
$fields_questions = implode(', ', array_fill(0, count($fields), '?'));
|
||||
$query = "INSERT INTO `{$this->getTable()}` ({$fields_string}) VALUES ({$fields_questions})";
|
||||
$values = $this->valuesForInsert($model);
|
||||
$st = $this->getConnection()->prepare($query);
|
||||
$st->execute($values);
|
||||
}
|
||||
protected function getId(Define\Model $model): int
|
||||
{
|
||||
return $model->getId();
|
||||
}
|
||||
protected function fetchOne(string $query, ?array $values = null): Define\Model
|
||||
{
|
||||
if ($values !== null) {
|
||||
$st = $this->getConnection()->prepare($query);
|
||||
@ -174,14 +182,14 @@ abstract class Repository
|
||||
return array_map([$this, 'load'], $rows);
|
||||
}
|
||||
|
||||
public function fetchAll(): array
|
||||
{
|
||||
$query = "SELECT * FROM `{$this->getTable()}`";
|
||||
return $this->fetchMany($query);
|
||||
}
|
||||
public function fetchById(int $id): ModelInterface
|
||||
{
|
||||
$query = "SELECT * FROM `{$this->getTable()}` WHERE `{$this->idField()}` = ?";
|
||||
return $this->fetchOne($query, [$id]);
|
||||
}
|
||||
}
|
||||
abstract public function install(): void;
|
||||
abstract public function load(array $row): Define\Model;
|
||||
abstract protected function fieldsForUpdate(): array;
|
||||
abstract protected function valuesForUpdate(Define\Model $model): array;
|
||||
abstract protected function fieldsForInsert(): array;
|
||||
abstract protected function valuesForInsert(Define\Model $model): array;
|
||||
abstract protected function defaultFind(Define\Model $model): Define\Model;
|
||||
abstract protected function fieldsForCreate(): array;
|
||||
abstract protected function valuesForCreate(array $data): array;
|
||||
abstract protected function defaultSearch(array $data): Define\Model;
|
||||
}
|
||||
|
Reference in New Issue
Block a user