Skip to content

Commit 5c682cb

Browse files
committed
重构 enum RequestRole 为 String 方便用户自定义扩展;删除部分已废弃的方法
1 parent d46d1f3 commit 5c682cb

10 files changed

Lines changed: 108 additions & 185 deletions

File tree

APIJSONORM/src/main/java/apijson/MethodAccess.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
import java.lang.annotation.Retention;
1111
import java.lang.annotation.Target;
1212

13-
import static apijson.RequestRole.ADMIN;
14-
import static apijson.RequestRole.CIRCLE;
15-
import static apijson.RequestRole.CONTACT;
16-
import static apijson.RequestRole.LOGIN;
17-
import static apijson.RequestRole.OWNER;
18-
import static apijson.RequestRole.UNKNOWN;
13+
import static apijson.orm.AbstractVerifier.ADMIN;
14+
import static apijson.orm.AbstractVerifier.CIRCLE;
15+
import static apijson.orm.AbstractVerifier.CONTACT;
16+
import static apijson.orm.AbstractVerifier.LOGIN;
17+
import static apijson.orm.AbstractVerifier.OWNER;
18+
import static apijson.orm.AbstractVerifier.UNKNOWN;
1919
import static java.lang.annotation.ElementType.TYPE;
2020
import static java.lang.annotation.RetentionPolicy.RUNTIME;
2121

@@ -31,36 +31,36 @@
3131
/**@see {@link RequestMethod#GET}
3232
* @return 该请求方法允许的角色 default {UNKNOWN, LOGIN, CONTACT, CIRCLE, OWNER, ADMIN};
3333
*/
34-
RequestRole[] GET() default {UNKNOWN, LOGIN, CONTACT, CIRCLE, OWNER, ADMIN};
34+
String[] GET() default {UNKNOWN, LOGIN, CONTACT, CIRCLE, OWNER, ADMIN};
3535

3636
/**@see {@link RequestMethod#HEAD}
3737
* @return 该请求方法允许的角色 default {UNKNOWN, LOGIN, CONTACT, CIRCLE, OWNER, ADMIN};
3838
*/
39-
RequestRole[] HEAD() default {UNKNOWN, LOGIN, CONTACT, CIRCLE, OWNER, ADMIN};
39+
String[] HEAD() default {UNKNOWN, LOGIN, CONTACT, CIRCLE, OWNER, ADMIN};
4040

4141
/**@see {@link RequestMethod#GETS}
4242
* @return 该请求方法允许的角色 default {LOGIN, CONTACT, CIRCLE, OWNER, ADMIN};
4343
*/
44-
RequestRole[] GETS() default {LOGIN, CONTACT, CIRCLE, OWNER, ADMIN};
44+
String[] GETS() default {LOGIN, CONTACT, CIRCLE, OWNER, ADMIN};
4545

4646
/**@see {@link RequestMethod#HEADS}
4747
* @return 该请求方法允许的角色 default {LOGIN, CONTACT, CIRCLE, OWNER, ADMIN};
4848
*/
49-
RequestRole[] HEADS() default {LOGIN, CONTACT, CIRCLE, OWNER, ADMIN};
49+
String[] HEADS() default {LOGIN, CONTACT, CIRCLE, OWNER, ADMIN};
5050

5151
/**@see {@link RequestMethod#POST}
5252
* @return 该请求方法允许的角色 default {LOGIN, ADMIN};
5353
*/
54-
RequestRole[] POST() default {OWNER, ADMIN};
54+
String[] POST() default {OWNER, ADMIN};
5555

5656
/**@see {@link RequestMethod#PUT}
5757
* @return 该请求方法允许的角色 default {OWNER, ADMIN};
5858
*/
59-
RequestRole[] PUT() default {OWNER, ADMIN};
59+
String[] PUT() default {OWNER, ADMIN};
6060

6161
/**@see {@link RequestMethod#DELETE}
6262
* @return 该请求方法允许的角色 default {OWNER, ADMIN};
6363
*/
64-
RequestRole[] DELETE() default {OWNER, ADMIN};
64+
String[] DELETE() default {OWNER, ADMIN};
6565

