提交 | 用户 | 时间
|
a6de49
|
1 |
package com.iailab.framework.datapermission.core.db; |
H |
2 |
|
|
3 |
import com.baomidou.mybatisplus.extension.plugins.inner.DataPermissionInterceptor; |
|
4 |
import com.iailab.framework.datapermission.core.rule.DataPermissionRule; |
|
5 |
import com.iailab.framework.datapermission.core.rule.DataPermissionRuleFactory; |
|
6 |
import com.iailab.framework.mybatis.core.util.MyBatisUtils; |
|
7 |
import com.iailab.framework.test.core.ut.BaseMockitoUnitTest; |
|
8 |
import net.sf.jsqlparser.expression.Alias; |
|
9 |
import net.sf.jsqlparser.expression.Expression; |
|
10 |
import net.sf.jsqlparser.expression.LongValue; |
|
11 |
import net.sf.jsqlparser.expression.Parenthesis; |
|
12 |
import net.sf.jsqlparser.expression.operators.relational.EqualsTo; |
|
13 |
import net.sf.jsqlparser.expression.operators.relational.ExpressionList; |
|
14 |
import net.sf.jsqlparser.expression.operators.relational.InExpression; |
|
15 |
import net.sf.jsqlparser.schema.Column; |
|
16 |
import org.junit.jupiter.api.BeforeEach; |
|
17 |
import org.junit.jupiter.api.Test; |
|
18 |
import org.mockito.InjectMocks; |
|
19 |
import org.mockito.Mock; |
|
20 |
|
|
21 |
import java.util.Arrays; |
|
22 |
import java.util.Set; |
|
23 |
|
|
24 |
import static com.iailab.framework.common.util.collection.SetUtils.asSet; |
|
25 |
import static org.junit.jupiter.api.Assertions.assertEquals; |
|
26 |
import static org.mockito.ArgumentMatchers.any; |
|
27 |
import static org.mockito.Mockito.when; |
|
28 |
|
|
29 |
/** |
|
30 |
* {@link DataPermissionRuleHandler} 的单元测试 |
|
31 |
* 主要复用了 MyBatis Plus 的 TenantLineInnerInterceptorTest 的单元测试 |
|
32 |
* 不过它的单元测试不是很规范,考虑到是复用的,所以暂时不进行修改~ |
|
33 |
* |
4d4165
|
34 |
* @author iailab |
a6de49
|
35 |
*/ |
H |
36 |
public class DataPermissionRuleHandlerTest extends BaseMockitoUnitTest { |
|
37 |
|
|
38 |
@InjectMocks |
|
39 |
private DataPermissionRuleHandler handler; |
|
40 |
|
|
41 |
@Mock |
|
42 |
private DataPermissionRuleFactory ruleFactory; |
|
43 |
|
|
44 |
private DataPermissionInterceptor interceptor; |
|
45 |
|
|
46 |
@BeforeEach |
|
47 |
public void setUp() { |
|
48 |
interceptor = new DataPermissionInterceptor(handler); |
|
49 |
|
|
50 |
// 租户的数据权限规则 |
|
51 |
DataPermissionRule tenantRule = new DataPermissionRule() { |
|
52 |
|
|
53 |
private static final String COLUMN = "tenant_id"; |
|
54 |
|
|
55 |
@Override |
|
56 |
public Set<String> getTableNames() { |
|
57 |
return asSet("entity", "entity1", "entity2", "entity3", "t1", "t2", "sys_dict_item", // 支持 MyBatis Plus 的单元测试 |
|
58 |
"t_user", "t_role"); // 满足自己的单元测试 |
|
59 |
} |
|
60 |
|
|
61 |
@Override |
|
62 |
public Expression getExpression(String tableName, Alias tableAlias) { |
|
63 |
Column column = MyBatisUtils.buildColumn(tableName, tableAlias, COLUMN); |
|
64 |
LongValue value = new LongValue(1L); |
|
65 |
return new EqualsTo(column, value); |
|
66 |
} |
|
67 |
|
|
68 |
}; |
|
69 |
// 部门的数据权限规则 |
|
70 |
DataPermissionRule deptRule = new DataPermissionRule() { |
|
71 |
|
|
72 |
private static final String COLUMN = "dept_id"; |
|
73 |
|
|
74 |
@Override |
|
75 |
public Set<String> getTableNames() { |
|
76 |
return asSet("t_user"); // 满足自己的单元测试 |
|
77 |
} |
|
78 |
|
|
79 |
@Override |
|
80 |
public Expression getExpression(String tableName, Alias tableAlias) { |
|
81 |
Column column = MyBatisUtils.buildColumn(tableName, tableAlias, COLUMN); |
|
82 |
ExpressionList<LongValue> values = new ExpressionList<>(new LongValue(10L), |
|
83 |
new LongValue(20L)); |
|
84 |
return new InExpression(column, new Parenthesis((values))); |
|
85 |
} |
|
86 |
|
|
87 |
}; |
|
88 |
// 设置到上下文 |
|
89 |
when(ruleFactory.getDataPermissionRule(any())).thenReturn(Arrays.asList(tenantRule, deptRule)); |
|
90 |
} |
|
91 |
|
|
92 |
@Test |
|
93 |
void delete() { |
|
94 |
assertSql("delete from entity where id = ?", |
|
95 |
"DELETE FROM entity WHERE id = ? AND entity.tenant_id = 1"); |
|
96 |
} |
|
97 |
|
|
98 |
@Test |
|
99 |
void update() { |
|
100 |
assertSql("update entity set name = ? where id = ?", |
|
101 |
"UPDATE entity SET name = ? WHERE id = ? AND entity.tenant_id = 1"); |
|
102 |
} |
|
103 |
|
|
104 |
@Test |
|
105 |
void selectSingle() { |
|
106 |
// 单表 |
|
107 |
assertSql("select * from entity where id = ?", |
|
108 |
"SELECT * FROM entity WHERE id = ? AND entity.tenant_id = 1"); |
|
109 |
|
|
110 |
assertSql("select * from entity where id = ? or name = ?", |
|
111 |
"SELECT * FROM entity WHERE (id = ? OR name = ?) AND entity.tenant_id = 1"); |
|
112 |
|
|
113 |
assertSql("SELECT * FROM entity WHERE (id = ? OR name = ?)", |
|
114 |
"SELECT * FROM entity WHERE (id = ? OR name = ?) AND entity.tenant_id = 1"); |
|
115 |
|
|
116 |
/* not */ |
|
117 |
assertSql("SELECT * FROM entity WHERE not (id = ? OR name = ?)", |
|
118 |
"SELECT * FROM entity WHERE NOT (id = ? OR name = ?) AND entity.tenant_id = 1"); |
|
119 |
} |
|
120 |
|
|
121 |
@Test |
|
122 |
void selectSubSelectIn() { |
|
123 |
/* in */ |
|
124 |
assertSql("SELECT * FROM entity e WHERE e.id IN (select e1.id from entity1 e1 where e1.id = ?)", |
|
125 |
"SELECT * FROM entity e WHERE e.id IN (SELECT e1.id FROM entity1 e1 WHERE e1.id = ? AND e1.tenant_id = 1) AND e.tenant_id = 1"); |
|
126 |
// 在最前 |
|
127 |
assertSql("SELECT * FROM entity e WHERE e.id IN " + |
|
128 |
"(select e1.id from entity1 e1 where e1.id = ?) and e.id = ?", |
|
129 |
"SELECT * FROM entity e WHERE e.id IN " + |
|
130 |
"(SELECT e1.id FROM entity1 e1 WHERE e1.id = ? AND e1.tenant_id = 1) AND e.id = ? AND e.tenant_id = 1"); |
|
131 |
// 在最后 |
|
132 |
assertSql("SELECT * FROM entity e WHERE e.id = ? and e.id IN " + |
|
133 |
"(select e1.id from entity1 e1 where e1.id = ?)", |
|
134 |
"SELECT * FROM entity e WHERE e.id = ? AND e.id IN " + |
|
135 |
"(SELECT e1.id FROM entity1 e1 WHERE e1.id = ? AND e1.tenant_id = 1) AND e.tenant_id = 1"); |
|
136 |
// 在中间 |
|
137 |
assertSql("SELECT * FROM entity e WHERE e.id = ? and e.id IN " + |
|
138 |
"(select e1.id from entity1 e1 where e1.id = ?) and e.id = ?", |
|
139 |
"SELECT * FROM entity e WHERE e.id = ? AND e.id IN " + |
|
140 |
"(SELECT e1.id FROM entity1 e1 WHERE e1.id = ? AND e1.tenant_id = 1) AND e.id = ? AND e.tenant_id = 1"); |
|
141 |
} |
|
142 |
|
|
143 |
@Test |
|
144 |
void selectSubSelectEq() { |
|
145 |
/* = */ |
|
146 |
assertSql("SELECT * FROM entity e WHERE e.id = (select e1.id from entity1 e1 where e1.id = ?)", |
|
147 |
"SELECT * FROM entity e WHERE e.id = (SELECT e1.id FROM entity1 e1 WHERE e1.id = ? AND e1.tenant_id = 1) AND e.tenant_id = 1"); |
|
148 |
} |
|
149 |
|
|
150 |
@Test |
|
151 |
void selectSubSelectInnerNotEq() { |
|
152 |
/* inner not = */ |
|
153 |
assertSql("SELECT * FROM entity e WHERE not (e.id = (select e1.id from entity1 e1 where e1.id = ?))", |
|
154 |
"SELECT * FROM entity e WHERE NOT (e.id = (SELECT e1.id FROM entity1 e1 WHERE e1.id = ? AND e1.tenant_id = 1)) AND e.tenant_id = 1"); |
|
155 |
|
|
156 |
assertSql("SELECT * FROM entity e WHERE not (e.id = (select e1.id from entity1 e1 where e1.id = ?) and e.id = ?)", |
|
157 |
"SELECT * FROM entity e WHERE NOT (e.id = (SELECT e1.id FROM entity1 e1 WHERE e1.id = ? AND e1.tenant_id = 1) AND e.id = ?) AND e.tenant_id = 1"); |
|
158 |
} |
|
159 |
|
|
160 |
@Test |
|
161 |
void selectSubSelectExists() { |
|
162 |
/* EXISTS */ |
|
163 |
assertSql("SELECT * FROM entity e WHERE EXISTS (select e1.id from entity1 e1 where e1.id = ?)", |
|
164 |
"SELECT * FROM entity e WHERE EXISTS (SELECT e1.id FROM entity1 e1 WHERE e1.id = ? AND e1.tenant_id = 1) AND e.tenant_id = 1"); |
|
165 |
|
|
166 |
|
|
167 |
/* NOT EXISTS */ |
|
168 |
assertSql("SELECT * FROM entity e WHERE NOT EXISTS (select e1.id from entity1 e1 where e1.id = ?)", |
|
169 |
"SELECT * FROM entity e WHERE NOT EXISTS (SELECT e1.id FROM entity1 e1 WHERE e1.id = ? AND e1.tenant_id = 1) AND e.tenant_id = 1"); |
|
170 |
} |
|
171 |
|
|
172 |
@Test |
|
173 |
void selectSubSelect() { |
|
174 |
/* >= */ |
|
175 |
assertSql("SELECT * FROM entity e WHERE e.id >= (select e1.id from entity1 e1 where e1.id = ?)", |
|
176 |
"SELECT * FROM entity e WHERE e.id >= (SELECT e1.id FROM entity1 e1 WHERE e1.id = ? AND e1.tenant_id = 1) AND e.tenant_id = 1"); |
|
177 |
|
|
178 |
|
|
179 |
/* <= */ |
|
180 |
assertSql("SELECT * FROM entity e WHERE e.id <= (select e1.id from entity1 e1 where e1.id = ?)", |
|
181 |
"SELECT * FROM entity e WHERE e.id <= (SELECT e1.id FROM entity1 e1 WHERE e1.id = ? AND e1.tenant_id = 1) AND e.tenant_id = 1"); |
|
182 |
|
|
183 |
|
|
184 |
/* <> */ |
|
185 |
assertSql("SELECT * FROM entity e WHERE e.id <> (select e1.id from entity1 e1 where e1.id = ?)", |
|
186 |
"SELECT * FROM entity e WHERE e.id <> (SELECT e1.id FROM entity1 e1 WHERE e1.id = ? AND e1.tenant_id = 1) AND e.tenant_id = 1"); |
|
187 |
} |
|
188 |
|
|
189 |
@Test |
|
190 |
void selectFromSelect() { |
|
191 |
assertSql("SELECT * FROM (select e.id from entity e WHERE e.id = (select e1.id from entity1 e1 where e1.id = ?))", |
|
192 |
"SELECT * FROM (SELECT e.id FROM entity e WHERE e.id = (SELECT e1.id FROM entity1 e1 WHERE e1.id = ? AND e1.tenant_id = 1) AND e.tenant_id = 1)"); |
|
193 |
} |
|
194 |
|
|
195 |
@Test |
|
196 |
void selectBodySubSelect() { |
|
197 |
assertSql("select t1.col1,(select t2.col2 from t2 t2 where t1.col1=t2.col1) from t1 t1", |
|
198 |
"SELECT t1.col1, (SELECT t2.col2 FROM t2 t2 WHERE t1.col1 = t2.col1 AND t2.tenant_id = 1) FROM t1 t1 WHERE t1.tenant_id = 1"); |
|
199 |
} |
|
200 |
|
|
201 |
@Test |
|
202 |
void selectLeftJoin() { |
|
203 |
// left join |
|
204 |
assertSql("SELECT * FROM entity e " + |
|
205 |
"left join entity1 e1 on e1.id = e.id " + |
|
206 |
"WHERE e.id = ? OR e.name = ?", |
|
207 |
"SELECT * FROM entity e " + |
|
208 |
"LEFT JOIN entity1 e1 ON e1.id = e.id AND e1.tenant_id = 1 " + |
|
209 |
"WHERE (e.id = ? OR e.name = ?) AND e.tenant_id = 1"); |
|
210 |
|
|
211 |
assertSql("SELECT * FROM entity e " + |
|
212 |
"left join entity1 e1 on e1.id = e.id " + |
|
213 |
"WHERE (e.id = ? OR e.name = ?)", |
|
214 |
"SELECT * FROM entity e " + |
|
215 |
"LEFT JOIN entity1 e1 ON e1.id = e.id AND e1.tenant_id = 1 " + |
|
216 |
"WHERE (e.id = ? OR e.name = ?) AND e.tenant_id = 1"); |
|
217 |
|
|
218 |
assertSql("SELECT * FROM entity e " + |
|
219 |
"left join entity1 e1 on e1.id = e.id " + |
|
220 |
"left join entity2 e2 on e1.id = e2.id", |
|
221 |
"SELECT * FROM entity e " + |
|
222 |
"LEFT JOIN entity1 e1 ON e1.id = e.id AND e1.tenant_id = 1 " + |
|
223 |
"LEFT JOIN entity2 e2 ON e1.id = e2.id AND e2.tenant_id = 1 " + |
|
224 |
"WHERE e.tenant_id = 1"); |
|
225 |
} |
|
226 |
|
|
227 |
@Test |
|
228 |
void selectRightJoin() { |
|
229 |
// right join |
|
230 |
assertSql("SELECT * FROM entity e " + |
|
231 |
"right join entity1 e1 on e1.id = e.id", |
|
232 |
"SELECT * FROM entity e " + |
|
233 |
"RIGHT JOIN entity1 e1 ON e1.id = e.id AND e.tenant_id = 1 " + |
|
234 |
"WHERE e1.tenant_id = 1"); |
|
235 |
|
|
236 |
assertSql("SELECT * FROM with_as_1 e " + |
|
237 |
"right join entity1 e1 on e1.id = e.id", |
|
238 |
"SELECT * FROM with_as_1 e " + |
|
239 |
"RIGHT JOIN entity1 e1 ON e1.id = e.id " + |
|
240 |
"WHERE e1.tenant_id = 1"); |
|
241 |
|
|
242 |
assertSql("SELECT * FROM entity e " + |
|
243 |
"right join entity1 e1 on e1.id = e.id " + |
|
244 |
"WHERE e.id = ? OR e.name = ?", |
|
245 |
"SELECT * FROM entity e " + |
|
246 |
"RIGHT JOIN entity1 e1 ON e1.id = e.id AND e.tenant_id = 1 " + |
|
247 |
"WHERE (e.id = ? OR e.name = ?) AND e1.tenant_id = 1"); |
|
248 |
|
|
249 |
assertSql("SELECT * FROM entity e " + |
|
250 |
"right join entity1 e1 on e1.id = e.id " + |
|
251 |
"right join entity2 e2 on e1.id = e2.id ", |
|
252 |
"SELECT * FROM entity e " + |
|
253 |
"RIGHT JOIN entity1 e1 ON e1.id = e.id AND e.tenant_id = 1 " + |
|
254 |
"RIGHT JOIN entity2 e2 ON e1.id = e2.id AND e1.tenant_id = 1 " + |
|
255 |
"WHERE e2.tenant_id = 1"); |
|
256 |
} |
|
257 |
|
|
258 |
@Test |
|
259 |
void selectMixJoin() { |
|
260 |
assertSql("SELECT * FROM entity e " + |
|
261 |
"right join entity1 e1 on e1.id = e.id " + |
|
262 |
"left join entity2 e2 on e1.id = e2.id", |
|
263 |
"SELECT * FROM entity e " + |
|
264 |
"RIGHT JOIN entity1 e1 ON e1.id = e.id AND e.tenant_id = 1 " + |
|
265 |
"LEFT JOIN entity2 e2 ON e1.id = e2.id AND e2.tenant_id = 1 " + |
|
266 |
"WHERE e1.tenant_id = 1"); |
|
267 |
|
|
268 |
assertSql("SELECT * FROM entity e " + |
|
269 |
"left join entity1 e1 on e1.id = e.id " + |
|
270 |
"right join entity2 e2 on e1.id = e2.id", |
|
271 |
"SELECT * FROM entity e " + |
|
272 |
"LEFT JOIN entity1 e1 ON e1.id = e.id AND e1.tenant_id = 1 " + |
|
273 |
"RIGHT JOIN entity2 e2 ON e1.id = e2.id AND e.tenant_id = 1 " + |
|
274 |
"WHERE e2.tenant_id = 1"); |
|
275 |
|
|
276 |
assertSql("SELECT * FROM entity e " + |
|
277 |
"left join entity1 e1 on e1.id = e.id " + |
|
278 |
"inner join entity2 e2 on e1.id = e2.id", |
|
279 |
"SELECT * FROM entity e " + |
|
280 |
"LEFT JOIN entity1 e1 ON e1.id = e.id AND e1.tenant_id = 1 " + |
|
281 |
"INNER JOIN entity2 e2 ON e1.id = e2.id AND e.tenant_id = 1 AND e2.tenant_id = 1"); |
|
282 |
} |
|
283 |
|
|
284 |
|
|
285 |
@Test |
|
286 |
void selectJoinSubSelect() { |
|
287 |
assertSql("select * from (select * from entity) e1 " + |
|
288 |
"left join entity2 e2 on e1.id = e2.id", |
|
289 |
"SELECT * FROM (SELECT * FROM entity WHERE entity.tenant_id = 1) e1 " + |
|
290 |
"LEFT JOIN entity2 e2 ON e1.id = e2.id AND e2.tenant_id = 1"); |
|
291 |
|
|
292 |
assertSql("select * from entity1 e1 " + |
|
293 |
"left join (select * from entity2) e2 " + |
|
294 |
"on e1.id = e2.id", |
|
295 |
"SELECT * FROM entity1 e1 " + |
|
296 |
"LEFT JOIN (SELECT * FROM entity2 WHERE entity2.tenant_id = 1) e2 " + |
|
297 |
"ON e1.id = e2.id " + |
|
298 |
"WHERE e1.tenant_id = 1"); |
|
299 |
} |
|
300 |
|
|
301 |
@Test |
|
302 |
void selectSubJoin() { |
|
303 |
|
|
304 |
assertSql("select * FROM " + |
|
305 |
"(entity1 e1 right JOIN entity2 e2 ON e1.id = e2.id)", |
|
306 |
"SELECT * FROM " + |
|
307 |
"(entity1 e1 RIGHT JOIN entity2 e2 ON e1.id = e2.id AND e1.tenant_id = 1) " + |
|
308 |
"WHERE e2.tenant_id = 1"); |
|
309 |
|
|
310 |
assertSql("select * FROM " + |
|
311 |
"(entity1 e1 LEFT JOIN entity2 e2 ON e1.id = e2.id)", |
|
312 |
"SELECT * FROM " + |
|
313 |
"(entity1 e1 LEFT JOIN entity2 e2 ON e1.id = e2.id AND e2.tenant_id = 1) " + |
|
314 |
"WHERE e1.tenant_id = 1"); |
|
315 |
|
|
316 |
|
|
317 |
assertSql("select * FROM " + |
|
318 |
"(entity1 e1 LEFT JOIN entity2 e2 ON e1.id = e2.id) " + |
|
319 |
"right join entity3 e3 on e1.id = e3.id", |
|
320 |
"SELECT * FROM " + |
|
321 |
"(entity1 e1 LEFT JOIN entity2 e2 ON e1.id = e2.id AND e2.tenant_id = 1) " + |
|
322 |
"RIGHT JOIN entity3 e3 ON e1.id = e3.id AND e1.tenant_id = 1 " + |
|
323 |
"WHERE e3.tenant_id = 1"); |
|
324 |
|
|
325 |
|
|
326 |
assertSql("select * FROM entity e " + |
|
327 |
"LEFT JOIN (entity1 e1 right join entity2 e2 ON e1.id = e2.id) " + |
|
328 |
"on e.id = e2.id", |
|
329 |
"SELECT * FROM entity e " + |
|
330 |
"LEFT JOIN (entity1 e1 RIGHT JOIN entity2 e2 ON e1.id = e2.id AND e1.tenant_id = 1) " + |
|
331 |
"ON e.id = e2.id AND e2.tenant_id = 1 " + |
|
332 |
"WHERE e.tenant_id = 1"); |
|
333 |
|
|
334 |
assertSql("select * FROM entity e " + |
|
335 |
"LEFT JOIN (entity1 e1 left join entity2 e2 ON e1.id = e2.id) " + |
|
336 |
"on e.id = e2.id", |
|
337 |
"SELECT * FROM entity e " + |
|
338 |
"LEFT JOIN (entity1 e1 LEFT JOIN entity2 e2 ON e1.id = e2.id AND e2.tenant_id = 1) " + |
|
339 |
"ON e.id = e2.id AND e1.tenant_id = 1 " + |
|
340 |
"WHERE e.tenant_id = 1"); |
|
341 |
|
|
342 |
assertSql("select * FROM entity e " + |
|
343 |
"RIGHT JOIN (entity1 e1 left join entity2 e2 ON e1.id = e2.id) " + |
|
344 |
"on e.id = e2.id", |
|
345 |
"SELECT * FROM entity e " + |
|
346 |
"RIGHT JOIN (entity1 e1 LEFT JOIN entity2 e2 ON e1.id = e2.id AND e2.tenant_id = 1) " + |
|
347 |
"ON e.id = e2.id AND e.tenant_id = 1 " + |
|
348 |
"WHERE e1.tenant_id = 1"); |
|
349 |
} |
|
350 |
|
|
351 |
|
|
352 |
@Test |
|
353 |
void selectLeftJoinMultipleTrailingOn() { |
|
354 |
// 多个 on 尾缀的 |
|
355 |
assertSql("SELECT * FROM entity e " + |
|
356 |
"LEFT JOIN entity1 e1 " + |
|
357 |
"LEFT JOIN entity2 e2 ON e2.id = e1.id " + |
|
358 |
"ON e1.id = e.id " + |
|
359 |
"WHERE (e.id = ? OR e.NAME = ?)", |
|
360 |
"SELECT * FROM entity e " + |
|
361 |
"LEFT JOIN entity1 e1 " + |
|
362 |
"LEFT JOIN entity2 e2 ON e2.id = e1.id AND e2.tenant_id = 1 " + |
|
363 |
"ON e1.id = e.id AND e1.tenant_id = 1 " + |
|
364 |
"WHERE (e.id = ? OR e.NAME = ?) AND e.tenant_id = 1"); |
|
365 |
|
|
366 |
assertSql("SELECT * FROM entity e " + |
|
367 |
"LEFT JOIN entity1 e1 " + |
|
368 |
"LEFT JOIN with_as_A e2 ON e2.id = e1.id " + |
|
369 |
"ON e1.id = e.id " + |
|
370 |
"WHERE (e.id = ? OR e.NAME = ?)", |
|
371 |
"SELECT * FROM entity e " + |
|
372 |
"LEFT JOIN entity1 e1 " + |
|
373 |
"LEFT JOIN with_as_A e2 ON e2.id = e1.id " + |
|
374 |
"ON e1.id = e.id AND e1.tenant_id = 1 " + |
|
375 |
"WHERE (e.id = ? OR e.NAME = ?) AND e.tenant_id = 1"); |
|
376 |
} |
|
377 |
|
|
378 |
@Test |
|
379 |
void selectInnerJoin() { |
|
380 |
// inner join |
|
381 |
assertSql("SELECT * FROM entity e " + |
|
382 |
"inner join entity1 e1 on e1.id = e.id " + |
|
383 |
"WHERE e.id = ? OR e.name = ?", |
|
384 |
"SELECT * FROM entity e " + |
|
385 |
"INNER JOIN entity1 e1 ON e1.id = e.id AND e.tenant_id = 1 AND e1.tenant_id = 1 " + |
|
386 |
"WHERE e.id = ? OR e.name = ?"); |
|
387 |
|
|
388 |
assertSql("SELECT * FROM entity e " + |
|
389 |
"inner join entity1 e1 on e1.id = e.id " + |
|
390 |
"WHERE (e.id = ? OR e.name = ?)", |
|
391 |
"SELECT * FROM entity e " + |
|
392 |
"INNER JOIN entity1 e1 ON e1.id = e.id AND e.tenant_id = 1 AND e1.tenant_id = 1 " + |
|
393 |
"WHERE (e.id = ? OR e.name = ?)"); |
|
394 |
|
|
395 |
// 隐式内连接 |
|
396 |
assertSql("SELECT * FROM entity,entity1 " + |
|
397 |
"WHERE entity.id = entity1.id", |
|
398 |
"SELECT * FROM entity, entity1 " + |
|
399 |
"WHERE entity.id = entity1.id AND entity.tenant_id = 1 AND entity1.tenant_id = 1"); |
|
400 |
|
|
401 |
// 隐式内连接 |
|
402 |
assertSql("SELECT * FROM entity a, with_as_entity1 b " + |
|
403 |
"WHERE a.id = b.id", |
|
404 |
"SELECT * FROM entity a, with_as_entity1 b " + |
|
405 |
"WHERE a.id = b.id AND a.tenant_id = 1"); |
|
406 |
|
|
407 |
assertSql("SELECT * FROM with_as_entity a, with_as_entity1 b " + |
|
408 |
"WHERE a.id = b.id", |
|
409 |
"SELECT * FROM with_as_entity a, with_as_entity1 b " + |
|
410 |
"WHERE a.id = b.id"); |
|
411 |
|
|
412 |
// SubJoin with 隐式内连接 |
|
413 |
assertSql("SELECT * FROM (entity,entity1) " + |
|
414 |
"WHERE entity.id = entity1.id", |
|
415 |
"SELECT * FROM (entity, entity1) " + |
|
416 |
"WHERE entity.id = entity1.id " + |
|
417 |
"AND entity.tenant_id = 1 AND entity1.tenant_id = 1"); |
|
418 |
|
|
419 |
assertSql("SELECT * FROM ((entity,entity1),entity2) " + |
|
420 |
"WHERE entity.id = entity1.id and entity.id = entity2.id", |
|
421 |
"SELECT * FROM ((entity, entity1), entity2) " + |
|
422 |
"WHERE entity.id = entity1.id AND entity.id = entity2.id " + |
|
423 |
"AND entity.tenant_id = 1 AND entity1.tenant_id = 1 AND entity2.tenant_id = 1"); |
|
424 |
|
|
425 |
assertSql("SELECT * FROM (entity,(entity1,entity2)) " + |
|
426 |
"WHERE entity.id = entity1.id and entity.id = entity2.id", |
|
427 |
"SELECT * FROM (entity, (entity1, entity2)) " + |
|
428 |
"WHERE entity.id = entity1.id AND entity.id = entity2.id " + |
|
429 |
"AND entity.tenant_id = 1 AND entity1.tenant_id = 1 AND entity2.tenant_id = 1"); |
|
430 |
|
|
431 |
// 沙雕的括号写法 |
|
432 |
assertSql("SELECT * FROM (((entity,entity1))) " + |
|
433 |
"WHERE entity.id = entity1.id", |
|
434 |
"SELECT * FROM (((entity, entity1))) " + |
|
435 |
"WHERE entity.id = entity1.id " + |
|
436 |
"AND entity.tenant_id = 1 AND entity1.tenant_id = 1"); |
|
437 |
|
|
438 |
} |
|
439 |
|
|
440 |
|
|
441 |
@Test |
|
442 |
void selectWithAs() { |
|
443 |
assertSql("with with_as_A as (select * from entity) select * from with_as_A", |
|
444 |
"WITH with_as_A AS (SELECT * FROM entity WHERE entity.tenant_id = 1) SELECT * FROM with_as_A"); |
|
445 |
} |
|
446 |
|
|
447 |
|
|
448 |
@Test |
|
449 |
void selectIgnoreTable() { |
|
450 |
assertSql(" SELECT dict.dict_code, item.item_text AS \"text\", item.item_value AS \"value\" FROM sys_dict_item item INNER JOIN sys_dict dict ON dict.id = item.dict_id WHERE dict.dict_code IN (1, 2, 3) AND item.item_value IN (1, 2, 3)", |
|
451 |
"SELECT dict.dict_code, item.item_text AS \"text\", item.item_value AS \"value\" FROM sys_dict_item item INNER JOIN sys_dict dict ON dict.id = item.dict_id AND item.tenant_id = 1 WHERE dict.dict_code IN (1, 2, 3) AND item.item_value IN (1, 2, 3)"); |
|
452 |
} |
|
453 |
|
|
454 |
private void assertSql(String sql, String targetSql) { |
|
455 |
assertEquals(targetSql, interceptor.parserSingle(sql, null)); |
|
456 |
} |
|
457 |
|
|
458 |
// ========== 额外的测试 ========== |
|
459 |
|
|
460 |
@Test |
|
461 |
public void testSelectSingle() { |
|
462 |
// 单表 |
|
463 |
assertSql("select * from t_user where id = ?", |
|
464 |
"SELECT * FROM t_user WHERE id = ? AND t_user.tenant_id = 1 AND t_user.dept_id IN (10, 20)"); |
|
465 |
|
|
466 |
assertSql("select * from t_user where id = ? or name = ?", |
|
467 |
"SELECT * FROM t_user WHERE (id = ? OR name = ?) AND t_user.tenant_id = 1 AND t_user.dept_id IN (10, 20)"); |
|
468 |
|
|
469 |
assertSql("SELECT * FROM t_user WHERE (id = ? OR name = ?)", |
|
470 |
"SELECT * FROM t_user WHERE (id = ? OR name = ?) AND t_user.tenant_id = 1 AND t_user.dept_id IN (10, 20)"); |
|
471 |
|
|
472 |
/* not */ |
|
473 |
assertSql("SELECT * FROM t_user WHERE not (id = ? OR name = ?)", |
|
474 |
"SELECT * FROM t_user WHERE NOT (id = ? OR name = ?) AND t_user.tenant_id = 1 AND t_user.dept_id IN (10, 20)"); |
|
475 |
} |
|
476 |
|
|
477 |
@Test |
|
478 |
public void testSelectLeftJoin() { |
|
479 |
// left join |
|
480 |
assertSql("SELECT * FROM t_user e " + |
|
481 |
"left join t_role e1 on e1.id = e.id " + |
|
482 |
"WHERE e.id = ? OR e.name = ?", |
|
483 |
"SELECT * FROM t_user e " + |
|
484 |
"LEFT JOIN t_role e1 ON e1.id = e.id AND e1.tenant_id = 1 " + |
|
485 |
"WHERE (e.id = ? OR e.name = ?) AND e.tenant_id = 1 AND e.dept_id IN (10, 20)"); |
|
486 |
|
|
487 |
// 条件 e.id = ? OR e.name = ? 带括号 |
|
488 |
assertSql("SELECT * FROM t_user e " + |
|
489 |
"left join t_role e1 on e1.id = e.id " + |
|
490 |
"WHERE (e.id = ? OR e.name = ?)", |
|
491 |
"SELECT * FROM t_user e " + |
|
492 |
"LEFT JOIN t_role e1 ON e1.id = e.id AND e1.tenant_id = 1 " + |
|
493 |
"WHERE (e.id = ? OR e.name = ?) AND e.tenant_id = 1 AND e.dept_id IN (10, 20)"); |
|
494 |
} |
|
495 |
|
|
496 |
@Test |
|
497 |
public void testSelectRightJoin() { |
|
498 |
// right join |
|
499 |
assertSql("SELECT * FROM t_user e " + |
|
500 |
"right join t_role e1 on e1.id = e.id " + |
|
501 |
"WHERE e.id = ? OR e.name = ?", |
|
502 |
"SELECT * FROM t_user e " + |
|
503 |
"RIGHT JOIN t_role e1 ON e1.id = e.id AND e.tenant_id = 1 AND e.dept_id IN (10, 20) " + |
|
504 |
"WHERE (e.id = ? OR e.name = ?) AND e1.tenant_id = 1"); |
|
505 |
|
|
506 |
// 条件 e.id = ? OR e.name = ? 带括号 |
|
507 |
assertSql("SELECT * FROM t_user e " + |
|
508 |
"right join t_role e1 on e1.id = e.id " + |
|
509 |
"WHERE (e.id = ? OR e.name = ?)", |
|
510 |
"SELECT * FROM t_user e " + |
|
511 |
"RIGHT JOIN t_role e1 ON e1.id = e.id AND e.tenant_id = 1 AND e.dept_id IN (10, 20) " + |
|
512 |
"WHERE (e.id = ? OR e.name = ?) AND e1.tenant_id = 1"); |
|
513 |
} |
|
514 |
|
|
515 |
@Test |
|
516 |
public void testSelectInnerJoin() { |
|
517 |
// inner join |
|
518 |
assertSql("SELECT * FROM t_user e " + |
|
519 |
"inner join entity1 e1 on e1.id = e.id " + |
|
520 |
"WHERE e.id = ? OR e.name = ?", |
|
521 |
"SELECT * FROM t_user e " + |
|
522 |
"INNER JOIN entity1 e1 ON e1.id = e.id AND e.tenant_id = 1 AND e.dept_id IN (10, 20) AND e1.tenant_id = 1 " + |
|
523 |
"WHERE e.id = ? OR e.name = ?"); |
|
524 |
|
|
525 |
// 条件 e.id = ? OR e.name = ? 带括号 |
|
526 |
assertSql("SELECT * FROM t_user e " + |
|
527 |
"inner join entity1 e1 on e1.id = e.id " + |
|
528 |
"WHERE (e.id = ? OR e.name = ?)", |
|
529 |
"SELECT * FROM t_user e " + |
|
530 |
"INNER JOIN entity1 e1 ON e1.id = e.id AND e.tenant_id = 1 AND e.dept_id IN (10, 20) AND e1.tenant_id = 1 " + |
|
531 |
"WHERE (e.id = ? OR e.name = ?)"); |
|
532 |
|
|
533 |
// 没有 On 的 inner join |
|
534 |
assertSql("SELECT * FROM entity,entity1 " + |
|
535 |
"WHERE entity.id = entity1.id", |
|
536 |
"SELECT * FROM entity, entity1 " + |
|
537 |
"WHERE entity.id = entity1.id AND entity.tenant_id = 1 AND entity1.tenant_id = 1"); |
|
538 |
} |
|
539 |
|
|
540 |
} |