eee
This commit is contained in:
@@ -285,6 +285,7 @@ class Model extends Base\Model
|
|||||||
|
|
||||||
$primaryKey = str_replace('\\', '_', $modelName) . '_' . $foreignKey . '_' . $value;
|
$primaryKey = str_replace('\\', '_', $modelName) . '_' . $foreignKey . '_' . $value;
|
||||||
if (!$relation->hasIdentification($primaryKey)) {
|
if (!$relation->hasIdentification($primaryKey)) {
|
||||||
|
/** @var $modelName ModelInterface */
|
||||||
$relation->bindIdentification($primaryKey, $modelName::query()->where([$foreignKey => $value]));
|
$relation->bindIdentification($primaryKey, $modelName::query()->where([$foreignKey => $value]));
|
||||||
}
|
}
|
||||||
return $primaryKey;
|
return $primaryKey;
|
||||||
@@ -346,6 +347,7 @@ class Model extends Base\Model
|
|||||||
|
|
||||||
$primaryKey = str_replace('\\', '_', $modelName) . '_' . $foreignKey . '_' . implode('_', $value);
|
$primaryKey = str_replace('\\', '_', $modelName) . '_' . $foreignKey . '_' . implode('_', $value);
|
||||||
if (!$relation->hasIdentification($primaryKey)) {
|
if (!$relation->hasIdentification($primaryKey)) {
|
||||||
|
/** @var $modelName ModelInterface */
|
||||||
$relation->bindIdentification($primaryKey, $modelName::query()->whereIn($foreignKey, $value));
|
$relation->bindIdentification($primaryKey, $modelName::query()->whereIn($foreignKey, $value));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+23
-23
@@ -52,7 +52,7 @@ class SqlBuilder extends Component
|
|||||||
*/
|
*/
|
||||||
public function getCondition(): string
|
public function getCondition(): string
|
||||||
{
|
{
|
||||||
return $this->where($this->query->getWhere());
|
return $this->where($this->query);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -74,9 +74,9 @@ class SqlBuilder extends Component
|
|||||||
*/
|
*/
|
||||||
public function update(array $attributes): bool|string
|
public function update(array $attributes): bool|string
|
||||||
{
|
{
|
||||||
$conditions = $this->query->getParams();
|
$conditions = $this->query;
|
||||||
$this->query->setParams([]);
|
$this->query = [];
|
||||||
$data = $this->__updateBuilder($this->makeParams($attributes));
|
$data = $this->__updateBuilder($this->makeParams($attributes));
|
||||||
foreach ($conditions as $condition) {
|
foreach ($conditions as $condition) {
|
||||||
$this->query->pushParam($condition);
|
$this->query->pushParam($condition);
|
||||||
}
|
}
|
||||||
@@ -110,7 +110,7 @@ class SqlBuilder extends Component
|
|||||||
if (empty($string)) {
|
if (empty($string)) {
|
||||||
return Kiri::getLogger()->logCategory('None data update.');
|
return Kiri::getLogger()->logCategory('None data update.');
|
||||||
}
|
}
|
||||||
return 'UPDATE ' . $this->query->getFrom() . ' SET ' . implode(',', $string) . $this->make();
|
return 'UPDATE ' . $this->query . ' SET ' . implode(',', $string) . $this->make();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -122,7 +122,7 @@ class SqlBuilder extends Component
|
|||||||
*/
|
*/
|
||||||
public function insert(array $attributes, bool $isBatch = false): string
|
public function insert(array $attributes, bool $isBatch = false): string
|
||||||
{
|
{
|
||||||
$update = 'INSERT INTO ' . $this->query->getFrom();
|
$update = 'INSERT INTO ' . $this->query;
|
||||||
if ($isBatch === false) {
|
if ($isBatch === false) {
|
||||||
$attributes = [$attributes];
|
$attributes = [$attributes];
|
||||||
}
|
}
|
||||||
@@ -144,7 +144,7 @@ class SqlBuilder extends Component
|
|||||||
*/
|
*/
|
||||||
public function delete(): string
|
public function delete(): string
|
||||||
{
|
{
|
||||||
return 'DELETE FROM ' . $this->query->getFrom() . $this->make();
|
return 'DELETE FROM ' . $this->query . $this->make();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -207,7 +207,7 @@ class SqlBuilder extends Component
|
|||||||
public function one(): string
|
public function one(): string
|
||||||
{
|
{
|
||||||
$this->query->offset(0)->limit(1);
|
$this->query->offset(0)->limit(1);
|
||||||
return $this->makeSelect($this->query->getSelect()) . $this->make() . $this->makeLimit();
|
return $this->makeSelect($this->query) . $this->make() . $this->makeLimit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -217,7 +217,7 @@ class SqlBuilder extends Component
|
|||||||
*/
|
*/
|
||||||
public function all(): string
|
public function all(): string
|
||||||
{
|
{
|
||||||
return $this->makeSelect($this->query->getSelect()) . $this->make() . $this->makeLimit();
|
return $this->makeSelect($this->query) . $this->make() . $this->makeLimit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -286,12 +286,12 @@ class SqlBuilder extends Component
|
|||||||
*/
|
*/
|
||||||
private function makeSelect(array $select = ['*']): string
|
private function makeSelect(array $select = ['*']): string
|
||||||
{
|
{
|
||||||
$select = "SELECT " . implode(',', $select) . " FROM " . $this->query->getFrom();
|
$select = "SELECT " . implode(',', $select) . " FROM " . $this->query;
|
||||||
if ($this->query->getAlias() != "") {
|
if ($this->query != "") {
|
||||||
$select .= " AS " . $this->query->getAlias();
|
$select .= " AS " . $this->query;
|
||||||
}
|
}
|
||||||
if (count($this->query->getJoin()) > 0) {
|
if (count($this->query) > 0) {
|
||||||
$select .= ' ' . implode(' ', $this->query->getJoin());
|
$select .= ' ' . implode(' ', $this->query);
|
||||||
}
|
}
|
||||||
return $select;
|
return $select;
|
||||||
}
|
}
|
||||||
@@ -302,8 +302,8 @@ class SqlBuilder extends Component
|
|||||||
*/
|
*/
|
||||||
private function makeGroup(): string
|
private function makeGroup(): string
|
||||||
{
|
{
|
||||||
if ($this->query->getGroup() != "") {
|
if ($this->query != "") {
|
||||||
return ' GROUP BY ' . $this->query->getGroup();
|
return ' GROUP BY ' . $this->query;
|
||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
@@ -314,8 +314,8 @@ class SqlBuilder extends Component
|
|||||||
*/
|
*/
|
||||||
private function makeOrder(): string
|
private function makeOrder(): string
|
||||||
{
|
{
|
||||||
if (count($this->query->getOrder()) > 0) {
|
if (count($this->query) > 0) {
|
||||||
return ' ORDER BY ' . implode(',', $this->query->getOrder());
|
return ' ORDER BY ' . implode(',', $this->query);
|
||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
@@ -326,7 +326,7 @@ class SqlBuilder extends Component
|
|||||||
*/
|
*/
|
||||||
private function makeCondition(): string
|
private function makeCondition(): string
|
||||||
{
|
{
|
||||||
$condition = $this->where($this->query->getWhere());
|
$condition = $this->where($this->query);
|
||||||
if (empty($condition)) {
|
if (empty($condition)) {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
@@ -340,14 +340,14 @@ class SqlBuilder extends Component
|
|||||||
*/
|
*/
|
||||||
private function makeLimit(): string
|
private function makeLimit(): string
|
||||||
{
|
{
|
||||||
if ($this->query->getOffset() >= 0 && $this->query->getLimit() >= 1) {
|
if ($this->query >= 0 && $this->query >= 1) {
|
||||||
$driver = $this->getDriver();
|
$driver = $this->getDriver();
|
||||||
if (in_array($driver, ['pgsql', 'postgresql'])) {
|
if (in_array($driver, ['pgsql', 'postgresql'])) {
|
||||||
// PostgreSQL 使用 LIMIT ... OFFSET ... 语法
|
// PostgreSQL 使用 LIMIT ... OFFSET ... 语法
|
||||||
return ' LIMIT ' . $this->query->getLimit() . ' OFFSET ' . $this->query->getOffset();
|
return ' LIMIT ' . $this->query . ' OFFSET ' . $this->query;
|
||||||
}
|
}
|
||||||
// MySQL 使用 LIMIT offset,limit 语法
|
// MySQL 使用 LIMIT offset,limit 语法
|
||||||
return ' LIMIT ' . $this->query->getOffset() . ',' . $this->query->getLimit();
|
return ' LIMIT ' . $this->query . ',' . $this->query;
|
||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
@@ -373,7 +373,7 @@ class SqlBuilder extends Component
|
|||||||
*/
|
*/
|
||||||
public function truncate(): string
|
public function truncate(): string
|
||||||
{
|
{
|
||||||
return sprintf('TRUNCATE %s', $this->query->getFrom());
|
return sprintf('TRUNCATE %s', $this->query);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+113
-253
@@ -27,26 +27,93 @@ use Kiri\Abstracts\Component;
|
|||||||
*/
|
*/
|
||||||
abstract class QueryTrait extends Component implements ActiveQueryInterface, ISqlBuilder
|
abstract class QueryTrait extends Component implements ActiveQueryInterface, ISqlBuilder
|
||||||
{
|
{
|
||||||
protected array $where = [];
|
public array $where = [] {
|
||||||
protected array $select = ['t1.*'];
|
&get {
|
||||||
protected array $join = [];
|
return $this->where;
|
||||||
protected array $order = [];
|
}
|
||||||
protected int $offset = -1;
|
}
|
||||||
protected int $limit = -1;
|
public array $select = ['t1.*'] {
|
||||||
protected string $group = '';
|
&get {
|
||||||
protected string $from = '';
|
return $this->select;
|
||||||
protected string $alias = '';
|
}
|
||||||
protected array $filter = [];
|
}
|
||||||
|
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 bool $lock = FALSE;
|
||||||
protected SqlBuilder $builder;
|
protected SqlBuilder $builder;
|
||||||
protected array $params = [];
|
public array $params = [] {
|
||||||
protected array $_alias = [];
|
&get {
|
||||||
|
return $this->params;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
protected array $_alias = [] {
|
||||||
|
&get {
|
||||||
|
return $this->_alias;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var ModelInterface|string|null
|
* @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 Closure|Query $callback
|
||||||
* @param array $attributes
|
* @param array $attributes
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
@@ -93,7 +140,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
{
|
{
|
||||||
if ($callback instanceof Query) {
|
if ($callback instanceof Query) {
|
||||||
$this->where[] = 'NOT EXISTS(' . $callback->build() . ')';
|
$this->where[] = 'NOT EXISTS(' . $callback->build() . ')';
|
||||||
$this->mergeParams($callback->getParams());
|
$this->mergeParams($callback->params);
|
||||||
} else {
|
} else {
|
||||||
$this->where[] = 'NOT EXISTS(' . $this->makeClosureFunction($callback) . ')';
|
$this->where[] = 'NOT EXISTS(' . $this->makeClosureFunction($callback) . ')';
|
||||||
}
|
}
|
||||||
@@ -106,7 +153,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Closure|Query $callback
|
* @param Closure|Query $callback
|
||||||
* @param array $attributes
|
* @param array $attributes
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
@@ -114,7 +161,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
{
|
{
|
||||||
if ($callback instanceof Query) {
|
if ($callback instanceof Query) {
|
||||||
$this->where[] = 'EXISTS(' . $callback->build() . ')';
|
$this->where[] = 'EXISTS(' . $callback->build() . ')';
|
||||||
$this->mergeParams($callback->getParams());
|
$this->mergeParams($callback->params);
|
||||||
} else {
|
} else {
|
||||||
$this->where[] = 'EXISTS(' . $this->makeClosureFunction($callback) . ')';
|
$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
|
* @param array $params
|
||||||
* @return void
|
* @return void
|
||||||
@@ -326,7 +185,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
|
||||||
@@ -465,7 +324,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
{
|
{
|
||||||
if ($tableName instanceof Query) {
|
if ($tableName instanceof Query) {
|
||||||
$this->from = '(' . $tableName->build() . ')';
|
$this->from = '(' . $tableName->build() . ')';
|
||||||
$this->mergeParams($tableName->getParams());
|
$this->mergeParams($tableName->params);
|
||||||
} else if ($tableName instanceof Closure) {
|
} else if ($tableName instanceof Closure) {
|
||||||
$this->from = '(' . $this->makeClosureFunction($tableName) . ')';
|
$this->from = '(' . $this->makeClosureFunction($tableName) . ')';
|
||||||
} else if (class_exists($tableName)) {
|
} else if (class_exists($tableName)) {
|
||||||
@@ -480,8 +339,8 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
/**
|
/**
|
||||||
* @param string $tableName
|
* @param string $tableName
|
||||||
* @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)
|
||||||
@@ -563,8 +422,8 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
/**
|
/**
|
||||||
* @param string $tableName
|
* @param string $tableName
|
||||||
* @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
|
||||||
@@ -585,8 +444,8 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
/**
|
/**
|
||||||
* @param string $tableName
|
* @param string $tableName
|
||||||
* @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
|
||||||
@@ -607,8 +466,8 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
/**
|
/**
|
||||||
* @param string $tableName
|
* @param string $tableName
|
||||||
* @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
|
||||||
@@ -651,8 +510,8 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $lngField
|
* @param string $lngField
|
||||||
* @param string $latField
|
* @param string $latField
|
||||||
* @param int|float $lng1
|
* @param int|float $lng1
|
||||||
* @param int|float $lat1
|
* @param int|float $lat1
|
||||||
*
|
*
|
||||||
@@ -668,7 +527,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string|array $column
|
* @param string|array $column
|
||||||
* @param string $sort
|
* @param string $sort
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
*
|
*
|
||||||
@@ -736,8 +595,8 @@ 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
|
||||||
@@ -816,7 +675,7 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $columns
|
* @param string $columns
|
||||||
* @param array|Closure|Query $value
|
* @param array|Closure|Query $value
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
@@ -825,12 +684,13 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
public function whereIn(string $columns, array|Closure|Query $value): static
|
public function whereIn(string $columns, array|Closure|Query $value): static
|
||||||
{
|
{
|
||||||
if (is_array($value)) {
|
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) . ')';
|
$this->where[] = $columns . ' IN (' . implode(',', $value) . ')';
|
||||||
} else if ($value instanceof Query) {
|
} else if ($value instanceof Query) {
|
||||||
$this->where[] = $columns . ' IN (' . $value->build() . ')';
|
$this->where[] = $columns . ' IN (' . $value->build() . ')';
|
||||||
$this->mergeParams($value->getParams());
|
$this->mergeParams($value->params);
|
||||||
} else {
|
} else {
|
||||||
$this->where[] = $columns . ' IN (' . $this->makeClosureFunction($value) . ')';
|
$this->where[] = $columns . ' IN (' . $this->makeClosureFunction($value) . ')';
|
||||||
}
|
}
|
||||||
@@ -844,13 +704,13 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
*/
|
*/
|
||||||
public function queryInstance(): Query
|
public function queryInstance(): Query
|
||||||
{
|
{
|
||||||
return new Query();
|
return new Query;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $columns
|
* @param string $columns
|
||||||
* @param array $value
|
* @param array $value
|
||||||
*
|
*
|
||||||
* @return $this
|
* @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 $start
|
||||||
* @param int|float $end
|
* @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 $start
|
||||||
* @param int|float $end
|
* @param int|float $end
|
||||||
*
|
*
|
||||||
@@ -920,7 +780,7 @@ 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
|
||||||
*/
|
*/
|
||||||
@@ -940,7 +800,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
|
||||||
*/
|
*/
|
||||||
@@ -978,13 +838,13 @@ abstract class QueryTrait extends Component implements ActiveQueryInterface, ISq
|
|||||||
} else {
|
} else {
|
||||||
$generate->addArray($closure);
|
$generate->addArray($closure);
|
||||||
}
|
}
|
||||||
$this->mergeParams($generate->getParams());
|
$this->mergeParams($generate->params);
|
||||||
return $generate->build();
|
return $generate->build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $name
|
* @param string $name
|
||||||
* @param string|null $having
|
* @param string|null $having
|
||||||
*
|
*
|
||||||
* @return $this
|
* @return $this
|
||||||
@@ -1060,7 +920,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
|
||||||
@@ -1084,7 +944,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
|
||||||
*/
|
*/
|
||||||
|
|||||||
+65
-46
@@ -18,61 +18,80 @@ use JetBrains\PhpStorm\Pure;
|
|||||||
class When
|
class When
|
||||||
{
|
{
|
||||||
|
|
||||||
public ActiveQuery|ISqlBuilder $query;
|
public ActiveQuery|ISqlBuilder $query;
|
||||||
|
|
||||||
|
|
||||||
private array $_condition = [];
|
/**
|
||||||
|
* @var array
|
||||||
private string $else = '';
|
*/
|
||||||
|
private array $_condition = [] {
|
||||||
|
&get {
|
||||||
|
return $this->_condition;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* CaseWhen constructor.
|
* @var string
|
||||||
* @param string $column
|
*/
|
||||||
* @param ActiveQuery|ISqlBuilder $activeQuery
|
private string $else = '' {
|
||||||
*/
|
get {
|
||||||
public function __construct(public string $column, public ActiveQuery|ISqlBuilder $activeQuery)
|
return $this->else;
|
||||||
{
|
}
|
||||||
$this->_condition[] = 'CASE ' . $column;
|
set(string $value) {
|
||||||
}
|
$this->else = $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string|int $condition
|
* CaseWhen constructor.
|
||||||
* @param string $then
|
* @param string $column
|
||||||
* @return $this
|
* @param ActiveQuery|ISqlBuilder $activeQuery
|
||||||
* @throws
|
*/
|
||||||
*/
|
public function __construct(public string $column, public ActiveQuery|ISqlBuilder $activeQuery)
|
||||||
public function when(string|int $condition, string $then): static
|
{
|
||||||
{
|
$this->_condition[] = 'CASE ' . $column;
|
||||||
$this->_condition[] = sprintf('WHEN %s THEN %s', $condition, $then);
|
}
|
||||||
|
|
||||||
return $this;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $alias
|
* @param string|int $condition
|
||||||
*/
|
* @param string $then
|
||||||
public function else(string $alias): void
|
* @return $this
|
||||||
{
|
* @throws
|
||||||
$this->else = $alias;
|
*/
|
||||||
}
|
public function when(string|int $condition, string $then): static
|
||||||
|
{
|
||||||
|
$this->_condition[] = sprintf('WHEN %s THEN %s', $condition, $then);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @param string $alias
|
||||||
*/
|
*/
|
||||||
#[Pure] public function end(): string
|
public function else(string $alias): void
|
||||||
{
|
{
|
||||||
if (empty($this->_condition)) {
|
$this->else = $alias;
|
||||||
return '';
|
}
|
||||||
}
|
|
||||||
$prefix = implode(' ', $this->_condition);
|
|
||||||
if (!empty($this->else)) {
|
/**
|
||||||
$prefix .= ' ELSE ' . $this->else;
|
* @return string
|
||||||
}
|
*/
|
||||||
return '(' . $prefix . ' END)';
|
#[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)';
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -9,7 +9,7 @@
|
|||||||
],
|
],
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"require": {
|
"require": {
|
||||||
"php": ">=8.4",
|
"php": ">=8.5",
|
||||||
"ext-json": "*",
|
"ext-json": "*",
|
||||||
"ext-pdo": "*",
|
"ext-pdo": "*",
|
||||||
"game-worker/kiri-pool": "^v1.0"
|
"game-worker/kiri-pool": "^v1.0"
|
||||||
|
|||||||
Reference in New Issue
Block a user