Skip to content

Commit df5599b

Browse files
authored
Merge pull request kvnZero#9 from kvnZero/feat/支持查询方式
Feat/支持查询方式
2 parents 0a1e746 + 12a9742 commit df5599b

12 files changed

+182
-31
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/Handle/AbstractHandle.php

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

55
use App\ApiJson\Entity\ConditionEntity;
6+
use App\ApiJson\Entity\TableEntity;
67
use App\ApiJson\Interface\QueryInterface;
8+
use App\ApiJson\Parse\Handle;
9+
use Hyperf\Contract\ConfigInterface;
10+
use Hyperf\Utils\ApplicationContext;
711

812
abstract class AbstractHandle
913
{
@@ -38,5 +42,15 @@ protected function unsetKeySaveCondition()
3842
$this->condition->setCondition($condition);
3943
}
4044

45+
protected function subTableQuery(array $data): QueryInterface
46+
{
47+
$tableName = $data['from'];
48+
$tableEntity = new TableEntity($tableName, $data);
49+
$handle = new Handle($tableEntity->getConditionEntity(), $tableEntity);
50+
$handle->build();
51+
/** @var QueryInterface $query */
52+
return new (ApplicationContext::getContainer()->get(ConfigInterface::class)->get(QueryInterface::class))($tableEntity->getRealTableName(), $tableEntity->getConditionEntity());
53+
}
54+
4155
abstract protected function buildModel();
4256
}
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+
}

app/ApiJson/Interface/QueryInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ public function delete($id = null): bool;
2222

2323
public function all();
2424

25+
public function toSql();
26+
27+
public function getBindings();
28+
2529
public function getDb();
2630

2731
public function query();

0 commit comments

Comments
 (0)