Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
liu21st committed Jan 6, 2022
1 parent 17accf9 commit 091ad5e
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 80 deletions.
33 changes: 3 additions & 30 deletions src/db/BaseQuery.php
Expand Up @@ -263,14 +263,7 @@ public function value(string $field, $default = null)
$result = $this->connection->value($this, $field, $default);

$array[$field] = $result;

if (!empty($this->options['json'])) {
$this->jsonResult($array, $this->options['json']);
}

if (!empty($this->options['with_attr'])) {
$array = $this->getResultAttr($array, $this->options['with_attr']);
}
$this->result($array);

return $array[$field];
}
Expand All @@ -287,15 +280,7 @@ public function column($field, string $key = ''): array
$result = $this->connection->column($this, $field, $key);

if (count($result) != count($result, 1)) {
foreach ($result as &$val) {
if (!empty($this->options['json'])) {
$this->jsonResult($val, $this->options['json']);
}

if (!empty($this->options['with_attr'])) {
$val = $this->getResultAttr($val, $this->options['with_attr']);
}
}
$this->resultSet($result, false);
}

return $result;
Expand Down Expand Up @@ -887,19 +872,7 @@ public function json(array $json = [], bool $assoc = false)
$this->options['json'] = $json;
$this->options['json_assoc'] = $assoc;

if ($this->model) {
return $this->filter(function ($result) use ($json, $assoc) {
if (!empty($json)) {
$this->jsonModelResult($result, $json, $assoc);
}
});
}

return $this->filter(function ($result) use ($json) {
if (!empty($json)) {
$this->jsonResult($result, $json);
}
});
return $this;
}

