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
* @throws
*/
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)) {
return $this->populate($data);
} else {
@@ -231,7 +230,6 @@ class ActiveQuery extends Component implements ISqlBuilder
}
/**
* @return array|Collection
* @throws
+1 -1
View File
@@ -18,7 +18,7 @@ class DefaultCondition extends Condition
*/
#[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
* @throws \Exception
*/
public function builder(): string
{
$array = [];
if (empty($this->value)) {
return '';
if (count($this->value) < 1) {
throw new \Exception('Builder data by a empty array.');
}
foreach ($this->value as $key => $value) {
if (is_null($value)) continue;
$array[] = sprintf("%s = '%s'", $key, addslashes($value));
$array[] = $key . '=' . addslashes($value);
}
return implode(' AND ', $array);
}
+2 -2
View File
@@ -22,9 +22,9 @@ class InCondition extends Condition
#[Pure] public function builder(): string
{
if (is_array($this->value)) {
return sprintf('%s IN (%s)', $this->column, implode(',', $this->value));
return $this->column . ' IN (' . implode(',', $this->value) . ')';
} 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
* @throws \Exception
*/
#[Pure] public function builder(): ?string
{
if (!is_array($this->value)) {
return null;
throw new \Exception('Builder data by a empty string. need array');
}
$value = '\'' . implode('\',\'', $this->value) . '\'';
return '`' . $this->column . '` not in(' . $value . ')';
+1 -1
View File
@@ -21,7 +21,7 @@ class OrCondition extends Condition
*/
#[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
{
$this->query->limit(0, 1);
return $this->_selectPrefix() . $this->_prefix() . $this->builderLimit($this->query);
}
@@ -321,14 +320,14 @@ class SqlBuilder extends Component
*/
public function tableName(): string
{
if ($this->query->from === null) {
return $this->query->modelClass->getTable();
}
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) {
$this->query->from = '(' . SqlBuilder::builder($this->query->from)->get($this->query->from) . ')';
}
if ($this->query->from == "") {
return $this->query->modelClass->getTable();
return $this->query->from = '(' . SqlBuilder::builder($this->query->from)->get($this->query->from) . ')';
} else {
return $this->query->from;
}
+14 -14
View File
@@ -110,23 +110,21 @@ trait Builder
/**
* @param $where
* @param array $where
* @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 = [];
if (empty($where)) return '';
if (is_string($where)) return $where;
foreach ($where as $key => $value) {
if (is_null($value)) continue;
if (($_value = $this->resolveCondition($key, $value, $_tmp)) == '') {
continue;
$_tmp[] = $this->resolveCondition($key, $value, $_tmp);
}
$_tmp[] = $_value;
}
if (!empty($_tmp)) {
if (count($_tmp) > 0) {
return implode(' AND ', $_tmp);
} else {
return '';
@@ -194,6 +192,9 @@ trait Builder
}
public array $params = [];
/**
* @param $condition
* @return array
@@ -202,10 +203,9 @@ trait Builder
{
$_array = [];
foreach ($condition as $key => $value) {
if (is_null($value)) continue;
$value = is_numeric($value) ? $value : '\'' . $value . '\'';
if (!is_numeric($key)) {
$_array[] = $key . '=' . $value;
$this->query->bindParam(':' . $key, $value);
$_array[] = $key . '=:' . $key;
} else {
$_array[] = $value;
}
+23 -9
View File
@@ -34,7 +34,7 @@ trait QueryTrait
public int $offset = 0;
public int $limit = 500;
public string $group = '';
public string|Closure|ActiveQuery|null $from = '';
public string|Closure|ActiveQuery|null $from = null;
public string $alias = 't1';
public array $filter = [];
@@ -757,7 +757,9 @@ trait QueryTrait
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;
}
@@ -774,7 +776,9 @@ trait QueryTrait
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;
}
@@ -786,13 +790,24 @@ trait QueryTrait
*/
public function bindParams(?array $params = []): static
{
if (empty($params)) {
if ($params === null) {
return $this;
}
$this->attributes = $params;
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 string $opera
@@ -809,7 +824,8 @@ trait QueryTrait
$this->where[] = $column;
} else {
[$column, $opera, $value] = $this->opera(...func_get_args());
$this->where[] = "$column $opera $value";
$this->bindParam(':' . $column, $value);
$this->where[] = $column . ' ' . $opera . ':' . $column;
}
return $this;
}
@@ -900,10 +916,8 @@ trait QueryTrait
*/
private function sprintf($column, $value, string $opera = '='): string
{
if (is_string($value)) {
$value = addslashes($value);
}
return $column . ' ' . $opera . ' \'' . $value . '\'';
$this->bindParam(':' . $column, $value);
return $column . ' ' . $opera . ' :' . $column;
}