Skip to content

Commit 381b676

Browse files
authored
Merge pull request kvnZero#22 from kvnZero/feat/增强update
update 拆离定义
2 parents d7df486 + ad350f9 commit 381b676

File tree

5 files changed

+61
-32
lines changed

5 files changed

+61
-32
lines changed

app/ApiJson/Handle/AbstractHandle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ protected function subTableQuery(array $data): QueryInterface
6868
$conditionEntity = $tableEntity->getConditionEntity();
6969
$conditionEntity->setLimit(0);
7070
$handle = new Handle($conditionEntity, $tableEntity);
71-
$handle->build();
71+
$handle->buildQuery();
7272
/** @var QueryInterface $query */
7373
return new (ApplicationContext::getContainer()->get(ConfigInterface::class)->get(QueryInterface::class))($tableEntity->getRealTableName(), $tableEntity->getConditionEntity());
7474
}

app/ApiJson/Method/GetMethod.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ protected function process()
2424
}
2525

2626
$handle = new Handle($this->tableEntity->getConditionEntity(), $this->tableEntity);
27-
$handle->build();
27+
$handle->buildQuery();
2828

2929
//该事件鼓励是做语句缓存或者事件触发 不赞成修改语句做法 修改语句应在更上层的QueryHandle事件
3030
$event = new QueryExecuteBefore($this->query->toSql(), $this->method);

app/ApiJson/Method/HeadMethod.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ protected function validateCondition(): bool
1818
protected function process()
1919
{
2020
$handle = new Handle($this->tableEntity->getConditionEntity(), $this->tableEntity);
21-
$handle->build();
21+
$handle->buildQuery();
2222

2323
$event = new QueryExecuteBefore($this->query->toSql(), $this->method);
2424
ApplicationContext::getContainer()->get(EventDispatcherInterface::class)->dispatch($event);

app/ApiJson/Model/MysqlQuery.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@
99

1010
class MysqlQuery implements QueryInterface
1111
{
12+
/** @var int 查询行为 */
13+
const ACTION_QUERY = 1;
14+
15+
/** @var int 修改行为 */
16+
const ACTION_UPDATE = 2;
17+
18+
/** @var int 插入行为 */
19+
const ACTION_INSERT = 4;
20+
21+
1222
/** @var string $primaryKey */
1323
protected string $primaryKey = 'id';
1424

@@ -71,7 +81,7 @@ public function insert(array $values, $sequence = null): int
7181
public function update(array $values): bool
7282
{
7383
$this->build = true;
74-
$this->buildQuery(false);
84+
$this->buildQuery(self::ACTION_UPDATE);
7585
if (empty($this->db->getBindings()['where'])) return false; // 不允许空条件修改
7686
return $this->db->update($values);
7787
}
@@ -131,15 +141,15 @@ public function callProcedure(array $dataItem, bool $query = true): array
131141
];
132142
}
133143

134-
protected function buildQuery(bool $query = true)
144+
protected function buildQuery(int $actionType = self::ACTION_QUERY)
135145
{
136146
if ($this->build) return;
137147
$this->build = true;
138148
$queryWhere = $this->conditionEntity->getQueryWhere();
139149
foreach ($queryWhere as $itemWhere) {
140150
$this->db->whereRaw($itemWhere['sql'], $itemWhere['bind']);
141151
}
142-
if (!$query) return; //下面不再非查询操作
152+
if ($actionType != self::ACTION_QUERY) return; //下面不再非查询操作
143153

144154
$this->db->select(Db::raw($this->conditionEntity->getColumn()));
145155
$limit = $this->conditionEntity->getLimit();

app/ApiJson/Parse/Handle.php

Lines changed: 45 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,55 +39,74 @@
3939

4040
class Handle
4141
{
42+
4243
/**
4344
* 替换规则
4445
* @var AbstractReplace[]
4546
*/
4647
protected array $replaceRules = [
47-
KeywordCountReplace::class,
48-
KeywordPageReplace::class,
49-
QuoteReplace::class,
48+
'query' => [
49+
KeywordCountReplace::class,
50+
KeywordPageReplace::class,
51+
QuoteReplace::class,
52+
],
53+
'update' => []
5054
];
5155

5256

5357
/**
5458
* 匹配规则 根据从上自下优先先匹先出
5559
* @var AbstractHandle[]
5660
*/
57-
protected array $methodRules = [
58-
FunctionProcedureHandle::class,
59-
FunctionColumnHandle::class,
60-
FunctionHavingHandle::class,
61-
FunctionOffsetHandle::class,
62-
FunctionLimitHandle::class,
63-
FunctionGroupHandle::class,
64-
FunctionOrderHandle::class,
65-
WhereJsonContainsHandle::class,
66-
WhereBetweenHandle::class,
67-
WhereExistsHandle::class,
68-
WhereRegexpHandle::class,
69-
WhereNotInHandle::class,
70-
WhereLikeHandle::class,
71-
WhereRawHandle::class,
72-
WhereInHandle::class,
73-
WhereSubQueryHandle::class,
74-
WhereOpHandle::class,
75-
WhereHandle::class,
76-
FunctionCombineHandle::class
61+
protected array $queryMethodRules = [
62+
'query' => [
63+
FunctionProcedureHandle::class,
64+
FunctionColumnHandle::class,
65+
FunctionHavingHandle::class,
66+
FunctionOffsetHandle::class,
67+
FunctionLimitHandle::class,
68+
FunctionGroupHandle::class,
69+
FunctionOrderHandle::class,
70+
WhereJsonContainsHandle::class,
71+
WhereBetweenHandle::class,
72+
WhereExistsHandle::class,
73+
WhereRegexpHandle::class,
74+
WhereNotInHandle::class,
75+
WhereLikeHandle::class,
76+
WhereRawHandle::class,
77+
WhereInHandle::class,
78+
WhereSubQueryHandle::class,
79+
WhereOpHandle::class,
80+
WhereHandle::class,
81+
FunctionCombineHandle::class
82+
],
83+
'update' => [
84+
85+
]
7786
];
7887

7988
public function __construct(protected ConditionEntity $conditionEntity, protected TableEntity $tableEntity)
8089
{
8190
}
8291

83-
public function build()
92+
public function buildQuery()
93+
{
94+
$this->build('query');
95+
}
96+
97+
public function buildUpdate()
98+
{
99+
$this->build('update');
100+
}
101+
102+
protected function build(string $action)
84103
{
85-
foreach ($this->replaceRules as $replaceRuleClass) {
104+
foreach ($this->replaceRules[$action] ?? [] as $replaceRuleClass) {
86105
/** @var AbstractReplace $replaceRule */
87106
$replaceRule = new $replaceRuleClass($this->conditionEntity);
88107
$replaceRule->handle();
89108
}
90-
foreach ($this->methodRules as $methodRuleClass) {
109+
foreach ($this->queryMethodRules[$action] ?? [] as $methodRuleClass) {
91110
/** @var AbstractHandle $methodRule */
92111
$methodRule = new $methodRuleClass($this->conditionEntity);
93112
$methodRule->handle();

0 commit comments

Comments
 (0)