Files
kiri-databases/ActiveQuery.php
T

346 lines
7.0 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;
use Database\Traits\QueryTrait;
use Exception;
use JetBrains\PhpStorm\ArrayShape;
use Kiri\Abstracts\Component;
/**
* Class ActiveQuery
* @package Database
*/
class ActiveQuery extends Component implements ISqlBuilder
{
use QueryTrait;
/** @var array */
public array $with = [];
/** @var bool */
public bool $asArray = FALSE;
/** @var bool */
public bool $useCache = FALSE;
/**
* @var Connection|null
*/
public ?Connection $db = NULL;
/**
* @var array
* 参数绑定
*/
public array $attributes = [];
/**
* Comply constructor.
* @param $model
* @param array $config
* @throws
*/
public function __construct($model, array $config = [])
{
$this->modelClass = $model;
$this->builder = SqlBuilder::builder($this);
parent::__construct($config);
}
/**
* 清除不完整数据
*/
public function clear()
{
$this->db = NULL;
$this->useCache = FALSE;
$this->with = [];
}
/**
* @param $key
* @param $value
* @return $this
*/
public function addParam($key, $value): static
{
$this->attributes[$key] = $value;
return $this;
}
/**
* @param int $size
* @param int $page
* @return array
2022-06-09 10:40:43 +08:00
* @throws
2022-01-09 03:49:51 +08:00
*/
2022-05-30 16:55:42 +08:00
#[ArrayShape([])]
2022-01-09 03:49:51 +08:00
public function pagination(int $size = 20, int $page = 1): array
{
$page = max(1, $page);
$size = max(1, $size);
$offset = ($page - 1) * $size;
$count = $this->count();
$lists = $this->limit($offset, $size)->get()->toArray();
return [
'code' => 0,
'message' => 'ok',
'size' => $size,
'page' => $page,
'count' => $count,
'next' => max($page + 1, 1),
'prev' => max($page - 1, 1),
'param' => $lists,
];
}
2023-04-01 21:04:28 +08:00
/**
* @param bool $asArray
* @return static
*/
public function asArray(bool $asArray = true): static
{
$this->asArray = $asArray;
return $this;
}
2022-01-09 03:49:51 +08:00
/**
* @param array $values
* @return $this
*/
public function addParams(array $values): static
{
foreach ($values as $key => $val) {
$this->addParam($key, $val);
}
return $this;
}
2023-04-01 16:03:02 +08:00
/**
* @param array $name
* @return $this
*/
public function with(array $name): static
2022-01-09 03:49:51 +08:00
{
foreach ($name as $val) {
2022-01-19 14:57:14 +08:00
$this->with[] = $val;
2022-01-09 03:49:51 +08:00
}
return $this;
}
/**
* @param $sql
* @param array $params
* @return mixed
2022-06-09 10:40:43 +08:00
* @throws
2022-01-09 03:49:51 +08:00
*/
public function execute($sql, array $params = []): Command
{
return $this->modelClass->getConnection()->createCommand($sql, $params);
}
/**
* @return ModelInterface|null
2022-06-09 10:40:43 +08:00
* @throws
2022-01-09 03:49:51 +08:00
*/
public function first(): ModelInterface|null
{
$data = $this->execute($this->builder->one())->one();
2023-04-01 15:54:38 +08:00
if (!is_null($data)) {
return $this->populate($data);
} else {
return NULL;
2022-01-09 03:49:51 +08:00
}
}
/**
* @return string
2022-06-09 10:40:43 +08:00
* @throws
2022-01-09 03:49:51 +08:00
*/
public function toSql(): string
{
return $this->builder->get();
}
/**
* @return array|Collection
*/
public function get(): Collection|array
{
return $this->all();
}
/**
2022-06-09 10:40:43 +08:00
* @throws
2022-01-09 03:49:51 +08:00
*/
public function flush(): array|bool|int|string|null
{
return $this->execute($this->builder->truncate())->exec();
}
/**
* @param int $size
* @param callable $callback
2022-05-30 16:54:57 +08:00
* @param int $offset
2022-01-09 03:49:51 +08:00
* @return Pagination
2022-06-09 10:40:43 +08:00
* @throws
2022-01-09 03:49:51 +08:00
*/
2022-05-29 01:52:10 +08:00
public function page(int $size, callable $callback, int $offset = 0): Pagination
2022-01-09 03:49:51 +08:00
{
$pagination = new Pagination($this);
2022-05-29 01:52:10 +08:00
$pagination->setOffset($offset);
2022-01-09 03:49:51 +08:00
$pagination->setLimit($size);
$pagination->setCallback($callback);
return $pagination;
}
/**
* @param string $field
* @param string $setKey
*
* @return array|null
2022-06-09 10:40:43 +08:00
* @throws
2022-01-09 03:49:51 +08:00
*/
public function column(string $field, string $setKey = ''): ?array
{
return $this->all()->column($field, $setKey);
}
2022-04-08 19:18:02 +08:00
2022-04-08 19:19:50 +08:00
2022-01-09 03:49:51 +08:00
/**
* @return array|Collection
* @throws
*/
public function all(): Collection|array
{
2022-05-30 16:55:15 +08:00
if (!($data = $this->execute($this->builder->all())->all())) {
2022-04-08 19:18:02 +08:00
return new Collection($this, [], $this->modelClass);
}
2023-04-01 15:54:38 +08:00
$this->getWith($this->modelClass);
2022-01-09 03:49:51 +08:00
$collect = new Collection($this, $data, $this->modelClass);
2023-04-01 15:58:17 +08:00
return $this->asArray ? $collect->toArray() : $collect;
2022-01-09 03:49:51 +08:00
}
/**
* @param $data
* @return ModelInterface
2022-06-09 10:40:43 +08:00
* @throws
2022-01-09 03:49:51 +08:00
*/
public function populate($data): ModelInterface
{
return $this->getWith($this->modelClass::populate($data));
}
/**
* @param ModelInterface $model
* @return ModelInterface
*/
public function getWith(ModelInterface $model): ModelInterface
{
return $model->setWith($this->with);
}
/**
* @return int
2022-06-09 10:40:43 +08:00
* @throws
2022-01-09 03:49:51 +08:00
*/
public function count(): int
{
$data = $this->execute($this->builder->count())->one();
if ($data && is_array($data)) {
return (int)array_shift($data);
}
return 0;
}
/**
* @param array $data
2022-05-29 01:52:10 +08:00
* @return bool
2022-06-09 10:40:43 +08:00
* @throws
2022-01-09 03:49:51 +08:00
*/
2022-09-07 14:36:32 +08:00
public function update(array $data): bool
2022-01-09 03:49:51 +08:00
{
$generate = $this->builder->update($data);
if (is_bool($generate)) {
return $generate;
}
2022-05-29 01:52:10 +08:00
return (bool)$this->execute(...$generate)->exec();
2022-01-09 03:49:51 +08:00
}
/**
* @param array $data
* @return bool
2022-06-09 10:40:43 +08:00
* @throws
2022-01-09 03:49:51 +08:00
*/
2022-09-07 14:36:32 +08:00
public function insert(array $data): bool
2022-01-09 03:49:51 +08:00
{
[$sql, $params] = $this->builder->insert($data, TRUE);
2022-05-29 01:52:10 +08:00
return (bool)$this->execute($sql, $params)->exec();
2022-01-09 03:49:51 +08:00
}
/**
* @param $filed
*
* @return null
2022-06-09 10:40:43 +08:00
* @throws
2022-01-09 03:49:51 +08:00
*/
public function value($filed)
{
return $this->first()[$filed] ?? NULL;
}
/**
* @return bool
2022-06-09 10:40:43 +08:00
* @throws
2022-01-09 03:49:51 +08:00
*/
public function exists(): bool
{
return !empty($this->execute($this->builder->one())->fetchColumn());
}
2022-09-12 02:49:18 +08:00
/**
* @param bool $getSql
* @return bool|string
* @throws Exception
*/
public function delete(bool $getSql = FALSE): bool|string
2022-01-09 03:49:51 +08:00
{
$sql = $this->builder->delete();
if ($getSql === FALSE) {
2022-09-12 02:49:18 +08:00
return (bool)$this->execute($sql)->delete();
2022-01-09 03:49:51 +08:00
}
return $sql;
}
}