This commit is contained in:
2026-06-12 23:57:19 +08:00
parent c7b980d969
commit 8a985a65df
5 changed files with 204 additions and 323 deletions
+113 -253
View File
@@ -27,26 +27,93 @@ use Kiri\Abstracts\Component;
*/
abstract class QueryTrait extends Component implements ActiveQueryInterface, ISqlBuilder
{
protected array $where = [];
protected array $select = ['t1.*'];
protected array $join = [];
protected array $order = [];
protected int $offset = -1;
protected int $limit = -1;
protected string $group = '';
protected string $from = '';
protected string $alias = '';
protected array $filter = [];
public array $where = [] {
&get {
return $this->where;
}
}
public array $select = ['t1.*'] {
&get {
return $this->select;
}
}
public array $join = [] {
&get {
return $this->join;
}
}
public array $order = [] {
&get {
return $this->order;
}
}
public int $offset = -1 {
get {
return $this->offset;
}
set {
$this->offset = $value;
}
}
public int $limit = -1 {
get {
return $this->limit;
}
set {
$this->limit = $value;
}
}
public string $group = '' {
get {
return $this->group;
}
set {
$this->group = $value;
}
}
public string $from = '' {
get {
return $this->from;
}
set {
$this->from = $value;
}
}
public string $alias = '' {
get {
return $this->alias;
}
set {
$this->alias = $value;
}
}
protected array $filter = [] {
&get {
return $this->filter;
}
}
protected bool $lock = FALSE;
protected SqlBuilder $builder;
protected array $params = [];
protected array $_alias = [];
public array $params = [] {
&get {
return $this->params;
}
}
protected array $_alias = [] {
&get {
return $this->_alias;
}
}
/**
* @var ModelInterface|string|null
*/
protected ModelInterface|string|null $modelClass;
protected ModelInterface|string|null $modelClass {
get {
return $this->modelClass;
}
}
/**
@@ -63,29 +130,9 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
}
/**
* @return mixed
*/
public function getModelClass(): mixed
{
return $this->modelClass;
}
/**
* @param array $where
*
* @return void
*/
public function setWhere(array $where): void
{
$this->where = $where;
}
/**
* @param Closure|Query $callback
* @param array $attributes
* @param array $attributes
*
* @return $this
*/
@@ -93,7 +140,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
{
if ($callback instanceof Query) {
$this->where[] = 'NOT EXISTS(' . $callback->build() . ')';
$this->mergeParams($callback->getParams());
$this->mergeParams($callback->params);
} else {
$this->where[] = 'NOT EXISTS(' . $this->makeClosureFunction($callback) . ')';
}
@@ -106,7 +153,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
/**
* @param Closure|Query $callback
* @param array $attributes
* @param array $attributes
*
* @return $this
*/
@@ -114,7 +161,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
{
if ($callback instanceof Query) {
$this->where[] = 'EXISTS(' . $callback->build() . ')';
$this->mergeParams($callback->getParams());
$this->mergeParams($callback->params);
} else {
$this->where[] = 'EXISTS(' . $this->makeClosureFunction($callback) . ')';
}
@@ -125,194 +172,6 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
}
/**
* @param array $select
*
* @return void
*/
public function setSelect(array $select): void
{
$this->select = $select;
}
/**
* @param array $join
*
* @return void
*/
public function setJoin(array $join): void
{
$this->join = $join;
}
/**
* @param array $order
*
* @return void
*/
public function setOrder(array $order): void
{
$this->order = $order;
}
/**
* @param int $offset
*
* @return void
*/
public function setOffset(int $offset): void
{
$this->offset = $offset;
}
/**
* @param int $limit
*
* @return void
*/
public function setLimit(int $limit): void
{
$this->limit = $limit;
}
/**
* @param string $group
*
* @return void
*/
public function setGroup(string $group): void
{
$this->group = $group;
}
/**
* @param string $from
*
* @return void
*/
public function setFrom(string $from): void
{
$this->from = $from;
}
/**
* @param string $alias
*
* @return void
*/
public function setAlias(string $alias): void
{
$this->alias = $alias;
}
/**
* @param array $params
*
* @return void
*/
public function setParams(array $params): void
{
$this->params = $params;
}
/**
* @return array
*/
public function getWhere(): array
{
return $this->where;
}
/**
* @return array|string[]
*/
public function getSelect(): array
{
return $this->select;
}
/**
* @return array
*/
public function getJoin(): array
{
return $this->join;
}
/**
* @return array
*/
public function getOrder(): array
{
return $this->order;
}
/**
* @return int
*/
public function getOffset(): int
{
return $this->offset;
}
/**
* @return int
*/
public function getLimit(): int
{
return $this->limit;
}
/**
* @return string
*/
public function getGroup(): string
{
return $this->group;
}
/**
* @return string
*/
public function getFrom(): string
{
return $this->from;
}
/**
* @return string
*/
public function getAlias(): string
{
return $this->alias;
}
/**
* @return array
*/
public function getParams(): array
{
return $this->params;
}
/**
* @param array $params
* @return void
@@ -326,7 +185,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
/**
* @param string $column
* @param string $column
* @param callable $callable
*
* @return $this
@@ -465,7 +324,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
{
if ($tableName instanceof Query) {
$this->from = '(' . $tableName->build() . ')';
$this->mergeParams($tableName->getParams());
$this->mergeParams($tableName->params);
} else if ($tableName instanceof Closure) {
$this->from = '(' . $this->makeClosureFunction($tableName) . ')';
} else if (class_exists($tableName)) {
@@ -480,8 +339,8 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
/**
* @param string $tableName
* @param string $alias
* @param array $on
* @param array $param
* @param array $on
* @param array $param
*
* @return $this
* $query->join([$tableName, ['userId'=>'uuvOd']], $param)
@@ -563,8 +422,8 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
/**
* @param string $tableName
* @param string $alias
* @param array $onCondition
* @param array $param
* @param array $onCondition
* @param array $param
*
* @return $this
* @throws Exception
@@ -585,8 +444,8 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
/**
* @param string $tableName
* @param string $alias
* @param array $onCondition
* @param array $param
* @param array $onCondition
* @param array $param
*
* @return $this
* @throws Exception
@@ -607,8 +466,8 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
/**
* @param string $tableName
* @param string $alias
* @param array $onCondition
* @param array $param
* @param array $onCondition
* @param array $param
*
* @return $this
* @throws Exception
@@ -651,8 +510,8 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
/**
* @param string $lngField
* @param string $latField
* @param string $lngField
* @param string $latField
* @param int|float $lng1
* @param int|float $lat1
*
@@ -668,7 +527,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
/**
* @param string|array $column
* @param string $sort
* @param string $sort
*
* @return $this
*
@@ -736,8 +595,8 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
/**
* @param array|Closure|string $conditionArray
* @param string $opera
* @param null $value
* @param string $opera
* @param null $value
*
* @return QueryTrait
* @throws
@@ -816,7 +675,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
/**
* @param string $columns
* @param string $columns
* @param array|Closure|Query $value
*
* @return $this
@@ -825,12 +684,13 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
public function whereIn(string $columns, array|Closure|Query $value): static
{
if (is_array($value)) {
if (count($value) < 1) throw new Exception('Value must be an not empty array');
if (count($value) < 1)
throw new Exception('Value must be an not empty array');
$this->where[] = $columns . ' IN (' . implode(',', $value) . ')';
} else if ($value instanceof Query) {
$this->where[] = $columns . ' IN (' . $value->build() . ')';
$this->mergeParams($value->getParams());
$this->mergeParams($value->params);
} else {
$this->where[] = $columns . ' IN (' . $this->makeClosureFunction($value) . ')';
}
@@ -844,13 +704,13 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
*/
public function queryInstance(): Query
{
return new Query();
return new Query;
}
/**
* @param string $columns
* @param array $value
* @param array $value
*
* @return $this
*/
@@ -865,7 +725,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
/**
* @param string $column
* @param string $column
* @param int|float $start
* @param int|float $end
*
@@ -886,7 +746,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
/**
* @param string $column
* @param string $column
* @param int|float $start
* @param int|float $end
*
@@ -920,7 +780,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
/**
* @param array|string $column
* @param mixed $value
* @param mixed $value
*
* @return $this
*/
@@ -940,7 +800,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
/**
* @param string $column
* @param string $opera
* @param mixed $value
* @param mixed $value
*
* @return $this
*/
@@ -978,13 +838,13 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
} else {
$generate->addArray($closure);
}
$this->mergeParams($generate->getParams());
$this->mergeParams($generate->params);
return $generate->build();
}
/**
* @param string $name
* @param string $name
* @param string|null $having
*
* @return $this
@@ -1060,7 +920,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
/**
* @param string $column
* @param mixed $value
* @param mixed $value
* @param string $opera
*
* @return string
@@ -1084,7 +944,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
/**
* @param string $querySql
* @param array $params
* @param array $params
*
* @return Command
*/
+65 -46
View File
@@ -18,61 +18,80 @@ use JetBrains\PhpStorm\Pure;
class When
{
public ActiveQuery|ISqlBuilder $query;
public ActiveQuery|ISqlBuilder $query;
private array $_condition = [];
private string $else = '';
/**
* @var array
*/
private array $_condition = [] {
&get {
return $this->_condition;
}
}
/**
* CaseWhen constructor.
* @param string $column
* @param ActiveQuery|ISqlBuilder $activeQuery
*/
public function __construct(public string $column, public ActiveQuery|ISqlBuilder $activeQuery)
{
$this->_condition[] = 'CASE ' . $column;
}
/**
* @var string
*/
private string $else = '' {
get {
return $this->else;
}
set(string $value) {
$this->else = $value;
}
}
/**
* @param string|int $condition
* @param string $then
* @return $this
* @throws
*/
public function when(string|int $condition, string $then): static
{
$this->_condition[] = sprintf('WHEN %s THEN %s', $condition, $then);
return $this;
}
/**
* CaseWhen constructor.
* @param string $column
* @param ActiveQuery|ISqlBuilder $activeQuery
*/
public function __construct(public string $column, public ActiveQuery|ISqlBuilder $activeQuery)
{
$this->_condition[] = 'CASE ' . $column;
}
/**
* @param string $alias
*/
public function else(string $alias): void
{
$this->else = $alias;
}
/**
* @param string|int $condition
* @param string $then
* @return $this
* @throws
*/
public function when(string|int $condition, string $then): static
{
$this->_condition[] = sprintf('WHEN %s THEN %s', $condition, $then);
return $this;
}
/**
* @return string
*/
#[Pure] public function end(): string
{
if (empty($this->_condition)) {
return '';
}
$prefix = implode(' ', $this->_condition);
if (!empty($this->else)) {
$prefix .= ' ELSE ' . $this->else;
}
return '(' . $prefix . ' END)';
}
/**
* @param string $alias
*/
public function else(string $alias): void
{
$this->else = $alias;
}
/**
* @return string
*/
#[Pure]
public function end(): string
{
if (empty($this->_condition)) {
return '';
}
$prefix = implode(' ', $this->_condition);
if (!empty($this->else)) {
$prefix .= ' ELSE ' . $this->else;
}
return '(' . $prefix . ' END)';
}
}