Skip to content

Commit 24fc7f1

Browse files
committed
fix/merge 解决冲突
2 parents 5f12b00 + aa2bb4d commit 24fc7f1

16 files changed

+253
-56
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ https://github.com/Tencent/APIJSON
3636
### 开发进度
3737

3838
1. ✅最基本CRUD 包括批量写入 批量更新 批量删除
39-
2. ✅支持@column @having @order @group
40-
3. ✅支持运算符 {}, }{@, $, %, ~
39+
2. ✅支持@column @having @order @group @combine
40+
3. ✅支持运算符 {}, !{}, &{}, |{}, }{@, $, %, ~
4141
4. ✅支持多表查询、一对一查询、一对多查询、数组内多对多查询
4242
5. ✅支持数组内count/page
4343
6. ✅支持debug标签可返回语句执行
4444

45-
待完成:子查询, 存储过程调用,远程函数,权限,标签,<b>单元测试(高优先)</b>
45+
待完成:<b>复杂查询</b>, 存储过程调用,远程函数,权限,标签
4646

4747

4848
### 如何使用

app/ApiJson/ApiJson.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ public function Query(): array
2525
if (empty($beforeEvent->response)) { //提供更多可能性, 如接入缓存功能
2626
if (!is_array(json_decode($beforeEvent->content, true))) {
2727
return [
28-
'code' => ResponseCode::SERVER_ERROR,
29-
'msg' => ResponseCode::getMessage(ResponseCode::SERVER_ERROR)
28+
'code' => ResponseCode::CODE_UNSUPPORTED_ENCODING,
29+
'msg' => ResponseCode::getMessage(ResponseCode::CODE_UNSUPPORTED_ENCODING)
3030
];
3131
}
3232
$parse = new Parse(json_decode($beforeEvent->content, true), $this->method, $this->request->input('tag', ''));
@@ -38,8 +38,8 @@ public function Query(): array
3838
ApplicationContext::getContainer()->get(EventDispatcherInterface::class)->dispatch(new RequestHandleAfter($beforeEvent->content, $response));
3939

4040
return array_merge([
41-
'code' => ResponseCode::SUCCESS,
42-
'msg' => ResponseCode::getMessage(ResponseCode::SUCCESS)
41+
'code' => ResponseCode::CODE_SUCCESS,
42+
'msg' => ResponseCode::getMessage(ResponseCode::CODE_SUCCESS)
4343
], $response);
4444
}
4545
}

app/ApiJson/Handle/AbstractHandle.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
namespace App\ApiJson\Handle;
44

55
use App\ApiJson\Entity\ConditionEntity;
6+
use App\ApiJson\Entity\TableEntity;
67
use App\ApiJson\Interface\QueryInterface;
78
use App\Event\ApiJson\QueryHandleAfter;
89
use App\Event\ApiJson\QueryHandleBefore;
910
use Hyperf\Utils\ApplicationContext;
1011
use Psr\EventDispatcher\EventDispatcherInterface;
12+
use App\ApiJson\Parse\Handle;
13+
use Hyperf\Contract\ConfigInterface;
1114

1215
abstract class AbstractHandle
1316
{
@@ -58,5 +61,15 @@ protected function handleAfter()
5861
$this->condition = $event->condition;
5962
}
6063

