变更
This commit is contained in:
+261
-263
@@ -21,95 +21,95 @@ use Kiri\Abstracts\Component;
|
|||||||
class ActiveQuery extends Component implements ISqlBuilder
|
class ActiveQuery extends Component implements ISqlBuilder
|
||||||
{
|
{
|
||||||
|
|
||||||
use QueryTrait;
|
use QueryTrait;
|
||||||
|
|
||||||
/** @var array */
|
/** @var array */
|
||||||
public array $with = [];
|
public array $with = [];
|
||||||
|
|
||||||
/** @var bool */
|
/** @var bool */
|
||||||
public bool $asArray = FALSE;
|
public bool $asArray = FALSE;
|
||||||
|
|
||||||
/** @var bool */
|
/** @var bool */
|
||||||
public bool $useCache = FALSE;
|
public bool $useCache = FALSE;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Connection|null
|
* @var Connection|null
|
||||||
*/
|
*/
|
||||||
public ?Connection $db = NULL;
|
public ?Connection $db = NULL;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var array
|
* @var array
|
||||||
* 参数绑定
|
* 参数绑定
|
||||||
*/
|
*/
|
||||||
public array $attributes = [];
|
public array $attributes = [];
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Comply constructor.
|
* Comply constructor.
|
||||||
* @param $model
|
* @param $model
|
||||||
* @param array $config
|
* @param array $config
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function __construct($model, array $config = [])
|
public function __construct($model, array $config = [])
|
||||||
{
|
{
|
||||||
$this->modelClass = $model;
|
$this->modelClass = $model;
|
||||||
|
|
||||||
$this->builder = SqlBuilder::builder($this);
|
$this->builder = SqlBuilder::builder($this);
|
||||||
parent::__construct($config);
|
parent::__construct($config);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 清除不完整数据
|
* 清除不完整数据
|
||||||
*/
|
*/
|
||||||
public function clear()
|
public function clear()
|
||||||
{
|
{
|
||||||
$this->db = NULL;
|
$this->db = NULL;
|
||||||
$this->useCache = FALSE;
|
$this->useCache = FALSE;
|
||||||
$this->with = [];
|
$this->with = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $key
|
* @param $key
|
||||||
* @param $value
|
* @param $value
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function addParam($key, $value): static
|
public function addParam($key, $value): static
|
||||||
{
|
{
|
||||||
$this->attributes[$key] = $value;
|
$this->attributes[$key] = $value;
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $size
|
* @param int $size
|
||||||
* @param int $page
|
* @param int $page
|
||||||
* @return array
|
* @return array
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
#[ArrayShape([])]
|
#[ArrayShape([])]
|
||||||
public function pagination(int $size = 20, int $page = 1): array
|
public function pagination(int $size = 20, int $page = 1): array
|
||||||
{
|
{
|
||||||
$page = max(1, $page);
|
$page = max(1, $page);
|
||||||
$size = max(1, $size);
|
$size = max(1, $size);
|
||||||
|
|
||||||
|
$offset = ($page - 1) * $size;
|
||||||
|
|
||||||
|
$count = $this->count();
|
||||||
|
$lists = $this->limit($offset, $size)->get()->toArray();
|
||||||
|
return [
|
||||||
|
'code' => 0,
|
||||||
|
'message' => 'ok',
|
||||||
|
'size' => $size,
|
||||||
|
'page' => $page,
|
||||||
|
'count' => $count,
|
||||||
|
'next' => max($page + 1, 1),
|
||||||
|
'prev' => max($page - 1, 1),
|
||||||
|
'param' => $lists,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
$offset = ($page - 1) * $size;
|
|
||||||
|
|
||||||
$count = $this->count();
|
|
||||||
$lists = $this->limit($offset, $size)->get()->toArray();
|
|
||||||
return [
|
|
||||||
'code' => 0,
|
|
||||||
'message' => 'ok',
|
|
||||||
'size' => $size,
|
|
||||||
'page' => $page,
|
|
||||||
'count' => $count,
|
|
||||||
'next' => max($page + 1, 1),
|
|
||||||
'prev' => max($page - 1, 1),
|
|
||||||
'param' => $lists,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param bool $asArray
|
* @param bool $asArray
|
||||||
* @return static
|
* @return static
|
||||||
@@ -121,226 +121,224 @@ class ActiveQuery extends Component implements ISqlBuilder
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $values
|
* @param array $values
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function addParams(array $values): static
|
public function addParams(array $values): static
|
||||||
{
|
{
|
||||||
foreach ($values as $key => $val) {
|
foreach ($values as $key => $val) {
|
||||||
$this->addParam($key, $val);
|
$this->addParam($key, $val);
|
||||||
}
|
}
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $name
|
* @param array $name
|
||||||
* @return $this
|
* @return $this
|
||||||
*/
|
*/
|
||||||
public function with(array $name): static
|
public function with(array $name): static
|
||||||
{
|
{
|
||||||
foreach ($name as $val) {
|
foreach ($name as $val) {
|
||||||
$this->with[] = $val;
|
$this->with[] = $val;
|
||||||
}
|
}
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $sql
|
* @param $sql
|
||||||
* @param array $params
|
* @param array $params
|
||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function execute($sql, array $params = []): Command
|
public function execute($sql, array $params = []): Command
|
||||||
{
|
{
|
||||||
return $this->modelClass->getConnection()->createCommand($sql, $params);
|
return $this->modelClass->getConnection()->createCommand($sql, $params);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return ModelInterface|null
|
|
||||||
* @throws
|
|
||||||
*/
|
|
||||||
public function first(): ModelInterface|null
|
|
||||||
{
|
|
||||||
$data = $this->execute($this->builder->one())->one();
|
|
||||||
if (is_array($data)) {
|
|
||||||
return $this->populate($data);
|
|
||||||
} else {
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string
|
* @return ModelInterface|null
|
||||||
* @throws
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function toSql(): string
|
public function first(): ModelInterface|null
|
||||||
{
|
{
|
||||||
return $this->builder->get();
|
$data = $this->limit(0, 1)->execute($this->builder->one(), $this->attributes)->one();
|
||||||
}
|
if (is_array($data)) {
|
||||||
|
return $this->populate($data);
|
||||||
|
} else {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array|Collection
|
* @return string
|
||||||
*/
|
* @throws
|
||||||
public function get(): Collection|array
|
*/
|
||||||
{
|
public function toSql(): string
|
||||||
return $this->all();
|
{
|
||||||
}
|
return $this->builder->get();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws
|
* @return array|Collection
|
||||||
*/
|
*/
|
||||||
public function flush(): array|bool|int|string|null
|
public function get(): Collection|array
|
||||||
{
|
{
|
||||||
return $this->execute($this->builder->truncate())->exec();
|
return $this->all();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param int $size
|
* @throws
|
||||||
* @param callable $callback
|
*/
|
||||||
* @param int $offset
|
public function flush(): array|bool|int|string|null
|
||||||
* @return Pagination
|
{
|
||||||
* @throws
|
return $this->execute($this->builder->truncate())->exec();
|
||||||
*/
|
}
|
||||||
public function page(int $size, callable $callback, int $offset = 0): Pagination
|
|
||||||
{
|
|
||||||
$pagination = new Pagination($this);
|
|
||||||
$pagination->setOffset($offset);
|
|
||||||
$pagination->setLimit($size);
|
|
||||||
$pagination->setCallback($callback);
|
|
||||||
return $pagination;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param string $field
|
|
||||||
* @param string $setKey
|
|
||||||
*
|
|
||||||
* @return array|null
|
|
||||||
* @throws
|
|
||||||
*/
|
|
||||||
public function column(string $field, string $setKey = ''): ?array
|
|
||||||
{
|
|
||||||
return $this->all()->column($field, $setKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param int $size
|
||||||
|
* @param callable $callback
|
||||||
|
* @param int $offset
|
||||||
|
* @return Pagination
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function page(int $size, callable $callback, int $offset = 0): Pagination
|
||||||
|
{
|
||||||
|
$pagination = new Pagination($this);
|
||||||
|
$pagination->setOffset($offset);
|
||||||
|
$pagination->setLimit($size);
|
||||||
|
$pagination->setCallback($callback);
|
||||||
|
return $pagination;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array|Collection
|
* @param string $field
|
||||||
* @throws
|
* @param string $setKey
|
||||||
*/
|
*
|
||||||
public function all(): Collection|array
|
* @return array|null
|
||||||
{
|
* @throws
|
||||||
if (!($data = $this->execute($this->builder->all())->all())) {
|
*/
|
||||||
return new Collection($this, [], $this->modelClass);
|
public function column(string $field, string $setKey = ''): ?array
|
||||||
}
|
{
|
||||||
|
return $this->all()->column($field, $setKey);
|
||||||
$this->getWith($this->modelClass);
|
}
|
||||||
$collect = new Collection($this, $data, $this->modelClass);
|
|
||||||
|
|
||||||
return $this->asArray ? $collect->toArray() : $collect;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $data
|
|
||||||
* @return ModelInterface
|
|
||||||
* @throws
|
|
||||||
*/
|
|
||||||
public function populate($data): ModelInterface
|
|
||||||
{
|
|
||||||
return $this->getWith($this->modelClass::populate($data));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ModelInterface $model
|
* @return array|Collection
|
||||||
* @return ModelInterface
|
* @throws
|
||||||
*/
|
*/
|
||||||
public function getWith(ModelInterface $model): ModelInterface
|
public function all(): Collection|array
|
||||||
{
|
{
|
||||||
return $model->setWith($this->with);
|
if (!($data = $this->execute($this->builder->all())->all())) {
|
||||||
}
|
return new Collection($this, [], $this->modelClass);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
$this->getWith($this->modelClass);
|
||||||
* @return int
|
$collect = new Collection($this, $data, $this->modelClass);
|
||||||
* @throws
|
|
||||||
*/
|
return $this->asArray ? $collect->toArray() : $collect;
|
||||||
public function count(): int
|
}
|
||||||
{
|
|
||||||
$data = $this->execute($this->builder->count())->one();
|
/**
|
||||||
if ($data && is_array($data)) {
|
* @param $data
|
||||||
return (int)array_shift($data);
|
* @return ModelInterface
|
||||||
}
|
* @throws
|
||||||
return 0;
|
*/
|
||||||
}
|
public function populate($data): ModelInterface
|
||||||
|
{
|
||||||
|
return $this->getWith($this->modelClass::populate($data));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $data
|
* @param ModelInterface $model
|
||||||
* @return bool
|
* @return ModelInterface
|
||||||
* @throws
|
*/
|
||||||
*/
|
public function getWith(ModelInterface $model): ModelInterface
|
||||||
public function update(array $data): bool
|
{
|
||||||
{
|
return $model->setWith($this->with);
|
||||||
$generate = $this->builder->update($data);
|
}
|
||||||
if (is_bool($generate)) {
|
|
||||||
return $generate;
|
|
||||||
}
|
|
||||||
return (bool)$this->execute(...$generate)->exec();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $data
|
* @return int
|
||||||
* @return bool
|
* @throws
|
||||||
* @throws
|
*/
|
||||||
*/
|
public function count(): int
|
||||||
public function insert(array $data): bool
|
{
|
||||||
{
|
$data = $this->execute($this->builder->count())->one();
|
||||||
[$sql, $params] = $this->builder->insert($data, TRUE);
|
if ($data && is_array($data)) {
|
||||||
|
return (int)array_shift($data);
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return (bool)$this->execute($sql, $params)->exec();
|
/**
|
||||||
}
|
* @param array $data
|
||||||
|
* @return bool
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function update(array $data): bool
|
||||||
|
{
|
||||||
|
$generate = $this->builder->update($data);
|
||||||
|
if (is_bool($generate)) {
|
||||||
|
return $generate;
|
||||||
|
}
|
||||||
|
return (bool)$this->execute(...$generate)->exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $data
|
||||||
|
* @return bool
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function insert(array $data): bool
|
||||||
|
{
|
||||||
|
[$sql, $params] = $this->builder->insert($data, TRUE);
|
||||||
|
|
||||||
|
|
||||||
|
return (bool)$this->execute($sql, $params)->exec();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $filed
|
||||||
|
*
|
||||||
|
* @return null
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function value($filed)
|
||||||
|
{
|
||||||
|
return $this->first()[$filed] ?? NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return bool
|
||||||
|
* @throws
|
||||||
|
*/
|
||||||
|
public function exists(): bool
|
||||||
|
{
|
||||||
|
return !empty($this->execute($this->builder->one())->fetchColumn());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param $filed
|
|
||||||
*
|
|
||||||
* @return null
|
|
||||||
* @throws
|
|
||||||
*/
|
|
||||||
public function value($filed)
|
|
||||||
{
|
|
||||||
return $this->first()[$filed] ?? NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @return bool
|
|
||||||
* @throws
|
|
||||||
*/
|
|
||||||
public function exists(): bool
|
|
||||||
{
|
|
||||||
return !empty($this->execute($this->builder->one())->fetchColumn());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param bool $getSql
|
* @param bool $getSql
|
||||||
* @return bool|string
|
* @return bool|string
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function delete(bool $getSql = FALSE): bool|string
|
public function delete(bool $getSql = FALSE): bool|string
|
||||||
{
|
{
|
||||||
$sql = $this->builder->delete();
|
$sql = $this->builder->delete();
|
||||||
if ($getSql === FALSE) {
|
if ($getSql === FALSE) {
|
||||||
return (bool)$this->execute($sql)->delete();
|
return (bool)$this->execute($sql)->delete();
|
||||||
}
|
}
|
||||||
return $sql;
|
return $sql;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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 . ')';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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 . ')';
|
||||||
|
|||||||
@@ -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
@@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
+40
-40
@@ -22,8 +22,8 @@ use ReflectionException;
|
|||||||
*/
|
*/
|
||||||
trait Builder
|
trait Builder
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $alias
|
* @param $alias
|
||||||
* @return string
|
* @return string
|
||||||
@@ -32,7 +32,7 @@ trait Builder
|
|||||||
{
|
{
|
||||||
return " AS " . $alias;
|
return " AS " . $alias;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $table
|
* @param $table
|
||||||
* @return string
|
* @return string
|
||||||
@@ -45,7 +45,7 @@ trait Builder
|
|||||||
}
|
}
|
||||||
return " FROM " . $table;
|
return " FROM " . $table;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $join
|
* @param $join
|
||||||
* @return string
|
* @return string
|
||||||
@@ -57,8 +57,8 @@ trait Builder
|
|||||||
}
|
}
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $select
|
* @param string $select
|
||||||
* @return string
|
* @return string
|
||||||
@@ -67,8 +67,8 @@ trait Builder
|
|||||||
{
|
{
|
||||||
return "SELECT " . $select;
|
return "SELECT " . $select;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $group
|
* @param $group
|
||||||
* @return string
|
* @return string
|
||||||
@@ -80,7 +80,7 @@ trait Builder
|
|||||||
}
|
}
|
||||||
return ' GROUP BY ' . $group;
|
return ' GROUP BY ' . $group;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $order
|
* @param $order
|
||||||
* @return string
|
* @return string
|
||||||
@@ -93,7 +93,7 @@ trait Builder
|
|||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param ActiveQuery|Query $query
|
* @param ActiveQuery|Query $query
|
||||||
* @param bool $hasLimit
|
* @param bool $hasLimit
|
||||||
@@ -107,33 +107,31 @@ trait Builder
|
|||||||
return ' LIMIT ' . $query->limit;
|
return ' LIMIT ' . $query->limit;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @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
|
||||||
{
|
{
|
||||||
$_tmp = [];
|
if (count($where) < 1) {
|
||||||
if (empty($where)) return '';
|
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[] = $_value;
|
|
||||||
}
|
}
|
||||||
if (!empty($_tmp)) {
|
$_tmp = [];
|
||||||
|
foreach ($where as $key => $value) {
|
||||||
|
$_tmp[] = $this->resolveCondition($key, $value, $_tmp);
|
||||||
|
}
|
||||||
|
if (count($_tmp) > 0) {
|
||||||
return implode(' AND ', $_tmp);
|
return implode(' AND ', $_tmp);
|
||||||
} else {
|
} else {
|
||||||
return '';
|
return '';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $field
|
* @param $field
|
||||||
* @param $condition
|
* @param $condition
|
||||||
@@ -152,8 +150,8 @@ trait Builder
|
|||||||
return $this->_arrayMap($condition, $_tmp);
|
return $this->_arrayMap($condition, $_tmp);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $condition
|
* @param $condition
|
||||||
* @param $array
|
* @param $array
|
||||||
@@ -176,10 +174,10 @@ trait Builder
|
|||||||
$builder->oldParams = $array;
|
$builder->oldParams = $array;
|
||||||
} else if (isset(ConditionClassMap::$conditionMap[$stroppier])) {
|
} else if (isset(ConditionClassMap::$conditionMap[$stroppier])) {
|
||||||
$defaultConfig = ConditionClassMap::$conditionMap[$stroppier];
|
$defaultConfig = ConditionClassMap::$conditionMap[$stroppier];
|
||||||
|
|
||||||
$class = $defaultConfig['class'];
|
$class = $defaultConfig['class'];
|
||||||
unset($defaultConfig['class']);
|
unset($defaultConfig['class']);
|
||||||
|
|
||||||
$builder = Kiri::getDi()->make($class, [], $defaultConfig);
|
$builder = Kiri::getDi()->make($class, [], $defaultConfig);
|
||||||
$builder->setValue($condition[2]);
|
$builder->setValue($condition[2]);
|
||||||
$builder->setColumn($condition[1]);
|
$builder->setColumn($condition[1]);
|
||||||
@@ -187,13 +185,16 @@ trait Builder
|
|||||||
$builder = Kiri::getDi()->get(HashCondition::class);
|
$builder = Kiri::getDi()->get(HashCondition::class);
|
||||||
$builder->setValue($condition);
|
$builder->setValue($condition);
|
||||||
}
|
}
|
||||||
|
|
||||||
$array[] = $builder->builder();
|
$array[] = $builder->builder();
|
||||||
|
|
||||||
return implode(' AND ', $array);
|
return implode(' AND ', $array);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public array $params = [];
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $condition
|
* @param $condition
|
||||||
* @return array
|
* @return array
|
||||||
@@ -202,16 +203,15 @@ 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $_array;
|
return $_array;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+23
-9
@@ -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 . '\'';
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user