This commit is contained in:
2023-12-12 15:35:35 +08:00
parent 510a13e3da
commit e2c0f62532
32 changed files with 922 additions and 1095 deletions
+4 -4
View File
@@ -143,7 +143,7 @@ class ActiveQuery extends Component implements ISqlBuilder
/** /**
* @return ModelInterface|array|null * @return ModelInterface|array|null
* @throws Exception * @throws
*/ */
public function first(): ModelInterface|null|array public function first(): ModelInterface|null|array
{ {
@@ -168,7 +168,7 @@ class ActiveQuery extends Component implements ISqlBuilder
/** /**
* @return Collection * @return Collection
* @throws Exception * @throws
*/ */
public function get(): Collection public function get(): Collection
{ {
@@ -242,7 +242,7 @@ class ActiveQuery extends Component implements ISqlBuilder
/** /**
* @param $data * @param $data
* @return ModelInterface|array * @return ModelInterface|array
* @throws Exception * @throws
*/ */
public function populate($data): ModelInterface|array public function populate($data): ModelInterface|array
{ {
@@ -316,7 +316,7 @@ class ActiveQuery extends Component implements ISqlBuilder
/** /**
* @param bool $getSql * @param bool $getSql
* @return bool|string * @return bool|string
* @throws Exception * @throws
*/ */
public function delete(bool $getSql = FALSE): bool|string public function delete(bool $getSql = FALSE): bool|string
{ {
-41
View File
@@ -1,41 +0,0 @@
<?php
namespace Database\Annotation;
use Attribute;
/**
* Class Get
* @package Annotation\Model
* @deprecated
*/
#[Attribute(Attribute::TARGET_METHOD)] class Get
{
/**
* Get constructor.
* @param string $name
*/
public function __construct(public string $name)
{
}
/**
* @param mixed $class
* @param mixed|null $method
* @return bool
*/
public function execute(mixed $class, mixed $method = null): bool
{
// $keys = \Kiri::getDi()->get(Getter::class);
// $keys->write($this->name, $class, $method);
return true;
}
}
-38
View File
@@ -1,38 +0,0 @@
<?php
namespace Database\Annotation;
use Exception;
/**
* Class Relation
* @package Annotation\Model
*/
#[\Attribute(\Attribute::TARGET_METHOD)] class Relation
{
/**
* Relation constructor.
* @param string $name
*/
public function __construct(public string $name)
{
}
/**
* @param mixed $class
* @param mixed|null $method
* @return bool
* @throws Exception
*/
public function execute(mixed $class, mixed $method = null): bool
{
return true;
}
}
-38
View File
@@ -1,38 +0,0 @@
<?php
namespace Database\Annotation;
/**
* @deprecated
*/
#[\Attribute(\Attribute::TARGET_METHOD)] class Set
{
/**
* Set constructor.
* @param string $name
*/
public function __construct(public string $name)
{
}
/**
* @param mixed $class
* @param mixed|null $method
* @return bool
*/
public function execute(mixed $class, mixed $method = null): bool
{
// $keys = \Kiri::getDi()->get(Setter::class);
// $keys->write($this->name, $class, $method);
return true;
}
}
+124 -122
View File
@@ -26,153 +26,155 @@ use Traversable;
abstract class AbstractCollection extends Component implements \IteratorAggregate, \ArrayAccess, \Arrayable abstract class AbstractCollection extends Component implements \IteratorAggregate, \ArrayAccess, \Arrayable
{ {
/** /**
* @var ModelInterface[] * @var ModelInterface[]
*/ */
protected array $_item = []; protected array $_item = [];
/** /**
* @var ModelInterface|string|null * @var ModelInterface|string|null
*/ */
protected ModelInterface|string|null $model; protected ModelInterface|string|null $model;
/** /**
* @var ActiveQuery * @var ActiveQuery
*/ */
protected ActiveQuery $query; protected ActiveQuery $query;
public function clean() public function clean(): void
{ {
unset($this->query, $this->model, $this->_item); unset($this->query, $this->model, $this->_item);
} }
/** /**
* Collection constructor. * Collection constructor.
* *
* @param $query * @param $query
* @param array $array * @param array $array
* @param ModelInterface|null $model * @param ModelInterface|null $model
* @throws Exception * @throws
*/ */
public function __construct($query, array $array = [], ModelInterface $model = null) public function __construct($query, array $array = [], ModelInterface $model = null)
{ {
$this->_item = $array; $this->_item = $array;
$this->query = $query; $this->query = $query;
$this->model = $model; $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): void
{ {
$this->_item = $item; $this->_item = $item;
} }
/** /**
* @param $model * @param $model
*/ */
public function setModel($model) public function setModel($model): void
{ {
$this->model = $model; $this->model = $model;
} }
/** /**
* @param $item * @param $item
*/ */
public function addItem($item) public function addItem($item): void
{ {
$this->_item[] = $item; $this->_item[] = $item;
} }
/** /**
* @return Traversable|CollectionIterator|ArrayIterator * @return Traversable|CollectionIterator|ArrayIterator
* @throws Exception * @throws
*/ */
public function getIterator(): Traversable|CollectionIterator|ArrayIterator public function getIterator(): Traversable|CollectionIterator|ArrayIterator
{ {
return new CollectionIterator($this->model, $this->_item); return new CollectionIterator($this->model, $this->_item);
} }
/** /**
* @return mixed * @return mixed
* @throws Exception * @throws
*/ */
public function getModel(): ModelInterface public function getModel(): ModelInterface
{ {
return $this->model; return $this->model;
} }
/** /**
* @return ActiveQuery * @return ActiveQuery
*/ */
public function makeNewQuery(): ActiveQuery public function makeNewQuery(): ActiveQuery
{ {
return $this->model::query(); return $this->model::query();
} }
/** /**
* @param mixed $offset * @param mixed $offset
* @return bool * @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 * @param mixed $offset
* @return ModelInterface|null * @return ModelInterface|null
* @throws Exception * @throws
*/ */
public function offsetGet(mixed $offset): ?ModelInterface public function offsetGet(mixed $offset): ?ModelInterface
{ {
if (!$this->offsetExists($offset)) { if (!$this->offsetExists($offset)) {
return NULL; return NULL;
} }
if (!($this->_item[$offset] instanceof ModelInterface)) { if (!($this->_item[$offset] instanceof ModelInterface)) {
return $this->model->populates($this->_item[$offset]); return $this->model->populates($this->_item[$offset]);
} }
return $this->_item[$offset]; return $this->_item[$offset];
} }
/** /**
* @param mixed $offset * @param mixed $offset
* @param mixed $value * @param mixed $value
*/ */
#[ReturnTypeWillChange] public function offsetSet(mixed $offset, mixed $value) #[ReturnTypeWillChange]
{ public function offsetSet(mixed $offset, mixed $value): void
$this->_item[$offset] = $value; {
} $this->_item[$offset] = $value;
}
/** /**
* @param mixed $offset * @param mixed $offset
*/ */
#[ReturnTypeWillChange] public function offsetUnset(mixed $offset) #[ReturnTypeWillChange]
{ public function offsetUnset(mixed $offset): void
if ($this->offsetExists($offset)) { {
unset($this->_item[$offset]); if ($this->offsetExists($offset)) {
} unset($this->_item[$offset]);
} }
}
} }
+32 -33
View File
@@ -16,45 +16,44 @@ use Exception;
class CollectionIterator extends \ArrayIterator class CollectionIterator extends \ArrayIterator
{ {
private ModelInterface|string $model; private ModelInterface|string $model;
/**
/** * CollectionIterator constructor.
* CollectionIterator constructor. * @param $model
* @param $model * @param array $array
* @param array $array * @param int $flags
* @param int $flags * @throws
* @throws Exception */
*/ public function __construct($model, array $array = [], int $flags = 0)
public function __construct($model, array $array = [], int $flags = 0) {
{ $this->model = $model;
$this->model = $model; parent::__construct($array, $flags);
parent::__construct($array, $flags); }
}
/** /**
* @param $current * @param $current
* @return ModelInterface * @return ModelInterface
* @throws Exception * @throws
*/ */
protected function newModel($current): ModelInterface protected function newModel($current): ModelInterface
{ {
return $this->model->populates($current); return $this->model->populates($current);
} }
/** /**
* @throws Exception * @throws
*/ */
public function current(): ModelInterface public function current(): ModelInterface
{ {
if (is_array($current = parent::current())) { if (is_array($current = parent::current())) {
$current = $this->newModel($current); $current = $this->newModel($current);
} }
return $current; return $current;
} }
} }
+36 -36
View File
@@ -173,7 +173,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @return string * @return string
* @throws Exception * @throws
* get last exception or other error * get last exception or other error
*/ */
public function getLastError(): string public function getLastError(): string
@@ -184,7 +184,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @return bool * @return bool
* @throws Exception * @throws
*/ */
public function hasPrimary(): bool public function hasPrimary(): bool
{ {
@@ -193,7 +193,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @return null|string * @return null|string
* @throws Exception * @throws
*/ */
public function getPrimary(): ?string public function getPrimary(): ?string
{ {
@@ -206,7 +206,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @return bool * @return bool
* @throws Exception * @throws
*/ */
public function hasPrimaryValue(): bool public function hasPrimaryValue(): bool
{ {
@@ -219,7 +219,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @return int|null * @return int|null
* @throws Exception * @throws
*/ */
public function getPrimaryValue(): ?int public function getPrimaryValue(): ?int
{ {
@@ -233,7 +233,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
* @param int|string|array $param * @param int|string|array $param
* @param null $db * @param null $db
* @return Model|null * @return Model|null
* @throws Exception * @throws
*/ */
public static function findOne(int|string|array $param, $db = NULL): ?static public static function findOne(int|string|array $param, $db = NULL): ?static
{ {
@@ -254,7 +254,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
* @param int $param * @param int $param
* @param null $db * @param null $db
* @return Model|null * @return Model|null
* @throws Exception * @throws
*/ */
public static function primary(int $param, $db = NULL): ?static public static function primary(int $param, $db = NULL): ?static
{ {
@@ -267,7 +267,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @return mixed * @return mixed
* @throws Exception * @throws
*/ */
public function optimize(): mixed public function optimize(): mixed
{ {
@@ -287,7 +287,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @param int|string|array $condition * @param int|string|array $condition
* @return static|null * @return static|null
* @throws Exception * @throws
*/ */
public static function first(int|string|array $condition): ?static public static function first(int|string|array $condition): ?static
{ {
@@ -298,7 +298,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @param string|array $condition * @param string|array $condition
* @return Collection * @return Collection
* @throws Exception * @throws
*/ */
public static function all(string|array $condition): Collection public static function all(string|array $condition): Collection
{ {
@@ -315,7 +315,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @return ActiveQuery * @return ActiveQuery
* @throws Exception * @throws
*/ */
public static function query(): ActiveQuery public static function query(): ActiveQuery
{ {
@@ -327,7 +327,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @return Connection * @return Connection
* @throws Exception * @throws
*/ */
public function getConnection(): Connection public function getConnection(): Connection
{ {
@@ -340,7 +340,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
* @param array $attributes * @param array $attributes
* *
* @return bool * @return bool
* @throws Exception * @throws
*/ */
protected static function deleteByCondition(array|string|null $condition = NULL, array $attributes = []): bool protected static function deleteByCondition(array|string|null $condition = NULL, array $attributes = []): bool
{ {
@@ -356,7 +356,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @return array * @return array
* @throws Exception * @throws
*/ */
public function getAttributes(): array public function getAttributes(): array
{ {
@@ -402,7 +402,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @param array $param * @param array $param
* @return $this * @return $this
* @throws Exception * @throws
*/ */
public function setAttributes(array $param): static public function setAttributes(array $param): static
{ {
@@ -428,7 +428,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @return $this|bool * @return $this|bool
* @throws Exception * @throws
*/ */
private function insert(): bool|static private function insert(): bool|static
{ {
@@ -449,7 +449,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
* @param array $condition * @param array $condition
* @param array $change * @param array $change
* @return $this|bool * @return $this|bool
* @throws Exception * @throws
*/ */
protected function updateInternal(array $old, array $condition, array $change): bool|static protected function updateInternal(array $old, array $condition, array $change): bool|static
{ {
@@ -469,7 +469,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @return bool|$this * @return bool|$this
* @throws Exception * @throws
*/ */
public function save(): static|bool public function save(): static|bool
{ {
@@ -488,7 +488,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @return array<array, array> * @return array<array, array>
* @throws Exception * @throws
*/ */
private function diff(): array private function diff(): array
{ {
@@ -518,7 +518,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @param array $rule * @param array $rule
* @return bool * @return bool
* @throws Exception * @throws
*/ */
public function validator(array $rule): bool public function validator(array $rule): bool
{ {
@@ -536,7 +536,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @param $rule * @param $rule
* @return Validator * @return Validator
* @throws Exception * @throws
*/ */
private function resolve($rule): Validator private function resolve($rule): Validator
{ {
@@ -552,7 +552,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @param string $name * @param string $name
* @return null * @return null
* @throws Exception * @throws
*/ */
public function getAttribute(string $name) public function getAttribute(string $name)
{ {
@@ -562,7 +562,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @return Relation|null * @return Relation|null
* @throws ReflectionException * @throws
*/ */
public function getRelation(): ?Relation public function getRelation(): ?Relation
{ {
@@ -573,7 +573,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @param $attribute * @param $attribute
* @return bool * @return bool
* @throws Exception * @throws
*/ */
public function has($attribute): bool public function has($attribute): bool
{ {
@@ -582,7 +582,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/**ƒ /**ƒ
* @return string * @return string
* @throws Exception * @throws
*/ */
public function getTable(): string public function getTable(): string
{ {
@@ -613,7 +613,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @param self $model * @param self $model
* @return bool * @return bool
* @throws Exception * @throws
*/ */
public function beforeSave(self $model): bool public function beforeSave(self $model): bool
{ {
@@ -633,7 +633,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @param $name * @param $name
* @param $value * @param $value
* @throws Exception * @throws
*/ */
public function __set($name, $value): void public function __set($name, $value): void
{ {
@@ -652,7 +652,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @param $name * @param $name
* @return mixed * @return mixed
* @throws Exception * @throws
*/ */
public function __get($name): mixed public function __get($name): mixed
{ {
@@ -669,7 +669,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
* @param $name * @param $name
* @param null $value * @param null $value
* @return mixed * @return mixed
* @throws Exception * @throws
*/ */
protected function withPropertyOverride($name, $value = null): mixed protected function withPropertyOverride($name, $value = null): mixed
{ {
@@ -720,7 +720,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @param mixed $offset * @param mixed $offset
* @return bool * @return bool
* @throws Exception * @throws
*/ */
public function offsetExists(mixed $offset): bool public function offsetExists(mixed $offset): bool
{ {
@@ -730,7 +730,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @param mixed $offset * @param mixed $offset
* @return mixed * @return mixed
* @throws Exception * @throws
*/ */
public function offsetGet(mixed $offset): mixed public function offsetGet(mixed $offset): mixed
{ {
@@ -740,18 +740,18 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @param mixed $offset * @param mixed $offset
* @param mixed $value * @param mixed $value
* @throws Exception * @throws
*/ */
#[ReturnTypeWillChange] public function offsetSet(mixed $offset, mixed $value) #[ReturnTypeWillChange] public function offsetSet(mixed $offset, mixed $value): void
{ {
$this->__set($offset, $value); $this->__set($offset, $value);
} }
/** /**
* @param mixed $offset * @param mixed $offset
* @throws Exception * @throws
*/ */
#[ReturnTypeWillChange] public function offsetUnset(mixed $offset) #[ReturnTypeWillChange] public function offsetUnset(mixed $offset): void
{ {
if (!isset($this->_attributes[$offset]) && !isset($this->_oldAttributes[$offset])) { if (!isset($this->_attributes[$offset]) && !isset($this->_oldAttributes[$offset])) {
return; return;
@@ -772,7 +772,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
/** /**
* @return Columns * @return Columns
* @throws Exception * @throws
*/ */
public function getColumns(): Columns public function getColumns(): Columns
{ {
+204 -204
View File
@@ -21,234 +21,234 @@ use JetBrains\PhpStorm\Pure;
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
*/ */
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 array $attributes * @param array $attributes
* @return bool * @return bool
* @throws Exception * @throws
*/ */
public function update(array $attributes): bool public function update(array $attributes): bool
{ {
$lists = []; $lists = [];
$model = $this->getModel(); $model = $this->getModel();
if (!$this->isEmpty()) { if (!$this->isEmpty()) {
foreach ($this->_item as $item) { foreach ($this->_item as $item) {
$lists[] = $item[$model->getPrimary()]; $lists[] = $item[$model->getPrimary()];
} }
return $model::query()->whereIn($model->getPrimary(), $lists)->update($attributes); return $model::query()->whereIn($model->getPrimary(), $lists)->update($attributes);
} }
return false; return false;
} }
/** /**
* @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)) { if (empty($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 count($this->_item); return 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) {
$array[] = $value->toArray(); $array[] = $value->toArray();
} }
$this->_item = []; $this->_item = [];
return $array; return $array;
} }
/** /**
* @throws Exception * @throws
* 批量删除 * 批量删除
*/ */
public function delete(): bool public function delete(): bool
{ {
$model = $this->getModel(); $model = $this->getModel();
if ($model->hasPrimary()) { if ($model->hasPrimary()) {
return $model::query()->whereIn($model->getPrimary(), $this->column($model->getPrimary()))->delete(); return $model::query()->whereIn($model->getPrimary(), $this->column($model->getPrimary()))->delete();
} }
throw new Exception('Must set primary key. if you wante delete'); throw new Exception('Must set primary key. if you wante 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
*/ */
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;
} }
} }
+12 -13
View File
@@ -13,7 +13,6 @@ namespace Database;
use Exception; use Exception;
use Kiri\Abstracts\Component; use Kiri\Abstracts\Component;
use Kiri\Di\Container; use Kiri\Di\Container;
use Kiri\Exception\ConfigException;
use PDO; use PDO;
use PDOStatement; use PDOStatement;
use Throwable; use Throwable;
@@ -32,7 +31,7 @@ class Command extends Component
/** /**
* @param array $params * @param array $params
* @throws Exception * @throws
*/ */
public function __construct(array $params = []) public function __construct(array $params = [])
{ {
@@ -43,7 +42,7 @@ class Command extends Component
/** /**
* @return int|bool * @return int|bool
* @throws Exception * @throws
*/ */
public function incrOrDecr(): int|bool public function incrOrDecr(): int|bool
{ {
@@ -52,7 +51,7 @@ class Command extends Component
/** /**
* @return int|bool * @return int|bool
* @throws Exception * @throws
*/ */
public function save(): int|bool public function save(): int|bool
{ {
@@ -62,7 +61,7 @@ class Command extends Component
/** /**
* @return bool|array * @return bool|array
* @throws Exception * @throws
*/ */
public function all(): bool|array public function all(): bool|array
{ {
@@ -71,7 +70,7 @@ class Command extends Component
/** /**
* @return bool|array|null * @return bool|array|null
* @throws Exception * @throws
*/ */
public function one(): null|bool|array public function one(): null|bool|array
{ {
@@ -80,7 +79,7 @@ class Command extends Component
/** /**
* @return mixed * @return mixed
* @throws Exception * @throws
*/ */
public function fetchColumn(): mixed public function fetchColumn(): mixed
{ {
@@ -91,7 +90,7 @@ class Command extends Component
/** /**
* @param string $method * @param string $method
* @return mixed * @return mixed
* @throws Exception * @throws
*/ */
protected function search(string $method): mixed protected function search(string $method): mixed
{ {
@@ -117,7 +116,7 @@ class Command extends Component
/** /**
* @return int|bool * @return int|bool
* @throws Exception * @throws
*/ */
public function flush(): int|bool public function flush(): int|bool
{ {
@@ -127,7 +126,7 @@ class Command extends Component
/** /**
* @return PDOStatement|int * @return PDOStatement|int
* @throws Exception * @throws
*/ */
private function _prepare(): bool|int private function _prepare(): bool|int
{ {
@@ -186,7 +185,7 @@ class Command extends Component
/** /**
* @return int|bool * @return int|bool
* @throws Exception * @throws
*/ */
public function delete(): int|bool public function delete(): int|bool
{ {
@@ -195,7 +194,7 @@ class Command extends Component
/** /**
* @return int|bool * @return int|bool
* @throws Exception * @throws
*/ */
public function exec(): int|bool public function exec(): int|bool
{ {
@@ -217,7 +216,7 @@ class Command extends Component
/** /**
* @param $sql * @param $sql
* @return $this * @return $this
* @throws Exception * @throws
*/ */
public function setSql($sql): static public function setSql($sql): static
{ {
+15 -15
View File
@@ -10,20 +10,20 @@ namespace Database\Condition;
class HashCondition extends Condition class HashCondition extends Condition
{ {
/** /**
* @return string * @return string
* @throws \Exception * @throws
*/ */
public function builder(): string public function builder(): string
{ {
$array = []; $array = [];
if (count($this->value) < 1) { if (count($this->value) < 1) {
throw new \Exception('Builder data by a empty array.'); throw new \Exception('Builder data by a empty array.');
} }
foreach ($this->value as $key => $value) { foreach ($this->value as $key => $value) {
$array[] = $key . '=' . addslashes($value); $array[] = $key . '=' . addslashes($value);
} }
return implode(' AND ', $array); return implode(' AND ', $array);
} }
} }
+12 -13
View File
@@ -3,7 +3,6 @@ declare(strict_types=1);
namespace Database\Condition; namespace Database\Condition;
use Exception;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
/** /**
@@ -14,17 +13,17 @@ class InCondition extends Condition
{ {
/** /**
* @return string * @return string
* @throws Exception * @throws
*/ */
#[Pure] public function builder(): string #[Pure] public function builder(): string
{ {
if (is_array($this->value)) { if (is_array($this->value)) {
return $this->column . ' IN (' . implode(',', $this->value) . ')'; return $this->column . ' IN (' . implode(',', $this->value) . ')';
} else { } else {
return $this->column . ' IN (' . $this->value . ')'; return $this->column . ' IN (' . $this->value . ')';
} }
} }
} }
+6 -2
View File
@@ -12,9 +12,13 @@ class JsonCondition extends Condition
{ {
public function builder() /**
{ * @return bool
*/
public function builder(): bool
{
// TODO: Implement builder() method. // TODO: Implement builder() method.
return \json_validate($this->value);
} }
} }
+12 -12
View File
@@ -13,17 +13,17 @@ class NotInCondition extends Condition
{ {
/** /**
* @return string|null * @return string|null
* @throws \Exception * @throws
*/ */
#[Pure] public function builder(): ?string #[Pure] public function builder(): ?string
{ {
if (!is_array($this->value)) { if (!is_array($this->value)) {
throw new \Exception('Builder data by a empty string. need array'); throw new \Exception('Builder data by a empty string. need array');
} }
$value = '\'' . implode('\',\'', $this->value) . '\''; $value = '\'' . implode('\',\'', $this->value) . '\'';
return '`' . $this->column . '` not in(' . $value . ')'; return '`' . $this->column . '` not in(' . $value . ')';
} }
} }
+1 -1
View File
@@ -283,7 +283,7 @@ class Connection extends Component
* @param null $sql * @param null $sql
* @param array $attributes * @param array $attributes
* @return Command * @return Command
* @throws Exception * @throws
*/ */
public function createCommand($sql = null, array $attributes = []): Command public function createCommand($sql = null, array $attributes = []): Command
{ {
+15 -29
View File
@@ -15,11 +15,6 @@ 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;
use Kiri\Exception\ConfigException;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use ReflectionException;
use Throwable; use Throwable;
/** /**
@@ -43,7 +38,7 @@ class Db implements ISqlBuilder
/** /**
* @param string|Connection $dbname * @param string|Connection $dbname
* @return Db * @return Db
* @throws Exception * @throws
*/ */
public static function connect(string|Connection $dbname): Db public static function connect(string|Connection $dbname): Db
{ {
@@ -57,9 +52,7 @@ class Db implements ISqlBuilder
/** /**
* @return void * @return void
* @throws ContainerExceptionInterface * @throws
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/ */
public static function beginTransaction(): void public static function beginTransaction(): void
{ {
@@ -71,9 +64,7 @@ class Db implements ISqlBuilder
* @param Closure $closure * @param Closure $closure
* @param mixed ...$params * @param mixed ...$params
* @return mixed * @return mixed
* @throws ContainerExceptionInterface * @throws
* @throws NotFoundExceptionInterface
* @throws Exception
*/ */
public static function Transaction(Closure $closure, ...$params): mixed public static function Transaction(Closure $closure, ...$params): mixed
{ {
@@ -95,9 +86,7 @@ class Db implements ISqlBuilder
/** /**
* @return void * @return void
* @throws ContainerExceptionInterface * @throws
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/ */
public static function commit(): void public static function commit(): void
{ {
@@ -107,9 +96,7 @@ class Db implements ISqlBuilder
/** /**
* @return void * @return void
* @throws ContainerExceptionInterface * @throws
* @throws NotFoundExceptionInterface
* @throws ReflectionException
*/ */
public static function rollback(): void public static function rollback(): void
{ {
@@ -147,7 +134,7 @@ class Db implements ISqlBuilder
/** /**
* @return array|bool * @return array|bool
* @throws Exception * @throws
*/ */
public function get(): array|bool public function get(): array|bool
{ {
@@ -156,7 +143,7 @@ class Db implements ISqlBuilder
/** /**
* @return array|bool|null * @return array|bool|null
* @throws Exception * @throws
*/ */
public function first(): array|bool|null public function first(): array|bool|null
{ {
@@ -165,7 +152,7 @@ class Db implements ISqlBuilder
/** /**
* @return bool|int * @return bool|int
* @throws Exception * @throws
*/ */
public function count(): bool|int public function count(): bool|int
{ {
@@ -174,7 +161,7 @@ class Db implements ISqlBuilder
/** /**
* @return bool|int * @return bool|int
* @throws Exception * @throws
*/ */
public function exists(): bool|int public function exists(): bool|int
{ {
@@ -185,7 +172,7 @@ class Db implements ISqlBuilder
* @param string $sql * @param string $sql
* @param array $attributes * @param array $attributes
* @return array|bool|int|string|null * @return array|bool|int|string|null
* @throws Exception * @throws
*/ */
public function query(string $sql, array $attributes = []): int|bool|array|string|null public function query(string $sql, array $attributes = []): int|bool|array|string|null
{ {
@@ -196,7 +183,7 @@ class Db implements ISqlBuilder
* @param string $sql * @param string $sql
* @param array $attributes * @param array $attributes
* @return array|bool|int|string|null * @return array|bool|int|string|null
* @throws Exception * @throws
*/ */
public function one(string $sql, array $attributes = []): int|bool|array|string|null public function one(string $sql, array $attributes = []): int|bool|array|string|null
{ {
@@ -206,8 +193,7 @@ class Db implements ISqlBuilder
/** /**
* @return bool|int * @return bool|int
* @throws ConfigException * @throws
* @throws Exception
*/ */
public function delete(): bool|int public function delete(): bool|int
{ {
@@ -217,7 +203,7 @@ class Db implements ISqlBuilder
/** /**
* @param string $table * @param string $table
* @return array|bool|null * @return array|bool|null
* @throws Exception * @throws
*/ */
public static function show(string $table): array|bool|null public static function show(string $table): array|bool|null
{ {
@@ -240,7 +226,7 @@ class Db implements ISqlBuilder
* @param Connection|null $connection * @param Connection|null $connection
* @param string $database * @param string $database
* @return array|null * @return array|null
* @throws Exception * @throws
*/ */
public static function desc(string $table, ?Connection $connection = null, string $database = 'db'): ?array public static function desc(string $table, ?Connection $connection = null, string $database = 'db'): ?array
{ {
@@ -256,7 +242,7 @@ class Db implements ISqlBuilder
* @param null|Connection $connection * @param null|Connection $connection
* @param string $database * @param string $database
* @return mixed * @return mixed
* @throws Exception * @throws
*/ */
public static function getDefaultConnection(?Connection $connection = null, string $database = 'db'): Connection public static function getDefaultConnection(?Connection $connection = null, string $database = 'db'): Connection
{ {
+9 -9
View File
@@ -14,14 +14,14 @@ use Kiri;
class HasCount extends HasBase class HasCount extends HasBase
{ {
/** /**
* @return array|null|ModelInterface * @return array|null|ModelInterface
* @throws Exception * @throws
*/ */
public function get(): array|ModelInterface|null public function get(): array|ModelInterface|null
{ {
$relation = Kiri::getDi()->get(Relation::class); $relation = Kiri::getDi()->get(Relation::class);
return $relation->get($this->name); return $relation->get($this->name);
} }
} }
+9 -9
View File
@@ -22,13 +22,13 @@ use Kiri;
class HasMany extends HasBase class HasMany extends HasBase
{ {
/** /**
* @return array|null|Collection * @return array|null|Collection
* @throws Exception * @throws
*/ */
public function get(): array|Collection|null public function get(): array|Collection|null
{ {
$relation = Kiri::getDi()->get(Relation::class); $relation = Kiri::getDi()->get(Relation::class);
return $relation->get($this->name); return $relation->get($this->name);
} }
} }
+10 -10
View File
@@ -20,14 +20,14 @@ use Kiri;
*/ */
class HasOne extends HasBase class HasOne extends HasBase
{ {
/** /**
* @return array|null|ModelInterface * @return array|null|ModelInterface
* @throws Exception * @throws
*/ */
public function get(): array|ModelInterface|null public function get(): array|ModelInterface|null
{ {
$relation = Kiri::getDi()->get(Relation::class); $relation = Kiri::getDi()->get(Relation::class);
return $relation->first($this->name); return $relation->first($this->name);
} }
} }
+2 -3
View File
@@ -31,7 +31,6 @@ class ImplodeCommand extends Command
public string $description = 'php kiri.php db:implode --database users /Users/admin/snowflake-bi/test.sql'; public string $description = 'php kiri.php db:implode --database users /Users/admin/snowflake-bi/test.sql';
/** /**
* @var Channel * @var Channel
*/ */
@@ -43,7 +42,7 @@ class ImplodeCommand extends Command
/** /**
* *
*/ */
protected function configure() protected function configure(): void
{ {
$this->setName('db:implode') $this->setName('db:implode')
->addArgument('path', InputArgument::REQUIRED, "save to path", null) ->addArgument('path', InputArgument::REQUIRED, "save to path", null)
@@ -57,7 +56,7 @@ class ImplodeCommand extends Command
* @param InputInterface $input * @param InputInterface $input
* @param OutputInterface $output * @param OutputInterface $output
* @return int * @return int
* @throws Exception * @throws
*/ */
public function execute(InputInterface $input, OutputInterface $output): int public function execute(InputInterface $input, OutputInterface $output): int
{ {
+20 -24
View File
@@ -11,8 +11,6 @@ namespace Database;
use Exception; use Exception;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
defined('SAVE_FAIL') or define('SAVE_FAIL', 3227); 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('FIND_OR_CREATE_MESSAGE') or define('FIND_OR_CREATE_MESSAGE', 'Create a new model, but the data cannot be empty.');
@@ -32,7 +30,7 @@ class Model extends Base\Model
* @param string $column * @param string $column
* @param int|float $value * @param int|float $value
* @return ModelInterface|false * @return ModelInterface|false
* @throws Exception * @throws
*/ */
public function increment(string $column, int|float $value): bool|ModelInterface public function increment(string $column, int|float $value): bool|ModelInterface
{ {
@@ -48,7 +46,7 @@ class Model extends Base\Model
* @param string $column * @param string $column
* @param int|float $value * @param int|float $value
* @return ModelInterface|false * @return ModelInterface|false
* @throws Exception * @throws
*/ */
public function decrement(string $column, int|float $value): bool|ModelInterface public function decrement(string $column, int|float $value): bool|ModelInterface
{ {
@@ -63,7 +61,7 @@ class Model extends Base\Model
/** /**
* @param array $columns * @param array $columns
* @return ModelInterface|false * @return ModelInterface|false
* @throws Exception * @throws
*/ */
public function increments(array $columns): bool|static public function increments(array $columns): bool|static
{ {
@@ -80,7 +78,7 @@ class Model extends Base\Model
/** /**
* @param array $columns * @param array $columns
* @return ModelInterface|false * @return ModelInterface|false
* @throws Exception * @throws
*/ */
public function decrements(array $columns): bool|static public function decrements(array $columns): bool|static
{ {
@@ -97,8 +95,7 @@ class Model extends Base\Model
* @param array $condition * @param array $condition
* @param array $attributes * @param array $attributes
* @return bool|static * @return bool|static
* @throws ContainerExceptionInterface * @throws
* @throws NotFoundExceptionInterface
*/ */
public static function findOrCreate(array $condition, array $attributes): bool|static public static function findOrCreate(array $condition, array $attributes): bool|static
{ {
@@ -117,8 +114,7 @@ class Model extends Base\Model
* @param array $condition * @param array $condition
* @param array $attributes * @param array $attributes
* @return bool|static * @return bool|static
* @throws ContainerExceptionInterface * @throws
* @throws NotFoundExceptionInterface
*/ */
public static function createOrUpdate(array $condition, array $attributes = []): bool|static public static function createOrUpdate(array $condition, array $attributes = []): bool|static
{ {
@@ -138,14 +134,14 @@ class Model extends Base\Model
* @param $columns * @param $columns
* @param $action * @param $action
* @return array|bool|int|string|null * @return array|bool|int|string|null
* @throws Exception * @throws
*/ */
private function mathematics($columns, $action): int|bool|array|string|null private function mathematics($columns, $action): int|bool|array|string|null
{ {
$condition = [$this->getPrimary() => $this->getPrimaryValue()]; $condition = [$this->getPrimary() => $this->getPrimaryValue()];
$activeQuery = static::query()->where($condition); $activeQuery = static::query()->where($condition);
$create = SqlBuilder::builder($activeQuery)->mathematics($columns, $action); $create = SqlBuilder::builder($activeQuery)->mathematics($columns, $action);
if (is_bool($create)) { if (is_bool($create)) {
return false; return false;
} }
@@ -156,7 +152,7 @@ class Model extends Base\Model
/** /**
* @param array $params * @param array $params
* @return ModelInterface|bool * @return ModelInterface|bool
* @throws Exception * @throws
*/ */
public function update(array $params): static|bool public function update(array $params): static|bool
{ {
@@ -175,7 +171,7 @@ class Model extends Base\Model
/** /**
* @param array $data * @param array $data
* @return bool * @return bool
* @throws Exception * @throws
*/ */
public static function inserts(array $data): bool public static function inserts(array $data): bool
{ {
@@ -187,7 +183,7 @@ class Model extends Base\Model
/** /**
* @return bool * @return bool
* @throws Exception * @throws
*/ */
public function delete(): bool public function delete(): bool
{ {
@@ -209,7 +205,7 @@ class Model extends Base\Model
* @param array $attributes * @param array $attributes
* *
* @return bool * @return bool
* @throws Exception * @throws
*/ */
public static function updateAll(mixed $condition, array $attributes = []): bool public static function updateAll(mixed $condition, array $attributes = []): bool
{ {
@@ -220,7 +216,7 @@ class Model extends Base\Model
/** /**
* @param $condition * @param $condition
* @return array|Collection * @return array|Collection
* @throws Exception * @throws
*/ */
public static function get($condition): Collection|array public static function get($condition): Collection|array
{ {
@@ -233,7 +229,7 @@ class Model extends Base\Model
* @param array $attributes * @param array $attributes
* *
* @return array|Collection * @return array|Collection
* @throws Exception * @throws
*/ */
public static function findAll($condition, array $attributes = []): array|Collection public static function findAll($condition, array $attributes = []): array|Collection
{ {
@@ -247,7 +243,7 @@ class Model extends Base\Model
/** /**
* @return array * @return array
* @throws Exception * @throws
*/ */
public function toArray(): array public function toArray(): array
{ {
@@ -286,7 +282,7 @@ class Model extends Base\Model
* @param $foreignKey * @param $foreignKey
* @param $localKey * @param $localKey
* @return string * @return string
* @throws Exception * @throws
*/ */
private function _hasBase(ModelInterface|string $modelName, $foreignKey, $localKey): string private function _hasBase(ModelInterface|string $modelName, $foreignKey, $localKey): string
{ {
@@ -309,7 +305,7 @@ class Model extends Base\Model
* @param $foreignKey * @param $foreignKey
* @param $localKey * @param $localKey
* @return HasOne|ActiveQuery * @return HasOne|ActiveQuery
* @throws Exception * @throws
*/ */
public function hasOne(ModelInterface|string $modelName, $foreignKey, $localKey): HasOne|ActiveQuery public function hasOne(ModelInterface|string $modelName, $foreignKey, $localKey): HasOne|ActiveQuery
{ {
@@ -322,7 +318,7 @@ class Model extends Base\Model
* @param $foreignKey * @param $foreignKey
* @param $localKey * @param $localKey
* @return ActiveQuery|HasCount * @return ActiveQuery|HasCount
* @throws Exception * @throws
*/ */
public function hasCount(ModelInterface|string $modelName, $foreignKey, $localKey): ActiveQuery|HasCount public function hasCount(ModelInterface|string $modelName, $foreignKey, $localKey): ActiveQuery|HasCount
{ {
@@ -335,7 +331,7 @@ class Model extends Base\Model
* @param $foreignKey * @param $foreignKey
* @param $localKey * @param $localKey
* @return ActiveQuery|HasMany * @return ActiveQuery|HasMany
* @throws Exception * @throws
*/ */
public function hasMany(ModelInterface|string $modelName, $foreignKey, $localKey): ActiveQuery|HasMany public function hasMany(ModelInterface|string $modelName, $foreignKey, $localKey): ActiveQuery|HasMany
{ {
@@ -347,7 +343,7 @@ class Model extends Base\Model
* @param $foreignKey * @param $foreignKey
* @param $localKey * @param $localKey
* @return ActiveQuery|HasMany * @return ActiveQuery|HasMany
* @throws Exception * @throws
*/ */
public function hasIn(ModelInterface|string $modelName, $foreignKey, $localKey): ActiveQuery|HasMany public function hasIn(ModelInterface|string $modelName, $foreignKey, $localKey): ActiveQuery|HasMany
{ {
+17 -18
View File
@@ -13,7 +13,6 @@ namespace Database\Mysql;
use Database\Connection; use Database\Connection;
use Database\SqlBuilder; use Database\SqlBuilder;
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;
@@ -44,7 +43,7 @@ class Columns extends Component
/** /**
* @param string $table * @param string $table
* @return $this * @return $this
* @throws Exception * @throws
*/ */
public function table(string $table): static public function table(string $table): static
{ {
@@ -64,7 +63,7 @@ class Columns extends Component
* @param $key * @param $key
* @param $val * @param $val
* @return string|int|bool|float * @return string|int|bool|float
* @throws Exception * @throws
*/ */
public function fieldFormat($key, $val): string|int|bool|float public function fieldFormat($key, $val): string|int|bool|float
{ {
@@ -115,7 +114,7 @@ class Columns extends Component
* @param string $name * @param string $name
* @param $value * @param $value
* @return mixed * @return mixed
* @throws Exception * @throws
*/ */
public function _decode(string $name, $value): mixed public function _decode(string $name, $value): mixed
{ {
@@ -127,7 +126,7 @@ class Columns extends Component
* @param $val * @param $val
* @param null $format * @param null $format
* @return float|bool|int|string * @return float|bool|int|string
* @throws Exception * @throws
*/ */
public function encode($val, $format = null): float|bool|int|string public function encode($val, $format = null): float|bool|int|string
{ {
@@ -195,7 +194,7 @@ class Columns extends Component
/** /**
* @return mixed * @return mixed
* @throws Exception * @throws
*/ */
public function getFields(): array public function getFields(): array
{ {
@@ -209,7 +208,7 @@ class Columns extends Component
/** /**
* @param string $name * @param string $name
* @return bool * @return bool
* @throws Exception * @throws
*/ */
public function hasField(string $name): bool public function hasField(string $name): bool
{ {
@@ -219,7 +218,7 @@ class Columns extends Component
/** /**
* @return int|string|null * @return int|string|null
* @throws Exception * @throws
*/ */
public function getAutoIncrement(): int|string|null public function getAutoIncrement(): int|string|null
{ {
@@ -229,7 +228,7 @@ class Columns extends Component
/** /**
* @return array|null|string * @return array|null|string
* *
* @throws Exception * @throws
*/ */
public function getPrimaryKeys(): array|string|null public function getPrimaryKeys(): array|string|null
{ {
@@ -242,7 +241,7 @@ class Columns extends Component
/** /**
* @return array|null|string * @return array|null|string
* *
* @throws Exception * @throws
*/ */
#[Pure] public function getFirstPrimary(): array|string|null #[Pure] public function getFirstPrimary(): array|string|null
{ {
@@ -259,7 +258,7 @@ class Columns extends Component
* @param $name * @param $name
* @param null $index * @param null $index
* @return array * @return array
* @throws Exception * @throws
*/ */
private function columns($name, $index = null): array private function columns($name, $index = null): array
{ {
@@ -272,7 +271,7 @@ class Columns extends Component
/** /**
* @return array|static * @return array|static
* @throws Exception * @throws
*/ */
private function getColumns(): array|static private function getColumns(): array|static
{ {
@@ -283,14 +282,14 @@ class Columns extends Component
/** /**
* @param $table * @param $table
* @return array|Columns * @return array|Columns
* @throws Exception * @throws
*/ */
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);
} }
@@ -319,7 +318,7 @@ class Columns extends Component
* @param $item * @param $item
* @param $table * @param $table
*/ */
private function addPrimary($item, $table) private function addPrimary($item, $table): void
{ {
if (!isset($this->_primary[$table])) { if (!isset($this->_primary[$table])) {
$this->_primary[$table] = []; $this->_primary[$table] = [];
@@ -335,7 +334,7 @@ class Columns extends Component
* @param $item * @param $item
* @param $table * @param $table
*/ */
private function addIncrement($item, $table) private function addIncrement($item, $table): void
{ {
if ($item['Extra'] !== 'auto_increment') { if ($item['Extra'] !== 'auto_increment') {
return; return;
@@ -363,7 +362,7 @@ class Columns extends Component
/** /**
* @param null $field * @param null $field
* @return array|string|null * @return array|string|null
* @throws Exception * @throws
*/ */
public function get_fields($field = null): array|string|null public function get_fields($field = null): array|string|null
{ {
@@ -379,7 +378,7 @@ class Columns extends Component
/** /**
* @return array * @return array
* @throws Exception * @throws
*/ */
public function getAllField(): array public function getAllField(): array
{ {
+1 -2
View File
@@ -3,7 +3,6 @@ declare(strict_types=1);
namespace Database\Mysql; namespace Database\Mysql;
use Exception;
use Kiri\Abstracts\Component; use Kiri\Abstracts\Component;
use Database\Connection; use Database\Connection;
@@ -23,7 +22,7 @@ class Schema extends Component
/** /**
* @return Columns|null * @return Columns|null
* @throws Exception * @throws
*/ */
public function getColumns(): ?Columns public function getColumns(): ?Columns
{ {
+10 -11
View File
@@ -5,7 +5,6 @@ namespace Database\Orm;
use Database\Traits\Builder; use Database\Traits\Builder;
use Exception;
/** /**
* Trait Condition * Trait Condition
@@ -15,15 +14,15 @@ trait Condition
{ {
use Builder; use Builder;
/** /**
* @param $query * @param $query
* @return string * @return string
* @throws Exception * @throws
*/ */
public function getWhere($query): string public function getWhere($query): string
{ {
return $this->where($query); return $this->where($query);
} }
} }
+7 -7
View File
@@ -38,7 +38,7 @@ class Pagination extends Component
* PaginationIteration constructor. * PaginationIteration constructor.
* @param ActiveQuery $activeQuery * @param ActiveQuery $activeQuery
* @param array $config * @param array $config
* @throws Exception * @throws
*/ */
public function __construct(ActiveQuery $activeQuery, array $config = []) public function __construct(ActiveQuery $activeQuery, array $config = [])
{ {
@@ -71,9 +71,9 @@ class Pagination extends Component
/** /**
* @param array|Closure $callback * @param array|Closure $callback
* @throws Exception * @throws
*/ */
public function setCallback(array|Closure $callback) public function setCallback(array|Closure $callback): void
{ {
if (!is_callable($callback, true)) { if (!is_callable($callback, true)) {
throw new Exception('非法回调函数~'); throw new Exception('非法回调函数~');
@@ -129,7 +129,7 @@ class Pagination extends Component
/** /**
* @param array $param * @param array $param
* @return void * @return void
* @throws Exception * @throws
*/ */
public function plunk(array $param = []): void public function plunk(array $param = []): void
{ {
@@ -141,7 +141,7 @@ class Pagination extends Component
* 轮训 * 轮训
* @param $param * @param $param
* @return array * @return array
* @throws Exception * @throws
*/ */
public function loop($param): array public function loop($param): array
{ {
@@ -175,7 +175,7 @@ class Pagination extends Component
/** /**
* @param $data * @param $data
* @param $param * @param $param
* @throws Exception * @throws
*/ */
private function executed($data, $param): void private function executed($data, $param): void
{ {
@@ -189,7 +189,7 @@ class Pagination extends Component
/** /**
* @return array|Collection * @return array|Collection
* @throws Exception * @throws
*/ */
private function get(): Collection|array private function get(): Collection|array
{ {
+24 -25
View File
@@ -11,7 +11,6 @@ namespace Database;
use Database\Traits\QueryTrait; use Database\Traits\QueryTrait;
use Exception;
/** /**
* Class Query * Class Query
@@ -20,35 +19,35 @@ use Exception;
class Query implements ISqlBuilder class Query implements ISqlBuilder
{ {
use QueryTrait; use QueryTrait;
/** /**
* @throws Exception * @throws
*/ */
public function __construct() public function __construct()
{ {
$this->builder = SqlBuilder::builder($this); $this->builder = SqlBuilder::builder($this);
} }
/** /**
* @return string * @return string
* @throws Exception * @throws
*/ */
public function getSql(): string public function getSql(): string
{ {
return $this->builder->get(); return $this->builder->get();
} }
/** /**
* @return string * @return string
* @throws Exception * @throws
*/ */
public function getCondition(): string public function getCondition(): string
{ {
return $this->builder->getCondition(); return $this->builder->getCondition();
} }
} }
+69 -79
View File
@@ -4,7 +4,6 @@ declare(strict_types=1);
namespace Database; namespace Database;
use Exception;
use Kiri\Abstracts\Component; use Kiri\Abstracts\Component;
use Kiri\Di\Context; use Kiri\Di\Context;
@@ -15,92 +14,83 @@ use Kiri\Di\Context;
class Relation extends Component class Relation extends Component
{ {
/** @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 $identification * @param string $identification
* @return bool * @return bool
*/ */
public function hasIdentification(string $identification): bool public function hasIdentification(string $identification): bool
{ {
return isset($this->_query[$identification]) && $this->_query[$identification] instanceof ActiveQuery; return isset($this->_query[$identification]) && $this->_query[$identification] instanceof ActiveQuery;
} }
/** /**
* @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
* @return mixed
* @throws Exception
*/
public function first(string $_identification): mixed
{
if (Context::exists($_identification)) {
return Context::get($_identification);
}
$activeModel = $this->_query[$_identification]->first();
if (empty($activeModel)) {
return $this->_query[$_identification]->mock();
}
unset($this->_query[$_identification]);
return Context::set($_identification, $activeModel);
}
/**
* @param string $_identification
* @return mixed
*/
public function count(string $_identification): mixed
{
if (Context::exists($_identification)) {
return Context::get($_identification);
}
$activeModel = $this->_query[$_identification]->count();
if (empty($activeModel)) {
return $this->_query[$_identification]->mock();
}
unset($this->_query[$_identification]);
return Context::set($_identification, $activeModel);
}
/** /**
* @param string $_identification * @param string $_identification
* @return mixed * @return mixed
* @throws Exception * @throws
*/ */
public function get(string $_identification): mixed public function first(string $_identification): mixed
{ {
if (Context::exists($_identification)) { if (Context::exists($_identification)) {
return Context::get($_identification); return Context::get($_identification);
} }
$activeModel = $this->_query[$_identification]->get(); $activeModel = $this->_query[$_identification]->first();
if (empty($activeModel)) { unset($this->_query[$_identification]);
return $activeModel; return Context::set($_identification, $activeModel);
} }
unset($this->_query[$_identification]);
return Context::set($_identification, $activeModel);
} /**
* @param string $_identification
* @return mixed
*/
public function count(string $_identification): mixed
{
if (Context::exists($_identification)) {
return Context::get($_identification);
}
$activeModel = $this->_query[$_identification]->count();
unset($this->_query[$_identification]);
return Context::set($_identification, $activeModel);
}
/**
* @param string $_identification
* @return mixed
* @throws
*/
public function get(string $_identification): mixed
{
if (Context::exists($_identification)) {
return Context::get($_identification);
}
$activeModel = $this->_query[$_identification]->get();
unset($this->_query[$_identification]);
return Context::set($_identification, $activeModel);
}
} }
+15 -16
View File
@@ -6,7 +6,6 @@ namespace Database;
use Database\Traits\Builder; use Database\Traits\Builder;
use Exception;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use Kiri\Abstracts\Component; use Kiri\Abstracts\Component;
@@ -41,7 +40,7 @@ class SqlBuilder extends Component
/** /**
* @param ISqlBuilder|null $query * @param ISqlBuilder|null $query
* @return $this * @return $this
* @throws Exception * @throws
*/ */
public static function builder(ISqlBuilder|null $query): static public static function builder(ISqlBuilder|null $query): static
{ {
@@ -51,7 +50,7 @@ class SqlBuilder extends Component
/** /**
* @return string * @return string
* @throws Exception * @throws
*/ */
public function getCondition(): string public function getCondition(): string
{ {
@@ -62,7 +61,7 @@ class SqlBuilder extends Component
/** /**
* @param array $compiler * @param array $compiler
* @return string * @return string
* @throws Exception * @throws
*/ */
public function hashCompiler(array $compiler): string public function hashCompiler(array $compiler): string
{ {
@@ -73,7 +72,7 @@ class SqlBuilder extends Component
/** /**
* @param array $attributes * @param array $attributes
* @return bool|array * @return bool|array
* @throws Exception * @throws
*/ */
public function update(array $attributes): bool|string public function update(array $attributes): bool|string
{ {
@@ -91,7 +90,7 @@ class SqlBuilder extends Component
* @param array $attributes * @param array $attributes
* @param string $opera * @param string $opera
* @return bool|array * @return bool|array
* @throws Exception * @throws
*/ */
public function mathematics(array $attributes, string $opera = '+'): bool|string public function mathematics(array $attributes, string $opera = '+'): bool|string
{ {
@@ -106,7 +105,7 @@ class SqlBuilder extends Component
/** /**
* @param array $string * @param array $string
* @return string|bool * @return string|bool
* @throws Exception * @throws
*/ */
private function __updateBuilder(array $string): string|bool private function __updateBuilder(array $string): string|bool
{ {
@@ -121,7 +120,7 @@ class SqlBuilder extends Component
* @param array $attributes * @param array $attributes
* @param false $isBatch * @param false $isBatch
* @return array * @return array
* @throws Exception * @throws
*/ */
public function insert(array $attributes, bool $isBatch = false): array public function insert(array $attributes, bool $isBatch = false): array
{ {
@@ -145,7 +144,7 @@ class SqlBuilder extends Component
/** /**
* @return string * @return string
* @throws Exception * @throws
*/ */
public function delete(): string public function delete(): string
{ {
@@ -209,7 +208,7 @@ class SqlBuilder extends Component
/** /**
* @return string * @return string
* @throws Exception * @throws
*/ */
public function one(): string public function one(): string
{ {
@@ -222,7 +221,7 @@ class SqlBuilder extends Component
/** /**
* @return string * @return string
* @throws Exception * @throws
*/ */
public function all(): string public function all(): string
{ {
@@ -235,7 +234,7 @@ class SqlBuilder extends Component
/** /**
* @return string * @return string
* @throws Exception * @throws
*/ */
public function count(): string public function count(): string
{ {
@@ -255,7 +254,7 @@ class SqlBuilder extends Component
/** /**
* @return string * @return string
* @throws Exception * @throws
*/ */
private function _prefix(): string private function _prefix(): string
{ {
@@ -292,7 +291,7 @@ class SqlBuilder extends Component
/** /**
* @param false $isCount * @param false $isCount
* @return string * @return string
* @throws Exception * @throws
*/ */
public function get(bool $isCount = false): string public function get(bool $isCount = false): string
{ {
@@ -305,7 +304,7 @@ class SqlBuilder extends Component
/** /**
* @return string * @return string
* @throws Exception * @throws
*/ */
public function truncate(): string public function truncate(): string
{ {
@@ -315,7 +314,7 @@ class SqlBuilder extends Component
/** /**
* @return string * @return string
* @throws Exception * @throws
*/ */
private function conditionToString(): string private function conditionToString(): string
{ {
+158 -159
View File
@@ -25,177 +25,176 @@ trait Builder
{ {
/** /**
* @param $alias * @param $alias
* @return string * @return string
*/
private function builderAlias($alias): string
{
return " AS " . $alias;
}
/**
* @param $table
* @return string
* @throws Exception
*/
private function builderFrom($table): string
{
if ($table instanceof ActiveQuery) {
$table = '(' . $table->toSql() . ')';
}
return " FROM " . $table;
}
/**
* @param $join
* @return string
*/
#[Pure] private function builderJoin($join): string
{
if (!empty($join)) {
return ' ' . implode(' ', $join);
}
return '';
}
/**
* @param string $select
* @return string
*/
#[Pure] private function builderSelect(string $select = "*"): string
{
return "SELECT " . $select;
}
/**
* @param string $group
* @return string
*/
private function builderGroup(string $group): string
{
if ($group != '') {
return '';
}
return ' GROUP BY ' . $group;
}
/**
* @param $order
* @return string
*/
#[Pure] private function builderOrder($order): string
{
if (!empty($order)) {
return ' ORDER BY ' . implode(',', $order);
} else {
return '';
}
}
/**
* @param ActiveQuery|Query $query
* @return string
*/
#[Pure] private function builderLimit(ActiveQuery|Query $query): string
{
if ($query->limit > 0) {
return ' LIMIT ' . $query->offset . ',' . $query->limit;
} else {
return '';
}
}
/**
* @param array $where
* @return string
*/ */
private function where(array $where): string private function builderAlias($alias): string
{ {
if (count($where) < 1) { return " AS " . $alias;
return ''; }
}
/**
* @param $table
* @return string
* @throws
*/
private function builderFrom($table): string
{
if ($table instanceof ActiveQuery) {
$table = '(' . $table->toSql() . ')';
}
return " FROM " . $table;
}
/**
* @param $join
* @return string
*/
#[Pure] private function builderJoin($join): string
{
if (!empty($join)) {
return ' ' . implode(' ', $join);
}
return '';
}
/**
* @param string $select
* @return string
*/
#[Pure] private function builderSelect(string $select = "*"): string
{
return "SELECT " . $select;
}
/**
* @param string $group
* @return string
*/
private function builderGroup(string $group): string
{
if ($group != '') {
return '';
}
return ' GROUP BY ' . $group;
}
/**
* @param $order
* @return string
*/
#[Pure] private function builderOrder($order): string
{
if (!empty($order)) {
return ' ORDER BY ' . implode(',', $order);
} else {
return '';
}
}
/**
* @param ActiveQuery|Query $query
* @return string
*/
#[Pure] private function builderLimit(ActiveQuery|Query $query): string
{
if ($query->limit > 0) {
return ' LIMIT ' . $query->offset . ',' . $query->limit;
} else {
return '';
}
}
/**
* @param array $where
* @return string
*/
private function where(array $where): string
{
if (count($where) < 1) {
return '';
}
return implode(' AND ', $where); return implode(' AND ', $where);
} }
/** /**
* @param $field * @param $field
* @param $condition * @param $condition
* @param $_tmp * @param $_tmp
* @return string * @return string
* @throws NotFindClassException * @throws
* @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)) { $this->query->pushParam($condition);
$this->query->pushParam($condition); return $field . ' = ?';
return $field . ' = ?'; } else if (is_string($condition)) {
} else if (is_string($condition)) { return $condition;
return $condition; } else {
} else { return $this->_arrayMap($condition, $_tmp);
return $this->_arrayMap($condition, $_tmp); }
} }
}
/** /**
* @param $condition * @param $condition
* @param $array * @param $array
* @return string * @return string
* @throws Exception * @throws
*/ */
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()->make($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) {
$this->query->pushParam($value); $this->query->pushParam($value);
$_array[] = $key . '= ?'; $_array[] = $key . '= ?';
} }
return $_array; return $_array;
} }
} }
+49 -49
View File
@@ -26,54 +26,54 @@ use Kiri;
*/ */
abstract class HasBase implements \Database\Traits\Relation abstract class HasBase implements \Database\Traits\Relation
{ {
/** @var ModelInterface|Collection */
protected mixed $data = null;
/**
* @var ModelInterface
*/
protected mixed $model;
protected mixed $value = 0;
/**
* HasBase constructor.
* @param string $name
*/
public function __construct(public string $name)
{
}
/** /** @var ModelInterface|Collection */
* @param $name protected mixed $data = null;
* @param $arguments
* @return static /**
* @throws \ReflectionException * @var ModelInterface
*/ */
public function __call($name, $arguments) protected mixed $model;
{
if ($name !== 'get') {
$relation = Kiri::getDi()->get(Relation::class); protected mixed $value = 0;
$relation->getQuery($this->name)->$name(...$arguments);
return $this;
} else { /**
return $this->get(); * HasBase constructor.
} * @param string $name
} */
public function __construct(public string $name)
{
/** }
* @param $name
* @return mixed /**
*/ * @param $name
public function __get($name): mixed * @param $arguments
{ * @return static
if ($this->data === null) { * @throws
$this->data = $this->get(); */
} public function __call($name, $arguments)
return $this->data; {
} if ($name !== 'get') {
$relation = Kiri::getDi()->get(Relation::class);
$relation->getQuery($this->name)->$name(...$arguments);
return $this;
} else {
return $this->get();
}
}
/**
* @param $name
* @return mixed
*/
public function __get($name): mixed
{
if ($this->data === null) {
$this->data = $this->get();
}
return $this->data;
}
} }
-24
View File
@@ -38,8 +38,6 @@ trait QueryTrait
public bool $lock = false; public bool $lock = false;
public bool $ifNotWhere = false;
private SqlBuilder $builder; private SqlBuilder $builder;
@@ -310,28 +308,6 @@ trait QueryTrait
return $this->join("INNER JOIN " . $tableName, $alias, $onCondition, $param); return $this->join("INNER JOIN " . $tableName, $alias, $onCondition, $param);
} }
/**
* @param $array
*
* @return string
*/
private function toString($array): string
{
$tmp = [];
if (!is_array($array)) {
return $array;
}
foreach ($array as $key => $val) {
if (is_array($val)) {
$tmp[] = $this->toString($array);
} else {
$tmp[] = $key . '=:' . $key;
$this->attributes[':' . $key] = $val;
}
}
return implode(' AND ', $tmp);
}
/** /**
* @param string $field * @param string $field
* *
+48 -48
View File
@@ -18,61 +18,61 @@ use JetBrains\PhpStorm\Pure;
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;
}
/**
* @param string|int $condition
* @param string $then
* @return $this
* @throws Exception
*/
public function when(string|int $condition, string $then): static
{
$this->_condition[] = sprintf('WHEN %s THEN %s', $condition, $then);
return $this;
}
/**
* @param string $alias
*/
public function else(string $alias): void
{ {
$this->else = $alias; $this->_condition[] = 'CASE ' . $column;
} }
/** /**
* @return string * @param string|int $condition
*/ * @param string $then
#[Pure] public function end(): string * @return $this
{ * @throws
if (empty($this->_condition)) { */
return ''; public function when(string|int $condition, string $then): static
} {
$prefix = implode(' ', $this->_condition); $this->_condition[] = sprintf('WHEN %s THEN %s', $condition, $then);
if (!empty($this->else)) {
$prefix .= ' ELSE ' . $this->else; return $this;
} }
return '(' . $prefix . ' END)';
}
/**
* @param string $alias
*/
public function else(string $alias): void
{
$this->else = $alias;
}
/**
* @return string
*/
#[Pure] public function end(): string
{
if (empty($this->_condition)) {
return '';
}
$prefix = implode(' ', $this->_condition);
if (!empty($this->else)) {
$prefix .= ' ELSE ' . $this->else;
}
return '(' . $prefix . ' END)';
}
} }
+1 -1
View File
@@ -9,7 +9,7 @@
], ],
"license": "MIT", "license": "MIT",
"require": { "require": {
"php": ">=8.0", "php": ">=8.3",
"ext-json": "*", "ext-json": "*",
"ext-pdo": "*", "ext-pdo": "*",
"game-worker/kiri-pool": "~v1.0" "game-worker/kiri-pool": "~v1.0"