64+
protected function subTableQuery(array $data): QueryInterface
65+
{
66+
$tableName = $data['from'];
67+
$tableEntity = new TableEntity($tableName, $data);
68+
$handle = new Handle($tableEntity->getConditionEntity(), $tableEntity);
69+
$handle->build();
70+
/** @var QueryInterface $query */
71+
return new (ApplicationContext::getContainer()->get(ConfigInterface::class)->get(QueryInterface::class))($tableEntity->getRealTableName(), $tableEntity->getConditionEntity());
72+
}
73+
6174
abstract protected function buildModel();
6275
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace App\ApiJson\Handle;
4+
5+
class FunctionCombineHandle extends AbstractHandle
6+
{
7+
protected string $keyWord = '@combine';
8+
9+
public function buildModel()
10+
{
11+
if (!in_array($this->keyWord, array_keys($this->condition->getCondition()))) {
12+
return;
13+
}
14+
foreach (array_filter($this->condition->getCondition(), function($key){
15+
return $key == $this->keyWord;
16+
}, ARRAY_FILTER_USE_KEY) as $key => $value)
17+
{
18+
$conditionKeyArr = explode(',', $value);
19+
$op = [
20+
'&' => [],
21+
'|' => [],
22+
'!' => []
23+
];
24+
foreach ($conditionKeyArr as $conditionKey) {
25+
if (str_starts_with($conditionKey, '&')) {
26+
$op['&'] = $conditionKey;
27+
} else if (str_starts_with($conditionKey, '!')) {
28+
$op['!'] = $conditionKey;
29+
} else {
30+
$op['|'] = $conditionKey;
31+
}
32+
}
33+
$sql = [];
34+
$bind = [];
35+
$queryWhere = $this->condition->getQueryWhere();
36+
foreach ($op as $opKey => $opValue) {
37+
if (empty($value)) continue;
38+
$subSql = [];
39+
foreach ($opValue as $key) {
40+
$subSql[] = $queryWhere[$key]['sql'];
41+
$bind = array_merge($bind, $queryWhere[$key]['bind']);
42+
unset($queryWhere[$key]);
43+
}
44+
$boolean = ' OR ';
45+
if ($opKey == '&') $boolean = ' AND ';
46+
$pref = ($opKey == '!') ? '!' : '';
47+
$sql[] = sprintf('%s(%s)', $pref, join($boolean, $subSql));
48+
}
49+
$queryWhere[$this->keyWord] = [
50+
'sql' => join(' AND ', $sql),
51+
'bind' => $bind
52+
];
53+
}
54+
}
55+
}

app/ApiJson/Handle/WhereExistsHandle.php

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,6 @@
22

33
namespace App\ApiJson\Handle;
44

5-
use App\ApiJson\Entity\TableEntity;
6-
use App\ApiJson\Interface\QueryInterface;
7-
use App\ApiJson\Parse\Handle;
8-
use Hyperf\Contract\ConfigInterface;
9-
use Hyperf\Database\Query\Builder;
10-
use Hyperf\Utils\ApplicationContext;
11-
125
class WhereExistsHandle extends AbstractHandle
136
{
147
protected function buildModel()
@@ -17,24 +10,9 @@ protected function buildModel()
1710
return str_ends_with($key, '}{@');
1811
}, ARRAY_FILTER_USE_KEY) as $key => $value)
1912
{
20-
$bind = [];
21-
$existsSql = '';
22-
23-
$tableName = $value['from'];
24-
$tableEntity = new TableEntity($tableName, $value[$value['from']]);
25-
$query = new (ApplicationContext::getContainer()->get(ConfigInterface::class)->get(QueryInterface::class))($tableEntity->getRealTableName());
26-
$handle = new Handle($tableEntity->getConditionEntity(), $tableEntity);
27-
$handle->build($query);
28-
$queryWhere = $tableEntity->getConditionEntity()->getQueryWhere();
29-
//
30-
//
31-
// foreach ($value[$value['from']] as $k => $v) {
32-
// $query->where($k, $v);
33-
// }
34-
// $sql = sprintf("WHERE EXISTS %s", $existsSql);
35-
// $this->query->whereExists(function(Builder $query) use($value) {
36-
//
37-
// });
13+
$query = $this->subTableQuery($value);
14+
$sql = sprintf('EXISTS(%s)', $query->toSql());
15+
$this->condition->addQueryWhere($key, $sql, $query->getBindings());
3816
$this->unsetKey[] = $key;
3917
}
4018
}

