Compare commits
41 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7c71fdf49c | ||
|
|
56d801cc54 | ||
|
|
d6d571cb1a | ||
|
|
7e8dfe0678 | ||
|
|
f42bc7751a | ||
|
|
35026bb223 | ||
|
|
e2727c34e8 | ||
|
|
e16700fe8d | ||
|
|
335c4d240c | ||
|
|
91af1ea14f | ||
|
|
41523e6899 | ||
|
|
4a56e42af4 | ||
|
|
4b1c0d7044 | ||
|
|
d300a6b628 | ||
|
|
09e8520ded | ||
|
|
e6fd18fa2f | ||
|
|
9c1f6be326 | ||
|
|
f3458d1870 | ||
|
|
1ce2a09e01 | ||
|
|
fd996d40c8 | ||
|
|
96ee46476c | ||
|
|
d8991dcef5 | ||
|
|
30a20d427b | ||
|
|
10f6d62b43 | ||
|
|
2beff209de | ||
|
|
a39c0a6bf1 | ||
|
|
c516546b5d | ||
|
|
945c95441f | ||
|
|
d0e38875e6 | ||
|
|
73c9af71ab | ||
|
|
5b0c0bc7b4 | ||
|
|
dfbcfdf321 | ||
|
|
a54b55cc4e | ||
|
|
612132f938 | ||
|
|
73b7cb3a5f | ||
|
|
4f7993750c | ||
|
|
bdff890b41 | ||
|
|
e390bc262e | ||
|
|
2afc439354 | ||
|
|
2cfc92584c | ||
|
|
ac572cc92b |
@@ -1,6 +1,6 @@
|
|||||||
## Rmphp/Storage
|
## Rmphp/Storage
|
||||||
|
|
||||||
DB component for **Rmphp**
|
Storage component for **Rmphp**
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
||||||
@@ -10,12 +10,12 @@ Stable version
|
|||||||
composer require rmphp/storage
|
composer require rmphp/storage
|
||||||
```
|
```
|
||||||
```bash
|
```bash
|
||||||
composer require rmphp/storage:"^3.0"
|
composer require rmphp/storage:"^12.0"
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
Dev version contains the latest changes
|
Dev version contains the latest changes
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
composer require rmphp/storage:"3.x-dev"
|
composer require rmphp/storage:"12.x-dev"
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"php": "^8.1",
|
"php": "^8.1",
|
||||||
|
"rmphp/odm": "^1.0",
|
||||||
"ext-mysqli": "*"
|
"ext-mysqli": "*"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
|
|||||||
253
src/AbstractMysqlRepository.php
Normal file
253
src/AbstractMysqlRepository.php
Normal file
@@ -0,0 +1,253 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Rmphp\Storage;
|
||||||
|
|
||||||
|
use Rmphp\ODM\ObjectDataMapper;
|
||||||
|
use Rmphp\ODM\ODMException;
|
||||||
|
use Rmphp\Storage\Mysql\MysqlRepositoryInterface;
|
||||||
|
use Rmphp\Storage\Mysql\MysqlResultData;
|
||||||
|
use Rmphp\Storage\Mysql\MysqlStorageInterface;
|
||||||
|
use Rmphp\Storage\Repository\EntityInterface;
|
||||||
|
use Rmphp\Storage\Repository\RepositoryException;
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
|
abstract class AbstractMysqlRepository implements MysqlRepositoryInterface {
|
||||||
|
|
||||||
|
public const DEBUG = false;
|
||||||
|
public const TABLE = null;
|
||||||
|
public const ENTITY = null;
|
||||||
|
|
||||||
|
private string $table;
|
||||||
|
private string $entity;
|
||||||
|
private bool $debug;
|
||||||
|
|
||||||
|
public function __construct(
|
||||||
|
public readonly MysqlStorageInterface $mysql,
|
||||||
|
public readonly ObjectDataMapper $mapper
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @inheritDoc
|
||||||
|
*/
|
||||||
|
final public function getData(object $object, callable $method = null) : array {
|
||||||
|
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()))) {
|
||||||
|
$this->mysql->updateById($table, $in, $object->getId());
|
||||||
|
return $object->getId();
|
||||||
|
} else {
|
||||||
|
$this->mysql->insert($table, $in);
|
||||||
|
return (is_string($object->getId())) ? $object->getId() : $this->mysql->mysql()->insert_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (ODMException|Throwable $throwable) {
|
||||||
|
throw new RepositoryException($throwable->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @inheritDoc */
|
||||||
|
final public function saveData(array $data, string $table = null, string $primaryKey = 'id') : mixed {
|
||||||
|
if(!isset($table)) $table = $this->getTable();
|
||||||
|
$in = array_map(function ($value){
|
||||||
|
return (is_string($value)) ? $this->mysql->escapeStr($value) : $value;
|
||||||
|
}, $data);
|
||||||
|
if($this->isDebug()) {$this->debug($data, $in, $table, ...$this->getDebugExtraData()); exit;}
|
||||||
|
try {
|
||||||
|
if (!empty($data[$primaryKey]) && !empty($this->mysql->findById($table, $data[$primaryKey], $primaryKey))) {
|
||||||
|
$this->mysql->updateById($table, $in, $data[$primaryKey]);
|
||||||
|
return $data[$primaryKey];
|
||||||
|
} else {
|
||||||
|
$this->mysql->insert($table, $in);
|
||||||
|
return (is_string($data[$primaryKey])) ? $data[$primaryKey] : $this->mysql->mysql()->insert_id;
|
||||||
|
}
|
||||||
|
} catch (Throwable $throwable) {throw new RepositoryException($throwable->getMessage());}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @inheritDoc */
|
||||||
|
final public function saveEntityGroup(array $objects, string $table = null): array {
|
||||||
|
try{
|
||||||
|
$this->mysql->mysql()->begin_transaction();
|
||||||
|
foreach($objects as $object) $id[] = $this->saveEntity($object, $table);
|
||||||
|
$this->mysql->mysql()->commit();
|
||||||
|
return $id ?? [];
|
||||||
|
}
|
||||||
|
catch (\Exception $exception){
|
||||||
|
$this->mysql->mysql()->rollback();
|
||||||
|
throw new RepositoryException($exception->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @inheritDoc */
|
||||||
|
final public function saveDataGroup(array $objects, string $table = null, string $primaryKey = 'id'): array {
|
||||||
|
try{
|
||||||
|
$this->mysql->mysql()->begin_transaction();
|
||||||
|
foreach($objects as $object) $id[] = $this->saveData($object, $table, $primaryKey);
|
||||||
|
$this->mysql->mysql()->commit();
|
||||||
|
return $id ?? [];
|
||||||
|
}
|
||||||
|
catch (\Exception $exception){
|
||||||
|
$this->mysql->mysql()->rollback();
|
||||||
|
throw new RepositoryException($exception->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @inheritDoc */
|
||||||
|
final public function deleteEntity(EntityInterface $object, string $table = null) : bool {
|
||||||
|
if(!isset($table)) $table = $this->getTable();
|
||||||
|
if(!empty($object->getId())){
|
||||||
|
return $this->mysql->deleteById($table, $object->getId());
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @inheritDoc */
|
||||||
|
final public function getStorageLogs() : array {
|
||||||
|
return $this->mysql->getLogs();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @inheritDoc */
|
||||||
|
final public function setTable(string $table) : void {
|
||||||
|
$this->table = $table;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @inheritDoc */
|
||||||
|
final public function setEntity(string $entity) : void {
|
||||||
|
$this->entity = $entity;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @inheritDoc */
|
||||||
|
final public function setDebug(bool $debug) : void {
|
||||||
|
$this->debug = $debug;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
* @throws RepositoryException
|
||||||
|
*/
|
||||||
|
private function getTable() : string {
|
||||||
|
if(!empty($this->table)) return $this->table;
|
||||||
|
if(!empty(static::TABLE)) return static::TABLE;
|
||||||
|
throw new RepositoryException("Имя таблицы не задано");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
* @throws RepositoryException
|
||||||
|
*/
|
||||||
|
private function getEntityClass() : string {
|
||||||
|
if(!empty($this->entity)) return $this->entity;
|
||||||
|
if(!empty(static::ENTITY)) return static::ENTITY;
|
||||||
|
throw new RepositoryException("Не указан объект");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
private function isDebug(): bool {
|
||||||
|
if(!empty($this->debug)) return $this->debug;
|
||||||
|
if(!empty(static::DEBUG)) return static::DEBUG;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getDebugExtraData() : array {
|
||||||
|
return [$this->mapper->getRepositoryStack(), $this->mapper->getClassesCache(), $this->mapper->getAttributesObjectsCache()];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ...$arg
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function debug(...$arg) : void {
|
||||||
|
if(function_exists('dd')) dd(...$arg);
|
||||||
|
if(function_exists('vdd')) vdd(...$arg);
|
||||||
|
var_dump(...$arg);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Rmphp\Storage\Mysql\Exception;
|
|
||||||
|
|
||||||
|
|
||||||
class MysqlException extends \Exception {
|
|
||||||
|
|
||||||
}
|
|
||||||
137
src/Mysql/MysqlRepositoryInterface.php
Normal file
137
src/Mysql/MysqlRepositoryInterface.php
Normal file
@@ -0,0 +1,137 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: Zuev Yuri
|
||||||
|
* Date: 12.01.2025
|
||||||
|
* Time: 21:44
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Rmphp\Storage\Mysql;
|
||||||
|
|
||||||
|
use Rmphp\ODM\ObjectDataMapper;
|
||||||
|
use Rmphp\Storage\Repository\EntityInterface;
|
||||||
|
use Rmphp\Storage\Repository\RepositoryException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @property MysqlStorageInterface $mysql
|
||||||
|
* @property ObjectDataMapper $mapper
|
||||||
|
*/
|
||||||
|
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 MysqlResultData|null $result
|
||||||
|
* @param callable|null $function
|
||||||
|
* @return mixed
|
||||||
|
* @throws RepositoryException
|
||||||
|
*/
|
||||||
|
public function createFromResult(string $class, ?MysqlResultData $result, callable $function = null): mixed;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $class
|
||||||
|
* @param MysqlResultData|null $result
|
||||||
|
* @param callable|null $function
|
||||||
|
* @return array
|
||||||
|
* @throws RepositoryException
|
||||||
|
*/
|
||||||
|
public function createListFromResult(string $class, ?MysqlResultData $result, callable $function = null): array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $id
|
||||||
|
* @param string|null $table
|
||||||
|
* @return mixed
|
||||||
|
* @throws RepositoryException
|
||||||
|
*/
|
||||||
|
public function getEntityById(int $id, string $table = null): mixed;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param EntityInterface $object
|
||||||
|
* @param string|null $table
|
||||||
|
* @return mixed
|
||||||
|
* @throws RepositoryException
|
||||||
|
*/
|
||||||
|
public function saveEntity(EntityInterface $object, string $table = null) : mixed;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $data
|
||||||
|
* @param string|null $table
|
||||||
|
* @param string $primaryKey
|
||||||
|
* @return mixed
|
||||||
|
* @throws RepositoryException
|
||||||
|
*/
|
||||||
|
public function saveData(array $data, string $table = null, string $primaryKey = 'id') : mixed;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $objects
|
||||||
|
* @param string|null $table
|
||||||
|
* @return array
|
||||||
|
* @throws RepositoryException
|
||||||
|
*/
|
||||||
|
public function saveEntityGroup(array $objects, string $table = null): array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $objects
|
||||||
|
* @param string|null $table
|
||||||
|
* @param string $primaryKey
|
||||||
|
* @return array
|
||||||
|
* @throws RepositoryException
|
||||||
|
*/
|
||||||
|
public function saveDataGroup(array $objects, string $table = null, string $primaryKey = 'id'): array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param EntityInterface $object
|
||||||
|
* @param string|null $table
|
||||||
|
* @return bool
|
||||||
|
* @throws RepositoryException
|
||||||
|
*/
|
||||||
|
public function deleteEntity(EntityInterface $object, string $table = null) : bool;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getStorageLogs() : array;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $table
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function setTable(string $table) : void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $entity
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function setEntity(string $entity) : void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param bool $debug
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function setDebug(bool $debug) : void;
|
||||||
|
|
||||||
|
}
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
namespace Rmphp\Storage\Mysql;
|
namespace Rmphp\Storage\Mysql;
|
||||||
|
|
||||||
|
|
||||||
class MysqlStorageData {
|
class MysqlResultData {
|
||||||
|
|
||||||
private ?\mysqli_result $result;
|
private ?\mysqli_result $result;
|
||||||
private array $arrayData = [];
|
private array $arrayData = [];
|
||||||
@@ -13,19 +13,23 @@ class MysqlStorage implements MysqlStorageInterface {
|
|||||||
private Mysqli $mysqli;
|
private Mysqli $mysqli;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Внутренний конструктор подключения к БД
|
* @param array $paramsArray
|
||||||
* Mysql constructor.
|
|
||||||
* @param array $params
|
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function __construct(array $params) {
|
public function __construct(array $paramsArray) {
|
||||||
$this->mysqli = new mysqli($params['host'], $params['user'], $params['pass'], $params['base']);
|
$params = (object) $paramsArray;
|
||||||
|
$this->mysqli = new mysqli(
|
||||||
|
$params->host,
|
||||||
|
$params->user,
|
||||||
|
$params->pass,
|
||||||
|
$params->base
|
||||||
|
);
|
||||||
// выводим ошибку при неудачном подключении
|
// выводим ошибку при неудачном подключении
|
||||||
if ($this->mysqli->connect_errno) {
|
if ($this->mysqli->connect_errno) {
|
||||||
throw new Exception($this->mysqli->connect_errno);
|
throw new Exception($this->mysqli->connect_errno);
|
||||||
}
|
}
|
||||||
$this->mysqli->set_charset("utf8");
|
$this->mysqli->set_charset($params->chartset ?? "utf8");
|
||||||
if(!empty($params['logsEnable'])) $this->logsEnabled = true;
|
if(!empty($params->logs)) $this->logsEnabled = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @inheritDoc */
|
/** @inheritDoc */
|
||||||
@@ -111,7 +115,7 @@ class MysqlStorage implements MysqlStorageInterface {
|
|||||||
|
|
||||||
|
|
||||||
/** @inheritDoc */
|
/** @inheritDoc */
|
||||||
public function find(string $sql, int $ln=0, int $numPage=1, int $count=0): bool|MysqlStorageData {
|
public function find(string $sql, int $ln=0, int $numPage=1, int $count=0): ?MysqlResultData {
|
||||||
|
|
||||||
if ($ln > 1) {
|
if ($ln > 1) {
|
||||||
$cnts = (!empty($count)) ? $count : $this->query($sql)->num_rows;
|
$cnts = (!empty($count)) ? $count : $this->query($sql)->num_rows;
|
||||||
@@ -124,28 +128,27 @@ class MysqlStorage implements MysqlStorageInterface {
|
|||||||
}
|
}
|
||||||
|
|
||||||
$result = $this->query($sql.$limit);
|
$result = $this->query($sql.$limit);
|
||||||
if (!$result || $result->num_rows == 0) return false;
|
if (!$result || $result->num_rows == 0) return null;
|
||||||
|
|
||||||
$data = new MysqlStorageData($result);
|
$data = new MysqlResultData($result);
|
||||||
$data->count = $cnts ?? 0;
|
$data->count = $cnts ?? 0;
|
||||||
$data->hex = md5($sql);
|
$data->hex = md5($sql);
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @inheritDoc */
|
/** @inheritDoc */
|
||||||
public function findOne(string $sql) : bool|array {
|
public function findOne(string $sql) : ?MysqlResultData {
|
||||||
$result = $this->query($sql." limit 0, 1");
|
$result = $this->query($sql." limit 0, 1");
|
||||||
if (!$result || $result->num_rows == 0) return false;
|
if (!$result || $result->num_rows == 0) return null;
|
||||||
$data = new MysqlStorageData($result);
|
return new MysqlResultData($result);
|
||||||
return $data->fetchOne();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/** @inheritDoc */
|
/** @inheritDoc */
|
||||||
public function findById(string $table, mixed $id, string $name = 'id') : bool|array {
|
public function findById(string $table, mixed $id, string $name = 'id') : ?array {
|
||||||
$id = (is_numeric($id)) ? (int) $id : $this->escapeStr($id);
|
$id = (is_numeric($id)) ? (int) $id : $this->escapeStr($id);
|
||||||
$result = $this->query("select * from ".$this->escapeStr($table)." where `$name`='$id' limit 0, 1");
|
$result = $this->query("select * from ".$this->escapeStr($table)." where `$name`='$id' limit 0, 1");
|
||||||
if (!$result || $result->num_rows == 0) return false;
|
if (!$result || $result->num_rows == 0) return null;
|
||||||
$data = new MysqlStorageData($result);
|
$data = new MysqlResultData($result);
|
||||||
return $data->fetchOne();
|
return $data->fetchOne();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -80,15 +80,15 @@ interface MysqlStorageInterface {
|
|||||||
* @param int $ln
|
* @param int $ln
|
||||||
* @param int $numPage
|
* @param int $numPage
|
||||||
* @param int $count
|
* @param int $count
|
||||||
* @return bool|MysqlStorageData
|
* @return bool|MysqlResultData
|
||||||
*/
|
*/
|
||||||
public function find(string $sql, int $ln = 0, int $numPage = 1, int $count=0) : bool|MysqlStorageData;
|
public function find(string $sql, int $ln = 0, int $numPage = 1, int $count=0) : ?MysqlResultData;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $sql
|
* @param string $sql
|
||||||
* @return bool|array
|
* @return bool|array
|
||||||
*/
|
*/
|
||||||
public function findOne(string $sql) : bool|array;
|
public function findOne(string $sql) : ?MysqlResultData;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $table
|
* @param string $table
|
||||||
@@ -96,7 +96,7 @@ interface MysqlStorageInterface {
|
|||||||
* @param string $name
|
* @param string $name
|
||||||
* @return bool|array
|
* @return bool|array
|
||||||
*/
|
*/
|
||||||
public function findById(string $table, mixed $id, string $name = 'id') : bool|array;
|
public function findById(string $table, mixed $id, string $name = 'id') : ?array;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Метод экранирования данных с учетом текущего подключения в т.ч для LIKE
|
* Метод экранирования данных с учетом текущего подключения в т.ч для LIKE
|
||||||
|
|||||||
39
src/Repository/AbstractEntity.php
Normal file
39
src/Repository/AbstractEntity.php
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Rmphp\Storage\Repository;
|
||||||
|
|
||||||
|
abstract class AbstractEntity implements EntityInterface {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getId(): mixed {
|
||||||
|
return (isset($this->id)) ? (($this->id instanceof ValueObjectInterface) ? $this->id->getValue() : $this->id) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
* @param $value
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __set(string $name, $value): void {
|
||||||
|
$this->$name = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function __get(string $name) {
|
||||||
|
return $this->$name ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $name
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function __isset(string $name): bool {
|
||||||
|
return isset($this->$name);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
18
src/Repository/EntityInterface.php
Normal file
18
src/Repository/EntityInterface.php
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: Zuev Yuri
|
||||||
|
* Date: 23.04.2024
|
||||||
|
* Time: 3:58
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Rmphp\Storage\Repository;
|
||||||
|
|
||||||
|
interface EntityInterface {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function getId(): mixed;
|
||||||
|
|
||||||
|
}
|
||||||
16
src/Repository/RepositoryException.php
Normal file
16
src/Repository/RepositoryException.php
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Rmphp\Storage\Repository;
|
||||||
|
|
||||||
|
use Throwable;
|
||||||
|
|
||||||
|
class RepositoryException extends \Exception {
|
||||||
|
|
||||||
|
public array $data;
|
||||||
|
|
||||||
|
public function __construct($message="", $code=0, array $data = [], Throwable $previous=null) {
|
||||||
|
parent::__construct($message, $code, $previous);
|
||||||
|
$this->data = $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
9
src/Repository/ValueObjectInterface.php
Normal file
9
src/Repository/ValueObjectInterface.php
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Rmphp\Storage\Repository;
|
||||||
|
|
||||||
|
interface ValueObjectInterface {
|
||||||
|
|
||||||
|
public function __toString(): string;
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user