This commit is contained in:
2023-04-01 00:25:22 +08:00
parent c165f5f205
commit 79db796ba3
3 changed files with 123 additions and 178 deletions
+28 -68
View File
@@ -96,12 +96,7 @@ class SqlBuilder extends Component
return $this->logger->addError('None data update.'); return $this->logger->addError('None data update.');
} }
$condition = $this->conditionToString(); $update = 'UPDATE ' . $this->tableName() . ' SET ' . implode(',', $string) . $this->_prefix();
if (!empty($condition)) {
$condition = ' WHERE ' . $condition;
}
$update = 'UPDATE ' . $this->tableName() . ' SET ' . implode(',', $string) . $condition;
$update .= $this->builderLimit($this->query, false); $update .= $this->builderLimit($this->query, false);
return [$update, $params]; return [$update, $params];
@@ -140,11 +135,7 @@ class SqlBuilder extends Component
*/ */
public function delete(): string public function delete(): string
{ {
$delete = sprintf('DELETE FROM %s ', $this->query->modelClass->getTable()); return 'DELETE FROM ' . $this->tableName() . ' WHERE ' . $this->_prefix();
$this->query->from = null;
return $delete . ' WHERE ' . $this->_prefix(true);
} }
@@ -214,10 +205,7 @@ class SqlBuilder extends Component
public function one(): string public function one(): string
{ {
$this->query->limit(0, 1); $this->query->limit(0, 1);
if (empty($this->query->from) && !empty($this->query->modelClass)) { return $this->_selectPrefix() . $this->_prefix();
$this->query->from($this->query->getTable());
}
return $this->_prefix(true);
} }
@@ -227,10 +215,7 @@ class SqlBuilder extends Component
*/ */
public function all(): string public function all(): string
{ {
if (empty($this->query->from) && !empty($this->query->modelClass)) { return $this->_selectPrefix() . $this->_prefix();
$this->query->from($this->query->getTable());
}
return $this->_prefix(true);
} }
@@ -240,10 +225,8 @@ class SqlBuilder extends Component
*/ */
public function count(): string public function count(): string
{ {
if (empty($this->query->from) && !empty($this->query->modelClass)) { $this->query->select('COUNT(*)');
$this->query->from($this->query->getTable()); return $this->_selectPrefix() . $this->_prefix();
}
return $this->_prefix(false, true);
} }
@@ -258,66 +241,42 @@ class SqlBuilder extends Component
/** /**
* @param bool $hasOrder
* @param bool $isCount
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
private function _prefix(bool $hasOrder = false, bool $isCount = false): string private function _prefix(): string
{ {
$select = ''; $select = '';
if (!empty($this->query->from)) { if (($condition = $this->conditionToString()) != '') {
$select = $this->_selectPrefix($isCount); $select .= " WHERE ${$condition}";
} }
$select = $this->_wherePrefix($select); if (count($this->query->attributes) > 0) {
if (!empty($this->query->attributes) && is_array($this->query->attributes)) {
$select = strtr($select, $this->query->attributes); $select = strtr($select, $this->query->attributes);
} }
if ($this->query->group != "") {
if (!empty($this->query->group)) { $select .= ' GROUP BY ' . $this->query->group;
$select .= $this->builderGroup($this->query->group);
} }
if ($hasOrder === true && !empty($this->query->order)) { if ($this->query->order != "") {
$select .= $this->builderOrder($this->query->order); $select .= ' ORDER BY ' . implode(',', $this->query->order);
} }
$sql = $select . $this->builderLimit($this->query); return $select . $this->builderLimit($this->query);
if ($this->query->lock) {
$sql .= ' FOR UPDATE';
} }
return $sql;
}
/** /**
* @param $select
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
private function _wherePrefix($select): string private function _selectPrefix(): string
{ {
$condition = $this->conditionToString(); if (count($this->query->select) < 1) {
if (empty($condition)) { $this->query->select = ['*'];
return $select;
} else if (empty($select)) {
return $condition;
} }
return sprintf('%s WHERE %s', $select, $condition); $select = "SELECT " . implode(',', $this->query->select) . " FROM " . $this->tableName();
if ($this->query->alias != "") {
$select .= " AS " . $this->query->alias;
} }
if (count($this->query->join) > 0) {
$select .= ' ' . implode(' ', $this->query->join);
/**
* @param bool $isCount
* @return string
* @throws Exception
*/
private function _selectPrefix(bool $isCount): string
{
$select = $this->builderSelect($this->query->select, $isCount) . ' 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; return $select;
} }
@@ -364,15 +323,16 @@ class SqlBuilder extends Component
public function tableName(): string public function tableName(): string
{ {
if ($this->query->from instanceof \Closure) { if ($this->query->from instanceof \Closure) {
$this->query->from = sprintf('(%s)', $this->query->makeClosureFunction($this->query->from)); $this->query->from = '(' . $this->query->makeClosureFunction($this->query->from) . ')';
} }
if ($this->query->from instanceof ActiveQuery) { if ($this->query->from instanceof ActiveQuery) {
$this->query->from = sprintf('%s', SqlBuilder::builder($this->query->from)->get($this->query->from)); $this->query->from = '(' . SqlBuilder::builder($this->query->from)->get($this->query->from) . ')';
} }
if (empty($this->query->from)) { if ($this->query->from == "") {
return $this->query->modelClass->getTable(); return $this->query->modelClass->getTable();
} } else {
return $this->query->from; return $this->query->from;
} }
}
} }
+14 -29
View File
@@ -60,24 +60,13 @@ trait Builder
/** /**
* @param null $select * @param string $select
* @param bool $isCount
* @return string * @return string
*/ */
#[Pure] private function builderSelect($select = NULL, bool $isCount = false): string #[Pure] private function builderSelect(string $select = "*"): string
{ {
if ($isCount) {
return "SELECT COUNT(*)";
}
if (empty($select)) {
return "SELECT *";
}
if (is_array($select)) {
return "SELECT " . implode(',', $select);
} else {
return "SELECT " . $select; return "SELECT " . $select;
} }
}
/** /**
@@ -112,14 +101,12 @@ trait Builder
*/ */
#[Pure] private function builderLimit(ActiveQuery|Query $query, bool $hasLimit = true): string #[Pure] private function builderLimit(ActiveQuery|Query $query, bool $hasLimit = true): string
{ {
if (!is_numeric($query->limit) || $query->limit < 1) { if ($hasLimit) {
return "";
}
if ($query->offset !== null && $hasLimit) {
return ' LIMIT ' . $query->offset . ',' . $query->limit; return ' LIMIT ' . $query->offset . ',' . $query->limit;
} } else {
return ' LIMIT ' . $query->limit; return ' LIMIT ' . $query->limit;
} }
}
/** /**
@@ -134,17 +121,17 @@ trait Builder
if (is_string($where)) return $where; if (is_string($where)) return $where;
foreach ($where as $key => $value) { foreach ($where as $key => $value) {
if (is_null($value)) continue; if (is_null($value)) continue;
if (($_value = $this->resolveCondition($key, $value, $_tmp)) == '') {
$_value = $this->resolveCondition($key, $value, $_tmp); continue;
}
if (empty($_value)) continue;
$_tmp[] = $_value; $_tmp[] = $_value;
} }
if (!empty($_tmp)) { if (!empty($_tmp)) {
return implode(' AND ', $_tmp); return implode(' AND ', $_tmp);
} } else {
return ''; return '';
} }
}
/** /**
@@ -158,13 +145,12 @@ trait Builder
private function resolveCondition($field, $condition, $_tmp): string private function resolveCondition($field, $condition, $_tmp): string
{ {
if (is_string($field)) { if (is_string($field)) {
$_value = sprintf('%s = \'%s\'', $field, $condition); return $field . ' = \'' . $condition . '\'';
} else if (is_string($condition)) { } else if (is_string($condition)) {
$_value = $condition; return $condition;
} else { } else {
$_value = $this->_arrayMap($condition, $_tmp); return $this->_arrayMap($condition, $_tmp);
} }
return $_value;
} }
@@ -217,10 +203,9 @@ trait Builder
$_array = []; $_array = [];
foreach ($condition as $key => $value) { foreach ($condition as $key => $value) {
if (is_null($value)) continue; if (is_null($value)) continue;
$value = is_numeric($value) ? $value : '\'' . $value . '\''; $value = is_numeric($value) ? $value : '\'' . $value . '\'';
if (!is_numeric($key)) { if (!is_numeric($key)) {
$_array[] = sprintf('%s = %s', $key, $value); $_array[] = $key . '=' . $value;
} else { } else {
$_array[] = $value; $_array[] = $value;
} }
+6 -6
View File
@@ -31,8 +31,8 @@ trait QueryTrait
public array $select = []; public array $select = [];
public array $join = []; public array $join = [];
public array $order = []; public array $order = [];
public ?int $offset = NULL; public int $offset = 0;
public ?int $limit = NULL; public int $limit = 500;
public string $group = ''; public string $group = '';
public string|Closure|ActiveQuery|null $from = ''; public string|Closure|ActiveQuery|null $from = '';
public string $alias = 't1'; public string $alias = 't1';
@@ -54,14 +54,14 @@ trait QueryTrait
/** /**
* clear * clear
*/ */
public function clear() public function clear(): void
{ {
$this->where = []; $this->where = [];
$this->select = []; $this->select = [];
$this->join = []; $this->join = [];
$this->order = []; $this->order = [];
$this->offset = NULL; $this->offset = 0;
$this->limit = NULL; $this->limit = 500;
$this->group = ''; $this->group = '';
$this->from = ''; $this->from = '';
$this->alias = 't1'; $this->alias = 't1';
@@ -480,7 +480,7 @@ trait QueryTrait
public function select(array|string $column = '*'): static public function select(array|string $column = '*'): static
{ {
if ($column == '*') { if ($column == '*') {
$this->select = $column; $this->select = [$column];
} else { } else {
if (!is_array($column)) { if (!is_array($column)) {
$column = explode(',', $column); $column = explode(',', $column);