改名
This commit is contained in:
@@ -0,0 +1,79 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
|
||||||
|
namespace Database\Traits;
|
||||||
|
|
||||||
|
|
||||||
|
use Database\ActiveQuery;
|
||||||
|
use Database\Sql;
|
||||||
|
use Exception;
|
||||||
|
use JetBrains\PhpStorm\Pure;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class CaseWhen
|
||||||
|
* @package Database\Traits
|
||||||
|
*/
|
||||||
|
class CaseWhen
|
||||||
|
{
|
||||||
|
|
||||||
|
public ActiveQuery|QueryTrait $query;
|
||||||
|
|
||||||
|
|
||||||
|
private array $_condition = [];
|
||||||
|
|
||||||
|
private string $else = '';
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* CaseWhen constructor.
|
||||||
|
* @param string $column
|
||||||
|
* @param ActiveQuery|QueryTrait $activeQuery
|
||||||
|
*/
|
||||||
|
public function __construct(public string $column, public ActiveQuery|QueryTrait $activeQuery)
|
||||||
|
{
|
||||||
|
$this->_condition[] = 'CASE ' . $column;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array|string $condition
|
||||||
|
* @param string $then
|
||||||
|
* @return $this
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public function when(array|string $condition, string $then): static
|
||||||
|
{
|
||||||
|
$this->_condition[] = sprintf('WHEN %s THEN %s', $this->activeQuery->makeNewSqlGenerate()
|
||||||
|
->where($condition)
|
||||||
|
->getCondition(), $then);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $alias
|
||||||
|
*/
|
||||||
|
public function else(string $alias)
|
||||||
|
{
|
||||||
|
$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';
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -15,6 +15,10 @@ use Database\ActiveRecord;
|
|||||||
use Database\Condition\MathematicsCondition;
|
use Database\Condition\MathematicsCondition;
|
||||||
use Database\Sql;
|
use Database\Sql;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use JetBrains\PhpStorm\Pure;
|
||||||
|
use ReflectionException;
|
||||||
|
use Snowflake\Exception\NotFindClassException;
|
||||||
|
use Snowflake\Snowflake;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Trait QueryTrait
|
* Trait QueryTrait
|
||||||
@@ -58,6 +62,17 @@ trait QueryTrait
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $column
|
||||||
|
* @param callable $callable
|
||||||
|
* @return CaseWhen
|
||||||
|
*/
|
||||||
|
public function case(string $column, callable $callable): CaseWhen
|
||||||
|
{
|
||||||
|
return call_user_func($callable, new CaseWhen($column, $this));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $condition
|
* @param $condition
|
||||||
* @param $condition1
|
* @param $condition1
|
||||||
@@ -678,6 +693,17 @@ trait QueryTrait
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Sql
|
||||||
|
* @throws ReflectionException
|
||||||
|
* @throws NotFindClassException
|
||||||
|
*/
|
||||||
|
public function makeNewSqlGenerate(): Sql
|
||||||
|
{
|
||||||
|
return Snowflake::createObject(Sql::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $columns
|
* @param $columns
|
||||||
* @param $value
|
* @param $value
|
||||||
|
|||||||
Reference in New Issue
Block a user