8000 add User数组查询引用子查询测试 && update 引用值替换 & 判断条件为null的判断过滤 · APIJSON/hyperf-APIJSON@c9370fd · GitHub
[go: up one dir, main page]

Skip to content

Commit c9370fd

Browse files
committed
add User数组查询引用子查询测试 && update 引用值替换 & 判断条件为null的判断过滤
1 parent a6cbae7 commit c9370fd

File tree

3 files changed

+67
-4
lines changed

3 files changed

+67
-4
lines changed

app/ApiJson/Parse/Parse.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ public function __construct(protected array $json, protected string $method = 'G
3939

4040
public function handle(bool $isQueryMany = false, array $extendData = []): array
4141
{
42+
if (empty($extendData)) {
43+
$extendData = $this->json; //引入原json
44+
}
4245
$result = [];
4346
foreach ($this->json as $tableName => $condition) { //可以优化成协程行为(如果没有依赖赋值的前提下)
47+
if (is_null($condition)) continue;
4448
if (in_array($tableName, $this->filterKey())) {
4549
$this->tagColumn[$tableName] = $condition;
4650
continue;
@@ -93,6 +97,7 @@ protected function handleArray(array $jsonData, array $extendData = []): array
9397
{
9498
$result = [[]];
9599
foreach ($jsonData as $tableName => $condition) { //可以优化成协程行为(如果没有依赖赋值的前提下)
100+
if (is_null($condition)) continue;
96101
if (!preg_match("/^[A-Z].+/", $tableName) || !is_array($condition)) {
97102
continue; //不满足表名规范 跳出不往下执行
98103
}

app/ApiJson/Replace/QuoteReplace.php

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,24 @@ protected function process()
99
$condition = $this->condition->getCondition();
1010

1111
foreach (array_filter($condition, function($key){
12-
return str_ends_with($key, '@') && !str_ends_with($key, '}{@') && !str_ends_with($key, '{}@');
12+
return str_ends_with($key, '@');
1313
}, ARRAY_FILTER_USE_KEY) as $key => $value)
1414
{
1515
if (!is_string($value)) continue;
1616
$path = str_replace(['/', '[]'], ['.', 'currentItem'], $value);
1717
if (str_starts_with($path, '.')) {
1818
$path = 'currentItem' . $path;
1919
}
20-
$newKey = substr($key, 0, strlen($key) - 1);
21-
$condition[$newKey] = data_get($this->condition->getExtendData(), $path);
22-
unset($condition[$key]);
20+
$value = data_get($this->condition->getExtendData(), $path);
21+
if (!is_null($value)) { //常规情况下的引用
22+
$newKey = substr($key, 0, strlen($key) - 1);
23+
$condition[$newKey] = $value;
24+
unset($condition[$key]);
25+
} else { //非常规情况下引入 比如引入子查询等
26+
$path .= '@';
27+
$value = data_get($this->condition->getExtendData(), $path);
28+
$condition[$key] = $value;
29+
}
2330
$this->condition->setCondition($condition);
2431
}
2532
}

test/Cases/GetTest.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,4 +178,55 @@ public function testWhereIn()
178178
], $result);
179179
}
180180

181+
public function testWhereInUseSubQuery()
182+
{
183+
$json = [
184+
"subquery@" => [
185+
"from" =>"Comment",
186+
"Comment" => [
187+
"momentId" =>15,
188+
"@column" =>"userId"
189+
]
190+
],
191+
"User[]" => [
192+
"User" => [
193+
"id{}@" =>"subquery",
194+
"@column" =>"id,sex,name"
195+
]
196+
],
197+
"[]" =>null
198+
];
199+
$parse = new Parse($json, $this->method, '');
200+
$result = $parse->handle();
201+
202+
$this->assertSame([
203+
"User[]" =>[
204+
[
205+
"id" =>38710,
206+
"sex" =>0,
207+
"name" =>"TommyLemon"
208+
],
209+
[
210+
"id" =>82001,
211+
"sex" =>0,
212+
"name" =>"测试账号"
213+
],
214+
[
215+
"id" =>82002,
216+
"sex" =>1,
217+
"name" =>"Happy~"
218+
],
219+
[
220+
"id" =>82003,
221+
"sex" =>0,
222+
"name" =>"Wechat"
223+
],
224+
[
225+
"id" =>82055,
226+
"sex" =>1,
227+
"name" =>"Solid"
228+
]
229+
]
230+
], $result);
231+
}
181232
}

0 commit comments

Comments
 (0)
0