This commit is contained in:
2021-03-30 16:03:44 +08:00
parent b57ca5e9ea
commit 9f84636b6c
2 changed files with 267 additions and 262 deletions
+5 -4
View File
@@ -309,10 +309,11 @@ class ActiveQuery extends Component implements ISqlBuilder
} }
/** /**
* @return bool * @param bool $getSql
* @throws Exception * @return string|bool
*/ * @throws Exception
*/
public function delete($getSql = false): string|bool public function delete($getSql = false): string|bool
{ {
$sql = $this->builder->delete(); $sql = $this->builder->delete();
+262 -258
View File
@@ -17,315 +17,319 @@ use Snowflake\Abstracts\Component;
class SqlBuilder extends Component class SqlBuilder extends Component
{ {
use Builder; use Builder;
public ActiveQuery|Query|null $query; public ActiveQuery|Query|null $query;
/** /**
* @param $query * @param $query
* @return $this * @return $this
*/ */
public static function builder(ISqlBuilder|null $query): static public static function builder(ISqlBuilder|null $query): static
{ {
return new static(['query' => $query]); return new static(['query' => $query]);
} }
/** /**
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
public function getCondition(): string public function getCondition(): string
{ {
return $this->conditionToString(); return $this->conditionToString();
} }
/** /**
* @param array $attributes * @param array $attributes
* @return bool|array * @return bool|array
* @throws Exception * @throws Exception
*/ */
public function update(array $attributes): bool|array public function update(array $attributes): bool|array
{ {
[$string, $array] = $this->builderParams($attributes); [$string, $array] = $this->builderParams($attributes);
if (empty($string) || empty($array)) { if (empty($string) || empty($array)) {
return $this->addError('None data update.'); return $this->addError('None data update.');
} }
$condition = $this->conditionToString(); $condition = $this->conditionToString();
if (!empty($condition)) { if (!empty($condition)) {
$condition = ' WHERE ' . $condition; $condition = ' WHERE ' . $condition;
} }
$update = 'UPDATE ' . $this->tableName() . ' SET ' . implode(',', $string) . $condition; $update = 'UPDATE ' . $this->tableName() . ' SET ' . implode(',', $string) . $condition;
$update .= $this->builderLimit($this->query); $update .= $this->builderLimit($this->query);
return [$update, $array]; return [$update, $array];
} }
/** /**
* @param array $attributes * @param array $attributes
* @param string $opera * @param string $opera
* @return bool|array * @return bool|array
* @throws Exception * @throws Exception
*/ */
public function mathematics(array $attributes, $opera = '+'): bool|array public function mathematics(array $attributes, $opera = '+'): bool|array
{ {
$string = $array = []; $string = $array = [];
foreach ($attributes as $attribute => $value) { foreach ($attributes as $attribute => $value) {
$string[] = $attribute . '=' . $attribute . $opera . $value; $string[] = $attribute . '=' . $attribute . $opera . $value;
} }
if (empty($string)) { if (empty($string)) {
return $this->addError('None data update.'); return $this->addError('None data update.');
} }
$condition = $this->conditionToString(); $condition = $this->conditionToString();
if (!empty($condition)) { if (!empty($condition)) {
$condition = ' WHERE ' . $condition; $condition = ' WHERE ' . $condition;
} }
$update = 'UPDATE ' . $this->tableName() . ' SET ' . implode(',', $string) . $condition; $update = 'UPDATE ' . $this->tableName() . ' SET ' . implode(',', $string) . $condition;
$update .= $this->builderLimit($this->query); $update .= $this->builderLimit($this->query);
return [$update, []]; return [$update, []];
} }
/** /**
* @param array $attributes * @param array $attributes
* @param false $isBatch * @param false $isBatch
* @return array * @return array
* @throws Exception * @throws Exception
*/ */
public function insert(array $attributes, $isBatch = false): array public function insert(array $attributes, $isBatch = false): array
{ {
$update = sprintf('INSERT INTO %s', $this->tableName()); $update = sprintf('INSERT INTO %s', $this->tableName());
if ($isBatch === false) { if ($isBatch === false) {
$attributes = [$attributes]; $attributes = [$attributes];
} }
$update .= '(' . implode(',', $this->getFields($attributes)) . ') VALUES '; $update .= '(' . implode(',', $this->getFields($attributes)) . ') VALUES ';
$order = 0; $order = 0;
$keys = $params = []; $keys = $params = [];
foreach ($attributes as $attribute) { foreach ($attributes as $attribute) {
[$_keys, $params] = $this->builderParams($attribute, true, $params, $order); [$_keys, $params] = $this->builderParams($attribute, true, $params, $order);
$keys[] = implode(',', $_keys); $keys[] = implode(',', $_keys);
$order++; $order++;
} }
return [$update . '(' . implode('),(', $keys) . ')', $params]; return [$update . '(' . implode('),(', $keys) . ')', $params];
} }
/** /**
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
public function delete() public function delete(): string
{ {
$delete = sprintf('DELETE FROM %s ', $this->query->modelClass::getTable()); $delete = sprintf('DELETE FROM %s ', $this->query->modelClass::getTable());
$this->query->from = null; $this->query->from = null;
return $delete . ' WHERE ' . $this->_prefix(true); return $delete . ' WHERE ' . $this->_prefix(true);
} }
/** /**
* @param $attributes * @param $attributes
* @return array * @return array
*/ */
#[Pure] private function getFields($attributes): array #[Pure] private function getFields($attributes): array
{ {
return array_keys(current($attributes)); return array_keys(current($attributes));
} }
/** /**
* @param array $attributes * @param array $attributes
* @param bool $isInsert * @param bool $isInsert
* @param array $params * @param array $params
* @param int $order * @param int $order
* @return array[] * @return array[]
* a=:b, * a=:b,
*/ */
private function builderParams(array $attributes, bool $isInsert = false, $params = [], $order = 0): array private function builderParams(array $attributes, bool $isInsert = false, $params = [], $order = 0): array
{ {
$keys = []; $keys = [];
foreach ($attributes as $key => $value) { foreach ($attributes as $key => $value) {
if ($isInsert === true) { if ($isInsert === true) {
$keys[] = ':' . $key . $order; $keys[] = ':' . $key . $order;
} else { } else {
$keys[] = $key . '=:' . $key . $order; $keys[] = $key . '=:' . $key . $order;
} }
$params[$key . $order] = $value; $params[$key . $order] = $value;
} }
return [$keys, $params]; return [$keys, $params];
} }
/** /**
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
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)) { if (empty($this->query->from) && !empty($this->query->modelClass)) {
$this->query->from($this->query->getTable()); $this->query->from($this->query->getTable());
} }
return $this->_prefix(true); return $this->_prefix(true);
} }
/** /**
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
public function all(): string public function all(): string
{ {
if (empty($this->query->from) && !empty($this->query->modelClass)) { if (empty($this->query->from) && !empty($this->query->modelClass)) {
$this->query->from($this->query->getTable()); $this->query->from($this->query->getTable());
} }
return $this->_prefix(true); return $this->_prefix(true);
} }
/** /**
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
public function count(): string public function count(): string
{ {
if (empty($this->query->from) && !empty($this->query->modelClass)) { if (empty($this->query->from) && !empty($this->query->modelClass)) {
$this->query->from($this->query->getTable()); $this->query->from($this->query->getTable());
} }
return $this->_prefix(); return $this->_prefix();
} }
/** /**
* @param $table * @param $table
* @return string * @return string
*/ */
public function columns($table): string public function columns($table): string
{ {
return 'SHOW FULL FIELDS FROM ' . $table; return 'SHOW FULL FIELDS FROM ' . $table;
} }
/** /**
* @param bool $hasOrder * @param bool $hasOrder
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
private function _prefix($hasOrder = false): string private function _prefix($hasOrder = false): string
{ {
$select = ''; $select = '';
if (!empty($this->query->from)) { if (!empty($this->query->from)) {
$select = $this->_selectPrefix(); $select = $this->_selectPrefix();
} }
$select = $this->_wherePrefix($select); $select = $this->_wherePrefix($select);
if (!empty($this->query->group)) { if (!empty($this->query->attributes) && is_array($this->query->attributes)) {
$select .= $this->builderGroup($this->query->group); $select = strtr($select, $this->query->attributes);
} }
if ($hasOrder === true && !empty($this->query->order)) {
$select .= $this->builderOrder($this->query->order); if (!empty($this->query->group)) {
} $select .= $this->builderGroup($this->query->group);
return $select . $this->builderLimit($this->query); }
} if ($hasOrder === true && !empty($this->query->order)) {
$select .= $this->builderOrder($this->query->order);
}
return $select . $this->builderLimit($this->query);
}
/** /**
* @param $select * @param $select
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
private function _wherePrefix($select): string private function _wherePrefix($select): string
{ {
$condition = $this->conditionToString(); $condition = $this->conditionToString();
if (empty($condition)) { if (empty($condition)) {
return $select; return $select;
} else if (empty($select)) { } else if (empty($select)) {
return $condition; return $condition;
} }
return sprintf('%s WHERE %s', $select, $condition); return sprintf('%s WHERE %s', $select, $condition);
} }
/** /**
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
private function _selectPrefix(): string private function _selectPrefix(): string
{ {
$select = $this->builderSelect($this->query->select) . ' FROM ' . $this->tableName(); $select = $this->builderSelect($this->query->select) . ' FROM ' . $this->tableName();
if (!empty($this->query->alias)) { if (!empty($this->query->alias)) {
$select .= $this->builderAlias($this->query->alias); $select .= $this->builderAlias($this->query->alias);
} }
if (!empty($this->query->join)) { if (!empty($this->query->join)) {
$select .= $this->builderJoin($this->query->join); $select .= $this->builderJoin($this->query->join);
} }
return $select; return $select;
} }
/** /**
* @param false $isCount * @param false $isCount
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
public function get($isCount = false): string public function get($isCount = false): string
{ {
if ($isCount === false) { if ($isCount === false) {
return $this->all(); return $this->all();
} }
return $this->count(); return $this->count();
} }
/** /**
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
public function truncate(): string public function truncate(): string
{ {
return sprintf('TRUNCATE %s', $this->tableName()); return sprintf('TRUNCATE %s', $this->tableName());
} }
/** /**
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
private function conditionToString(): string private function conditionToString(): string
{ {
return $this->where($this->query->where); return $this->where($this->query->where);
} }
/** /**
* @return string * @return string
* @throws Exception * @throws Exception
*/ */
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 = sprintf('(%s)', $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 = sprintf('%s', SqlBuilder::builder($this->query->from)->get($this->query->from));
} }
if (empty($this->query->from)) { if (empty($this->query->from)) {
return $this->query->modelClass::getTable(); return $this->query->modelClass::getTable();
} }
return $this->query->from; return $this->query->from;
} }
} }