20250622#3

This commit is contained in:
User
2025-06-22 02:49:36 +03:00
parent 7e8dfe0678
commit d6d571cb1a
2 changed files with 128 additions and 69 deletions

View File

@@ -2,7 +2,6 @@
namespace Rmphp\Storage; namespace Rmphp\Storage;
use Rmphp\ODM\ObjectDataMapper; use Rmphp\ODM\ObjectDataMapper;
use Rmphp\ODM\ODMException; use Rmphp\ODM\ODMException;
use Rmphp\Storage\Mysql\MysqlRepositoryInterface; use Rmphp\Storage\Mysql\MysqlRepositoryInterface;
@@ -10,6 +9,7 @@ use Rmphp\Storage\Mysql\MysqlResultData;
use Rmphp\Storage\Mysql\MysqlStorageInterface; use Rmphp\Storage\Mysql\MysqlStorageInterface;
use Rmphp\Storage\Repository\EntityInterface; use Rmphp\Storage\Repository\EntityInterface;
use Rmphp\Storage\Repository\RepositoryException; use Rmphp\Storage\Repository\RepositoryException;
use Throwable;
abstract class AbstractMysqlRepository implements MysqlRepositoryInterface { abstract class AbstractMysqlRepository implements MysqlRepositoryInterface {
@@ -23,60 +23,98 @@ abstract class AbstractMysqlRepository implements MysqlRepositoryInterface {
public function __construct( public function __construct(
public readonly MysqlStorageInterface $mysql, public readonly MysqlStorageInterface $mysql,
public readonly ObjectDataMapper $mapper protected readonly ObjectDataMapper $mapper
) {} ) {}
/** /**
* @inheritDoc * @inheritDoc
* @throws ODMException
*/ */
public function createFromResult(string $class, ?MysqlResultData $result, callable $function = null): mixed { final public function getData(object $object, callable $method = null) : array {
if($result instanceof MysqlResultData) {
$val = (isset($function)) ? $function($result->fetchOne()) : $result->fetchOne();
$out = $this->mapper->createObjectFromData($class, $val);
}
return $out ?? null;
}
/**
* @inheritDoc
* @throws ODMException
*/
public function createListFromResult(string $class, ?MysqlResultData $result, callable $function = null): array {
if($result instanceof MysqlResultData) {
foreach($result->fetch() as $resultValue) {
$val = (isset($function)) ? $function($resultValue) : $resultValue;
$out[] = $this->mapper->createObjectFromData($class, $val);
}
}
return $out ?? [];
}
/**
* @inheritDoc
* @throws ODMException
*/
public function getEntityById(int $id, string $table = null): mixed {
if(!isset($table)) $table = $this->getTable();
if($result = $this->mysql->findById($table, $id)) $out = $this->mapper->createObjectFromData($this->getEntityClass(), $result);
return $out ?? null;
}
/**
* @inheritDoc
* @throws ODMException
*/
public function saveEntity(EntityInterface $object, string $table = null) : mixed {
if(!isset($table)) $table = $this->getTable();
$in = $this->mapper->getDataFromObject($object, function ($value){
return (is_string($value)) ? $this->mysql->escapeStr($value) : $value;
});
if($this->isDebug()) {$this->debug($object, $in, $table, ...$this->getDebugExtraData()); exit;}
try { try {
return $this->mapper->getDataFromObject($object, $method);
} catch(ODMException $odmException) {
throw new RepositoryException($odmException->getMessage(), $odmException->getCode());
}
}
/**
* @inheritDoc
*/
final public function createFromData(string $class, array|object $data, bool $withEmpty = true) : object {
try{
return $this->mapper->createObjectFromData($class, $data, $withEmpty);
} catch(ODMException $odmException) {
throw new RepositoryException($odmException->getMessage(), $odmException->getCode());
}
}
/**
* @inheritDoc
*/
final public function updateFromData(object $object, array|object $data, bool $withEmpty = true) : object {
try{
return $this->mapper->updateObjectFromData($object, $data, $withEmpty);
} catch(ODMException $odmException) {
throw new RepositoryException($odmException->getMessage(), $odmException->getCode());
}
}
/**
* @inheritDoc
*/
final public function createFromResult(string $class, ?MysqlResultData $result, callable $function = null): mixed {
try {
if($result instanceof MysqlResultData) {
$val = (isset($function)) ? $function($result->fetchOne()) : $result->fetchOne();
$out = $this->mapper->createObjectFromData($class, $val);
}
return $out ?? null;
} catch(ODMException $odmException) {
throw new RepositoryException($odmException->getMessage(), $odmException->getCode());
}
}
/**
* @inheritDoc
*/
final public function createListFromResult(string $class, ?MysqlResultData $result, callable $function = null): array {
try {
if($result instanceof MysqlResultData) {
foreach($result->fetch() as $resultValue) {
$val = (isset($function)) ? $function($resultValue) : $resultValue;
$out[] = $this->mapper->createObjectFromData($class, $val);
}
}
return $out ?? [];
} catch(ODMException $odmException) {
throw new RepositoryException($odmException->getMessage(), $odmException->getCode());
}
}
/**
* @inheritDoc
*/
final public function getEntityById(int $id, string $table = null): mixed {
try {
if(!isset($table)) $table = $this->getTable();
if($result = $this->mysql->findById($table, $id)) $out = $this->mapper->createObjectFromData($this->getEntityClass(), $result);
return $out ?? null;
} catch(ODMException $odmException) {
throw new RepositoryException($odmException->getMessage(), $odmException->getCode());
}
}
/**
* @inheritDoc
*/
final public function saveEntity(EntityInterface $object, string $table = null) : mixed {
try {
if(!isset($table)) $table = $this->getTable();
$in = $this->mapper->getDataFromObject($object, function ($value){
return (is_string($value)) ? $this->mysql->escapeStr($value) : $value;
});
if($this->isDebug()) {$this->debug($object, $in, $table, ...$this->getDebugExtraData()); exit;}
if (!empty($object->getId()) && !empty($this->mysql->findById($table, $object->getId()))) { if (!empty($object->getId()) && !empty($this->mysql->findById($table, $object->getId()))) {
$this->mysql->updateById($table, $in, $object->getId()); $this->mysql->updateById($table, $in, $object->getId());
return $object->getId(); return $object->getId();
@@ -84,12 +122,14 @@ abstract class AbstractMysqlRepository implements MysqlRepositoryInterface {
$this->mysql->insert($table, $in); $this->mysql->insert($table, $in);
return (is_string($object->getId())) ? $object->getId() : $this->mysql->mysql()->insert_id; return (is_string($object->getId())) ? $object->getId() : $this->mysql->mysql()->insert_id;
} }
} catch (\Throwable $throwable) {throw new RepositoryException($throwable->getMessage());} }
catch (ODMException|Throwable $throwable) {
throw new RepositoryException($throwable->getMessage());
}
} }
/** @inheritDoc */ /** @inheritDoc */
public function saveData(array $data, string $table = null, string $primaryKey = 'id') : mixed { final public function saveData(array $data, string $table = null, string $primaryKey = 'id') : mixed {
if(!isset($table)) $table = $this->getTable(); if(!isset($table)) $table = $this->getTable();
$in = array_map(function ($value){ $in = array_map(function ($value){
return (is_string($value)) ? $this->mysql->escapeStr($value) : $value; return (is_string($value)) ? $this->mysql->escapeStr($value) : $value;
@@ -103,12 +143,11 @@ abstract class AbstractMysqlRepository implements MysqlRepositoryInterface {
$this->mysql->insert($table, $in); $this->mysql->insert($table, $in);
return (is_string($data[$primaryKey])) ? $data[$primaryKey] : $this->mysql->mysql()->insert_id; return (is_string($data[$primaryKey])) ? $data[$primaryKey] : $this->mysql->mysql()->insert_id;
} }
} catch (\Throwable $throwable) {throw new RepositoryException($throwable->getMessage());} } catch (Throwable $throwable) {throw new RepositoryException($throwable->getMessage());}
} }
/** @inheritDoc */ /** @inheritDoc */
public function saveEntityGroup(array $objects, string $table = null): array { final public function saveEntityGroup(array $objects, string $table = null): array {
try{ try{
$this->mysql->mysql()->begin_transaction(); $this->mysql->mysql()->begin_transaction();
foreach($objects as $object) $id[] = $this->saveEntity($object, $table); foreach($objects as $object) $id[] = $this->saveEntity($object, $table);
@@ -121,9 +160,8 @@ abstract class AbstractMysqlRepository implements MysqlRepositoryInterface {
} }
} }
/** @inheritDoc */ /** @inheritDoc */
public function saveDataGroup(array $objects, string $table = null, string $primaryKey = 'id'): array { final public function saveDataGroup(array $objects, string $table = null, string $primaryKey = 'id'): array {
try{ try{
$this->mysql->mysql()->begin_transaction(); $this->mysql->mysql()->begin_transaction();
foreach($objects as $object) $id[] = $this->saveData($object, $table, $primaryKey); foreach($objects as $object) $id[] = $this->saveData($object, $table, $primaryKey);
@@ -136,9 +174,8 @@ abstract class AbstractMysqlRepository implements MysqlRepositoryInterface {
} }
} }
/** @inheritDoc */ /** @inheritDoc */
public function deleteEntity(EntityInterface $object, string $table = null) : bool { final public function deleteEntity(EntityInterface $object, string $table = null) : bool {
if(!isset($table)) $table = $this->getTable(); if(!isset($table)) $table = $this->getTable();
if(!empty($object->getId())){ if(!empty($object->getId())){
return $this->mysql->deleteById($table, $object->getId()); return $this->mysql->deleteById($table, $object->getId());
@@ -146,27 +183,23 @@ abstract class AbstractMysqlRepository implements MysqlRepositoryInterface {
return false; return false;
} }
/** @inheritDoc */ /** @inheritDoc */
public function getStorageLogs() : array { final public function getStorageLogs() : array {
return $this->mysql->getLogs(); return $this->mysql->getLogs();
} }
/** @inheritDoc */ /** @inheritDoc */
public function setTable(string $table) : void { final public function setTable(string $table) : void {
$this->table = $table; $this->table = $table;
} }
/** @inheritDoc */ /** @inheritDoc */
public function setEntity(string $entity) : void { final public function setEntity(string $entity) : void {
$this->entity = $entity; $this->entity = $entity;
} }
/** @inheritDoc */ /** @inheritDoc */
public function setDebug(bool $debug) : void { final public function setDebug(bool $debug) : void {
$this->debug = $debug; $this->debug = $debug;
} }
@@ -181,7 +214,6 @@ abstract class AbstractMysqlRepository implements MysqlRepositoryInterface {
throw new RepositoryException("Имя таблицы не задано"); throw new RepositoryException("Имя таблицы не задано");
} }
/** /**
* @return string * @return string
* @throws RepositoryException * @throws RepositoryException
@@ -192,7 +224,6 @@ abstract class AbstractMysqlRepository implements MysqlRepositoryInterface {
throw new RepositoryException("Не указан объект"); throw new RepositoryException("Не указан объект");
} }
/** /**
* @return bool * @return bool
*/ */

View File

@@ -8,16 +8,43 @@
namespace Rmphp\Storage\Mysql; namespace Rmphp\Storage\Mysql;
use Rmphp\ODM\ObjectDataMapper;
use Rmphp\Storage\Repository\EntityInterface; use Rmphp\Storage\Repository\EntityInterface;
use Rmphp\Storage\Repository\RepositoryException; use Rmphp\Storage\Repository\RepositoryException;
interface MysqlRepositoryInterface { interface MysqlRepositoryInterface {
/**
* @param object $object
* @param callable|null $method
* @return array
* @throws RepositoryException
*/
public function getData(object $object, callable $method = null) : array;
/**
* @param string $class
* @param array|object $data
* @param bool $withEmpty
* @return mixed
*/
public function createFromData(string $class, array|object $data, bool $withEmpty = true) : mixed;
/**
* @param object $object
* @param array|object $data
* @param bool $withEmpty
* @return mixed
*/
public function updateFromData(object $object, array|object $data, bool $withEmpty = true) : mixed;
/** /**
* @param string $class * @param string $class
* @param MysqlResultData|null $result * @param MysqlResultData|null $result
* @param callable|null $function * @param callable|null $function
* @return mixed * @return mixed
* @throws RepositoryException
*/ */
public function createFromResult(string $class, ?MysqlResultData $result, callable $function = null): mixed; public function createFromResult(string $class, ?MysqlResultData $result, callable $function = null): mixed;
@@ -27,6 +54,7 @@ interface MysqlRepositoryInterface {
* @param MysqlResultData|null $result * @param MysqlResultData|null $result
* @param callable|null $function * @param callable|null $function
* @return array * @return array
* @throws RepositoryException
*/ */
public function createListFromResult(string $class, ?MysqlResultData $result, callable $function = null): array; public function createListFromResult(string $class, ?MysqlResultData $result, callable $function = null): array;