Compare commits
15 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b3f50fc6e4 | |||
| 7b4604ce98 | |||
| 654cc483b3 | |||
| c6b88256e3 | |||
| 5b4a6b9caa | |||
| 42af2e8bf9 | |||
| db92238e9e | |||
| 530104ceb6 | |||
| 7ea245fc89 | |||
| 0cc1d3d9f7 | |||
| c444de4815 | |||
| 263f99c8c3 | |||
| 450870ea19 | |||
| b3e3e95081 | |||
| f084c171ff |
+234
-212
@@ -22,250 +22,272 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return int
|
* @return string
|
||||||
* @throws
|
*/
|
||||||
*/
|
public function toSql(): string
|
||||||
public function count(): int
|
{
|
||||||
{
|
return $this->builder->get();
|
||||||
return $this->buildCommand($this->builder->count())->rowCount();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $data
|
* @return int
|
||||||
* @return bool
|
* @throws
|
||||||
* @throws
|
*/
|
||||||
*/
|
public function count(): int
|
||||||
public function update(array $data): bool
|
{
|
||||||
{
|
return $this->buildCommand($this->builder->count())->rowCount();
|
||||||
if (count($data) < 1) {
|
}
|
||||||
return true;
|
|
||||||
}
|
|
||||||
$generate = $this->builder->update($data);
|
|
||||||
if (!is_bool($generate)) {
|
|
||||||
return (bool)$this->buildCommand($generate)->exec();
|
|
||||||
} else {
|
|
||||||
return $generate;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param array $data
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
public function insert(array $data): bool
|
|
||||||
{
|
|
||||||
$sql = $this->builder->insert($data, isset($data[0]));
|
|
||||||
|
|
||||||
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 array $data
|
||||||
* @param array $params
|
*
|
||||||
* @return int|bool
|
* @return bool
|
||||||
*/
|
* @throws
|
||||||
public function execute(string $sql, array $params = []): int|bool
|
*/
|
||||||
{
|
public function update(array $data): bool
|
||||||
return $this->buildCommand($sql, $params)->exec();
|
{
|
||||||
}
|
if (count($data) < 1) {
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
$generate = $this->builder->update($data);
|
||||||
|
if (!is_bool($generate)) {
|
||||||
|
return (bool)$this->buildCommand($generate)->exec();
|
||||||
|
} else {
|
||||||
|
return $generate;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $data
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function insert(array $data): bool
|
||||||
|
{
|
||||||
|
$sql = $this->builder->insert($data, isset($data[0]));
|
||||||
|
|
||||||
|
return (bool)$this->buildCommand($sql)->exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return bool
|
* @param string $filed
|
||||||
*/
|
*
|
||||||
public function delete(): bool
|
* @return mixed
|
||||||
{
|
* @throws
|
||||||
return $this->buildCommand($this->builder->delete())->delete();
|
*/
|
||||||
}
|
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 array $params
|
||||||
|
*
|
||||||
|
* @return int|bool
|
||||||
|
*/
|
||||||
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+826
-781
File diff suppressed because it is too large
Load Diff
+1
-11
@@ -189,7 +189,7 @@ class SqlBuilder extends Component
|
|||||||
if (is_null($value)) {
|
if (is_null($value)) {
|
||||||
return $keys;
|
return $keys;
|
||||||
}
|
}
|
||||||
if (is_string($value) && $this->isMath($value)) {
|
if (preg_match('/^[+|-]\s\d+$/', $value)) {
|
||||||
$keys[] = $key . '=' . $key . ' ' . $value;
|
$keys[] = $key . '=' . $key . ' ' . $value;
|
||||||
} else {
|
} else {
|
||||||
$this->query->pushParam($value);
|
$this->query->pushParam($value);
|
||||||
@@ -199,16 +199,6 @@ class SqlBuilder extends Component
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $value
|
|
||||||
* @return bool
|
|
||||||
*/
|
|
||||||
private function isMath(string $value): bool
|
|
||||||
{
|
|
||||||
return str_starts_with($value, '+ ') || str_starts_with($value, '- ');
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return string
|
||||||
* @throws
|
* @throws
|
||||||
|
|||||||
+1041
-952
File diff suppressed because it is too large
Load Diff
@@ -1,13 +1,23 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
use Database\Query;
|
||||||
|
|
||||||
class Users extends \Database\Model
|
class Users extends \Database\Model
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
|
public function hasD()
|
||||||
public function hasD()
|
{
|
||||||
{
|
return $this->hasOne(static::class, 'id', 'id')->with([]);
|
||||||
return $this->hasOne(static::class, 'id', 'id')->with([]);
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Users::query()
|
||||||
|
->select(['*', (new Query(Users::class))
|
||||||
|
->where(['id' => 2])])
|
||||||
|
->from(function (Query $query) {
|
||||||
|
$query->from(Users::class)
|
||||||
|
->where(['id' => 1])
|
||||||
|
->groupBy('name DESC');
|
||||||
|
})->toSql();
|
||||||
Reference in New Issue
Block a user