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;
|
||||
protected mixed $_mock = null;
|
||||
public bool $asArray = FALSE;
|
||||
protected mixed $_mock = NULL;
|
||||
|
||||
|
||||
/**
|
||||
* @param bool $asArray
|
||||
* @return static
|
||||
*/
|
||||
public function asArray(bool $asArray = true): static
|
||||
{
|
||||
$this->asArray = $asArray;
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @param bool $asArray
|
||||
*
|
||||
* @return static
|
||||
*/
|
||||
public function asArray(bool $asArray = TRUE): static
|
||||
{
|
||||
$this->asArray = $asArray;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $methods
|
||||
* @return $this
|
||||
*/
|
||||
public function with(array $methods): static
|
||||
{
|
||||
$this->modelClass->setWith($methods);
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @param array $methods
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function with(array $methods): static
|
||||
{
|
||||
$this->modelClass->setWith($methods);
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return ModelInterface|array|null|bool
|
||||
* @throws
|
||||
*/
|
||||
public function first(): ModelInterface|null|array|bool
|
||||
{
|
||||
$data = $this->buildCommand($this->builder->one())->one();
|
||||
if (is_array($data)) {
|
||||
return $this->populate($data);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
/**
|
||||
* @return ModelInterface|array|null|bool
|
||||
* @throws
|
||||
*/
|
||||
public function first(): ModelInterface|null|array|bool
|
||||
{
|
||||
$data = $this->buildCommand($this->builder->one())->one();
|
||||
if (is_array($data)) {
|
||||
return $this->populate($data);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool|Collection
|
||||
*/
|
||||
public function get(): bool|Collection
|
||||
{
|
||||
$data = $this->buildCommand($this->builder->all())->all();
|
||||
if (is_array($data)) {
|
||||
return new Collection($this, $this->modelClass, $data);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* @return bool|Collection
|
||||
*/
|
||||
public function get(): bool|Collection
|
||||
{
|
||||
$data = $this->buildCommand($this->builder->all())->all();
|
||||
if (is_array($data)) {
|
||||
return new Collection($this, $this->modelClass, $data);
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $column
|
||||
* @param int|float $amount
|
||||
* @return bool
|
||||
*/
|
||||
public function increment(string $column, int|float $amount = 1): bool
|
||||
{
|
||||
return (bool)$this->buildCommand($this->builder->mathematics([$column => $amount]))->exec();
|
||||
}
|
||||
/**
|
||||
* @param string $column
|
||||
* @param int|float $amount
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function increment(string $column, int|float $amount = 1): bool
|
||||
{
|
||||
return (bool)$this->buildCommand($this->builder->mathematics([$column => $amount]))->exec();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $attributes
|
||||
* @return bool
|
||||
*/
|
||||
public function increments(array $attributes): bool
|
||||
{
|
||||
return (bool)$this->buildCommand($this->builder->mathematics($attributes))->exec();
|
||||
}
|
||||
/**
|
||||
* @param array $attributes
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function increments(array $attributes): bool
|
||||
{
|
||||
return (bool)$this->buildCommand($this->builder->mathematics($attributes))->exec();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $column
|
||||
* @param int|float $amount
|
||||
* @return bool
|
||||
*/
|
||||
public function decrement(string $column, int|float $amount = 1): bool
|
||||
{
|
||||
return (bool)$this->buildCommand($this->builder->mathematics([$column => $amount], '-'))->exec();
|
||||
}
|
||||
/**
|
||||
* @param string $column
|
||||
* @param int|float $amount
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function decrement(string $column, int|float $amount = 1): bool
|
||||
{
|
||||
return (bool)$this->buildCommand($this->builder->mathematics([$column => $amount], '-'))->exec();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $attributes
|
||||
* @return bool
|
||||
*/
|
||||
public function decrements(array $attributes): bool
|
||||
{
|
||||
return (bool)$this->buildCommand($this->builder->mathematics($attributes, '-'))->exec();
|
||||
}
|
||||
/**
|
||||
* @param array $attributes
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function decrements(array $attributes): bool
|
||||
{
|
||||
return (bool)$this->buildCommand($this->builder->mathematics($attributes, '-'))->exec();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws
|
||||
*/
|
||||
public function flush(): bool
|
||||
{
|
||||
return (bool)$this->buildCommand($this->builder->truncate())->exec();
|
||||
}
|
||||
/**
|
||||
* @throws
|
||||
*/
|
||||
public function flush(): bool
|
||||
{
|
||||
return (bool)$this->buildCommand($this->builder->truncate())->exec();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param int $size
|
||||
* @param Closure $closure
|
||||
* @return 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 (!$data || $data->isEmpty()) {
|
||||
return;
|
||||
}
|
||||
if (Context::inCoroutine()) {
|
||||
Coroutine::create(fn () => $closure($data));
|
||||
} else {
|
||||
call_user_func($closure, $data);
|
||||
}
|
||||
$this->offset += $size;
|
||||
$this->chunk($size, $closure);
|
||||
}
|
||||
/**
|
||||
* @param int $size
|
||||
* @param Closure $closure
|
||||
*
|
||||
* @return 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 (!$data || $data->isEmpty()) {
|
||||
return;
|
||||
}
|
||||
if (Context::inCoroutine()) {
|
||||
Coroutine::create(fn() => $closure($data));
|
||||
} else {
|
||||
call_user_func($closure, $data);
|
||||
}
|
||||
$this->offset += $size;
|
||||
$this->chunk($size, $closure);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $field
|
||||
* @param string|null $setKey
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
public function column(string $field, ?string $setKey = null): ?array
|
||||
{
|
||||
return $this->get()->column($field, $setKey);
|
||||
}
|
||||
/**
|
||||
* @param string $field
|
||||
* @param string|null $setKey
|
||||
*
|
||||
* @return array|null
|
||||
*/
|
||||
public function column(string $field, ?string $setKey = NULL): ?array
|
||||
{
|
||||
return $this->get()->column($field, $setKey);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
* @return $this
|
||||
*/
|
||||
public function withMock(mixed $value): static
|
||||
{
|
||||
$this->_mock = $value;
|
||||
return $this;
|
||||
}
|
||||
/**
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function withMock(mixed $value): static
|
||||
{
|
||||
$this->_mock = $value;
|
||||
return $this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function mock(): mixed
|
||||
{
|
||||
return $this->_mock;
|
||||
}
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
public function mock(): mixed
|
||||
{
|
||||
return $this->_mock;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return ModelInterface|array
|
||||
* @throws
|
||||
*/
|
||||
public function populate(array $data): ModelInterface|array
|
||||
{
|
||||
$model = $this->modelClass->populates($data);
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return ModelInterface|array
|
||||
* @throws
|
||||
*/
|
||||
public function populate(array $data): ModelInterface|array
|
||||
{
|
||||
$model = $this->modelClass->populates($data);
|
||||
|
||||
return $this->asArray ? $model->toArray() : $model;
|
||||
}
|
||||
return $this->asArray ? $model->toArray() : $model;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return int
|
||||
* @throws
|
||||
*/
|
||||
public function count(): int
|
||||
{
|
||||
return $this->buildCommand($this->builder->count())->rowCount();
|
||||
}
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function toSql(): string
|
||||
{
|
||||
return $this->builder->get();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return bool
|
||||
* @throws
|
||||
*/
|
||||
public function update(array $data): bool
|
||||
{
|
||||
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();
|
||||
}
|
||||
/**
|
||||
* @return int
|
||||
* @throws
|
||||
*/
|
||||
public function count(): int
|
||||
{
|
||||
return $this->buildCommand($this->builder->count())->rowCount();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $sql
|
||||
* @param array $params
|
||||
* @return int|bool
|
||||
*/
|
||||
public function execute(string $sql, array $params = []): int|bool
|
||||
{
|
||||
return $this->buildCommand($sql, $params)->exec();
|
||||
}
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return bool
|
||||
* @throws
|
||||
*/
|
||||
public function update(array $data): bool
|
||||
{
|
||||
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
|
||||
*/
|
||||
public function delete(): bool
|
||||
{
|
||||
return $this->buildCommand($this->builder->delete())->delete();
|
||||
}
|
||||
/**
|
||||
* @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 $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)) {
|
||||
return $keys;
|
||||
}
|
||||
if (is_string($value) && $this->isMath($value)) {
|
||||
if (preg_match('/^[+|-]\s\d+$/', $value)) {
|
||||
$keys[] = $key . '=' . $key . ' ' . $value;
|
||||
} else {
|
||||
$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
|
||||
* @throws
|
||||
|
||||
+1041
-952
File diff suppressed because it is too large
Load Diff
@@ -1,13 +1,23 @@
|
||||
<?php
|
||||
|
||||
|
||||
use Database\Query;
|
||||
|
||||
class Users extends \Database\Model
|
||||
{
|
||||
|
||||
|
||||
public function hasD()
|
||||
{
|
||||
return $this->hasOne(static::class, 'id', 'id')->with([]);
|
||||
}
|
||||
}
|
||||
|
||||
public function hasD()
|
||||
{
|
||||
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