Skip to content

Commit e2d1f64

Browse files
committed
Server:新增支持自定义主键名getIdKey和用户主键名getUserIdKey
1 parent 4d31430 commit e2d1f64

File tree

9 files changed

+155
-80
lines changed

9 files changed

+155
-80
lines changed

APIJSON-Java-Server/APIJSONBoot/src/main/java/apijson/demo/server/DemoSQLConfig.java

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414

1515
package apijson.demo.server;
1616

17+
import static zuo.biao.apijson.JSONObject.KEY_ID;
18+
import static zuo.biao.apijson.JSONObject.KEY_USER_ID;
19+
1720
import java.util.List;
1821

1922
import com.alibaba.fastjson.JSONObject;
@@ -33,19 +36,42 @@
3336
*/
3437
public class DemoSQLConfig extends AbstractSQLConfig {
3538

36-
39+
40+
public static final Callback SIMPLE_CALLBACK;
41+
3742
//表名映射,隐藏真实表名,对安全要求很高的表可以这么做
3843
static {
3944
TABLE_KEY_MAP.put(User.class.getSimpleName(), "apijson_user");
4045
TABLE_KEY_MAP.put(Privacy.class.getSimpleName(), "apijson_privacy");
46+
47+
SIMPLE_CALLBACK = new SimpleCallback() {
48+
49+
@Override
50+
public DemoSQLConfig getSQLConfig(RequestMethod method, String table) {
51+
return new DemoSQLConfig(method, table);
52+
}
53+
54+
// @Override
55+
// public String getIdKey(String schema, String table) {
56+
// return StringUtil.firstCase(table + "Id"); // userId, comemntId ...
57+
// // return StringUtil.toLowerCase(t) + "_id"; // user_id, comemnt_id ...
58+
// // return StringUtil.toUpperCase(t) + "_ID"; // USER_ID, COMMENT_ID ...
59+
// }
60+
61+
@Override
62+
public String getUserIdKey(String schema, String table) {
63+
return Controller.USER_.equals(table) || Controller.PRIVACY_.equals(table) ? KEY_ID : KEY_USER_ID; // id / userId
64+
}
65+
66+
};
4167
}
42-
68+
4369
//取消注释后,默认的数据库类型会由 MySQL 改为 PostgreSQL
44-
// @Override
45-
// public String getDatabase() {
46-
// String db = super.getDatabase();
47-
// return db == null ? DATABASE_POSTGRESQL : db;
48-
// }
70+
// @Override
71+
// public String getDatabase() {
72+
// String db = super.getDatabase();
73+
// return db == null ? DATABASE_POSTGRESQL : db;
74+
// }
4975

5076
@Override
5177
public String getDBUri() {
@@ -66,6 +92,18 @@ public String getSchema() {
6692
return StringUtil.isEmpty(s, true) ? "sys" : s; //TODO 改成你自己的
6793
}
6894

95+
96+
@Override
97+
public String getIdKey() {
98+
return SIMPLE_CALLBACK.getIdKey(getSchema(), getTable());
99+
}
100+
101+
@Override
102+
public String getUserIdKey() {
103+
return SIMPLE_CALLBACK.getUserIdKey(getSchema(), getTable());
104+
}
105+
106+
69107
public DemoSQLConfig() {
70108
this(RequestMethod.GET);
71109
}
@@ -88,18 +126,7 @@ public DemoSQLConfig(RequestMethod method, int count, int page) {
88126
* @throws Exception
89127
*/
90128
public static SQLConfig newSQLConfig(RequestMethod method, String table, JSONObject request, List<Join> joinList) throws Exception {
91-
return newSQLConfig(method, table, request, joinList, new Callback() {
92-
93-
@Override
94-
public DemoSQLConfig getSQLConfig(RequestMethod method, String table) {
95-
return new DemoSQLConfig(method, table);
96-
}
97-
98-
@Override
99-
public Object newId(RequestMethod method, String table) {
100-
return System.currentTimeMillis(); //为 post 请求提供 id, 只能是 Long 或 String
101-
}
102-
});
129+
return newSQLConfig(method, table, request, joinList, SIMPLE_CALLBACK);
103130
}
104131

105132

APIJSON-Java-Server/APIJSONBoot/src/main/java/apijson/demo/server/DemoVerifier.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,6 @@ public DemoParser createParser() {
5656
return parser;
5757
}
5858

59-
@Override
60-
public String getVisitorKey() {
61-
return Controller.USER_;
62-
}
63-
64-
@Override
65-
public String getVisitorIdKey() {
66-
return Controller.USER_ID;
67-
}
68-
69-
@Override
70-
public String getVisitorIdKey(String table) {
71-
return Controller.USER_.equals(table) || Controller.PRIVACY_.equals(table) ? Controller.ID : getVisitorIdKey();
72-
}
73-
7459
/**登录校验
7560
* @author
7661
* @modifier Lemon

APIJSON-Java-Server/APIJSONFinal/src/main/java/apijson/demo/server/DemoVerifier.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import zuo.biao.apijson.MethodAccess;
2727
import zuo.biao.apijson.NotNull;
2828
import zuo.biao.apijson.server.AbstractVerifier;
29+
import zuo.biao.apijson.server.SQLConfig;
2930
import zuo.biao.apijson.server.Visitor;
3031

3132

@@ -57,18 +58,13 @@ public DemoParser createParser() {
5758
}
5859

5960
@Override
60-
public String getVisitorKey() {
61+
public String getVisitorKey(SQLConfig config) {
6162
return Controller.USER_;
6263
}
6364

6465
@Override
65-
public String getVisitorIdKey() {
66-
return Controller.USER_ID;
67-
}
68-
69-
@Override
70-
public String getVisitorIdKey(String table) {
71-
return Controller.USER_.equals(table) || Controller.PRIVACY_.equals(table) ? Controller.ID : getVisitorIdKey();
66+
public String getVisitorIdKey(SQLConfig config) {
67+
return Controller.USER_.equals(config.getTable()) || Controller.PRIVACY_.equals(config.getTable()) ? Controller.ID : Controller.USER_ID;
7268
}
7369

7470
/**登录校验

APIJSON-Java-Server/APIJSONORM/src/main/java/zuo/biao/apijson/server/AbstractParser.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,10 @@ public JSONArray onArrayParse(JSONObject request, String parentPath, String name
695695
query2 = JSONRequest.QUERY_TABLE;
696696
}
697697
else {
698+
// if (isSubquery) {
699+
// throw new IllegalArgumentException("子查询内不支持传 " + JSONRequest.KEY_QUERY + "!");
700+
// }
701+
698702
switch (query) {
699703
case "0":
700704
case "TABLE":

APIJSON-Java-Server/APIJSONORM/src/main/java/zuo/biao/apijson/server/AbstractSQLConfig.java

Lines changed: 81 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,10 @@
2121
import static zuo.biao.apijson.JSONObject.KEY_GROUP;
2222
import static zuo.biao.apijson.JSONObject.KEY_HAVING;
2323
import static zuo.biao.apijson.JSONObject.KEY_ID;
24-
import static zuo.biao.apijson.JSONObject.KEY_ID_IN;
2524
import static zuo.biao.apijson.JSONObject.KEY_ORDER;
2625
import static zuo.biao.apijson.JSONObject.KEY_ROLE;
2726
import static zuo.biao.apijson.JSONObject.KEY_SCHEMA;
2827
import static zuo.biao.apijson.JSONObject.KEY_USER_ID;
29-
import static zuo.biao.apijson.JSONObject.KEY_USER_ID_IN;
3028
import static zuo.biao.apijson.RequestMethod.DELETE;
3129
import static zuo.biao.apijson.RequestMethod.GET;
3230
import static zuo.biao.apijson.RequestMethod.GETS;
@@ -80,6 +78,16 @@ public abstract class AbstractSQLConfig implements SQLConfig {
8078
TABLE_KEY_MAP.put(Column.class.getSimpleName(), Column.TAG);
8179
}
8280

81+
@NotNull
82+
@Override
83+
public String getIdKey() {
84+
return KEY_ID;
85+
}
86+
@NotNull
87+
@Override
88+
public String getUserIdKey() {
89+
return KEY_USER_ID;
90+
}
8391

8492

8593
private Object id; //Table的id
@@ -914,16 +922,22 @@ else if (andList == null || andList.contains(key) == false) {
914922
andList = new ArrayList<>();
915923
}
916924
else if (prior && andList.isEmpty() == false) {
917-
if (andList.contains(KEY_ID)) {
925+
926+
String idKey = getIdKey();
927+
String idInKey = idKey + "{}";
928+
String userIdKey = getUserIdKey();
929+
String userIdInKey = userIdKey + "{}";
930+
931+
if (andList.contains(idKey)) {
918932
i ++;
919933
}
920-
if (andList.contains(KEY_ID_IN)) {
934+
if (andList.contains(idInKey)) {
921935
i ++;
922936
}
923-
if (andList.contains(KEY_USER_ID)) {
937+
if (andList.contains(userIdKey)) {
924938
i ++;
925939
}
926-
if (andList.contains(KEY_USER_ID_IN)) {
940+
if (andList.contains(userIdInKey)) {
927941
i ++;
928942
}
929943
}
@@ -1689,9 +1703,10 @@ public String getSetString(RequestMethod method, Map<String, Object> content, bo
16891703
int keyType = 0;// 0 - =; 1 - +, 2 - -
16901704
Object value;
16911705

1706+
String idKey = getIdKey();
16921707
for (String key : set) {
16931708
//避免筛选到全部 value = key == null ? null : content.get(key);
1694-
if (key == null || KEY_ID.equals(key)) {
1709+
if (key == null || idKey.equals(key)) {
16951710
continue;
16961711
}
16971712

@@ -1951,21 +1966,28 @@ public static AbstractSQLConfig newSQLConfig(RequestMethod method, String table,
19511966
if (request.isEmpty()) { // User:{} 这种空内容在查询时也有效
19521967
return config; //request.remove(key); 前都可以直接return,之后必须保证 put 回去
19531968
}
1969+
1970+
String schema = request.getString(KEY_SCHEMA);
1971+
1972+
String idKey = callback.getIdKey(schema, table);
1973+
String idInKey = idKey + "{}";
1974+
String userIdKey = callback.getUserIdKey(schema, table);
1975+
String userIdInKey = userIdKey + "{}";
19541976

1955-
Object idIn = request.get(KEY_ID_IN); //可能是 id{}:">0"
1977+
Object idIn = request.get(idInKey); //可能是 id{}:">0"
19561978

19571979
if (method == POST) {
19581980
if (idIn != null) { //不能在这里确定[]的长度,只能在外面传进来
19591981
if ((idIn instanceof List == false) || ((List<?>)idIn).isEmpty()) { // id{}:[] 表示同时插入多条记录
19601982
throw new IllegalArgumentException("POST请求,生成多条记录请用 id{}:[] ! [] 类型为JSONArray且不能为空!");
19611983
}
1962-
} else if (request.get(KEY_ID) == null) {
1963-
request.put(KEY_ID, callback.newId(method, table));
1984+
} else if (request.get(idKey) == null) {
1985+
request.put(idKey, callback.newId(method, table));
19641986
}
19651987
}
19661988

19671989
//对id和id{}处理,这两个一定会作为条件
1968-
Object id = request.get(KEY_ID);
1990+
Object id = request.get(idKey);
19691991
if (id != null) { //null无效
19701992
if (id instanceof Number) {
19711993
if (((Number) id).longValue() <= 0) { //一定没有值
@@ -1979,7 +2001,7 @@ else if (id instanceof String) {
19792001
}
19802002
else if (id instanceof Subquery) {}
19812003
else {
1982-
throw new IllegalArgumentException(KEY_ID + ":value 中 value 的类型只能是 Long , String 或 Subquery !");
2004+
throw new IllegalArgumentException(idKey + ":value 中 value 的类型只能是 Long , String 或 Subquery !");
19832005
}
19842006

19852007
if (idIn instanceof List) { //共用idIn场景少性能差
@@ -1992,7 +2014,6 @@ else if (id instanceof Subquery) {}
19922014

19932015
String role = request.getString(KEY_ROLE);
19942016
String database = request.getString(KEY_DATABASE);
1995-
String schema = request.getString(KEY_SCHEMA);
19962017
String combine = request.getString(KEY_COMBINE);
19972018
Subquery from = (Subquery) request.get(KEY_FROM);
19982019
String column = request.getString(KEY_COLUMN);
@@ -2001,8 +2022,8 @@ else if (id instanceof Subquery) {}
20012022
String order = request.getString(KEY_ORDER);
20022023

20032024
//强制作为条件且放在最前面优化性能
2004-
request.remove(KEY_ID);
2005-
request.remove(KEY_ID_IN);
2025+
request.remove(idKey);
2026+
request.remove(idInKey);
20062027
//关键词
20072028
request.remove(KEY_ROLE);
20082029
request.remove(KEY_DATABASE);
@@ -2043,7 +2064,7 @@ else if (id instanceof Subquery) {}
20432064
throw new Exception("服务器内部错误:\n" + TAG
20442065
+ " newSQLConfig values == null || values.length != columns.length !");
20452066
}
2046-
column = KEY_ID + "," + StringUtil.getString(columns); //set已经判断过不为空
2067+
column = idKey + "," + StringUtil.getString(columns); //set已经判断过不为空
20472068
final int size = columns.length + 1; //以key数量为准
20482069

20492070
List<List<Object>> valuess = new ArrayList<>(idList.size()); // [idList.size()][]
@@ -2072,12 +2093,12 @@ else if (id instanceof Subquery) {}
20722093

20732094
//强制作为条件且放在最前面优化性能
20742095
if (id != null) {
2075-
tableWhere.put(KEY_ID, id);
2076-
andList.add(KEY_ID);
2096+
tableWhere.put(idKey, id);
2097+
andList.add(idKey);
20772098
}
20782099
if (idIn != null) {
2079-
tableWhere.put(KEY_ID_IN, idIn);
2080-
andList.add(KEY_ID_IN);
2100+
tableWhere.put(idInKey, idIn);
2101+
andList.add(idInKey);
20812102
}
20822103

20832104
String[] ws = StringUtil.split(combine);
@@ -2119,9 +2140,9 @@ else if (w.startsWith("!")) {
21192140
throw new IllegalArgumentException(table + ":{} 里的 @combine:value 中的value里条件 " + ws[i] + " 不合法!不允许为空值!");
21202141
}
21212142
else {
2122-
if (KEY_ID.equals(w) || KEY_ID_IN.equals(w) || KEY_USER_ID.equals(w) || KEY_USER_ID_IN.equals(w)) {
2143+
if (idKey.equals(w) || idInKey.equals(w) || userIdKey.equals(w) || userIdInKey.equals(w)) {
21232144
throw new UnsupportedOperationException(table + ":{} 里的 @combine:value 中的value里 " + ws[i] + " 不合法!"
2124-
+ "不允许传 [" + KEY_ID + ", " + KEY_ID_IN + ", " + KEY_USER_ID + ", " + KEY_USER_ID_IN + "] 其中任何一个!");
2145+
+ "不允许传 [" + idKey + ", " + idInKey + ", " + userIdKey + ", " + userIdInKey + "] 其中任何一个!");
21252146
}
21262147
}
21272148

@@ -2203,8 +2224,8 @@ else if (whereList != null && whereList.contains(key)) {
22032224

22042225
//后面还可能用到,要还原
22052226
//id或id{}条件
2206-
request.put(KEY_ID, id);
2207-
request.put(KEY_ID_IN, idIn);
2227+
request.put(idKey, id);
2228+
request.put(idInKey, idIn);
22082229
//关键词
22092230
request.put(KEY_ROLE, role);
22102231
request.put(KEY_DATABASE, database);
@@ -2385,20 +2406,55 @@ else if (key.endsWith("-")) {//缩减,PUT查询时处理
23852406
}
23862407

23872408

2388-
public interface Callback {
2409+
public static interface Callback {
23892410
/**获取 SQLConfig 的实例
23902411
* @param method
23912412
* @param table
23922413
* @return
23932414
*/
23942415
AbstractSQLConfig getSQLConfig(RequestMethod method, String table);
23952416

2417+
23962418
/**为 post 请求新建 id, 只能是 Long 或 String
23972419
* @param method
23982420
* @param table
23992421
* @return
24002422
*/
24012423
Object newId(RequestMethod method, String table);
2424+
2425+
/**获取主键名
2426+
* @param schema
2427+
* @param table
2428+
* @return
2429+
*/
2430+
String getIdKey(String schema, String table);
2431+
2432+
/**获取 User 的主键名
2433+
* @param schema
2434+
* @param table
2435+
* @return
2436+
*/
2437+
String getUserIdKey(String schema, String table);
2438+
}
2439+
2440+
public static abstract class SimpleCallback implements Callback {
2441+
2442+
2443+
@Override
2444+
public Object newId(RequestMethod method, String table) {
2445+
return System.currentTimeMillis();
2446+
}
2447+
2448+
@Override
2449+
public String getIdKey(String schema, String table) {
2450+
return KEY_ID;
2451+
}
2452+
2453+
@Override
2454+
public String getUserIdKey(String schema, String table) {
2455+
return KEY_USER_ID;
2456+
}
2457+
24022458
}
24032459

24042460
}

0 commit comments

Comments
 (0)