This commit is contained in:
2024-12-13 14:04:58 +08:00
parent f084c171ff
commit b3e3e95081
+223 -210
View File
@@ -22,172 +22,181 @@ class ActiveQuery extends QueryTrait implements ISqlBuilder
{ {
public bool $asArray = FALSE; public bool $asArray = FALSE;
protected mixed $_mock = null; protected mixed $_mock = NULL;
/** /**
* @param bool $asArray * @param bool $asArray
* @return static *
*/ * @return static
public function asArray(bool $asArray = true): static */
{ public function asArray(bool $asArray = TRUE): static
$this->asArray = $asArray; {
return $this; $this->asArray = $asArray;
} return $this;
}
/** /**
* @param array $methods * @param array $methods
* @return $this *
*/ * @return $this
public function with(array $methods): static */
{ public function with(array $methods): static
$this->modelClass->setWith($methods); {
return $this; $this->modelClass->setWith($methods);
} return $this;
}
/** /**
* @return ModelInterface|array|null|bool * @return ModelInterface|array|null|bool
* @throws * @throws
*/ */
public function first(): ModelInterface|null|array|bool public function first(): ModelInterface|null|array|bool
{ {
$data = $this->buildCommand($this->builder->one())->one(); $data = $this->buildCommand($this->builder->one())->one();
if (is_array($data)) { if (is_array($data)) {
return $this->populate($data); return $this->populate($data);
} }
return null; return NULL;
} }
/** /**
* @return bool|Collection * @return bool|Collection
*/ */
public function get(): bool|Collection public function get(): bool|Collection
{ {
$data = $this->buildCommand($this->builder->all())->all(); $data = $this->buildCommand($this->builder->all())->all();
if (is_array($data)) { if (is_array($data)) {
return new Collection($this, $this->modelClass, $data); return new Collection($this, $this->modelClass, $data);
} }
return false; return FALSE;
} }
/** /**
* @param string $column * @param string $column
* @param int|float $amount * @param int|float $amount
* @return bool *
*/ * @return bool
public function increment(string $column, int|float $amount = 1): bool */
{ public function increment(string $column, int|float $amount = 1): bool
return (bool)$this->buildCommand($this->builder->mathematics([$column => $amount]))->exec(); {
} return (bool)$this->buildCommand($this->builder->mathematics([$column => $amount]))->exec();
}
/** /**
* @param array $attributes * @param array $attributes
* @return bool *
*/ * @return bool
public function increments(array $attributes): bool */
{ public function increments(array $attributes): bool
return (bool)$this->buildCommand($this->builder->mathematics($attributes))->exec(); {
} return (bool)$this->buildCommand($this->builder->mathematics($attributes))->exec();
}
/** /**
* @param string $column * @param string $column
* @param int|float $amount * @param int|float $amount
* @return bool *
*/ * @return bool
public function decrement(string $column, int|float $amount = 1): bool */
{ public function decrement(string $column, int|float $amount = 1): bool
return (bool)$this->buildCommand($this->builder->mathematics([$column => $amount], '-'))->exec(); {
} return (bool)$this->buildCommand($this->builder->mathematics([$column => $amount], '-'))->exec();
}
/** /**
* @param array $attributes * @param array $attributes
* @return bool *
*/ * @return bool
public function decrements(array $attributes): bool */
{ public function decrements(array $attributes): bool
return (bool)$this->buildCommand($this->builder->mathematics($attributes, '-'))->exec(); {
} return (bool)$this->buildCommand($this->builder->mathematics($attributes, '-'))->exec();
}
/** /**
* @throws * @throws
*/ */
public function flush(): bool public function flush(): bool
{ {
return (bool)$this->buildCommand($this->builder->truncate())->exec(); return (bool)$this->buildCommand($this->builder->truncate())->exec();
} }
/** /**
* @param int $size * @param int $size
* @param Closure $closure * @param Closure $closure
* @return void *
*/ * @return void
public function chunk(int $size, Closure $closure): void */
{ public function chunk(int $size, Closure $closure): void
if ($this->offset === -1) $this->offset = 0; {
$data = $this->offset($this->offset)->limit($size)->get(); if ($this->offset === -1) $this->offset = 0;
if (!$data || $data->isEmpty()) { $data = $this->offset($this->offset)->limit($size)->get();
return; if (!$data || $data->isEmpty()) {
} return;
if (Context::inCoroutine()) { }
Coroutine::create(fn () => $closure($data)); if (Context::inCoroutine()) {
} else { Coroutine::create(fn() => $closure($data));
call_user_func($closure, $data); } else {
} call_user_func($closure, $data);
$this->offset += $size; }
$this->chunk($size, $closure); $this->offset += $size;
} $this->chunk($size, $closure);
}
/** /**
* @param string $field * @param string $field
* @param string|null $setKey * @param string|null $setKey
* *
* @return array|null * @return array|null
*/ */
public function column(string $field, ?string $setKey = null): ?array public function column(string $field, ?string $setKey = NULL): ?array
{ {
return $this->get()->column($field, $setKey); return $this->get()->column($field, $setKey);
} }
/** /**
* @param mixed $value * @param mixed $value
* @return $this *
*/ * @return $this
public function withMock(mixed $value): static */
{ public function withMock(mixed $value): static
$this->_mock = $value; {
return $this; $this->_mock = $value;
} return $this;
}
/** /**
* @return mixed * @return mixed
*/ */
public function mock(): mixed public function mock(): mixed
{ {
return $this->_mock; return $this->_mock;
} }
/** /**
* @param array $data * @param array $data
* @return ModelInterface|array *
* @throws * @return ModelInterface|array
*/ * @throws
public function populate(array $data): ModelInterface|array */
{ public function populate(array $data): ModelInterface|array
$model = $this->modelClass->populates($data); {
$model = $this->modelClass->populates($data);
return $this->asArray ? $model->toArray() : $model; return $this->asArray ? $model->toArray() : $model;
} }
/** /**
@@ -199,82 +208,86 @@ class ActiveQuery extends QueryTrait implements ISqlBuilder
} }
/** /**
* @return int * @return int
* @throws * @throws
*/ */
public function count(): int public function count(): int
{ {
return $this->buildCommand($this->builder->count())->rowCount(); return $this->buildCommand($this->builder->count())->rowCount();
} }
/** /**
* @param array $data * @param array $data
* @return bool *
* @throws * @return bool
*/ * @throws
public function update(array $data): bool */
{ public function update(array $data): bool
if (count($data) < 1) { {
return true; if (count($data) < 1) {
} return TRUE;
$generate = $this->builder->update($data); }
if (!is_bool($generate)) { $generate = $this->builder->update($data);
return (bool)$this->buildCommand($generate)->exec(); if (!is_bool($generate)) {
} else { return (bool)$this->buildCommand($generate)->exec();
return $generate; } else {
} return $generate;
} }
}
/** /**
* @param array $data * @param array $data
* @return bool *
*/ * @return bool
public function insert(array $data): bool */
{ public function insert(array $data): bool
$sql = $this->builder->insert($data, isset($data[0])); {
$sql = $this->builder->insert($data, isset($data[0]));
return (bool)$this->buildCommand($sql)->exec(); return (bool)$this->buildCommand($sql)->exec();
} }
/**
* @param string $filed
*
* @return mixed
* @throws
*/
public function value(string $filed): mixed
{
return $this->first()[$filed] ?? NULL;
}
/**
* @return bool
* @throws
*/
public function exists(): bool
{
return $this->buildCommand($this->limit(1)->builder->exists())->exists();
}
/** /**
* @param string $sql * @param string $filed
* @param array $params *
* @return int|bool * @return mixed
*/ * @throws
public function execute(string $sql, array $params = []): int|bool */
{ public function value(string $filed): mixed
return $this->buildCommand($sql, $params)->exec(); {
} return $this->first()[$filed] ?? NULL;
}
/**
* @return bool
* @throws
*/
public function exists(): bool
{
return $this->buildCommand($this->limit(1)->builder->exists())->exists();
}
/** /**
* @return bool * @param string $sql
*/ * @param array $params
public function delete(): bool *
{ * @return int|bool
return $this->buildCommand($this->builder->delete())->delete(); */
} public function execute(string $sql, array $params = []): int|bool
{
return $this->buildCommand($sql, $params)->exec();
}
/**
* @return bool
*/
public function delete(): bool
{
return $this->buildCommand($this->builder->delete())->delete();
}
} }