Skip to content

Commit 7f66a3e

Browse files
author
veasion
committed
抽象拦截器
1 parent 0daed26 commit 7f66a3e

File tree

2 files changed

+203
-100
lines changed

2 files changed

+203
-100
lines changed
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
package cn.veasion.db.interceptor;
2+
3+
import cn.veasion.db.AbstractFilter;
4+
import cn.veasion.db.base.Filter;
5+
import cn.veasion.db.query.AbstractJoinQuery;
6+
import cn.veasion.db.query.AbstractQuery;
7+
import cn.veasion.db.query.JoinQueryParam;
8+
import cn.veasion.db.query.SubQuery;
9+
import cn.veasion.db.query.SubQueryParam;
10+
import cn.veasion.db.query.UnionQueryParam;
11+
import cn.veasion.db.update.AbstractUpdate;
12+
import cn.veasion.db.update.BatchEntityInsert;
13+
import cn.veasion.db.update.Delete;
14+
import cn.veasion.db.update.EntityInsert;
15+
import cn.veasion.db.update.EntityUpdate;
16+
import cn.veasion.db.update.JoinUpdateParam;
17+
18+
import java.util.Collections;
19+
import java.util.List;
20+
import java.util.Map;
21+
import java.util.function.Consumer;
22+
import java.util.function.Supplier;
23+
24+
/**
25+
* AbstractInterceptor
26+
*
27+
* @author luozhuowei
28+
* @date 2021/12/14
29+
*/
30+
public abstract class AbstractInterceptor implements EntityDaoInterceptor {
31+
32+
private boolean handleQuery, handleUpdate, handleDelete, handleAbstractFilter, handleInsert;
33+
34+
public AbstractInterceptor(boolean handleQuery, boolean handleUpdate, boolean handleDelete, boolean handleAbstractFilter, boolean handleInsert) {
35+
this.handleQuery = handleQuery;
36+
this.handleUpdate = handleUpdate;
37+
this.handleDelete = handleDelete;
38+
this.handleAbstractFilter = handleAbstractFilter;
39+
this.handleInsert = handleInsert;
40+
}
41+
42+
@Override
43+
public <R> R intercept(EntityDaoInvocation<R> invocation) {
44+
Object[] args = invocation.getArgs();
45+
if (args != null && !skip()) {
46+
for (Object arg : args) {
47+
if (arg instanceof AbstractQuery && handleQuery) {
48+
handleQuery((AbstractQuery<?>) arg);
49+
} else if (arg instanceof AbstractUpdate && handleUpdate) {
50+
handleUpdate((AbstractUpdate<?>) arg);
51+
} else if (arg instanceof Delete && handleDelete) {
52+
if (!containSkipClass(((Delete) arg).getEntityClass())) {
53+
handleDelete((Delete) arg);
54+
}
55+
} else if (arg instanceof AbstractFilter) {
56+
if (handleAbstractFilter) {
57+
handleAbstractFilter((AbstractFilter<?>) arg);
58+
}
59+
} else if (arg instanceof BatchEntityInsert) {
60+
if (!containSkipClass(((BatchEntityInsert) arg).getEntityClass())) {
61+
handleBatchInsert((BatchEntityInsert) arg);
62+
}
63+
} else if (arg instanceof EntityInsert && handleInsert) {
64+
EntityInsert insert = (EntityInsert) arg;
65+
if (!containSkipClass(insert.getEntityClass())) {
66+
handleInsert(insert.getEntityClass(), Collections.singletonList(insert.getEntity()), Collections.singletonList(insert.getFieldValueMap()));
67+
}
68+
}
69+
}
70+
}
71+
return invocation.proceed();
72+
}
73+
74+
protected abstract void handleDelete(Delete delete);
75+
76+
protected abstract void handleInsert(Class<?> entityClass, List<?> entityList, List<Map<String, Object>> fieldValueMapList);
77+
78+
protected abstract void handleOnFilter(Object joinParam, Supplier<List<Filter>> onFilters, Consumer<Filter> onMethod, String tableAs);
79+
80+
protected abstract void handleFilter(AbstractFilter<?> abstractFilter);
81+
82+
protected boolean skip() {
83+
return false;
84+
}
85+
86+
protected boolean containSkipClass(Class<?> clazz) {
87+
return false;
88+
}
89+
90+
protected boolean containSkipClass(AbstractFilter<?> filter) {
91+
return filter != null && containSkipClass(filter.getEntityClass());
92+
}
93+
94+
protected void handleQuery(AbstractQuery<?> query) {
95+
if (query instanceof SubQuery) {
96+
handleQuery(((SubQuery) query).getSubQuery());
97+
}
98+
handleSelectSubQuery(query.getSelectSubQueryList());
99+
handleAbstractFilter(query);
100+
if (query instanceof AbstractJoinQuery) {
101+
List<JoinQueryParam> joinList = ((AbstractJoinQuery<?>) query).getJoinAll();
102+
if (joinList != null) {
103+
for (JoinQueryParam joinQueryParam : joinList) {
104+
AbstractJoinQuery<?> joinQuery = joinQueryParam.getJoinQuery();
105+
if (joinQuery instanceof SubQuery) {
106+
handleQuery(((SubQuery) joinQuery).getSubQuery());
107+
} else {
108+
if (!containSkipClass(joinQuery)) {
109+
handleSelectSubQuery(joinQuery.getSelectSubQueryList());
110+
handleOnFilter(joinQueryParam, joinQueryParam::getOnFilters, joinQueryParam::on, joinQuery.getTableAs());
111+
}
112+
handleFilterSubQuery(joinQuery.getFilters());
113+
}
114+
}
115+
}
116+
}
117+
List<UnionQueryParam> unions = query.getUnions();
118+
if (unions != null) {
119+
unions.stream().map(UnionQueryParam::getUnion).forEach(this::handleQuery);
120+
}
121+
}
122+
123+
protected void handleUpdate(AbstractUpdate<?> update) {
124+
handleAbstractFilter(update);
125+
if (update instanceof EntityUpdate) {
126+
List<JoinUpdateParam> joinList = ((EntityUpdate) update).getJoinAll();
127+
if (joinList == null || joinList.isEmpty()) return;
128+
for (JoinUpdateParam joinUpdateParam : joinList) {
129+
EntityUpdate joinUpdate = joinUpdateParam.getJoinUpdate();
130+
if (!containSkipClass(joinUpdate)) {
131+
handleOnFilter(joinUpdateParam, joinUpdateParam::getOnFilters, joinUpdateParam::on, joinUpdate.getTableAs());
132+
}
133+
handleFilterSubQuery(joinUpdate.getFilters());
134+
}
135+
}
136+
}
137+
138+
protected void handleBatchInsert(BatchEntityInsert insert) {
139+
AbstractQuery<?> insertSelectQuery = insert.getInsertSelectQuery();
140+
if (insertSelectQuery != null) {
141+
if (handleQuery) {
142+
handleQuery(insertSelectQuery);
143+
}
144+
} else if (handleInsert) {
145+
handleInsert(insert.getEntityClass(), insert.getEntityList(), insert.getFieldValueMapList());
146+
}
147+
}
148+
149+
protected void handleAbstractFilter(AbstractFilter<?> abstractFilter) {
150+
if (abstractFilter == null) return;
151+
if (containSkipClass(abstractFilter)) {
152+
return;
153+
}
154+
handleFilter(abstractFilter);
155+
handleFilterSubQuery(abstractFilter.getFilters());
156+
}
157+
158+
protected void handleSelectSubQuery(List<SubQueryParam> list) {
159+
if (list == null || list.isEmpty()) return;
160+
for (SubQueryParam sub : list) {
161+
handleQuery(sub.getQuery());
162+
}
163+
}
164+
165+
protected void handleFilterSubQuery(List<Filter> filters) {
166+
if (filters == null || filters.isEmpty()) return;
167+
for (Filter filter : filters) {
168+
if (filter.isSpecial() && filter.getValue() instanceof SubQueryParam) {
169+
handleQuery(((SubQueryParam) filter.getValue()).getQuery());
170+
}
171+
}
172+
}
173+
174+
}

