ea
This commit is contained in:
@@ -190,6 +190,15 @@ class ActiveQuery extends QueryTrait implements ISqlBuilder
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function toSql(): string
|
||||
{
|
||||
return $this->builder->get();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return int
|
||||
* @throws
|
||||
|
||||
+57
-10
@@ -28,7 +28,7 @@ use Kiri\Abstracts\Component;
|
||||
abstract class QueryTrait extends Component implements ActiveQueryInterface, ISqlBuilder
|
||||
{
|
||||
protected array $where = [];
|
||||
protected array $select = ['*'];
|
||||
protected array $select = [];
|
||||
protected array $join = [];
|
||||
protected array $order = [];
|
||||
protected int $offset = -1;
|
||||
@@ -37,7 +37,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
protected string $from = '';
|
||||
protected string $alias = 't1';
|
||||
protected array $filter = [];
|
||||
protected bool $lock = false;
|
||||
protected bool $lock = FALSE;
|
||||
protected SqlBuilder $builder;
|
||||
protected array $params = [];
|
||||
protected array $_alias = ['t1'];
|
||||
@@ -53,7 +53,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
* Comply constructor.
|
||||
* @throws
|
||||
*/
|
||||
public function __construct($model = null)
|
||||
public function __construct($model = NULL)
|
||||
{
|
||||
if (!is_null($model)) {
|
||||
$this->modelClass = $model;
|
||||
@@ -65,6 +65,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
|
||||
/**
|
||||
* @param array $where
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setWhere(array $where): void
|
||||
@@ -75,6 +76,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
|
||||
/**
|
||||
* @param array $select
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setSelect(array $select): void
|
||||
@@ -85,6 +87,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
|
||||
/**
|
||||
* @param array $join
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setJoin(array $join): void
|
||||
@@ -95,6 +98,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
|
||||
/**
|
||||
* @param array $order
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setOrder(array $order): void
|
||||
@@ -105,6 +109,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
|
||||
/**
|
||||
* @param int $offset
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setOffset(int $offset): void
|
||||
@@ -115,6 +120,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
|
||||
/**
|
||||
* @param int $limit
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setLimit(int $limit): void
|
||||
@@ -125,6 +131,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
|
||||
/**
|
||||
* @param string $group
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setGroup(string $group): void
|
||||
@@ -135,6 +142,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
|
||||
/**
|
||||
* @param string $from
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setFrom(string $from): void
|
||||
@@ -145,6 +153,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
|
||||
/**
|
||||
* @param string $alias
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setAlias(string $alias): void
|
||||
@@ -154,6 +163,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function setParams(array $params): void
|
||||
@@ -255,6 +265,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
/**
|
||||
* @param string $column
|
||||
* @param callable $callable
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function when(string $column, callable $callable): static
|
||||
@@ -271,6 +282,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
|
||||
/**
|
||||
* @param bool $lock
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function lock(bool $lock): static
|
||||
@@ -282,6 +294,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
|
||||
/**
|
||||
* @param string $whereRaw
|
||||
*
|
||||
* @return QueryTrait
|
||||
*/
|
||||
public function whereRaw(string $whereRaw): static
|
||||
@@ -307,6 +320,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
/**
|
||||
* @param string $column
|
||||
* @param string $value
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function whereLocate(string $column, string $value): static
|
||||
@@ -318,6 +332,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
|
||||
/**
|
||||
* @param string $column
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function whereNull(string $column): static
|
||||
@@ -329,6 +344,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
|
||||
/**
|
||||
* @param string $column
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function whereEmpty(string $column): static
|
||||
@@ -340,6 +356,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
|
||||
/**
|
||||
* @param string $column
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function whereNotEmpty(string $column): static
|
||||
@@ -351,6 +368,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
|
||||
/**
|
||||
* @param string $column
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function whereNotNull(string $column): static
|
||||
@@ -383,9 +401,12 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
public function from(string|Closure $tableName): static
|
||||
{
|
||||
if ($tableName instanceof Closure) {
|
||||
$tableName = call_user_func($tableName, $this->queryInstance());
|
||||
}
|
||||
$this->from = call_user_func($tableName, $this->queryInstance());
|
||||
} else if (class_exists($tableName)) {
|
||||
$this->from = (new $tableName)->getTable();
|
||||
} else {
|
||||
$this->from = $tableName;
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
@@ -395,6 +416,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
* @param string $alias
|
||||
* @param array $on
|
||||
* @param array $param
|
||||
*
|
||||
* @return $this
|
||||
* $query->join([$tableName, ['userId'=>'uuvOd']], $param)
|
||||
* $query->join([$tableName, ['userId'=>'uuvOd'], $param])
|
||||
@@ -425,6 +447,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
|
||||
/**
|
||||
* @param array $params
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function bindParams(array $params): void
|
||||
@@ -437,6 +460,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
|
||||
/**
|
||||
* @param array $condition
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function onCondition(array $condition): string
|
||||
@@ -456,16 +480,17 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
|
||||
/**
|
||||
* @param string $value
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function isAliasField(string $value): bool
|
||||
{
|
||||
foreach ($this->_alias as $alias) {
|
||||
if (str_starts_with($value, $alias . '.')) {
|
||||
return true;
|
||||
return TRUE;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
|
||||
@@ -474,6 +499,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
* @param string $alias
|
||||
* @param array $onCondition
|
||||
* @param array $param
|
||||
*
|
||||
* @return $this
|
||||
* @throws Exception
|
||||
*/
|
||||
@@ -495,6 +521,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
* @param string $alias
|
||||
* @param array $onCondition
|
||||
* @param array $param
|
||||
*
|
||||
* @return $this
|
||||
* @throws Exception
|
||||
*/
|
||||
@@ -516,6 +543,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
* @param string $alias
|
||||
* @param array $onCondition
|
||||
* @param array $param
|
||||
*
|
||||
* @return $this
|
||||
* @throws Exception
|
||||
*/
|
||||
@@ -546,6 +574,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
|
||||
/**
|
||||
* @param string $field
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function max(string $field): static
|
||||
@@ -643,10 +672,11 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
* @param array|Closure|string $conditionArray
|
||||
* @param string $opera
|
||||
* @param null $value
|
||||
*
|
||||
* @return QueryTrait
|
||||
* @throws
|
||||
*/
|
||||
public function whereOr(array|Closure|string $conditionArray = [], string $opera = '=', $value = null): static
|
||||
public function whereOr(array|Closure|string $conditionArray = [], string $opera = '=', $value = NULL): static
|
||||
{
|
||||
if ($conditionArray instanceof Closure) {
|
||||
$conditionArray = $this->makeClosureFunction($conditionArray);
|
||||
@@ -666,6 +696,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
/**
|
||||
* @param string $column
|
||||
* @param string $value
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function whereLike(string $column, string $value): static
|
||||
@@ -679,6 +710,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
/**
|
||||
* @param string $column
|
||||
* @param string $value
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function whereLeftLike(string $column, string $value): static
|
||||
@@ -692,6 +724,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
/**
|
||||
* @param string $column
|
||||
* @param string $value
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function whereRightLike(string $column, string $value): static
|
||||
@@ -705,6 +738,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
/**
|
||||
* @param string $column
|
||||
* @param string $value
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function whereNotLike(string $column, string $value): static
|
||||
@@ -718,6 +752,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
/**
|
||||
* @param string $columns
|
||||
* @param array|Closure $value
|
||||
*
|
||||
* @return $this
|
||||
* @throws
|
||||
*/
|
||||
@@ -747,6 +782,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
/**
|
||||
* @param string $columns
|
||||
* @param array $value
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function whereNotIn(string $columns, array $value): static
|
||||
@@ -763,6 +799,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
* @param string $column
|
||||
* @param int|float $start
|
||||
* @param int|float $end
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function whereBetween(string $column, int|float $start, int|float $end): static
|
||||
@@ -783,6 +820,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
* @param string $column
|
||||
* @param int|float $start
|
||||
* @param int|float $end
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function whereNotBetween(string $column, int|float $start, int|float $end): static
|
||||
@@ -801,6 +839,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
|
||||
/**
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function pushParam(mixed $value): static
|
||||
@@ -813,9 +852,10 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
/**
|
||||
* @param array|string $column
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function where(array|string $column, mixed $value = null): static
|
||||
public function where(array|string $column, mixed $value = NULL): static
|
||||
{
|
||||
if (func_num_args() === 2) {
|
||||
return $this->addArray([$column => $value]);
|
||||
@@ -832,6 +872,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
* @param string $column
|
||||
* @param string $opera
|
||||
* @param mixed $value
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function whereMath(string $column, string $opera, mixed $value): static
|
||||
@@ -844,6 +885,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
|
||||
/**
|
||||
* @param Closure $closure
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function whereClosure(Closure $closure): static
|
||||
@@ -855,6 +897,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
|
||||
/**
|
||||
* @param Closure|array $closure
|
||||
*
|
||||
* @return string
|
||||
* @throws
|
||||
*/
|
||||
@@ -901,6 +944,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
|
||||
/**
|
||||
* @param int $offset
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function offset(int $offset): static
|
||||
@@ -929,12 +973,13 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
|
||||
/**
|
||||
* @param array $array
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
private function addArray(array $array): static
|
||||
{
|
||||
foreach ($array as $key => $value) {
|
||||
if ($value === null) {
|
||||
if ($value === NULL) {
|
||||
continue;
|
||||
}
|
||||
$this->where[] = is_numeric($key) ? $value : $this->sprintf($key, $value);
|
||||
@@ -947,6 +992,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
* @param string $column
|
||||
* @param mixed $value
|
||||
* @param string $opera
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
private function sprintf(string $column, mixed $value, string $opera = '='): string
|
||||
@@ -959,6 +1005,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
||||
/**
|
||||
* @param string $querySql
|
||||
* @param array $params
|
||||
*
|
||||
* @return Command
|
||||
*/
|
||||
public function buildCommand(string $querySql, array $params = []): Command
|
||||
|
||||
@@ -1,13 +1,23 @@
|
||||
<?php
|
||||
|
||||
|
||||
use Database\Query;
|
||||
|
||||
class Users extends \Database\Model
|
||||
{
|
||||
|
||||
|
||||
|
||||
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