This commit is contained in:
2023-12-12 15:35:35 +08:00
parent 510a13e3da
commit e2c0f62532
32 changed files with 922 additions and 1095 deletions
+158 -159
View File
@@ -25,177 +25,176 @@ trait Builder
{
/**
* @param $alias
* @return string
*/
private function builderAlias($alias): string
{
return " AS " . $alias;
}
/**
* @param $table
* @return string
* @throws Exception
*/
private function builderFrom($table): string
{
if ($table instanceof ActiveQuery) {
$table = '(' . $table->toSql() . ')';
}
return " FROM " . $table;
}
/**
* @param $join
* @return string
*/
#[Pure] private function builderJoin($join): string
{
if (!empty($join)) {
return ' ' . implode(' ', $join);
}
return '';
}
/**
* @param string $select
* @return string
*/
#[Pure] private function builderSelect(string $select = "*"): string
{
return "SELECT " . $select;
}
/**
* @param string $group
* @return string
*/
private function builderGroup(string $group): string
{
if ($group != '') {
return '';
}
return ' GROUP BY ' . $group;
}
/**
* @param $order
* @return string
*/
#[Pure] private function builderOrder($order): string
{
if (!empty($order)) {
return ' ORDER BY ' . implode(',', $order);
} else {
return '';
}
}
/**
* @param ActiveQuery|Query $query
* @return string
*/
#[Pure] private function builderLimit(ActiveQuery|Query $query): string
{
if ($query->limit > 0) {
return ' LIMIT ' . $query->offset . ',' . $query->limit;
} else {
return '';
}
}
/**
* @param array $where
* @return string
/**
* @param $alias
* @return string
*/
private function where(array $where): string
{
if (count($where) < 1) {
return '';
}
private function builderAlias($alias): string
{
return " AS " . $alias;
}
/**
* @param $table
* @return string
* @throws
*/
private function builderFrom($table): string
{
if ($table instanceof ActiveQuery) {
$table = '(' . $table->toSql() . ')';
}
return " FROM " . $table;
}
/**
* @param $join
* @return string
*/
#[Pure] private function builderJoin($join): string
{
if (!empty($join)) {
return ' ' . implode(' ', $join);
}
return '';
}
/**
* @param string $select
* @return string
*/
#[Pure] private function builderSelect(string $select = "*"): string
{
return "SELECT " . $select;
}
/**
* @param string $group
* @return string
*/
private function builderGroup(string $group): string
{
if ($group != '') {
return '';
}
return ' GROUP BY ' . $group;
}
/**
* @param $order
* @return string
*/
#[Pure] private function builderOrder($order): string
{
if (!empty($order)) {
return ' ORDER BY ' . implode(',', $order);
} else {
return '';
}
}
/**
* @param ActiveQuery|Query $query
* @return string
*/
#[Pure] private function builderLimit(ActiveQuery|Query $query): string
{
if ($query->limit > 0) {
return ' LIMIT ' . $query->offset . ',' . $query->limit;
} else {
return '';
}
}
/**
* @param array $where
* @return string
*/
private function where(array $where): string
{
if (count($where) < 1) {
return '';
}
return implode(' AND ', $where);
}
}
/**
* @param $field
* @param $condition
* @param $_tmp
* @return string
* @throws NotFindClassException
* @throws ReflectionException|Exception
*/
private function resolveCondition($field, $condition, $_tmp): string
{
if (is_string($field)) {
$this->query->pushParam($condition);
return $field . ' = ?';
} else if (is_string($condition)) {
return $condition;
} else {
return $this->_arrayMap($condition, $_tmp);
}
}
/**
* @param $field
* @param $condition
* @param $_tmp
* @return string
* @throws
*/
private function resolveCondition($field, $condition, $_tmp): string
{
if (is_string($field)) {
$this->query->pushParam($condition);
return $field . ' = ?';
} else if (is_string($condition)) {
return $condition;
} else {
return $this->_arrayMap($condition, $_tmp);
}
}
/**
* @param $condition
* @param $array
* @return string
* @throws Exception
*/
private function _arrayMap($condition, $array): string
{
if (!isset($condition[0])) {
return implode(' AND ', $this->_hashMap($condition));
}
$stroppier = strtoupper($condition[0]);
if (str_contains($stroppier, 'OR')) {
if (!is_string($condition[2])) {
$condition[2] = $this->_hashMap($condition[2]);
}
$builder = Kiri::getDi()->get(OrCondition::class);
$builder->setValue($condition[2]);
$builder->setColumn($condition[1]);
$builder->oldParams = $array;
} else if (isset(ConditionClassMap::$conditionMap[$stroppier])) {
$defaultConfig = ConditionClassMap::$conditionMap[$stroppier];
/**
* @param $condition
* @param $array
* @return string
* @throws
*/
private function _arrayMap($condition, $array): string
{
if (!isset($condition[0])) {
return implode(' AND ', $this->_hashMap($condition));
}
$stroppier = strtoupper($condition[0]);
if (str_contains($stroppier, 'OR')) {
if (!is_string($condition[2])) {
$condition[2] = $this->_hashMap($condition[2]);
}
$builder = Kiri::getDi()->get(OrCondition::class);
$builder->setValue($condition[2]);
$builder->setColumn($condition[1]);
$builder->oldParams = $array;
} else if (isset(ConditionClassMap::$conditionMap[$stroppier])) {
$defaultConfig = ConditionClassMap::$conditionMap[$stroppier];
$class = $defaultConfig['class'];
unset($defaultConfig['class']);
$class = $defaultConfig['class'];
unset($defaultConfig['class']);
$builder = Kiri::getDi()->make($class, [], $defaultConfig);
$builder->setValue($condition[2]);
$builder->setColumn($condition[1]);
} else {
$builder = Kiri::getDi()->get(HashCondition::class);
$builder->setValue($condition);
}
$builder = Kiri::getDi()->make($class, [], $defaultConfig);
$builder->setValue($condition[2]);
$builder->setColumn($condition[1]);
} else {
$builder = Kiri::getDi()->get(HashCondition::class);
$builder->setValue($condition);
}
$array[] = $builder->builder();
$array[] = $builder->builder();
return implode(' AND ', $array);
}
return implode(' AND ', $array);
}
/**
* @param $condition
* @return array
*/
private function _hashMap($condition): array
{
$_array = [];
foreach ($condition as $key => $value) {
$this->query->pushParam($value);
$_array[] = $key . '= ?';
}
return $_array;
}
/**
* @param $condition
* @return array
*/
private function _hashMap($condition): array
{
$_array = [];
foreach ($condition as $key => $value) {
$this->query->pushParam($value);
$_array[] = $key . '= ?';
}
return $_array;
}
}
+49 -49
View File
@@ -26,54 +26,54 @@ use Kiri;
*/
abstract class HasBase implements \Database\Traits\Relation
{
/** @var ModelInterface|Collection */
protected mixed $data = null;
/**
* @var ModelInterface
*/
protected mixed $model;
protected mixed $value = 0;
/**
* HasBase constructor.
* @param string $name
*/
public function __construct(public string $name)
{
}
/**
* @param $name
* @param $arguments
* @return static
* @throws \ReflectionException
*/
public function __call($name, $arguments)
{
if ($name !== 'get') {
$relation = Kiri::getDi()->get(Relation::class);
$relation->getQuery($this->name)->$name(...$arguments);
return $this;
} else {
return $this->get();
}
}
/**
* @param $name
* @return mixed
*/
public function __get($name): mixed
{
if ($this->data === null) {
$this->data = $this->get();
}
return $this->data;
}
/** @var ModelInterface|Collection */
protected mixed $data = null;
/**
* @var ModelInterface
*/
protected mixed $model;
protected mixed $value = 0;
/**
* HasBase constructor.
* @param string $name
*/
public function __construct(public string $name)
{
}
/**
* @param $name
* @param $arguments
* @return static
* @throws
*/
public function __call($name, $arguments)
{
if ($name !== 'get') {
$relation = Kiri::getDi()->get(Relation::class);
$relation->getQuery($this->name)->$name(...$arguments);
return $this;
} else {
return $this->get();
}
}
/**
* @param $name
* @return mixed
*/
public function __get($name): mixed
{
if ($this->data === null) {
$this->data = $this->get();
}
return $this->data;
}
}
-24
View File
@@ -38,8 +38,6 @@ trait QueryTrait
public bool $lock = false;
public bool $ifNotWhere = false;
private SqlBuilder $builder;
@@ -310,28 +308,6 @@ trait QueryTrait
return $this->join("INNER JOIN " . $tableName, $alias, $onCondition, $param);
}
/**
* @param $array
*
* @return string
*/
private function toString($array): string
{
$tmp = [];
if (!is_array($array)) {
return $array;
}
foreach ($array as $key => $val) {
if (is_array($val)) {
$tmp[] = $this->toString($array);
} else {
$tmp[] = $key . '=:' . $key;
$this->attributes[':' . $key] = $val;
}
}
return implode(' AND ', $tmp);
}
/**
* @param string $field
*
+48 -48
View File
@@ -18,61 +18,61 @@ use JetBrains\PhpStorm\Pure;
class When
{
public ActiveQuery|ISqlBuilder $query;
public ActiveQuery|ISqlBuilder $query;
private array $_condition = [];
private array $_condition = [];
private string $else = '';
private string $else = '';
/**
* 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|int $condition
* @param string $then
* @return $this
* @throws Exception
*/
public function when(string|int $condition, string $then): static
{
$this->_condition[] = sprintf('WHEN %s THEN %s', $condition, $then);
return $this;
}
/**
* @param string $alias
*/
public function else(string $alias): void
/**
* CaseWhen constructor.
* @param string $column
* @param ActiveQuery|ISqlBuilder $activeQuery
*/
public function __construct(public string $column, public ActiveQuery|ISqlBuilder $activeQuery)
{
$this->else = $alias;
}
$this->_condition[] = 'CASE ' . $column;
}
/**
* @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|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;
}
/**
* @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)';
}
}