This commit is contained in:
2023-04-07 18:23:27 +08:00
parent 6d122c4ae1
commit 5d41c7219f
9 changed files with 339 additions and 328 deletions
+1 -3
View File
@@ -158,14 +158,13 @@ class ActiveQuery extends Component implements ISqlBuilder
} }
/** /**
* @return ModelInterface|null * @return ModelInterface|null
* @throws * @throws
*/ */
public function first(): ModelInterface|null public function first(): ModelInterface|null
{ {
$data = $this->execute($this->builder->one())->one(); $data = $this->limit(0, 1)->execute($this->builder->one(), $this->attributes)->one();
if (is_array($data)) { if (is_array($data)) {
return $this->populate($data); return $this->populate($data);
} else { } else {
@@ -231,7 +230,6 @@ class ActiveQuery extends Component implements ISqlBuilder
} }
/** /**
* @return array|Collection * @return array|Collection
* @throws * @throws
+1 -1
View File
@@ -18,7 +18,7 @@ class DefaultCondition extends Condition
*/ */
#[Pure] public function builder(): string #[Pure] public function builder(): string
{ {
return sprintf('%s %s %s', $this->column, $this->opera, addslashes($this->value)); return $this->column . ' ' . $this->opera . ' ' . addslashes($this->value);
} }
} }
+4 -5
View File
@@ -12,17 +12,16 @@ class HashCondition extends Condition
/** /**
* @return string * @return string
* @throws \Exception
*/ */
public function builder(): string public function builder(): string
{ {
$array = []; $array = [];
if (empty($this->value)) { if (count($this->value) < 1) {
return ''; throw new \Exception('Builder data by a empty array.');
} }
foreach ($this->value as $key => $value) { foreach ($this->value as $key => $value) {
if (is_null($value)) continue; $array[] = $key . '=' . addslashes($value);
$array[] = sprintf("%s = '%s'", $key, addslashes($value));
} }
return implode(' AND ', $array); return implode(' AND ', $array);
} }
+2 -2
View File
@@ -22,9 +22,9 @@ class InCondition extends Condition
#[Pure] public function builder(): string #[Pure] public function builder(): string
{ {
if (is_array($this->value)) { if (is_array($this->value)) {
return sprintf('%s IN (%s)', $this->column, implode(',', $this->value)); return $this->column . ' IN (' . implode(',', $this->value) . ')';
} else { } else {
return sprintf('%s IN (%s)', $this->column, $this->value); return $this->column . ' IN (' . $this->value . ')';
} }
} }
+2 -1
View File
@@ -15,11 +15,12 @@ class NotInCondition extends Condition
/** /**
* @return string|null * @return string|null
* @throws \Exception
*/ */
#[Pure] public function builder(): ?string #[Pure] public function builder(): ?string
{ {
if (!is_array($this->value)) { if (!is_array($this->value)) {
return null; throw new \Exception('Builder data by a empty string. need array');
} }
$value = '\'' . implode('\',\'', $this->value) . '\''; $value = '\'' . implode('\',\'', $this->value) . '\'';
return '`' . $this->column . '` not in(' . $value . ')'; return '`' . $this->column . '` not in(' . $value . ')';
+1 -1
View File
@@ -21,7 +21,7 @@ class OrCondition extends Condition
*/ */
#[Pure] public function builder(): string #[Pure] public function builder(): string
{ {
return sprintf('(%s) OR %s', implode(' AND ', $this->oldParams), addslashes($this->value)); return '(' . implode(' AND ', $this->oldParams) . ') OR ' . addslashes($this->value);
} }
} }
+5 -6
View File
@@ -203,7 +203,6 @@ class SqlBuilder extends Component
*/ */
public function one(): string public function one(): string
{ {
$this->query->limit(0, 1);
return $this->_selectPrefix() . $this->_prefix() . $this->builderLimit($this->query); return $this->_selectPrefix() . $this->_prefix() . $this->builderLimit($this->query);
} }
@@ -321,14 +320,14 @@ class SqlBuilder extends Component
*/ */
public function tableName(): string public function tableName(): string
{ {
if ($this->query->from === null) {
return $this->query->modelClass->getTable();
}
if ($this->query->from instanceof \Closure) { if ($this->query->from instanceof \Closure) {
$this->query->from = '(' . $this->query->makeClosureFunction($this->query->from) . ')'; return $this->query->from = '(' . $this->query->makeClosureFunction($this->query->from) . ')';
} }
if ($this->query->from instanceof ActiveQuery) { if ($this->query->from instanceof ActiveQuery) {
$this->query->from = '(' . SqlBuilder::builder($this->query->from)->get($this->query->from) . ')'; return $this->query->from = '(' . SqlBuilder::builder($this->query->from)->get($this->query->from) . ')';
}
if ($this->query->from == "") {
return $this->query->modelClass->getTable();
} else { } else {
return $this->query->from; return $this->query->from;
} }
+14 -14
View File
@@ -110,23 +110,21 @@ trait Builder
/** /**
* @param $where * @param array $where
* @return string * @return string
* @throws Exception * @throws NotFindClassException
* @throws ReflectionException
*/ */
private function where($where): string private function where(array $where): string
{ {
if (count($where) < 1) {
return '';
}
$_tmp = []; $_tmp = [];
if (empty($where)) return '';
if (is_string($where)) return $where;
foreach ($where as $key => $value) { foreach ($where as $key => $value) {
if (is_null($value)) continue; $_tmp[] = $this->resolveCondition($key, $value, $_tmp);
if (($_value = $this->resolveCondition($key, $value, $_tmp)) == '') {
continue;
} }
$_tmp[] = $_value; if (count($_tmp) > 0) {
}
if (!empty($_tmp)) {
return implode(' AND ', $_tmp); return implode(' AND ', $_tmp);
} else { } else {
return ''; return '';
@@ -194,6 +192,9 @@ trait Builder
} }
public array $params = [];
/** /**
* @param $condition * @param $condition
* @return array * @return array
@@ -202,10 +203,9 @@ trait Builder
{ {
$_array = []; $_array = [];
foreach ($condition as $key => $value) { foreach ($condition as $key => $value) {
if (is_null($value)) continue;
$value = is_numeric($value) ? $value : '\'' . $value . '\'';
if (!is_numeric($key)) { if (!is_numeric($key)) {
$_array[] = $key . '=' . $value; $this->query->bindParam(':' . $key, $value);
$_array[] = $key . '=:' . $key;
} else { } else {
$_array[] = $value; $_array[] = $value;
} }
+23 -9
View File
@@ -34,7 +34,7 @@ trait QueryTrait
public int $offset = 0; public int $offset = 0;
public int $limit = 500; public int $limit = 500;
public string $group = ''; public string $group = '';
public string|Closure|ActiveQuery|null $from = ''; public string|Closure|ActiveQuery|null $from = null;
public string $alias = 't1'; public string $alias = 't1';
public array $filter = []; public array $filter = [];
@@ -757,7 +757,9 @@ trait QueryTrait
return $this; return $this;
} }
$this->where[] = $column . ' BETWEEN ' . $start . ' AND ' . $end; $this->bindParam(':between_start' . $column, $start);
$this->bindParam(':between_end' . $column, $end);
$this->where[] = $column . ' BETWEEN :not_between_start' . $column . ' AND :not_between_end' . $column;
return $this; return $this;
} }
@@ -774,7 +776,9 @@ trait QueryTrait
return $this; return $this;
} }
$this->where[] = $column . 'NOT BETWEEN' . $start . ' AND ' . $end; $this->bindParam(':not_between_start' . $column, $start);
$this->bindParam(':not_between_end' . $column, $end);
$this->where[] = $column . ' NOT BETWEEN :not_between_start' . $column . ' AND :not_between_end' . $column;
return $this; return $this;
} }
@@ -786,13 +790,24 @@ trait QueryTrait
*/ */
public function bindParams(?array $params = []): static public function bindParams(?array $params = []): static
{ {
if (empty($params)) { if ($params === null) {
return $this; return $this;
} }
$this->attributes = $params; $this->attributes = $params;
return $this; return $this;
} }
/**
* @param string $key
* @param mixed $value
* @return $this
*/
public function bindParam(string $key, mixed $value): static
{
$this->attributes[$key] = $value;
return $this;
}
/** /**
* @param array|string $column * @param array|string $column
* @param string $opera * @param string $opera
@@ -809,7 +824,8 @@ trait QueryTrait
$this->where[] = $column; $this->where[] = $column;
} else { } else {
[$column, $opera, $value] = $this->opera(...func_get_args()); [$column, $opera, $value] = $this->opera(...func_get_args());
$this->where[] = "$column $opera $value"; $this->bindParam(':' . $column, $value);
$this->where[] = $column . ' ' . $opera . ':' . $column;
} }
return $this; return $this;
} }
@@ -900,10 +916,8 @@ trait QueryTrait
*/ */
private function sprintf($column, $value, string $opera = '='): string private function sprintf($column, $value, string $opera = '='): string
{ {
if (is_string($value)) { $this->bindParam(':' . $column, $value);
$value = addslashes($value); return $column . ' ' . $opera . ' :' . $column;
}
return $column . ' ' . $opera . ' \'' . $value . '\'';
} }