This commit is contained in:
2023-04-07 21:47:15 +08:00
parent 521fe64b62
commit ad43d02673
+54 -54
View File
@@ -16,13 +16,13 @@ use Kiri\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 ISqlBuilder|null $query * @param ISqlBuilder|null $query
* @return $this * @return $this
@@ -32,8 +32,8 @@ class SqlBuilder extends Component
{ {
return new static(['query' => $query]); return new static(['query' => $query]);
} }
/** /**
* @return string * @return string
* @throws Exception * @throws Exception
@@ -42,8 +42,8 @@ class SqlBuilder extends Component
{ {
return $this->conditionToString(); return $this->conditionToString();
} }
/** /**
* @param array $compiler * @param array $compiler
* @return string * @return string
@@ -53,8 +53,8 @@ class SqlBuilder extends Component
{ {
return $this->where($compiler); return $this->where($compiler);
} }
/** /**
* @param array $attributes * @param array $attributes
* @return bool|array * @return bool|array
@@ -63,11 +63,11 @@ class SqlBuilder extends Component
public function update(array $attributes): bool|array public function update(array $attributes): bool|array
{ {
[$string, $array] = $this->builderParams($attributes); [$string, $array] = $this->builderParams($attributes);
return $this->__updateBuilder($string, $array); return $this->__updateBuilder($string, $array);
} }
/** /**
* @param array $attributes * @param array $attributes
* @param string $opera * @param string $opera
@@ -82,8 +82,8 @@ class SqlBuilder extends Component
} }
return $this->__updateBuilder($string, []); return $this->__updateBuilder($string, []);
} }
/** /**
* @param array $string * @param array $string
* @param array $params * @param array $params
@@ -95,13 +95,13 @@ class SqlBuilder extends Component
if (empty($string)) { if (empty($string)) {
return $this->logger->addError('None data update.'); return $this->logger->addError('None data update.');
} }
$update = 'UPDATE ' . $this->tableName() . ' SET ' . implode(',', $string) . $this->_prefix(); $update = 'UPDATE ' . $this->tableName() . ' SET ' . implode(',', $string) . $this->_prefix();
return [$update, $params]; return [$update, $params];
} }
/** /**
* @param array $attributes * @param array $attributes
* @param false $isBatch * @param false $isBatch
@@ -115,19 +115,19 @@ class SqlBuilder extends Component
$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
@@ -136,8 +136,8 @@ class SqlBuilder extends Component
{ {
return 'DELETE FROM ' . $this->tableName() . ' WHERE ' . $this->_prefix(); return 'DELETE FROM ' . $this->tableName() . ' WHERE ' . $this->_prefix();
} }
/** /**
* @param $attributes * @param $attributes
* @return array * @return array
@@ -146,8 +146,8 @@ class SqlBuilder extends Component
{ {
return array_keys(current($attributes)); return array_keys(current($attributes));
} }
/** /**
* @param array $attributes * @param array $attributes
* @param bool $isInsert * @param bool $isInsert
@@ -161,16 +161,16 @@ class SqlBuilder extends Component
$keys = []; $keys = [];
foreach ($attributes as $key => $value) { foreach ($attributes as $key => $value) {
if ($isInsert === true) { if ($isInsert === true) {
$keys[] = ':' . $key . $order; $keys[] = ':save' . $key . $order;
$params[$key . $order] = $value; $params['save' . $key . $order] = $value;
} else { } else {
[$keys, $params] = $this->resolveParams($key, $value, $order, $params, $keys); [$keys, $params] = $this->resolveParams($key, $value, $order, $params, $keys);
} }
} }
return [$keys, $params]; return [$keys, $params];
} }
/** /**
* @param string $key * @param string $key
* @param mixed $value * @param mixed $value
@@ -190,13 +190,13 @@ class SqlBuilder extends Component
) { ) {
$keys[] = $key . '=' . $key . ' ' . $value; $keys[] = $key . '=' . $key . ' ' . $value;
} else { } else {
$params[$key . $order] = $value; $params['update' . $key . $order] = $value;
$keys[] = $key . '=:' . $key . $order; $keys[] = $key . '=:update' . $key . $order;
} }
return [$keys, $params]; return [$keys, $params];
} }
/** /**
* @return string * @return string
* @throws Exception * @throws Exception
@@ -205,8 +205,8 @@ class SqlBuilder extends Component
{ {
return $this->_selectPrefix() . $this->_prefix() . $this->builderLimit($this->query); return $this->_selectPrefix() . $this->_prefix() . $this->builderLimit($this->query);
} }
/** /**
* @return string * @return string
* @throws Exception * @throws Exception
@@ -215,8 +215,8 @@ class SqlBuilder extends Component
{ {
return $this->_selectPrefix() . $this->_prefix() . $this->builderLimit($this->query); return $this->_selectPrefix() . $this->_prefix() . $this->builderLimit($this->query);
} }
/** /**
* @return string * @return string
* @throws Exception * @throws Exception
@@ -226,8 +226,8 @@ class SqlBuilder extends Component
$this->query->select('COUNT(*)'); $this->query->select('COUNT(*)');
return $this->_selectPrefix() . $this->_prefix() . $this->builderLimit($this->query); return $this->_selectPrefix() . $this->_prefix() . $this->builderLimit($this->query);
} }
/** /**
* @param $table * @param $table
* @return string * @return string
@@ -236,8 +236,8 @@ class SqlBuilder extends Component
{ {
return 'SHOW FULL FIELDS FROM ' . $table; return 'SHOW FULL FIELDS FROM ' . $table;
} }
/** /**
* @return string * @return string
* @throws Exception * @throws Exception
@@ -256,7 +256,7 @@ class SqlBuilder extends Component
} }
return $select; return $select;
} }
/** /**
* @return string * @return string
* @throws Exception * @throws Exception
@@ -275,8 +275,8 @@ class SqlBuilder extends Component
} }
return $select; return $select;
} }
/** /**
* @param false $isCount * @param false $isCount
* @return string * @return string
@@ -289,8 +289,8 @@ class SqlBuilder extends Component
} }
return $this->count(); return $this->count();
} }
/** /**
* @return string * @return string
* @throws Exception * @throws Exception
@@ -299,8 +299,8 @@ class SqlBuilder extends Component
{ {
return sprintf('TRUNCATE %s', $this->tableName()); return sprintf('TRUNCATE %s', $this->tableName());
} }
/** /**
* @return string * @return string
* @throws Exception * @throws Exception
@@ -309,8 +309,8 @@ class SqlBuilder extends Component
{ {
return $this->where($this->query->where); return $this->where($this->query->where);
} }
/** /**
* @return string * @return string
* @throws Exception * @throws Exception
@@ -329,5 +329,5 @@ class SqlBuilder extends Component
return $this->query->from; return $this->query->from;
} }
} }
} }