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
+78 -118
View File
@@ -16,13 +16,13 @@ use Kiri\Abstracts\Component;
*/
class SqlBuilder extends Component
{
use Builder;
public ActiveQuery|Query|null $query;
/**
* @param ISqlBuilder|null $query
* @return $this
@@ -32,8 +32,8 @@ class SqlBuilder extends Component
{
return new static(['query' => $query]);
}
/**
* @return string
* @throws Exception
@@ -42,8 +42,8 @@ class SqlBuilder extends Component
{
return $this->conditionToString();
}
/**
* @param array $compiler
* @return string
@@ -53,8 +53,8 @@ class SqlBuilder extends Component
{
return $this->where($compiler);
}
/**
* @param array $attributes
* @return bool|array
@@ -63,11 +63,11 @@ class SqlBuilder extends Component
public function update(array $attributes): bool|array
{
[$string, $array] = $this->builderParams($attributes);
return $this->__updateBuilder($string, $array);
}
/**
* @param array $attributes
* @param string $opera
@@ -82,8 +82,8 @@ class SqlBuilder extends Component
}
return $this->__updateBuilder($string, []);
}
/**
* @param array $string
* @param array $params
@@ -95,19 +95,14 @@ class SqlBuilder extends Component
if (empty($string)) {
return $this->logger->addError('None data update.');
}
$condition = $this->conditionToString();
if (!empty($condition)) {
$condition = ' WHERE ' . $condition;
}
$update = 'UPDATE ' . $this->tableName() . ' SET ' . implode(',', $string) . $condition;
$update = 'UPDATE ' . $this->tableName() . ' SET ' . implode(',', $string) . $this->_prefix();
$update .= $this->builderLimit($this->query, false);
return [$update, $params];
}
/**
* @param array $attributes
* @param false $isBatch
@@ -121,33 +116,29 @@ class SqlBuilder extends Component
$attributes = [$attributes];
}
$update .= '(' . implode(',', $this->getFields($attributes)) . ') VALUES ';
$order = 0;
$keys = $params = [];
foreach ($attributes as $attribute) {
[$_keys, $params] = $this->builderParams($attribute, true, $params, $order);
$keys[] = implode(',', $_keys);
$order++;
}
return [$update . '(' . implode('),(', $keys) . ')', $params];
}
/**
* @return string
* @throws Exception
*/
public function delete(): string
{
$delete = sprintf('DELETE FROM %s ', $this->query->modelClass->getTable());
$this->query->from = null;
return $delete . ' WHERE ' . $this->_prefix(true);
return 'DELETE FROM ' . $this->tableName() . ' WHERE ' . $this->_prefix();
}
/**
* @param $attributes
* @return array
@@ -156,8 +147,8 @@ class SqlBuilder extends Component
{
return array_keys(current($attributes));
}
/**
* @param array $attributes
* @param bool $isInsert
@@ -179,8 +170,8 @@ class SqlBuilder extends Component
}
return [$keys, $params];
}
/**
* @param string $key
* @param mixed $value
@@ -205,8 +196,8 @@ class SqlBuilder extends Component
}
return [$keys, $params];
}
/**
* @return string
* @throws Exception
@@ -214,39 +205,31 @@ class SqlBuilder extends Component
public function one(): string
{
$this->query->limit(0, 1);
if (empty($this->query->from) && !empty($this->query->modelClass)) {
$this->query->from($this->query->getTable());
}
return $this->_prefix(true);
return $this->_selectPrefix() . $this->_prefix();
}
/**
* @return string
* @throws Exception
*/
public function all(): string
{
if (empty($this->query->from) && !empty($this->query->modelClass)) {
$this->query->from($this->query->getTable());
}
return $this->_prefix(true);
return $this->_selectPrefix() . $this->_prefix();
}
/**
* @return string
* @throws Exception
*/
public function count(): string
{
if (empty($this->query->from) && !empty($this->query->modelClass)) {
$this->query->from($this->query->getTable());
}
return $this->_prefix(false, true);
$this->query->select('COUNT(*)');
return $this->_selectPrefix() . $this->_prefix();
}
/**
* @param $table
* @return string
@@ -255,74 +238,50 @@ class SqlBuilder extends Component
{
return 'SHOW FULL FIELDS FROM ' . $table;
}
/**
* @param bool $hasOrder
* @param bool $isCount
* @return string
* @throws Exception
*/
private function _prefix(bool $hasOrder = false, bool $isCount = false): string
private function _prefix(): string
{
$select = '';
if (!empty($this->query->from)) {
$select = $this->_selectPrefix($isCount);
if (($condition = $this->conditionToString()) != '') {
$select .= " WHERE ${$condition}";
}
$select = $this->_wherePrefix($select);
if (!empty($this->query->attributes) && is_array($this->query->attributes)) {
if (count($this->query->attributes) > 0) {
$select = strtr($select, $this->query->attributes);
}
if (!empty($this->query->group)) {
$select .= $this->builderGroup($this->query->group);
if ($this->query->group != "") {
$select .= ' GROUP BY ' . $this->query->group;
}
if ($hasOrder === true && !empty($this->query->order)) {
$select .= $this->builderOrder($this->query->order);
if ($this->query->order != "") {
$select .= ' ORDER BY ' . implode(',', $this->query->order);
}
$sql = $select . $this->builderLimit($this->query);
if ($this->query->lock) {
$sql .= ' FOR UPDATE';
}
return $sql;
return $select . $this->builderLimit($this->query);
}
/**
* @param $select
* @return string
* @throws Exception
*/
private function _wherePrefix($select): string
private function _selectPrefix(): string
{
$condition = $this->conditionToString();
if (empty($condition)) {
return $select;
} else if (empty($select)) {
return $condition;
if (count($this->query->select) < 1) {
$this->query->select = ['*'];
}
return sprintf('%s WHERE %s', $select, $condition);
}
/**
* @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);
$select = "SELECT " . implode(',', $this->query->select) . " FROM " . $this->tableName();
if ($this->query->alias != "") {
$select .= " AS " . $this->query->alias;
}
if (!empty($this->query->join)) {
$select .= $this->builderJoin($this->query->join);
if (count($this->query->join) > 0) {
$select .= ' ' . implode(' ', $this->query->join);
}
return $select;
}
/**
* @param false $isCount
* @return string
@@ -335,8 +294,8 @@ class SqlBuilder extends Component
}
return $this->count();
}
/**
* @return string
* @throws Exception
@@ -345,8 +304,8 @@ class SqlBuilder extends Component
{
return sprintf('TRUNCATE %s', $this->tableName());
}
/**
* @return string
* @throws Exception
@@ -355,8 +314,8 @@ class SqlBuilder extends Component
{
return $this->where($this->query->where);
}
/**
* @return string
* @throws Exception
@@ -364,15 +323,16 @@ class SqlBuilder extends Component
public function tableName(): string
{
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) {
$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();
} else {
return $this->query->from;
}
return $this->query->from;
}
}
+39 -54
View File
@@ -22,8 +22,8 @@ use ReflectionException;
*/
trait Builder
{
/**
* @param $alias
* @return string
@@ -32,7 +32,7 @@ trait Builder
{
return " AS " . $alias;
}
/**
* @param $table
* @return string
@@ -45,7 +45,7 @@ trait Builder
}
return " FROM " . $table;
}
/**
* @param $join
* @return string
@@ -57,29 +57,18 @@ trait Builder
}
return '';
}
/**
* @param null $select
* @param bool $isCount
* @param string $select
* @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;
}
/**
* @param $group
* @return string
@@ -91,7 +80,7 @@ trait Builder
}
return ' GROUP BY ' . $group;
}
/**
* @param $order
* @return string
@@ -104,7 +93,7 @@ trait Builder
return '';
}
}
/**
* @param ActiveQuery|Query $query
* @param bool $hasLimit
@@ -112,16 +101,14 @@ trait Builder
*/
#[Pure] private function builderLimit(ActiveQuery|Query $query, bool $hasLimit = true): string
{
if (!is_numeric($query->limit) || $query->limit < 1) {
return "";
}
if ($query->offset !== null && $hasLimit) {
if ($hasLimit) {
return ' LIMIT ' . $query->offset . ',' . $query->limit;
} else {
return ' LIMIT ' . $query->limit;
}
return ' LIMIT ' . $query->limit;
}
/**
* @param $where
* @return string
@@ -134,19 +121,19 @@ trait Builder
if (is_string($where)) return $where;
foreach ($where as $key => $value) {
if (is_null($value)) continue;
$_value = $this->resolveCondition($key, $value, $_tmp);
if (empty($_value)) continue;
if (($_value = $this->resolveCondition($key, $value, $_tmp)) == '') {
continue;
}
$_tmp[] = $_value;
}
if (!empty($_tmp)) {
return implode(' AND ', $_tmp);
} else {
return '';
}
return '';
}
/**
* @param $field
* @param $condition
@@ -158,16 +145,15 @@ trait Builder
private function resolveCondition($field, $condition, $_tmp): string
{
if (is_string($field)) {
$_value = sprintf('%s = \'%s\'', $field, $condition);
return $field . ' = \'' . $condition . '\'';
} else if (is_string($condition)) {
$_value = $condition;
return $condition;
} else {
$_value = $this->_arrayMap($condition, $_tmp);
return $this->_arrayMap($condition, $_tmp);
}
return $_value;
}
/**
* @param $condition
* @param $array
@@ -190,10 +176,10 @@ trait Builder
$builder->oldParams = $array;
} else if (isset(ConditionClassMap::$conditionMap[$stroppier])) {
$defaultConfig = ConditionClassMap::$conditionMap[$stroppier];
$class = $defaultConfig['class'];
unset($defaultConfig['class']);
$builder = Kiri::getDi()->make($class, [], $defaultConfig);
$builder->setValue($condition[2]);
$builder->setColumn($condition[1]);
@@ -201,13 +187,13 @@ trait Builder
$builder = Kiri::getDi()->get(HashCondition::class);
$builder->setValue($condition);
}
$array[] = $builder->builder();
return implode(' AND ', $array);
}
/**
* @param $condition
* @return array
@@ -217,16 +203,15 @@ trait Builder
$_array = [];
foreach ($condition as $key => $value) {
if (is_null($value)) continue;
$value = is_numeric($value) ? $value : '\'' . $value . '\'';
if (!is_numeric($key)) {
$_array[] = sprintf('%s = %s', $key, $value);
$_array[] = $key . '=' . $value;
} else {
$_array[] = $value;
}
}
return $_array;
}
}
+6 -6
View File
@@ -31,8 +31,8 @@ trait QueryTrait
public array $select = [];
public array $join = [];
public array $order = [];
public ?int $offset = NULL;
public ?int $limit = NULL;
public int $offset = 0;
public int $limit = 500;
public string $group = '';
public string|Closure|ActiveQuery|null $from = '';
public string $alias = 't1';
@@ -54,14 +54,14 @@ trait QueryTrait
/**
* clear
*/
public function clear()
public function clear(): void
{
$this->where = [];
$this->select = [];
$this->join = [];
$this->order = [];
$this->offset = NULL;
$this->limit = NULL;
$this->offset = 0;
$this->limit = 500;
$this->group = '';
$this->from = '';
$this->alias = 't1';
@@ -480,7 +480,7 @@ trait QueryTrait
public function select(array|string $column = '*'): static
{
if ($column == '*') {
$this->select = $column;
$this->select = [$column];
} else {
if (!is_array($column)) {
$column = explode(',', $column);