This commit is contained in:
2021-03-26 18:51:47 +08:00
parent 21dd7c0901
commit 4a7cfb90a7
2 changed files with 73 additions and 17 deletions
+38 -9
View File
@@ -204,16 +204,11 @@ class SqlBuilder extends Component
*/ */
private function _prefix($hasOrder = false): string private function _prefix($hasOrder = false): string
{ {
$select = $this->builderSelect($this->query->select) . ' FROM ' . $this->tableName(); $select = '';
if (!empty($this->query->alias)) { if (!empty($this->query->from)) {
$select .= $this->builderAlias($this->query->alias); $select = $this->_selectPrefix();
}
if (!empty($this->query->join)) {
$select .= $this->builderJoin($this->query->join);
}
if (!empty($condition = $this->conditionToString())) {
$select = sprintf('%s WHERE %s', $select, $condition);
} }
$select = $this->_wherePrefix($select);
if (!empty($this->query->group)) { if (!empty($this->query->group)) {
$select .= $this->builderGroup($this->query->group); $select .= $this->builderGroup($this->query->group);
} }
@@ -224,6 +219,40 @@ class SqlBuilder extends Component
} }
/**
* @param $select
* @return string
* @throws Exception
*/
private function _wherePrefix($select): string
{
$condition = $this->conditionToString();
if (empty($condition)) {
return $select;
} else if (empty($select)) {
return $condition;
}
return sprintf('%s WHERE %s', $select, $condition);
}
/**
* @return string
* @throws Exception
*/
private function _selectPrefix(): string
{
$select = $this->builderSelect($this->query->select) . ' FROM ' . $this->tableName();
if (!empty($this->query->alias)) {
$select .= $this->builderAlias($this->query->alias);
}
if (!empty($this->query->join)) {
$select .= $this->builderJoin($this->query->join);
}
return $select;
}
/** /**
* @param false $isCount * @param false $isCount
* @return string * @return string
+35 -8
View File
@@ -10,6 +10,7 @@ declare(strict_types=1);
namespace Database\Traits; namespace Database\Traits;
use Closure;
use Database\ActiveQuery; use Database\ActiveQuery;
use Database\ActiveRecord; use Database\ActiveRecord;
use Database\Condition\MathematicsCondition; use Database\Condition\MathematicsCondition;
@@ -33,7 +34,7 @@ trait QueryTrait
public ?int $offset = NULL; public ?int $offset = NULL;
public ?int $limit = NULL; public ?int $limit = NULL;
public string $group = ''; public string $group = '';
public string|\Closure|ActiveQuery $from = ''; public string|Closure|ActiveQuery $from = '';
public string $alias = 't1'; public string $alias = 't1';
public array $filter = []; public array $filter = [];
@@ -86,16 +87,16 @@ trait QueryTrait
* @return $this * @return $this
* @throws Exception * @throws Exception
*/ */
public function if(string|array $condition, string|array|\Closure $condition1, string|array|\Closure $condition2): static public function if(string|array $condition, string|array|Closure $condition1, string|array|Closure $condition2): static
{ {
if (!is_string($condition)) { if (!is_string($condition)) {
$condition = $this->conditionToString($condition); $condition = $this->makeClosureFunction($condition, true);
} }
if (!is_string($condition1)) { if (!is_string($condition1)) {
$condition1 = $this->conditionToString($condition1); $condition1 = $this->makeClosureFunction($condition1, true);
} }
if (!is_string($condition2)) { if (!is_string($condition2)) {
$condition2 = $this->conditionToString($condition2); $condition2 = $this->makeClosureFunction($condition2, true);
} }
$this->where[] = 'IF(' . $condition . ', ' . $condition1 . ', ' . $condition2 . ')'; $this->where[] = 'IF(' . $condition . ', ' . $condition1 . ', ' . $condition2 . ')';
return $this; return $this;
@@ -112,7 +113,7 @@ trait QueryTrait
private function conditionToString($condition): string private function conditionToString($condition): string
{ {
$newSql = $this->makeNewSqlGenerate(); $newSql = $this->makeNewSqlGenerate();
if ($condition instanceof \Closure) { if ($condition instanceof Closure) {
call_user_func($condition, $newSql); call_user_func($condition, $newSql);
} else { } else {
$newSql->where($condition); $newSql->where($condition);
@@ -231,11 +232,11 @@ trait QueryTrait
} }
/** /**
* @param string|\Closure $tableName * @param string|Closure $tableName
* *
* @return $this * @return $this
*/ */
public function from(string|\Closure $tableName): static public function from(string|Closure $tableName): static
{ {
$this->from = $tableName; $this->from = $tableName;
return $this; return $this;
@@ -802,13 +803,39 @@ trait QueryTrait
/** /**
* @param callable|array|string $conditions * @param callable|array|string $conditions
* @return $this * @return $this
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/ */
public function where(callable|array|string $conditions): static public function where(callable|array|string $conditions): static
{ {
if ($conditions instanceof Closure) {
$conditions = $this->makeClosureFunction($conditions);
}
$this->where[] = $conditions; $this->where[] = $conditions;
return $this; return $this;
} }
/**
* @param Closure|array $closure
* @param bool $onlyWhere
* @return string
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
private function makeClosureFunction(Closure|array $closure, $onlyWhere = false): string
{
$generate = $this->makeNewSqlGenerate();
call_user_func($closure, $generate);
if ($onlyWhere === true) {
return $generate->getCondition();
}
return $generate->getSql();
}
/** /**
* @param string $name * @param string $name
* @param string|null $having * @param string|null $having