This commit is contained in:
2023-04-07 18:23:27 +08:00
parent 6d122c4ae1
commit 5d41c7219f
9 changed files with 339 additions and 328 deletions
+40 -40
View File
@@ -22,8 +22,8 @@ use ReflectionException;
*/
trait Builder
{
/**
* @param $alias
* @return string
@@ -32,7 +32,7 @@ trait Builder
{
return " AS " . $alias;
}
/**
* @param $table
* @return string
@@ -45,7 +45,7 @@ trait Builder
}
return " FROM " . $table;
}
/**
* @param $join
* @return string
@@ -57,8 +57,8 @@ trait Builder
}
return '';
}
/**
* @param string $select
* @return string
@@ -67,8 +67,8 @@ trait Builder
{
return "SELECT " . $select;
}
/**
* @param $group
* @return string
@@ -80,7 +80,7 @@ trait Builder
}
return ' GROUP BY ' . $group;
}
/**
* @param $order
* @return string
@@ -93,7 +93,7 @@ trait Builder
return '';
}
}
/**
* @param ActiveQuery|Query $query
* @param bool $hasLimit
@@ -107,33 +107,31 @@ trait Builder
return ' LIMIT ' . $query->limit;
}
}
/**
* @param $where
* @param array $where
* @return string
* @throws Exception
* @throws NotFindClassException
* @throws ReflectionException
*/
private function where($where): string
private function where(array $where): string
{
$_tmp = [];
if (empty($where)) return '';
if (is_string($where)) return $where;
foreach ($where as $key => $value) {
if (is_null($value)) continue;
if (($_value = $this->resolveCondition($key, $value, $_tmp)) == '') {
continue;
}
$_tmp[] = $_value;
if (count($where) < 1) {
return '';
}
if (!empty($_tmp)) {
$_tmp = [];
foreach ($where as $key => $value) {
$_tmp[] = $this->resolveCondition($key, $value, $_tmp);
}
if (count($_tmp) > 0) {
return implode(' AND ', $_tmp);
} else {
return '';
}
}
/**
* @param $field
* @param $condition
@@ -152,8 +150,8 @@ trait Builder
return $this->_arrayMap($condition, $_tmp);
}
}
/**
* @param $condition
* @param $array
@@ -176,10 +174,10 @@ trait Builder
$builder->oldParams = $array;
} else if (isset(ConditionClassMap::$conditionMap[$stroppier])) {
$defaultConfig = ConditionClassMap::$conditionMap[$stroppier];
$class = $defaultConfig['class'];
unset($defaultConfig['class']);
$builder = Kiri::getDi()->make($class, [], $defaultConfig);
$builder->setValue($condition[2]);
$builder->setColumn($condition[1]);
@@ -187,13 +185,16 @@ trait Builder
$builder = Kiri::getDi()->get(HashCondition::class);
$builder->setValue($condition);
}
$array[] = $builder->builder();
return implode(' AND ', $array);
}
public array $params = [];
/**
* @param $condition
* @return array
@@ -202,16 +203,15 @@ trait Builder
{
$_array = [];
foreach ($condition as $key => $value) {
if (is_null($value)) continue;
$value = is_numeric($value) ? $value : '\'' . $value . '\'';
if (!is_numeric($key)) {
$_array[] = $key . '=' . $value;
$this->query->bindParam(':' . $key, $value);
$_array[] = $key . '=:' . $key;
} else {
$_array[] = $value;
}
}
return $_array;
}
}
+23 -9
View File
@@ -34,7 +34,7 @@ trait QueryTrait
public int $offset = 0;
public int $limit = 500;
public string $group = '';
public string|Closure|ActiveQuery|null $from = '';
public string|Closure|ActiveQuery|null $from = null;
public string $alias = 't1';
public array $filter = [];
@@ -757,7 +757,9 @@ trait QueryTrait
return $this;
}
$this->where[] = $column . ' BETWEEN ' . $start . ' AND ' . $end;
$this->bindParam(':between_start' . $column, $start);
$this->bindParam(':between_end' . $column, $end);
$this->where[] = $column . ' BETWEEN :not_between_start' . $column . ' AND :not_between_end' . $column;
return $this;
}
@@ -774,7 +776,9 @@ trait QueryTrait
return $this;
}
$this->where[] = $column . 'NOT BETWEEN' . $start . ' AND ' . $end;
$this->bindParam(':not_between_start' . $column, $start);
$this->bindParam(':not_between_end' . $column, $end);
$this->where[] = $column . ' NOT BETWEEN :not_between_start' . $column . ' AND :not_between_end' . $column;
return $this;
}
@@ -786,13 +790,24 @@ trait QueryTrait
*/
public function bindParams(?array $params = []): static
{
if (empty($params)) {
if ($params === null) {
return $this;
}
$this->attributes = $params;
return $this;
}
/**
* @param string $key
* @param mixed $value
* @return $this
*/
public function bindParam(string $key, mixed $value): static
{
$this->attributes[$key] = $value;
return $this;
}
/**
* @param array|string $column
* @param string $opera
@@ -809,7 +824,8 @@ trait QueryTrait
$this->where[] = $column;
} else {
[$column, $opera, $value] = $this->opera(...func_get_args());
$this->where[] = "$column $opera $value";
$this->bindParam(':' . $column, $value);
$this->where[] = $column . ' ' . $opera . ':' . $column;
}
return $this;
}
@@ -900,10 +916,8 @@ trait QueryTrait
*/
private function sprintf($column, $value, string $opera = '='): string
{
if (is_string($value)) {
$value = addslashes($value);
}
return $column . ' ' . $opera . ' \'' . $value . '\'';
$this->bindParam(':' . $column, $value);
return $column . ' ' . $opera . ' :' . $column;
}