/**
Expand Down
105 changes: 63 additions & 42 deletions src/db/concern/ModelRelationQuery.php
Expand Up @@ -137,9 +137,8 @@ public function relation(array $relation)
return $this;
}

return $this->filter(function ($result, $options) use ($relation) {
$result->relationQuery($relation, $this->options['with_relation_attr']);
});
$this->options['relation'] = $relation;
return $this;
}

/**
Expand Down Expand Up @@ -206,15 +205,7 @@ public function withAttr($name, callable $callback = null)
}
}

if (empty($this->model)) {
return $this->filter(function ($result) {
return $this->getResultAttr($result, $this->options['with_attr']);
}, 'with_attr');
}

return $this->filter(function ($result) {
$result->withAttr($this->options['with_attr']);
}, 'with_attr');
return $this;
}

/**
Expand All @@ -229,14 +220,8 @@ public function with($with)
return $this;
}

$with = (array) $with;

$this->options['with'] = $with;
return $this->filter(function ($result) use ($with) {
if (empty($this->options['is_resultSet'])) {
$result->eagerlyResult($with, $this->options['with_relation_attr'], false, $this->options['with_cache'] ?? false);
}
}, 'with');
$this->options['with'] = (array) $with;
return $this;
}

/**
Expand Down Expand Up @@ -282,12 +267,7 @@ public function withJoin($with, string $joinType = '')
$this->via();
$this->options['with_join'] = $with;

return $this->filter(function ($result) use ($with) {
// JOIN预载入查询
if (empty($this->options['is_resultSet'])) {
$result->eagerlyResult($with, $this->options['with_relation_attr'], true, $this->options['with_cache'] ?? false);
}
}, 'with_join');
return $this;
}

/**
Expand All @@ -306,12 +286,8 @@ protected function withAggregate($relations, string $aggregate = 'count', $field
}

if (!$subQuery) {
$this->options['with_aggregate'][] = [$relations, $aggregate, $field];
return $this->filter(function ($result) use ($withAggregate) {
foreach ($this->options['with_aggregate'] as $val) {
$result->relationCount($this, (array) $val[0], $val[1], $val[2], false);
}
});
$this->options['with_aggregate'][] = [(array) $relations, $aggregate, $field];
return $this;
}

if (!isset($this->options['field'])) {
Expand Down Expand Up @@ -459,28 +435,26 @@ public function hasWhere(string $relation, $where = [], string $fields = '*', st
/**
* JSON字段数据转换
* @access protected
* @param Model $result 查询数据
* @param array $json JSON字段
* @param bool $assoc 是否转换为数组
* @param array $result 查询数据
* @return void
*/
protected function jsonModelResult(Model $result, array $json = [], bool $assoc = false): void
protected function jsonModelResult(array &$result): void
{
$withAttr = $this->options['with_attr'];
foreach ($json as $name) {
if (!isset($result->$name)) {
foreach ($this->options['json'] as $name) {
if (!isset($result[$name])) {
continue;
}

$jsonData = json_decode($result->getData($name), true);
$jsonData = json_decode($result[$name], true);

if (isset($withAttr[$name])) {
foreach ($withAttr[$name] as $key => $closure) {
$jsonData[$key] = $closure($jsonData[$key] ?? null, $jsonData);
}
}

$result->set($name, !$assoc ? (object) $jsonData : $jsonData);
$result[$name] = !$this->options['json_assoc'] ? (object) $jsonData : $jsonData;
}
}

Expand All @@ -497,6 +471,7 @@ protected function resultSetToModelCollection(array $resultSet): ModelCollection
}

$this->options['is_resultSet'] = true;

foreach ($resultSet as $key => &$result) {
// 数据转换为模型对象
$this->resultToModel($result);
Expand All @@ -505,7 +480,13 @@ protected function resultSetToModelCollection(array $resultSet): ModelCollection
foreach (['with', 'with_join'] as $with) {
// 关联预载入
if (!empty($this->options[$with])) {
$result->eagerlyResultSet($resultSet, $this->options[$with], $this->options['with_relation_attr'], false, $this->options['with_cache'] ?? false);
$result->eagerlyResultSet(
$resultSet,
$this->options[$with],
$this->options['with_relation_attr'],
'with_join' == $with ? true : false,
$this->options['with_cache'] ?? false
);
}
}

Expand All @@ -521,13 +502,53 @@ protected function resultSetToModelCollection(array $resultSet): ModelCollection
*/
protected function resultToModel(array &$result): void
{
$result = $this->model->newInstance($result, !empty($this->options['is_resultSet']) ? null : $this->getModelUpdateCondition($this->options), $this->options);
// JSON数据处理
if (!empty($this->options['json'])) {
$this->jsonModelResult($result);
}

$result = $this->model->newInstance(
$result,
!empty($this->options['is_resultSet']) ? null : $this->getModelUpdateCondition($this->options),
$this->options
);

// 模型数据处理
foreach ($this->options['filter'] as $filter) {
call_user_func_array($filter, [$result, $this->options]);
}

// 关联查询
if (!empty($this->options['relation'])) {
$result->relationQuery($this->options['relation'], $this->options['with_relation_attr']);
}

// 关联预载入查询
if (empty($this->options['is_resultSet'])) {
foreach (['with', 'with_join'] as $with) {
if (!empty($this->options[$with])) {
$result->eagerlyResult(
$this->options[$with],
$this->options['with_relation_attr'],
'with_join' == $with ? true : false,
$this->options['with_cache'] ?? false
);
}
}
}

// 关联统计查询
if (!empty($this->options['with_aggregate'])) {
foreach ($this->options['with_aggregate'] as $val) {
$result->relationCount($this, $val[0], $val[1], $val[2], false);
}
}

// 动态获取器
if (!empty($this->options['with_attr'])) {
$result->withAttr($this->options['with_attr']);
}

foreach (['hidden', 'visible', 'append'] as $name) {
if (!empty($this->options[$name])) {
$result->$name($this->options[$name]);
Expand Down
23 changes: 15 additions & 8 deletions src/db/concern/ResultOperation.php
Expand Up @@ -75,10 +75,20 @@ public function failException(bool $fail = true)
*/
protected function result(array &$result): void
{
// JSON数据处理
if (!empty($this->options['json'])) {
$this->jsonResult($result);
}

// 查询数据处理
foreach ($this->options['filter'] as $filter) {
$result = call_user_func_array($filter, [$result, $this->options]);
}

// 获取器
if (!empty($this->options['with_attr'])) {
$this->getResultAttr($result, $this->options['with_attr']);
}
}

/**
Expand All @@ -105,9 +115,9 @@ protected function resultSet(array &$resultSet, bool $toCollection = true): void
* @access protected
* @param array $result 查询数据
* @param array $withAttr 字段获取器
* @return array
* @return void
*/
protected function getResultAttr(array $result, array $withAttr = []): array
protected function getResultAttr(array &$result, array $withAttr = []): void
{
foreach ($withAttr as $name => $closure) {
$name = Str::snake($name);
Expand All @@ -123,8 +133,6 @@ protected function getResultAttr(array $result, array $withAttr = []): array
$result[$name] = $closure($result[$name] ?? null, $result);
}
}

return $result;
}

/**
Expand Down Expand Up @@ -158,13 +166,12 @@ public function findOrEmpty($data = null)
/**
* JSON字段数据转换
* @access protected
* @param array $result 查询数据
* @param array $json JSON字段
* @param array $result 查询数据
* @return void
*/
protected function jsonResult(array &$result, array $json = []): void
protected function jsonResult(array &$result): void
{
foreach ($json as $name) {
foreach ($this->options['json'] as $name) {
if (!isset($result[$name])) {
continue;
}
Expand Down

0 comments on commit 091ad5e

Please sign in to comment.