Skip to content

Commit 0a1e746

Browse files
authored
Merge pull request kvnZero#8 from kvnZero/feat/语句解析修改链方式
Feat/语句解析修改链方式
2 parents e8d63df + 9e447af commit 0a1e746

28 files changed

+531
-331
lines changed

app/ApiJson/Entity/ConditionEntity.php

Lines changed: 140 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2,92 +2,164 @@
22

33
namespace App\ApiJson\Entity;
44

5-
use App\ApiJson\Handle\FunctionLimitHandle;
6-
use App\ApiJson\Handle\FunctionOffsetHandle;
7-
use App\ApiJson\Interface\QueryInterface;
8-
use App\ApiJson\Handle\AbstractHandle;
9-
use App\ApiJson\Handle\FunctionColumnHandle;
10-
use App\ApiJson\Handle\FunctionGroupHandle;
11-
use App\ApiJson\Handle\FunctionHavingHandle;
12-
use App\ApiJson\Handle\FunctionOrderHandle;
13-
use App\ApiJson\Handle\WhereBetweenHandle;
14-
use App\ApiJson\Handle\WhereExistsHandle;
15-
use App\ApiJson\Handle\WhereHandle;
16-
use App\ApiJson\Handle\WhereInHandle;
17-
use App\ApiJson\Handle\WhereJsonContainsHandle;
18-
use App\ApiJson\Handle\WhereLikeHandle;
19-
use App\ApiJson\Handle\WhereRawHandle;
20-
use App\ApiJson\Handle\WhereRegexpHandle;
21-
use App\ApiJson\Method\AbstractMethod;
22-
use App\ApiJson\Replace\AbstractReplace;
23-
use App\ApiJson\Replace\KeywordCountReplace;
24-
use App\ApiJson\Replace\KeywordPageReplace;
25-
use App\ApiJson\Replace\QuoteReplace;
26-
275
class ConditionEntity
286
{
7+
protected array $changeLog = [];
8+
299
/**
30-
* 替换规则
31-
* @var AbstractReplace[]
10+
* @var array
3211
*/
33-
protected array $replaceRules = [
34-
KeywordCountReplace::class,
35-
KeywordPageReplace::class,
36-
QuoteReplace::class,
37-
];
12+
protected array $where = [];
3813

14+
protected int $limit = 0;
15+
protected int $offset = 0;
16+
protected array $column = ['*'];
17+
protected array $group = [];
18+
protected array $order = [];
19+
protected array $having = [];
3920

4021
/**
41-
* 匹配规则 根据从上自下优先先匹先出
42-
* @var AbstractHandle[]
22+
* @param array $condition 条件
4323
*/
44-
protected array $methodRules = [
45-
FunctionColumnHandle::class,
46-
FunctionHavingHandle::class,
47-
FunctionOffsetHandle::class,
48-
FunctionLimitHandle::class,
49-
FunctionGroupHandle::class,
50-
FunctionOrderHandle::class,
51-
WhereJsonContainsHandle::class,
52-
WhereBetweenHandle::class,
53-
WhereExistsHandle::class,
54-
WhereRegexpHandle::class,
55-
WhereLikeHandle::class,
56-
WhereRawHandle::class,
57-
WhereInHandle::class,
58-
WhereHandle::class,
59-
];
24+
public function __construct(protected array $condition, protected array $extendData = [])
25+
{
26+
}
27+
28+
public function getExtendData(): array
29+
{
30+
return $this->extendData;
31+
}
32+
33+
public function setCondition(array $condition)
34+
{
35+
$this->log($condition);
36+
$this->condition = $condition;
37+
}
38+
39+
public function getCondition(): array
40+
{
41+
return $this->condition;
42+
}
43+
44+
public function getQueryWhere(): array
45+
{
46+
return $this->where;
47+
}
48+
49+
public function setQueryWhere(array $where)
50+
{
51+
$this->where = $where;
52+
}
53+
54+
public function addQueryWhere(string $key, string $sql, array $bindArgs = [])
55+
{
56+
$this->where[$key] = [
57+
'sql' => $sql,
58+
'bind' => $bindArgs
59+
];
60+
}
6061

6162
/**
62-
* @param array $condition 条件
63+
* @param array|string[] $column
6364
*/
64-
public function __construct(protected array $condition, protected array $extendData = [])
65+
public function setColumn(array $column): void
6566
{
67+
$this->column = $column;
6668
}
6769

68-
protected function replaceHandle($key, $value, array $condition = []): array
70+
/**
71+
* @param array $group
72+
*/
73+
public function setGroup(array $group): void
6974
{
70-
foreach ($this->replaceRules as $rule) {
71-
/** @var AbstractReplace $replaceRule */
72-
$replaceRule = new $rule($key, $value, $condition, $this->extendData);
73-
$response = $replaceRule->handle();
74-
if (!is_null($response)) return $response;
75-
}
76-
return [$key, $value];
75+
$this->group = $group;
7776
}
7877

7978
/**
80-
* 整理语句
79+
* @param array $having
8180
*/
82-
public function setQueryCondition(QueryInterface $query)
83-
{
84-
foreach ($this->condition as $key => $value) {
85-
[$key, $value] = $this->replaceHandle($key, $value, $this->condition); //解决引用问题
86-
/** @var AbstractMethod $rule */
87-
foreach ($this->methodRules as $rule) {
88-
$methodRule = new $rule($query, $key, $value);
89-
if ($methodRule->handle()) break;
90-
}
91-
}
81+
public function setHaving(array $having): void
82+
{
83+
$this->having = $having;
84+
}
85+
86+
/**
87+
* @param int $limit
88+
*/
89+
public function setLimit(int $limit): void
90+
{
91+
$this->limit = $limit;
92+
}
93+
94+
/**
95+
* @param int $offset
96+
*/
97+
public function setOffset(int $offset): void
98+
{
99+
$this->offset = $offset;
100+
}
101+
102+
/**
103+
* @param array $order
104+
*/
105+
public function setOrder(array $order): void
106+
{
107+
$this->order = $order;
108+
}
109+
110+
/**
111+
* @return array
112+
*/
113+
public function getColumn(): array
114+
{
115+
return $this->column;
116+
}
117+
118+
/**
119+
* @return array
120+
*/
121+
public function getGroup(): array
122+
{
123+
return $this->group;
124+
}
125+
126+
/**
127+
* @return array
128+
*/
129+
public function getHaving(): array
130+
{
131+
return $this->having;
132+
}
133+
134+
/**
135+
* @return int
136+
*/
137+
public function getLimit(): int
138+
{
139+
return $this->limit;
140+
}
141+
142+
/**
143+
* @return int
144+
*/
145+
public function getOffset(): int
146+
{
147+
return $this->offset;
148+
}
149+
150+
/**
151+
* @return array
152+
*/
153+
public function getOrder(): array
154+
{
155+
return $this->order;
156+
}
157+
158+
protected function log(array $condition)
159+
{
160+
$this->changeLog[] = [
161+
'old' => $this->condition,
162+
'new' => $condition
163+
];
92164
}
93165
}

app/ApiJson/Entity/TableEntity.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class TableEntity
1717
* @param string $tableName 表名
1818
* @param array $jsonContent json源数据
1919
*/
20-
public function __construct(protected string $tableName, protected array $jsonContent, protected array $globalArgs, protected array $extendData = [])
20+
public function __construct(protected string $tableName, protected array $jsonContent, protected array $globalArgs = [], protected array $extendData = [])
2121
{
2222
$sanitizeTableName = str_replace(['[]'], '', $this->tableName);
2323
$this->realTableName = $sanitizeTableName;

app/ApiJson/Handle/AbstractHandle.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,41 @@
22

33
namespace App\ApiJson\Handle;
44

5+
use App\ApiJson\Entity\ConditionEntity;
56
use App\ApiJson\Interface\QueryInterface;
67

78
abstract class AbstractHandle
89
{
9-
/** @var string 清洗后的查询key */
10-
protected string $sanitizeKey;
10+
/** @var string 关键字 */
11+
protected string $keyWord;
1112

12-
public function __construct(protected QueryInterface $query, protected string $key, protected $value)
13+
protected array $unsetKey = [];
14+
15+
public function __construct(protected ConditionEntity $condition)
16+
{
17+
}
18+
19+
protected function sanitizeKey(string $key): string
1320
{
14-
preg_match('#(?<key>[a-zA-z0-9_]+)#', $this->key, $match);
15-
$this->sanitizeKey = $match['key'] ?? $this->key;
21+
preg_match('#(?<key>[a-zA-z0-9_]+)#', $key, $match);
22+
return $match['key'] ?? $key;
1623
}
1724

18-
public function handle(): bool
25+
public function handle()
1926
{
20-
if (!$this->validateCondition()) return false;
2127
$this->buildModel();
22-
return true;
28+
$this->unsetKeySaveCondition();
2329
}
2430

25-
abstract protected function validateCondition(): bool;
31+
protected function unsetKeySaveCondition()
32+
{
33+
if (empty($this->unsetKey)) return;
34+
$condition = $this->condition->getCondition();
35+
foreach ($this->unsetKey as $key) {
36+
unset($condition[$key]);
37+
}
38+
$this->condition->setCondition($condition);
39+
}
2640

2741
abstract protected function buildModel();
2842
}

app/ApiJson/Handle/FunctionColumnHandle.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44

55
class FunctionColumnHandle extends AbstractHandle
66
{
7-
protected function validateCondition(): bool
8-
{
9-
return $this->key === '@column';
10-
}
7+
protected string $keyWord = '@column';
118

12-
protected function buildModel()
9+
public function buildModel()
1310
{
14-
$this->value = str_replace([';',':'], [',', ' AS '], $this->value);
15-
$this->query->select(explode(',', $this->value));
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+
$value = str_replace([';',':'], [',', ' AS '], $value);
19+
$this->condition->setColumn(explode(',', $value));
20+
$this->unsetKey[] = $this->keyWord;
21+
}
1622
}
1723
}

app/ApiJson/Handle/FunctionGroupHandle.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@
44

55
class FunctionGroupHandle extends AbstractHandle
66
{
7-
protected function validateCondition(): bool
8-
{
9-
return $this->key === '@group';
10-
}
7+
protected string $keyWord = '@group';
118

12-
protected function buildModel()
9+
public function buildModel()
1310
{
14-
$groupArr = explode(',', $this->value);
15-
$this->query->groupBy($groupArr);
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+
$groupArr = explode(',', $value);
19+
$this->condition->setGroup($groupArr);
20+
$this->unsetKey[] = $this->keyWord;
21+
}
1622
}
1723
}

app/ApiJson/Handle/FunctionHavingHandle.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,24 @@
44

55
class FunctionHavingHandle extends AbstractHandle
66
{
7-
protected function validateCondition(): bool
8-
{
9-
return $this->key === '@having';
10-
}
7+
protected string $keyWord = '@having';
118

12-
protected function buildModel()
9+
public function buildModel()
1310
{
14-
$havingArr = explode(';', $this->value);
15-
foreach ($havingArr as $having) {
16-
$this->query->having($having);
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+
$havingArr = explode(';', $value);
19+
$sql = [];
20+
foreach ($havingArr as $having) {
21+
$sql[] = sprintf("`%s`%s", $this->sanitizeKey($key), trim($having));
22+
}
23+
$this->condition->setHaving($sql); //解析时为AND
24+
$this->unsetKey[] = $key;
1725
}
1826
}
1927
}

0 commit comments

Comments
 (0)