Skip to content

Commit 8df36e2

Browse files
committed
优化 SQL 执行缓存;优化主键泛型;部分常量改为可自定义静态变量;解决高并发下 id 冲突导致新增记录失败等
1 parent 5d40217 commit 8df36e2

12 files changed

+156
-141
lines changed

APIJSONORM/src/main/java/apijson/RequestMethod.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public enum RequestMethod {
4545
*/
4646
DELETE;
4747

48+
public static final RequestMethod[] ALL = new RequestMethod[]{ GET, HEAD, GETS, HEADS, POST, PUT, DELETE};
4849

4950
/**是否为GET请求方法
5051
* @param method

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,15 @@ public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull Str
178178
+ "\n请检查函数名和参数数量是否与已定义的函数一致!"
179179
+ "\n且必须为 function(key0,key1,...) 这种单函数格式!"
180180
+ "\nfunction必须符合Java函数命名,key是用于在request内取值的键!"
181-
+ "\n调用时不要有空格!");
181+
+ "\n调用时不要有空格!" + e.getMessage());
182182
}
183183
if (e instanceof InvocationTargetException) {
184184
Throwable te = ((InvocationTargetException) e).getTargetException();
185185
if (StringUtil.isEmpty(te.getMessage(), true) == false) { //到处把函数声明throws Exception改成throws Throwable挺麻烦
186186
throw te instanceof Exception ? (Exception) te : new Exception(te.getMessage());
187187
}
188188
throw new IllegalArgumentException("字符 " + function + " 对应的远程函数传参类型错误!"
189-
+ "\n请检查 key:value 中value的类型是否满足已定义的函数 " + getFunction(fb.getMethod(), fb.getKeys()) + " 的要求!");
189+
+ "\n请检查 key:value 中value的类型是否满足已定义的函数 " + getFunction(fb.getMethod(), fb.getKeys()) + " 的要求!" + e.getMessage());
190190
}
191191
throw e;
192192
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -908,7 +908,7 @@ public JSONObject onSQLExecute() throws Exception {
908908
&& (table.equals(arrayTable));
909909

910910
// 提取并缓存数组主表的列表数据
911-
rawList = (List<JSONObject>) result.remove(SQLExecutor.KEY_RAW_LIST);
911+
rawList = (List<JSONObject>) result.remove(AbstractSQLExecutor.KEY_RAW_LIST);
912912
if (rawList != null) {
913913
String arrayPath = parentPath.substring(0, parentPath.lastIndexOf("[]") + 2);
914914

@@ -936,7 +936,7 @@ public JSONObject onSQLExecute() throws Exception {
936936
parser.putQueryResult(path, result); // 解决获取关联数据时requestObject里不存在需要的关联数据
937937

938938
if (isSimpleArray && rawList != null) {
939-
result.put(SQLExecutor.KEY_RAW_LIST, rawList);
939+
result.put(AbstractSQLExecutor.KEY_RAW_LIST, rawList);
940940
}
941941
}
942942
}

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

Lines changed: 45 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
/**parser for parsing request to JSONObject
5151
* @author Lemon
5252
*/
53-
public abstract class AbstractParser<T> implements Parser<T>, ParserCreator<T>, VerifierCreator<T>, SQLCreator {
53+
public abstract class AbstractParser<T extends Object> implements Parser<T>, ParserCreator<T>, VerifierCreator<T>, SQLCreator {
5454
protected static final String TAG = "AbstractParser";
5555

5656
/**
@@ -72,6 +72,49 @@ public abstract class AbstractParser<T> implements Parser<T>, ParserCreator<T>,
7272
public static boolean IS_PRINT_REQUEST_ENDTIME_LOG = false;
7373

7474

75+
public static int DEFAULT_QUERY_COUNT = 10;
76+
public static int MAX_QUERY_PAGE = 100;
77+
public static int MAX_QUERY_COUNT = 100;
78+
public static int MAX_UPDATE_COUNT = 10;
79+
public static int MAX_SQL_COUNT = 200;
80+
public static int MAX_OBJECT_COUNT = 5;
81+
public static int MAX_ARRAY_COUNT = 5;
82+
public static int MAX_QUERY_DEPTH = 5;
83+
84+
@Override
85+
public int getDefaultQueryCount() {
86+
return DEFAULT_QUERY_COUNT;
87+
}
88+
@Override
89+
public int getMaxQueryPage() {
90+
return MAX_QUERY_PAGE;
91+
}
92+
@Override
93+
public int getMaxQueryCount() {
94+
return MAX_QUERY_COUNT;
95+
}
96+
@Override
97+
public int getMaxUpdateCount() {
98+
return MAX_UPDATE_COUNT;
99+
}
100+
@Override
101+
public int getMaxSQLCount() {
102+
return MAX_SQL_COUNT;
103+
}
104+
@Override
105+
public int getMaxObjectCount() {
106+
return MAX_OBJECT_COUNT;
107+
}
108+
@Override
109+
public int getMaxArrayCount() {
110+
return MAX_ARRAY_COUNT;
111+
}
112+
@Override
113+
public int getMaxQueryDepth() {
114+
return MAX_QUERY_DEPTH;
115+
}
116+
117+
75118
/**
76119
* method = null
77120
*/
@@ -1276,7 +1319,7 @@ else if (childKeys.length == 1 && JSONRequest.isTableKey(childKeys[0])) { //
12761319
*/
12771320
JSONObject fo = i != 0 || arrTableKey == null ? null : parent.getJSONObject(arrTableKey);
12781321
@SuppressWarnings("unchecked")
1279-
List<JSONObject> list = fo == null ? null : (List<JSONObject>) fo.remove(SQLExecutor.KEY_RAW_LIST);
1322+
List<JSONObject> list = fo == null ? null : (List<JSONObject>) fo.remove(AbstractSQLExecutor.KEY_RAW_LIST);
12801323

12811324
if (list != null && list.isEmpty() == false) {
12821325
isExtract = false;
@@ -1629,43 +1672,6 @@ else if (join != null){
16291672
return joinList;
16301673
}
16311674

1632-
1633-
1634-
1635-
@Override
1636-
public int getDefaultQueryCount() {
1637-
return DEFAULT_QUERY_COUNT;
1638-
}
1639-
@Override
1640-
public int getMaxQueryPage() {
1641-
return MAX_QUERY_PAGE;
1642-
}
1643-
@Override
1644-
public int getMaxQueryCount() {
1645-
return MAX_QUERY_COUNT;
1646-
}
1647-
@Override
1648-
public int getMaxUpdateCount() {
1649-
return MAX_UPDATE_COUNT;
1650-
}
1651-
@Override
1652-
public int getMaxSQLCount() {
1653-
return MAX_SQL_COUNT;
1654-
}
1655-
@Override
1656-
public int getMaxObjectCount() {
1657-
return MAX_OBJECT_COUNT;
1658-
}
1659-
@Override
1660-
public int getMaxArrayCount() {
1661-
return MAX_ARRAY_COUNT;
1662-
}
1663-
@Override
1664-
public int getMaxQueryDepth() {
1665-
return MAX_QUERY_DEPTH;
1666-
}
1667-
1668-
16691675
/**根据路径取值
16701676
* @param parent
16711677
* @param pathKeys

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

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -103,22 +103,21 @@ public abstract class AbstractSQLConfig implements SQLConfig {
103103
public static String PREFFIX_DISTINCT = "DISTINCT ";
104104

105105
// * 和 / 不能同时出现,防止 /* */ 段注释! # 和 -- 不能出现,防止行注释! ; 不能出现,防止隔断SQL语句!空格不能出现,防止 CRUD,DROP,SHOW TABLES等语句!
106-
private static final Pattern PATTERN_RANGE;
107-
private static final Pattern PATTERN_FUNCTION;
106+
private static Pattern PATTERN_RANGE;
107+
private static Pattern PATTERN_FUNCTION;
108108

109109
/**
110110
* 表名映射,隐藏真实表名,对安全要求很高的表可以这么做
111111
*/
112-
public static final Map<String, String> TABLE_KEY_MAP;
113-
public static final List<String> CONFIG_TABLE_LIST;
114-
public static final List<String> DATABASE_LIST;
112+
public static Map<String, String> TABLE_KEY_MAP;
113+
public static List<String> CONFIG_TABLE_LIST;
114+
public static List<String> DATABASE_LIST;
115115

116116
// 自定义原始 SQL 片段 Map<key, substring>:当 substring 为 null 时忽略;当 substring 为 "" 时整个 value 是 raw SQL;其它情况则只是 substring 这段为 raw SQL
117-
public static final Map<String, String> RAW_MAP;
117+
public static Map<String, String> RAW_MAP;
118118
// 允许调用的 SQL 函数:当 substring 为 null 时忽略;当 substring 为 "" 时整个 value 是 raw SQL;其它情况则只是 substring 这段为 raw SQL
119-
public static final Map<String, String> SQL_AGGREGATE_FUNCTION_MAP;
120-
public static final Map<String, String> SQL_FUNCTION_MAP;
121-
119+
public static Map<String, String> SQL_AGGREGATE_FUNCTION_MAP;
120+
public static Map<String, String> SQL_FUNCTION_MAP;
122121

123122
static { // 凡是 SQL 边界符、分隔符、注释符 都不允许,例如 ' " ` ( ) ; # -- /**/ ,以免拼接 SQL 时被注入意外可执行指令
124123
PATTERN_RANGE = Pattern.compile("^[0-9%,!=\\<\\>/\\.\\+\\-\\*\\^]+$"); // ^[a-zA-Z0-9_*%!=<>(),"]+$ 导致 exists(select*from(Comment)) 通过!
@@ -4339,7 +4338,7 @@ protected void onGetCrossJoinString(Join j) throws UnsupportedOperationException
43394338
* @return
43404339
* @throws Exception
43414340
*/
4342-
public static SQLConfig newSQLConfig(RequestMethod method, String table, String alias, JSONObject request, List<Join> joinList, boolean isProcedure, Callback callback) throws Exception {
4341+
public static <T extends Object> SQLConfig newSQLConfig(RequestMethod method, String table, String alias, JSONObject request, List<Join> joinList, boolean isProcedure, Callback<T> callback) throws Exception {
43434342
if (request == null) { // User:{} 这种空内容在查询时也有效
43444343
throw new NullPointerException(TAG + ": newSQLConfig request == null!");
43454344
}
@@ -4357,7 +4356,7 @@ public static SQLConfig newSQLConfig(RequestMethod method, String table, String
43574356
String schema = request.getString(KEY_SCHEMA);
43584357
String datasource = request.getString(KEY_DATASOURCE);
43594358

4360-
SQLConfig config = callback.getSQLConfig(method, database, schema, table);
4359+
SQLConfig config = callback.getSQLConfig(method, database, schema, datasource, table);
43614360
config.setAlias(alias);
43624361

43634362
config.setDatabase(database); //不删,后面表对象还要用的,必须放在 parseJoin 前
@@ -4404,7 +4403,7 @@ public static SQLConfig newSQLConfig(RequestMethod method, String table, String
44044403

44054404
Object id = request.get(idKey);
44064405
if (id == null && method == POST) {
4407-
id = callback.newId(method, database, schema, table); // null 表示数据库自增 id
4406+
id = callback.newId(method, database, schema, datasource, table); // null 表示数据库自增 id
44084407
}
44094408

44104409
if (id != null) { //null无效
@@ -4992,7 +4991,7 @@ else if (newHaving != null) {
49924991
* @return
49934992
* @throws Exception
49944993
*/
4995-
public static SQLConfig parseJoin(RequestMethod method, SQLConfig config, List<Join> joinList, Callback callback) throws Exception {
4994+
public static <T extends Object> SQLConfig parseJoin(RequestMethod method, SQLConfig config, List<Join> joinList, Callback<T> callback) throws Exception {
49964995
boolean isQuery = RequestMethod.isQueryMethod(method);
49974996
config.setKeyPrefix(isQuery && config.isMain() == false);
49984997

@@ -5203,15 +5202,15 @@ else if (key.endsWith("-")) {//缩减,PUT查询时处理
52035202
}
52045203

52055204

5206-
public static interface IdCallback {
5205+
public static interface IdCallback<T extends Object> {
52075206
/**为 post 请求新建 id, 只能是 Long 或 String
52085207
* @param method
52095208
* @param database
52105209
* @param schema
52115210
* @param table
52125211
* @return
52135212
*/
5214-
Object newId(RequestMethod method, String database, String schema, String table);
5213+
T newId(RequestMethod method, String database, String schema, String datasource, String table);
52155214

52165215

52175216
/**获取主键名
@@ -5231,15 +5230,15 @@ public static interface IdCallback {
52315230
String getUserIdKey(String database, String schema, String datasource, String table);
52325231
}
52335232

5234-
public static interface Callback extends IdCallback {
5233+
public static interface Callback<T extends Object> extends IdCallback<T> {
52355234
/**获取 SQLConfig 的实例
52365235
* @param method
52375236
* @param database
52385237
* @param schema
52395238
* @param table
52405239
* @return
52415240
*/
5242-
SQLConfig getSQLConfig(RequestMethod method, String database, String schema, String table);
5241+
SQLConfig getSQLConfig(RequestMethod method, String database, String schema, String datasource, String table);
52435242

52445243
/**combine 里的 key 在 request 中 value 为 null 或不存在,即 request 中缺少用来作为 combine 条件的 key: value
52455244
* @param combine
@@ -5249,12 +5248,23 @@ public static interface Callback extends IdCallback {
52495248
public void onMissingKey4Combine(String name, JSONObject request, String combine, String item, String key) throws Exception;
52505249
}
52515250

5252-
public static abstract class SimpleCallback implements Callback {
5251+
public static Long LAST_ID;
5252+
static {
5253+
LAST_ID = System.currentTimeMillis();
5254+
}
52535255

5256+
public static abstract class SimpleCallback<T extends Object> implements Callback<T> {
52545257

5258+
@SuppressWarnings("unchecked")
52555259
@Override
5256-
public Object newId(RequestMethod method, String database, String schema, String table) {
5257-
return System.currentTimeMillis();
5260+
public T newId(RequestMethod method, String database, String schema, String datasource, String table) {
5261+
Long id = System.currentTimeMillis();
5262+
if (id <= LAST_ID) {
5263+
id = LAST_ID + 1; // 解决高并发下 id 冲突导致新增记录失败
5264+
}
5265+
LAST_ID = id;
5266+
5267+
return (T) id;
52585268
}
52595269

52605270
@Override

0 commit comments

Comments
 (0)