Files
kiri-core/Database/ActiveQuery.php
T

312 lines
5.2 KiB
PHP
Raw Normal View History

2020-08-31 12:38:32 +08:00
<?php
/**
* Created by PhpStorm.
* User: whwyy
* Date: 2018/4/4 0004
* Time: 14:42
*/
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-12-17 14:09:14 +08:00
2020-08-31 12:38:32 +08:00
namespace Database;
use Snowflake\Abstracts\Component;
use Database\Traits\QueryTrait;
use Exception;
use Snowflake\Snowflake;
/**
* Class ActiveQuery
* @package Database
*/
2021-02-25 17:59:59 +08:00
class ActiveQuery extends Component implements ISqlBuilder
2020-08-31 12:38:32 +08:00
{
use QueryTrait;
/** @var array */
2020-10-29 18:17:25 +08:00
public array $with = [];
2020-08-31 12:38:32 +08:00
/** @var bool */
2020-10-29 18:17:25 +08:00
public bool $asArray = FALSE;
2020-08-31 12:38:32 +08:00
/** @var bool */
2020-10-29 18:17:25 +08:00
public bool $useCache = FALSE;
2020-08-31 12:38:32 +08:00
2020-10-29 18:17:25 +08:00
/**
* @var Connection|null
*/
public ?Connection $db = NULL;
2020-08-31 12:38:32 +08:00
/**
* @var array
* 参数绑定
*/
2020-10-29 18:17:25 +08:00
public array $attributes = [];
2020-08-31 12:38:32 +08:00
2021-02-25 16:43:40 +08:00
private SqlBuilder $builder;
2020-08-31 12:38:32 +08:00
/**
* Comply constructor.
* @param $model
* @param array $config
2020-09-11 18:04:46 +08:00
* @throws
2020-08-31 12:38:32 +08:00
*/
2021-02-25 16:43:40 +08:00
public function __construct($model, $config = [])
2020-08-31 12:38:32 +08:00
{
2020-09-11 18:04:46 +08:00
if (!is_object($model)) {
$model = Snowflake::createObject($model);
}
2020-08-31 12:38:32 +08:00
$this->modelClass = $model;
2021-02-25 16:43:40 +08:00
$this->builder = SqlBuilder::builder($this);
parent::__construct($config);
2020-08-31 12:38:32 +08:00
}
2021-02-25 16:43:40 +08:00
2020-08-31 12:38:32 +08:00
/**
* 清除不完整数据
*/
public function clear()
{
$this->db = null;
$this->useCache = false;
$this->with = [];
}
/**
* @param $key
* @param $value
* @return $this
*/
2020-12-17 14:09:14 +08:00
public function addParam($key, $value): static
2020-08-31 12:38:32 +08:00
{
$this->attributes[$key] = $value;
return $this;
}
/**
* @param array $values
* @return $this
*/
2020-12-17 14:09:14 +08:00
public function addParams(array $values): static
2020-08-31 12:38:32 +08:00
{
foreach ($values as $key => $val) {
$this->addParam($key, $val);
}
return $this;
}
/**
* @param $name
* @return $this
*/
2020-12-17 14:09:14 +08:00
public function with($name): static
2020-08-31 12:38:32 +08:00
{
if (empty($name)) {
return $this;
}
if (is_string($name)) {
$name = explode(',', $name);
}
foreach ($name as $key => $val) {
array_push($this->with, $val);
}
return $this;
}
/**
* @param bool $isArray
* @return $this
*/
2020-12-17 14:09:14 +08:00
public function asArray($isArray = TRUE): static
2020-08-31 12:38:32 +08:00
{
$this->asArray = $isArray;
return $this;
}
2021-02-25 16:43:40 +08:00
/**
* @param $sql
* @return mixed
*/
public function execute($sql): Command
{
return $this->modelClass::getDb()->createCommand($sql);
}
2020-08-31 12:38:32 +08:00
/**
2020-12-17 14:09:14 +08:00
* @return ActiveRecord|array|null
* @throws Exception
2020-08-31 12:38:32 +08:00
*/
2020-12-17 14:09:14 +08:00
public function first(): ActiveRecord|array|null
2020-08-31 12:38:32 +08:00
{
2021-02-25 16:43:40 +08:00
$data = $this->execute($this->builder->one())->one();
2020-08-31 12:38:32 +08:00
if (empty($data)) {
return NULL;
}
2020-09-11 18:04:46 +08:00
$newModel = $this->modelClass;
2020-08-31 12:38:32 +08:00
$newModel = $this->populate($newModel, $data);
if ($this->asArray) {
return $newModel->toArray();
}
return $newModel;
}
/**
* @return array|Collection
*/
2020-12-17 14:09:14 +08:00
public function get(): Collection|array
2020-08-31 12:38:32 +08:00
{
return $this->all();
}
/**
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function flush(): array|bool|int|string|null
2020-08-31 12:38:32 +08:00
{
2021-02-25 16:43:40 +08:00
return $this->execute($this->builder->truncate())->exec();
2020-08-31 12:38:32 +08:00
}
/**
* @param int $size
2020-09-14 10:43:02 +08:00
* @param callable $callback
* @return Pagination
2020-08-31 12:38:32 +08:00
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function page(int $size, callable $callback): Pagination
2020-08-31 12:38:32 +08:00
{
2021-02-23 17:43:13 +08:00
$pagination = new Pagination($this);
2020-09-14 10:47:18 +08:00
$pagination->setOffset(0);
2020-09-14 10:40:49 +08:00
$pagination->setLimit($size);
$pagination->setCallback($callback);
2020-09-14 10:43:02 +08:00
return $pagination;
2020-08-31 12:38:32 +08:00
}
/**
* @param string $field
* @param string $setKey
*
* @return array|null
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function column(string $field, $setKey = ''): ?array
2020-08-31 12:38:32 +08:00
{
return $this->all()->column($field, $setKey);
}
/**
* @return array|Collection
* @throws
*/
2020-12-17 14:09:14 +08:00
public function all(): Collection|array
2020-08-31 12:38:32 +08:00
{
2021-02-25 16:43:40 +08:00
$data = $this->execute($this->builder->all())->all();
2021-03-05 18:21:48 +08:00
var_dump($data);
2021-02-23 17:43:13 +08:00
$collect = new Collection($this, $data, $this->modelClass);
2020-08-31 12:38:32 +08:00
if ($this->asArray) {
return $collect->toArray();
}
return $collect;
}
/**
* @param ActiveRecord $model
* @param $data
* @return ActiveRecord
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function populate(ActiveRecord $model, $data): ActiveRecord
2020-09-08 01:21:07 +08:00
{
return $this->getWith($model::populate($data));
}
/**
2021-03-04 14:24:45 +08:00
* @param ActiveRecord $model
* @return ActiveRecord
2020-09-08 01:21:07 +08:00
*/
2021-03-04 14:24:45 +08:00
public function getWith(ActiveRecord $model): ActiveRecord
2020-08-31 12:38:32 +08:00
{
if (empty($this->with) || !is_array($this->with)) {
return $model;
}
2021-03-04 15:05:46 +08:00
return $model->setWith($this->with);
2020-08-31 12:38:32 +08:00
}
/**
* @return int
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function count(): int
2020-08-31 12:38:32 +08:00
{
2021-02-25 16:43:40 +08:00
$data = $this->execute($this->builder->count())->one();
2020-08-31 12:38:32 +08:00
if ($data && is_array($data)) {
return (int)array_shift($data);
}
return 0;
}
/**
* @param array $data
* @return array|Command|bool|int|string
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function batchUpdate(array $data): Command|array|bool|int|string
2020-08-31 12:38:32 +08:00
{
2021-02-25 18:08:19 +08:00
$generate = $this->builder->update($data);
if (is_bool($generate)) {
return $generate;
}
return $this->execute(...$generate)->exec();
2020-08-31 12:38:32 +08:00
}
/**
* @param array $data
* @return bool
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function batchInsert(array $data): bool
2020-08-31 12:38:32 +08:00
{
2021-02-25 17:57:20 +08:00
return $this->execute($this->builder->insert($data, true))->exec();
2020-08-31 12:38:32 +08:00
}
/**
* @param $filed
*
* @return null
* @throws Exception
*/
public function value($filed)
{
2021-02-25 16:43:40 +08:00
return $this->first()[$filed] ?? null;
2020-08-31 12:38:32 +08:00
}
/**
* @return bool
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function exists(): bool
2020-08-31 12:38:32 +08:00
{
2021-02-25 16:43:40 +08:00
return $this->execute($this->builder->one())->fetchColumn() !== false;
2020-08-31 12:38:32 +08:00
}
2020-12-17 14:09:14 +08:00
2020-08-31 12:38:32 +08:00
/**
* @return bool
* @throws Exception
*/
2020-12-17 14:09:14 +08:00
public function delete(): bool
2020-08-31 12:38:32 +08:00
{
2021-02-25 16:43:40 +08:00
return $this->execute($this->builder->all())->delete();
2020-08-31 12:38:32 +08:00
}
}