Compare commits
84 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 5d77face53 | |||
| 2109ed7667 | |||
| a45d71d760 | |||
| e71adc7cf3 | |||
| ef3e874c0c | |||
| 614b601afa | |||
| cf2f26ec21 | |||
| 2a52172af6 | |||
| 5bf8a7feb1 | |||
| 52d26c4481 | |||
| dd369d348c | |||
| 547bb85ba9 | |||
| 4ddcc87263 | |||
| 31a6862d62 | |||
| 6e1d1a300a | |||
| e260e43c17 | |||
| 76292522e4 | |||
| 7eb6151111 | |||
| 33db0bc463 | |||
| 014ccb5fb8 | |||
| ad537d1085 | |||
| 8be23dd4c7 | |||
| f75457b18a | |||
| 89b30b8bc8 | |||
| beb522a8bf | |||
| a883a65e3b | |||
| 34f8aaaca5 | |||
| 89bd7a8ee9 | |||
| bf0d2f7611 | |||
| 348b850c31 | |||
| fd830c6a9e | |||
| 6936b5cc8f | |||
| f50782c930 | |||
| 731af328d7 | |||
| 5756573d8d | |||
| eb75e69d60 | |||
| 6745036672 | |||
| 3e75bdc089 | |||
| 70f3714b15 | |||
| d7f982b44f | |||
| 368a102409 | |||
| 1f42c353b9 | |||
| 50f6deeae7 | |||
| 68dbaba012 | |||
| cb83878684 | |||
| 1b4c7ea957 | |||
| a58b5217a8 | |||
| f4084b0b06 | |||
| 2f4e6b23e0 | |||
| c13d4101b9 | |||
| 07a40f4449 | |||
| 229c0fa523 | |||
| 8c3d3b2ce0 | |||
| 8b164c7611 | |||
| 8e9047de8a | |||
| ec8f0849ee | |||
| 3038c332c0 | |||
| 98e8abfb7c | |||
| d3bb5e851f | |||
| d60799d5a4 | |||
| 0c32559716 | |||
| ea4f7ba11a | |||
| a167169207 | |||
| b5f347b07a | |||
| a113d5f64b | |||
| c3dbaa8828 | |||
| b0dea25640 | |||
| 6dbc18c7d9 | |||
| f22a58e121 | |||
| d45725603d | |||
| f49d7cfa4b | |||
| e5a723cdb2 | |||
| d56b28a4aa | |||
| b3d1f64241 | |||
| 6b78d130fb | |||
| ee44cfbca9 | |||
| 3d588f3f33 | |||
| d903bba60f | |||
| 2f3be8cabc | |||
| fcfc2213c2 | |||
| f76755407c | |||
| 2f64375ca2 | |||
| 606cf7a722 | |||
| 7400b27cb9 |
+34
-34
@@ -1,34 +1,34 @@
|
|||||||
# Created by .ignore support plugin (hsz.mobi)
|
# Created by .ignore support plugin (hsz.mobi)
|
||||||
### Yii template
|
### Yii template
|
||||||
assets/*
|
assets/*
|
||||||
!assets/.gitignore
|
!assets/.gitignore
|
||||||
protected/runtime/*
|
protected/runtime/*
|
||||||
!protected/runtime/.gitignore
|
!protected/runtime/.gitignore
|
||||||
protected/data/*.db
|
protected/data/*.db
|
||||||
themes/classic/views/
|
themes/classic/views/
|
||||||
|
|
||||||
### Example user template template
|
### Example user template template
|
||||||
### Example user template
|
### Example user template
|
||||||
|
|
||||||
# IntelliJ project files
|
# IntelliJ project files
|
||||||
.idea
|
.idea
|
||||||
*.iml
|
*.iml
|
||||||
out
|
out
|
||||||
gen
|
gen
|
||||||
|
|
||||||
composer.lock
|
composer.lock
|
||||||
|
|
||||||
*.log
|
*.log
|
||||||
commands/result
|
commands/result
|
||||||
config/setting.php
|
config/setting.php
|
||||||
tests/
|
tests/
|
||||||
vendor/
|
vendor/
|
||||||
runtime/
|
runtime/
|
||||||
|
|
||||||
*.xml
|
*.xml
|
||||||
*.lock
|
*.lock
|
||||||
|
|
||||||
oot
|
oot
|
||||||
d
|
d
|
||||||
|
|
||||||
composer.lock
|
composer.lock
|
||||||
|
|||||||
+18
-15
@@ -1,15 +1,18 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace PHPSTORM_META {
|
namespace PHPSTORM_META {
|
||||||
|
|
||||||
// Reflect
|
// Reflect
|
||||||
use Kiri\Di\Container;
|
use Kiri\Di\Container;
|
||||||
|
use Psr\Container\ContainerInterface;
|
||||||
override(Container::get(0), map('@'));
|
|
||||||
override(Container::create(0), map('@'));
|
override(ContainerInterface::get(0), map('@'));
|
||||||
// override(\Hyperf\Utils\Context::get(0), map('@'));
|
override(Container::make(0), map('@'));
|
||||||
// override(\make(0), map('@'));
|
override(Container::get(0), map('@'));
|
||||||
override(\di(0), map('@'));
|
override(Container::create(0), map('@'));
|
||||||
override(\duplicate(0), map('@'));
|
// override(\Hyperf\Utils\Context::get(0), map('@'));
|
||||||
|
// override(\make(0), map('@'));
|
||||||
}
|
override(\di(0), map('@'));
|
||||||
|
override(\duplicate(0), map('@'));
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
+340
@@ -0,0 +1,340 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Created by PhpStorm.
|
||||||
|
* User: whwyy
|
||||||
|
* Date: 2018/4/4 0004
|
||||||
|
* Time: 14:42
|
||||||
|
*/
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Database;
|
||||||
|
|
||||||
|
use Database\Traits\QueryTrait;
|
||||||
|
use Exception;
|
||||||
|
use JetBrains\PhpStorm\ArrayShape;
|
||||||
|
use Kiri\Abstracts\Component;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ActiveQuery
|
||||||
|
* @package Database
|
||||||
|
*/
|
||||||
|
class ActiveQuery extends Component implements ISqlBuilder
|
||||||
|
{
|
||||||
|
|
||||||
|
use QueryTrait;
|
||||||
|
|
||||||
|
/** @var array */
|
||||||
|
public array $with = [];
|
||||||
|
|
||||||
|
/** @var bool */
|
||||||
|
public bool $asArray = FALSE;
|
||||||
|
|
||||||
|
/** @var bool */
|
||||||
|
public bool $useCache = FALSE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var Connection|null
|
||||||
|
*/
|
||||||
|
public ?Connection $db = NULL;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
* 参数绑定
|
||||||
|
*/
|
||||||
|
public array $attributes = [];
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Comply constructor.
|
||||||
|
* @param $model
|
||||||
|
* @param array $config
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function __construct($model, array $config = [])
|
||||||
|
{
|
||||||
|
$this->modelClass = $model;
|
||||||
|
|
||||||
|
$this->builder = SqlBuilder::builder($this);
|
||||||
|
parent::__construct($config);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 清除不完整数据
|
||||||
|
*/
|
||||||
|
public function clear()
|
||||||
|
{
|
||||||
|
$this->db = NULL;
|
||||||
|
$this->useCache = FALSE;
|
||||||
|
$this->with = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $key
|
||||||
|
* @param $value
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function addParam($key, $value): static
|
||||||
|
{
|
||||||
|
$this->attributes[$key] = $value;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $size
|
||||||
|
* @param int $page
|
||||||
|
* @return array
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
#[ArrayShape(['code' => "int", 'message' => "string", 'size' => "mixed", 'page' => "mixed", 'count' => "int", 'next' => "mixed", 'prev' => "mixed", 'param' => "array"])]
|
||||||
|
public function pagination(int $size = 20, int $page = 1): array
|
||||||
|
{
|
||||||
|
$page = max(1, $page);
|
||||||
|
$size = max(1, $size);
|
||||||
|
|
||||||
|
$offset = ($page - 1) * $size;
|
||||||
|
|
||||||
|
$count = $this->count();
|
||||||
|
$lists = $this->limit($offset, $size)->get()->toArray();
|
||||||
|
return [
|
||||||
|
'code' => 0,
|
||||||
|
'message' => 'ok',
|
||||||
|
'size' => $size,
|
||||||
|
'page' => $page,
|
||||||
|
'count' => $count,
|
||||||
|
'next' => max($page + 1, 1),
|
||||||
|
'prev' => max($page - 1, 1),
|
||||||
|
'param' => $lists,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $values
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function addParams(array $values): static
|
||||||
|
{
|
||||||
|
foreach ($values as $key => $val) {
|
||||||
|
$this->addParam($key, $val);
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $name
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function with($name): static
|
||||||
|
{
|
||||||
|
if (empty($name)) {
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
if (is_string($name)) {
|
||||||
|
$name = explode(',', $name);
|
||||||
|
}
|
||||||
|
foreach ($name as $val) {
|
||||||
|
array_push($this->with, $val);
|
||||||
|
}
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $sql
|
||||||
|
* @param array $params
|
||||||
|
* @return mixed
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function execute($sql, array $params = []): Command
|
||||||
|
{
|
||||||
|
return $this->modelClass->getConnection()->createCommand($sql, $params);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return ModelInterface|null
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function first(): ModelInterface|null
|
||||||
|
{
|
||||||
|
$data = $this->execute($this->builder->one())->one();
|
||||||
|
if (empty($data)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return $this->populate($data);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function toSql(): string
|
||||||
|
{
|
||||||
|
return $this->builder->get();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array|Collection
|
||||||
|
*/
|
||||||
|
public function get(): Collection|array
|
||||||
|
{
|
||||||
|
return $this->all();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function flush(): array|bool|int|string|null
|
||||||
|
{
|
||||||
|
return $this->execute($this->builder->truncate())->exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $size
|
||||||
|
* @param callable $callback
|
||||||
|
* @return Pagination
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function page(int $size, callable $callback): Pagination
|
||||||
|
{
|
||||||
|
$pagination = new Pagination($this);
|
||||||
|
$pagination->setOffset(0);
|
||||||
|
$pagination->setLimit($size);
|
||||||
|
$pagination->setCallback($callback);
|
||||||
|
return $pagination;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $field
|
||||||
|
* @param string $setKey
|
||||||
|
*
|
||||||
|
* @return array|null
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function column(string $field, string $setKey = ''): ?array
|
||||||
|
{
|
||||||
|
return $this->all()->column($field, $setKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array|Collection
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function all(): Collection|array
|
||||||
|
{
|
||||||
|
$data = $this->execute($this->builder->all())->all();
|
||||||
|
if (!empty($this->with)) {
|
||||||
|
$this->getWith($this->modelClass);
|
||||||
|
}
|
||||||
|
$collect = new Collection($this, $data, $this->modelClass);
|
||||||
|
if ($this->asArray) {
|
||||||
|
return $collect->toArray();
|
||||||
|
}
|
||||||
|
return $collect;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $data
|
||||||
|
* @return ModelInterface
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function populate($data): ModelInterface
|
||||||
|
{
|
||||||
|
return $this->getWith($this->modelClass::populate($data));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ModelInterface $model
|
||||||
|
* @return ModelInterface
|
||||||
|
*/
|
||||||
|
public function getWith(ModelInterface $model): ModelInterface
|
||||||
|
{
|
||||||
|
if (empty($this->with) || !is_array($this->with)) {
|
||||||
|
return $model;
|
||||||
|
}
|
||||||
|
return $model->setWith($this->with);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return int
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function count(): int
|
||||||
|
{
|
||||||
|
$data = $this->execute($this->builder->count())->one();
|
||||||
|
if ($data && is_array($data)) {
|
||||||
|
return (int)array_shift($data);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $data
|
||||||
|
* @return array|Command|bool|int|string
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function batchUpdate(array $data): Command|array|bool|int|string
|
||||||
|
{
|
||||||
|
$generate = $this->builder->update($data);
|
||||||
|
if (is_bool($generate)) {
|
||||||
|
return $generate;
|
||||||
|
}
|
||||||
|
return $this->execute(...$generate)->exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $data
|
||||||
|
* @return bool
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function batchInsert(array $data): bool
|
||||||
|
{
|
||||||
|
[$sql, $params] = $this->builder->insert($data, TRUE);
|
||||||
|
|
||||||
|
|
||||||
|
return $this->execute($sql, $params)->exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $filed
|
||||||
|
*
|
||||||
|
* @return null
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function value($filed)
|
||||||
|
{
|
||||||
|
return $this->first()[$filed] ?? NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function exists(): bool
|
||||||
|
{
|
||||||
|
return !empty($this->execute($this->builder->one())->fetchColumn());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param bool $getSql
|
||||||
|
* @return int|bool|string|null
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function delete(bool $getSql = FALSE): int|bool|string|null
|
||||||
|
{
|
||||||
|
$sql = $this->builder->delete();
|
||||||
|
if ($getSql === FALSE) {
|
||||||
|
return $this->execute($sql)->delete();
|
||||||
|
}
|
||||||
|
return $sql;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Database\Affair;
|
namespace Database\Affair;
|
||||||
|
|
||||||
class BeginTransaction
|
class BeginTransaction
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Database\Affair;
|
namespace Database\Affair;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
|
|
||||||
class Commit
|
class Commit
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Database\Affair;
|
namespace Database\Affair;
|
||||||
|
|
||||||
class Rollback
|
class Rollback
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,44 +1,44 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
namespace Database\Annotation;
|
namespace Database\Annotation;
|
||||||
|
|
||||||
|
|
||||||
use Attribute;
|
use Attribute;
|
||||||
use Database\Base\Getter;
|
use Database\Base\Getter;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Get
|
* Class Get
|
||||||
* @package Annotation\Model
|
* @package Annotation\Model
|
||||||
*/
|
*/
|
||||||
#[Attribute(Attribute::TARGET_METHOD)] class Get extends \Annotation\Attribute
|
#[Attribute(Attribute::TARGET_METHOD)] class Get extends \Annotation\Attribute
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get constructor.
|
* Get constructor.
|
||||||
* @param string $name
|
* @param string $name
|
||||||
*/
|
*/
|
||||||
public function __construct(public string $name)
|
public function __construct(public string $name)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param static $params
|
* @param static $params
|
||||||
* @param mixed $class
|
* @param mixed $class
|
||||||
* @param mixed|null $method
|
* @param mixed|null $method
|
||||||
* @return bool
|
* @return bool
|
||||||
* @throws \Kiri\Exception\NotFindClassException
|
* @throws \Kiri\Exception\NotFindClassException
|
||||||
* @throws \ReflectionException
|
* @throws \ReflectionException
|
||||||
*/
|
*/
|
||||||
public function execute(mixed $class, mixed $method = null): bool
|
public function execute(mixed $class, mixed $method = null): bool
|
||||||
{
|
{
|
||||||
di(Getter::class)->addGetter($this->name, $class, $method);
|
di(Getter::class)->addGetter($this->name, $class, $method);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,41 +1,41 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
namespace Database\Annotation;
|
namespace Database\Annotation;
|
||||||
|
|
||||||
|
|
||||||
use Annotation\Attribute;
|
use Kiri\Annotation\Attribute;
|
||||||
use Database\Base\Relate;
|
use Database\Base\Relate;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Relation
|
* Class Relation
|
||||||
* @package Annotation\Model
|
* @package Annotation\Model
|
||||||
*/
|
*/
|
||||||
#[\Attribute(\Attribute::TARGET_METHOD)] class Relation extends Attribute
|
#[\Attribute(\Attribute::TARGET_METHOD)] class Relation extends Attribute
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Relation constructor.
|
* Relation constructor.
|
||||||
* @param string $name
|
* @param string $name
|
||||||
*/
|
*/
|
||||||
public function __construct(public string $name)
|
public function __construct(public string $name)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param mixed $class
|
* @param mixed $class
|
||||||
* @param mixed|null $method
|
* @param mixed|null $method
|
||||||
* @return bool
|
* @return bool
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function execute(mixed $class, mixed $method = null): bool
|
public function execute(mixed $class, mixed $method = null): bool
|
||||||
{
|
{
|
||||||
di(Relate::class)->addRelate($this->name, $class, $method);
|
di(Relate::class)->addRelate($this->name, $class, $method);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,39 +1,39 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
namespace Database\Annotation;
|
namespace Database\Annotation;
|
||||||
|
|
||||||
|
|
||||||
use Annotation\Attribute;
|
use Kiri\Annotation\Attribute;
|
||||||
use Database\Base\Setter;
|
use Database\Base\Setter;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
|
||||||
#[\Attribute(\Attribute::TARGET_METHOD)] class Set extends Attribute
|
#[\Attribute(\Attribute::TARGET_METHOD)] class Set extends Attribute
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set constructor.
|
* Set constructor.
|
||||||
* @param string $name
|
* @param string $name
|
||||||
*/
|
*/
|
||||||
public function __construct(public string $name)
|
public function __construct(public string $name)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param static $params
|
* @param static $params
|
||||||
* @param mixed $class
|
* @param mixed $class
|
||||||
* @param mixed|null $method
|
* @param mixed|null $method
|
||||||
* @return bool
|
* @return bool
|
||||||
* @throws \Kiri\Exception\NotFindClassException
|
* @throws \Kiri\Exception\NotFindClassException
|
||||||
* @throws \ReflectionException
|
* @throws \ReflectionException
|
||||||
*/
|
*/
|
||||||
public function execute(mixed $class, mixed $method = null): bool
|
public function execute(mixed $class, mixed $method = null): bool
|
||||||
{
|
{
|
||||||
di(Setter::class)->addSetter($this->name, $class, $method);
|
di(Setter::class)->addSetter($this->name, $class, $method);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,161 +1,162 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Created by PhpStorm.
|
* Created by PhpStorm.
|
||||||
* User: whwyy
|
* User: whwyy
|
||||||
* Date: 2018/4/9 0009
|
* Date: 2018/4/9 0009
|
||||||
* Time: 9:44
|
* Time: 9:44
|
||||||
*/
|
*/
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database\Base;
|
namespace Database\Base;
|
||||||
|
|
||||||
|
|
||||||
use ArrayIterator;
|
use ArrayIterator;
|
||||||
use Database\ActiveQuery;
|
use Database\ActiveQuery;
|
||||||
use Database\ModelInterface;
|
use Database\ModelInterface;
|
||||||
use Kiri\ToArray;
|
use Kiri\ToArray;
|
||||||
use Exception;
|
use Exception;
|
||||||
use JetBrains\PhpStorm\Pure;
|
use JetBrains\PhpStorm\Pure;
|
||||||
use Kiri\Abstracts\Component;
|
use Kiri\Abstracts\Component;
|
||||||
use Traversable;
|
use ReturnTypeWillChange;
|
||||||
|
use Traversable;
|
||||||
/**
|
|
||||||
* Class AbstractCollection
|
/**
|
||||||
* @package Database\Base
|
* Class AbstractCollection
|
||||||
*/
|
* @package Database\Base
|
||||||
abstract class AbstractCollection extends Component implements \IteratorAggregate, \ArrayAccess, ToArray
|
*/
|
||||||
{
|
abstract class AbstractCollection extends Component implements \IteratorAggregate, \ArrayAccess, ToArray
|
||||||
|
{
|
||||||
/**
|
|
||||||
* @var ModelInterface[]
|
/**
|
||||||
*/
|
* @var ModelInterface[]
|
||||||
protected array $_item = [];
|
*/
|
||||||
|
protected array $_item = [];
|
||||||
protected ModelInterface|string|null $model;
|
|
||||||
|
protected ModelInterface|string|null $model;
|
||||||
protected ActiveQuery $query;
|
|
||||||
|
protected ActiveQuery $query;
|
||||||
|
|
||||||
public function clean()
|
|
||||||
{
|
public function clean()
|
||||||
unset($this->query, $this->model, $this->_item);
|
{
|
||||||
}
|
unset($this->query, $this->model, $this->_item);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Collection constructor.
|
/**
|
||||||
*
|
* Collection constructor.
|
||||||
* @param $query
|
*
|
||||||
* @param array $array
|
* @param $query
|
||||||
* @param ModelInterface|null $model
|
* @param array $array
|
||||||
* @throws Exception
|
* @param ModelInterface|null $model
|
||||||
*/
|
* @throws Exception
|
||||||
public function __construct($query, array $array = [], ModelInterface $model = null)
|
*/
|
||||||
{
|
public function __construct($query, array $array = [], ModelInterface $model = null)
|
||||||
$this->_item = $array;
|
{
|
||||||
$this->query = $query;
|
$this->_item = $array;
|
||||||
$this->model = $model;
|
$this->query = $query;
|
||||||
|
$this->model = $model;
|
||||||
parent::__construct([]);
|
|
||||||
}
|
parent::__construct([]);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return int
|
/**
|
||||||
*/
|
* @return int
|
||||||
#[Pure] public function getLength(): int
|
*/
|
||||||
{
|
#[Pure] public function getLength(): int
|
||||||
return count($this->_item);
|
{
|
||||||
}
|
return count($this->_item);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $item
|
/**
|
||||||
*/
|
* @param $item
|
||||||
public function setItems($item)
|
*/
|
||||||
{
|
public function setItems($item)
|
||||||
$this->_item = $item;
|
{
|
||||||
}
|
$this->_item = $item;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $model
|
/**
|
||||||
*/
|
* @param $model
|
||||||
public function setModel($model)
|
*/
|
||||||
{
|
public function setModel($model)
|
||||||
$this->model = $model;
|
{
|
||||||
}
|
$this->model = $model;
|
||||||
|
}
|
||||||
/**
|
|
||||||
* @param $item
|
/**
|
||||||
*/
|
* @param $item
|
||||||
public function addItem($item)
|
*/
|
||||||
{
|
public function addItem($item)
|
||||||
array_push($this->_item, $item);
|
{
|
||||||
}
|
$this->_item[] = $item;
|
||||||
|
}
|
||||||
/**
|
|
||||||
* @return Traversable|CollectionIterator|ArrayIterator
|
/**
|
||||||
* @throws Exception
|
* @return Traversable|CollectionIterator|ArrayIterator
|
||||||
*/
|
* @throws Exception
|
||||||
public function getIterator(): Traversable|CollectionIterator|ArrayIterator
|
*/
|
||||||
{
|
public function getIterator(): Traversable|CollectionIterator|ArrayIterator
|
||||||
return new CollectionIterator($this->model, $this->query, $this->_item);
|
{
|
||||||
}
|
return new CollectionIterator($this->model, $this->_item);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @return mixed
|
/**
|
||||||
* @throws Exception
|
* @return mixed
|
||||||
*/
|
* @throws Exception
|
||||||
public function getModel(): ModelInterface
|
*/
|
||||||
{
|
public function getModel(): ModelInterface
|
||||||
return $this->model;
|
{
|
||||||
}
|
return $this->model;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $offset
|
/**
|
||||||
* @return bool
|
* @param mixed $offset
|
||||||
*/
|
* @return bool
|
||||||
public function offsetExists(mixed $offset): bool
|
*/
|
||||||
{
|
public function offsetExists(mixed $offset): bool
|
||||||
return !empty($this->_item) && isset($this->_item[$offset]);
|
{
|
||||||
}
|
return !empty($this->_item) && isset($this->_item[$offset]);
|
||||||
|
}
|
||||||
/**
|
|
||||||
* @param mixed $offset
|
/**
|
||||||
* @return ModelInterface|null
|
* @param mixed $offset
|
||||||
* @throws Exception
|
* @return ModelInterface|null
|
||||||
*/
|
* @throws Exception
|
||||||
public function offsetGet(mixed $offset): ?ModelInterface
|
*/
|
||||||
{
|
public function offsetGet(mixed $offset): ?ModelInterface
|
||||||
if (!$this->offsetExists($offset)) {
|
{
|
||||||
return NULL;
|
if (!$this->offsetExists($offset)) {
|
||||||
}
|
return NULL;
|
||||||
if (!($this->_item[$offset] instanceof ModelInterface)) {
|
}
|
||||||
return $this->model->setAttributes($this->_item[$offset]);
|
if (!($this->_item[$offset] instanceof ModelInterface)) {
|
||||||
}
|
return $this->model->populates($this->_item[$offset]);
|
||||||
return $this->_item[$offset];
|
}
|
||||||
}
|
return $this->_item[$offset];
|
||||||
|
}
|
||||||
/**
|
|
||||||
* @param mixed $offset
|
/**
|
||||||
* @param mixed $value
|
* @param mixed $offset
|
||||||
*/
|
* @param mixed $value
|
||||||
public function offsetSet(mixed $offset, mixed $value)
|
*/
|
||||||
{
|
#[ReturnTypeWillChange] public function offsetSet(mixed $offset, mixed $value)
|
||||||
$this->_item[$offset] = $value;
|
{
|
||||||
}
|
$this->_item[$offset] = $value;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param mixed $offset
|
/**
|
||||||
*/
|
* @param mixed $offset
|
||||||
public function offsetUnset(mixed $offset)
|
*/
|
||||||
{
|
#[ReturnTypeWillChange] public function offsetUnset(mixed $offset)
|
||||||
if ($this->offsetExists($offset)) {
|
{
|
||||||
unset($this->_item[$offset]);
|
if ($this->offsetExists($offset)) {
|
||||||
}
|
unset($this->_item[$offset]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
@@ -1,75 +1,61 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database\Base;
|
namespace Database\Base;
|
||||||
|
|
||||||
|
|
||||||
use Database\ActiveQuery;
|
use Database\ActiveQuery;
|
||||||
use Database\ModelInterface;
|
use Database\ModelInterface;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class CollectionIterator
|
* Class CollectionIterator
|
||||||
* @package Database\Base
|
* @package Database\Base
|
||||||
*/
|
*/
|
||||||
class CollectionIterator extends \ArrayIterator
|
class CollectionIterator extends \ArrayIterator
|
||||||
{
|
{
|
||||||
|
|
||||||
private ModelInterface|string $model;
|
private ModelInterface|string $model;
|
||||||
|
|
||||||
|
|
||||||
/** @var ActiveQuery */
|
|
||||||
private ActiveQuery $query;
|
/**
|
||||||
|
* CollectionIterator constructor.
|
||||||
|
* @param $model
|
||||||
private ?ModelInterface $_clone = null;
|
* @param array $array
|
||||||
|
* @param int $flags
|
||||||
|
* @throws Exception
|
||||||
public function clean()
|
*/
|
||||||
{
|
public function __construct($model, array $array = [], int $flags = 0)
|
||||||
unset($this->query);
|
{
|
||||||
}
|
$this->model = $model;
|
||||||
|
parent::__construct($array, $flags);
|
||||||
|
}
|
||||||
/**
|
|
||||||
* CollectionIterator constructor.
|
|
||||||
* @param $model
|
/**
|
||||||
* @param $query
|
* @param $current
|
||||||
* @param array $array
|
* @return ModelInterface
|
||||||
* @param int $flags
|
* @throws Exception
|
||||||
* @throws Exception
|
*/
|
||||||
*/
|
protected function newModel($current): ModelInterface
|
||||||
public function __construct($model, $query, array $array = [], int $flags = 0)
|
{
|
||||||
{
|
return $this->model->populates($current);
|
||||||
$this->model = $model;
|
}
|
||||||
$this->query = $query;
|
|
||||||
parent::__construct($array, $flags);
|
|
||||||
}
|
/**
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
/**
|
public function current(): ModelInterface
|
||||||
* @param $current
|
{
|
||||||
* @return ModelInterface
|
if (is_array($current = parent::current())) {
|
||||||
* @throws Exception
|
$current = $this->newModel($current);
|
||||||
*/
|
}
|
||||||
protected function newModel($current): ModelInterface
|
return $current;
|
||||||
{
|
}
|
||||||
return $this->model->setAttributes($current);
|
|
||||||
}
|
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function current(): ModelInterface
|
|
||||||
{
|
|
||||||
if (is_array($current = parent::current())) {
|
|
||||||
$current = $this->newModel($current);
|
|
||||||
}
|
|
||||||
return $current;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,71 +1,71 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
namespace Database\Base;
|
namespace Database\Base;
|
||||||
|
|
||||||
|
|
||||||
use Database\Condition\BetweenCondition;
|
use Database\Condition\BetweenCondition;
|
||||||
use Database\Condition\InCondition;
|
use Database\Condition\InCondition;
|
||||||
use Database\Condition\LikeCondition;
|
use Database\Condition\LikeCondition;
|
||||||
use Database\Condition\LLikeCondition;
|
use Database\Condition\LLikeCondition;
|
||||||
use Database\Condition\MathematicsCondition;
|
use Database\Condition\MathematicsCondition;
|
||||||
use Database\Condition\NotBetweenCondition;
|
use Database\Condition\NotBetweenCondition;
|
||||||
use Database\Condition\NotInCondition;
|
use Database\Condition\NotInCondition;
|
||||||
use Database\Condition\NotLikeCondition;
|
use Database\Condition\NotLikeCondition;
|
||||||
use Database\Condition\RLikeCondition;
|
use Database\Condition\RLikeCondition;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class ConditionClassMap
|
* Class ConditionClassMap
|
||||||
* @package Database\Base
|
* @package Database\Base
|
||||||
*/
|
*/
|
||||||
class ConditionClassMap
|
class ConditionClassMap
|
||||||
{
|
{
|
||||||
|
|
||||||
public static array $conditionMap = [
|
public static array $conditionMap = [
|
||||||
'IN' => [
|
'IN' => [
|
||||||
'class' => InCondition::class
|
'class' => InCondition::class
|
||||||
],
|
],
|
||||||
'NOT IN' => [
|
'NOT IN' => [
|
||||||
'class' => NotInCondition::class
|
'class' => NotInCondition::class
|
||||||
],
|
],
|
||||||
'LIKE' => [
|
'LIKE' => [
|
||||||
'class' => LikeCondition::class
|
'class' => LikeCondition::class
|
||||||
],
|
],
|
||||||
'NOT LIKE' => [
|
'NOT LIKE' => [
|
||||||
'class' => NotLikeCondition::class
|
'class' => NotLikeCondition::class
|
||||||
],
|
],
|
||||||
'LLike' => [
|
'LLike' => [
|
||||||
'class' => LLikeCondition::class
|
'class' => LLikeCondition::class
|
||||||
],
|
],
|
||||||
'RLike' => [
|
'RLike' => [
|
||||||
'class' => RLikeCondition::class
|
'class' => RLikeCondition::class
|
||||||
],
|
],
|
||||||
'EQ' => [
|
'EQ' => [
|
||||||
'class' => MathematicsCondition::class,
|
'class' => MathematicsCondition::class,
|
||||||
'type' => 'EQ'
|
'type' => 'EQ'
|
||||||
],
|
],
|
||||||
'NEQ' => [
|
'NEQ' => [
|
||||||
'class' => MathematicsCondition::class,
|
'class' => MathematicsCondition::class,
|
||||||
'type' => 'NEQ'
|
'type' => 'NEQ'
|
||||||
],
|
],
|
||||||
'GT' => [
|
'GT' => [
|
||||||
'class' => MathematicsCondition::class,
|
'class' => MathematicsCondition::class,
|
||||||
'type' => 'GT'
|
'type' => 'GT'
|
||||||
],
|
],
|
||||||
'EGT' => [
|
'EGT' => [
|
||||||
'class' => MathematicsCondition::class,
|
'class' => MathematicsCondition::class,
|
||||||
'type' => 'EGT'
|
'type' => 'EGT'
|
||||||
],
|
],
|
||||||
'LT' => [
|
'LT' => [
|
||||||
'class' => MathematicsCondition::class,
|
'class' => MathematicsCondition::class,
|
||||||
'type' => 'LT'
|
'type' => 'LT'
|
||||||
],
|
],
|
||||||
'ELT' => [
|
'ELT' => [
|
||||||
'class' => MathematicsCondition::class,
|
'class' => MathematicsCondition::class,
|
||||||
'type' => 'ELT'
|
'type' => 'ELT'
|
||||||
],
|
],
|
||||||
'BETWEEN' => BetweenCondition::class,
|
'BETWEEN' => BetweenCondition::class,
|
||||||
'NOT BETWEEN' => NotBetweenCondition::class,
|
'NOT BETWEEN' => NotBetweenCondition::class,
|
||||||
];
|
];
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,35 +1,35 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Database\Base;
|
namespace Database\Base;
|
||||||
|
|
||||||
class Getter
|
class Getter
|
||||||
{
|
{
|
||||||
|
|
||||||
private array $_classMapping = [];
|
private array $_classMapping = [];
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $name
|
* @param $name
|
||||||
* @param $class
|
* @param $class
|
||||||
* @param $method
|
* @param $method
|
||||||
*/
|
*/
|
||||||
public function addGetter($name, $class, $method)
|
public function addGetter($name, $class, $method)
|
||||||
{
|
{
|
||||||
$this->_classMapping[$class][$name] = $method;
|
$this->_classMapping[$class][$name] = $method;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $class
|
* @param $class
|
||||||
* @param null $name
|
* @param null $name
|
||||||
* @return array|string|null
|
* @return array|string|null
|
||||||
*/
|
*/
|
||||||
public function getGetter($class, $name = null): null|array|string
|
public function getGetter($class, $name = null): null|array|string
|
||||||
{
|
{
|
||||||
if (!empty($name)) {
|
if (!empty($name)) {
|
||||||
return $this->_classMapping[$class][$name] ?? null;
|
return $this->_classMapping[$class][$name] ?? null;
|
||||||
}
|
}
|
||||||
return $this->_classMapping[$class] ?? [];
|
return $this->_classMapping[$class] ?? [];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
+1090
-1077
File diff suppressed because it is too large
Load Diff
@@ -1,36 +1,36 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Database\Base;
|
namespace Database\Base;
|
||||||
|
|
||||||
class Relate
|
class Relate
|
||||||
{
|
{
|
||||||
|
|
||||||
private array $_classMapping = [];
|
private array $_classMapping = [];
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $name
|
* @param $name
|
||||||
* @param $class
|
* @param $class
|
||||||
* @param $method
|
* @param $method
|
||||||
*/
|
*/
|
||||||
public function addRelate($name, $class, $method)
|
public function addRelate($name, $class, $method)
|
||||||
{
|
{
|
||||||
$this->_classMapping[$class][$name] = $method;
|
$this->_classMapping[$class][$name] = $method;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $class
|
* @param $class
|
||||||
* @param $name
|
* @param $name
|
||||||
* @return null|array|string
|
* @return null|array|string
|
||||||
*/
|
*/
|
||||||
public function getRelate($class, $name = null): null|array|string
|
public function getRelate($class, $name = null): null|array|string
|
||||||
{
|
{
|
||||||
if (!empty($name)) {
|
if (!empty($name)) {
|
||||||
return $this->_classMapping[$class][$name] ?? null;
|
return $this->_classMapping[$class][$name] ?? null;
|
||||||
}
|
}
|
||||||
return $this->_classMapping[$class] ?? [];
|
return $this->_classMapping[$class] ?? [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,32 +1,32 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Database\Base;
|
namespace Database\Base;
|
||||||
|
|
||||||
class Setter
|
class Setter
|
||||||
{
|
{
|
||||||
|
|
||||||
private array $_classMapping = [];
|
private array $_classMapping = [];
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $name
|
* @param $name
|
||||||
* @param $class
|
* @param $class
|
||||||
* @param $method
|
* @param $method
|
||||||
*/
|
*/
|
||||||
public function addSetter($name, $class, $method)
|
public function addSetter($name, $class, $method)
|
||||||
{
|
{
|
||||||
$this->_classMapping[$class][$name] = $method;
|
$this->_classMapping[$class][$name] = $method;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $class
|
* @param $class
|
||||||
* @param $name
|
* @param $name
|
||||||
* @return null|array|string
|
* @return null|array|string
|
||||||
*/
|
*/
|
||||||
public function getSetter($class, $name): null|array|string
|
public function getSetter($class, $name): null|array|string
|
||||||
{
|
{
|
||||||
return $this->_classMapping[$class][$name] ?? null;
|
return $this->_classMapping[$class][$name] ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
+243
-243
@@ -1,243 +1,243 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Created by PhpStorm.
|
* Created by PhpStorm.
|
||||||
* User: whwyy
|
* User: whwyy
|
||||||
* Date: 2018/4/4 0004
|
* Date: 2018/4/4 0004
|
||||||
* Time: 13:38
|
* Time: 13:38
|
||||||
*/
|
*/
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database;
|
namespace Database;
|
||||||
|
|
||||||
use Database\Base\AbstractCollection;
|
use Database\Base\AbstractCollection;
|
||||||
use Exception;
|
use Exception;
|
||||||
use JetBrains\PhpStorm\Pure;
|
use JetBrains\PhpStorm\Pure;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Collection
|
* Class Collection
|
||||||
* @package Database
|
* @package Database
|
||||||
* @property-read $length
|
* @property-read $length
|
||||||
*/
|
*/
|
||||||
class Collection extends AbstractCollection
|
class Collection extends AbstractCollection
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function getItems(): array
|
public function getItems(): array
|
||||||
{
|
{
|
||||||
// TODO: Change the autogenerated stub
|
// TODO: Change the autogenerated stub
|
||||||
return $this->_item;
|
return $this->_item;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $field
|
* @param $field
|
||||||
*
|
*
|
||||||
* @return array|null
|
* @return array|null
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function values($field): ?array
|
public function values($field): ?array
|
||||||
{
|
{
|
||||||
if (empty($field) || !is_string($field)) {
|
if (empty($field) || !is_string($field)) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
$_tmp = [];
|
$_tmp = [];
|
||||||
$data = $this->toArray();
|
$data = $this->toArray();
|
||||||
foreach ($data as $val) {
|
foreach ($data as $val) {
|
||||||
/** @var ModelInterface $val */
|
/** @var ModelInterface $val */
|
||||||
$_tmp[] = $val[$field];
|
$_tmp[] = $val[$field];
|
||||||
}
|
}
|
||||||
return $_tmp;
|
return $_tmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @return array|null
|
* @return array|null
|
||||||
*/
|
*/
|
||||||
public function keyBy(string $field): ?array
|
public function keyBy(string $field): ?array
|
||||||
{
|
{
|
||||||
$array = $this->toArray();
|
$array = $this->toArray();
|
||||||
$column = array_flip(array_column($array, $field));
|
$column = array_flip(array_column($array, $field));
|
||||||
foreach ($column as $key => $value) {
|
foreach ($column as $key => $value) {
|
||||||
$column[$key] = $array[$value];
|
$column[$key] = $array[$value];
|
||||||
}
|
}
|
||||||
|
|
||||||
return $column;
|
return $column;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function orderRand(): static
|
public function orderRand(): static
|
||||||
{
|
{
|
||||||
shuffle($this->_item);
|
shuffle($this->_item);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $start
|
* @param int $start
|
||||||
* @param int $length
|
* @param int $length
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
#[Pure] public function slice(int $start = 0, int $length = 20): array
|
#[Pure] public function slice(int $start = 0, int $length = 20): array
|
||||||
{
|
{
|
||||||
if (empty($this->_item) || !is_array($this->_item)) {
|
if (empty($this->_item) || !is_array($this->_item)) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
if (count($this->_item) < $length) {
|
if (count($this->_item) < $length) {
|
||||||
return $this->_item;
|
return $this->_item;
|
||||||
} else {
|
} else {
|
||||||
return array_slice($this->_item, $start, $length);
|
return array_slice($this->_item, $start, $length);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @param string $setKey
|
* @param string $setKey
|
||||||
*
|
*
|
||||||
* @return array|null
|
* @return array|null
|
||||||
*/
|
*/
|
||||||
public function column(string $field, string $setKey = ''): ?array
|
public function column(string $field, string $setKey = ''): ?array
|
||||||
{
|
{
|
||||||
$data = $this->toArray();
|
$data = $this->toArray();
|
||||||
if (empty($data)) {
|
if (empty($data)) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
if (!empty($setKey) && is_string($setKey)) {
|
if (!empty($setKey) && is_string($setKey)) {
|
||||||
return array_column($data, $field, $setKey);
|
return array_column($data, $field, $setKey);
|
||||||
} else {
|
} else {
|
||||||
return array_column($data, $field);
|
return array_column($data, $field);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $field
|
* @param string $field
|
||||||
*
|
*
|
||||||
* @return float|int|null
|
* @return float|int|null
|
||||||
*/
|
*/
|
||||||
public function sum(string $field): float|int|null
|
public function sum(string $field): float|int|null
|
||||||
{
|
{
|
||||||
$array = $this->column($field);
|
$array = $this->column($field);
|
||||||
if (empty($array)) {
|
if (empty($array)) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return array_sum($array);
|
return array_sum($array);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return ModelInterface|array
|
* @return ModelInterface|array
|
||||||
*/
|
*/
|
||||||
#[Pure] public function current(): ModelInterface|array
|
#[Pure] public function current(): ModelInterface|array
|
||||||
{
|
{
|
||||||
return current($this->_item);
|
return current($this->_item);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return int
|
* @return int
|
||||||
*/
|
*/
|
||||||
#[Pure] public function size(): int
|
#[Pure] public function size(): int
|
||||||
{
|
{
|
||||||
return (int)count($this->_item);
|
return (int)count($this->_item);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function toArray(): array
|
public function toArray(): array
|
||||||
{
|
{
|
||||||
$array = [];
|
$array = [];
|
||||||
/** @var Model $value */
|
/** @var Model $value */
|
||||||
foreach ($this as $value) {
|
foreach ($this as $value) {
|
||||||
if (!is_object($value)) {
|
if (!is_object($value)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$array[] = $value->setWith($this->query->with)->toArray();
|
$array[] = $value->setWith($this->query->with)->toArray();
|
||||||
}
|
}
|
||||||
$this->_item = [];
|
$this->_item = [];
|
||||||
return $array;
|
return $array;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
* 批量删除
|
* 批量删除
|
||||||
*/
|
*/
|
||||||
public function delete(): bool
|
public function delete(): bool
|
||||||
{
|
{
|
||||||
$model = $this->getModel();
|
$model = $this->getModel();
|
||||||
if (!$model->hasPrimary()) return false;
|
if (!$model->hasPrimary()) return false;
|
||||||
$ids = [];
|
$ids = [];
|
||||||
foreach ($this as $item) {
|
foreach ($this as $item) {
|
||||||
$id = $item->getPrimaryValue();
|
$id = $item->getPrimaryValue();
|
||||||
if (!empty($id)) {
|
if (!empty($id)) {
|
||||||
$ids[] = $id;
|
$ids[] = $id;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $model::query()->whereIn($model->getPrimary(), $ids)->delete();
|
return $model::query()->whereIn($model->getPrimary(), $ids)->delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $condition
|
* @param array $condition
|
||||||
* @return Collection
|
* @return Collection
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function filter(array $condition): Collection|static
|
public function filter(array $condition): Collection|static
|
||||||
{
|
{
|
||||||
$_filters = [];
|
$_filters = [];
|
||||||
if (empty($condition)) {
|
if (empty($condition)) {
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
foreach ($this as $value) {
|
foreach ($this as $value) {
|
||||||
if (!$this->filterCheck($value, $condition)) {
|
if (!$this->filterCheck($value, $condition)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$_filters[] = $value;
|
$_filters[] = $value;
|
||||||
}
|
}
|
||||||
return new Collection($this->query, $_filters, $this->model);
|
return new Collection($this->query, $_filters, $this->model);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $value
|
* @param $value
|
||||||
* @param $condition
|
* @param $condition
|
||||||
* @return bool
|
* @return bool
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function filterCheck($value, $condition): bool
|
private function filterCheck($value, $condition): bool
|
||||||
{
|
{
|
||||||
$_value = $value;
|
$_value = $value;
|
||||||
if ($_value instanceof ModelInterface) {
|
if ($_value instanceof ModelInterface) {
|
||||||
$_value = $_value->toArray();
|
$_value = $_value->toArray();
|
||||||
}
|
}
|
||||||
$_tmp = array_intersect_key($_value, $condition);
|
$_tmp = array_intersect_key($_value, $condition);
|
||||||
if (count(array_diff_assoc($_tmp, $condition)) > 0) {
|
if (count(array_diff_assoc($_tmp, $condition)) > 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $key
|
* @param $key
|
||||||
* @param $value
|
* @param $value
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function exists($key, $value): mixed
|
public function exists($key, $value): mixed
|
||||||
{
|
{
|
||||||
foreach ($this as $item) {
|
foreach ($this as $item) {
|
||||||
if ($item->$key === $value) {
|
if ($item->$key === $value) {
|
||||||
return $item;
|
return $item;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
#[Pure] public function isEmpty(): bool
|
#[Pure] public function isEmpty(): bool
|
||||||
{
|
{
|
||||||
return $this->size() < 1;
|
return $this->size() < 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+195
-223
@@ -1,223 +1,195 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Created by PhpStorm.
|
* Created by PhpStorm.
|
||||||
* User: whwyy
|
* User: whwyy
|
||||||
* Date: 2018/3/30 0030
|
* Date: 2018/3/30 0030
|
||||||
* Time: 15:23
|
* Time: 15:23
|
||||||
*/
|
*/
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database;
|
namespace Database;
|
||||||
|
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Kiri\Abstracts\Component;
|
use Kiri\Abstracts\Component;
|
||||||
use Kiri\Core\Json;
|
use Kiri\Core\Json;
|
||||||
use PDOStatement;
|
use PDOStatement;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Command
|
* Class Command
|
||||||
* @package Database
|
* @package Database
|
||||||
*/
|
*/
|
||||||
class Command extends Component
|
class Command extends Component
|
||||||
{
|
{
|
||||||
const ROW_COUNT = 'ROW_COUNT';
|
const ROW_COUNT = 'ROW_COUNT';
|
||||||
const FETCH = 'FETCH';
|
const FETCH = 'FETCH';
|
||||||
const FETCH_ALL = 'FETCH_ALL';
|
const FETCH_ALL = 'FETCH_ALL';
|
||||||
const EXECUTE = 'EXECUTE';
|
const EXECUTE = 'EXECUTE';
|
||||||
const FETCH_COLUMN = 'FETCH_COLUMN';
|
const FETCH_COLUMN = 'FETCH_COLUMN';
|
||||||
|
|
||||||
/** @var Connection */
|
/** @var Connection */
|
||||||
public Connection $db;
|
public Connection $db;
|
||||||
|
|
||||||
/** @var ?string */
|
/** @var ?string */
|
||||||
public ?string $sql = '';
|
public ?string $sql = '';
|
||||||
|
|
||||||
/** @var array */
|
/** @var array */
|
||||||
public array $params = [];
|
public array $params = [];
|
||||||
|
|
||||||
/** @var string */
|
/** @var string */
|
||||||
public string $dbname = '';
|
public string $dbname = '';
|
||||||
|
|
||||||
/** @var PDOStatement|null */
|
|
||||||
private ?PDOStatement $prepare = null;
|
/**
|
||||||
|
* @return array|bool|int|string|PDOStatement|null
|
||||||
|
* @throws Exception
|
||||||
/**
|
*/
|
||||||
* @return array|bool|int|string|PDOStatement|null
|
public function incrOrDecr(): array|bool|int|string|PDOStatement|null
|
||||||
* @throws Exception
|
{
|
||||||
*/
|
return $this->execute(static::EXECUTE);
|
||||||
public function incrOrDecr(): array|bool|int|string|PDOStatement|null
|
}
|
||||||
{
|
|
||||||
return $this->execute(static::EXECUTE);
|
/**
|
||||||
}
|
* @return int|bool|array|string|null
|
||||||
|
* @throws Exception
|
||||||
/**
|
*/
|
||||||
* @param bool $isInsert
|
public function save(): int|bool|array|string|null
|
||||||
* @param mixed $hasAutoIncrement
|
{
|
||||||
* @return int|bool|array|string|null
|
return $this->execute(static::EXECUTE);
|
||||||
* @throws Exception
|
}
|
||||||
*/
|
|
||||||
public function save(bool $isInsert = TRUE, mixed $hasAutoIncrement = null): int|bool|array|string|null
|
|
||||||
{
|
/**
|
||||||
return $this->execute(static::EXECUTE, $isInsert, $hasAutoIncrement);
|
* @return int|bool|array|string|null
|
||||||
}
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function all(): int|bool|array|string|null
|
||||||
/**
|
{
|
||||||
* @return int|bool|array|string|null
|
return $this->execute(static::FETCH_ALL);
|
||||||
* @throws Exception
|
}
|
||||||
*/
|
|
||||||
public function all(): int|bool|array|string|null
|
/**
|
||||||
{
|
* @return array|bool|int|string|null
|
||||||
return $this->execute(static::FETCH_ALL);
|
* @throws Exception
|
||||||
}
|
*/
|
||||||
|
public function one(): null|array|bool|int|string
|
||||||
/**
|
{
|
||||||
* @return array|bool|int|string|null
|
return $this->execute(static::FETCH);
|
||||||
* @throws Exception
|
}
|
||||||
*/
|
|
||||||
public function one(): null|array|bool|int|string
|
/**
|
||||||
{
|
* @return int|bool|array|string|null
|
||||||
return $this->execute(static::FETCH);
|
* @throws Exception
|
||||||
}
|
*/
|
||||||
|
public function fetchColumn(): int|bool|array|string|null
|
||||||
/**
|
{
|
||||||
* @return int|bool|array|string|null
|
return $this->execute(static::FETCH_COLUMN);
|
||||||
* @throws Exception
|
}
|
||||||
*/
|
|
||||||
public function fetchColumn(): int|bool|array|string|null
|
/**
|
||||||
{
|
* @return int|bool|array|string|null
|
||||||
return $this->execute(static::FETCH_COLUMN);
|
* @throws Exception
|
||||||
}
|
*/
|
||||||
|
public function rowCount(): int|bool|array|string|null
|
||||||
/**
|
{
|
||||||
* @return int|bool|array|string|null
|
return $this->execute(static::ROW_COUNT);
|
||||||
* @throws Exception
|
}
|
||||||
*/
|
|
||||||
public function rowCount(): int|bool|array|string|null
|
/**
|
||||||
{
|
* @return int|bool|array|string|null
|
||||||
return $this->execute(static::ROW_COUNT);
|
* @throws Exception
|
||||||
}
|
*/
|
||||||
|
public function flush(): int|bool|array|string|null
|
||||||
/**
|
{
|
||||||
* @return int|bool|array|string|null
|
return $this->execute(static::EXECUTE);
|
||||||
* @throws Exception
|
}
|
||||||
*/
|
|
||||||
public function flush(): int|bool|array|string|null
|
/**
|
||||||
{
|
* @param string $type
|
||||||
return $this->execute(static::EXECUTE);
|
* @return int|bool|array|string|null
|
||||||
}
|
* @throws Exception
|
||||||
|
*/
|
||||||
/**
|
private function execute(string $type): int|bool|array|string|null
|
||||||
* @param $type
|
{
|
||||||
* @param null $isInsert
|
try {
|
||||||
* @param bool|null $hasAutoIncrement
|
$time = microtime(true);
|
||||||
* @return int|bool|array|string|null
|
if ($type === static::EXECUTE) {
|
||||||
* @throws Exception
|
$result = $this->db->getConnect($this->sql)->execute($this->sql,$this->params);
|
||||||
*/
|
} else {
|
||||||
private function execute($type, $isInsert = null, mixed $hasAutoIncrement = null): int|bool|array|string|null
|
$result = $this->search($type);
|
||||||
{
|
}
|
||||||
try {
|
if (microtime(true) - $time >= 0.02) {
|
||||||
$time = microtime(true);
|
$this->warning('Mysql:' . Json::encode([$this->sql, $this->params]) . (microtime(true) - $time));
|
||||||
if ($type === static::EXECUTE) {
|
}
|
||||||
$result = $this->insert_or_change($isInsert, $hasAutoIncrement);
|
} catch (\Throwable $exception) {
|
||||||
} else {
|
$result = $this->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql');
|
||||||
$result = $this->search($type);
|
} finally {
|
||||||
}
|
$this->db->release();
|
||||||
if (microtime(true) - $time >= 0.02) {
|
return $result;
|
||||||
$this->warning('Mysql:' . Json::encode([$this->sql, $this->params]) . (microtime(true) - $time));
|
}
|
||||||
}
|
}
|
||||||
} catch (\Throwable $exception) {
|
|
||||||
$result = $this->addError($this->sql . '. error: ' . $exception->getMessage(), 'mysql');
|
|
||||||
} finally {
|
/**
|
||||||
$this->db->release();
|
* @param string $type
|
||||||
return $result;
|
* @return array|int|bool|null
|
||||||
}
|
* @throws Exception
|
||||||
}
|
*/
|
||||||
|
private function search(string $type): array|int|bool|null
|
||||||
|
{
|
||||||
/**
|
$pdo = $this->db->getConnect($this->sql);
|
||||||
* @param $type
|
if ($type === static::FETCH_COLUMN) {
|
||||||
* @return array|int|bool|null
|
$data = $pdo->fetchColumn($this->sql, $this->params);
|
||||||
* @throws Exception
|
} else if ($type === static::ROW_COUNT) {
|
||||||
*/
|
$data = $pdo->count($this->sql, $this->params);
|
||||||
private function search($type): array|int|bool|null
|
} else if ($type === static::FETCH_ALL) {
|
||||||
{
|
$data = $pdo->fetchAll($this->sql, $this->params);
|
||||||
$pdo = $this->db->getConnect($this->sql);
|
} else {
|
||||||
if ($type === static::FETCH_COLUMN) {
|
$data = $pdo->fetch($this->sql, $this->params);
|
||||||
$data = $pdo->fetchColumn($this->sql, $this->params);
|
}
|
||||||
} else if ($type === static::ROW_COUNT) {
|
return $data;
|
||||||
$data = $pdo->count($this->sql, $this->params);
|
}
|
||||||
} else if ($type === static::FETCH_ALL) {
|
|
||||||
$data = $pdo->fetchAll($this->sql, $this->params);
|
|
||||||
} else {
|
/**
|
||||||
$data = $pdo->fetch($this->sql, $this->params);
|
* @return int|bool|array|string|null
|
||||||
}
|
* @throws Exception
|
||||||
return $data;
|
*/
|
||||||
}
|
public function delete(): int|bool|array|string|null
|
||||||
|
{
|
||||||
|
return $this->execute(static::EXECUTE);
|
||||||
/**
|
}
|
||||||
* @param $isInsert
|
|
||||||
* @param $hasAutoIncrement
|
/**
|
||||||
* @return bool|int
|
* @return int|bool|array|string|null
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function insert_or_change($isInsert, $hasAutoIncrement): bool|int
|
public function exec(): int|bool|array|string|null
|
||||||
{
|
{
|
||||||
$pdo = $this->db->getConnect($this->sql);
|
return $this->execute(static::EXECUTE);
|
||||||
$result = $pdo->execute($this->sql, $isInsert, $this->params);
|
}
|
||||||
if ($hasAutoIncrement && $result == 0) {
|
|
||||||
return false;
|
/**
|
||||||
}
|
* @param array $data
|
||||||
return $result;
|
* @return $this
|
||||||
}
|
*/
|
||||||
|
public function bindValues(array $data = []): static
|
||||||
/**
|
{
|
||||||
* @return int|bool|array|string|null
|
if (!empty($data)) {
|
||||||
* @throws Exception
|
$this->params = array_merge($this->params, $data);
|
||||||
*/
|
}
|
||||||
public function delete(): int|bool|array|string|null
|
return $this;
|
||||||
{
|
}
|
||||||
return $this->execute(static::EXECUTE);
|
|
||||||
}
|
/**
|
||||||
|
* @param $sql
|
||||||
/**
|
* @return $this
|
||||||
* @param null $scope
|
* @throws Exception
|
||||||
* @param bool $insert
|
*/
|
||||||
* @return int|bool|array|string|null
|
public function setSql($sql): static
|
||||||
* @throws Exception
|
{
|
||||||
*/
|
$this->sql = $sql;
|
||||||
public function exec($scope = null, bool $insert = false): int|bool|array|string|null
|
return $this;
|
||||||
{
|
}
|
||||||
return $this->execute(static::EXECUTE, $insert, $scope);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $data
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function bindValues(array $data = []): static
|
|
||||||
{
|
|
||||||
if (!is_array($this->params)) {
|
|
||||||
$this->params = [];
|
|
||||||
}
|
|
||||||
if (!empty($data)) {
|
|
||||||
$this->params = array_merge($this->params, $data);
|
|
||||||
}
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $sql
|
|
||||||
* @return $this
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function setSql($sql): static
|
|
||||||
{
|
|
||||||
$this->sql = $sql;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,23 +1,23 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database\Condition;
|
namespace Database\Condition;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class BetweenCondition
|
* Class BetweenCondition
|
||||||
* @package Database\Condition
|
* @package Database\Condition
|
||||||
*/
|
*/
|
||||||
class BetweenCondition extends Condition
|
class BetweenCondition extends Condition
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function builder(): string
|
public function builder(): string
|
||||||
{
|
{
|
||||||
return $this->column . ' BETWEEN ' . (int)$this->value[0] . ' AND ' . (int)$this->value[1];
|
return $this->column . ' BETWEEN ' . (int)$this->value[0] . ' AND ' . (int)$this->value[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,22 +1,22 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database\Condition;
|
namespace Database\Condition;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class ChildCondition
|
* Class ChildCondition
|
||||||
* @package Database\Condition
|
* @package Database\Condition
|
||||||
*/
|
*/
|
||||||
class ChildCondition extends Condition
|
class ChildCondition extends Condition
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function builder(): string
|
public function builder(): string
|
||||||
{
|
{
|
||||||
return $this->column . ' ' . $this->opera . ' (' . $this->value . ')';
|
return $this->column . ' ' . $this->opera . ' (' . $this->value . ')';
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,83 +1,82 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database\Condition;
|
namespace Database\Condition;
|
||||||
|
|
||||||
|
|
||||||
use JetBrains\PhpStorm\Pure;
|
use JetBrains\PhpStorm\Pure;
|
||||||
use Kiri\Abstracts\BaseObject;
|
use Kiri\Abstracts\Component;
|
||||||
use Kiri\Core\Str;
|
|
||||||
|
/**
|
||||||
/**
|
* Class Condition
|
||||||
* Class Condition
|
* @package Database\Condition
|
||||||
* @package Database\Condition
|
*/
|
||||||
*/
|
abstract class Condition extends Component
|
||||||
abstract class Condition extends BaseObject
|
{
|
||||||
{
|
|
||||||
|
protected string $column = '';
|
||||||
protected string $column = '';
|
protected string $opera = '=';
|
||||||
protected string $opera = '=';
|
|
||||||
|
/** @var array|mixed */
|
||||||
/** @var array|mixed */
|
protected $value;
|
||||||
protected $value;
|
|
||||||
|
const INT_TYPE = ['bit', 'bool', 'tinyint', 'smallint', 'mediumint', 'int', 'bigint', 'float', 'double', 'decimal', 'timestamp'];
|
||||||
const INT_TYPE = ['bit', 'bool', 'tinyint', 'smallint', 'mediumint', 'int', 'bigint', 'float', 'double', 'decimal', 'timestamp'];
|
|
||||||
|
protected array $attributes = [];
|
||||||
protected array $attributes = [];
|
|
||||||
|
abstract public function builder();
|
||||||
abstract public function builder();
|
|
||||||
|
/**
|
||||||
/**
|
* @param string $column
|
||||||
* @param string $column
|
*/
|
||||||
*/
|
public function setColumn(string $column): void
|
||||||
public function setColumn(string $column): void
|
{
|
||||||
{
|
$this->column = $column;
|
||||||
$this->column = $column;
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
/**
|
* @param string $opera
|
||||||
* @param string $opera
|
*/
|
||||||
*/
|
public function setOpera(string $opera): void
|
||||||
public function setOpera(string $opera): void
|
{
|
||||||
{
|
$this->opera = $opera;
|
||||||
$this->opera = $opera;
|
}
|
||||||
}
|
|
||||||
|
/**
|
||||||
/**
|
* @param $params
|
||||||
* @param $params
|
*/
|
||||||
*/
|
public function setValue($params): void
|
||||||
public function setValue($params): void
|
{
|
||||||
{
|
if (is_array($params)) {
|
||||||
if (is_array($params)) {
|
$values = [];
|
||||||
$values = [];
|
foreach ($params as $item => $value) {
|
||||||
foreach ($params as $item => $value) {
|
$values[$item] = is_numeric($value) ? $value : '\'' . $value . '\'';
|
||||||
$values[$item] = is_numeric($value) ? $value : '\'' . $value . '\'';
|
}
|
||||||
}
|
$this->value = $values;
|
||||||
$this->value = $values;
|
} else {
|
||||||
} else {
|
$this->value = $this->checkIsSqlString($params);
|
||||||
$this->value = $this->checkIsSqlString($params);
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
/**
|
* @param $params
|
||||||
* @param $params
|
* @return int|string
|
||||||
* @return int|string
|
*/
|
||||||
*/
|
#[Pure] private function checkIsSqlString($params): int|string
|
||||||
#[Pure] private function checkIsSqlString($params): int|string
|
{
|
||||||
{
|
if (is_numeric($params)) {
|
||||||
if (is_numeric($params)) {
|
return $params;
|
||||||
return $params;
|
}
|
||||||
}
|
|
||||||
|
$check = ltrim($params, '(');
|
||||||
$check = ltrim($params, '(');
|
$check = strtolower(substr($check, 0, 6));
|
||||||
$check = strtolower(substr($check, 0, 6));
|
if (in_array($check, ['update', 'select', 'insert', 'delete'])) {
|
||||||
if (in_array($check, ['update', 'select', 'insert', 'delete'])) {
|
return $params;
|
||||||
return $params;
|
} else {
|
||||||
} else {
|
return sprintf('\'%s\'', $params);
|
||||||
return sprintf('\'%s\'', $params);
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
}
|
||||||
}
|
|
||||||
@@ -1,24 +1,24 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database\Condition;
|
namespace Database\Condition;
|
||||||
|
|
||||||
|
|
||||||
use JetBrains\PhpStorm\Pure;
|
use JetBrains\PhpStorm\Pure;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class DefaultCondition
|
* Class DefaultCondition
|
||||||
* @package Database\Condition
|
* @package Database\Condition
|
||||||
*/
|
*/
|
||||||
class DefaultCondition extends Condition
|
class DefaultCondition extends Condition
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
#[Pure] public function builder(): string
|
#[Pure] public function builder(): string
|
||||||
{
|
{
|
||||||
return sprintf('%s %s %s', $this->column, $this->opera, addslashes($this->value));
|
return sprintf('%s %s %s', $this->column, $this->opera, addslashes($this->value));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,30 +1,30 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database\Condition;
|
namespace Database\Condition;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class HashCondition
|
* Class HashCondition
|
||||||
* @package Yoc\db\condition
|
* @package Yoc\db\condition
|
||||||
*/
|
*/
|
||||||
class HashCondition extends Condition
|
class HashCondition extends Condition
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function builder(): string
|
public function builder(): string
|
||||||
{
|
{
|
||||||
$array = [];
|
$array = [];
|
||||||
if (empty($this->value)) {
|
if (empty($this->value)) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
foreach ($this->value as $key => $value) {
|
foreach ($this->value as $key => $value) {
|
||||||
if (is_null($value)) continue;
|
if (is_null($value)) continue;
|
||||||
|
|
||||||
$array[] = sprintf("%s = '%s'", $key, addslashes($value));
|
$array[] = sprintf("%s = '%s'", $key, addslashes($value));
|
||||||
}
|
}
|
||||||
return implode(' AND ', $array);
|
return implode(' AND ', $array);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,31 +1,31 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database\Condition;
|
namespace Database\Condition;
|
||||||
|
|
||||||
use Database\ActiveQuery;
|
use Database\ActiveQuery;
|
||||||
use Exception;
|
use Exception;
|
||||||
use JetBrains\PhpStorm\Pure;
|
use JetBrains\PhpStorm\Pure;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class InCondition
|
* Class InCondition
|
||||||
* @package Database\Condition
|
* @package Database\Condition
|
||||||
*/
|
*/
|
||||||
class InCondition extends Condition
|
class InCondition extends Condition
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
#[Pure] public function builder(): string
|
#[Pure] public function builder(): string
|
||||||
{
|
{
|
||||||
if (is_array($this->value)) {
|
if (is_array($this->value)) {
|
||||||
return sprintf('%s IN (%s)', $this->column, implode(',', $this->value));
|
return sprintf('%s IN (%s)', $this->column, implode(',', $this->value));
|
||||||
} else {
|
} else {
|
||||||
return sprintf('%s IN (%s)', $this->column, $this->value);
|
return sprintf('%s IN (%s)', $this->column, $this->value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,20 +1,20 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
namespace Database\Condition;
|
namespace Database\Condition;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class JsonCondition
|
* Class JsonCondition
|
||||||
* @package Database\Condition
|
* @package Database\Condition
|
||||||
*/
|
*/
|
||||||
class JsonCondition extends Condition
|
class JsonCondition extends Condition
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
public function builder()
|
public function builder()
|
||||||
{
|
{
|
||||||
// TODO: Implement builder() method.
|
// TODO: Implement builder() method.
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,28 +1,28 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database\Condition;
|
namespace Database\Condition;
|
||||||
|
|
||||||
use Kiri\Core\Str;
|
use Kiri\Core\Str;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class LLikeCondition
|
* Class LLikeCondition
|
||||||
* @package Database\Condition
|
* @package Database\Condition
|
||||||
*/
|
*/
|
||||||
class LLikeCondition extends Condition
|
class LLikeCondition extends Condition
|
||||||
{
|
{
|
||||||
|
|
||||||
public string $pos = '';
|
public string $pos = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function builder(): string
|
public function builder(): string
|
||||||
{
|
{
|
||||||
if (!is_string($this->value)) {
|
if (!is_string($this->value)) {
|
||||||
$this->value = array_shift($this->value);
|
$this->value = array_shift($this->value);
|
||||||
}
|
}
|
||||||
return $this->column . ' LIKE \'%' . addslashes($this->value) . '\'';
|
return $this->column . ' LIKE \'%' . addslashes($this->value) . '\'';
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,28 +1,28 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database\Condition;
|
namespace Database\Condition;
|
||||||
|
|
||||||
use Kiri\Core\Str;
|
use Kiri\Core\Str;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class LikeCondition
|
* Class LikeCondition
|
||||||
* @package Database\Condition
|
* @package Database\Condition
|
||||||
*/
|
*/
|
||||||
class LikeCondition extends Condition
|
class LikeCondition extends Condition
|
||||||
{
|
{
|
||||||
|
|
||||||
public string $pos = '';
|
public string $pos = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function builder(): string
|
public function builder(): string
|
||||||
{
|
{
|
||||||
if (!is_string($this->value)) {
|
if (!is_string($this->value)) {
|
||||||
$this->value = array_shift($this->value);
|
$this->value = array_shift($this->value);
|
||||||
}
|
}
|
||||||
return $this->column . ' LIKE \'%' . addslashes($this->value) . '%\'';
|
return $this->column . ' LIKE \'%' . addslashes($this->value) . '%\'';
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,78 +1,78 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database\Condition;
|
namespace Database\Condition;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class MathematicsCondition
|
* Class MathematicsCondition
|
||||||
* @package Database\Condition
|
* @package Database\Condition
|
||||||
*/
|
*/
|
||||||
class MathematicsCondition extends Condition
|
class MathematicsCondition extends Condition
|
||||||
{
|
{
|
||||||
|
|
||||||
public string $type = '';
|
public string $type = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function builder(): mixed
|
public function builder(): mixed
|
||||||
{
|
{
|
||||||
return $this->{strtolower($this->type)}((float)$this->value);
|
return $this->{strtolower($this->type)}((float)$this->value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $value
|
* @param $value
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function eq($value): string
|
public function eq($value): string
|
||||||
{
|
{
|
||||||
return $this->column . ' = ' . $value;
|
return $this->column . ' = ' . $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $value
|
* @param $value
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function neq($value): string
|
public function neq($value): string
|
||||||
{
|
{
|
||||||
return $this->column . ' <> ' . $value;
|
return $this->column . ' <> ' . $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $value
|
* @param $value
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function gt($value): string
|
public function gt($value): string
|
||||||
{
|
{
|
||||||
return $this->column . ' > ' . $value;
|
return $this->column . ' > ' . $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $value
|
* @param $value
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function egt($value): string
|
public function egt($value): string
|
||||||
{
|
{
|
||||||
return $this->column . ' >= ' . $value;
|
return $this->column . ' >= ' . $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $value
|
* @param $value
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function lt($value): string
|
public function lt($value): string
|
||||||
{
|
{
|
||||||
return $this->column . ' < ' . $value;
|
return $this->column . ' < ' . $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $value
|
* @param $value
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function elt($value): string
|
public function elt($value): string
|
||||||
{
|
{
|
||||||
return $this->column . ' <= ' . $value;
|
return $this->column . ' <= ' . $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,22 +1,22 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database\Condition;
|
namespace Database\Condition;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class NotBetweenCondition
|
* Class NotBetweenCondition
|
||||||
* @package Database\Condition
|
* @package Database\Condition
|
||||||
*/
|
*/
|
||||||
class NotBetweenCondition extends Condition
|
class NotBetweenCondition extends Condition
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function builder(): string
|
public function builder(): string
|
||||||
{
|
{
|
||||||
return $this->column . ' NOT BETWEEN ' . (int)$this->value[0] . ' AND ' . (int)$this->value[1];
|
return $this->column . ' NOT BETWEEN ' . (int)$this->value[0] . ' AND ' . (int)$this->value[1];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,28 +1,28 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database\Condition;
|
namespace Database\Condition;
|
||||||
|
|
||||||
use JetBrains\PhpStorm\Pure;
|
use JetBrains\PhpStorm\Pure;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class NotInCondition
|
* Class NotInCondition
|
||||||
* @package Database\Condition
|
* @package Database\Condition
|
||||||
*/
|
*/
|
||||||
class NotInCondition extends Condition
|
class NotInCondition extends Condition
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string|null
|
* @return string|null
|
||||||
*/
|
*/
|
||||||
#[Pure] public function builder(): ?string
|
#[Pure] public function builder(): ?string
|
||||||
{
|
{
|
||||||
if (!is_array($this->value)) {
|
if (!is_array($this->value)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
$value = '\'' . implode('\',\'', $this->value) . '\'';
|
$value = '\'' . implode('\',\'', $this->value) . '\'';
|
||||||
return '`' . $this->column . '` not in(' . $value . ')';
|
return '`' . $this->column . '` not in(' . $value . ')';
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,28 +1,28 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database\Condition;
|
namespace Database\Condition;
|
||||||
|
|
||||||
use Kiri\Core\Str;
|
use Kiri\Core\Str;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class NotLikeCondition
|
* Class NotLikeCondition
|
||||||
* @package Database\Condition
|
* @package Database\Condition
|
||||||
*/
|
*/
|
||||||
class NotLikeCondition extends Condition
|
class NotLikeCondition extends Condition
|
||||||
{
|
{
|
||||||
|
|
||||||
public string $pos = '';
|
public string $pos = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function builder(): string
|
public function builder(): string
|
||||||
{
|
{
|
||||||
if (!is_string($this->value)) {
|
if (!is_string($this->value)) {
|
||||||
$this->value = array_shift($this->value);
|
$this->value = array_shift($this->value);
|
||||||
}
|
}
|
||||||
return $this->column . ' NOT LIKE \'%' . addslashes($this->value) . '%\'';
|
return $this->column . ' NOT LIKE \'%' . addslashes($this->value) . '%\'';
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,27 +1,27 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database\Condition;
|
namespace Database\Condition;
|
||||||
|
|
||||||
|
|
||||||
use JetBrains\PhpStorm\Pure;
|
use JetBrains\PhpStorm\Pure;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class OrCondition
|
* Class OrCondition
|
||||||
* @package Database\Condition
|
* @package Database\Condition
|
||||||
*/
|
*/
|
||||||
class OrCondition extends Condition
|
class OrCondition extends Condition
|
||||||
{
|
{
|
||||||
|
|
||||||
public array $oldParams = [];
|
public array $oldParams = [];
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
#[Pure] public function builder(): string
|
#[Pure] public function builder(): string
|
||||||
{
|
{
|
||||||
return sprintf('(%s) OR %s', implode(' AND ', $this->oldParams), addslashes($this->value));
|
return sprintf('(%s) OR %s', implode(' AND ', $this->oldParams), addslashes($this->value));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,28 +1,28 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database\Condition;
|
namespace Database\Condition;
|
||||||
|
|
||||||
use Kiri\Core\Str;
|
use Kiri\Core\Str;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class RLikeCondition
|
* Class RLikeCondition
|
||||||
* @package Database\Condition
|
* @package Database\Condition
|
||||||
*/
|
*/
|
||||||
class RLikeCondition extends Condition
|
class RLikeCondition extends Condition
|
||||||
{
|
{
|
||||||
|
|
||||||
public string $pos = '';
|
public string $pos = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function builder(): string
|
public function builder(): string
|
||||||
{
|
{
|
||||||
if (!is_string($this->value)) {
|
if (!is_string($this->value)) {
|
||||||
$this->value = array_shift($this->value);
|
$this->value = array_shift($this->value);
|
||||||
}
|
}
|
||||||
return sprintf('%s LIKE \'%s\'', $this->column, addslashes($this->value));
|
return sprintf('%s LIKE \'%s\'', $this->column, addslashes($this->value));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
+333
-352
@@ -1,352 +1,333 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Created by PhpStorm.
|
* Created by PhpStorm.
|
||||||
* User: whwyy
|
* User: whwyy
|
||||||
* Date: 2018/3/30 0030
|
* Date: 2018/3/30 0030
|
||||||
* Time: 14:09
|
* Time: 14:09
|
||||||
*/
|
*/
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
|
||||||
namespace Database;
|
namespace Database;
|
||||||
|
|
||||||
|
|
||||||
use Annotation\Inject;
|
use Database\Affair\BeginTransaction;
|
||||||
use Database\Affair\BeginTransaction;
|
use Database\Affair\Commit;
|
||||||
use Database\Affair\Commit;
|
use Database\Affair\Rollback;
|
||||||
use Database\Affair\Rollback;
|
use Database\Mysql\PDO;
|
||||||
use Database\Mysql\PDO;
|
use Database\Mysql\Schema;
|
||||||
use Database\Mysql\Schema;
|
use Exception;
|
||||||
use Exception;
|
use Kiri\Abstracts\Component;
|
||||||
use Kiri\Abstracts\Component;
|
use Kiri\Abstracts\Config;
|
||||||
use Kiri\Abstracts\Config;
|
use Kiri\Events\EventProvider;
|
||||||
use Kiri\Events\EventProvider;
|
use Kiri\Exception\NotFindClassException;
|
||||||
use Kiri\Exception\NotFindClassException;
|
use Kiri\Kiri;
|
||||||
use Kiri\Kiri;
|
use Kiri\Annotation\Inject;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
use Server\Events\OnWorkerExit;
|
use Server\Events\OnWorkerExit;
|
||||||
use Server\Events\OnWorkerStop;
|
use Server\Events\OnWorkerStop;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Connection
|
* Class Connection
|
||||||
* @package Database
|
* @package Database
|
||||||
*/
|
*/
|
||||||
class Connection extends Component
|
class Connection extends Component
|
||||||
{
|
{
|
||||||
|
|
||||||
public string $id = 'db';
|
public string $id = 'db';
|
||||||
public string $cds = '';
|
public string $cds = '';
|
||||||
public string $password = '';
|
public string $password = '';
|
||||||
public string $username = '';
|
public string $username = '';
|
||||||
public string $charset = 'utf-8';
|
public string $charset = 'utf-8';
|
||||||
public string $tablePrefix = '';
|
public string $tablePrefix = '';
|
||||||
|
|
||||||
public string $database = '';
|
public string $database = '';
|
||||||
|
|
||||||
public int $timeout = 1900;
|
public int $connect_timeout = 30;
|
||||||
|
|
||||||
/**
|
public int $read_timeout = 10;
|
||||||
* @var bool
|
|
||||||
* enable database cache
|
public array $pool;
|
||||||
*/
|
|
||||||
public bool $enableCache = false;
|
/**
|
||||||
|
* @var bool
|
||||||
|
* enable database cache
|
||||||
/**
|
*/
|
||||||
* @var string
|
public bool $enableCache = false;
|
||||||
*/
|
|
||||||
public string $cacheDriver = 'redis';
|
|
||||||
|
/**
|
||||||
/**
|
* @var string
|
||||||
* @var array
|
*/
|
||||||
*/
|
public string $cacheDriver = 'redis';
|
||||||
public array $slaveConfig = [];
|
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
/**
|
*/
|
||||||
* @var Schema
|
public array $slaveConfig = [];
|
||||||
*/
|
public array $attributes = [];
|
||||||
#[Inject(Schema::class)]
|
|
||||||
public Schema $_schema;
|
|
||||||
|
private ?Schema $_schema = null;
|
||||||
|
|
||||||
/**
|
|
||||||
* @var EventProvider
|
/**
|
||||||
*/
|
* execute by __construct
|
||||||
#[Inject(EventProvider::class)]
|
* @throws Exception
|
||||||
public EventProvider $eventProvider;
|
*/
|
||||||
|
public function init()
|
||||||
|
{
|
||||||
/**
|
$this->eventProvider->on(OnWorkerStop::class, [$this, 'clear_connection'], 0);
|
||||||
* execute by __construct
|
$this->eventProvider->on(OnWorkerExit::class, [$this, 'clear_connection'], 0);
|
||||||
* @throws Exception
|
$this->eventProvider->on(BeginTransaction::class, [$this, 'beginTransaction'], 0);
|
||||||
*/
|
$this->eventProvider->on(Rollback::class, [$this, 'rollback'], 0);
|
||||||
public function init()
|
$this->eventProvider->on(Commit::class, [$this, 'commit'], 0);
|
||||||
{
|
}
|
||||||
$this->eventProvider->on(OnWorkerStop::class, [$this, 'clear_connection'], 0);
|
|
||||||
$this->eventProvider->on(OnWorkerExit::class, [$this, 'clear_connection'], 0);
|
|
||||||
$this->eventProvider->on(BeginTransaction::class, [$this, 'beginTransaction'], 0);
|
/**
|
||||||
$this->eventProvider->on(Rollback::class, [$this, 'rollback'], 0);
|
* @param null $sql
|
||||||
$this->eventProvider->on(Commit::class, [$this, 'commit'], 0);
|
* @return PDO
|
||||||
|
* @throws Exception
|
||||||
if (Db::transactionsActive()) {
|
*/
|
||||||
$this->beginTransaction();
|
public function getConnect($sql = NULL): PDO
|
||||||
}
|
{
|
||||||
|
return $this->getPdo($sql);
|
||||||
$this->_schema->db = $this;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
/**
|
||||||
/**
|
* @throws Exception
|
||||||
* @param null $sql
|
*/
|
||||||
* @return PDO
|
public function fill()
|
||||||
* @throws Exception
|
{
|
||||||
*/
|
$connections = $this->connections();
|
||||||
public function getConnect($sql = NULL): PDO
|
$pool = Config::get('databases.pool.max', 10);
|
||||||
{
|
|
||||||
return $this->getPdo($sql);
|
$connections->initConnections('Mysql:' . $this->cds, true, $pool);
|
||||||
}
|
if (!empty($this->slaveConfig) && $this->cds != $this->slaveConfig['cds']) {
|
||||||
|
$connections->initConnections('Mysql:' . $this->slaveConfig['cds'], false, $pool);
|
||||||
|
}
|
||||||
/**
|
}
|
||||||
* @param $config
|
|
||||||
* @return $this
|
|
||||||
*/
|
/**
|
||||||
public function configure($config): static
|
* @param $sql
|
||||||
{
|
* @return PDO
|
||||||
Kiri::configure($this, $config);
|
* @throws Exception
|
||||||
return $this;
|
*/
|
||||||
}
|
private function getPdo($sql): PDO
|
||||||
|
{
|
||||||
|
if ($this->isWrite($sql)) {
|
||||||
/**
|
return $this->masterInstance();
|
||||||
* @throws Exception
|
} else {
|
||||||
*/
|
return $this->slaveInstance();
|
||||||
public function fill()
|
}
|
||||||
{
|
}
|
||||||
$connections = $this->connections();
|
|
||||||
$pool = Config::get('databases.pool.max', 10);
|
/**
|
||||||
|
* @return mixed
|
||||||
$connections->initConnections('Mysql:' . $this->cds, true, $pool);
|
* @throws ReflectionException
|
||||||
if (!empty($this->slaveConfig) && $this->cds != $this->slaveConfig['cds']) {
|
* @throws NotFindClassException
|
||||||
$connections->initConnections('Mysql:' . $this->slaveConfig['cds'], false, $pool);
|
* @throws Exception
|
||||||
}
|
*/
|
||||||
}
|
public function getSchema(): Schema
|
||||||
|
{
|
||||||
|
if ($this->_schema === null) {
|
||||||
/**
|
$this->_schema = Kiri::createObject([
|
||||||
* @param $sql
|
'class' => Schema::class,
|
||||||
* @return PDO
|
'db' => $this
|
||||||
* @throws Exception
|
]);
|
||||||
*/
|
}
|
||||||
private function getPdo($sql): PDO
|
return $this->_schema;
|
||||||
{
|
}
|
||||||
if ($this->isWrite($sql)) {
|
|
||||||
return $this->masterInstance();
|
/**
|
||||||
} else {
|
* @param $sql
|
||||||
return $this->slaveInstance();
|
* @return bool
|
||||||
}
|
*/
|
||||||
}
|
public function isWrite($sql): bool
|
||||||
|
{
|
||||||
/**
|
if (empty($sql)) return false;
|
||||||
* @return mixed
|
if (str_starts_with(strtolower($sql), 'select')) {
|
||||||
* @throws ReflectionException
|
return false;
|
||||||
* @throws NotFindClassException
|
}
|
||||||
* @throws Exception
|
return true;
|
||||||
*/
|
}
|
||||||
public function getSchema(): Schema
|
|
||||||
{
|
/**
|
||||||
if ($this->_schema === null) {
|
* @return mixed
|
||||||
$this->_schema = Kiri::createObject([
|
* @throws Exception
|
||||||
'class' => Schema::class,
|
*/
|
||||||
'db' => $this
|
public function getCacheDriver(): mixed
|
||||||
]);
|
{
|
||||||
}
|
if (!$this->enableCache) {
|
||||||
return $this->_schema;
|
return null;
|
||||||
}
|
}
|
||||||
|
return Kiri::app()->get($this->cacheDriver);
|
||||||
/**
|
}
|
||||||
* @param $sql
|
|
||||||
* @return bool
|
/**
|
||||||
*/
|
* @return PDO
|
||||||
public function isWrite($sql): bool
|
* @throws Exception
|
||||||
{
|
*/
|
||||||
if (empty($sql)) return false;
|
public function masterInstance(): PDO
|
||||||
if (str_starts_with(strtolower($sql), 'select')) {
|
{
|
||||||
return false;
|
return $this->connections()->get([
|
||||||
}
|
'cds' => $this->cds,
|
||||||
return true;
|
'username' => $this->username,
|
||||||
}
|
'password' => $this->password,
|
||||||
|
'attributes' => $this->attributes,
|
||||||
/**
|
'connect_timeout' => $this->connect_timeout,
|
||||||
* @return mixed
|
'read_timeout' => $this->read_timeout,
|
||||||
* @throws Exception
|
'dbname' => $this->database,
|
||||||
*/
|
'pool' => $this->pool
|
||||||
public function getCacheDriver(): mixed
|
], true);
|
||||||
{
|
}
|
||||||
if (!$this->enableCache) {
|
|
||||||
return null;
|
/**
|
||||||
}
|
* @return PDO
|
||||||
return Kiri::app()->get($this->cacheDriver);
|
* @throws Exception
|
||||||
}
|
*/
|
||||||
|
public function slaveInstance(): PDO
|
||||||
/**
|
{
|
||||||
* @return PDO
|
if (empty($this->slaveConfig) || Db::transactionsActive()) {
|
||||||
* @throws Exception
|
return $this->masterInstance();
|
||||||
*/
|
}
|
||||||
public function masterInstance(): PDO
|
if ($this->slaveConfig['cds'] == $this->cds) {
|
||||||
{
|
return $this->masterInstance();
|
||||||
return $this->connections()->get([
|
}
|
||||||
'cds' => $this->cds,
|
return $this->connections()->get($this->slaveConfig, false);
|
||||||
'username' => $this->username,
|
}
|
||||||
'password' => $this->password,
|
|
||||||
'database' => $this->database
|
|
||||||
], true);
|
/**
|
||||||
}
|
* @return \Kiri\Pool\Connection
|
||||||
|
* @throws Exception
|
||||||
/**
|
*/
|
||||||
* @return PDO
|
private function connections(): \Kiri\Pool\Connection
|
||||||
* @throws Exception
|
{
|
||||||
*/
|
return Kiri::getDi()->get(\Kiri\Pool\Connection::class);
|
||||||
public function slaveInstance(): PDO
|
}
|
||||||
{
|
|
||||||
if (empty($this->slaveConfig) || Db::transactionsActive()) {
|
|
||||||
return $this->masterInstance();
|
/**
|
||||||
}
|
* @return $this
|
||||||
if ($this->slaveConfig['cds'] == $this->cds) {
|
* @throws Exception
|
||||||
return $this->masterInstance();
|
*/
|
||||||
}
|
public function beginTransaction(): static
|
||||||
return $this->connections()->get($this->slaveConfig, false);
|
{
|
||||||
}
|
$this->connections()->beginTransaction($this->cds);
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
/**
|
|
||||||
* @return \Kiri\Pool\Connection
|
/**
|
||||||
* @throws Exception
|
* @return $this|bool
|
||||||
*/
|
* @throws Exception
|
||||||
private function connections(): \Kiri\Pool\Connection
|
*/
|
||||||
{
|
public function inTransaction(): bool|static
|
||||||
return Kiri::getDi()->get(\Kiri\Pool\Connection::class);
|
{
|
||||||
}
|
return $this->connections()->inTransaction($this->cds);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return $this
|
* @throws Exception
|
||||||
* @throws Exception
|
* 事务回滚
|
||||||
*/
|
*/
|
||||||
public function beginTransaction(): static
|
public function rollback()
|
||||||
{
|
{
|
||||||
$this->connections()->beginTransaction($this->cds);
|
$this->connections()->rollback($this->cds);
|
||||||
return $this;
|
$this->release();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return $this|bool
|
* @throws Exception
|
||||||
* @throws Exception
|
* 事务提交
|
||||||
*/
|
*/
|
||||||
public function inTransaction(): bool|static
|
public function commit()
|
||||||
{
|
{
|
||||||
return $this->connections()->inTransaction($this->cds);
|
$this->connections()->commit($this->cds);
|
||||||
}
|
$this->release();
|
||||||
|
}
|
||||||
/**
|
|
||||||
* @throws Exception
|
/**
|
||||||
* 事务回滚
|
* @param $sql
|
||||||
*/
|
* @return PDO
|
||||||
public function rollback()
|
* @throws Exception
|
||||||
{
|
*/
|
||||||
$this->connections()->rollback($this->cds);
|
public function refresh($sql): PDO
|
||||||
$this->release();
|
{
|
||||||
}
|
if ($this->isWrite($sql)) {
|
||||||
|
$instance = $this->masterInstance();
|
||||||
/**
|
} else {
|
||||||
* @throws Exception
|
$instance = $this->slaveInstance();
|
||||||
* 事务提交
|
}
|
||||||
*/
|
return $instance;
|
||||||
public function commit()
|
}
|
||||||
{
|
|
||||||
$this->connections()->commit($this->cds);
|
/**
|
||||||
$this->release();
|
* @param null $sql
|
||||||
}
|
* @param array $attributes
|
||||||
|
* @return Command
|
||||||
/**
|
* @throws Exception
|
||||||
* @param $sql
|
*/
|
||||||
* @return PDO
|
public function createCommand($sql = null, array $attributes = []): Command
|
||||||
* @throws Exception
|
{
|
||||||
*/
|
$command = new Command(['db' => $this, 'sql' => $sql]);
|
||||||
public function refresh($sql): PDO
|
return $command->bindValues($attributes);
|
||||||
{
|
}
|
||||||
if ($this->isWrite($sql)) {
|
|
||||||
$instance = $this->masterInstance();
|
|
||||||
} else {
|
/**
|
||||||
$instance = $this->slaveInstance();
|
*
|
||||||
}
|
* 回收链接
|
||||||
return $instance;
|
* @throws
|
||||||
}
|
*/
|
||||||
|
public function release()
|
||||||
/**
|
{
|
||||||
* @param null $sql
|
if (!Kiri::isWorker() && !Kiri::isProcess()) {
|
||||||
* @param array $attributes
|
$this->clear_connection();
|
||||||
* @return Command
|
return;
|
||||||
* @throws Exception
|
}
|
||||||
*/
|
$connections = $this->connections();
|
||||||
public function createCommand($sql = null, array $attributes = []): Command
|
$connections->release($this->cds, true);
|
||||||
{
|
$connections->release($this->slaveConfig['cds'], false);
|
||||||
$command = new Command(['db' => $this, 'sql' => $sql]);
|
}
|
||||||
return $command->bindValues($attributes);
|
|
||||||
}
|
|
||||||
|
/**
|
||||||
|
* @throws Exception
|
||||||
/**
|
*/
|
||||||
*
|
public function recovery()
|
||||||
* 回收链接
|
{
|
||||||
* @throws
|
$connections = $this->connections();
|
||||||
*/
|
|
||||||
public function release()
|
$connections->release($this->cds, true);
|
||||||
{
|
$connections->release($this->slaveConfig['cds'], false);
|
||||||
if (!Kiri::isWorker() && !Kiri::isProcess()) {
|
}
|
||||||
$this->clear_connection();
|
|
||||||
return;
|
/**
|
||||||
}
|
*
|
||||||
$connections = $this->connections();
|
* 回收链接
|
||||||
$connections->release($this->cds, true);
|
* @throws
|
||||||
$connections->release($this->slaveConfig['cds'], false);
|
*/
|
||||||
}
|
public function clear_connection()
|
||||||
|
{
|
||||||
|
$connections = $this->connections();
|
||||||
/**
|
|
||||||
* @throws Exception
|
$connections->connection_clear($this->cds, true);
|
||||||
*/
|
$connections->connection_clear($this->slaveConfig['cds'], false);
|
||||||
public function recovery()
|
}
|
||||||
{
|
|
||||||
$connections = $this->connections();
|
|
||||||
|
/**
|
||||||
$connections->release($this->cds, true);
|
* @throws Exception
|
||||||
$connections->release($this->slaveConfig['cds'], false);
|
*/
|
||||||
}
|
public function disconnect()
|
||||||
|
{
|
||||||
/**
|
$connections = $this->connections();
|
||||||
*
|
$connections->disconnect($this->cds, true);
|
||||||
* 回收链接
|
$connections->disconnect($this->slaveConfig['cds'], false);
|
||||||
* @throws
|
}
|
||||||
*/
|
|
||||||
public function clear_connection()
|
}
|
||||||
{
|
|
||||||
$connections = $this->connections();
|
|
||||||
|
|
||||||
$connections->connection_clear($this->cds, true);
|
|
||||||
$connections->connection_clear($this->slaveConfig['cds'], false);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function disconnect()
|
|
||||||
{
|
|
||||||
$connections = $this->connections();
|
|
||||||
$connections->disconnect($this->cds, true);
|
|
||||||
$connections->disconnect($this->slaveConfig['cds'], false);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
<?php
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Database;
|
||||||
|
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use Kiri\Abstracts\Config;
|
||||||
|
use Kiri\Abstracts\Providers;
|
||||||
|
use Kiri\Application;
|
||||||
|
use Kiri\Exception\ConfigException;
|
||||||
|
use Kiri\Kiri;
|
||||||
|
use Server\Events\OnWorkerStart;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class DatabasesProviders
|
||||||
|
* @package Database
|
||||||
|
*/
|
||||||
|
class DatabasesProviders extends Providers
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Application $application
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function onImport(Application $application)
|
||||||
|
{
|
||||||
|
$this->eventProvider->on(OnWorkerStart::class, [$this, 'createPool']);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $name
|
||||||
|
* @return Connection
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function get($name): Connection
|
||||||
|
{
|
||||||
|
return Kiri::app()->get($name);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws ConfigException
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function createPool(OnWorkerStart $onWorkerStart)
|
||||||
|
{
|
||||||
|
$databases = Config::get('databases.connections', []);
|
||||||
|
if (empty($databases)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$app = Kiri::app();
|
||||||
|
foreach ($databases as $key => $database) {
|
||||||
|
$database = $this->_settings($database);
|
||||||
|
|
||||||
|
$connection = Kiri::getDi()->create(Connection::class, [$database]);
|
||||||
|
$connection->fill();
|
||||||
|
|
||||||
|
$app->set($key, $connection);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $database
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function _settings($database): array
|
||||||
|
{
|
||||||
|
$clientPool = $database['pool'] ?? ['min' => 1, 'max' => 5, 'tick' => 60];
|
||||||
|
return [
|
||||||
|
'id' => $database['id'],
|
||||||
|
'cds' => $database['cds'],
|
||||||
|
'username' => $database['username'],
|
||||||
|
'password' => $database['password'],
|
||||||
|
'tablePrefix' => $database['tablePrefix'],
|
||||||
|
'database' => $database['database'],
|
||||||
|
'connect_timeout' => $database['connect_timeout'] ?? 30,
|
||||||
|
'read_timeout' => $database['read_timeout'] ?? 10,
|
||||||
|
'pool' => $clientPool,
|
||||||
|
'attributes' => $database['attributes'] ?? [],
|
||||||
|
'charset' => $database['charset'] ?? 'utf8mb4',
|
||||||
|
'slaveConfig' => $database['slaveConfig']
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
+365
-365
@@ -1,365 +1,365 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Created by PhpStorm.
|
* Created by PhpStorm.
|
||||||
* User: whwyy
|
* User: whwyy
|
||||||
* Date: 2018/4/4 0004
|
* Date: 2018/4/4 0004
|
||||||
* Time: 15:40
|
* Time: 15:40
|
||||||
*/
|
*/
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database;
|
namespace Database;
|
||||||
|
|
||||||
use Database\Affair\BeginTransaction;
|
use Database\Affair\BeginTransaction;
|
||||||
use Database\Affair\Commit;
|
use Database\Affair\Commit;
|
||||||
use Database\Affair\Rollback;
|
use Database\Affair\Rollback;
|
||||||
use Database\Traits\QueryTrait;
|
use Database\Traits\QueryTrait;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Kiri\Abstracts\Config;
|
use Kiri\Abstracts\Config;
|
||||||
use Kiri\Events\EventDispatch;
|
use Kiri\Events\EventDispatch;
|
||||||
use Kiri\Exception\ConfigException;
|
use Kiri\Exception\ConfigException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Db
|
* Class Db
|
||||||
* @package Database
|
* @package Database
|
||||||
*/
|
*/
|
||||||
class Db implements ISqlBuilder
|
class Db implements ISqlBuilder
|
||||||
{
|
{
|
||||||
use QueryTrait;
|
use QueryTrait;
|
||||||
|
|
||||||
|
|
||||||
private static bool $_inTransaction = false;
|
private static bool $_inTransaction = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
public static function transactionsActive(): bool
|
public static function transactionsActive(): bool
|
||||||
{
|
{
|
||||||
return static::$_inTransaction === true;
|
return static::$_inTransaction === true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static function beginTransaction()
|
public static function beginTransaction()
|
||||||
{
|
{
|
||||||
if (!static::transactionsActive()) {
|
if (!static::transactionsActive()) {
|
||||||
di(EventDispatch::class)->dispatch(new BeginTransaction());
|
di(EventDispatch::class)->dispatch(new BeginTransaction());
|
||||||
}
|
}
|
||||||
static::$_inTransaction = true;
|
static::$_inTransaction = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static function commit()
|
public static function commit()
|
||||||
{
|
{
|
||||||
if (static::transactionsActive()) {
|
if (static::transactionsActive()) {
|
||||||
di(EventDispatch::class)->dispatch(new Commit());
|
di(EventDispatch::class)->dispatch(new Commit());
|
||||||
}
|
}
|
||||||
static::$_inTransaction = false;
|
static::$_inTransaction = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static function rollback()
|
public static function rollback()
|
||||||
{
|
{
|
||||||
if (static::transactionsActive()) {
|
if (static::transactionsActive()) {
|
||||||
di(EventDispatch::class)->dispatch(new Rollback());
|
di(EventDispatch::class)->dispatch(new Rollback());
|
||||||
}
|
}
|
||||||
static::$_inTransaction = false;
|
static::$_inTransaction = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $table
|
* @param $table
|
||||||
*
|
*
|
||||||
* @return static
|
* @return static
|
||||||
*/
|
*/
|
||||||
public static function table($table): Db|static
|
public static function table($table): Db|static
|
||||||
{
|
{
|
||||||
$connection = new Db();
|
$connection = new Db();
|
||||||
$connection->from($table);
|
$connection->from($table);
|
||||||
return $connection;
|
return $connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $column
|
* @param string $column
|
||||||
* @param string $alias
|
* @param string $alias
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function any_value(string $column, string $alias = ''): string
|
public static function any_value(string $column, string $alias = ''): string
|
||||||
{
|
{
|
||||||
if (empty($alias)) {
|
if (empty($alias)) {
|
||||||
$alias = $column . '_any_value';
|
$alias = $column . '_any_value';
|
||||||
}
|
}
|
||||||
return 'ANY_VALUE(' . $column . ') as ' . $alias;
|
return 'ANY_VALUE(' . $column . ') as ' . $alias;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $column
|
* @param string $column
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function increment(string $column): string
|
public static function increment(string $column): string
|
||||||
{
|
{
|
||||||
return '+ ' . $column;
|
return '+ ' . $column;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $column
|
* @param string $column
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function decrement(string $column): string
|
public static function decrement(string $column): string
|
||||||
{
|
{
|
||||||
return '- ' . $column;
|
return '- ' . $column;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Connection|null $connection
|
* @param Connection|null $connection
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function get(Connection $connection = NULL): mixed
|
public function get(Connection $connection = NULL): mixed
|
||||||
{
|
{
|
||||||
$connection = static::getDefaultConnection($connection);
|
$connection = static::getDefaultConnection($connection);
|
||||||
|
|
||||||
return $connection->createCommand(SqlBuilder::builder($this)->one())
|
return $connection->createCommand(SqlBuilder::builder($this)->one())
|
||||||
->all();
|
->all();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $column
|
* @param $column
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public static function raw($column): string
|
public static function raw($column): string
|
||||||
{
|
{
|
||||||
return '`' . $column . '`';
|
return '`' . $column . '`';
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Connection|null $connection
|
* @param Connection|null $connection
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function find(Connection $connection = NULL): mixed
|
public function find(Connection $connection = NULL): mixed
|
||||||
{
|
{
|
||||||
$connection = static::getDefaultConnection($connection);
|
$connection = static::getDefaultConnection($connection);
|
||||||
|
|
||||||
return $connection->createCommand(SqlBuilder::builder($this)->all())
|
return $connection->createCommand(SqlBuilder::builder($this)->all())
|
||||||
->one();
|
->one();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Connection|NULL $connection
|
* @param Connection|NULL $connection
|
||||||
* @return bool|int
|
* @return bool|int
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function count(Connection $connection = NULL): bool|int
|
public function count(Connection $connection = NULL): bool|int
|
||||||
{
|
{
|
||||||
$connection = static::getDefaultConnection($connection);
|
$connection = static::getDefaultConnection($connection);
|
||||||
|
|
||||||
return $connection->createCommand(SqlBuilder::builder($this)->count())
|
return $connection->createCommand(SqlBuilder::builder($this)->count())
|
||||||
->exec();
|
->exec();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Connection|NULL $connection
|
* @param Connection|NULL $connection
|
||||||
* @return bool|int
|
* @return bool|int
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function exists(Connection $connection = NULL): bool|int
|
public function exists(Connection $connection = NULL): bool|int
|
||||||
{
|
{
|
||||||
$connection = static::getDefaultConnection($connection);
|
$connection = static::getDefaultConnection($connection);
|
||||||
|
|
||||||
return $connection->createCommand(SqlBuilder::builder($this)->one())
|
return $connection->createCommand(SqlBuilder::builder($this)->one())
|
||||||
->fetchColumn();
|
->fetchColumn();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $sql
|
* @param string $sql
|
||||||
* @param array $attributes
|
* @param array $attributes
|
||||||
* @param Connection|null $connection
|
* @param Connection|null $connection
|
||||||
* @return array|bool|int|string|null
|
* @return array|bool|int|string|null
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static function findAllBySql(string $sql, array $attributes = [], Connection $connection = NULL): int|bool|array|string|null
|
public static function findAllBySql(string $sql, array $attributes = [], Connection $connection = NULL): int|bool|array|string|null
|
||||||
{
|
{
|
||||||
$connection = static::getDefaultConnection($connection);
|
$connection = static::getDefaultConnection($connection);
|
||||||
|
|
||||||
return $connection->createCommand($sql, $attributes)->all();
|
return $connection->createCommand($sql, $attributes)->all();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $sql
|
* @param string $sql
|
||||||
* @param array $attributes
|
* @param array $attributes
|
||||||
* @param Connection|NULL $connection
|
* @param Connection|NULL $connection
|
||||||
* @return string|array|bool|int|null
|
* @return string|array|bool|int|null
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static function findBySql(string $sql, array $attributes = [], Connection $connection = NULL): string|array|bool|int|null
|
public static function findBySql(string $sql, array $attributes = [], Connection $connection = NULL): string|array|bool|int|null
|
||||||
{
|
{
|
||||||
$connection = static::getDefaultConnection($connection);
|
$connection = static::getDefaultConnection($connection);
|
||||||
|
|
||||||
return $connection->createCommand($sql, $attributes)->one();
|
return $connection->createCommand($sql, $attributes)->one();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $field
|
* @param string $field
|
||||||
* @return array|null
|
* @return array|null
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function values(string $field): ?array
|
public function values(string $field): ?array
|
||||||
{
|
{
|
||||||
$data = $this->get();
|
$data = $this->get();
|
||||||
if (empty($data) || empty($field)) {
|
if (empty($data) || empty($field)) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
$first = current($data);
|
$first = current($data);
|
||||||
if (!isset($first[$field])) {
|
if (!isset($first[$field])) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return array_column($data, $field);
|
return array_column($data, $field);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $field
|
* @param $field
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function value($field): mixed
|
public function value($field): mixed
|
||||||
{
|
{
|
||||||
$data = $this->find();
|
$data = $this->find();
|
||||||
if (!empty($field) && isset($data[$field])) {
|
if (!empty($field) && isset($data[$field])) {
|
||||||
return $data[$field];
|
return $data[$field];
|
||||||
}
|
}
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Connection|null $connection
|
* @param Connection|null $connection
|
||||||
* @return bool|int
|
* @return bool|int
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function delete(?Connection $connection = null): bool|int
|
public function delete(?Connection $connection = null): bool|int
|
||||||
{
|
{
|
||||||
$connection = static::getDefaultConnection($connection);
|
$connection = static::getDefaultConnection($connection);
|
||||||
|
|
||||||
return $connection->createCommand($connection->getBuild()->builder($this))->delete();
|
return $connection->createCommand($connection->getBuild()->builder($this))->delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $table
|
* @param string $table
|
||||||
* @param null $connection
|
* @param null $connection
|
||||||
* @return bool|int
|
* @return bool|int
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static function drop(string $table, $connection = null): bool|int
|
public static function drop(string $table, $connection = null): bool|int
|
||||||
{
|
{
|
||||||
$connection = static::getDefaultConnection($connection);
|
$connection = static::getDefaultConnection($connection);
|
||||||
|
|
||||||
$sprint = sprintf('DROP TABLE `%s`.`%s`', $connection->database, $table);
|
$sprint = sprintf('DROP TABLE `%s`.`%s`', $connection->database, $table);
|
||||||
return $connection->createCommand($sprint)->delete();
|
return $connection->createCommand($sprint)->delete();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $table
|
* @param string $table
|
||||||
* @param null $connection
|
* @param null $connection
|
||||||
* @return bool|int
|
* @return bool|int
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static function truncate(string $table, $connection = null): bool|int
|
public static function truncate(string $table, $connection = null): bool|int
|
||||||
{
|
{
|
||||||
$connection = static::getDefaultConnection($connection);
|
$connection = static::getDefaultConnection($connection);
|
||||||
|
|
||||||
$sprint = sprintf('TRUNCATE `%s`.`%s`', $connection->database, $table);
|
$sprint = sprintf('TRUNCATE `%s`.`%s`', $connection->database, $table);
|
||||||
return $connection->createCommand($sprint)->exec();
|
return $connection->createCommand($sprint)->exec();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $table
|
* @param string $table
|
||||||
* @param Connection|NULL $connection
|
* @param Connection|NULL $connection
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static function showCreateSql(string $table, Connection $connection = NULL): mixed
|
public static function showCreateSql(string $table, Connection $connection = NULL): mixed
|
||||||
{
|
{
|
||||||
$connection = static::getDefaultConnection($connection);
|
$connection = static::getDefaultConnection($connection);
|
||||||
|
|
||||||
$sprint = sprintf('SHOW CREATE TABLE `%s`.`%s`', $connection->database, $table);
|
$sprint = sprintf('SHOW CREATE TABLE `%s`.`%s`', $connection->database, $table);
|
||||||
return $connection->createCommand($sprint)->one();
|
return $connection->createCommand($sprint)->one();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $table
|
* @param string $table
|
||||||
* @param Connection|NULL $connection
|
* @param Connection|NULL $connection
|
||||||
* @return bool|int|null
|
* @return bool|int|null
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static function desc(string $table, Connection $connection = NULL): bool|int|null
|
public static function desc(string $table, Connection $connection = NULL): bool|int|null
|
||||||
{
|
{
|
||||||
$connection = static::getDefaultConnection($connection);
|
$connection = static::getDefaultConnection($connection);
|
||||||
|
|
||||||
$sprint = sprintf('SHOW FULL FIELDS FROM `%s`.`%s`', $connection->database, $table);
|
$sprint = sprintf('SHOW FULL FIELDS FROM `%s`.`%s`', $connection->database, $table);
|
||||||
return $connection->createCommand($sprint)->all();
|
return $connection->createCommand($sprint)->all();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $table
|
* @param string $table
|
||||||
* @param Connection|NULL $connection
|
* @param Connection|NULL $connection
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static function show(string $table, Connection $connection = NULL): mixed
|
public static function show(string $table, Connection $connection = NULL): mixed
|
||||||
{
|
{
|
||||||
if (empty($table)) {
|
if (empty($table)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
$connection = static::getDefaultConnection($connection);
|
$connection = static::getDefaultConnection($connection);
|
||||||
|
|
||||||
$table = [' const TABLE = \'select * from %s where REFERENCED_TABLE_NAME=%s\';'];
|
$table = [' const TABLE = \'select * from %s where REFERENCED_TABLE_NAME=%s\';'];
|
||||||
return $connection->createCommand((new Query())
|
return $connection->createCommand((new Query())
|
||||||
->select('*')
|
->select('*')
|
||||||
->from('INFORMATION_SCHEMA.KEY_COLUMN_USAGE')
|
->from('INFORMATION_SCHEMA.KEY_COLUMN_USAGE')
|
||||||
->where(['REFERENCED_TABLE_NAME' => $table])
|
->where(['REFERENCED_TABLE_NAME' => $table])
|
||||||
->getSql())->one();
|
->getSql())->one();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param null|Connection $connection
|
* @param null|Connection $connection
|
||||||
* @param null $name
|
* @param null $name
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws ConfigException
|
* @throws ConfigException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static function getDefaultConnection(?Connection $connection, $name = null): Connection
|
public static function getDefaultConnection(?Connection $connection, $name = null): Connection
|
||||||
{
|
{
|
||||||
if ($connection instanceof Connection) {
|
if ($connection instanceof Connection) {
|
||||||
return $connection;
|
return $connection;
|
||||||
}
|
}
|
||||||
$databases = Config::get('databases.connections', []);
|
$databases = Config::get('databases.connections', []);
|
||||||
if (empty($databases) || !is_array($databases)) {
|
if (empty($databases) || !is_array($databases)) {
|
||||||
throw new Exception('Please configure the database link.');
|
throw new Exception('Please configure the database link.');
|
||||||
}
|
}
|
||||||
if (!empty($name)) {
|
if (!empty($name)) {
|
||||||
if (!isset($databases[$name])) {
|
if (!isset($databases[$name])) {
|
||||||
throw new Exception('Please configure the database link.');
|
throw new Exception('Please configure the database link.');
|
||||||
}
|
}
|
||||||
return $databases[$name];
|
return $databases[$name];
|
||||||
}
|
}
|
||||||
return current($databases);
|
return current($databases);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,39 +1,39 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database;
|
namespace Database;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Database\Traits\HasBase;
|
use Database\Traits\HasBase;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class HasCount
|
* Class HasCount
|
||||||
* @package Database
|
* @package Database
|
||||||
*/
|
*/
|
||||||
class HasCount extends HasBase
|
class HasCount extends HasBase
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $name
|
* @param $name
|
||||||
* @param $arguments
|
* @param $arguments
|
||||||
* @return ActiveQuery
|
* @return ActiveQuery
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function __call($name, $arguments): mixed
|
public function __call($name, $arguments): mixed
|
||||||
{
|
{
|
||||||
if (method_exists($this, $name)) {
|
if (method_exists($this, $name)) {
|
||||||
return call_user_func([$this, $name], ...$arguments);
|
return call_user_func([$this, $name], ...$arguments);
|
||||||
}
|
}
|
||||||
return $this->_relation->getQuery($this->model::className())->$name(...$arguments);
|
return $this->_relation->getQuery($this->model::className())->$name(...$arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array|null|ModelInterface
|
* @return array|null|ModelInterface
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function get(): array|ModelInterface|null
|
public function get(): array|ModelInterface|null
|
||||||
{
|
{
|
||||||
return $this->_relation->count($this->model::className(), $this->value);
|
return $this->_relation->count($this->model::className(), $this->value);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,44 +1,44 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Created by PhpStorm.
|
* Created by PhpStorm.
|
||||||
* User: whwyy
|
* User: whwyy
|
||||||
* Date: 2018/4/4 0004
|
* Date: 2018/4/4 0004
|
||||||
* Time: 13:58
|
* Time: 13:58
|
||||||
*/
|
*/
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
namespace Database;
|
namespace Database;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Database\Traits\HasBase;
|
use Database\Traits\HasBase;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class HasMany
|
* Class HasMany
|
||||||
* @package Database
|
* @package Database
|
||||||
*
|
*
|
||||||
* @method with($name)
|
* @method with($name)
|
||||||
*/
|
*/
|
||||||
class HasMany extends HasBase
|
class HasMany extends HasBase
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $name
|
* @param $name
|
||||||
* @param $arguments
|
* @param $arguments
|
||||||
* @return ActiveQuery
|
* @return ActiveQuery
|
||||||
*/
|
*/
|
||||||
public function __call($name, $arguments): mixed
|
public function __call($name, $arguments): mixed
|
||||||
{
|
{
|
||||||
if (method_exists($this, $name)) {
|
if (method_exists($this, $name)) {
|
||||||
return call_user_func([$this, $name], ...$arguments);
|
return call_user_func([$this, $name], ...$arguments);
|
||||||
}
|
}
|
||||||
return $this->_relation->getQuery($this->model::className())->$name(...$arguments);
|
return $this->_relation->getQuery($this->model::className())->$name(...$arguments);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array|null|ModelInterface
|
* @return array|null|ModelInterface
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function get(): array|ModelInterface|null
|
public function get(): array|ModelInterface|null
|
||||||
{
|
{
|
||||||
return $this->_relation->get($this->model::className(), $this->value);
|
return $this->_relation->get($this->model::className(), $this->value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+45
-45
@@ -1,45 +1,45 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Created by PhpStorm.
|
* Created by PhpStorm.
|
||||||
* User: whwyy
|
* User: whwyy
|
||||||
* Date: 2018/4/4 0004
|
* Date: 2018/4/4 0004
|
||||||
* Time: 13:47
|
* Time: 13:47
|
||||||
*/
|
*/
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database;
|
namespace Database;
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Database\Traits\HasBase;
|
use Database\Traits\HasBase;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class HasOne
|
* Class HasOne
|
||||||
* @package Database
|
* @package Database
|
||||||
* @internal Query
|
* @internal Query
|
||||||
*/
|
*/
|
||||||
class HasOne extends HasBase
|
class HasOne extends HasBase
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $name
|
* @param $name
|
||||||
* @param $arguments
|
* @param $arguments
|
||||||
* @return ActiveQuery
|
* @return ActiveQuery
|
||||||
*/
|
*/
|
||||||
public function __call($name, $arguments): mixed
|
public function __call($name, $arguments): mixed
|
||||||
{
|
{
|
||||||
if (method_exists($this, $name)) {
|
if (method_exists($this, $name)) {
|
||||||
return call_user_func([$this, $name], ...$arguments);
|
return call_user_func([$this, $name], ...$arguments);
|
||||||
}
|
}
|
||||||
$this->_relation->getQuery($this->model::className())->$name(...$arguments);
|
$this->_relation->getQuery($this->model::className())->$name(...$arguments);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array|null|ModelInterface
|
* @return array|null|ModelInterface
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function get(): array|ModelInterface|null
|
public function get(): array|ModelInterface|null
|
||||||
{
|
{
|
||||||
return $this->_relation->first($this->model::className(), $this->value);
|
return $this->_relation->first($this->model::className(), $this->value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
namespace Database;
|
namespace Database;
|
||||||
|
|
||||||
|
|
||||||
interface ISqlBuilder
|
interface ISqlBuilder
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
+422
-419
@@ -1,419 +1,422 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Created by PhpStorm.
|
* Created by PhpStorm.
|
||||||
* User: whwyy
|
* User: whwyy
|
||||||
* Date: 2018/3/30 0030
|
* Date: 2018/3/30 0030
|
||||||
* Time: 14:39
|
* Time: 14:39
|
||||||
*/
|
*/
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database;
|
namespace Database;
|
||||||
|
|
||||||
|
|
||||||
use Database\Base\Getter;
|
use Database\Base\Getter;
|
||||||
use Database\Traits\HasBase;
|
use Database\Traits\HasBase;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Kiri\Exception\NotFindClassException;
|
use Kiri\Exception\NotFindClassException;
|
||||||
use Kiri\Kiri;
|
use Kiri\Kiri;
|
||||||
use Kiri\ToArray;
|
use Kiri\ToArray;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
|
use Swoole\Coroutine;
|
||||||
defined('SAVE_FAIL') or define('SAVE_FAIL', 3227);
|
|
||||||
defined('FIND_OR_CREATE_MESSAGE') or define('FIND_OR_CREATE_MESSAGE', 'Create a new model, but the data cannot be empty.');
|
defined('SAVE_FAIL') or define('SAVE_FAIL', 3227);
|
||||||
|
defined('FIND_OR_CREATE_MESSAGE') or define('FIND_OR_CREATE_MESSAGE', 'Create a new model, but the data cannot be empty.');
|
||||||
/**
|
|
||||||
* Class Orm
|
/**
|
||||||
* @package Database
|
* Class Orm
|
||||||
*
|
* @package Database
|
||||||
* @property $attributes
|
*
|
||||||
* @property-read $oldAttributes
|
* @property $attributes
|
||||||
*/
|
* @property-read $oldAttributes
|
||||||
class Model extends Base\Model
|
*/
|
||||||
{
|
class Model extends Base\Model
|
||||||
|
{
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $column
|
/**
|
||||||
* @param int $value
|
* @param string $column
|
||||||
* @return ModelInterface|false
|
* @param int $value
|
||||||
* @throws Exception
|
* @return ModelInterface|false
|
||||||
*/
|
* @throws Exception
|
||||||
public function increment(string $column, int $value): bool|ModelInterface
|
*/
|
||||||
{
|
public function increment(string $column, int $value): bool|ModelInterface
|
||||||
if (!$this->mathematics([$column => $value], '+')) {
|
{
|
||||||
return false;
|
if (!$this->mathematics([$column => $value], '+')) {
|
||||||
}
|
return false;
|
||||||
$this->{$column} += $value;
|
}
|
||||||
return $this->refresh();
|
$this->{$column} += $value;
|
||||||
}
|
return $this->refresh();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $column
|
/**
|
||||||
* @param int $value
|
* @param string $column
|
||||||
* @return ModelInterface|false
|
* @param int $value
|
||||||
* @throws Exception
|
* @return ModelInterface|false
|
||||||
*/
|
* @throws Exception
|
||||||
public function decrement(string $column, int $value): bool|ModelInterface
|
*/
|
||||||
{
|
public function decrement(string $column, int $value): bool|ModelInterface
|
||||||
if (!$this->mathematics([$column => $value], '-')) {
|
{
|
||||||
return false;
|
if (!$this->mathematics([$column => $value], '-')) {
|
||||||
}
|
return false;
|
||||||
$this->{$column} -= $value;
|
}
|
||||||
return $this->refresh();
|
$this->{$column} -= $value;
|
||||||
}
|
return $this->refresh();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $columns
|
/**
|
||||||
* @return ModelInterface|false
|
* @param array $columns
|
||||||
* @throws Exception
|
* @return ModelInterface|false
|
||||||
*/
|
* @throws Exception
|
||||||
public function increments(array $columns): bool|static
|
*/
|
||||||
{
|
public function increments(array $columns): bool|static
|
||||||
if (!$this->mathematics($columns, '+')) {
|
{
|
||||||
return false;
|
if (!$this->mathematics($columns, '+')) {
|
||||||
}
|
return false;
|
||||||
foreach ($columns as $key => $attribute) {
|
}
|
||||||
$this->$key += $attribute;
|
foreach ($columns as $key => $attribute) {
|
||||||
}
|
$this->$key += $attribute;
|
||||||
return $this;
|
}
|
||||||
}
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $columns
|
/**
|
||||||
* @return ModelInterface|false
|
* @param array $columns
|
||||||
* @throws Exception
|
* @return ModelInterface|false
|
||||||
*/
|
* @throws Exception
|
||||||
public function decrements(array $columns): bool|static
|
*/
|
||||||
{
|
public function decrements(array $columns): bool|static
|
||||||
if (!$this->mathematics($columns, '-')) {
|
{
|
||||||
return false;
|
if (!$this->mathematics($columns, '-')) {
|
||||||
}
|
return false;
|
||||||
foreach ($columns as $key => $attribute) {
|
}
|
||||||
$this->$key -= $attribute;
|
foreach ($columns as $key => $attribute) {
|
||||||
}
|
$this->$key -= $attribute;
|
||||||
return $this;
|
}
|
||||||
}
|
return $this;
|
||||||
|
}
|
||||||
/**
|
|
||||||
* @param array $condition
|
/**
|
||||||
* @param array $attributes
|
* @param array $condition
|
||||||
* @return bool|ModelInterface
|
* @param array $attributes
|
||||||
* @throws ReflectionException
|
* @return bool|ModelInterface
|
||||||
* @throws NotFindClassException
|
* @throws ReflectionException
|
||||||
* @throws Exception
|
* @throws NotFindClassException
|
||||||
*/
|
* @throws Exception
|
||||||
public static function findOrCreate(array $condition, array $attributes): bool|static
|
*/
|
||||||
{
|
public static function findOrCreate(array $condition, array $attributes): bool|static
|
||||||
$logger = Kiri::app()->getLogger();
|
{
|
||||||
if (empty($attributes)) {
|
$logger = Kiri::app()->getLogger();
|
||||||
return $logger->addError(FIND_OR_CREATE_MESSAGE, 'mysql');
|
if (empty($attributes)) {
|
||||||
}
|
return $logger->addError(FIND_OR_CREATE_MESSAGE, 'mysql');
|
||||||
Db::beginTransaction();
|
}
|
||||||
/** @var static $select */
|
Db::beginTransaction();
|
||||||
$select = static::query()->where($condition)->first();
|
/** @var static $select */
|
||||||
if (empty($select)) {
|
$select = static::query()->where($condition)->first();
|
||||||
$select = duplicate(static::class);
|
if (empty($select)) {
|
||||||
$select->attributes = $attributes;
|
$select = duplicate(static::class);
|
||||||
if (!$select->save()) {
|
$select->attributes = $attributes;
|
||||||
Db::rollback();
|
$select->setIsNowExample(true);
|
||||||
return $logger->addError($select->getLastError(), 'mysql');
|
if (!$select->save()) {
|
||||||
}
|
Db::rollback();
|
||||||
}
|
return $logger->addError($select->getLastError(), 'mysql');
|
||||||
Db::commit();
|
}
|
||||||
return $select;
|
}
|
||||||
}
|
Db::commit();
|
||||||
|
return $select;
|
||||||
|
}
|
||||||
/**
|
|
||||||
* @param array $condition
|
|
||||||
* @param array $attributes
|
/**
|
||||||
* @return bool|static
|
* @param array $condition
|
||||||
* @throws Exception
|
* @param array $attributes
|
||||||
*/
|
* @return bool|static
|
||||||
public static function createOrUpdate(array $condition, array $attributes = []): bool|static
|
* @throws Exception
|
||||||
{
|
*/
|
||||||
$logger = Kiri::app()->getLogger();
|
public static function createOrUpdate(array $condition, array $attributes = []): bool|static
|
||||||
if (empty($attributes)) {
|
{
|
||||||
return $logger->addError(FIND_OR_CREATE_MESSAGE, 'mysql');
|
$logger = Kiri::app()->getLogger();
|
||||||
}
|
if (empty($attributes)) {
|
||||||
|
return $logger->addError(FIND_OR_CREATE_MESSAGE, 'mysql');
|
||||||
Db::beginTransaction();
|
}
|
||||||
/** @var static $select */
|
|
||||||
$select = static::query()->where($condition)->first();
|
Db::beginTransaction();
|
||||||
if (empty($select)) {
|
/** @var static $select */
|
||||||
$select = duplicate(static::class);
|
$select = static::query()->where($condition)->first();
|
||||||
}
|
if (empty($select)) {
|
||||||
$select->attributes = $attributes;
|
$select = duplicate(static::class);
|
||||||
if (!$select->save()) {
|
}
|
||||||
Db::rollback();
|
$select->attributes = $attributes;
|
||||||
$select = $logger->addError($select->getLastError(), 'mysql');
|
if (!$select->save()) {
|
||||||
} else {
|
Db::rollback();
|
||||||
Db::commit();
|
$select = $logger->addError($select->getLastError(), 'mysql');
|
||||||
}
|
} else {
|
||||||
return $select;
|
Db::commit();
|
||||||
}
|
}
|
||||||
|
return $select;
|
||||||
|
}
|
||||||
/**
|
|
||||||
* @param $action
|
|
||||||
* @param $columns
|
/**
|
||||||
* @param null|array $condition
|
* @param $action
|
||||||
* @return array|bool|int|string|null
|
* @param $columns
|
||||||
* @throws Exception
|
* @param null|array $condition
|
||||||
*/
|
* @return array|bool|int|string|null
|
||||||
private function mathematics($columns, $action, ?array $condition = null): int|bool|array|string|null
|
* @throws Exception
|
||||||
{
|
*/
|
||||||
if (empty($condition)) {
|
private function mathematics($columns, $action, ?array $condition = null): int|bool|array|string|null
|
||||||
$condition = [$this->getPrimary() => $this->getPrimaryValue()];
|
{
|
||||||
}
|
if (empty($condition)) {
|
||||||
|
$condition = [$this->getPrimary() => $this->getPrimaryValue()];
|
||||||
$activeQuery = static::query()->where($condition);
|
}
|
||||||
$create = SqlBuilder::builder($activeQuery)->mathematics($columns, $action);
|
|
||||||
if (is_bool($create)) {
|
$activeQuery = static::query()->where($condition);
|
||||||
return false;
|
$create = SqlBuilder::builder($activeQuery)->mathematics($columns, $action);
|
||||||
}
|
if (is_bool($create)) {
|
||||||
return $this->getConnection()->createCommand($create[0], $create[1])->exec();
|
return false;
|
||||||
}
|
}
|
||||||
|
return $this->getConnection()->createCommand($create[0], $create[1])->exec();
|
||||||
|
}
|
||||||
/**
|
|
||||||
* @param array $fields
|
|
||||||
* @return ModelInterface|bool
|
/**
|
||||||
* @throws Exception
|
* @param array $fields
|
||||||
*/
|
* @return ModelInterface|bool
|
||||||
public function update(array $fields): static|bool
|
* @throws Exception
|
||||||
{
|
*/
|
||||||
return $this->save($fields);
|
public function update(array $fields): static|bool
|
||||||
}
|
{
|
||||||
|
return $this->save($fields);
|
||||||
|
}
|
||||||
/**
|
|
||||||
* @param array $data
|
|
||||||
* @return bool
|
/**
|
||||||
* @throws Exception
|
* @param array $data
|
||||||
*/
|
* @return bool
|
||||||
public static function inserts(array $data): bool
|
* @throws Exception
|
||||||
{
|
*/
|
||||||
$result = false;
|
public static function inserts(array $data): bool
|
||||||
if (empty($data)) {
|
{
|
||||||
error('Insert data empty.', 'mysql');
|
$result = false;
|
||||||
} else {
|
if (empty($data)) {
|
||||||
$result = static::query()->batchInsert($data);
|
error('Insert data empty.', 'mysql');
|
||||||
}
|
} else {
|
||||||
return $result;
|
$result = static::query()->batchInsert($data);
|
||||||
}
|
}
|
||||||
|
return $result;
|
||||||
/**
|
}
|
||||||
* @return bool
|
|
||||||
* @throws Exception
|
/**
|
||||||
*/
|
* @return bool
|
||||||
public function delete(): bool
|
* @throws Exception
|
||||||
{
|
*/
|
||||||
$conditions = $this->_oldAttributes;
|
public function delete(): bool
|
||||||
if (empty($conditions)) {
|
{
|
||||||
return $this->addError("Delete condition do not empty.", 'mysql');
|
$primary = $this->getPrimary();
|
||||||
}
|
if (empty($primary) || !$this->hasPrimaryValue()) {
|
||||||
$primary = $this->getPrimary();
|
return $this->addError("Only primary key operations are supported.", 'mysql');
|
||||||
|
}
|
||||||
if (!empty($primary)) {
|
if (!$this->beforeDelete()) {
|
||||||
$conditions = [$primary => $this->getAttribute($primary)];
|
$result = static::deleteByCondition([$primary => $this->getPrimaryValue()]);
|
||||||
}
|
Coroutine::create(function () use ($result) {
|
||||||
return static::deleteByCondition($conditions);
|
$this->afterDelete($result);
|
||||||
}
|
});
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
/**
|
return false;
|
||||||
* @param mixed $condition
|
}
|
||||||
* @param array $attributes
|
|
||||||
*
|
|
||||||
* @return bool
|
/**
|
||||||
* @throws NotFindClassException
|
* @param mixed $condition
|
||||||
* @throws ReflectionException
|
* @param array $attributes
|
||||||
* @throws Exception
|
*
|
||||||
*/
|
* @return bool
|
||||||
public static function updateAll(mixed $condition, array $attributes = []): bool
|
* @throws NotFindClassException
|
||||||
{
|
* @throws ReflectionException
|
||||||
$condition = static::query()->where($condition);
|
* @throws Exception
|
||||||
return $condition->batchUpdate($attributes);
|
*/
|
||||||
}
|
public static function updateAll(mixed $condition, array $attributes = []): bool
|
||||||
|
{
|
||||||
|
$condition = static::query()->where($condition);
|
||||||
/**
|
return $condition->batchUpdate($attributes);
|
||||||
* @param $condition
|
}
|
||||||
* @return array|Collection
|
|
||||||
* @throws NotFindClassException
|
|
||||||
* @throws ReflectionException
|
/**
|
||||||
*/
|
* @param $condition
|
||||||
public static function get($condition): Collection|array
|
* @return array|Collection
|
||||||
{
|
* @throws NotFindClassException
|
||||||
return static::query()->where($condition)->all();
|
* @throws ReflectionException
|
||||||
}
|
*/
|
||||||
|
public static function get($condition): Collection|array
|
||||||
|
{
|
||||||
/**
|
return static::query()->where($condition)->all();
|
||||||
* @param $condition
|
}
|
||||||
* @param array $attributes
|
|
||||||
*
|
|
||||||
* @return array|Collection
|
/**
|
||||||
* @throws Exception
|
* @param $condition
|
||||||
*/
|
* @param array $attributes
|
||||||
public static function findAll($condition, array $attributes = []): array|Collection
|
*
|
||||||
{
|
* @return array|Collection
|
||||||
$query = static::query()->where($condition);
|
* @throws Exception
|
||||||
if (!empty($attributes)) {
|
*/
|
||||||
$query->bindParams($attributes);
|
public static function findAll($condition, array $attributes = []): array|Collection
|
||||||
}
|
{
|
||||||
return $query->all();
|
$query = static::query()->where($condition);
|
||||||
}
|
if (!empty($attributes)) {
|
||||||
|
$query->bindParams($attributes);
|
||||||
/**
|
}
|
||||||
* @param $method
|
return $query->all();
|
||||||
* @return mixed
|
}
|
||||||
* @throws Exception
|
|
||||||
*/
|
/**
|
||||||
private function withRelation($method): mixed
|
* @param $method
|
||||||
{
|
* @return mixed
|
||||||
$method = $this->getRelate($method);
|
* @throws Exception
|
||||||
if (empty($method)) {
|
*/
|
||||||
return null;
|
private function withRelation($method): mixed
|
||||||
}
|
{
|
||||||
$resolve = $this->{$method}();
|
$method = $this->getRelate($method);
|
||||||
if ($resolve instanceof HasBase) {
|
if (empty($method)) {
|
||||||
$resolve = $resolve->get();
|
return null;
|
||||||
}
|
}
|
||||||
if ($resolve instanceof ToArray) {
|
$resolve = $this->{$method}();
|
||||||
return $resolve->toArray();
|
if ($resolve instanceof HasBase) {
|
||||||
} else if (is_object($resolve)) {
|
$resolve = $resolve->get();
|
||||||
return get_object_vars($resolve);
|
}
|
||||||
} else {
|
if ($resolve instanceof ToArray) {
|
||||||
return $resolve;
|
return $resolve->toArray();
|
||||||
}
|
} else if (is_object($resolve)) {
|
||||||
}
|
return get_object_vars($resolve);
|
||||||
|
} else {
|
||||||
|
return $resolve;
|
||||||
/**
|
}
|
||||||
* @return array
|
}
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function toArray(): array
|
/**
|
||||||
{
|
* @return array
|
||||||
$data = $this->_attributes;
|
* @throws Exception
|
||||||
$lists = di(Getter::class)->getGetter(static::class);
|
*/
|
||||||
foreach ($lists as $key => $item) {
|
public function toArray(): array
|
||||||
$data[$key] = $this->{$item}($data[$key] ?? null);
|
{
|
||||||
}
|
$data = $this->_attributes;
|
||||||
return $this->withRelates($data);
|
$lists = di(Getter::class)->getGetter(static::class);
|
||||||
}
|
foreach ($lists as $key => $item) {
|
||||||
|
$data[$key] = $this->{$item}($data[$key] ?? null);
|
||||||
/**
|
}
|
||||||
* @param $relates
|
return $this->withRelates($data);
|
||||||
* @return array
|
}
|
||||||
* @throws Exception
|
|
||||||
*/
|
/**
|
||||||
private function withRelates($relates): array
|
* @param $relates
|
||||||
{
|
* @return array
|
||||||
if (empty($with = $this->getWith())) {
|
* @throws Exception
|
||||||
return $relates;
|
*/
|
||||||
}
|
private function withRelates($relates): array
|
||||||
foreach ($with as $val) {
|
{
|
||||||
$relates[$val] = $this->withRelation($val);
|
if (empty($with = $this->getWith())) {
|
||||||
}
|
return $relates;
|
||||||
return $relates;
|
}
|
||||||
}
|
foreach ($with as $val) {
|
||||||
|
$relates[$val] = $this->withRelation($val);
|
||||||
|
}
|
||||||
/**
|
return $relates;
|
||||||
* @param string $modelName
|
}
|
||||||
* @param $foreignKey
|
|
||||||
* @param $localKey
|
|
||||||
* @return HasOne|ActiveQuery
|
/**
|
||||||
* @throws Exception
|
* @param string $modelName
|
||||||
*/
|
* @param $foreignKey
|
||||||
public function hasOne(string $modelName, $foreignKey, $localKey): HasOne|ActiveQuery
|
* @param $localKey
|
||||||
{
|
* @return HasOne|ActiveQuery
|
||||||
if (($value = $this->{$localKey}) === null) {
|
* @throws Exception
|
||||||
throw new Exception("Need join table primary key.");
|
*/
|
||||||
}
|
public function hasOne(string $modelName, $foreignKey, $localKey): HasOne|ActiveQuery
|
||||||
|
{
|
||||||
$relation = $this->getRelation();
|
if (($value = $this->{$localKey}) === null) {
|
||||||
|
throw new Exception("Need join table primary key.");
|
||||||
return new HasOne($modelName, $foreignKey, $value, $relation);
|
}
|
||||||
}
|
|
||||||
|
$relation = $this->getRelation();
|
||||||
|
|
||||||
/**
|
return new HasOne($modelName, $foreignKey, $value, $relation);
|
||||||
* @param $modelName
|
}
|
||||||
* @param $foreignKey
|
|
||||||
* @param $localKey
|
|
||||||
* @return ActiveQuery|HasCount
|
/**
|
||||||
* @throws Exception
|
* @param $modelName
|
||||||
*/
|
* @param $foreignKey
|
||||||
public function hasCount($modelName, $foreignKey, $localKey): ActiveQuery|HasCount
|
* @param $localKey
|
||||||
{
|
* @return ActiveQuery|HasCount
|
||||||
if (($value = $this->{$localKey}) === null) {
|
* @throws Exception
|
||||||
throw new Exception("Need join table primary key.");
|
*/
|
||||||
}
|
public function hasCount($modelName, $foreignKey, $localKey): ActiveQuery|HasCount
|
||||||
|
{
|
||||||
$relation = $this->getRelation();
|
if (($value = $this->{$localKey}) === null) {
|
||||||
|
throw new Exception("Need join table primary key.");
|
||||||
return new HasCount($modelName, $foreignKey, $value, $relation);
|
}
|
||||||
}
|
|
||||||
|
$relation = $this->getRelation();
|
||||||
|
|
||||||
/**
|
return new HasCount($modelName, $foreignKey, $value, $relation);
|
||||||
* @param $modelName
|
}
|
||||||
* @param $foreignKey
|
|
||||||
* @param $localKey
|
|
||||||
* @return ActiveQuery|HasMany
|
/**
|
||||||
* @throws Exception
|
* @param $modelName
|
||||||
*/
|
* @param $foreignKey
|
||||||
public function hasMany($modelName, $foreignKey, $localKey): ActiveQuery|HasMany
|
* @param $localKey
|
||||||
{
|
* @return ActiveQuery|HasMany
|
||||||
if (($value = $this->{$localKey}) === null) {
|
* @throws Exception
|
||||||
throw new Exception("Need join table primary key.");
|
*/
|
||||||
}
|
public function hasMany($modelName, $foreignKey, $localKey): ActiveQuery|HasMany
|
||||||
|
{
|
||||||
$relation = $this->getRelation();
|
if (($value = $this->{$localKey}) === null) {
|
||||||
|
throw new Exception("Need join table primary key.");
|
||||||
return new HasMany($modelName, $foreignKey, $value, $relation);
|
}
|
||||||
}
|
|
||||||
|
$relation = $this->getRelation();
|
||||||
/**
|
|
||||||
* @param $modelName
|
return new HasMany($modelName, $foreignKey, $value, $relation);
|
||||||
* @param $foreignKey
|
}
|
||||||
* @param $localKey
|
|
||||||
* @return ActiveQuery|HasMany
|
/**
|
||||||
* @throws Exception
|
* @param $modelName
|
||||||
*/
|
* @param $foreignKey
|
||||||
public function hasIn($modelName, $foreignKey, $localKey): ActiveQuery|HasMany
|
* @param $localKey
|
||||||
{
|
* @return ActiveQuery|HasMany
|
||||||
if (($value = $this->{$localKey}) === null) {
|
* @throws Exception
|
||||||
throw new Exception("Need join table primary key.");
|
*/
|
||||||
}
|
public function hasIn($modelName, $foreignKey, $localKey): ActiveQuery|HasMany
|
||||||
|
{
|
||||||
$relation = $this->getRelation();
|
if (($value = $this->{$localKey}) === null) {
|
||||||
|
throw new Exception("Need join table primary key.");
|
||||||
return new HasMany($modelName, $foreignKey, $value, $relation);
|
}
|
||||||
}
|
|
||||||
|
$relation = $this->getRelation();
|
||||||
/**
|
|
||||||
* @return bool
|
return new HasMany($modelName, $foreignKey, $value, $relation);
|
||||||
* @throws Exception
|
}
|
||||||
*/
|
|
||||||
public function afterDelete(): bool
|
/**
|
||||||
{
|
* @param bool $result
|
||||||
return TRUE;
|
* @return void
|
||||||
}
|
*/
|
||||||
|
public function afterDelete(bool $result): void
|
||||||
/**
|
{
|
||||||
* @return bool
|
}
|
||||||
* @throws Exception
|
|
||||||
*/
|
/**
|
||||||
public function beforeDelete(): bool
|
* @return bool
|
||||||
{
|
* @throws Exception
|
||||||
return TRUE;
|
*/
|
||||||
}
|
public function beforeDelete(): bool
|
||||||
}
|
{
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,60 +1,61 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Created by PhpStorm.
|
* Created by PhpStorm.
|
||||||
* User: whwyy
|
* User: whwyy
|
||||||
* Date: 2018/3/30 0030
|
* Date: 2018/3/30 0030
|
||||||
* Time: 14:39
|
* Time: 14:39
|
||||||
*/
|
*/
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database;
|
namespace Database;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interface ModelInterface
|
* Interface ModelInterface
|
||||||
* @package Database
|
* @package Database
|
||||||
*/
|
*/
|
||||||
interface ModelInterface
|
interface ModelInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $param
|
* @param $param
|
||||||
* @param null $db
|
* @param null $db
|
||||||
* @return ModelInterface
|
* @return ModelInterface
|
||||||
*/
|
*/
|
||||||
public static function findOne($param, $db = NULL): mixed;
|
public static function findOne($param, $db = NULL): mixed;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string|int $param
|
* @param string|int $param
|
||||||
* @return ModelInterface
|
* @return ModelInterface
|
||||||
*/
|
*/
|
||||||
public static function find(string|int $param): mixed;
|
public static function find(string|int $param): mixed;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @param array $data
|
||||||
*/
|
* @return static
|
||||||
public static function className(): string;
|
*/
|
||||||
|
public static function populate(array $data): static;
|
||||||
|
|
||||||
/**
|
|
||||||
* @return ActiveQuery
|
/**
|
||||||
* return a sql queryBuilder
|
* @return ActiveQuery
|
||||||
*/
|
* return a sql queryBuilder
|
||||||
public static function query(): ActiveQuery;
|
*/
|
||||||
|
public static function query(): ActiveQuery;
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
/**
|
||||||
*/
|
* @return string
|
||||||
public function getTable(): string;
|
*/
|
||||||
|
public function getTable(): string;
|
||||||
|
|
||||||
/**
|
|
||||||
* @return Connection
|
/**
|
||||||
*/
|
* @return Connection
|
||||||
public function getConnection(): Connection;
|
*/
|
||||||
|
public function getConnection(): Connection;
|
||||||
|
|
||||||
}
|
|
||||||
|
}
|
||||||
@@ -1,406 +1,406 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Created by PhpStorm.
|
* Created by PhpStorm.
|
||||||
* User: admin
|
* User: admin
|
||||||
* Date: 2019-03-18
|
* Date: 2019-03-18
|
||||||
* Time: 17:22
|
* Time: 17:22
|
||||||
*/
|
*/
|
||||||
|
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database\Mysql;
|
namespace Database\Mysql;
|
||||||
|
|
||||||
|
|
||||||
use Database\Connection;
|
use Database\Connection;
|
||||||
use Database\SqlBuilder;
|
use Database\SqlBuilder;
|
||||||
use Exception;
|
use Exception;
|
||||||
use JetBrains\PhpStorm\Pure;
|
use JetBrains\PhpStorm\Pure;
|
||||||
use Kiri\Abstracts\Component;
|
use Kiri\Abstracts\Component;
|
||||||
use Kiri\Core\Json;
|
use Kiri\Core\Json;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Columns
|
* Class Columns
|
||||||
* @package Database\Mysql
|
* @package Database\Mysql
|
||||||
*/
|
*/
|
||||||
class Columns extends Component
|
class Columns extends Component
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
* field types
|
* field types
|
||||||
*/
|
*/
|
||||||
private array $columns = [];
|
private array $columns = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Connection
|
* @var Connection
|
||||||
* Mysql client
|
* Mysql client
|
||||||
*/
|
*/
|
||||||
public Connection $db;
|
public Connection $db;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
* tableName
|
* tableName
|
||||||
*/
|
*/
|
||||||
public string $table = '';
|
public string $table = '';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
* field primary key
|
* field primary key
|
||||||
*/
|
*/
|
||||||
private array $_primary = [];
|
private array $_primary = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
* by mysql field auto_increment
|
* by mysql field auto_increment
|
||||||
*/
|
*/
|
||||||
private array $_auto_increment = [];
|
private array $_auto_increment = [];
|
||||||
|
|
||||||
|
|
||||||
private array $_fields = [];
|
private array $_fields = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $table
|
* @param string $table
|
||||||
* @return $this
|
* @return $this
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function table(string $table): static
|
public function table(string $table): static
|
||||||
{
|
{
|
||||||
$this->structure($this->table = $table);
|
$this->structure($this->table = $table);
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function getTable(): string
|
public function getTable(): string
|
||||||
{
|
{
|
||||||
return $this->table;
|
return $this->table;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $key
|
* @param $key
|
||||||
* @param $val
|
* @param $val
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function fieldFormat($key, $val): mixed
|
public function fieldFormat($key, $val): mixed
|
||||||
{
|
{
|
||||||
return $this->encode($val, $this->get_fields($key));
|
return $this->encode($val, $this->get_fields($key));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $data
|
* @param $data
|
||||||
* @return array
|
* @return array
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function populate($data): array
|
public function populate($data): array
|
||||||
{
|
{
|
||||||
$column = $this->get_fields();
|
$column = $this->get_fields();
|
||||||
foreach ($data as $key => $val) {
|
foreach ($data as $key => $val) {
|
||||||
if (!isset($column[$key])) {
|
if (!isset($column[$key])) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$data[$key] = $this->decode($val, $column[$key]);
|
$data[$key] = $this->decode($val, $column[$key]);
|
||||||
}
|
}
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $val
|
* @param $val
|
||||||
* @param null $format
|
* @param null $format
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function decode($val, $format = null): mixed
|
public function decode($val, $format = null): mixed
|
||||||
{
|
{
|
||||||
if (empty($format) || $val === null) {
|
if (empty($format) || $val === null) {
|
||||||
return $val;
|
return $val;
|
||||||
}
|
}
|
||||||
$format = strtolower($format);
|
$format = strtolower($format);
|
||||||
if ($this->isInt($format)) {
|
if ($this->isInt($format)) {
|
||||||
return (int)$val;
|
return (int)$val;
|
||||||
} else if ($this->isJson($format)) {
|
} else if ($this->isJson($format)) {
|
||||||
return Json::decode($val, true);
|
return Json::decode($val, true);
|
||||||
} else if ($this->isFloat($format)) {
|
} else if ($this->isFloat($format)) {
|
||||||
return (float)$val;
|
return (float)$val;
|
||||||
} else {
|
} else {
|
||||||
return stripslashes($val);
|
return stripslashes($val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param $value
|
* @param $value
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function _decode(string $name, $value): mixed
|
public function _decode(string $name, $value): mixed
|
||||||
{
|
{
|
||||||
return $this->decode($value, $this->get_fields($name));
|
return $this->decode($value, $this->get_fields($name));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $val
|
* @param $val
|
||||||
* @param null $format
|
* @param null $format
|
||||||
* @return float|bool|int|string
|
* @return float|bool|int|string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function encode($val, $format = null): float|bool|int|string
|
public function encode($val, $format = null): float|bool|int|string
|
||||||
{
|
{
|
||||||
if (empty($format)) {
|
if (empty($format)) {
|
||||||
return $val;
|
return $val;
|
||||||
}
|
}
|
||||||
$format = strtolower($format);
|
$format = strtolower($format);
|
||||||
if ($this->isInt($format)) {
|
if ($this->isInt($format)) {
|
||||||
return (int)$val;
|
return (int)$val;
|
||||||
} else if ($this->isJson($format)) {
|
} else if ($this->isJson($format)) {
|
||||||
return Json::encode($val);
|
return Json::encode($val);
|
||||||
} else if ($this->isFloat($format)) {
|
} else if ($this->isFloat($format)) {
|
||||||
return (float)$val;
|
return (float)$val;
|
||||||
} else {
|
} else {
|
||||||
return addslashes($val);
|
return addslashes($val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $format
|
* @param $format
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
#[Pure] public function isInt($format): bool
|
#[Pure] public function isInt($format): bool
|
||||||
{
|
{
|
||||||
return in_array($format, ['int', 'bigint', 'tinyint', 'smallint', 'mediumint']);
|
return in_array($format, ['int', 'bigint', 'tinyint', 'smallint', 'mediumint']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $format
|
* @param $format
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
#[Pure] public function isFloat($format): bool
|
#[Pure] public function isFloat($format): bool
|
||||||
{
|
{
|
||||||
return in_array($format, ['float', 'double', 'decimal']);
|
return in_array($format, ['float', 'double', 'decimal']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $format
|
* @param $format
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
#[Pure] public function isJson($format): bool
|
#[Pure] public function isJson($format): bool
|
||||||
{
|
{
|
||||||
return in_array($format, ['json']);
|
return in_array($format, ['json']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $format
|
* @param $format
|
||||||
* @return bool
|
* @return bool
|
||||||
*/
|
*/
|
||||||
#[Pure] public function isString($format): bool
|
#[Pure] public function isString($format): bool
|
||||||
{
|
{
|
||||||
return in_array($format, ['varchar', 'char', 'text', 'longtext', 'tinytext', 'mediumtext']);
|
return in_array($format, ['varchar', 'char', 'text', 'longtext', 'tinytext', 'mediumtext']);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function format(): array
|
public function format(): array
|
||||||
{
|
{
|
||||||
return $this->columns('Default', 'Field');
|
return $this->columns('Default', 'Field');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function getFields(): array
|
public function getFields(): array
|
||||||
{
|
{
|
||||||
if (empty($this->_fields)) {
|
if (empty($this->_fields)) {
|
||||||
$this->structure($this->table);
|
$this->structure($this->table);
|
||||||
}
|
}
|
||||||
return $this->_fields[$this->table];
|
return $this->_fields[$this->table];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @return bool
|
* @return bool
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function hasField(string $name): bool
|
public function hasField(string $name): bool
|
||||||
{
|
{
|
||||||
return array_key_exists($name, $this->getFields());
|
return array_key_exists($name, $this->getFields());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return int|string|null
|
* @return int|string|null
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function getAutoIncrement(): int|string|null
|
public function getAutoIncrement(): int|string|null
|
||||||
{
|
{
|
||||||
return $this->_auto_increment[$this->table] ?? null;
|
return $this->_auto_increment[$this->table] ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array|null|string
|
* @return array|null|string
|
||||||
*
|
*
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function getPrimaryKeys(): array|string|null
|
public function getPrimaryKeys(): array|string|null
|
||||||
{
|
{
|
||||||
if (isset($this->_auto_increment[$this->table])) {
|
if (isset($this->_auto_increment[$this->table])) {
|
||||||
return $this->_auto_increment[$this->table];
|
return $this->_auto_increment[$this->table];
|
||||||
}
|
}
|
||||||
return $this->_primary[$this->table] ?? null;
|
return $this->_primary[$this->table] ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array|null|string
|
* @return array|null|string
|
||||||
*
|
*
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
#[Pure] public function getFirstPrimary(): array|string|null
|
#[Pure] public function getFirstPrimary(): array|string|null
|
||||||
{
|
{
|
||||||
if (isset($this->_auto_increment[$this->table])) {
|
if (isset($this->_auto_increment[$this->table])) {
|
||||||
return $this->_auto_increment[$this->table];
|
return $this->_auto_increment[$this->table];
|
||||||
}
|
}
|
||||||
if (isset($this->_primary[$this->table])) {
|
if (isset($this->_primary[$this->table])) {
|
||||||
return current($this->_primary[$this->table]);
|
return current($this->_primary[$this->table]);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $name
|
* @param $name
|
||||||
* @param null $index
|
* @param null $index
|
||||||
* @return array
|
* @return array
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function columns($name, $index = null): array
|
private function columns($name, $index = null): array
|
||||||
{
|
{
|
||||||
if (empty($index)) {
|
if (empty($index)) {
|
||||||
return array_column($this->getColumns(), $name);
|
return array_column($this->getColumns(), $name);
|
||||||
} else {
|
} else {
|
||||||
return array_column($this->getColumns(), $name, $index);
|
return array_column($this->getColumns(), $name, $index);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array|static
|
* @return array|static
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function getColumns(): array|static
|
private function getColumns(): array|static
|
||||||
{
|
{
|
||||||
return $this->structure($this->getTable());
|
return $this->structure($this->getTable());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $table
|
* @param $table
|
||||||
* @return array|Columns
|
* @return array|Columns
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function structure($table): array|static
|
private function structure($table): array|static
|
||||||
{
|
{
|
||||||
if (!isset($this->columns[$table]) || empty($this->columns[$table])) {
|
if (!isset($this->columns[$table]) || empty($this->columns[$table])) {
|
||||||
$column = $this->db->createCommand(SqlBuilder::builder(null)->columns($table))->all();
|
$column = $this->db->createCommand(SqlBuilder::builder(null)->columns($table))->all();
|
||||||
if (empty($column)) {
|
if (empty($column)) {
|
||||||
throw new Exception("The table " . $table . " not exists.");
|
throw new Exception("The table " . $table . " not exists.");
|
||||||
}
|
}
|
||||||
return $this->columns[$table] = $this->resolve($column, $table);
|
return $this->columns[$table] = $this->resolve($column, $table);
|
||||||
}
|
}
|
||||||
return $this->columns[$table];
|
return $this->columns[$table];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $column
|
* @param array $column
|
||||||
* @param $table
|
* @param $table
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
private function resolve(array $column, $table): array
|
private function resolve(array $column, $table): array
|
||||||
{
|
{
|
||||||
foreach ($column as $key => $item) {
|
foreach ($column as $key => $item) {
|
||||||
$this->addPrimary($item, $table);
|
$this->addPrimary($item, $table);
|
||||||
$column[$key]['Type'] = $this->clean($item['Type']);
|
$column[$key]['Type'] = $this->clean($item['Type']);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->_fields[$table] = array_column($column, 'Default', 'Field');
|
$this->_fields[$table] = array_column($column, 'Default', 'Field');
|
||||||
|
|
||||||
return $column;
|
return $column;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $item
|
* @param $item
|
||||||
* @param $table
|
* @param $table
|
||||||
*/
|
*/
|
||||||
private function addPrimary($item, $table)
|
private function addPrimary($item, $table)
|
||||||
{
|
{
|
||||||
if (!isset($this->_primary[$table])) {
|
if (!isset($this->_primary[$table])) {
|
||||||
$this->_primary[$table] = [];
|
$this->_primary[$table] = [];
|
||||||
}
|
}
|
||||||
if ($item['Key'] === 'PRI') {
|
if ($item['Key'] === 'PRI') {
|
||||||
$this->_primary[$table][] = $item['Field'];
|
$this->_primary[$table][] = $item['Field'];
|
||||||
}
|
}
|
||||||
$this->addIncrement($item, $table);
|
$this->addIncrement($item, $table);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $item
|
* @param $item
|
||||||
* @param $table
|
* @param $table
|
||||||
*/
|
*/
|
||||||
private function addIncrement($item, $table)
|
private function addIncrement($item, $table)
|
||||||
{
|
{
|
||||||
if ($item['Extra'] !== 'auto_increment') {
|
if ($item['Extra'] !== 'auto_increment') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
$this->_auto_increment[$table] = $item['Field'];
|
$this->_auto_increment[$table] = $item['Field'];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $type
|
* @param $type
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function clean($type): string
|
public function clean($type): string
|
||||||
{
|
{
|
||||||
if (!str_contains($type, ')')) {
|
if (!str_contains($type, ')')) {
|
||||||
return $type;
|
return $type;
|
||||||
}
|
}
|
||||||
$replace = preg_replace('/\(\d+(,\d+)?\)(\s+\w+)*/', '', $type);
|
$replace = preg_replace('/\(\d+(,\d+)?\)(\s+\w+)*/', '', $type);
|
||||||
if (str_contains($replace, ' ')) {
|
if (str_contains($replace, ' ')) {
|
||||||
$replace = explode(' ', $replace)[1];
|
$replace = explode(' ', $replace)[1];
|
||||||
}
|
}
|
||||||
return $replace;
|
return $replace;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param null $field
|
* @param null $field
|
||||||
* @return array|string|null
|
* @return array|string|null
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function get_fields($field = null): array|string|null
|
public function get_fields($field = null): array|string|null
|
||||||
{
|
{
|
||||||
$fields = $this->getAllField();
|
$fields = $this->getAllField();
|
||||||
if (empty($field)) {
|
if (empty($field)) {
|
||||||
return $fields;
|
return $fields;
|
||||||
}
|
}
|
||||||
if (isset($fields[$field])) {
|
if (isset($fields[$field])) {
|
||||||
return strtolower($fields[$field]);
|
return strtolower($fields[$field]);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function getAllField(): array
|
public function getAllField(): array
|
||||||
{
|
{
|
||||||
return $this->columns('Type', 'Field');
|
return $this->columns('Type', 'Field');
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
+324
@@ -0,0 +1,324 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Mysql;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use Kiri\Abstracts\Config;
|
||||||
|
use Kiri\Abstracts\Logger;
|
||||||
|
use Kiri\Kiri;
|
||||||
|
use Kiri\Pool\StopHeartbeatCheck;
|
||||||
|
use PDOStatement;
|
||||||
|
use Swoole\Timer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
class PDO implements StopHeartbeatCheck
|
||||||
|
{
|
||||||
|
|
||||||
|
const DB_ERROR_MESSAGE = 'The system is busy, please try again later.';
|
||||||
|
|
||||||
|
|
||||||
|
private ?\PDO $pdo = null;
|
||||||
|
|
||||||
|
|
||||||
|
private int $_transaction = 0;
|
||||||
|
|
||||||
|
|
||||||
|
private int $_timer = -1;
|
||||||
|
|
||||||
|
private int $_last = 0;
|
||||||
|
|
||||||
|
public string $dbname;
|
||||||
|
public string $cds;
|
||||||
|
public string $username;
|
||||||
|
public string $password;
|
||||||
|
public string $charset;
|
||||||
|
public int $connect_timeout;
|
||||||
|
public int $read_timeout;
|
||||||
|
|
||||||
|
|
||||||
|
public array $attributes = [];
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $config
|
||||||
|
*/
|
||||||
|
public function __construct(array $config)
|
||||||
|
{
|
||||||
|
$this->dbname = $config['dbname'];
|
||||||
|
$this->cds = $config['cds'];
|
||||||
|
$this->username = $config['username'];
|
||||||
|
$this->password = $config['password'];
|
||||||
|
$this->connect_timeout = $config['connect_timeout'] ?? 30;
|
||||||
|
$this->read_timeout = $config['read_timeout'] ?? 10;
|
||||||
|
$this->charset = $config['charset'] ?? 'utf8mb4';
|
||||||
|
$this->attributes = $config['attributes'] ?? [];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function init()
|
||||||
|
{
|
||||||
|
$this->heartbeat_check();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function inTransaction(): bool
|
||||||
|
{
|
||||||
|
return $this->_transaction > 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function heartbeat_check(): void
|
||||||
|
{
|
||||||
|
if (env('state', 'start') == 'exit') {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if ($this->_timer === -1) {
|
||||||
|
$this->_timer = Timer::tick(1000, fn() => $this->waite());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private function waite(): void
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
if (env('state', 'start') == 'exit') {
|
||||||
|
Kiri::getDi()->get(Logger::class)->critical('timer end');
|
||||||
|
$this->stopHeartbeatCheck();
|
||||||
|
}
|
||||||
|
if (time() - $this->_last > (int)Config::get('databases.pool.tick', 60)) {
|
||||||
|
$this->stopHeartbeatCheck();
|
||||||
|
$this->pdo = null;
|
||||||
|
}
|
||||||
|
} catch (\Throwable $throwable) {
|
||||||
|
error($throwable);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function stopHeartbeatCheck(): void
|
||||||
|
{
|
||||||
|
if ($this->_timer > -1) {
|
||||||
|
Timer::clear($this->_timer);
|
||||||
|
}
|
||||||
|
$this->_timer = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function beginTransaction()
|
||||||
|
{
|
||||||
|
if ($this->_transaction == 0) {
|
||||||
|
$this->_pdo()->beginTransaction();
|
||||||
|
}
|
||||||
|
$this->_transaction++;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function commit()
|
||||||
|
{
|
||||||
|
if ($this->_transaction == 0) {
|
||||||
|
$this->_pdo()->commit();
|
||||||
|
}
|
||||||
|
$this->_transaction--;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public function rollback()
|
||||||
|
{
|
||||||
|
if ($this->_transaction == 0) {
|
||||||
|
$this->_pdo()->rollBack();
|
||||||
|
}
|
||||||
|
$this->_transaction--;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $sql
|
||||||
|
* @param array $params
|
||||||
|
* @return bool|array|null
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function fetchAll(string $sql, array $params = []): bool|null|array
|
||||||
|
{
|
||||||
|
$pdo = $this->queryPrev($sql, $params);
|
||||||
|
|
||||||
|
$result = $pdo->fetchAll(\PDO::FETCH_ASSOC);
|
||||||
|
$pdo->closeCursor();
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $sql
|
||||||
|
* @param array $params
|
||||||
|
* @return bool|array|null
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function fetch(string $sql, array $params = []): bool|null|array
|
||||||
|
{
|
||||||
|
$pdo = $this->queryPrev($sql, $params);
|
||||||
|
|
||||||
|
$result = $pdo->fetch(\PDO::FETCH_ASSOC);
|
||||||
|
$pdo->closeCursor();
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $sql
|
||||||
|
* @param array $params
|
||||||
|
* @return bool|array|null
|
||||||
|
* @throws \Exception
|
||||||
|
*/
|
||||||
|
public function fetchColumn(string $sql, array $params = []): bool|null|array
|
||||||
|
{
|
||||||
|
$pdo = $this->queryPrev($sql, $params);
|
||||||
|
|
||||||
|
$result = $pdo->fetchColumn(\PDO::FETCH_ASSOC);
|
||||||
|
$pdo->closeCursor();
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $sql
|
||||||
|
* @param array $params
|
||||||
|
* @return int
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function count(string $sql, array $params = []): int
|
||||||
|
{
|
||||||
|
$pdo = $this->queryPrev($sql, $params);
|
||||||
|
|
||||||
|
$result = $pdo->rowCount();
|
||||||
|
$pdo->closeCursor();
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $sql
|
||||||
|
* @param array $params
|
||||||
|
* @return PDOStatement
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
private function queryPrev(string $sql, array $params = []): PDOStatement
|
||||||
|
{
|
||||||
|
$this->_last = time();
|
||||||
|
try {
|
||||||
|
if (($statement = $this->_pdo()->query($sql)) === false) {
|
||||||
|
throw new Exception($this->_pdo()->errorInfo()[1]);
|
||||||
|
}
|
||||||
|
return $this->bindValue($statement, $params);
|
||||||
|
} catch (\PDOException | \Throwable $throwable) {
|
||||||
|
if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
|
||||||
|
$this->pdo = null;
|
||||||
|
|
||||||
|
return $this->queryPrev($sql, $params);
|
||||||
|
}
|
||||||
|
throw new Exception($throwable->getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param PDOStatement $statement
|
||||||
|
* @param array $params
|
||||||
|
* @return PDOStatement
|
||||||
|
*/
|
||||||
|
private function bindValue(PDOStatement $statement, array $params = []): PDOStatement
|
||||||
|
{
|
||||||
|
if (empty($params)) return $statement;
|
||||||
|
foreach ($params as $key => $param) {
|
||||||
|
$statement->bindValue($key, $param);
|
||||||
|
}
|
||||||
|
return $statement;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $sql
|
||||||
|
* @param array $params
|
||||||
|
* @return int
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function execute(string $sql, array $params = []): int
|
||||||
|
{
|
||||||
|
$this->_last = time();
|
||||||
|
$pdo = $this->_pdo();
|
||||||
|
if (!(($prepare = $pdo->prepare($sql)) instanceof PDOStatement)) {
|
||||||
|
throw new Exception($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE);
|
||||||
|
}
|
||||||
|
if ($prepare->execute($params) === false) {
|
||||||
|
throw new Exception($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE);
|
||||||
|
}
|
||||||
|
$result = (int)$pdo->lastInsertId();
|
||||||
|
$prepare->closeCursor();
|
||||||
|
if ($result == 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return \PDO
|
||||||
|
*/
|
||||||
|
public function _pdo(): \PDO
|
||||||
|
{
|
||||||
|
if ($this->_timer === -1) {
|
||||||
|
$this->heartbeat_check();
|
||||||
|
}
|
||||||
|
if (!($this->pdo instanceof \PDO)) {
|
||||||
|
$this->pdo = $this->newClient();
|
||||||
|
}
|
||||||
|
return $this->pdo;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return \PDO
|
||||||
|
*/
|
||||||
|
private function newClient(): \PDO
|
||||||
|
{
|
||||||
|
$link = new \PDO('mysql:dbname=' . $this->dbname . ';host=' . $this->cds, $this->username, $this->password, [
|
||||||
|
\PDO::ATTR_EMULATE_PREPARES => false,
|
||||||
|
\PDO::ATTR_CASE => \PDO::CASE_NATURAL,
|
||||||
|
\PDO::ATTR_TIMEOUT => $this->connect_timeout,
|
||||||
|
\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
|
||||||
|
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $this->charset
|
||||||
|
]);
|
||||||
|
$link->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
||||||
|
$link->setAttribute(\PDO::ATTR_STRINGIFY_FETCHES, false);
|
||||||
|
$link->setAttribute(\PDO::ATTR_ORACLE_NULLS, \PDO::NULL_EMPTY_STRING);
|
||||||
|
if (!empty($this->attributes) && is_array($this->attributes)) {
|
||||||
|
foreach ($this->attributes as $key => $attribute) {
|
||||||
|
$link->setAttribute($key, $attribute);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $link;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -1,36 +1,36 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
namespace Database\Mysql;
|
namespace Database\Mysql;
|
||||||
|
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Kiri\Abstracts\Component;
|
use Kiri\Abstracts\Component;
|
||||||
use Database\Connection;
|
use Database\Connection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Schema
|
* Class Schema
|
||||||
* @package Database\Mysql
|
* @package Database\Mysql
|
||||||
*/
|
*/
|
||||||
class Schema extends Component
|
class Schema extends Component
|
||||||
{
|
{
|
||||||
|
|
||||||
/** @var ?Connection */
|
/** @var ?Connection */
|
||||||
public ?Connection $db = null;
|
public ?Connection $db = null;
|
||||||
|
|
||||||
/** @var ?Columns $_column*/
|
/** @var ?Columns $_column*/
|
||||||
private ?Columns $_column = null;
|
private ?Columns $_column = null;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return Columns|null
|
* @return Columns|null
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function getColumns(): ?Columns
|
public function getColumns(): ?Columns
|
||||||
{
|
{
|
||||||
if ($this->_column === null) {
|
if ($this->_column === null) {
|
||||||
$this->_column = new Columns(['db' => $this->db]);
|
$this->_column = new Columns(['db' => $this->db]);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->_column;
|
return $this->_column;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,29 +1,29 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database\Orm;
|
namespace Database\Orm;
|
||||||
|
|
||||||
|
|
||||||
use Database\Traits\Builder;
|
use Database\Traits\Builder;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Trait Condition
|
* Trait Condition
|
||||||
* @package Database\Orm
|
* @package Database\Orm
|
||||||
*/
|
*/
|
||||||
trait Condition
|
trait Condition
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
use Builder;
|
use Builder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $query
|
* @param $query
|
||||||
* @return string
|
* @return string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function getWhere($query): string
|
public function getWhere($query): string
|
||||||
{
|
{
|
||||||
return $this->where($query);
|
return $this->where($query);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+204
-204
@@ -1,204 +1,204 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database;
|
namespace Database;
|
||||||
|
|
||||||
|
|
||||||
use Closure;
|
use Closure;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Kiri\Abstracts\Component;
|
use Kiri\Abstracts\Component;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Pagination
|
* Class Pagination
|
||||||
* @package Database
|
* @package Database
|
||||||
*/
|
*/
|
||||||
class Pagination extends Component
|
class Pagination extends Component
|
||||||
{
|
{
|
||||||
|
|
||||||
/** @var ActiveQuery */
|
/** @var ActiveQuery */
|
||||||
private ActiveQuery $activeQuery;
|
private ActiveQuery $activeQuery;
|
||||||
|
|
||||||
/** @var int 从第几个开始查 */
|
/** @var int 从第几个开始查 */
|
||||||
private int $_offset = 0;
|
private int $_offset = 0;
|
||||||
|
|
||||||
/** @var int 每页数量 */
|
/** @var int 每页数量 */
|
||||||
private int $_limit = 100;
|
private int $_limit = 100;
|
||||||
|
|
||||||
/** @var int 最大查询数量 */
|
/** @var int 最大查询数量 */
|
||||||
private int $_max = 0;
|
private int $_max = 0;
|
||||||
|
|
||||||
/** @var int 当前已查询数量 */
|
/** @var int 当前已查询数量 */
|
||||||
private int $_length = 0;
|
private int $_length = 0;
|
||||||
|
|
||||||
/** @var Closure */
|
/** @var Closure */
|
||||||
private Closure $_callback;
|
private Closure $_callback;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* PaginationIteration constructor.
|
* PaginationIteration constructor.
|
||||||
* @param ActiveQuery $activeQuery
|
* @param ActiveQuery $activeQuery
|
||||||
* @param array $config
|
* @param array $config
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function __construct(ActiveQuery $activeQuery, array $config = [])
|
public function __construct(ActiveQuery $activeQuery, array $config = [])
|
||||||
{
|
{
|
||||||
parent::__construct($config);
|
parent::__construct($config);
|
||||||
$this->activeQuery = $activeQuery;
|
$this->activeQuery = $activeQuery;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function clean()
|
public function clean()
|
||||||
{
|
{
|
||||||
unset($this->activeQuery, $this->_callback, $this->_group);
|
unset($this->activeQuery, $this->_callback, $this->_group);
|
||||||
$this->_offset = 0;
|
$this->_offset = 0;
|
||||||
$this->_limit = 100;
|
$this->_limit = 100;
|
||||||
$this->_max = 0;
|
$this->_max = 0;
|
||||||
$this->_length = 0;;
|
$this->_length = 0;;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* recover class by clone
|
* recover class by clone
|
||||||
*/
|
*/
|
||||||
public function __clone()
|
public function __clone()
|
||||||
{
|
{
|
||||||
$this->clean();
|
$this->clean();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array|Closure $callback
|
* @param array|Closure $callback
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function setCallback(array|Closure $callback)
|
public function setCallback(array|Closure $callback)
|
||||||
{
|
{
|
||||||
if (!is_callable($callback, true)) {
|
if (!is_callable($callback, true)) {
|
||||||
throw new Exception('非法回调函数~');
|
throw new Exception('非法回调函数~');
|
||||||
}
|
}
|
||||||
$this->_callback = $callback;
|
$this->_callback = $callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $number
|
* @param int $number
|
||||||
* @return Pagination
|
* @return Pagination
|
||||||
*/
|
*/
|
||||||
public function setOffset(int $number): static
|
public function setOffset(int $number): static
|
||||||
{
|
{
|
||||||
if ($number < 0) {
|
if ($number < 0) {
|
||||||
$number = 0;
|
$number = 0;
|
||||||
}
|
}
|
||||||
$this->_offset = $number;
|
$this->_offset = $number;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $number
|
* @param int $number
|
||||||
* @return Pagination
|
* @return Pagination
|
||||||
*/
|
*/
|
||||||
public function setLimit(int $number): static
|
public function setLimit(int $number): static
|
||||||
{
|
{
|
||||||
if ($number < 1) {
|
if ($number < 1) {
|
||||||
$number = 100;
|
$number = 100;
|
||||||
} else if ($number > 5000) {
|
} else if ($number > 5000) {
|
||||||
$number = 5000;
|
$number = 5000;
|
||||||
}
|
}
|
||||||
$this->_limit = $number;
|
$this->_limit = $number;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $number
|
* @param int $number
|
||||||
* @return Pagination
|
* @return Pagination
|
||||||
*/
|
*/
|
||||||
public function setMax(int $number): static
|
public function setMax(int $number): static
|
||||||
{
|
{
|
||||||
if ($number < 0) {
|
if ($number < 0) {
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
$this->_max = $number;
|
$this->_max = $number;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $param
|
* @param array $param
|
||||||
* @return void
|
* @return void
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function plunk(array $param = [])
|
public function plunk(array $param = [])
|
||||||
{
|
{
|
||||||
$this->loop($param);
|
$this->loop($param);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 轮训
|
* 轮训
|
||||||
* @param $param
|
* @param $param
|
||||||
* @return array
|
* @return array
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function loop($param): array
|
public function loop($param): array
|
||||||
{
|
{
|
||||||
if ($this->_max > 0 && $this->_length >= $this->_max) {
|
if ($this->_max > 0 && $this->_length >= $this->_max) {
|
||||||
return $this->output();
|
return $this->output();
|
||||||
}
|
}
|
||||||
[$length, $data] = $this->get();
|
[$length, $data] = $this->get();
|
||||||
|
|
||||||
$this->executed($data, $param);
|
$this->executed($data, $param);
|
||||||
|
|
||||||
unset($data);
|
unset($data);
|
||||||
if ($length < $this->_limit) {
|
if ($length < $this->_limit) {
|
||||||
return $this->output();
|
return $this->output();
|
||||||
}
|
}
|
||||||
return $this->loop($param);
|
return $this->loop($param);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function output(): array
|
public function output(): array
|
||||||
{
|
{
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $data
|
* @param $data
|
||||||
* @param $param
|
* @param $param
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function executed($data, $param): void
|
private function executed($data, $param): void
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
call_user_func($this->_callback, $data, $param);
|
call_user_func($this->_callback, $data, $param);
|
||||||
} catch (\Throwable $exception) {
|
} catch (\Throwable $exception) {
|
||||||
$this->addError($exception, 'throwable');
|
$this->addError($exception, 'throwable');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array|Collection
|
* @return array|Collection
|
||||||
*/
|
*/
|
||||||
private function get(): Collection|array
|
private function get(): Collection|array
|
||||||
{
|
{
|
||||||
if ($this->_max > 0 && $this->_length + $this->_limit > $this->_max) {
|
if ($this->_max > 0 && $this->_length + $this->_limit > $this->_max) {
|
||||||
$this->_limit = $this->_length + $this->_limit - $this->_max;
|
$this->_limit = $this->_length + $this->_limit - $this->_max;
|
||||||
}
|
}
|
||||||
$data = $this->activeQuery->limit($this->_offset, $this->_limit)->get();
|
$data = $this->activeQuery->limit($this->_offset, $this->_limit)->get();
|
||||||
$this->_offset += $this->_limit;
|
$this->_offset += $this->_limit;
|
||||||
|
|
||||||
if (is_array($data)) {
|
if (is_array($data)) {
|
||||||
$size = count($data);
|
$size = count($data);
|
||||||
} else {
|
} else {
|
||||||
$size = $data->size();
|
$size = $data->size();
|
||||||
}
|
}
|
||||||
$this->_length += $size;
|
$this->_length += $size;
|
||||||
return [$size, $data];
|
return [$size, $data];
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
+54
-54
@@ -1,54 +1,54 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Created by PhpStorm.
|
* Created by PhpStorm.
|
||||||
* User: whwyy
|
* User: whwyy
|
||||||
* Date: 2018/6/27 0027
|
* Date: 2018/6/27 0027
|
||||||
* Time: 17:49
|
* Time: 17:49
|
||||||
*/
|
*/
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database;
|
namespace Database;
|
||||||
|
|
||||||
|
|
||||||
use Database\Traits\QueryTrait;
|
use Database\Traits\QueryTrait;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Query
|
* Class Query
|
||||||
* @package Database
|
* @package Database
|
||||||
*/
|
*/
|
||||||
class Query implements ISqlBuilder
|
class Query implements ISqlBuilder
|
||||||
{
|
{
|
||||||
|
|
||||||
use QueryTrait;
|
use QueryTrait;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->builder = SqlBuilder::builder($this);
|
$this->builder = SqlBuilder::builder($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function getSql(): string
|
public function getSql(): string
|
||||||
{
|
{
|
||||||
return $this->builder->get();
|
return $this->builder->get();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function getCondition(): string
|
public function getCondition(): string
|
||||||
{
|
{
|
||||||
return $this->builder->getCondition();
|
return $this->builder->getCondition();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
+111
-111
@@ -1,111 +1,111 @@
|
|||||||
<?php
|
<?php
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database;
|
namespace Database;
|
||||||
|
|
||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Kiri\Abstracts\Component;
|
use Kiri\Abstracts\Component;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Relation
|
* Class Relation
|
||||||
* @package Kiri\db
|
* @package Kiri\db
|
||||||
*/
|
*/
|
||||||
class Relation extends Component
|
class Relation extends Component
|
||||||
{
|
{
|
||||||
|
|
||||||
private array $_relations = [];
|
private array $_relations = [];
|
||||||
|
|
||||||
/** @var ActiveQuery[] $_query */
|
/** @var ActiveQuery[] $_query */
|
||||||
private array $_query = [];
|
private array $_query = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $identification
|
* @param string $identification
|
||||||
* @param ActiveQuery $query
|
* @param ActiveQuery $query
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function bindIdentification(string $identification, ActiveQuery $query): static
|
public function bindIdentification(string $identification, ActiveQuery $query): static
|
||||||
{
|
{
|
||||||
$this->_query[$identification] = $query;
|
$this->_query[$identification] = $query;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @return ActiveQuery|null
|
* @return ActiveQuery|null
|
||||||
*/
|
*/
|
||||||
public function getQuery(string $name): ?ActiveQuery
|
public function getQuery(string $name): ?ActiveQuery
|
||||||
{
|
{
|
||||||
return $this->_query[$name] ?? null;
|
return $this->_query[$name] ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $identification
|
* @param string $identification
|
||||||
* @param $localValue
|
* @param $localValue
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function first(string $identification, $localValue): mixed
|
public function first(string $identification, $localValue): mixed
|
||||||
{
|
{
|
||||||
$_identification = $identification . '_first_' . $localValue;
|
$_identification = $identification . '_first_' . $localValue;
|
||||||
if (isset($this->_relations[$_identification]) && $this->_relations[$_identification] !== null) {
|
if (isset($this->_relations[$_identification]) && $this->_relations[$_identification] !== null) {
|
||||||
return $this->_relations[$_identification];
|
return $this->_relations[$_identification];
|
||||||
}
|
}
|
||||||
|
|
||||||
$activeModel = $this->_query[$identification]->first();
|
$activeModel = $this->_query[$identification]->first();
|
||||||
if (empty($activeModel)) {
|
if (empty($activeModel)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->_relations[$_identification] = $activeModel;
|
return $this->_relations[$_identification] = $activeModel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $identification
|
* @param string $identification
|
||||||
* @param $localValue
|
* @param $localValue
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function count(string $identification, $localValue): mixed
|
public function count(string $identification, $localValue): mixed
|
||||||
{
|
{
|
||||||
$_identification = $identification . '_count_' . $localValue;
|
$_identification = $identification . '_count_' . $localValue;
|
||||||
if (isset($this->_relations[$_identification]) && $this->_relations[$_identification] !== null) {
|
if (isset($this->_relations[$_identification]) && $this->_relations[$_identification] !== null) {
|
||||||
return $this->_relations[$_identification];
|
return $this->_relations[$_identification];
|
||||||
}
|
}
|
||||||
|
|
||||||
$activeModel = $this->_query[$identification]->count();
|
$activeModel = $this->_query[$identification]->count();
|
||||||
if (empty($activeModel)) {
|
if (empty($activeModel)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->_relations[$_identification] = $activeModel;
|
return $this->_relations[$_identification] = $activeModel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $identification
|
* @param string $identification
|
||||||
* @param $localValue
|
* @param $localValue
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function get(string $identification, $localValue): mixed
|
public function get(string $identification, $localValue): mixed
|
||||||
{
|
{
|
||||||
if (is_array($localValue)) {
|
if (is_array($localValue)) {
|
||||||
$_identification = $identification . '_get_' . implode('_', $localValue);
|
$_identification = $identification . '_get_' . implode('_', $localValue);
|
||||||
} else {
|
} else {
|
||||||
$_identification = $identification . '_get_' . $localValue;
|
$_identification = $identification . '_get_' . $localValue;
|
||||||
}
|
}
|
||||||
if (isset($this->_relations[$_identification]) && $this->_relations[$_identification] !== null) {
|
if (isset($this->_relations[$_identification]) && $this->_relations[$_identification] !== null) {
|
||||||
return $this->_relations[$_identification];
|
return $this->_relations[$_identification];
|
||||||
}
|
}
|
||||||
|
|
||||||
$activeModel = $this->_query[$identification]->get();
|
$activeModel = $this->_query[$identification]->get();
|
||||||
if (empty($activeModel)) {
|
if (empty($activeModel)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->_relations[$_identification] = $activeModel;
|
return $this->_relations[$_identification] = $activeModel;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
+375
-375
@@ -1,375 +1,375 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
namespace Database;
|
namespace Database;
|
||||||
|
|
||||||
|
|
||||||
use Database\Traits\Builder;
|
use Database\Traits\Builder;
|
||||||
use Exception;
|
use Exception;
|
||||||
use JetBrains\PhpStorm\Pure;
|
use JetBrains\PhpStorm\Pure;
|
||||||
use Kiri\Abstracts\Component;
|
use Kiri\Abstracts\Component;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class SqlBuilder
|
* Class SqlBuilder
|
||||||
* @package Database
|
* @package Database
|
||||||
*/
|
*/
|
||||||
class SqlBuilder extends Component
|
class SqlBuilder extends Component
|
||||||
{
|
{
|
||||||
|
|
||||||
use Builder;
|
use Builder;
|
||||||
|
|
||||||
|
|
||||||
public ActiveQuery|Query|null $query;
|
public ActiveQuery|Query|null $query;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ISqlBuilder|null $query
|
* @param ISqlBuilder|null $query
|
||||||
* @return $this
|
* @return $this
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static function builder(ISqlBuilder|null $query): static
|
public static function builder(ISqlBuilder|null $query): static
|
||||||
{
|
{
|
||||||
return new static(['query' => $query]);
|
return new static(['query' => $query]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function getCondition(): string
|
public function getCondition(): string
|
||||||
{
|
{
|
||||||
return $this->conditionToString();
|
return $this->conditionToString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $compiler
|
* @param array $compiler
|
||||||
* @return string
|
* @return string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function hashCompiler(array $compiler): string
|
public function hashCompiler(array $compiler): string
|
||||||
{
|
{
|
||||||
return $this->where($compiler);
|
return $this->where($compiler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $attributes
|
* @param array $attributes
|
||||||
* @return bool|array
|
* @return bool|array
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function update(array $attributes): bool|array
|
public function update(array $attributes): bool|array
|
||||||
{
|
{
|
||||||
[$string, $array] = $this->builderParams($attributes);
|
[$string, $array] = $this->builderParams($attributes);
|
||||||
|
|
||||||
return $this->__updateBuilder($string, $array);
|
return $this->__updateBuilder($string, $array);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $attributes
|
* @param array $attributes
|
||||||
* @param string $opera
|
* @param string $opera
|
||||||
* @return bool|array
|
* @return bool|array
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function mathematics(array $attributes, string $opera = '+'): bool|array
|
public function mathematics(array $attributes, string $opera = '+'): bool|array
|
||||||
{
|
{
|
||||||
$string = [];
|
$string = [];
|
||||||
foreach ($attributes as $attribute => $value) {
|
foreach ($attributes as $attribute => $value) {
|
||||||
$string[] = $attribute . '=' . $attribute . $opera . $value;
|
$string[] = $attribute . '=' . $attribute . $opera . $value;
|
||||||
}
|
}
|
||||||
return $this->__updateBuilder($string, []);
|
return $this->__updateBuilder($string, []);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $string
|
* @param array $string
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @return array|bool
|
* @return array|bool
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function __updateBuilder(array $string, array $params): array|bool
|
private function __updateBuilder(array $string, array $params): array|bool
|
||||||
{
|
{
|
||||||
if (empty($string)) {
|
if (empty($string)) {
|
||||||
return $this->addError('None data update.');
|
return $this->addError('None data update.');
|
||||||
}
|
}
|
||||||
|
|
||||||
$condition = $this->conditionToString();
|
$condition = $this->conditionToString();
|
||||||
if (!empty($condition)) {
|
if (!empty($condition)) {
|
||||||
$condition = ' WHERE ' . $condition;
|
$condition = ' WHERE ' . $condition;
|
||||||
}
|
}
|
||||||
|
|
||||||
$update = 'UPDATE ' . $this->tableName() . ' SET ' . implode(',', $string) . $condition;
|
$update = 'UPDATE ' . $this->tableName() . ' SET ' . implode(',', $string) . $condition;
|
||||||
$update .= $this->builderLimit($this->query, false);
|
$update .= $this->builderLimit($this->query, false);
|
||||||
|
|
||||||
return [$update, $params];
|
return [$update, $params];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $attributes
|
* @param array $attributes
|
||||||
* @param false $isBatch
|
* @param false $isBatch
|
||||||
* @return array
|
* @return array
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function insert(array $attributes, bool $isBatch = false): array
|
public function insert(array $attributes, bool $isBatch = false): array
|
||||||
{
|
{
|
||||||
$update = 'INSERT INTO ' . $this->tableName();
|
$update = 'INSERT INTO ' . $this->tableName();
|
||||||
if ($isBatch === false) {
|
if ($isBatch === false) {
|
||||||
$attributes = [$attributes];
|
$attributes = [$attributes];
|
||||||
}
|
}
|
||||||
$update .= '(' . implode(',', $this->getFields($attributes)) . ') VALUES ';
|
$update .= '(' . implode(',', $this->getFields($attributes)) . ') VALUES ';
|
||||||
|
|
||||||
$order = 0;
|
$order = 0;
|
||||||
$keys = $params = [];
|
$keys = $params = [];
|
||||||
foreach ($attributes as $attribute) {
|
foreach ($attributes as $attribute) {
|
||||||
[$_keys, $params] = $this->builderParams($attribute, true, $params, $order);
|
[$_keys, $params] = $this->builderParams($attribute, true, $params, $order);
|
||||||
|
|
||||||
$keys[] = implode(',', $_keys);
|
$keys[] = implode(',', $_keys);
|
||||||
$order++;
|
$order++;
|
||||||
}
|
}
|
||||||
return [$update . '(' . implode('),(', $keys) . ')', $params];
|
return [$update . '(' . implode('),(', $keys) . ')', $params];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function delete(): string
|
public function delete(): string
|
||||||
{
|
{
|
||||||
$delete = sprintf('DELETE FROM %s ', $this->query->modelClass->getTable());
|
$delete = sprintf('DELETE FROM %s ', $this->query->modelClass->getTable());
|
||||||
|
|
||||||
$this->query->from = null;
|
$this->query->from = null;
|
||||||
|
|
||||||
return $delete . ' WHERE ' . $this->_prefix(true);
|
return $delete . ' WHERE ' . $this->_prefix(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $attributes
|
* @param $attributes
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
#[Pure] private function getFields($attributes): array
|
#[Pure] private function getFields($attributes): array
|
||||||
{
|
{
|
||||||
return array_keys(current($attributes));
|
return array_keys(current($attributes));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $attributes
|
* @param array $attributes
|
||||||
* @param bool $isInsert
|
* @param bool $isInsert
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @param int $order
|
* @param int $order
|
||||||
* @return array[]
|
* @return array[]
|
||||||
* a=:b,
|
* a=:b,
|
||||||
*/
|
*/
|
||||||
#[Pure] private function builderParams(array $attributes, bool $isInsert = false, array $params = [], int $order = 0): array
|
#[Pure] private function builderParams(array $attributes, bool $isInsert = false, array $params = [], int $order = 0): array
|
||||||
{
|
{
|
||||||
$keys = [];
|
$keys = [];
|
||||||
foreach ($attributes as $key => $value) {
|
foreach ($attributes as $key => $value) {
|
||||||
if ($isInsert === true) {
|
if ($isInsert === true) {
|
||||||
$keys[] = ':' . $key . $order;
|
$keys[] = ':' . $key . $order;
|
||||||
$params[$key . $order] = $value;
|
$params[$key . $order] = $value;
|
||||||
} else {
|
} else {
|
||||||
[$keys, $params] = $this->resolveParams($key, $value, $order, $params, $keys);
|
[$keys, $params] = $this->resolveParams($key, $value, $order, $params, $keys);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return [$keys, $params];
|
return [$keys, $params];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $key
|
* @param string $key
|
||||||
* @param mixed $value
|
* @param mixed $value
|
||||||
* @param int $order
|
* @param int $order
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @param array $keys
|
* @param array $keys
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
private function resolveParams(string $key, mixed $value, int $order, array $params, array $keys): array
|
private function resolveParams(string $key, mixed $value, int $order, array $params, array $keys): array
|
||||||
{
|
{
|
||||||
if (
|
if (
|
||||||
str_starts_with($value, '+ ') ||
|
str_starts_with($value, '+ ') ||
|
||||||
str_starts_with($value, '- ')
|
str_starts_with($value, '- ')
|
||||||
) {
|
) {
|
||||||
$keys[] = $key . '=' . $key . ' ' . $value;
|
$keys[] = $key . '=' . $key . ' ' . $value;
|
||||||
} else {
|
} else {
|
||||||
$params[$key . $order] = $value;
|
$params[$key . $order] = $value;
|
||||||
$keys[] = $key . '=:' . $key . $order;
|
$keys[] = $key . '=:' . $key . $order;
|
||||||
}
|
}
|
||||||
return [$keys, $params];
|
return [$keys, $params];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function one(): string
|
public function one(): string
|
||||||
{
|
{
|
||||||
$this->query->limit(0, 1);
|
$this->query->limit(0, 1);
|
||||||
if (empty($this->query->from) && !empty($this->query->modelClass)) {
|
if (empty($this->query->from) && !empty($this->query->modelClass)) {
|
||||||
$this->query->from($this->query->getTable());
|
$this->query->from($this->query->getTable());
|
||||||
}
|
}
|
||||||
return $this->_prefix(true);
|
return $this->_prefix(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function all(): string
|
public function all(): string
|
||||||
{
|
{
|
||||||
if (empty($this->query->from) && !empty($this->query->modelClass)) {
|
if (empty($this->query->from) && !empty($this->query->modelClass)) {
|
||||||
$this->query->from($this->query->getTable());
|
$this->query->from($this->query->getTable());
|
||||||
}
|
}
|
||||||
return $this->_prefix(true);
|
return $this->_prefix(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function count(): string
|
public function count(): string
|
||||||
{
|
{
|
||||||
if (empty($this->query->from) && !empty($this->query->modelClass)) {
|
if (empty($this->query->from) && !empty($this->query->modelClass)) {
|
||||||
$this->query->from($this->query->getTable());
|
$this->query->from($this->query->getTable());
|
||||||
}
|
}
|
||||||
return $this->_prefix(false, true);
|
return $this->_prefix(false, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $table
|
* @param $table
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function columns($table): string
|
public function columns($table): string
|
||||||
{
|
{
|
||||||
return 'SHOW FULL FIELDS FROM ' . $table;
|
return 'SHOW FULL FIELDS FROM ' . $table;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param bool $hasOrder
|
* @param bool $hasOrder
|
||||||
* @param bool $isCount
|
* @param bool $isCount
|
||||||
* @return string
|
* @return string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function _prefix(bool $hasOrder = false, bool $isCount = false): string
|
private function _prefix(bool $hasOrder = false, bool $isCount = false): string
|
||||||
{
|
{
|
||||||
$select = '';
|
$select = '';
|
||||||
if (!empty($this->query->from)) {
|
if (!empty($this->query->from)) {
|
||||||
$select = $this->_selectPrefix($isCount);
|
$select = $this->_selectPrefix($isCount);
|
||||||
}
|
}
|
||||||
$select = $this->_wherePrefix($select);
|
$select = $this->_wherePrefix($select);
|
||||||
if (!empty($this->query->attributes) && is_array($this->query->attributes)) {
|
if (!empty($this->query->attributes) && is_array($this->query->attributes)) {
|
||||||
$select = strtr($select, $this->query->attributes);
|
$select = strtr($select, $this->query->attributes);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!empty($this->query->group)) {
|
if (!empty($this->query->group)) {
|
||||||
$select .= $this->builderGroup($this->query->group);
|
$select .= $this->builderGroup($this->query->group);
|
||||||
}
|
}
|
||||||
if ($hasOrder === true && !empty($this->query->order)) {
|
if ($hasOrder === true && !empty($this->query->order)) {
|
||||||
$select .= $this->builderOrder($this->query->order);
|
$select .= $this->builderOrder($this->query->order);
|
||||||
}
|
}
|
||||||
$sql = $select . $this->builderLimit($this->query);
|
$sql = $select . $this->builderLimit($this->query);
|
||||||
if ($this->query->lock) {
|
if ($this->query->lock) {
|
||||||
$sql .= ' FOR UPDATE';
|
$sql .= ' FOR UPDATE';
|
||||||
}
|
}
|
||||||
return $sql;
|
return $sql;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $select
|
* @param $select
|
||||||
* @return string
|
* @return string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function _wherePrefix($select): string
|
private function _wherePrefix($select): string
|
||||||
{
|
{
|
||||||
$condition = $this->conditionToString();
|
$condition = $this->conditionToString();
|
||||||
if (empty($condition)) {
|
if (empty($condition)) {
|
||||||
return $select;
|
return $select;
|
||||||
} else if (empty($select)) {
|
} else if (empty($select)) {
|
||||||
return $condition;
|
return $condition;
|
||||||
}
|
}
|
||||||
return sprintf('%s WHERE %s', $select, $condition);
|
return sprintf('%s WHERE %s', $select, $condition);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param bool $isCount
|
* @param bool $isCount
|
||||||
* @return string
|
* @return string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function _selectPrefix(bool $isCount): string
|
private function _selectPrefix(bool $isCount): string
|
||||||
{
|
{
|
||||||
$select = $this->builderSelect($this->query->select, $isCount) . ' FROM ' . $this->tableName();
|
$select = $this->builderSelect($this->query->select, $isCount) . ' FROM ' . $this->tableName();
|
||||||
if (!empty($this->query->alias)) {
|
if (!empty($this->query->alias)) {
|
||||||
$select .= $this->builderAlias($this->query->alias);
|
$select .= $this->builderAlias($this->query->alias);
|
||||||
}
|
}
|
||||||
if (!empty($this->query->join)) {
|
if (!empty($this->query->join)) {
|
||||||
$select .= $this->builderJoin($this->query->join);
|
$select .= $this->builderJoin($this->query->join);
|
||||||
}
|
}
|
||||||
return $select;
|
return $select;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param false $isCount
|
* @param false $isCount
|
||||||
* @return string
|
* @return string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function get(bool $isCount = false): string
|
public function get(bool $isCount = false): string
|
||||||
{
|
{
|
||||||
if ($isCount === false) {
|
if ($isCount === false) {
|
||||||
return $this->all();
|
return $this->all();
|
||||||
}
|
}
|
||||||
return $this->count();
|
return $this->count();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function truncate(): string
|
public function truncate(): string
|
||||||
{
|
{
|
||||||
return sprintf('TRUNCATE %s', $this->tableName());
|
return sprintf('TRUNCATE %s', $this->tableName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function conditionToString(): string
|
private function conditionToString(): string
|
||||||
{
|
{
|
||||||
return $this->where($this->query->where);
|
return $this->where($this->query->where);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function tableName(): string
|
public function tableName(): string
|
||||||
{
|
{
|
||||||
if ($this->query->from instanceof \Closure) {
|
if ($this->query->from instanceof \Closure) {
|
||||||
$this->query->from = sprintf('(%s)', $this->query->makeClosureFunction($this->query->from));
|
$this->query->from = sprintf('(%s)', $this->query->makeClosureFunction($this->query->from));
|
||||||
}
|
}
|
||||||
if ($this->query->from instanceof ActiveQuery) {
|
if ($this->query->from instanceof ActiveQuery) {
|
||||||
$this->query->from = sprintf('%s', SqlBuilder::builder($this->query->from)->get($this->query->from));
|
$this->query->from = sprintf('%s', SqlBuilder::builder($this->query->from)->get($this->query->from));
|
||||||
}
|
}
|
||||||
if (empty($this->query->from)) {
|
if (empty($this->query->from)) {
|
||||||
return $this->query->modelClass->getTable();
|
return $this->query->modelClass->getTable();
|
||||||
}
|
}
|
||||||
return $this->query->from;
|
return $this->query->from;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,232 +1,232 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
namespace Database\Traits;
|
namespace Database\Traits;
|
||||||
|
|
||||||
|
|
||||||
use Database\ActiveQuery;
|
use Database\ActiveQuery;
|
||||||
use Database\Base\ConditionClassMap;
|
use Database\Base\ConditionClassMap;
|
||||||
use Database\Condition\HashCondition;
|
use Database\Condition\HashCondition;
|
||||||
use Database\Condition\OrCondition;
|
use Database\Condition\OrCondition;
|
||||||
use Database\Query;
|
use Database\Query;
|
||||||
use Exception;
|
use Exception;
|
||||||
use JetBrains\PhpStorm\Pure;
|
use JetBrains\PhpStorm\Pure;
|
||||||
use Kiri\Exception\NotFindClassException;
|
use Kiri\Exception\NotFindClassException;
|
||||||
use Kiri\Kiri;
|
use Kiri\Kiri;
|
||||||
use ReflectionException;
|
use ReflectionException;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Trait Builder
|
* Trait Builder
|
||||||
* @package Database\Traits
|
* @package Database\Traits
|
||||||
*/
|
*/
|
||||||
trait Builder
|
trait Builder
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $alias
|
* @param $alias
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function builderAlias($alias): string
|
private function builderAlias($alias): string
|
||||||
{
|
{
|
||||||
return " AS " . $alias;
|
return " AS " . $alias;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $table
|
* @param $table
|
||||||
* @return string
|
* @return string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function builderFrom($table): string
|
private function builderFrom($table): string
|
||||||
{
|
{
|
||||||
if ($table instanceof ActiveQuery) {
|
if ($table instanceof ActiveQuery) {
|
||||||
$table = '(' . $table->toSql() . ')';
|
$table = '(' . $table->toSql() . ')';
|
||||||
}
|
}
|
||||||
return " FROM " . $table;
|
return " FROM " . $table;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $join
|
* @param $join
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
#[Pure] private function builderJoin($join): string
|
#[Pure] private function builderJoin($join): string
|
||||||
{
|
{
|
||||||
if (!empty($join)) {
|
if (!empty($join)) {
|
||||||
return ' ' . implode(' ', $join);
|
return ' ' . implode(' ', $join);
|
||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param null $select
|
* @param null $select
|
||||||
* @param bool $isCount
|
* @param bool $isCount
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
#[Pure] private function builderSelect($select = NULL, bool $isCount = false): string
|
#[Pure] private function builderSelect($select = NULL, bool $isCount = false): string
|
||||||
{
|
{
|
||||||
if ($isCount) {
|
if ($isCount) {
|
||||||
return "SELECT COUNT(*)";
|
return "SELECT COUNT(*)";
|
||||||
}
|
}
|
||||||
if (empty($select)) {
|
if (empty($select)) {
|
||||||
return "SELECT *";
|
return "SELECT *";
|
||||||
}
|
}
|
||||||
if (is_array($select)) {
|
if (is_array($select)) {
|
||||||
return "SELECT " . implode(',', $select);
|
return "SELECT " . implode(',', $select);
|
||||||
} else {
|
} else {
|
||||||
return "SELECT " . $select;
|
return "SELECT " . $select;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $group
|
* @param $group
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
private function builderGroup($group): string
|
private function builderGroup($group): string
|
||||||
{
|
{
|
||||||
if (empty($group)) {
|
if (empty($group)) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
return ' GROUP BY ' . $group;
|
return ' GROUP BY ' . $group;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $order
|
* @param $order
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
#[Pure] private function builderOrder($order): string
|
#[Pure] private function builderOrder($order): string
|
||||||
{
|
{
|
||||||
if (!empty($order)) {
|
if (!empty($order)) {
|
||||||
return ' ORDER BY ' . implode(',', $order);
|
return ' ORDER BY ' . implode(',', $order);
|
||||||
} else {
|
} else {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ActiveQuery|Query $query
|
* @param ActiveQuery|Query $query
|
||||||
* @param bool $hasLimit
|
* @param bool $hasLimit
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
#[Pure] private function builderLimit(ActiveQuery|Query $query, bool $hasLimit = true): string
|
#[Pure] private function builderLimit(ActiveQuery|Query $query, bool $hasLimit = true): string
|
||||||
{
|
{
|
||||||
if (!is_numeric($query->limit) || $query->limit < 1) {
|
if (!is_numeric($query->limit) || $query->limit < 1) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
if ($query->offset !== null && $hasLimit) {
|
if ($query->offset !== null && $hasLimit) {
|
||||||
return ' LIMIT ' . $query->offset . ',' . $query->limit;
|
return ' LIMIT ' . $query->offset . ',' . $query->limit;
|
||||||
}
|
}
|
||||||
return ' LIMIT ' . $query->limit;
|
return ' LIMIT ' . $query->limit;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $where
|
* @param $where
|
||||||
* @return string
|
* @return string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function where($where): string
|
private function where($where): string
|
||||||
{
|
{
|
||||||
$_tmp = [];
|
$_tmp = [];
|
||||||
if (empty($where)) return '';
|
if (empty($where)) return '';
|
||||||
if (is_string($where)) return $where;
|
if (is_string($where)) return $where;
|
||||||
foreach ($where as $key => $value) {
|
foreach ($where as $key => $value) {
|
||||||
if (is_null($value)) continue;
|
if (is_null($value)) continue;
|
||||||
|
|
||||||
$_value = $this->resolveCondition($key, $value, $_tmp);
|
$_value = $this->resolveCondition($key, $value, $_tmp);
|
||||||
|
|
||||||
if (empty($_value)) continue;
|
if (empty($_value)) continue;
|
||||||
$_tmp[] = $_value;
|
$_tmp[] = $_value;
|
||||||
}
|
}
|
||||||
if (!empty($_tmp)) {
|
if (!empty($_tmp)) {
|
||||||
return implode(' AND ', $_tmp);
|
return implode(' AND ', $_tmp);
|
||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $field
|
* @param $field
|
||||||
* @param $condition
|
* @param $condition
|
||||||
* @param $_tmp
|
* @param $_tmp
|
||||||
* @return string
|
* @return string
|
||||||
* @throws NotFindClassException
|
* @throws NotFindClassException
|
||||||
* @throws ReflectionException|Exception
|
* @throws ReflectionException|Exception
|
||||||
*/
|
*/
|
||||||
private function resolveCondition($field, $condition, $_tmp): string
|
private function resolveCondition($field, $condition, $_tmp): string
|
||||||
{
|
{
|
||||||
if (is_string($field)) {
|
if (is_string($field)) {
|
||||||
$_value = sprintf('%s = \'%s\'', $field, $condition);
|
$_value = sprintf('%s = \'%s\'', $field, $condition);
|
||||||
} else if (is_string($condition)) {
|
} else if (is_string($condition)) {
|
||||||
$_value = $condition;
|
$_value = $condition;
|
||||||
} else {
|
} else {
|
||||||
$_value = $this->_arrayMap($condition, $_tmp);
|
$_value = $this->_arrayMap($condition, $_tmp);
|
||||||
}
|
}
|
||||||
return $_value;
|
return $_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $condition
|
* @param $condition
|
||||||
* @param $array
|
* @param $array
|
||||||
* @return string
|
* @return string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
private function _arrayMap($condition, $array): string
|
private function _arrayMap($condition, $array): string
|
||||||
{
|
{
|
||||||
if (!isset($condition[0])) {
|
if (!isset($condition[0])) {
|
||||||
return implode(' AND ', $this->_hashMap($condition));
|
return implode(' AND ', $this->_hashMap($condition));
|
||||||
}
|
}
|
||||||
$stroppier = strtoupper($condition[0]);
|
$stroppier = strtoupper($condition[0]);
|
||||||
if (str_contains($stroppier, 'OR')) {
|
if (str_contains($stroppier, 'OR')) {
|
||||||
if (!is_string($condition[2])) {
|
if (!is_string($condition[2])) {
|
||||||
$condition[2] = $this->_hashMap($condition[2]);
|
$condition[2] = $this->_hashMap($condition[2]);
|
||||||
}
|
}
|
||||||
$builder = Kiri::getDi()->get(OrCondition::class);
|
$builder = Kiri::getDi()->get(OrCondition::class);
|
||||||
$builder->setValue($condition[2]);
|
$builder->setValue($condition[2]);
|
||||||
$builder->setColumn($condition[1]);
|
$builder->setColumn($condition[1]);
|
||||||
$builder->oldParams = $array;
|
$builder->oldParams = $array;
|
||||||
} else if (isset(ConditionClassMap::$conditionMap[$stroppier])) {
|
} else if (isset(ConditionClassMap::$conditionMap[$stroppier])) {
|
||||||
$defaultConfig = ConditionClassMap::$conditionMap[$stroppier];
|
$defaultConfig = ConditionClassMap::$conditionMap[$stroppier];
|
||||||
|
|
||||||
$class = $defaultConfig['class'];
|
$class = $defaultConfig['class'];
|
||||||
unset($defaultConfig['class']);
|
unset($defaultConfig['class']);
|
||||||
|
|
||||||
$builder = Kiri::getDi()->get($class, [], $defaultConfig);
|
$builder = Kiri::getDi()->make($class, [], $defaultConfig);
|
||||||
$builder->setValue($condition[2]);
|
$builder->setValue($condition[2]);
|
||||||
$builder->setColumn($condition[1]);
|
$builder->setColumn($condition[1]);
|
||||||
} else {
|
} else {
|
||||||
$builder = Kiri::getDi()->get(HashCondition::class);
|
$builder = Kiri::getDi()->get(HashCondition::class);
|
||||||
$builder->setValue($condition);
|
$builder->setValue($condition);
|
||||||
}
|
}
|
||||||
|
|
||||||
$array[] = $builder->builder();
|
$array[] = $builder->builder();
|
||||||
|
|
||||||
return implode(' AND ', $array);
|
return implode(' AND ', $array);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $condition
|
* @param $condition
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
private function _hashMap($condition): array
|
private function _hashMap($condition): array
|
||||||
{
|
{
|
||||||
$_array = [];
|
$_array = [];
|
||||||
foreach ($condition as $key => $value) {
|
foreach ($condition as $key => $value) {
|
||||||
if (is_null($value)) continue;
|
if (is_null($value)) continue;
|
||||||
|
|
||||||
$value = is_numeric($value) ? $value : '\'' . $value . '\'';
|
$value = is_numeric($value) ? $value : '\'' . $value . '\'';
|
||||||
if (!is_numeric($key)) {
|
if (!is_numeric($key)) {
|
||||||
$_array[] = sprintf('%s = %s', $key, $value);
|
$_array[] = sprintf('%s = %s', $key, $value);
|
||||||
} else {
|
} else {
|
||||||
$_array[] = $value;
|
$_array[] = $value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $_array;
|
return $_array;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,81 +1,81 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* Created by PhpStorm.
|
* Created by PhpStorm.
|
||||||
* User: whwyy
|
* User: whwyy
|
||||||
* Date: 2018/4/4 0004
|
* Date: 2018/4/4 0004
|
||||||
* Time: 15:47
|
* Time: 15:47
|
||||||
*/
|
*/
|
||||||
declare(strict_types=1);
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace Database\Traits;
|
namespace Database\Traits;
|
||||||
|
|
||||||
use Database\ModelInterface;
|
use Database\ModelInterface;
|
||||||
use Database\Collection;
|
use Database\Collection;
|
||||||
use Database\Relation;
|
use Database\Relation;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class HasBase
|
* Class HasBase
|
||||||
* @package Database
|
* @package Database
|
||||||
*
|
*
|
||||||
* @include Query
|
* @include Query
|
||||||
*/
|
*/
|
||||||
abstract class HasBase implements \Database\Traits\Relation
|
abstract class HasBase implements \Database\Traits\Relation
|
||||||
{
|
{
|
||||||
|
|
||||||
/** @var ModelInterface|Collection */
|
/** @var ModelInterface|Collection */
|
||||||
protected Collection|ModelInterface $data;
|
protected Collection|ModelInterface $data;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var ModelInterface
|
* @var ModelInterface
|
||||||
*/
|
*/
|
||||||
protected mixed $model;
|
protected mixed $model;
|
||||||
|
|
||||||
protected mixed $value = [];
|
protected mixed $value = [];
|
||||||
|
|
||||||
|
|
||||||
/** @var Relation $_relation */
|
/** @var Relation $_relation */
|
||||||
protected Relation $_relation;
|
protected Relation $_relation;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* HasBase constructor.
|
* HasBase constructor.
|
||||||
* @param ModelInterface $model
|
* @param ModelInterface $model
|
||||||
* @param $primaryId
|
* @param $primaryId
|
||||||
* @param $value
|
* @param $value
|
||||||
* @param Relation $relation
|
* @param Relation $relation
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function __construct(mixed $model, $primaryId, $value, Relation $relation)
|
public function __construct(mixed $model, $primaryId, $value, Relation $relation)
|
||||||
{
|
{
|
||||||
if (!class_exists($model)) {
|
if (!class_exists($model)) {
|
||||||
throw new Exception('Model must implement ' . $model);
|
throw new Exception('Model must implement ' . $model);
|
||||||
}
|
}
|
||||||
if (!in_array(ModelInterface::class, class_implements($model))) {
|
if (!in_array(ModelInterface::class, class_implements($model))) {
|
||||||
throw new Exception('Model must implement ' . $model);
|
throw new Exception('Model must implement ' . $model);
|
||||||
}
|
}
|
||||||
if (is_array($value)) {
|
if (is_array($value)) {
|
||||||
if (empty($value)) $value = [];
|
if (empty($value)) $value = [];
|
||||||
$_model = $model::query()->whereIn($primaryId, $value);
|
$_model = $model::query()->whereIn($primaryId, $value);
|
||||||
} else {
|
} else {
|
||||||
$_model = $model::query()->where(['t1.' . $primaryId => $value]);
|
$_model = $model::query()->where(['t1.' . $primaryId => $value]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->_relation = $relation->bindIdentification($model, $_model);
|
$this->_relation = $relation->bindIdentification($model, $_model);
|
||||||
|
|
||||||
$this->model = $model;
|
$this->model = $model;
|
||||||
$this->value = $value;
|
$this->value = $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $name
|
* @param $name
|
||||||
* @return mixed
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
public function __get($name): mixed
|
public function __get($name): mixed
|
||||||
{
|
{
|
||||||
if (empty($this->value)) {
|
if (empty($this->value)) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return $this->get();
|
return $this->get();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -1,10 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
namespace Database\Traits;
|
namespace Database\Traits;
|
||||||
|
|
||||||
interface Relation
|
interface Relation
|
||||||
{
|
{
|
||||||
|
|
||||||
public function get(): mixed;
|
public function get(): mixed;
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,77 +1,77 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
namespace Database\Traits;
|
namespace Database\Traits;
|
||||||
|
|
||||||
|
|
||||||
use Database\ActiveQuery;
|
use Database\ActiveQuery;
|
||||||
use Database\ISqlBuilder;
|
use Database\ISqlBuilder;
|
||||||
use Exception;
|
use Exception;
|
||||||
use JetBrains\PhpStorm\Pure;
|
use JetBrains\PhpStorm\Pure;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class CaseWhen
|
* Class CaseWhen
|
||||||
* @package Database\Traits
|
* @package Database\Traits
|
||||||
*/
|
*/
|
||||||
class When
|
class When
|
||||||
{
|
{
|
||||||
|
|
||||||
public ActiveQuery|ISqlBuilder $query;
|
public ActiveQuery|ISqlBuilder $query;
|
||||||
|
|
||||||
|
|
||||||
private array $_condition = [];
|
private array $_condition = [];
|
||||||
|
|
||||||
private string $else = '';
|
private string $else = '';
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* CaseWhen constructor.
|
* CaseWhen constructor.
|
||||||
* @param string $column
|
* @param string $column
|
||||||
* @param ActiveQuery|ISqlBuilder $activeQuery
|
* @param ActiveQuery|ISqlBuilder $activeQuery
|
||||||
*/
|
*/
|
||||||
public function __construct(public string $column, public ActiveQuery|ISqlBuilder $activeQuery)
|
public function __construct(public string $column, public ActiveQuery|ISqlBuilder $activeQuery)
|
||||||
{
|
{
|
||||||
$this->_condition[] = 'CASE ' . $column;
|
$this->_condition[] = 'CASE ' . $column;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string|int $condition
|
* @param string|int $condition
|
||||||
* @param string $then
|
* @param string $then
|
||||||
* @return $this
|
* @return $this
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function when(string|int $condition, string $then): static
|
public function when(string|int $condition, string $then): static
|
||||||
{
|
{
|
||||||
$this->_condition[] = sprintf('WHEN %s THEN %s', $condition, $then);
|
$this->_condition[] = sprintf('WHEN %s THEN %s', $condition, $then);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $alias
|
* @param string $alias
|
||||||
*/
|
*/
|
||||||
public function else(string $alias)
|
public function else(string $alias)
|
||||||
{
|
{
|
||||||
$this->else = $alias;
|
$this->else = $alias;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
#[Pure] public function end(): string
|
#[Pure] public function end(): string
|
||||||
{
|
{
|
||||||
if (empty($this->_condition)) {
|
if (empty($this->_condition)) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
$prefix = implode(' ', $this->_condition);
|
$prefix = implode(' ', $this->_condition);
|
||||||
if (!empty($this->else)) {
|
if (!empty($this->else)) {
|
||||||
$prefix .= ' ELSE ' . $this->else;
|
$prefix .= ' ELSE ' . $this->else;
|
||||||
}
|
}
|
||||||
return '(' . $prefix . ' END)';
|
return '(' . $prefix . ' END)';
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
+3
-3
@@ -12,12 +12,12 @@
|
|||||||
"php": ">=8.0",
|
"php": ">=8.0",
|
||||||
"ext-json": "*",
|
"ext-json": "*",
|
||||||
"ext-pdo": "*",
|
"ext-pdo": "*",
|
||||||
"game-worker/kiri-validator": "^v1.2",
|
"game-worker/kiri-validator": "~v2.0",
|
||||||
"game-worker/kiri-event": "^v1.0"
|
"game-worker/kiri-event": "~v2.0"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
"Database\\": "src/"
|
"Database\\": "./"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"require-dev": {
|
"require-dev": {
|
||||||
|
|||||||
@@ -1,309 +0,0 @@
|
|||||||
<?php
|
|
||||||
/**
|
|
||||||
* Created by PhpStorm.
|
|
||||||
* User: whwyy
|
|
||||||
* Date: 2018/4/4 0004
|
|
||||||
* Time: 14:42
|
|
||||||
*/
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Database;
|
|
||||||
|
|
||||||
use Database\Traits\QueryTrait;
|
|
||||||
use Exception;
|
|
||||||
use Kiri\Abstracts\Component;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class ActiveQuery
|
|
||||||
* @package Database
|
|
||||||
*/
|
|
||||||
class ActiveQuery extends Component implements ISqlBuilder
|
|
||||||
{
|
|
||||||
|
|
||||||
use QueryTrait;
|
|
||||||
|
|
||||||
/** @var array */
|
|
||||||
public array $with = [];
|
|
||||||
|
|
||||||
/** @var bool */
|
|
||||||
public bool $asArray = FALSE;
|
|
||||||
|
|
||||||
/** @var bool */
|
|
||||||
public bool $useCache = FALSE;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var Connection|null
|
|
||||||
*/
|
|
||||||
public ?Connection $db = NULL;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var array
|
|
||||||
* 参数绑定
|
|
||||||
*/
|
|
||||||
public array $attributes = [];
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Comply constructor.
|
|
||||||
* @param $model
|
|
||||||
* @param array $config
|
|
||||||
* @throws
|
|
||||||
*/
|
|
||||||
public function __construct($model, array $config = [])
|
|
||||||
{
|
|
||||||
$this->modelClass = $model;
|
|
||||||
|
|
||||||
$this->builder = SqlBuilder::builder($this);
|
|
||||||
parent::__construct($config);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 清除不完整数据
|
|
||||||
*/
|
|
||||||
public function clear()
|
|
||||||
{
|
|
||||||
$this->db = null;
|
|
||||||
$this->useCache = false;
|
|
||||||
$this->with = [];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $key
|
|
||||||
* @param $value
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function addParam($key, $value): static
|
|
||||||
{
|
|
||||||
$this->attributes[$key] = $value;
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $values
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function addParams(array $values): static
|
|
||||||
{
|
|
||||||
foreach ($values as $key => $val) {
|
|
||||||
$this->addParam($key, $val);
|
|
||||||
}
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $name
|
|
||||||
* @return $this
|
|
||||||
*/
|
|
||||||
public function with($name): static
|
|
||||||
{
|
|
||||||
if (empty($name)) {
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
if (is_string($name)) {
|
|
||||||
$name = explode(',', $name);
|
|
||||||
}
|
|
||||||
foreach ($name as $val) {
|
|
||||||
array_push($this->with, $val);
|
|
||||||
}
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $sql
|
|
||||||
* @param array $params
|
|
||||||
* @return mixed
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function execute($sql, array $params = []): Command
|
|
||||||
{
|
|
||||||
return $this->modelClass->getConnection()->createCommand($sql, $params);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return ModelInterface|null
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function first(): ModelInterface|null
|
|
||||||
{
|
|
||||||
$data = $this->execute($this->builder->one())->one();
|
|
||||||
if (empty($data)) {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
return $this->populate($data);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return string
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function toSql(): string
|
|
||||||
{
|
|
||||||
return $this->builder->get();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array|Collection
|
|
||||||
*/
|
|
||||||
public function get(): Collection|array
|
|
||||||
{
|
|
||||||
return $this->all();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function flush(): array|bool|int|string|null
|
|
||||||
{
|
|
||||||
return $this->execute($this->builder->truncate())->exec();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param int $size
|
|
||||||
* @param callable $callback
|
|
||||||
* @return Pagination
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function page(int $size, callable $callback): Pagination
|
|
||||||
{
|
|
||||||
$pagination = new Pagination($this);
|
|
||||||
$pagination->setOffset(0);
|
|
||||||
$pagination->setLimit($size);
|
|
||||||
$pagination->setCallback($callback);
|
|
||||||
return $pagination;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $field
|
|
||||||
* @param string $setKey
|
|
||||||
*
|
|
||||||
* @return array|null
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function column(string $field, string $setKey = ''): ?array
|
|
||||||
{
|
|
||||||
return $this->all()->column($field, $setKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return array|Collection
|
|
||||||
* @throws
|
|
||||||
*/
|
|
||||||
public function all(): Collection|array
|
|
||||||
{
|
|
||||||
$data = $this->execute($this->builder->all())->all();
|
|
||||||
if (!empty($this->with)){
|
|
||||||
$this->getWith($this->modelClass);
|
|
||||||
}
|
|
||||||
$collect = new Collection($this, $data, $this->modelClass);
|
|
||||||
if ($this->asArray) {
|
|
||||||
return $collect->toArray();
|
|
||||||
}
|
|
||||||
return $collect;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $data
|
|
||||||
* @return ModelInterface
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function populate($data): ModelInterface
|
|
||||||
{
|
|
||||||
return $this->getWith($this->modelClass::populate($data));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param ModelInterface $model
|
|
||||||
* @return ModelInterface
|
|
||||||
*/
|
|
||||||
public function getWith(ModelInterface $model): ModelInterface
|
|
||||||
{
|
|
||||||
if (empty($this->with) || !is_array($this->with)) {
|
|
||||||
return $model;
|
|
||||||
}
|
|
||||||
return $model->setWith($this->with);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return int
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function count(): int
|
|
||||||
{
|
|
||||||
$data = $this->execute($this->builder->count())->one();
|
|
||||||
if ($data && is_array($data)) {
|
|
||||||
return (int)array_shift($data);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $data
|
|
||||||
* @return array|Command|bool|int|string
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function batchUpdate(array $data): Command|array|bool|int|string
|
|
||||||
{
|
|
||||||
$generate = $this->builder->update($data);
|
|
||||||
if (is_bool($generate)) {
|
|
||||||
return $generate;
|
|
||||||
}
|
|
||||||
return $this->execute(...$generate)->exec();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $data
|
|
||||||
* @return bool
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function batchInsert(array $data): bool
|
|
||||||
{
|
|
||||||
[$sql, $params] = $this->builder->insert($data, true);
|
|
||||||
|
|
||||||
|
|
||||||
return $this->execute($sql, $params)->exec(null, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $filed
|
|
||||||
*
|
|
||||||
* @return null
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function value($filed)
|
|
||||||
{
|
|
||||||
return $this->first()[$filed] ?? null;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function exists(): bool
|
|
||||||
{
|
|
||||||
return !empty($this->execute($this->builder->one())->fetchColumn());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param bool $getSql
|
|
||||||
* @return string|bool
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function delete(bool $getSql = false): string|bool
|
|
||||||
{
|
|
||||||
$sql = $this->builder->delete();
|
|
||||||
if ($getSql === false) {
|
|
||||||
return $this->execute($sql)->delete();
|
|
||||||
}
|
|
||||||
return $sql;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,113 +0,0 @@
|
|||||||
<?php
|
|
||||||
declare(strict_types=1);
|
|
||||||
|
|
||||||
namespace Database;
|
|
||||||
|
|
||||||
|
|
||||||
use Annotation\Inject;
|
|
||||||
use Exception;
|
|
||||||
use Kiri\Abstracts\Config;
|
|
||||||
use Kiri\Abstracts\Providers;
|
|
||||||
use Kiri\Application;
|
|
||||||
use Kiri\Events\EventProvider;
|
|
||||||
use Kiri\Exception\ConfigException;
|
|
||||||
use Kiri\Kiri;
|
|
||||||
use Server\Events\OnWorkerStart;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class DatabasesProviders
|
|
||||||
* @package Database
|
|
||||||
*/
|
|
||||||
class DatabasesProviders extends Providers
|
|
||||||
{
|
|
||||||
|
|
||||||
private array $_pooLength = ['min' => 0, 'max' => 1];
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var EventProvider
|
|
||||||
*/
|
|
||||||
#[Inject(EventProvider::class)]
|
|
||||||
public EventProvider $eventProvider;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param Application $application
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function onImport(Application $application)
|
|
||||||
{
|
|
||||||
$application->set('db', $this);
|
|
||||||
|
|
||||||
$this->_pooLength = Config::get('databases.pool', ['min' => 0, 'max' => 1]);
|
|
||||||
|
|
||||||
$this->eventProvider->on(OnWorkerStart::class, [$this, 'createPool']);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $name
|
|
||||||
* @return Connection
|
|
||||||
* @throws ConfigException
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function get($name): Connection
|
|
||||||
{
|
|
||||||
$config = $this->_settings($this->getConfig($name));
|
|
||||||
|
|
||||||
return Kiri::getDi()->get(Connection::class)->configure($config);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @throws ConfigException
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function createPool(OnWorkerStart $onWorkerStart)
|
|
||||||
{
|
|
||||||
$databases = Config::get('databases.connections', []);
|
|
||||||
if (empty($databases)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
$connection = Kiri::getDi()->get(Connection::class);
|
|
||||||
foreach ($databases as $database) {
|
|
||||||
/** @var Connection $connection */
|
|
||||||
$connection->configure($database)->fill();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $database
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
private function _settings($database): array
|
|
||||||
{
|
|
||||||
return [
|
|
||||||
'class' => Connection::class,
|
|
||||||
'id' => $database['id'],
|
|
||||||
'cds' => $database['cds'],
|
|
||||||
'username' => $database['username'],
|
|
||||||
'password' => $database['password'],
|
|
||||||
'tablePrefix' => $database['tablePrefix'],
|
|
||||||
'database' => $database['database'],
|
|
||||||
'maxNumber' => $this->_pooLength['max'],
|
|
||||||
'minNumber' => $this->_pooLength['min'],
|
|
||||||
'charset' => $database['charset'] ?? 'utf8mb4',
|
|
||||||
'slaveConfig' => $database['slaveConfig']
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $name
|
|
||||||
* @return mixed
|
|
||||||
* @throws ConfigException
|
|
||||||
*/
|
|
||||||
public function getConfig($name): mixed
|
|
||||||
{
|
|
||||||
return Config::get('databases.connections.' . $name, null, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,301 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Database\Mysql;
|
|
||||||
|
|
||||||
use Exception;
|
|
||||||
use Kiri\Context;
|
|
||||||
use Kiri\Abstracts\Logger;
|
|
||||||
use Kiri\Kiri;
|
|
||||||
use Kiri\Pool\StopHeartbeatCheck;
|
|
||||||
use PDOStatement;
|
|
||||||
use Swoole\Timer;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
class PDO implements StopHeartbeatCheck
|
|
||||||
{
|
|
||||||
|
|
||||||
const DB_ERROR_MESSAGE = 'The system is busy, please try again later.';
|
|
||||||
|
|
||||||
|
|
||||||
private ?\PDO $pdo = null;
|
|
||||||
|
|
||||||
|
|
||||||
private int $_transaction = 0;
|
|
||||||
|
|
||||||
|
|
||||||
private int $_timer = -1;
|
|
||||||
|
|
||||||
private int $_last = 0;
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $dbname
|
|
||||||
* @param string $cds
|
|
||||||
* @param string $username
|
|
||||||
* @param string $password
|
|
||||||
* @param string $chatset
|
|
||||||
* @throws
|
|
||||||
*/
|
|
||||||
public function __construct(public string $dbname, public string $cds,
|
|
||||||
public string $username, public string $password, public string $chatset = 'utf8mb4')
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public function init()
|
|
||||||
{
|
|
||||||
$this->heartbeat_check();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function inTransaction(): bool
|
|
||||||
{
|
|
||||||
return $this->_transaction > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public function heartbeat_check(): void
|
|
||||||
{
|
|
||||||
if (env('state', 'start') == 'exit') {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if ($this->_timer === -1 && Context::inCoroutine()) {
|
|
||||||
$this->_timer = Timer::tick(1000, function () {
|
|
||||||
try {
|
|
||||||
if (env('state', 'start') == 'exit') {
|
|
||||||
Kiri::getDi()->get(Logger::class)->critical('timer end');
|
|
||||||
$this->stopHeartbeatCheck();
|
|
||||||
}
|
|
||||||
if (time() - $this->_last > 10 * 60) {
|
|
||||||
$this->stopHeartbeatCheck();
|
|
||||||
$this->pdo = null;
|
|
||||||
}
|
|
||||||
} catch (\Throwable $throwable) {
|
|
||||||
error($throwable);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public function stopHeartbeatCheck(): void
|
|
||||||
{
|
|
||||||
if ($this->_timer > -1) {
|
|
||||||
Timer::clear($this->_timer);
|
|
||||||
}
|
|
||||||
$this->_timer = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public function beginTransaction()
|
|
||||||
{
|
|
||||||
if ($this->_transaction == 0) {
|
|
||||||
$this->_pdo()->beginTransaction();
|
|
||||||
}
|
|
||||||
$this->_transaction++;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public function commit()
|
|
||||||
{
|
|
||||||
if ($this->_transaction == 0) {
|
|
||||||
$this->_pdo()->commit();
|
|
||||||
}
|
|
||||||
$this->_transaction--;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public function rollback()
|
|
||||||
{
|
|
||||||
if ($this->_transaction == 0) {
|
|
||||||
$this->_pdo()->rollBack();
|
|
||||||
}
|
|
||||||
$this->_transaction--;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $sql
|
|
||||||
* @param array $params
|
|
||||||
* @return bool|array|null
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function fetchAll(string $sql, array $params = []): bool|null|array
|
|
||||||
{
|
|
||||||
$pdo = $this->queryPrev($sql, $params);
|
|
||||||
|
|
||||||
$result = $pdo->fetchAll(\PDO::FETCH_ASSOC);
|
|
||||||
$pdo->closeCursor();
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $sql
|
|
||||||
* @param array $params
|
|
||||||
* @return bool|array|null
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function fetch(string $sql, array $params = []): bool|null|array
|
|
||||||
{
|
|
||||||
$pdo = $this->queryPrev($sql, $params);
|
|
||||||
|
|
||||||
$result = $pdo->fetch(\PDO::FETCH_ASSOC);
|
|
||||||
$pdo->closeCursor();
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $sql
|
|
||||||
* @param array $params
|
|
||||||
* @return bool|array|null
|
|
||||||
* @throws \Exception
|
|
||||||
*/
|
|
||||||
public function fetchColumn(string $sql, array $params = []): bool|null|array
|
|
||||||
{
|
|
||||||
$pdo = $this->queryPrev($sql, $params);
|
|
||||||
|
|
||||||
$result = $pdo->fetchColumn(\PDO::FETCH_ASSOC);
|
|
||||||
$pdo->closeCursor();
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $sql
|
|
||||||
* @param array $params
|
|
||||||
* @return int
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function count(string $sql, array $params = []): int
|
|
||||||
{
|
|
||||||
$pdo = $this->queryPrev($sql, $params);
|
|
||||||
|
|
||||||
$result = $pdo->rowCount();
|
|
||||||
$pdo->closeCursor();
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $sql
|
|
||||||
* @param array $params
|
|
||||||
* @return PDOStatement
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
private function queryPrev(string $sql, array $params = []): PDOStatement
|
|
||||||
{
|
|
||||||
$this->_last = time();
|
|
||||||
try {
|
|
||||||
if (($statement = $this->_pdo()->query($sql)) === false) {
|
|
||||||
throw new Exception($this->_pdo()->errorInfo()[1]);
|
|
||||||
}
|
|
||||||
return $this->bindValue($statement, $params);
|
|
||||||
} catch (\Throwable $throwable) {
|
|
||||||
|
|
||||||
if (str_contains($throwable->getMessage(), 'MySQL server has gone away')) {
|
|
||||||
$this->pdo = null;
|
|
||||||
|
|
||||||
return $this->queryPrev($sql, $params);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new Exception($throwable->getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param PDOStatement $statement
|
|
||||||
* @param array $params
|
|
||||||
* @return PDOStatement
|
|
||||||
*/
|
|
||||||
private function bindValue(PDOStatement $statement, array $params = []): PDOStatement
|
|
||||||
{
|
|
||||||
if (empty($params)) return $statement;
|
|
||||||
foreach ($params as $key => $param) {
|
|
||||||
$statement->bindValue($key, $param);
|
|
||||||
}
|
|
||||||
return $statement;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $sql
|
|
||||||
* @param $isInsert
|
|
||||||
* @param array $params
|
|
||||||
* @return int
|
|
||||||
* @throws Exception
|
|
||||||
*/
|
|
||||||
public function execute(string $sql, $isInsert, array $params = []): int
|
|
||||||
{
|
|
||||||
$this->_last = time();
|
|
||||||
if (!(($prepare = $this->_pdo()->prepare($sql)) instanceof PDOStatement)) {
|
|
||||||
throw new Exception($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE);
|
|
||||||
}
|
|
||||||
if ($prepare->execute($params) === false) {
|
|
||||||
throw new Exception($prepare->errorInfo()[2] ?? static::DB_ERROR_MESSAGE);
|
|
||||||
}
|
|
||||||
$result = 1;
|
|
||||||
if ($isInsert) {
|
|
||||||
$result = (int)$this->_pdo()->lastInsertId();
|
|
||||||
}
|
|
||||||
$prepare->closeCursor();
|
|
||||||
return $result;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return \PDO
|
|
||||||
*/
|
|
||||||
public function _pdo(): \PDO
|
|
||||||
{
|
|
||||||
if ($this->_timer === -1) {
|
|
||||||
$this->heartbeat_check();
|
|
||||||
}
|
|
||||||
if (!($this->pdo instanceof \PDO)) {
|
|
||||||
$this->pdo = $this->newClient();
|
|
||||||
}
|
|
||||||
return $this->pdo;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return \PDO
|
|
||||||
*/
|
|
||||||
private function newClient(): \PDO
|
|
||||||
{
|
|
||||||
$link = new \PDO('mysql:dbname=' . $this->dbname . ';host=' . $this->cds, $this->username, $this->password, [
|
|
||||||
\PDO::ATTR_EMULATE_PREPARES => false,
|
|
||||||
\PDO::ATTR_CASE => \PDO::CASE_NATURAL,
|
|
||||||
\PDO::ATTR_TIMEOUT => 60,
|
|
||||||
\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
|
|
||||||
\PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES ' . $this->chatset
|
|
||||||
]);
|
|
||||||
$link->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
|
|
||||||
$link->setAttribute(\PDO::ATTR_STRINGIFY_FETCHES, false);
|
|
||||||
$link->setAttribute(\PDO::ATTR_ORACLE_NULLS, \PDO::NULL_EMPTY_STRING);
|
|
||||||
return $link;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,26 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Database;
|
|
||||||
|
|
||||||
|
|
||||||
use Database\Model;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
class TestModel extends Model
|
|
||||||
{
|
|
||||||
|
|
||||||
|
|
||||||
protected string $connection = '';
|
|
||||||
|
|
||||||
|
|
||||||
protected string $table = '';
|
|
||||||
|
|
||||||
|
|
||||||
public ?string $primary = '';
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
TestModel::query()->get();
|
|
||||||
Reference in New Issue
Block a user