6666
}

APIJSONORM/src/main/java/apijson/RequestRole.java

Lines changed: 0 additions & 60 deletions
This file was deleted.

APIJSONORM/src/main/java/apijson/orm/AbstractParser.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import apijson.Log;
3636
import apijson.NotNull;
3737
import apijson.RequestMethod;
38-
import apijson.RequestRole;
3938
import apijson.StringUtil;
4039
import apijson.orm.exception.ConditionErrorException;
4140
import apijson.orm.exception.ConflictException;
@@ -173,13 +172,13 @@ public AbstractParser<T> setGlobleFormat(Boolean globleFormat) {
173172
public Boolean getGlobleFormat() {
174173
return globleFormat;
175174
}
176-
protected RequestRole globleRole;
177-
public AbstractParser<T> setGlobleRole(RequestRole globleRole) {
175+
protected String globleRole;
176+
public AbstractParser<T> setGlobleRole(String globleRole) {
178177
this.globleRole = globleRole;
179178
return this;
180179
}
181180
@Override
182-
public RequestRole getGlobleRole() {
181+
public String getGlobleRole() {
183182
return globleRole;
184183
}
185184
protected String globleDatabase;
@@ -361,7 +360,7 @@ public JSONObject parseResponse(JSONObject request) {
361360
//必须在parseCorrectRequest后面,因为parseCorrectRequest可能会添加 @role
362361
if (isNeedVerifyRole() && globleRole == null) {
363362
try {
364-
setGlobleRole(RequestRole.get(requestObject.getString(JSONRequest.KEY_ROLE)));
363+
setGlobleRole(requestObject.getString(JSONRequest.KEY_ROLE));
365364
requestObject.remove(JSONRequest.KEY_ROLE);
366365
} catch (Exception e) {
367366
return extendErrorResult(requestObject, e);
@@ -466,7 +465,7 @@ public void onVerifyRole(@NotNull SQLConfig config) throws Exception {
466465
if (globleRole != null) {
467466
config.setRole(globleRole);
468467
} else {
469-
config.setRole(getVisitor().getId() == null ? RequestRole.UNKNOWN : RequestRole.LOGIN);
468+
config.setRole(getVisitor().getId() == null ? AbstractVerifier.UNKNOWN : AbstractVerifier.LOGIN);
470469
}
471470
}
472471
getVerifier().verifyAccess(config);

APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454
import apijson.Log;
5555
import apijson.NotNull;
5656
import apijson.RequestMethod;
57-
import apijson.RequestRole;
5857
import apijson.SQL;
5958
import apijson.StringUtil;
6059
import apijson.orm.exception.NotExistException;
@@ -196,10 +195,10 @@ public abstract class AbstractSQLConfig implements SQLConfig {
196195
RAW_MAP.put("LANGUAGE", "");
197196
RAW_MAP.put("MODE", "");
198197

198+
199199

200200
SQL_FUNCTION_MAP = new LinkedHashMap<>(); // 保证顺序,避免配置冲突等意外情况
201201

202-
203202
//窗口函数
204203
SQL_FUNCTION_MAP.put("rank", "");//得到数据项在分组中的排名,排名相等的时候会留下空位
205204
SQL_FUNCTION_MAP.put("dense_rank", ""); //得到数据项在分组中的排名,排名相等的时候不会留下空位
@@ -689,7 +688,7 @@ public String getUserIdKey() {
689688
/**
690689
* TODO 被关联的表通过就忽略关联的表?(这个不行 User:{"sex@":"/Comment/toId"})
691690
*/
692-
private RequestRole role; //发送请求的用户的角色
691+
private String role; //发送请求的用户的角色
693692
private boolean distinct = false;
694693
private String database; //表所在的数据库类型
695694
private String schema; //表所在的数据库名
@@ -789,15 +788,12 @@ public AbstractSQLConfig setId(Object id) {
789788
}
790789

791790
@Override
792-
public RequestRole getRole() {
791+
public String getRole() {
793792
//不能 @NotNull , AbstractParser#getSQLObject 内当getRole() == null时填充默认值
794793
return role;
795794
}
796-
public AbstractSQLConfig setRole(String roleName) throws Exception {
797-
return setRole(RequestRole.get(roleName));
798-
}
799795
@Override
800-
public AbstractSQLConfig setRole(RequestRole role) {
796+
public AbstractSQLConfig setRole(String role) {
801797
this.role = role;
802798
return this;
803799
}
@@ -3175,7 +3171,7 @@ public static String getSQL(AbstractSQLConfig config) throws Exception {
31753171
String explain = (config.isExplain() ? (config.isSQLServer() || config.isOracle() ? "SET STATISTICS PROFILE ON " : "EXPLAIN ") : "");
31763172
if (config.isTest() && RequestMethod.isGetMethod(config.getMethod(), true)) { // FIXME 为啥是 code 而不是 count ?
31773173
String q = config.getQuote(); // 生成 SELECT ( (24 >=0 AND 24 <3) ) AS `code` LIMIT 1 OFFSET 0
3178-
return explain + "SELECT " + config.getWhereString(false) + " AS " + q + JSONResponse.KEY_CODE + q + config.getLimitString();
3174+
return explain + "SELECT " + config.getWhereString(false) + " AS " + q + JSONResponse.KEY_COUNT + q + config.getLimitString();
31793175
}
31803176

31813177
config.setPreparedValueList(new ArrayList<Object>());
@@ -3728,7 +3724,7 @@ else if (whereList != null && whereList.contains(key)) {
37283724
config.setId(id);
37293725
//在 tableWhere 第0个 config.setIdIn(idIn);
37303726

3731-
config.setRole(RequestRole.get(role));
3727+
config.setRole(role);
37323728
config.setGroup(group);
37333729
config.setHaving(having);
37343730
config.setOrder(order);
@@ -3966,14 +3962,6 @@ public static interface IdCallback {
39663962
*/
39673963
Object newId(RequestMethod method, String database, String schema, String table);
39683964

3969-
/**已废弃,最早 5.0.0 移除,改用 {@link #getIdKey(String, String, String, String)}
3970-
* @param database
3971-
* @param schema
3972-
* @param table
3973-
* @return
3974-
*/
3975-
@Deprecated
3976-
String getIdKey(String database, String schema, String table);
39773965

39783966
/**获取主键名
39793967
* @param database
@@ -3983,15 +3971,6 @@ public static interface IdCallback {
39833971
*/
39843972
String getIdKey(String database, String schema, String datasource, String table);
39853973

3986-
/**已废弃,最早 5.0.0 移除,改用 {@link #getUserIdKey(String, String, String, String)}
3987-
* @param database
3988-
* @param schema
3989-
* @param table
3990-
* @return
3991-
*/
3992-
@Deprecated
3993-
String getUserIdKey(String database, String schema, String table);
3994-
39953974
/**获取 User 的主键名
39963975
* @param database
39973976
* @param schema
@@ -4027,24 +4006,14 @@ public Object newId(RequestMethod method, String database, String schema, String
40274006
return System.currentTimeMillis();
40284007
}
40294008

4030-
@Override
4031-
public String getIdKey(String database, String schema, String table) {
4032-
return KEY_ID;
4033-
}
4034-
40354009
@Override
40364010
public String getIdKey(String database, String schema, String datasource, String table) {
4037-
return getIdKey(database, schema, table);
4038-
}
4039-
4040-
@Override
4041-
public String getUserIdKey(String database, String schema, String table) {
4042-
return KEY_USER_ID;
4011+
return KEY_ID;
40434012
}
40444013

40454014
@Override
40464015
public String getUserIdKey(String database, String schema, String datasource, String table) {
4047-
return getUserIdKey(database, schema, table);
4016+
return KEY_USER_ID;
40484017
}
40494018

40504019
@Override

0 commit comments

Comments
 (0)