src/main/java/cn/veasion/db/interceptor/LogicDeleteInterceptor.java

Lines changed: 29 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,15 @@
33
import cn.veasion.db.AbstractFilter;
44
import cn.veasion.db.base.Expression;
55
import cn.veasion.db.base.Filter;
6-
import cn.veasion.db.query.AbstractJoinQuery;
7-
import cn.veasion.db.query.AbstractQuery;
8-
import cn.veasion.db.query.JoinQueryParam;
9-
import cn.veasion.db.query.SubQuery;
10-
import cn.veasion.db.query.SubQueryParam;
11-
import cn.veasion.db.query.UnionQueryParam;
126
import cn.veasion.db.update.AbstractUpdate;
137
import cn.veasion.db.update.Delete;
14-
import cn.veasion.db.update.EntityUpdate;
15-
import cn.veasion.db.update.JoinUpdateParam;
168
import cn.veasion.db.update.Update;
179
import cn.veasion.db.utils.FilterUtils;
1810

1911
import java.util.Arrays;
2012
import java.util.HashSet;
2113
import java.util.List;
14+
import java.util.Map;
2215
import java.util.Objects;
2316
import java.util.Set;
2417
import java.util.function.Consumer;
@@ -30,7 +23,7 @@
3023
* @author luozhuowei
3124
* @date 2021/12/6
3225
*/
33-
public class LogicDeleteInterceptor implements EntityDaoInterceptor {
26+
public class LogicDeleteInterceptor extends AbstractInterceptor {
3427

3528
private static ThreadLocal<Boolean> skipLogicDeleteFilter = new ThreadLocal<>();
3629
private static ThreadLocal<Set<Class<?>>> skipClassLogicDeleteFilter = new ThreadLocal<>();
@@ -48,6 +41,7 @@ public class LogicDeleteInterceptor implements EntityDaoInterceptor {
4841
* 如需要更新特殊为特殊值,如 id 或者 userId 时可以传 Expression 对象,如 Expression.update("${id}")
4942
*/
5043
public LogicDeleteInterceptor(String logicDeleteField, Object availableValue, Object deletedValue) {
44+
super(true, true, true, true, false);
5145
this.logicDeleteField = Objects.requireNonNull(logicDeleteField);
5246
this.availableValue = Objects.requireNonNull(availableValue);
5347
this.deletedValue = Objects.requireNonNull(deletedValue);
@@ -79,79 +73,25 @@ public static void clearSkip() {
7973
@Override
8074
public <R> R intercept(EntityDaoInvocation<R> invocation) {
8175
try {
82-
Object[] args = invocation.getArgs();
83-
if (args != null && !Boolean.TRUE.equals(skipLogicDeleteFilter.get())) {
84-
for (Object arg : args) {
85-
if (arg instanceof AbstractQuery) {
86-
handleQuery((AbstractQuery<?>) arg);
87-
} else if (arg instanceof AbstractUpdate) {
88-
handleUpdate((AbstractUpdate<?>) arg);
89-
} else if (arg instanceof Delete) {
90-
handleDelete((Delete) arg);
91-
} else if (arg instanceof AbstractFilter) {
92-
handleFilter((AbstractFilter<?>) arg);
93-
}
94-
}
95-
}
76+
return super.intercept(invocation);
9677
} finally {
9778
clearSkip();
9879
}
99-
return invocation.proceed();
100-
}
101-
102-
private boolean containSkipClass(AbstractFilter<?> filter) {
103-
return skipClassLogicDeleteFilter.get() != null &&
104-
skipClassLogicDeleteFilter.get().contains(filter.getEntityClass());
10580
}
10681

107-
private void handleQuery(AbstractQuery<?> query) {
108-
if (query instanceof SubQuery) {
109-
handleQuery(((SubQuery) query).getSubQuery());
110-
}
111-
handleSelectSubQuery(query.getSelectSubQueryList());
112-
handleFilter(query);
113-
if (query instanceof AbstractJoinQuery) {
114-
List<JoinQueryParam> joinList = ((AbstractJoinQuery<?>) query).getJoinAll();
115-
if (joinList != null) {
116-
for (JoinQueryParam joinQueryParam : joinList) {
117-
AbstractJoinQuery<?> joinQuery = joinQueryParam.getJoinQuery();
118-
if (joinQuery instanceof SubQuery) {
119-
handleQuery(((SubQuery) joinQuery).getSubQuery());
120-
} else {
121-
if (!containSkipClass(joinQuery)) {
122-
handleSelectSubQuery(joinQuery.getSelectSubQueryList());
123-
handleOnFilter(joinQueryParam::getOnFilters, joinQueryParam::on, joinQuery.getTableAs());
124-
}
125-
handleFilterSubQuery(joinQuery.getFilters());
126-
}
127-
}
128-
}
129-
}
130-
List<UnionQueryParam> unions = query.getUnions();
131-
if (unions != null) {
132-
unions.stream().map(UnionQueryParam::getUnion).forEach(this::handleQuery);
133-
}
82+
@Override
83+
protected boolean skip() {
84+
return Boolean.TRUE.equals(skipLogicDeleteFilter.get());
13485
}
13586

136-
private void handleUpdate(AbstractUpdate<?> update) {
137-
handleFilter(update);
138-
if (update instanceof EntityUpdate) {
139-
List<JoinUpdateParam> joinList = ((EntityUpdate) update).getJoinAll();
140-
if (joinList == null || joinList.isEmpty()) return;
141-
for (JoinUpdateParam joinUpdateParam : joinList) {
142-
EntityUpdate joinUpdate = joinUpdateParam.getJoinUpdate();
143-
if (!containSkipClass(joinUpdate)) {
144-
handleOnFilter(joinUpdateParam::getOnFilters, joinUpdateParam::on, joinUpdate.getTableAs());
145-
}
146-
handleFilterSubQuery(joinUpdate.getFilters());
147-
}
148-
}
87+
@Override
88+
protected boolean containSkipClass(Class<?> clazz) {
89+
return skipClassLogicDeleteFilter.get() != null &&
90+
skipClassLogicDeleteFilter.get().contains(clazz);
14991
}
15092

151-
private void handleDelete(Delete delete) {
152-
if (containSkipClass(delete)) {
153-
return;
154-
}
93+
@Override
94+
protected void handleDelete(Delete delete) {
15595
AbstractUpdate<?> convertUpdate = delete.getConvertUpdate();
15696
if (convertUpdate == null) {
15797
convertUpdate = new Update();
@@ -164,7 +104,15 @@ private void handleDelete(Delete delete) {
164104
delete.convertUpdate(convertUpdate);
165105
}
166106

167-
private void handleOnFilter(Supplier<List<Filter>> onFilters, Consumer<Filter> on, String tableAs) {
107+
@Override
108+
protected void handleFilter(AbstractFilter<?> abstractFilter) {
109+
if (!abstractFilter.hasFilter(logicDeleteField)) {
110+
abstractFilter.eq(logicDeleteField, availableValue);
111+
}
112+
}
113+
114+
@Override
115+
protected void handleOnFilter(Object joinParam, Supplier<List<Filter>> onFilters, Consumer<Filter> onMethod, String tableAs) {
168116
List<Filter> filters = onFilters.get();
169117
if (filters != null && !filters.isEmpty()) {
170118
for (Filter filter : filters) {
@@ -174,36 +122,17 @@ private void handleOnFilter(Supplier<List<Filter>> onFilters, Consumer<Filter> o
174122
}
175123
}
176124
}
177-
on.accept(Filter.AND);
178-
on.accept(Filter.eq(logicDeleteField, availableValue).fieldAs(tableAs));
179-
}
180-
181-
private void handleFilter(AbstractFilter<?> abstractFilter) {
182-
if (containSkipClass(abstractFilter)) {
183-
return;
184-
}
185-
if (abstractFilter != null) {
186-
if (!abstractFilter.hasFilter(logicDeleteField)) {
187-
abstractFilter.eq(logicDeleteField, availableValue);
188-
}
189-
handleFilterSubQuery(abstractFilter.getFilters());
190-
}
125+
onMethod.accept(Filter.AND);
126+
onMethod.accept(Filter.eq(logicDeleteField, availableValue).fieldAs(tableAs));
191127
}
192128

193-
private void handleSelectSubQuery(List<SubQueryParam> list) {
194-
if (list == null || list.isEmpty()) return;
195-
for (SubQueryParam sub : list) {
196-
handleQuery(sub.getQuery());
197-
}
129+
@Override
130+
protected void handleInsert(Class<?> entityClass, List<?> entityList, List<Map<String, Object>> fieldValueMapList) {
198131
}
199132

200-
private void handleFilterSubQuery(List<Filter> filters) {
201-
if (filters == null || filters.isEmpty()) return;
202-
for (Filter filter : filters) {
203-
if (filter.isSpecial() && filter.getValue() instanceof SubQueryParam) {
204-
handleQuery(((SubQueryParam) filter.getValue()).getQuery());
205-
}
206-
}
133+
@Override
134+
public int sortIndex() {
135+
return -1;
207136
}
208137

209138
}

0 commit comments

Comments
 (0)