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