diff --git a/Database/SqlBuilder.php b/Database/SqlBuilder.php index 0adbcfb9..e1e207e0 100644 --- a/Database/SqlBuilder.php +++ b/Database/SqlBuilder.php @@ -204,16 +204,11 @@ class SqlBuilder extends Component */ private function _prefix($hasOrder = false): 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); - } - if (!empty($condition = $this->conditionToString())) { - $select = sprintf('%s WHERE %s', $select, $condition); + $select = ''; + if (!empty($this->query->from)) { + $select = $this->_selectPrefix(); } + $select = $this->_wherePrefix($select); if (!empty($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 * @return string diff --git a/Database/Traits/QueryTrait.php b/Database/Traits/QueryTrait.php index f3a2dccb..b5cef699 100644 --- a/Database/Traits/QueryTrait.php +++ b/Database/Traits/QueryTrait.php @@ -10,6 +10,7 @@ declare(strict_types=1); namespace Database\Traits; +use Closure; use Database\ActiveQuery; use Database\ActiveRecord; use Database\Condition\MathematicsCondition; @@ -33,7 +34,7 @@ trait QueryTrait public ?int $offset = NULL; public ?int $limit = NULL; public string $group = ''; - public string|\Closure|ActiveQuery $from = ''; + public string|Closure|ActiveQuery $from = ''; public string $alias = 't1'; public array $filter = []; @@ -86,16 +87,16 @@ trait QueryTrait * @return $this * @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)) { - $condition = $this->conditionToString($condition); + $condition = $this->makeClosureFunction($condition, true); } if (!is_string($condition1)) { - $condition1 = $this->conditionToString($condition1); + $condition1 = $this->makeClosureFunction($condition1, true); } if (!is_string($condition2)) { - $condition2 = $this->conditionToString($condition2); + $condition2 = $this->makeClosureFunction($condition2, true); } $this->where[] = 'IF(' . $condition . ', ' . $condition1 . ', ' . $condition2 . ')'; return $this; @@ -112,7 +113,7 @@ trait QueryTrait private function conditionToString($condition): string { $newSql = $this->makeNewSqlGenerate(); - if ($condition instanceof \Closure) { + if ($condition instanceof Closure) { call_user_func($condition, $newSql); } else { $newSql->where($condition); @@ -231,11 +232,11 @@ trait QueryTrait } /** - * @param string|\Closure $tableName + * @param string|Closure $tableName * * @return $this */ - public function from(string|\Closure $tableName): static + public function from(string|Closure $tableName): static { $this->from = $tableName; return $this; @@ -802,13 +803,39 @@ trait QueryTrait /** * @param callable|array|string $conditions * @return $this + * @throws NotFindClassException + * @throws ReflectionException + * @throws Exception */ public function where(callable|array|string $conditions): static { + if ($conditions instanceof Closure) { + $conditions = $this->makeClosureFunction($conditions); + } $this->where[] = $conditions; 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|null $having