Skip to content

Commit

Permalink
完成数组内一对一查询 && 增加debug关键字 可返还debug数组包含查询的语句
Browse files Browse the repository at this point in the history
  • Loading branch information
kvnZero committed Nov 6, 2021
1 parent 6c27356 commit f65e0e4
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 18 deletions.
2 changes: 0 additions & 2 deletions app/ApiJson/Method/GetMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace App\ApiJson\Method;

use App\ApiJson\Interface\QueryInterface;

class GetMethod extends AbstractMethod
{
protected function validateCondition(): bool
Expand Down
54 changes: 49 additions & 5 deletions app/ApiJson/Parse/Parse.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@
use App\ApiJson\Method\HeadMethod;
use App\ApiJson\Method\PostMethod;
use App\ApiJson\Method\PutMethod;
use Hyperf\Utils\Context;

class Parse
{
protected array $tagColumn = [
'tag' => null
'tag' => null,
'debug' => false
];

protected array $globalKey = [
Expand Down Expand Up @@ -47,9 +49,8 @@ public function handle(bool $isQueryMany = false, array $extendData = []): array
$this->globalKey[$tableName] = $condition;
continue;
}
if ($tableName == '[]') {
$parse = new self($condition, $this->method, $condition['tag'] ?? '');
$result[$tableName] = $parse->handle(true, array_merge($result, $extendData)); //提供行为
if ($tableName == '[]' && $this->method == 'GET') {
$result[$tableName] = $this->handleArray($condition, array_merge($result, $extendData)); //提供行为
continue; //跳出不往下执行
}
if (str_ends_with($tableName, '[]')) {
Expand All @@ -70,7 +71,50 @@ public function handle(bool $isQueryMany = false, array $extendData = []): array
}
}
}
//TODO: 抽象操作方法
return $this->resultExtendHandle($result);
}

protected function resultExtendHandle(array $result)
{
if ($this->tagColumn['debug']) {
$result['debug'] = (new Context())->get('querySql');
}
return $result;
}

/**
* 处理[]的数据
* @param array $jsonData
* @param array $extendData
* @return array
*/
protected function handleArray(array $jsonData, array $extendData = []): array
{
$result = [[]];
foreach ($jsonData as $tableName => $condition) { //可以优化成协程行为(如果没有依赖赋值的前提下)
foreach ($result as $key => $item) {
if (in_array($tableName, $this->filterKey())) {
$this->tagColumn[$tableName] = $condition;
continue;
}
if (in_array($tableName, $this->globalKey())) {
$this->globalKey[$tableName] = $condition;
continue;
}
$extendData['currentItem'] = $item;
$this->tableEntities['[]'][$tableName] = new TableEntity($tableName, $jsonData, $this->getGlobalArgs(), array_merge($result, $extendData));
$method = new GetMethod($this->tableEntities['[]'][$tableName], $this->method);
$method->setQueryMany($result == [[]]);
$response = $method->handle();
if (!is_null($response)) {
if ($result == [[]]) {
$result = $response; //masterData
} else {
$result[$key][$tableName] = $response;
}
}
}
}
return $result;
}

Expand Down
6 changes: 2 additions & 4 deletions app/ApiJson/Replace/QuoteReplace.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace App\ApiJson\Replace;

use App\ApiJson\Interface\QueryInterface;
use Hyperf\Utils\Arr;

class QuoteReplace extends AbstractReplace
{
protected function validateCondition(): bool
Expand All @@ -14,7 +11,8 @@ protected function validateCondition(): bool

protected function process()
{
$path = str_replace('/', '.', $this->value);
$path = str_replace(['/', '[]'], ['.', 'currentItem'], $this->value);

$this->value = data_get($this->extendData, $path);
$this->key = substr($this->key, 0, strlen($this->key) - 1);

Expand Down
14 changes: 7 additions & 7 deletions app/Listener/DbQueryExecutedListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@
use Hyperf\Database\Events\QueryExecuted;
use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Logger\LoggerFactory;
use Hyperf\Utils\Arr;
use Hyperf\Utils\Context;
use Hyperf\Utils\Str;
use Psr\Container\ContainerInterface;
use Psr\Log\LoggerInterface;

/**
* @Listener
*/
class DbQueryExecutedListener implements ListenerInterface
{

/**
* @var LoggerInterface
* @var Context
*/
private $logger;
private $context;

public function __construct(ContainerInterface $container)
{
$this->logger = $container->get(LoggerFactory::class)->get('sql');
$this->context = new Context();
}

public function listen(): array
Expand All @@ -54,8 +54,8 @@ public function process(object $event)
$sql = Str::replaceFirst('?', "'{$value}'", $sql);
}
}

$this->logger->info(sprintf('[%s] %s', $event->time, $sql));
$querySql = $this->context->get('querySql');
$this->context->set('querySql', array_merge($querySql ?? [], [$sql]));
}
}
}

0 comments on commit f65e0e4

Please sign in to comment.