This commit is contained in:
2021-04-16 21:37:05 +08:00
parent 52a5722837
commit 393584da26
+259 -255
View File
@@ -9,9 +9,9 @@ declare(strict_types=1);
namespace Database; namespace Database;
use Snowflake\Abstracts\Component;
use Database\Traits\QueryTrait; use Database\Traits\QueryTrait;
use Exception; use Exception;
use Snowflake\Abstracts\Component;
use Snowflake\Snowflake; use Snowflake\Snowflake;
/** /**
@@ -21,292 +21,296 @@ use Snowflake\Snowflake;
class ActiveQuery extends Component implements ISqlBuilder class ActiveQuery extends Component implements ISqlBuilder
{ {
use QueryTrait; use QueryTrait;
/** @var array */ /** @var array */
public array $with = []; public array $with = [];
/** @var bool */ /** @var bool */
public bool $asArray = FALSE; public bool $asArray = FALSE;
/** @var bool */ /** @var bool */
public bool $useCache = FALSE; public bool $useCache = FALSE;
/** /**
* @var Connection|null * @var Connection|null
*/ */
public ?Connection $db = NULL; public ?Connection $db = NULL;
/** /**
* @var array * @var array
* 参数绑定 * 参数绑定
*/ */
public array $attributes = []; public array $attributes = [];
private SqlBuilder $builder; private SqlBuilder $builder;
/** /**
* Comply constructor. * Comply constructor.
* @param $model * @param $model
* @param array $config * @param array $config
* @throws * @throws
*/ */
public function __construct($model, $config = []) public function __construct($model, $config = [])
{ {
if (!is_object($model)) { if (!is_object($model)) {
$model = Snowflake::createObject($model); $model = Snowflake::createObject($model);
} }
$this->modelClass = $model; $this->modelClass = $model;
$this->builder = SqlBuilder::builder($this); $this->builder = SqlBuilder::builder($this);
parent::__construct($config); parent::__construct($config);
} }
/** /**
* 清除不完整数据 * 清除不完整数据
*/ */
public function clear() public function clear()
{ {
$this->db = null; $this->db = null;
$this->useCache = false; $this->useCache = false;
$this->with = []; $this->with = [];
} }
/** /**
* @param $key * @param $key
* @param $value * @param $value
* @return $this * @return $this
*/ */
public function addParam($key, $value): static public function addParam($key, $value): static
{ {
$this->attributes[$key] = $value; $this->attributes[$key] = $value;
return $this; return $this;
} }
/** /**
* @param array $values * @param array $values
* @return $this * @return $this
*/ */
public function addParams(array $values): static public function addParams(array $values): static
{ {
foreach ($values as $key => $val) { foreach ($values as $key => $val) {
$this->addParam($key, $val); $this->addParam($key, $val);
} }
return $this; return $this;
} }
/** /**
* @param $name * @param $name
* @return $this * @return $this
*/ */
public function with($name): static public function with($name): static
{ {
if (empty($name)) { if (empty($name)) {
return $this; return $this;
} }
if (is_string($name)) { if (is_string($name)) {
$name = explode(',', $name); $name = explode(',', $name);
} }
foreach ($name as $key => $val) { foreach ($name as $key => $val) {
array_push($this->with, $val); array_push($this->with, $val);
} }
return $this; return $this;
} }
/** /**
* @param bool $isArray * @param bool $isArray
* @return $this * @return $this
*/ */
public function asArray($isArray = TRUE): static public function asArray($isArray = TRUE): static
{ {
$this->asArray = $isArray; $this->asArray = $isArray;
return $this; return $this;
} }
/** /**
* @param $sql * @param $sql
* @return mixed * @param array $params
*/ * @return mixed
public function execute($sql): Command */
{ public function execute($sql, $params = []): Command
return $this->modelClass::getDb()->createCommand($sql); {
} return $this->modelClass::getDb()->createCommand($sql, $params);
}
/** /**
* @return ActiveRecord|array|null * @return ActiveRecord|array|null
* @throws Exception * @throws Exception
*/ */
public function first(): ActiveRecord|array|null public function first(): ActiveRecord|array|null
{ {
$data = $this->execute($this->builder->one())->one(); $data = $this->execute($this->builder->one())->one();
if (empty($data)) { if (empty($data)) {
return NULL; return NULL;
} }
$newModel = $this->modelClass; $newModel = $this->modelClass;
$newModel = $this->populate($newModel, $data); $newModel = $this->populate($newModel, $data);
if ($this->asArray) { if ($this->asArray) {
return $newModel->toArray(); return $newModel->toArray();
} }
return $newModel; return $newModel;
} }
/** /**
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
public function toSql(): string public function toSql(): string
{ {
return $this->builder->get(); return $this->builder->get();
} }
/** /**
* @return array|Collection * @return array|Collection
*/ */
public function get(): Collection|array public function get(): Collection|array
{ {
return $this->all(); return $this->all();
} }
/** /**
* @throws Exception * @throws Exception
*/ */
public function flush(): array|bool|int|string|null public function flush(): array|bool|int|string|null
{ {
return $this->execute($this->builder->truncate())->exec(); return $this->execute($this->builder->truncate())->exec();
} }
/** /**
* @param int $size * @param int $size
* @param callable $callback * @param callable $callback
* @return Pagination * @return Pagination
* @throws Exception * @throws Exception
*/ */
public function page(int $size, callable $callback): Pagination public function page(int $size, callable $callback): Pagination
{ {
$pagination = new Pagination($this); $pagination = new Pagination($this);
$pagination->setOffset(0); $pagination->setOffset(0);
$pagination->setLimit($size); $pagination->setLimit($size);
$pagination->setCallback($callback); $pagination->setCallback($callback);
return $pagination; return $pagination;
} }
/** /**
* @param string $field * @param string $field
* @param string $setKey * @param string $setKey
* *
* @return array|null * @return array|null
* @throws Exception * @throws Exception
*/ */
public function column(string $field, $setKey = ''): ?array public function column(string $field, $setKey = ''): ?array
{ {
return $this->all()->column($field, $setKey); return $this->all()->column($field, $setKey);
} }
/** /**
* @return array|Collection * @return array|Collection
* @throws * @throws
*/ */
public function all(): Collection|array public function all(): Collection|array
{ {
$data = $this->execute($this->builder->all())->all(); $data = $this->execute($this->builder->all())->all();
$collect = new Collection($this, $data, $this->modelClass); $collect = new Collection($this, $data, $this->modelClass);
if ($this->asArray) { if ($this->asArray) {
return $collect->toArray(); return $collect->toArray();
} }
return $collect; return $collect;
} }
/** /**
* @param ActiveRecord $model * @param ActiveRecord $model
* @param $data * @param $data
* @return ActiveRecord * @return ActiveRecord
* @throws Exception * @throws Exception
*/ */
public function populate(ActiveRecord $model, $data): ActiveRecord public function populate(ActiveRecord $model, $data): ActiveRecord
{ {
return $this->getWith($model::populate($data)); return $this->getWith($model::populate($data));
} }
/** /**
* @param ActiveRecord $model * @param ActiveRecord $model
* @return ActiveRecord * @return ActiveRecord
*/ */
public function getWith(ActiveRecord $model): ActiveRecord public function getWith(ActiveRecord $model): ActiveRecord
{ {
if (empty($this->with) || !is_array($this->with)) { if (empty($this->with) || !is_array($this->with)) {
return $model; return $model;
} }
return $model->setWith($this->with); return $model->setWith($this->with);
} }
/** /**
* @return int * @return int
* @throws Exception * @throws Exception
*/ */
public function count(): int public function count(): int
{ {
$data = $this->execute($this->builder->count())->one(); $data = $this->execute($this->builder->count())->one();
if ($data && is_array($data)) { if ($data && is_array($data)) {
return (int)array_shift($data); return (int)array_shift($data);
} }
return 0; return 0;
} }
/** /**
* @param array $data * @param array $data
* @return array|Command|bool|int|string * @return array|Command|bool|int|string
* @throws Exception * @throws Exception
*/ */
public function batchUpdate(array $data): Command|array|bool|int|string public function batchUpdate(array $data): Command|array|bool|int|string
{ {
$generate = $this->builder->update($data); $generate = $this->builder->update($data);
if (is_bool($generate)) { if (is_bool($generate)) {
return $generate; return $generate;
} }
return $this->execute(...$generate)->exec(); return $this->execute(...$generate)->exec();
} }
/** /**
* @param array $data * @param array $data
* @return bool * @return bool
* @throws Exception * @throws Exception
*/ */
public function batchInsert(array $data): bool public function batchInsert(array $data): bool
{ {
return $this->execute($this->builder->insert($data, true))->exec(); [$sql, $params] = $this->builder->insert($data, true);
}
/**
* @param $filed
*
* @return null
* @throws Exception
*/
public function value($filed)
{
return $this->first()[$filed] ?? null;
}
/** return $this->execute($sql, $params)->exec();
* @return bool }
* @throws Exception
*/ /**
public function exists(): bool * @param $filed
{ *
return $this->execute($this->builder->one())->fetchColumn() !== false; * @return null
} * @throws Exception
*/
public function value($filed)
{
return $this->first()[$filed] ?? null;
}
/**
* @return bool
* @throws Exception
*/
public function exists(): bool
{
return $this->execute($this->builder->one())->fetchColumn() !== false;
}
/** /**
@@ -314,12 +318,12 @@ class ActiveQuery extends Component implements ISqlBuilder
* @return string|bool * @return string|bool
* @throws Exception * @throws Exception
*/ */
public function delete($getSql = false): string|bool public function delete($getSql = false): string|bool
{ {
$sql = $this->builder->delete(); $sql = $this->builder->delete();
if ($getSql === false) { if ($getSql === false) {
return $this->execute($sql)->delete(); return $this->execute($sql)->delete();
} }
return $sql; return $sql;
} }
} }