eee
This commit is contained in:
+4
-4
@@ -143,7 +143,7 @@ class ActiveQuery extends Component implements ISqlBuilder
|
||||
|
||||
/**
|
||||
* @return ModelInterface|array|null
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function first(): ModelInterface|null|array
|
||||
{
|
||||
@@ -168,7 +168,7 @@ class ActiveQuery extends Component implements ISqlBuilder
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function get(): Collection
|
||||
{
|
||||
@@ -242,7 +242,7 @@ class ActiveQuery extends Component implements ISqlBuilder
|
||||
/**
|
||||
* @param $data
|
||||
* @return ModelInterface|array
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function populate($data): ModelInterface|array
|
||||
{
|
||||
@@ -316,7 +316,7 @@ class ActiveQuery extends Component implements ISqlBuilder
|
||||
/**
|
||||
* @param bool $getSql
|
||||
* @return bool|string
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function delete(bool $getSql = FALSE): bool|string
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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
@@ -26,153 +26,155 @@ use Traversable;
|
||||
abstract class AbstractCollection extends Component implements \IteratorAggregate, \ArrayAccess, \Arrayable
|
||||
{
|
||||
|
||||
/**
|
||||
* @var ModelInterface[]
|
||||
*/
|
||||
protected array $_item = [];
|
||||
/**
|
||||
* @var ModelInterface[]
|
||||
*/
|
||||
protected array $_item = [];
|
||||
|
||||
|
||||
/**
|
||||
* @var ModelInterface|string|null
|
||||
*/
|
||||
protected ModelInterface|string|null $model;
|
||||
/**
|
||||
* @var ModelInterface|string|null
|
||||
*/
|
||||
protected ModelInterface|string|null $model;
|
||||
|
||||
|
||||
/**
|
||||
* @var ActiveQuery
|
||||
*/
|
||||
protected ActiveQuery $query;
|
||||
/**
|
||||
* @var ActiveQuery
|
||||
*/
|
||||
protected ActiveQuery $query;
|
||||
|
||||
|
||||
public function clean()
|
||||
{
|
||||
unset($this->query, $this->model, $this->_item);
|
||||
}
|
||||
public function clean(): void
|
||||
{
|
||||
unset($this->query, $this->model, $this->_item);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Collection constructor.
|
||||
*
|
||||
* @param $query
|
||||
* @param array $array
|
||||
* @param ModelInterface|null $model
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct($query, array $array = [], ModelInterface $model = null)
|
||||
{
|
||||
$this->_item = $array;
|
||||
$this->query = $query;
|
||||
$this->model = $model;
|
||||
/**
|
||||
* Collection constructor.
|
||||
*
|
||||
* @param $query
|
||||
* @param array $array
|
||||
* @param ModelInterface|null $model
|
||||
* @throws
|
||||
*/
|
||||
public function __construct($query, array $array = [], ModelInterface $model = null)
|
||||
{
|
||||
$this->_item = $array;
|
||||
$this->query = $query;
|
||||
$this->model = $model;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
#[Pure] public function getLength(): int
|
||||
{
|
||||
return count($this->_item);
|
||||
}
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
#[Pure] public function getLength(): int
|
||||
{
|
||||
return count($this->_item);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $item
|
||||
*/
|
||||
public function setItems($item)
|
||||
{
|
||||
$this->_item = $item;
|
||||
}
|
||||
/**
|
||||
* @param $item
|
||||
*/
|
||||
public function setItems($item): void
|
||||
{
|
||||
$this->_item = $item;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $model
|
||||
*/
|
||||
public function setModel($model)
|
||||
{
|
||||
$this->model = $model;
|
||||
}
|
||||
/**
|
||||
* @param $model
|
||||
*/
|
||||
public function setModel($model): void
|
||||
{
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $item
|
||||
*/
|
||||
public function addItem($item)
|
||||
{
|
||||
$this->_item[] = $item;
|
||||
}
|
||||
/**
|
||||
* @param $item
|
||||
*/
|
||||
public function addItem($item): void
|
||||
{
|
||||
$this->_item[] = $item;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Traversable|CollectionIterator|ArrayIterator
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getIterator(): Traversable|CollectionIterator|ArrayIterator
|
||||
{
|
||||
return new CollectionIterator($this->model, $this->_item);
|
||||
}
|
||||
/**
|
||||
* @return Traversable|CollectionIterator|ArrayIterator
|
||||
* @throws
|
||||
*/
|
||||
public function getIterator(): Traversable|CollectionIterator|ArrayIterator
|
||||
{
|
||||
return new CollectionIterator($this->model, $this->_item);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getModel(): ModelInterface
|
||||
{
|
||||
return $this->model;
|
||||
}
|
||||
/**
|
||||
* @return mixed
|
||||
* @throws
|
||||
*/
|
||||
public function getModel(): ModelInterface
|
||||
{
|
||||
return $this->model;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return ActiveQuery
|
||||
*/
|
||||
public function makeNewQuery(): ActiveQuery
|
||||
{
|
||||
return $this->model::query();
|
||||
}
|
||||
/**
|
||||
* @return ActiveQuery
|
||||
*/
|
||||
public function makeNewQuery(): ActiveQuery
|
||||
{
|
||||
return $this->model::query();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $offset
|
||||
* @return bool
|
||||
*/
|
||||
public function offsetExists(mixed $offset): bool
|
||||
{
|
||||
return !empty($this->_item) && isset($this->_item[$offset]);
|
||||
}
|
||||
/**
|
||||
* @param mixed $offset
|
||||
* @return bool
|
||||
*/
|
||||
public function offsetExists(mixed $offset): bool
|
||||
{
|
||||
return !empty($this->_item) && isset($this->_item[$offset]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $offset
|
||||
* @return ModelInterface|null
|
||||
* @throws Exception
|
||||
*/
|
||||
public function offsetGet(mixed $offset): ?ModelInterface
|
||||
{
|
||||
if (!$this->offsetExists($offset)) {
|
||||
return NULL;
|
||||
}
|
||||
if (!($this->_item[$offset] instanceof ModelInterface)) {
|
||||
return $this->model->populates($this->_item[$offset]);
|
||||
}
|
||||
return $this->_item[$offset];
|
||||
}
|
||||
/**
|
||||
* @param mixed $offset
|
||||
* @return ModelInterface|null
|
||||
* @throws
|
||||
*/
|
||||
public function offsetGet(mixed $offset): ?ModelInterface
|
||||
{
|
||||
if (!$this->offsetExists($offset)) {
|
||||
return NULL;
|
||||
}
|
||||
if (!($this->_item[$offset] instanceof ModelInterface)) {
|
||||
return $this->model->populates($this->_item[$offset]);
|
||||
}
|
||||
return $this->_item[$offset];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $offset
|
||||
* @param mixed $value
|
||||
*/
|
||||
#[ReturnTypeWillChange] public function offsetSet(mixed $offset, mixed $value)
|
||||
{
|
||||
$this->_item[$offset] = $value;
|
||||
}
|
||||
/**
|
||||
* @param mixed $offset
|
||||
* @param mixed $value
|
||||
*/
|
||||
#[ReturnTypeWillChange]
|
||||
public function offsetSet(mixed $offset, mixed $value): void
|
||||
{
|
||||
$this->_item[$offset] = $value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $offset
|
||||
*/
|
||||
#[ReturnTypeWillChange] public function offsetUnset(mixed $offset)
|
||||
{
|
||||
if ($this->offsetExists($offset)) {
|
||||
unset($this->_item[$offset]);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param mixed $offset
|
||||
*/
|
||||
#[ReturnTypeWillChange]
|
||||
public function offsetUnset(mixed $offset): void
|
||||
{
|
||||
if ($this->offsetExists($offset)) {
|
||||
unset($this->_item[$offset]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+32
-33
@@ -16,45 +16,44 @@ use Exception;
|
||||
class CollectionIterator extends \ArrayIterator
|
||||
{
|
||||
|
||||
private ModelInterface|string $model;
|
||||
private ModelInterface|string $model;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* CollectionIterator constructor.
|
||||
* @param $model
|
||||
* @param array $array
|
||||
* @param int $flags
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct($model, array $array = [], int $flags = 0)
|
||||
{
|
||||
$this->model = $model;
|
||||
parent::__construct($array, $flags);
|
||||
}
|
||||
/**
|
||||
* CollectionIterator constructor.
|
||||
* @param $model
|
||||
* @param array $array
|
||||
* @param int $flags
|
||||
* @throws
|
||||
*/
|
||||
public function __construct($model, array $array = [], int $flags = 0)
|
||||
{
|
||||
$this->model = $model;
|
||||
parent::__construct($array, $flags);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $current
|
||||
* @return ModelInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function newModel($current): ModelInterface
|
||||
{
|
||||
return $this->model->populates($current);
|
||||
}
|
||||
/**
|
||||
* @param $current
|
||||
* @return ModelInterface
|
||||
* @throws
|
||||
*/
|
||||
protected function newModel($current): ModelInterface
|
||||
{
|
||||
return $this->model->populates($current);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function current(): ModelInterface
|
||||
{
|
||||
if (is_array($current = parent::current())) {
|
||||
$current = $this->newModel($current);
|
||||
}
|
||||
return $current;
|
||||
}
|
||||
/**
|
||||
* @throws
|
||||
*/
|
||||
public function current(): ModelInterface
|
||||
{
|
||||
if (is_array($current = parent::current())) {
|
||||
$current = $this->newModel($current);
|
||||
}
|
||||
return $current;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
+36
-36
@@ -173,7 +173,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws Exception
|
||||
* @throws
|
||||
* get last exception or other error
|
||||
*/
|
||||
public function getLastError(): string
|
||||
@@ -184,7 +184,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function hasPrimary(): bool
|
||||
{
|
||||
@@ -193,7 +193,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
|
||||
/**
|
||||
* @return null|string
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function getPrimary(): ?string
|
||||
{
|
||||
@@ -206,7 +206,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function hasPrimaryValue(): bool
|
||||
{
|
||||
@@ -219,7 +219,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
|
||||
/**
|
||||
* @return int|null
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function getPrimaryValue(): ?int
|
||||
{
|
||||
@@ -233,7 +233,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
* @param int|string|array $param
|
||||
* @param null $db
|
||||
* @return Model|null
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
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 null $db
|
||||
* @return Model|null
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public static function primary(int $param, $db = NULL): ?static
|
||||
{
|
||||
@@ -267,7 +267,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function optimize(): mixed
|
||||
{
|
||||
@@ -287,7 +287,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
/**
|
||||
* @param int|string|array $condition
|
||||
* @return static|null
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
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
|
||||
* @return Collection
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public static function all(string|array $condition): Collection
|
||||
{
|
||||
@@ -315,7 +315,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
|
||||
/**
|
||||
* @return ActiveQuery
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public static function query(): ActiveQuery
|
||||
{
|
||||
@@ -327,7 +327,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
|
||||
/**
|
||||
* @return Connection
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function getConnection(): Connection
|
||||
{
|
||||
@@ -340,7 +340,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
* @param array $attributes
|
||||
*
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
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
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function getAttributes(): array
|
||||
{
|
||||
@@ -402,7 +402,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
/**
|
||||
* @param array $param
|
||||
* @return $this
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function setAttributes(array $param): static
|
||||
{
|
||||
@@ -428,7 +428,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
|
||||
/**
|
||||
* @return $this|bool
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
private function insert(): bool|static
|
||||
{
|
||||
@@ -449,7 +449,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
* @param array $condition
|
||||
* @param array $change
|
||||
* @return $this|bool
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
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
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function save(): static|bool
|
||||
{
|
||||
@@ -488,7 +488,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
|
||||
/**
|
||||
* @return array<array, array>
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
private function diff(): array
|
||||
{
|
||||
@@ -518,7 +518,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
/**
|
||||
* @param array $rule
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function validator(array $rule): bool
|
||||
{
|
||||
@@ -536,7 +536,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
/**
|
||||
* @param $rule
|
||||
* @return Validator
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
private function resolve($rule): Validator
|
||||
{
|
||||
@@ -552,7 +552,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
/**
|
||||
* @param string $name
|
||||
* @return null
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function getAttribute(string $name)
|
||||
{
|
||||
@@ -562,7 +562,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
|
||||
/**
|
||||
* @return Relation|null
|
||||
* @throws ReflectionException
|
||||
* @throws
|
||||
*/
|
||||
public function getRelation(): ?Relation
|
||||
{
|
||||
@@ -573,7 +573,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
/**
|
||||
* @param $attribute
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function has($attribute): bool
|
||||
{
|
||||
@@ -582,7 +582,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
|
||||
/**ƒ
|
||||
* @return string
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function getTable(): string
|
||||
{
|
||||
@@ -613,7 +613,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
/**
|
||||
* @param self $model
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function beforeSave(self $model): bool
|
||||
{
|
||||
@@ -633,7 +633,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
/**
|
||||
* @param $name
|
||||
* @param $value
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function __set($name, $value): void
|
||||
{
|
||||
@@ -652,7 +652,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
/**
|
||||
* @param $name
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function __get($name): mixed
|
||||
{
|
||||
@@ -669,7 +669,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
* @param $name
|
||||
* @param null $value
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
protected function withPropertyOverride($name, $value = null): mixed
|
||||
{
|
||||
@@ -720,7 +720,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
/**
|
||||
* @param mixed $offset
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function offsetExists(mixed $offset): bool
|
||||
{
|
||||
@@ -730,7 +730,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
/**
|
||||
* @param mixed $offset
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function offsetGet(mixed $offset): mixed
|
||||
{
|
||||
@@ -740,18 +740,18 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
/**
|
||||
* @param mixed $offset
|
||||
* @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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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])) {
|
||||
return;
|
||||
@@ -772,7 +772,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
|
||||
|
||||
/**
|
||||
* @return Columns
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function getColumns(): Columns
|
||||
{
|
||||
|
||||
+204
-204
@@ -21,234 +21,234 @@ use JetBrains\PhpStorm\Pure;
|
||||
class Collection extends AbstractCollection
|
||||
{
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getItems(): array
|
||||
{
|
||||
// TODO: Change the autogenerated stub
|
||||
return $this->_item;
|
||||
}
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getItems(): array
|
||||
{
|
||||
// TODO: Change the autogenerated stub
|
||||
return $this->_item;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $field
|
||||
*
|
||||
* @return array|null
|
||||
* @throws Exception
|
||||
*/
|
||||
public function values($field): ?array
|
||||
{
|
||||
if (empty($field) || !is_string($field)) {
|
||||
return NULL;
|
||||
}
|
||||
$_tmp = [];
|
||||
$data = $this->toArray();
|
||||
foreach ($data as $val) {
|
||||
/** @var ModelInterface $val */
|
||||
$_tmp[] = $val[$field];
|
||||
}
|
||||
return $_tmp;
|
||||
}
|
||||
/**
|
||||
* @param $field
|
||||
*
|
||||
* @return array|null
|
||||
* @throws
|
||||
*/
|
||||
public function values($field): ?array
|
||||
{
|
||||
if (empty($field) || !is_string($field)) {
|
||||
return NULL;
|
||||
}
|
||||
$_tmp = [];
|
||||
$data = $this->toArray();
|
||||
foreach ($data as $val) {
|
||||
/** @var ModelInterface $val */
|
||||
$_tmp[] = $val[$field];
|
||||
}
|
||||
return $_tmp;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $attributes
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function update(array $attributes): bool
|
||||
{
|
||||
$lists = [];
|
||||
/**
|
||||
* @param array $attributes
|
||||
* @return bool
|
||||
* @throws
|
||||
*/
|
||||
public function update(array $attributes): bool
|
||||
{
|
||||
$lists = [];
|
||||
$model = $this->getModel();
|
||||
if (!$this->isEmpty()) {
|
||||
if (!$this->isEmpty()) {
|
||||
foreach ($this->_item as $item) {
|
||||
$lists[] = $item[$model->getPrimary()];
|
||||
}
|
||||
return $model::query()->whereIn($model->getPrimary(), $lists)->update($attributes);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $field
|
||||
* @return array|null
|
||||
*/
|
||||
public function keyBy(string $field): ?array
|
||||
{
|
||||
$array = $this->toArray();
|
||||
$column = array_flip(array_column($array, $field));
|
||||
foreach ($column as $key => $value) {
|
||||
$column[$key] = $array[$value];
|
||||
}
|
||||
/**
|
||||
* @param string $field
|
||||
* @return array|null
|
||||
*/
|
||||
public function keyBy(string $field): ?array
|
||||
{
|
||||
$array = $this->toArray();
|
||||
$column = array_flip(array_column($array, $field));
|
||||
foreach ($column as $key => $value) {
|
||||
$column[$key] = $array[$value];
|
||||
}
|
||||
|
||||
return $column;
|
||||
}
|
||||
return $column;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function orderRand(): static
|
||||
{
|
||||
shuffle($this->_item);
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function orderRand(): static
|
||||
{
|
||||
shuffle($this->_item);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $start
|
||||
* @param int $length
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
#[Pure] public function slice(int $start = 0, int $length = 20): array
|
||||
{
|
||||
if (empty($this->_item)) {
|
||||
return [];
|
||||
}
|
||||
if (\count($this->_item) < $length) {
|
||||
return $this->_item;
|
||||
} else {
|
||||
return array_slice($this->_item, $start, $length);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param int $start
|
||||
* @param int $length
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
#[Pure] public function slice(int $start = 0, int $length = 20): array
|
||||
{
|
||||
if (empty($this->_item)) {
|
||||
return [];
|
||||
}
|
||||
if (\count($this->_item) < $length) {
|
||||
return $this->_item;
|
||||
} else {
|
||||
return array_slice($this->_item, $start, $length);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $field
|
||||
* @param string $setKey
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
public function column(string $field, string $setKey = ''): ?array
|
||||
{
|
||||
$data = $this->toArray();
|
||||
if (empty($data)) {
|
||||
return [];
|
||||
}
|
||||
if (!empty($setKey) && is_string($setKey)) {
|
||||
return array_column($data, $field, $setKey);
|
||||
} else {
|
||||
return array_column($data, $field);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param string $field
|
||||
* @param string $setKey
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
public function column(string $field, string $setKey = ''): ?array
|
||||
{
|
||||
$data = $this->toArray();
|
||||
if (empty($data)) {
|
||||
return [];
|
||||
}
|
||||
if (!empty($setKey) && is_string($setKey)) {
|
||||
return array_column($data, $field, $setKey);
|
||||
} else {
|
||||
return array_column($data, $field);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $field
|
||||
*
|
||||
* @return float|int|null
|
||||
*/
|
||||
public function sum(string $field): float|int|null
|
||||
{
|
||||
$array = $this->column($field);
|
||||
if (empty($array)) {
|
||||
return NULL;
|
||||
}
|
||||
return array_sum($array);
|
||||
}
|
||||
/**
|
||||
* @param string $field
|
||||
*
|
||||
* @return float|int|null
|
||||
*/
|
||||
public function sum(string $field): float|int|null
|
||||
{
|
||||
$array = $this->column($field);
|
||||
if (empty($array)) {
|
||||
return NULL;
|
||||
}
|
||||
return array_sum($array);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ModelInterface|array
|
||||
*/
|
||||
#[Pure] public function current(): ModelInterface|array
|
||||
{
|
||||
return current($this->_item);
|
||||
}
|
||||
/**
|
||||
* @return ModelInterface|array
|
||||
*/
|
||||
#[Pure] public function current(): ModelInterface|array
|
||||
{
|
||||
return current($this->_item);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
#[Pure] public function size(): int
|
||||
{
|
||||
return count($this->_item);
|
||||
}
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
#[Pure] public function size(): int
|
||||
{
|
||||
return count($this->_item);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @throws
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
$array = [];
|
||||
/** @var Model $value */
|
||||
foreach ($this as $value) {
|
||||
$array[] = $value->toArray();
|
||||
}
|
||||
$this->_item = [];
|
||||
return $array;
|
||||
}
|
||||
/**
|
||||
* @return array
|
||||
* @throws
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
$array = [];
|
||||
/** @var Model $value */
|
||||
foreach ($this as $value) {
|
||||
$array[] = $value->toArray();
|
||||
}
|
||||
$this->_item = [];
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
* 批量删除
|
||||
*/
|
||||
public function delete(): bool
|
||||
{
|
||||
$model = $this->getModel();
|
||||
if ($model->hasPrimary()) {
|
||||
return $model::query()->whereIn($model->getPrimary(), $this->column($model->getPrimary()))->delete();
|
||||
}
|
||||
throw new Exception('Must set primary key. if you wante delete');
|
||||
}
|
||||
/**
|
||||
* @throws
|
||||
* 批量删除
|
||||
*/
|
||||
public function delete(): bool
|
||||
{
|
||||
$model = $this->getModel();
|
||||
if ($model->hasPrimary()) {
|
||||
return $model::query()->whereIn($model->getPrimary(), $this->column($model->getPrimary()))->delete();
|
||||
}
|
||||
throw new Exception('Must set primary key. if you wante delete');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $condition
|
||||
* @return Collection
|
||||
* @throws
|
||||
*/
|
||||
public function filter(array $condition): Collection|static
|
||||
{
|
||||
$_filters = [];
|
||||
if (empty($condition)) {
|
||||
return $this;
|
||||
}
|
||||
foreach ($this as $value) {
|
||||
if (!$this->filterCheck($value, $condition)) {
|
||||
continue;
|
||||
}
|
||||
$_filters[] = $value;
|
||||
}
|
||||
return new Collection($this->query, $_filters, $this->model);
|
||||
}
|
||||
/**
|
||||
* @param array $condition
|
||||
* @return Collection
|
||||
* @throws
|
||||
*/
|
||||
public function filter(array $condition): Collection|static
|
||||
{
|
||||
$_filters = [];
|
||||
if (empty($condition)) {
|
||||
return $this;
|
||||
}
|
||||
foreach ($this as $value) {
|
||||
if (!$this->filterCheck($value, $condition)) {
|
||||
continue;
|
||||
}
|
||||
$_filters[] = $value;
|
||||
}
|
||||
return new Collection($this->query, $_filters, $this->model);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $value
|
||||
* @param $condition
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
private function filterCheck($value, $condition): bool
|
||||
{
|
||||
$_value = $value;
|
||||
if ($_value instanceof ModelInterface) {
|
||||
$_value = $_value->toArray();
|
||||
}
|
||||
$_tmp = array_intersect_key($_value, $condition);
|
||||
if (count(array_diff_assoc($_tmp, $condition)) > 0) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
/**
|
||||
* @param $value
|
||||
* @param $condition
|
||||
* @return bool
|
||||
* @throws
|
||||
*/
|
||||
private function filterCheck($value, $condition): bool
|
||||
{
|
||||
$_value = $value;
|
||||
if ($_value instanceof ModelInterface) {
|
||||
$_value = $_value->toArray();
|
||||
}
|
||||
$_tmp = array_intersect_key($_value, $condition);
|
||||
if (count(array_diff_assoc($_tmp, $condition)) > 0) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $key
|
||||
* @param $value
|
||||
* @return mixed
|
||||
*/
|
||||
public function exists($key, $value): mixed
|
||||
{
|
||||
foreach ($this as $item) {
|
||||
if ($item->$key === $value) {
|
||||
return $item;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* @param $key
|
||||
* @param $value
|
||||
* @return mixed
|
||||
*/
|
||||
public function exists($key, $value): mixed
|
||||
{
|
||||
foreach ($this as $item) {
|
||||
if ($item->$key === $value) {
|
||||
return $item;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[Pure] public function isEmpty(): bool
|
||||
{
|
||||
return $this->size() < 1;
|
||||
}
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
#[Pure] public function isEmpty(): bool
|
||||
{
|
||||
return $this->size() < 1;
|
||||
}
|
||||
}
|
||||
|
||||
+12
-13
@@ -13,7 +13,6 @@ namespace Database;
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Kiri\Di\Container;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use PDO;
|
||||
use PDOStatement;
|
||||
use Throwable;
|
||||
@@ -32,7 +31,7 @@ class Command extends Component
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function __construct(array $params = [])
|
||||
{
|
||||
@@ -43,7 +42,7 @@ class Command extends Component
|
||||
|
||||
/**
|
||||
* @return int|bool
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function incrOrDecr(): int|bool
|
||||
{
|
||||
@@ -52,7 +51,7 @@ class Command extends Component
|
||||
|
||||
/**
|
||||
* @return int|bool
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function save(): int|bool
|
||||
{
|
||||
@@ -62,7 +61,7 @@ class Command extends Component
|
||||
|
||||
/**
|
||||
* @return bool|array
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function all(): bool|array
|
||||
{
|
||||
@@ -71,7 +70,7 @@ class Command extends Component
|
||||
|
||||
/**
|
||||
* @return bool|array|null
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function one(): null|bool|array
|
||||
{
|
||||
@@ -80,7 +79,7 @@ class Command extends Component
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function fetchColumn(): mixed
|
||||
{
|
||||
@@ -91,7 +90,7 @@ class Command extends Component
|
||||
/**
|
||||
* @param string $method
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
protected function search(string $method): mixed
|
||||
{
|
||||
@@ -117,7 +116,7 @@ class Command extends Component
|
||||
|
||||
/**
|
||||
* @return int|bool
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function flush(): int|bool
|
||||
{
|
||||
@@ -127,7 +126,7 @@ class Command extends Component
|
||||
|
||||
/**
|
||||
* @return PDOStatement|int
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
private function _prepare(): bool|int
|
||||
{
|
||||
@@ -186,7 +185,7 @@ class Command extends Component
|
||||
|
||||
/**
|
||||
* @return int|bool
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function delete(): int|bool
|
||||
{
|
||||
@@ -195,7 +194,7 @@ class Command extends Component
|
||||
|
||||
/**
|
||||
* @return int|bool
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function exec(): int|bool
|
||||
{
|
||||
@@ -217,7 +216,7 @@ class Command extends Component
|
||||
/**
|
||||
* @param $sql
|
||||
* @return $this
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function setSql($sql): static
|
||||
{
|
||||
|
||||
+15
-15
@@ -10,20 +10,20 @@ namespace Database\Condition;
|
||||
class HashCondition extends Condition
|
||||
{
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function builder(): string
|
||||
{
|
||||
$array = [];
|
||||
if (count($this->value) < 1) {
|
||||
throw new \Exception('Builder data by a empty array.');
|
||||
}
|
||||
foreach ($this->value as $key => $value) {
|
||||
$array[] = $key . '=' . addslashes($value);
|
||||
}
|
||||
return implode(' AND ', $array);
|
||||
}
|
||||
/**
|
||||
* @return string
|
||||
* @throws
|
||||
*/
|
||||
public function builder(): string
|
||||
{
|
||||
$array = [];
|
||||
if (count($this->value) < 1) {
|
||||
throw new \Exception('Builder data by a empty array.');
|
||||
}
|
||||
foreach ($this->value as $key => $value) {
|
||||
$array[] = $key . '=' . addslashes($value);
|
||||
}
|
||||
return implode(' AND ', $array);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+12
-13
@@ -3,7 +3,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace Database\Condition;
|
||||
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
|
||||
/**
|
||||
@@ -14,17 +13,17 @@ class InCondition extends Condition
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
#[Pure] public function builder(): string
|
||||
{
|
||||
if (is_array($this->value)) {
|
||||
return $this->column . ' IN (' . implode(',', $this->value) . ')';
|
||||
} else {
|
||||
return $this->column . ' IN (' . $this->value . ')';
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @return string
|
||||
* @throws
|
||||
*/
|
||||
#[Pure] public function builder(): string
|
||||
{
|
||||
if (is_array($this->value)) {
|
||||
return $this->column . ' IN (' . implode(',', $this->value) . ')';
|
||||
} else {
|
||||
return $this->column . ' IN (' . $this->value . ')';
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,9 +12,13 @@ class JsonCondition extends Condition
|
||||
{
|
||||
|
||||
|
||||
public function builder()
|
||||
{
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function builder(): bool
|
||||
{
|
||||
// TODO: Implement builder() method.
|
||||
return \json_validate($this->value);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -13,17 +13,17 @@ class NotInCondition extends Condition
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @return string|null
|
||||
* @throws \Exception
|
||||
*/
|
||||
#[Pure] public function builder(): ?string
|
||||
{
|
||||
if (!is_array($this->value)) {
|
||||
throw new \Exception('Builder data by a empty string. need array');
|
||||
}
|
||||
$value = '\'' . implode('\',\'', $this->value) . '\'';
|
||||
return '`' . $this->column . '` not in(' . $value . ')';
|
||||
}
|
||||
/**
|
||||
* @return string|null
|
||||
* @throws
|
||||
*/
|
||||
#[Pure] public function builder(): ?string
|
||||
{
|
||||
if (!is_array($this->value)) {
|
||||
throw new \Exception('Builder data by a empty string. need array');
|
||||
}
|
||||
$value = '\'' . implode('\',\'', $this->value) . '\'';
|
||||
return '`' . $this->column . '` not in(' . $value . ')';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -283,7 +283,7 @@ class Connection extends Component
|
||||
* @param null $sql
|
||||
* @param array $attributes
|
||||
* @return Command
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function createCommand($sql = null, array $attributes = []): Command
|
||||
{
|
||||
|
||||
@@ -15,11 +15,6 @@ use Database\Affair\Commit;
|
||||
use Database\Affair\Rollback;
|
||||
use Database\Traits\QueryTrait;
|
||||
use Exception;
|
||||
use Kiri;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use ReflectionException;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
@@ -43,7 +38,7 @@ class Db implements ISqlBuilder
|
||||
/**
|
||||
* @param string|Connection $dbname
|
||||
* @return Db
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public static function connect(string|Connection $dbname): Db
|
||||
{
|
||||
@@ -57,9 +52,7 @@ class Db implements ISqlBuilder
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws ReflectionException
|
||||
* @throws
|
||||
*/
|
||||
public static function beginTransaction(): void
|
||||
{
|
||||
@@ -71,9 +64,7 @@ class Db implements ISqlBuilder
|
||||
* @param Closure $closure
|
||||
* @param mixed ...$params
|
||||
* @return mixed
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public static function Transaction(Closure $closure, ...$params): mixed
|
||||
{
|
||||
@@ -95,9 +86,7 @@ class Db implements ISqlBuilder
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws ReflectionException
|
||||
* @throws
|
||||
*/
|
||||
public static function commit(): void
|
||||
{
|
||||
@@ -107,9 +96,7 @@ class Db implements ISqlBuilder
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws ReflectionException
|
||||
* @throws
|
||||
*/
|
||||
public static function rollback(): void
|
||||
{
|
||||
@@ -147,7 +134,7 @@ class Db implements ISqlBuilder
|
||||
|
||||
/**
|
||||
* @return array|bool
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function get(): array|bool
|
||||
{
|
||||
@@ -156,7 +143,7 @@ class Db implements ISqlBuilder
|
||||
|
||||
/**
|
||||
* @return array|bool|null
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function first(): array|bool|null
|
||||
{
|
||||
@@ -165,7 +152,7 @@ class Db implements ISqlBuilder
|
||||
|
||||
/**
|
||||
* @return bool|int
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function count(): bool|int
|
||||
{
|
||||
@@ -174,7 +161,7 @@ class Db implements ISqlBuilder
|
||||
|
||||
/**
|
||||
* @return bool|int
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function exists(): bool|int
|
||||
{
|
||||
@@ -185,7 +172,7 @@ class Db implements ISqlBuilder
|
||||
* @param string $sql
|
||||
* @param array $attributes
|
||||
* @return array|bool|int|string|null
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function query(string $sql, array $attributes = []): int|bool|array|string|null
|
||||
{
|
||||
@@ -196,7 +183,7 @@ class Db implements ISqlBuilder
|
||||
* @param string $sql
|
||||
* @param array $attributes
|
||||
* @return array|bool|int|string|null
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function one(string $sql, array $attributes = []): int|bool|array|string|null
|
||||
{
|
||||
@@ -206,8 +193,7 @@ class Db implements ISqlBuilder
|
||||
|
||||
/**
|
||||
* @return bool|int
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function delete(): bool|int
|
||||
{
|
||||
@@ -217,7 +203,7 @@ class Db implements ISqlBuilder
|
||||
/**
|
||||
* @param string $table
|
||||
* @return array|bool|null
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public static function show(string $table): array|bool|null
|
||||
{
|
||||
@@ -240,7 +226,7 @@ class Db implements ISqlBuilder
|
||||
* @param Connection|null $connection
|
||||
* @param string $database
|
||||
* @return array|null
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
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 string $database
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public static function getDefaultConnection(?Connection $connection = null, string $database = 'db'): Connection
|
||||
{
|
||||
|
||||
+9
-9
@@ -14,14 +14,14 @@ use Kiri;
|
||||
class HasCount extends HasBase
|
||||
{
|
||||
|
||||
/**
|
||||
* @return array|null|ModelInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
public function get(): array|ModelInterface|null
|
||||
{
|
||||
$relation = Kiri::getDi()->get(Relation::class);
|
||||
return $relation->get($this->name);
|
||||
}
|
||||
/**
|
||||
* @return array|null|ModelInterface
|
||||
* @throws
|
||||
*/
|
||||
public function get(): array|ModelInterface|null
|
||||
{
|
||||
$relation = Kiri::getDi()->get(Relation::class);
|
||||
return $relation->get($this->name);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+9
-9
@@ -22,13 +22,13 @@ use Kiri;
|
||||
class HasMany extends HasBase
|
||||
{
|
||||
|
||||
/**
|
||||
* @return array|null|Collection
|
||||
* @throws Exception
|
||||
*/
|
||||
public function get(): array|Collection|null
|
||||
{
|
||||
$relation = Kiri::getDi()->get(Relation::class);
|
||||
return $relation->get($this->name);
|
||||
}
|
||||
/**
|
||||
* @return array|null|Collection
|
||||
* @throws
|
||||
*/
|
||||
public function get(): array|Collection|null
|
||||
{
|
||||
$relation = Kiri::getDi()->get(Relation::class);
|
||||
return $relation->get($this->name);
|
||||
}
|
||||
}
|
||||
|
||||
+9
-9
@@ -21,13 +21,13 @@ use Kiri;
|
||||
class HasOne extends HasBase
|
||||
{
|
||||
|
||||
/**
|
||||
* @return array|null|ModelInterface
|
||||
* @throws Exception
|
||||
*/
|
||||
public function get(): array|ModelInterface|null
|
||||
{
|
||||
$relation = Kiri::getDi()->get(Relation::class);
|
||||
return $relation->first($this->name);
|
||||
}
|
||||
/**
|
||||
* @return array|null|ModelInterface
|
||||
* @throws
|
||||
*/
|
||||
public function get(): array|ModelInterface|null
|
||||
{
|
||||
$relation = Kiri::getDi()->get(Relation::class);
|
||||
return $relation->first($this->name);
|
||||
}
|
||||
}
|
||||
|
||||
+2
-3
@@ -31,7 +31,6 @@ class ImplodeCommand extends Command
|
||||
public string $description = 'php kiri.php db:implode --database users /Users/admin/snowflake-bi/test.sql';
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @var Channel
|
||||
*/
|
||||
@@ -43,7 +42,7 @@ class ImplodeCommand extends Command
|
||||
/**
|
||||
*
|
||||
*/
|
||||
protected function configure()
|
||||
protected function configure(): void
|
||||
{
|
||||
$this->setName('db:implode')
|
||||
->addArgument('path', InputArgument::REQUIRED, "save to path", null)
|
||||
@@ -57,7 +56,7 @@ class ImplodeCommand extends Command
|
||||
* @param InputInterface $input
|
||||
* @param OutputInterface $output
|
||||
* @return int
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function execute(InputInterface $input, OutputInterface $output): int
|
||||
{
|
||||
|
||||
@@ -11,8 +11,6 @@ namespace Database;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
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.');
|
||||
@@ -32,7 +30,7 @@ class Model extends Base\Model
|
||||
* @param string $column
|
||||
* @param int|float $value
|
||||
* @return ModelInterface|false
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function increment(string $column, int|float $value): bool|ModelInterface
|
||||
{
|
||||
@@ -48,7 +46,7 @@ class Model extends Base\Model
|
||||
* @param string $column
|
||||
* @param int|float $value
|
||||
* @return ModelInterface|false
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function decrement(string $column, int|float $value): bool|ModelInterface
|
||||
{
|
||||
@@ -63,7 +61,7 @@ class Model extends Base\Model
|
||||
/**
|
||||
* @param array $columns
|
||||
* @return ModelInterface|false
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function increments(array $columns): bool|static
|
||||
{
|
||||
@@ -80,7 +78,7 @@ class Model extends Base\Model
|
||||
/**
|
||||
* @param array $columns
|
||||
* @return ModelInterface|false
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function decrements(array $columns): bool|static
|
||||
{
|
||||
@@ -97,8 +95,7 @@ class Model extends Base\Model
|
||||
* @param array $condition
|
||||
* @param array $attributes
|
||||
* @return bool|static
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws
|
||||
*/
|
||||
public static function findOrCreate(array $condition, array $attributes): bool|static
|
||||
{
|
||||
@@ -117,8 +114,7 @@ class Model extends Base\Model
|
||||
* @param array $condition
|
||||
* @param array $attributes
|
||||
* @return bool|static
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
* @throws
|
||||
*/
|
||||
public static function createOrUpdate(array $condition, array $attributes = []): bool|static
|
||||
{
|
||||
@@ -138,14 +134,14 @@ class Model extends Base\Model
|
||||
* @param $columns
|
||||
* @param $action
|
||||
* @return array|bool|int|string|null
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
private function mathematics($columns, $action): int|bool|array|string|null
|
||||
{
|
||||
$condition = [$this->getPrimary() => $this->getPrimaryValue()];
|
||||
|
||||
$activeQuery = static::query()->where($condition);
|
||||
$create = SqlBuilder::builder($activeQuery)->mathematics($columns, $action);
|
||||
$create = SqlBuilder::builder($activeQuery)->mathematics($columns, $action);
|
||||
if (is_bool($create)) {
|
||||
return false;
|
||||
}
|
||||
@@ -156,7 +152,7 @@ class Model extends Base\Model
|
||||
/**
|
||||
* @param array $params
|
||||
* @return ModelInterface|bool
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function update(array $params): static|bool
|
||||
{
|
||||
@@ -175,7 +171,7 @@ class Model extends Base\Model
|
||||
/**
|
||||
* @param array $data
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public static function inserts(array $data): bool
|
||||
{
|
||||
@@ -187,7 +183,7 @@ class Model extends Base\Model
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function delete(): bool
|
||||
{
|
||||
@@ -209,7 +205,7 @@ class Model extends Base\Model
|
||||
* @param array $attributes
|
||||
*
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public static function updateAll(mixed $condition, array $attributes = []): bool
|
||||
{
|
||||
@@ -220,7 +216,7 @@ class Model extends Base\Model
|
||||
/**
|
||||
* @param $condition
|
||||
* @return array|Collection
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public static function get($condition): Collection|array
|
||||
{
|
||||
@@ -233,7 +229,7 @@ class Model extends Base\Model
|
||||
* @param array $attributes
|
||||
*
|
||||
* @return array|Collection
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public static function findAll($condition, array $attributes = []): array|Collection
|
||||
{
|
||||
@@ -247,7 +243,7 @@ class Model extends Base\Model
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function toArray(): array
|
||||
{
|
||||
@@ -286,7 +282,7 @@ class Model extends Base\Model
|
||||
* @param $foreignKey
|
||||
* @param $localKey
|
||||
* @return string
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
private function _hasBase(ModelInterface|string $modelName, $foreignKey, $localKey): string
|
||||
{
|
||||
@@ -309,7 +305,7 @@ class Model extends Base\Model
|
||||
* @param $foreignKey
|
||||
* @param $localKey
|
||||
* @return HasOne|ActiveQuery
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function hasOne(ModelInterface|string $modelName, $foreignKey, $localKey): HasOne|ActiveQuery
|
||||
{
|
||||
@@ -322,7 +318,7 @@ class Model extends Base\Model
|
||||
* @param $foreignKey
|
||||
* @param $localKey
|
||||
* @return ActiveQuery|HasCount
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function hasCount(ModelInterface|string $modelName, $foreignKey, $localKey): ActiveQuery|HasCount
|
||||
{
|
||||
@@ -335,7 +331,7 @@ class Model extends Base\Model
|
||||
* @param $foreignKey
|
||||
* @param $localKey
|
||||
* @return ActiveQuery|HasMany
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function hasMany(ModelInterface|string $modelName, $foreignKey, $localKey): ActiveQuery|HasMany
|
||||
{
|
||||
@@ -347,7 +343,7 @@ class Model extends Base\Model
|
||||
* @param $foreignKey
|
||||
* @param $localKey
|
||||
* @return ActiveQuery|HasMany
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function hasIn(ModelInterface|string $modelName, $foreignKey, $localKey): ActiveQuery|HasMany
|
||||
{
|
||||
|
||||
+17
-18
@@ -13,7 +13,6 @@ namespace Database\Mysql;
|
||||
|
||||
use Database\Connection;
|
||||
use Database\SqlBuilder;
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Kiri\Core\Json;
|
||||
@@ -44,7 +43,7 @@ class Columns extends Component
|
||||
/**
|
||||
* @param string $table
|
||||
* @return $this
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function table(string $table): static
|
||||
{
|
||||
@@ -64,7 +63,7 @@ class Columns extends Component
|
||||
* @param $key
|
||||
* @param $val
|
||||
* @return string|int|bool|float
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function fieldFormat($key, $val): string|int|bool|float
|
||||
{
|
||||
@@ -115,7 +114,7 @@ class Columns extends Component
|
||||
* @param string $name
|
||||
* @param $value
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function _decode(string $name, $value): mixed
|
||||
{
|
||||
@@ -127,7 +126,7 @@ class Columns extends Component
|
||||
* @param $val
|
||||
* @param null $format
|
||||
* @return float|bool|int|string
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function encode($val, $format = null): float|bool|int|string
|
||||
{
|
||||
@@ -195,7 +194,7 @@ class Columns extends Component
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function getFields(): array
|
||||
{
|
||||
@@ -209,7 +208,7 @@ class Columns extends Component
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function hasField(string $name): bool
|
||||
{
|
||||
@@ -219,7 +218,7 @@ class Columns extends Component
|
||||
|
||||
/**
|
||||
* @return int|string|null
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function getAutoIncrement(): int|string|null
|
||||
{
|
||||
@@ -229,7 +228,7 @@ class Columns extends Component
|
||||
/**
|
||||
* @return array|null|string
|
||||
*
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function getPrimaryKeys(): array|string|null
|
||||
{
|
||||
@@ -242,7 +241,7 @@ class Columns extends Component
|
||||
/**
|
||||
* @return array|null|string
|
||||
*
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
#[Pure] public function getFirstPrimary(): array|string|null
|
||||
{
|
||||
@@ -259,7 +258,7 @@ class Columns extends Component
|
||||
* @param $name
|
||||
* @param null $index
|
||||
* @return array
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
private function columns($name, $index = null): array
|
||||
{
|
||||
@@ -272,7 +271,7 @@ class Columns extends Component
|
||||
|
||||
/**
|
||||
* @return array|static
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
private function getColumns(): array|static
|
||||
{
|
||||
@@ -283,14 +282,14 @@ class Columns extends Component
|
||||
/**
|
||||
* @param $table
|
||||
* @return array|Columns
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
private function structure($table): array|static
|
||||
{
|
||||
if (!isset($this->columns[$table]) || empty($this->columns[$table])) {
|
||||
$column = $this->db->createCommand(SqlBuilder::builder(null)->columns($table))->all();
|
||||
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);
|
||||
}
|
||||
@@ -319,7 +318,7 @@ class Columns extends Component
|
||||
* @param $item
|
||||
* @param $table
|
||||
*/
|
||||
private function addPrimary($item, $table)
|
||||
private function addPrimary($item, $table): void
|
||||
{
|
||||
if (!isset($this->_primary[$table])) {
|
||||
$this->_primary[$table] = [];
|
||||
@@ -335,7 +334,7 @@ class Columns extends Component
|
||||
* @param $item
|
||||
* @param $table
|
||||
*/
|
||||
private function addIncrement($item, $table)
|
||||
private function addIncrement($item, $table): void
|
||||
{
|
||||
if ($item['Extra'] !== 'auto_increment') {
|
||||
return;
|
||||
@@ -363,7 +362,7 @@ class Columns extends Component
|
||||
/**
|
||||
* @param null $field
|
||||
* @return array|string|null
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function get_fields($field = null): array|string|null
|
||||
{
|
||||
@@ -379,7 +378,7 @@ class Columns extends Component
|
||||
|
||||
/**
|
||||
* @return array
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function getAllField(): array
|
||||
{
|
||||
|
||||
+1
-2
@@ -3,7 +3,6 @@ declare(strict_types=1);
|
||||
namespace Database\Mysql;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Database\Connection;
|
||||
|
||||
@@ -23,7 +22,7 @@ class Schema extends Component
|
||||
|
||||
/**
|
||||
* @return Columns|null
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function getColumns(): ?Columns
|
||||
{
|
||||
|
||||
+10
-11
@@ -5,7 +5,6 @@ namespace Database\Orm;
|
||||
|
||||
|
||||
use Database\Traits\Builder;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Trait Condition
|
||||
@@ -15,15 +14,15 @@ trait Condition
|
||||
{
|
||||
|
||||
|
||||
use Builder;
|
||||
use Builder;
|
||||
|
||||
/**
|
||||
* @param $query
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getWhere($query): string
|
||||
{
|
||||
return $this->where($query);
|
||||
}
|
||||
/**
|
||||
* @param $query
|
||||
* @return string
|
||||
* @throws
|
||||
*/
|
||||
public function getWhere($query): string
|
||||
{
|
||||
return $this->where($query);
|
||||
}
|
||||
}
|
||||
|
||||
+7
-7
@@ -38,7 +38,7 @@ class Pagination extends Component
|
||||
* PaginationIteration constructor.
|
||||
* @param ActiveQuery $activeQuery
|
||||
* @param array $config
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function __construct(ActiveQuery $activeQuery, array $config = [])
|
||||
{
|
||||
@@ -71,9 +71,9 @@ class Pagination extends Component
|
||||
|
||||
/**
|
||||
* @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)) {
|
||||
throw new Exception('非法回调函数~');
|
||||
@@ -129,7 +129,7 @@ class Pagination extends Component
|
||||
/**
|
||||
* @param array $param
|
||||
* @return void
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function plunk(array $param = []): void
|
||||
{
|
||||
@@ -141,7 +141,7 @@ class Pagination extends Component
|
||||
* 轮训
|
||||
* @param $param
|
||||
* @return array
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function loop($param): array
|
||||
{
|
||||
@@ -175,7 +175,7 @@ class Pagination extends Component
|
||||
/**
|
||||
* @param $data
|
||||
* @param $param
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
private function executed($data, $param): void
|
||||
{
|
||||
@@ -189,7 +189,7 @@ class Pagination extends Component
|
||||
|
||||
/**
|
||||
* @return array|Collection
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
private function get(): Collection|array
|
||||
{
|
||||
|
||||
@@ -11,7 +11,6 @@ namespace Database;
|
||||
|
||||
|
||||
use Database\Traits\QueryTrait;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Class Query
|
||||
@@ -20,35 +19,35 @@ use Exception;
|
||||
class Query implements ISqlBuilder
|
||||
{
|
||||
|
||||
use QueryTrait;
|
||||
use QueryTrait;
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->builder = SqlBuilder::builder($this);
|
||||
}
|
||||
/**
|
||||
* @throws
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->builder = SqlBuilder::builder($this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getSql(): string
|
||||
{
|
||||
return $this->builder->get();
|
||||
}
|
||||
/**
|
||||
* @return string
|
||||
* @throws
|
||||
*/
|
||||
public function getSql(): string
|
||||
{
|
||||
return $this->builder->get();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
public function getCondition(): string
|
||||
{
|
||||
return $this->builder->getCondition();
|
||||
}
|
||||
/**
|
||||
* @return string
|
||||
* @throws
|
||||
*/
|
||||
public function getCondition(): string
|
||||
{
|
||||
return $this->builder->getCondition();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
+69
-79
@@ -4,7 +4,6 @@ declare(strict_types=1);
|
||||
namespace Database;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Component;
|
||||
use Kiri\Di\Context;
|
||||
|
||||
@@ -15,92 +14,83 @@ use Kiri\Di\Context;
|
||||
class Relation extends Component
|
||||
{
|
||||
|
||||
/** @var ActiveQuery[] $_query */
|
||||
private array $_query = [];
|
||||
/** @var ActiveQuery[] $_query */
|
||||
private array $_query = [];
|
||||
|
||||
/**
|
||||
* @param string $identification
|
||||
* @param ActiveQuery $query
|
||||
* @return $this
|
||||
*/
|
||||
public function bindIdentification(string $identification, ActiveQuery $query): static
|
||||
{
|
||||
$this->_query[$identification] = $query;
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @param string $identification
|
||||
* @param ActiveQuery $query
|
||||
* @return $this
|
||||
*/
|
||||
public function bindIdentification(string $identification, ActiveQuery $query): static
|
||||
{
|
||||
$this->_query[$identification] = $query;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $identification
|
||||
* @return bool
|
||||
*/
|
||||
public function hasIdentification(string $identification): bool
|
||||
{
|
||||
return isset($this->_query[$identification]) && $this->_query[$identification] instanceof ActiveQuery;
|
||||
}
|
||||
/**
|
||||
* @param string $identification
|
||||
* @return bool
|
||||
*/
|
||||
public function hasIdentification(string $identification): bool
|
||||
{
|
||||
return isset($this->_query[$identification]) && $this->_query[$identification] instanceof ActiveQuery;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return ActiveQuery|null
|
||||
*/
|
||||
public function getQuery(string $name): ?ActiveQuery
|
||||
{
|
||||
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 $name
|
||||
* @return ActiveQuery|null
|
||||
*/
|
||||
public function getQuery(string $name): ?ActiveQuery
|
||||
{
|
||||
return $this->_query[$name] ?? null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $_identification
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function get(string $_identification): mixed
|
||||
{
|
||||
if (Context::exists($_identification)) {
|
||||
return Context::get($_identification);
|
||||
}
|
||||
$activeModel = $this->_query[$_identification]->get();
|
||||
if (empty($activeModel)) {
|
||||
return $activeModel;
|
||||
}
|
||||
unset($this->_query[$_identification]);
|
||||
return Context::set($_identification, $activeModel);
|
||||
}
|
||||
public function first(string $_identification): mixed
|
||||
{
|
||||
if (Context::exists($_identification)) {
|
||||
return Context::get($_identification);
|
||||
}
|
||||
$activeModel = $this->_query[$_identification]->first();
|
||||
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
@@ -6,7 +6,6 @@ namespace Database;
|
||||
|
||||
|
||||
use Database\Traits\Builder;
|
||||
use Exception;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Kiri\Abstracts\Component;
|
||||
|
||||
@@ -41,7 +40,7 @@ class SqlBuilder extends Component
|
||||
/**
|
||||
* @param ISqlBuilder|null $query
|
||||
* @return $this
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public static function builder(ISqlBuilder|null $query): static
|
||||
{
|
||||
@@ -51,7 +50,7 @@ class SqlBuilder extends Component
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function getCondition(): string
|
||||
{
|
||||
@@ -62,7 +61,7 @@ class SqlBuilder extends Component
|
||||
/**
|
||||
* @param array $compiler
|
||||
* @return string
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function hashCompiler(array $compiler): string
|
||||
{
|
||||
@@ -73,7 +72,7 @@ class SqlBuilder extends Component
|
||||
/**
|
||||
* @param array $attributes
|
||||
* @return bool|array
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function update(array $attributes): bool|string
|
||||
{
|
||||
@@ -91,7 +90,7 @@ class SqlBuilder extends Component
|
||||
* @param array $attributes
|
||||
* @param string $opera
|
||||
* @return bool|array
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function mathematics(array $attributes, string $opera = '+'): bool|string
|
||||
{
|
||||
@@ -106,7 +105,7 @@ class SqlBuilder extends Component
|
||||
/**
|
||||
* @param array $string
|
||||
* @return string|bool
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
private function __updateBuilder(array $string): string|bool
|
||||
{
|
||||
@@ -121,7 +120,7 @@ class SqlBuilder extends Component
|
||||
* @param array $attributes
|
||||
* @param false $isBatch
|
||||
* @return array
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function insert(array $attributes, bool $isBatch = false): array
|
||||
{
|
||||
@@ -145,7 +144,7 @@ class SqlBuilder extends Component
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function delete(): string
|
||||
{
|
||||
@@ -209,7 +208,7 @@ class SqlBuilder extends Component
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function one(): string
|
||||
{
|
||||
@@ -222,7 +221,7 @@ class SqlBuilder extends Component
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function all(): string
|
||||
{
|
||||
@@ -235,7 +234,7 @@ class SqlBuilder extends Component
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function count(): string
|
||||
{
|
||||
@@ -255,7 +254,7 @@ class SqlBuilder extends Component
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
private function _prefix(): string
|
||||
{
|
||||
@@ -292,7 +291,7 @@ class SqlBuilder extends Component
|
||||
/**
|
||||
* @param false $isCount
|
||||
* @return string
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function get(bool $isCount = false): string
|
||||
{
|
||||
@@ -305,7 +304,7 @@ class SqlBuilder extends Component
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
public function truncate(): string
|
||||
{
|
||||
@@ -315,7 +314,7 @@ class SqlBuilder extends Component
|
||||
|
||||
/**
|
||||
* @return string
|
||||
* @throws Exception
|
||||
* @throws
|
||||
*/
|
||||
private function conditionToString(): string
|
||||
{
|
||||
|
||||
+158
-159
@@ -25,177 +25,176 @@ trait Builder
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param $alias
|
||||
* @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
|
||||
/**
|
||||
* @param $alias
|
||||
* @return string
|
||||
*/
|
||||
private function where(array $where): string
|
||||
{
|
||||
if (count($where) < 1) {
|
||||
return '';
|
||||
}
|
||||
private function builderAlias($alias): string
|
||||
{
|
||||
return " AS " . $alias;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $field
|
||||
* @param $condition
|
||||
* @param $_tmp
|
||||
* @return string
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException|Exception
|
||||
*/
|
||||
private function resolveCondition($field, $condition, $_tmp): string
|
||||
{
|
||||
if (is_string($field)) {
|
||||
$this->query->pushParam($condition);
|
||||
return $field . ' = ?';
|
||||
} else if (is_string($condition)) {
|
||||
return $condition;
|
||||
} else {
|
||||
return $this->_arrayMap($condition, $_tmp);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param $field
|
||||
* @param $condition
|
||||
* @param $_tmp
|
||||
* @return string
|
||||
* @throws
|
||||
*/
|
||||
private function resolveCondition($field, $condition, $_tmp): string
|
||||
{
|
||||
if (is_string($field)) {
|
||||
$this->query->pushParam($condition);
|
||||
return $field . ' = ?';
|
||||
} else if (is_string($condition)) {
|
||||
return $condition;
|
||||
} else {
|
||||
return $this->_arrayMap($condition, $_tmp);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $condition
|
||||
* @param $array
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
private function _arrayMap($condition, $array): string
|
||||
{
|
||||
if (!isset($condition[0])) {
|
||||
return implode(' AND ', $this->_hashMap($condition));
|
||||
}
|
||||
$stroppier = strtoupper($condition[0]);
|
||||
if (str_contains($stroppier, 'OR')) {
|
||||
if (!is_string($condition[2])) {
|
||||
$condition[2] = $this->_hashMap($condition[2]);
|
||||
}
|
||||
$builder = Kiri::getDi()->get(OrCondition::class);
|
||||
$builder->setValue($condition[2]);
|
||||
$builder->setColumn($condition[1]);
|
||||
$builder->oldParams = $array;
|
||||
} else if (isset(ConditionClassMap::$conditionMap[$stroppier])) {
|
||||
$defaultConfig = ConditionClassMap::$conditionMap[$stroppier];
|
||||
/**
|
||||
* @param $condition
|
||||
* @param $array
|
||||
* @return string
|
||||
* @throws
|
||||
*/
|
||||
private function _arrayMap($condition, $array): string
|
||||
{
|
||||
if (!isset($condition[0])) {
|
||||
return implode(' AND ', $this->_hashMap($condition));
|
||||
}
|
||||
$stroppier = strtoupper($condition[0]);
|
||||
if (str_contains($stroppier, 'OR')) {
|
||||
if (!is_string($condition[2])) {
|
||||
$condition[2] = $this->_hashMap($condition[2]);
|
||||
}
|
||||
$builder = Kiri::getDi()->get(OrCondition::class);
|
||||
$builder->setValue($condition[2]);
|
||||
$builder->setColumn($condition[1]);
|
||||
$builder->oldParams = $array;
|
||||
} else if (isset(ConditionClassMap::$conditionMap[$stroppier])) {
|
||||
$defaultConfig = ConditionClassMap::$conditionMap[$stroppier];
|
||||
|
||||
$class = $defaultConfig['class'];
|
||||
unset($defaultConfig['class']);
|
||||
$class = $defaultConfig['class'];
|
||||
unset($defaultConfig['class']);
|
||||
|
||||
$builder = Kiri::getDi()->make($class, [], $defaultConfig);
|
||||
$builder->setValue($condition[2]);
|
||||
$builder->setColumn($condition[1]);
|
||||
} else {
|
||||
$builder = Kiri::getDi()->get(HashCondition::class);
|
||||
$builder->setValue($condition);
|
||||
}
|
||||
$builder = Kiri::getDi()->make($class, [], $defaultConfig);
|
||||
$builder->setValue($condition[2]);
|
||||
$builder->setColumn($condition[1]);
|
||||
} else {
|
||||
$builder = Kiri::getDi()->get(HashCondition::class);
|
||||
$builder->setValue($condition);
|
||||
}
|
||||
|
||||
$array[] = $builder->builder();
|
||||
$array[] = $builder->builder();
|
||||
|
||||
return implode(' AND ', $array);
|
||||
}
|
||||
return implode(' AND ', $array);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
private function _hashMap($condition): array
|
||||
{
|
||||
$_array = [];
|
||||
foreach ($condition as $key => $value) {
|
||||
$this->query->pushParam($value);
|
||||
$_array[] = $key . '= ?';
|
||||
}
|
||||
return $_array;
|
||||
}
|
||||
/**
|
||||
* @param $condition
|
||||
* @return array
|
||||
*/
|
||||
private function _hashMap($condition): array
|
||||
{
|
||||
$_array = [];
|
||||
foreach ($condition as $key => $value) {
|
||||
$this->query->pushParam($value);
|
||||
$_array[] = $key . '= ?';
|
||||
}
|
||||
return $_array;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
+41
-41
@@ -27,53 +27,53 @@ use Kiri;
|
||||
abstract class HasBase implements \Database\Traits\Relation
|
||||
{
|
||||
|
||||
/** @var ModelInterface|Collection */
|
||||
protected mixed $data = null;
|
||||
/** @var ModelInterface|Collection */
|
||||
protected mixed $data = null;
|
||||
|
||||
/**
|
||||
* @var ModelInterface
|
||||
*/
|
||||
protected mixed $model;
|
||||
/**
|
||||
* @var ModelInterface
|
||||
*/
|
||||
protected mixed $model;
|
||||
|
||||
|
||||
protected mixed $value = 0;
|
||||
protected mixed $value = 0;
|
||||
|
||||
|
||||
/**
|
||||
* HasBase constructor.
|
||||
* @param string $name
|
||||
*/
|
||||
public function __construct(public string $name)
|
||||
{
|
||||
}
|
||||
/**
|
||||
* HasBase constructor.
|
||||
* @param string $name
|
||||
*/
|
||||
public function __construct(public string $name)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $arguments
|
||||
* @return static
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
public function __call($name, $arguments)
|
||||
{
|
||||
if ($name !== 'get') {
|
||||
$relation = Kiri::getDi()->get(Relation::class);
|
||||
$relation->getQuery($this->name)->$name(...$arguments);
|
||||
return $this;
|
||||
} else {
|
||||
return $this->get();
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @param $name
|
||||
* @param $arguments
|
||||
* @return static
|
||||
* @throws
|
||||
*/
|
||||
public function __call($name, $arguments)
|
||||
{
|
||||
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;
|
||||
}
|
||||
/**
|
||||
* @param $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get($name): mixed
|
||||
{
|
||||
if ($this->data === null) {
|
||||
$this->data = $this->get();
|
||||
}
|
||||
return $this->data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,8 +38,6 @@ trait QueryTrait
|
||||
|
||||
public bool $lock = false;
|
||||
|
||||
public bool $ifNotWhere = false;
|
||||
|
||||
|
||||
private SqlBuilder $builder;
|
||||
|
||||
@@ -310,28 +308,6 @@ trait QueryTrait
|
||||
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
|
||||
*
|
||||
|
||||
+48
-48
@@ -18,61 +18,61 @@ use JetBrains\PhpStorm\Pure;
|
||||
class When
|
||||
{
|
||||
|
||||
public ActiveQuery|ISqlBuilder $query;
|
||||
public ActiveQuery|ISqlBuilder $query;
|
||||
|
||||
|
||||
private array $_condition = [];
|
||||
private array $_condition = [];
|
||||
|
||||
private string $else = '';
|
||||
private string $else = '';
|
||||
|
||||
|
||||
/**
|
||||
* CaseWhen constructor.
|
||||
* @param string $column
|
||||
* @param 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
|
||||
/**
|
||||
* CaseWhen constructor.
|
||||
* @param string $column
|
||||
* @param ActiveQuery|ISqlBuilder $activeQuery
|
||||
*/
|
||||
public function __construct(public string $column, public ActiveQuery|ISqlBuilder $activeQuery)
|
||||
{
|
||||
$this->else = $alias;
|
||||
}
|
||||
$this->_condition[] = 'CASE ' . $column;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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)';
|
||||
}
|
||||
/**
|
||||
* @param string|int $condition
|
||||
* @param string $then
|
||||
* @return $this
|
||||
* @throws
|
||||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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
@@ -9,7 +9,7 @@
|
||||
],
|
||||
"license": "MIT",
|
||||
"require": {
|
||||
"php": ">=8.0",
|
||||
"php": ">=8.3",
|
||||
"ext-json": "*",
|
||||
"ext-pdo": "*",
|
||||
"game-worker/kiri-pool": "~v1.0"
|
||||
|
||||
Reference in New Issue
Block a user