app/ApiJson/Handle/WhereInHandle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ protected function buildModel()
1111
}, ARRAY_FILTER_USE_KEY) as $key => $value)
1212
{
1313
if (!is_array($value)) continue;
14-
$sql = sprintf('`%s` in (?)', $this->sanitizeKey($key));
14+
$sql = sprintf('`%s` IN (?)', $this->sanitizeKey($key));
1515
$this->condition->addQueryWhere($key, $sql, [join(',', $value)]);
1616
$this->unsetKey[] = $key;
1717
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace App\ApiJson\Handle;
4+
5+
class WhereNotInHandle extends AbstractHandle
6+
{
7+
protected function buildModel()
8+
{
9+
foreach (array_filter($this->condition->getCondition(), function($key){
10+
return str_ends_with($key, '!{}');
11+
}, ARRAY_FILTER_USE_KEY) as $key => $value)
12+
{
13+
if (!is_array($value)) continue;
14+
$sql = sprintf('`%s` NOT IN (?)', $this->sanitizeKey($key));
15+
$this->condition->addQueryWhere($key, $sql, [join(',', $value)]);
16+
$this->unsetKey[] = $key;
17+
}
18+
}
19+
}

app/ApiJson/Handle/WhereOpHandle.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace App\ApiJson\Handle;
4+
5+
class WhereOpHandle extends AbstractHandle
6+
{
7+
protected function buildModel()
8+
{
9+
foreach ($this->condition->getCondition() as $key => $value)
10+
{
11+
if(str_ends_with($key, '>=')) {
12+
$op = '>=';
13+
} else if(str_ends_with($key, '<=')) {
14+
$op = '<=';
15+
} else if(str_ends_with($key, '>')) {
16+
$op = '>';
17+
} else if(str_ends_with($key, '<')) {
18+
$op = '<';
19+
}
20+
if (!isset($op)) continue;
21+
22+
$sql = sprintf("`%s` %s ?", $this->sanitizeKey($key), $op);
23+
$this->condition->addQueryWhere($key, $sql, [$value]);
24+
$this->unsetKey[] = $key;
25+
}
26+
}
27+
}

app/ApiJson/Handle/WhereRawHandle.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,18 @@ protected function buildModel()
1111
}, ARRAY_FILTER_USE_KEY) as $key => $value)
1212
{
1313
if (is_array($value)) continue;
14+
$boolean = ' OR ';
1415
$conditionArr = explode(',', (string)$value);
1516
$sql = [];
1617
foreach ($conditionArr as $condition) {
1718
$sql[] = sprintf("`%s`%s", $this->sanitizeKey($key), trim($condition));
1819
}
19-
$this->condition->addQueryWhere($key, join(' OR ', $sql), []);
20+
if (str_ends_with($key, '|{}')) {
21+
$boolean = ' OR ';
22+
} else if (str_ends_with($key, '&{}')) {
23+
$boolean = ' AND ';
24+
}
25+
$this->condition->addQueryWhere($key, join($boolean, $sql), []);
2026
$this->unsetKey[] = $key;
2127
}
2228
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace App\ApiJson\Handle;
4+
5+
class WhereSubQueryHandle extends AbstractHandle
6+
{
7+
protected function buildModel()
8+
{
9+
foreach (array_filter($this->condition->getCondition(), function($key){
10+
return str_ends_with($key, '@');
11+
}, ARRAY_FILTER_USE_KEY) as $key => $value)
12+
{
13+
$query = $this->subTableQuery($value);
14+
15+
$op = '=';
16+
if(str_ends_with($key, '>=@')) {
17+
$op = '>=';
18+
} else if(str_ends_with($key, '<=@')) {
19+
$op = '<=';
20+
} else if(str_ends_with($key, '>@')) {
21+
$op = '>';
22+
} else if(str_ends_with($key, '<@')) {
23+
$op = '<';
24+
}
25+
$sql = sprintf('`%s`%s(%s)', $this->sanitizeKey($key), $op, $query->toSql());
26+
$this->condition->addQueryWhere($key, $sql, $query->getBindings());
27+
$this->unsetKey[] = $key;
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)