Files
kiri-databases/ActiveQuery.php
T

230 lines
4.7 KiB
PHP
Raw Normal View History

2022-01-09 03:49:51 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/4 0004
* Time: 14:42
*/
declare(strict_types=1);
namespace Database;
2023-12-13 14:47:23 +08:00
use Closure;
2022-01-09 03:49:51 +08:00
use Database\Traits\QueryTrait;
2023-12-13 14:47:23 +08:00
use Kiri\Di\Context;
use Swoole\Coroutine;
2022-01-09 03:49:51 +08:00
/**
* Class ActiveQuery
* @package Database
*/
2023-12-13 10:05:42 +08:00
class ActiveQuery extends QueryTrait implements ISqlBuilder
2022-01-09 03:49:51 +08:00
{
2023-07-25 09:33:24 +08:00
2023-12-13 14:47:23 +08:00
public bool $asArray = FALSE;
protected mixed $_mock = null;
2023-07-25 09:33:24 +08:00
/**
* @param bool $asArray
* @return static
*/
public function asArray(bool $asArray = true): static
{
$this->asArray = $asArray;
return $this;
}
/**
* @param array $methods
* @return $this
*/
public function with(array $methods): static
{
$this->modelClass->setWith($methods);
return $this;
}
/**
2023-12-13 14:47:23 +08:00
* @return ModelInterface|array|null|bool
2023-07-25 09:33:24 +08:00
* @throws
*/
2023-12-13 14:47:23 +08:00
public function first(): ModelInterface|null|array|bool
2023-07-25 09:33:24 +08:00
{
2023-12-13 14:47:23 +08:00
$data = $this->buildCommand($this->builder->one())->one();
2023-07-25 09:33:24 +08:00
if (is_array($data)) {
return $this->populate($data);
}
2023-12-13 16:24:11 +08:00
return null;
2023-07-25 09:33:24 +08:00
}
/**
2023-12-13 14:47:23 +08:00
* @return bool|Collection
2023-07-25 09:33:24 +08:00
*/
2023-12-13 14:47:23 +08:00
public function get(): bool|Collection
2023-07-25 09:33:24 +08:00
{
2023-12-13 14:47:23 +08:00
$data = $this->buildCommand($this->builder->all())->all();
if (is_array($data)) {
return new Collection($this, $this->modelClass, $data);
2023-07-25 09:33:24 +08:00
}
2023-12-13 14:47:23 +08:00
return false;
2023-07-25 09:33:24 +08:00
}
/**
* @throws
*/
2023-12-13 14:47:23 +08:00
public function flush(): bool
2023-07-25 09:33:24 +08:00
{
2023-12-13 14:47:23 +08:00
return (bool)$this->buildCommand($this->builder->truncate())->exec();
2023-07-25 09:33:24 +08:00
}
/**
* @param int $size
2023-12-13 14:47:23 +08:00
* @param Closure $closure
* @return void
2023-07-25 09:33:24 +08:00
*/
2023-12-13 14:47:23 +08:00
public function chunk(int $size, Closure $closure): void
2023-07-25 09:33:24 +08:00
{
2023-12-26 17:41:03 +08:00
if ($this->offset === -1) $this->offset = 0;
2023-12-13 14:47:23 +08:00
$data = $this->offset($this->offset)->limit($size)->get();
if (!$data || $data->isEmpty()) {
return;
}
if (Context::inCoroutine()) {
Coroutine::create(fn() => $closure($data));
} else {
call_user_func($closure, $data);
}
$this->offset += $size;
$this->chunk($size, $closure);
2023-07-25 09:33:24 +08:00
}
/**
* @param string $field
2023-12-13 14:47:23 +08:00
* @param string|null $setKey
2023-07-25 09:33:24 +08:00
*
* @return array|null
*/
2023-12-13 14:47:23 +08:00
public function column(string $field, ?string $setKey = null): ?array
2023-07-25 09:33:24 +08:00
{
return $this->get()->column($field, $setKey);
}
/**
* @param mixed $value
* @return $this
*/
public function withMock(mixed $value): static
{
$this->_mock = $value;
return $this;
}
/**
* @return mixed
*/
public function mock(): mixed
{
return $this->_mock;
}
/**
2023-12-18 18:11:58 +08:00
* @param array $data
2023-07-25 09:33:24 +08:00
* @return ModelInterface|array
2023-12-12 15:35:35 +08:00
* @throws
2023-07-25 09:33:24 +08:00
*/
2023-12-18 18:11:58 +08:00
public function populate(array $data): ModelInterface|array
2023-07-25 09:33:24 +08:00
{
$model = $this->modelClass->populates($data);
2023-10-24 15:14:46 +08:00
return $this->asArray ? $model->toArray() : $model;
2023-07-25 09:33:24 +08:00
}
/**
* @return int
* @throws
*/
public function count(): int
{
2023-12-13 14:47:23 +08:00
return $this->buildCommand($this->builder->count())->rowCount();
2023-07-25 09:33:24 +08:00
}
/**
* @param array $data
* @return bool
* @throws
*/
public function update(array $data): bool
{
2023-08-02 14:37:59 +08:00
if (count($data) < 1) {
return true;
}
2023-07-25 09:33:24 +08:00
$generate = $this->builder->update($data);
2023-10-24 15:14:46 +08:00
if (!is_bool($generate)) {
2023-12-13 14:47:23 +08:00
return (bool)$this->buildCommand($generate)->exec();
2023-10-24 15:14:46 +08:00
} else {
2023-07-25 09:33:24 +08:00
return $generate;
}
}
/**
* @param array $data
* @return bool
*/
public function insert(array $data): bool
{
2023-12-13 18:07:39 +08:00
$sql = $this->builder->insert($data, isset($data[0]));
2023-07-25 09:33:24 +08:00
2023-12-13 18:07:39 +08:00
return (bool)$this->buildCommand($sql)->exec();
2023-07-25 09:33:24 +08:00
}
/**
2023-12-18 18:11:58 +08:00
* @param string $filed
2023-07-25 09:33:24 +08:00
*
2023-12-13 14:47:23 +08:00
* @return mixed
2023-07-25 09:33:24 +08:00
* @throws
*/
2023-12-18 18:11:58 +08:00
public function value(string $filed): mixed
2023-07-25 09:33:24 +08:00
{
return $this->first()[$filed] ?? NULL;
}
/**
* @return bool
* @throws
*/
public function exists(): bool
{
2023-12-13 14:47:23 +08:00
return $this->buildCommand($this->builder->one())->rowCount() > 0;
2023-07-25 09:33:24 +08:00
}
/**
2023-12-13 14:47:23 +08:00
* @param string $sql
* @param array $params
* @return int|bool
2023-07-25 09:33:24 +08:00
*/
2023-12-13 14:47:23 +08:00
public function execute(string $sql, array $params = []): int|bool
2023-07-25 09:33:24 +08:00
{
2023-12-13 14:47:23 +08:00
return $this->buildCommand($sql, $params)->exec();
}
/**
* @return bool
*/
public function delete(): bool
{
return $this->buildCommand($this->builder->delete())->delete();
2023-07-25 09:33:24 +08:00
}
2022-01-09 03:49:51 +08:00
}