This commit is contained in:
2023-04-10 17:13:24 +08:00
parent aaa3fba8ba
commit 94ebb18b7e
11 changed files with 409 additions and 767 deletions
+7 -13
View File
@@ -70,12 +70,12 @@ trait Builder
/**
* @param $group
* @param string $group
* @return string
*/
private function builderGroup($group): string
private function builderGroup(string $group): string
{
if (empty($group)) {
if ($group != '') {
return '';
}
return ' GROUP BY ' . $group;
@@ -143,7 +143,8 @@ trait Builder
private function resolveCondition($field, $condition, $_tmp): string
{
if (is_string($field)) {
return $field . ' = \'' . $condition . '\'';
$this->query->bindParam(':where' . $field, $condition);
return $field . ' = ' . ':where' . $field;
} else if (is_string($condition)) {
return $condition;
} else {
@@ -192,9 +193,6 @@ trait Builder
}
public array $params = [];
/**
* @param $condition
* @return array
@@ -203,12 +201,8 @@ trait Builder
{
$_array = [];
foreach ($condition as $key => $value) {
if (!is_numeric($key)) {
$this->query->bindParam(':' . $key, $value);
$_array[] = $key . '=:' . $key;
} else {
$_array[] = $value;
}
$this->query->bindParam(':hash' . $key, $value);
$_array[] = $key . '=:hash' . $key;
}
return $_array;
}
+10 -54
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 = null;
public string $from = '';
public string $alias = 't1';
public array $filter = [];
@@ -103,7 +103,7 @@ trait QueryTrait
*/
public function whereRaw(string $whereRaw): static
{
if (empty($whereRaw)) {
if ($whereRaw == '') {
return $this;
}
$this->where[] = $whereRaw;
@@ -112,36 +112,10 @@ trait QueryTrait
/**
* @param string|array|Closure $condition
* @param string|array|Closure $condition1
* @param string|array|Closure $condition2
* @return $this
* @throws
*/
public function whereIf(string|array|Closure $condition, string|array|Closure $condition1, string|array|Closure $condition2): static
{
if (!is_string($condition)) {
$condition = $this->makeClosureFunction($condition);
}
if (!is_string($condition1)) {
$condition1 = $this->makeClosureFunction($condition1);
}
if (!is_string($condition2)) {
$condition2 = $this->makeClosureFunction($condition2);
}
$this->where[] = 'IF(' . $condition . ', ' . $condition1 . ', ' . $condition2 . ')';
return $this;
}
/**
* @param $bool
* @param bool $bool
* @return $this
*/
public function ifNotWhere($bool): static
public function ifNotWhere(bool $bool): static
{
$this->ifNotWhere = $bool;
return $this;
@@ -210,27 +184,6 @@ trait QueryTrait
return $this;
}
/**
* @param array|Closure|string $columns
* @return $this
*/
public function filter(array|Closure|string $columns): static
{
if (!$columns) {
return $this;
}
if (is_callable($columns, TRUE)) {
return call_user_func($columns, $this);
}
if (is_string($columns)) {
$columns = explode(',', $columns);
}
if (!is_array($columns)) {
return $this;
}
$this->filter = $columns;
return $this;
}
/**
* @param string $alias
@@ -252,6 +205,9 @@ trait QueryTrait
*/
public function from(string|Closure $tableName): static
{
if ($tableName instanceof Closure) {
$tableName = call_user_func($tableName, $this->makeNewSqlGenerate());
}
$this->from = $tableName;
return $this;
}
@@ -325,7 +281,7 @@ trait QueryTrait
}
$tableName = $model->getTable();
}
return $this->join(...["LEFT JOIN " . $tableName, $alias, $onCondition, $param]);
return $this->join("LEFT JOIN " . $tableName, $alias, $onCondition, $param);
}
/**
@@ -345,7 +301,7 @@ trait QueryTrait
}
$tableName = $model->getTable();
}
return $this->join(...["RIGHT JOIN " . $tableName, $alias, $onCondition, $param]);
return $this->join("RIGHT JOIN " . $tableName, $alias, $onCondition, $param);
}
/**
@@ -365,7 +321,7 @@ trait QueryTrait
}
$tableName = $model->getTable();
}
return $this->join(...["INNER JOIN " . $tableName, $alias, $onCondition, $param]);
return $this->join("INNER JOIN " . $tableName, $alias, $onCondition, $param);
}
/**