Skip to content

Commit 72524b2

Browse files
committed
主键类型相关代码全面使用泛型;删除 @deprecated 代码
1 parent 74f4e73 commit 72524b2

File tree

10 files changed

+136
-149
lines changed

10 files changed

+136
-149
lines changed

APIJSONORM/src/main/java/apijson/JSONObject.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -421,18 +421,6 @@ public JSONObject setJson(String keys) {
421421
return puts(KEY_JSON, keys);
422422
}
423423

424-
/**用 setJson 替代。
425-
* set keys to cast to json
426-
* @param keys "key0,key1,key2..."
427-
* @return
428-
* @see #{@link #setJson(String)}
429-
*/
430-
@Deprecated
431-
public JSONObject setJSON(String keys) {
432-
return puts(KEY_JSON, keys);
433-
}
434-
435-
436424
//JSONObject内关键词 key >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
437425

438426

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
/**可远程调用的函数类
2626
* @author Lemon
2727
*/
28-
public class AbstractFunctionParser implements FunctionParser {
28+
public class AbstractFunctionParser<T extends Object> implements FunctionParser<T> {
2929
private static final String TAG = "AbstractFunctionParser";
3030

3131
/**是否解析参数 key 的对应的值,不用手动编码 curObj.getString(key)
@@ -65,15 +65,15 @@ public AbstractFunctionParser(RequestMethod method, String tag, int version, @No
6565
setRequest(request);
6666
}
6767

68-
private Parser<?> parser;
68+
private Parser<T> parser;
6969

7070
@Override
71-
public Parser<?> getParser() {
71+
public Parser<T> getParser() {
7272
return parser;
7373
}
7474

7575
@Override
76-
public AbstractFunctionParser setParser(Parser<?> parser) {
76+
public AbstractFunctionParser<T> setParser(Parser<T> parser) {
7777
this.parser = parser;
7878
return this;
7979
}
@@ -84,7 +84,7 @@ public RequestMethod getMethod() {
8484
}
8585

8686
@Override
87-
public AbstractFunctionParser setMethod(RequestMethod method) {
87+
public AbstractFunctionParser<T> setMethod(RequestMethod method) {
8888
this.method = method;
8989
return this;
9090
}
@@ -95,7 +95,7 @@ public String getTag() {
9595
}
9696

9797
@Override
98-
public AbstractFunctionParser setTag(String tag) {
98+
public AbstractFunctionParser<T> setTag(String tag) {
9999
this.tag = tag;
100100
return this;
101101
}
@@ -106,7 +106,7 @@ public int getVersion() {
106106
}
107107

108108
@Override
109-
public AbstractFunctionParser setVersion(int version) {
109+
public AbstractFunctionParser<T> setVersion(int version) {
110110
this.version = version;
111111
return this;
112112
}
@@ -119,7 +119,7 @@ public String getKey() {
119119
}
120120

121121
@Override
122-
public AbstractFunctionParser setKey(String key) {
122+
public AbstractFunctionParser<T> setKey(String key) {
123123
this.key = key;
124124
return this;
125125
}
@@ -132,7 +132,7 @@ public String getParentPath() {
132132
}
133133

134134
@Override
135-
public AbstractFunctionParser setParentPath(String parentPath) {
135+
public AbstractFunctionParser<T> setParentPath(String parentPath) {
136136
this.parentPath = parentPath;
137137
return this;
138138
}
@@ -145,7 +145,7 @@ public String getCurrentName() {
145145
}
146146

147147
@Override
148-
public AbstractFunctionParser setCurrentName(String currentName) {
148+
public AbstractFunctionParser<T> setCurrentName(String currentName) {
149149
this.currentName = currentName;
150150
return this;
151151
}
@@ -157,7 +157,7 @@ public JSONObject getRequest() {
157157
}
158158

159159
@Override
160-
public AbstractFunctionParser setRequest(@NotNull JSONObject request) {
160+
public AbstractFunctionParser<T> setRequest(@NotNull JSONObject request) {
161161
this.request = request;
162162
return this;
163163
}
@@ -171,7 +171,7 @@ public JSONObject getCurrentObject() {
171171
}
172172

173173
@Override
174-
public AbstractFunctionParser setCurrentObject(@NotNull JSONObject currentObject) {
174+
public AbstractFunctionParser<T> setCurrentObject(@NotNull JSONObject currentObject) {
175175
this.currentObject = currentObject;
176176
return this;
177177
}
@@ -294,7 +294,7 @@ public <T extends Object> T getArgVal(String path, Class<T> clazz) {
294294
/**根据路径取值
295295
* @param path
296296
* @param clazz
297-
* @param tryAll false-仅当前对象,true-本次请求的全局对象以及 Parser 缓存值
297+
* @param tryAll false-仅当前对象,true-本次请求的全局对象以及 Parser<T> 缓存值
298298
* @return
299299
* @param <T>
300300
*/
@@ -342,14 +342,14 @@ public Object invoke(@NotNull String function, @NotNull JSONObject currentObject
342342
public Object invoke(@NotNull String function, @NotNull JSONObject currentObject, boolean containRaw) throws Exception {
343343
return invoke(this, function, currentObject, containRaw);
344344
}
345-
345+
346346
/**反射调用
347347
* @param parser
348348
* @param function 例如get(Map:map,key),参数只允许引用,不能直接传值
349349
* @param currentObject
350350
* @return {@link #invoke(AbstractFunctionParser, String, Class[], Object[])}
351351
*/
352-
public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull String function, @NotNull JSONObject currentObject, boolean containRaw) throws Exception {
352+
public static <T extends Object> Object invoke(@NotNull AbstractFunctionParser<T> parser, @NotNull String function, @NotNull JSONObject currentObject, boolean containRaw) throws Exception {
353353
if (ENABLE_REMOTE_FUNCTION == false) {
354354
throw new UnsupportedOperationException("AbstractFunctionParser.ENABLE_REMOTE_FUNCTION" +
355355
" == false 时不支持远程函数!如需支持则设置 AbstractFunctionParser.ENABLE_REMOTE_FUNCTION = true !");
@@ -369,9 +369,9 @@ public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull Str
369369
throw new UnsupportedOperationException("language = " + language + " 不合法!AbstractFunctionParser.ENABLE_SCRIPT_FUNCTION" +
370370
" == false 时不支持远程函数中的脚本形式!如需支持则设置 AbstractFunctionParser.ENABLE_SCRIPT_FUNCTION = true !");
371371
}
372-
372+
373373
if (lang != null && SCRIPT_EXECUTOR_MAP.get(lang) == null) {
374-
throw new ClassNotFoundException("找不到脚本语言 " + lang + " 对应的执行引擎!请先依赖相关库并在后端 APIJSONFunctionParser 中注册!");
374+
throw new ClassNotFoundException("找不到脚本语言 " + lang + " 对应的执行引擎!请先依赖相关库并在后端 APIJSONFunctionParser<T> 中注册!");
375375
}
376376

377377
int version = row.getIntValue("version");
@@ -413,7 +413,7 @@ public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull Str
413413
}
414414

415415
}
416-
416+
417417
/**反射调用
418418
* @param parser
419419
* @param methodName
@@ -422,7 +422,7 @@ public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull Str
422422
* @return {@link #invoke(AbstractFunctionParser, String, Class[], Object[], String, JSONObject, ScriptExecutor)}
423423
* @throws Exception
424424
*/
425-
public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull String methodName
425+
public static <T extends Object> Object invoke(@NotNull AbstractFunctionParser<T> parser, @NotNull String methodName
426426
, @NotNull Class<?>[] parameterTypes, @NotNull Object[] args) throws Exception {
427427
return invoke(parser, methodName, parameterTypes, args, null, null, null);
428428
}
@@ -437,7 +437,7 @@ public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull Str
437437
* @return
438438
* @throws Exception
439439
*/
440-
public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull String methodName
440+
public static <T extends Object> Object invoke(@NotNull AbstractFunctionParser<T> parser, @NotNull String methodName
441441
, @NotNull Class<?>[] parameterTypes, @NotNull Object[] args, String returnType
442442
, JSONObject currentObject, ScriptExecutor scriptExecutor) throws Exception {
443443
if (scriptExecutor != null) {
@@ -474,7 +474,7 @@ public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull Str
474474
* @return
475475
* @throws Exception
476476
*/
477-
public static Object invokeScript(@NotNull AbstractFunctionParser parser, @NotNull String methodName
477+
public static <T extends Object> Object invokeScript(@NotNull AbstractFunctionParser<T> parser, @NotNull String methodName
478478
, @NotNull Class<?>[] parameterTypes, @NotNull Object[] args, String returnType, JSONObject currentObject, ScriptExecutor scriptExecutor) throws Exception {
479479
Object result = scriptExecutor.execute(parser, currentObject, methodName, args);
480480
if (Log.DEBUG && result != null) {

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,24 @@
3535
/**简化Parser,getObject和getArray(getArrayConfig)都能用
3636
* @author Lemon
3737
*/
38-
public abstract class AbstractObjectParser implements ObjectParser {
38+
public abstract class AbstractObjectParser<T extends Object> implements ObjectParser<T> {
3939
private static final String TAG = "AbstractObjectParser";
4040

4141
@NotNull
42-
protected AbstractParser<?> parser;
43-
public AbstractObjectParser setParser(AbstractParser<?> parser) {
44-
this.parser = parser;
42+
protected AbstractParser<T> parser;
43+
@Override
44+
public AbstractParser<T> getParser() {
45+
return parser;
46+
}
47+
@Override
48+
public AbstractObjectParser<T> setParser(Parser<T> parser) {
49+
this.parser = (AbstractParser<T>) parser;
4550
return this;
4651
}
4752

48-
4953
protected JSONObject request;//不用final是为了recycle
5054
protected String parentPath;//不用final是为了recycle
51-
protected SQLConfig arrayConfig;//不用final是为了recycle
55+
protected SQLConfig<T> arrayConfig;//不用final是为了recycle
5256
protected boolean isSubquery;
5357

5458
protected final int type;
@@ -435,6 +439,7 @@ else if (value instanceof String) { // //key{}@ getRealKey, 引用赋值路径
435439
return false; // 获取不到就不用再做无效的 query 了。不考虑 Table:{Table:{}} 嵌套
436440
}
437441

442+
438443
Log.d(TAG, "onParse isTable(table) == false >> return true;");
439444
return true; // 舍去,对Table无影响
440445
}
@@ -828,13 +833,13 @@ public void onTableArrayParse(String key, JSONArray valueArray) throws Exception
828833
@Override
829834
public JSONObject parseResponse(RequestMethod method, String table, String alias
830835
, JSONObject request, List<Join> joinList, boolean isProcedure) throws Exception {
831-
SQLConfig config = newSQLConfig(method, table, alias, request, joinList, isProcedure)
836+
SQLConfig<T> config = newSQLConfig(method, table, alias, request, joinList, isProcedure)
832837
.setParser(parser)
833838
.setObjectParser(this);
834839
return parseResponse(config, isProcedure);
835840
}
836841
@Override
837-
public JSONObject parseResponse(SQLConfig config, boolean isProcedure) throws Exception {
842+
public JSONObject parseResponse(SQLConfig<T> config, boolean isProcedure) throws Exception {
838843
if (parser.getSQLExecutor() == null) {
839844
parser.createSQLExecutor();
840845
}
@@ -1217,13 +1222,13 @@ public String getAlias() {
12171222
return alias;
12181223
}
12191224
@Override
1220-
public SQLConfig getArrayConfig() {
1225+
public SQLConfig<T> getArrayConfig() {
12211226
return arrayConfig;
12221227
}
12231228

12241229

12251230
@Override
1226-
public SQLConfig getSQLConfig() {
1231+
public SQLConfig<T> getSQLConfig() {
12271232
return sqlConfig;
12281233
}
12291234

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

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
import static apijson.RequestMethod.CRUD;
4040
import static apijson.RequestMethod.GET;
4141

42-
/**parser for parsing request to JSONObject
42+
/**Parser<T> for parsing request to JSONObject
4343
* @author Lemon
4444
*/
4545
public abstract class AbstractParser<T extends Object> implements Parser<T>, ParserCreator<T>, VerifierCreator<T>, SQLCreator {
@@ -595,7 +595,7 @@ public void onVerifyContent() throws Exception {
595595
* @throws Exception
596596
*/
597597
@Override
598-
public void onVerifyRole(@NotNull SQLConfig config) throws Exception {
598+
public void onVerifyRole(@NotNull SQLConfig<T> config) throws Exception {
599599
if (Log.DEBUG) {
600600
Log.i(TAG, "onVerifyRole config = " + JSON.toJSONString(config));
601601
}
@@ -1036,7 +1036,7 @@ public JSONObject getStructure(@NotNull String table, String method, String tag,
10361036
}
10371037

10381038
// 获取指定的JSON结构 <<<<<<<<<<<<<<
1039-
SQLConfig config = createSQLConfig().setMethod(GET).setTable(table);
1039+
SQLConfig<T> config = createSQLConfig().setMethod(GET).setTable(table);
10401040
config.setPrepared(false);
10411041
config.setColumn(Arrays.asList("structure"));
10421042

@@ -1066,7 +1066,7 @@ public JSONObject getStructure(@NotNull String table, String method, String tag,
10661066

10671067
protected Map<String, ObjectParser> arrayObjectParserCacheMap = new HashMap<>();
10681068

1069-
// protected SQLConfig itemConfig;
1069+
// protected SQLConfig<T> itemConfig;
10701070
/**获取单个对象,该对象处于parentObject内
10711071
* @param request parentObject 的 value
10721072
* @param parentPath parentObject 的路径
@@ -1078,7 +1078,7 @@ public JSONObject getStructure(@NotNull String table, String method, String tag,
10781078
*/
10791079
@Override
10801080
public JSONObject onObjectParse(final JSONObject request
1081-
, String parentPath, String name, final SQLConfig arrayConfig, boolean isSubquery) throws Exception {
1081+
, String parentPath, String name, final SQLConfig<T> arrayConfig, boolean isSubquery) throws Exception {
10821082

10831083
if (Log.DEBUG) {
10841084
Log.i(TAG, "\ngetObject: parentPath = " + parentPath
@@ -1111,7 +1111,7 @@ public JSONObject onObjectParse(final JSONObject request
11111111
boolean isArrayMainTable = isSubquery == false && isTable && type == SQLConfig.TYPE_ITEM_CHILD_0 && arrayConfig != null && RequestMethod.isGetMethod(arrayConfig.getMethod(), true);
11121112
boolean isReuse = isArrayMainTable && position > 0;
11131113

1114-
ObjectParser op = null;
1114+
ObjectParser<T> op = null;
11151115
if (isReuse) { // 数组主表使用专门的缓存数据
11161116
op = arrayObjectParserCacheMap.get(parentPath.substring(0, parentPath.lastIndexOf("[]") + 2));
11171117
op.setParentPath(parentPath);
@@ -1143,15 +1143,15 @@ public JSONObject onObjectParse(final JSONObject request
11431143
if (compat != null && compat) {
11441144
// 解决对聚合函数字段通过 query:2 分页查总数返回值错误
11451145
// 这里可能改变了内部的一些数据,下方通过 arrayConfig 还原
1146-
SQLConfig cfg = op.setSQLConfig(0, 0, 0).getSQLConfig();
1146+
SQLConfig<T> cfg = op.setSQLConfig(0, 0, 0).getSQLConfig();
11471147
boolean isExplain = cfg.isExplain();
11481148
cfg.setExplain(false);
11491149

11501150
Subquery subqy = new Subquery();
11511151
subqy.setFrom(cfg.getTable());
11521152
subqy.setConfig(cfg);
11531153

1154-
SQLConfig countSQLCfg = createSQLConfig();
1154+
SQLConfig<T> countSQLCfg = createSQLConfig();
11551155
countSQLCfg.setColumn(Arrays.asList("count(*):count"));
11561156
countSQLCfg.setFrom(subqy);
11571157

@@ -1336,7 +1336,7 @@ else if (childKeys.length == 1 && JSONRequest.isTableKey(childKeys[0])) { //
13361336

13371337
//Table<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
13381338
response = new JSONArray();
1339-
SQLConfig config = createSQLConfig()
1339+
SQLConfig<T> config = createSQLConfig()
13401340
.setMethod(requestMethod)
13411341
.setCount(size)
13421342
.setPage(page2)
@@ -1745,7 +1745,7 @@ else if (join != null){
17451745
// onList.add(table + "." + key + " = " + targetTable + "." + targetKey); // ON User.id = Moment.userId
17461746

17471747
// 保证和 SQLExcecutor 缓存的 Config 里 where 顺序一致,生成的 SQL 也就一致 <<<<<<<<<
1748-
// AbstractSQLConfig.newSQLConfig 中强制把 id, id{}, userId, userId{} 放到了最前面 tableObj.put(key, tableObj.remove(key));
1748+
// AbstractSQLConfig.newSQLConfig<T> 中强制把 id, id{}, userId, userId{} 放到了最前面 tableObj.put(key, tableObj.remove(key));
17491749

17501750
if (refObj.size() != tableObj.size()) { // 把 key 强制放最前,AbstractSQLExcecutor 中 config.putWhere 也是放尽可能最前
17511751
refObj.putAll(tableObj);
@@ -1757,8 +1757,8 @@ else if (join != null){
17571757
// 保证和 SQLExcecutor 缓存的 Config 里 where 顺序一致,生成的 SQL 也就一致 >>>>>>>>>
17581758
}
17591759

1760-
//拼接多个 SQLConfig 的SQL语句,然后执行,再把结果分别缓存(Moment, User等)到 SQLExecutor 的 cacheMap
1761-
// AbstractSQLConfig config0 = null;
1760+
//拼接多个 SQLConfig<T> 的SQL语句,然后执行,再把结果分别缓存(Moment, User等)到 SQLExecutor 的 cacheMap
1761+
// AbstractSQLConfig<T> config0 = null;
17621762
// String sql = "SELECT " + config0.getColumnString() + " FROM " + config0.getTable() + " INNER JOIN " + targetTable + " ON "
17631763
// + onList.get(0) + config0.getGroupString() + config0.getHavingString() + config0.getOrderString();
17641764

@@ -1981,7 +1981,7 @@ public JSONObject getArrayMainCacheItem(String arrayPath, int position) {
19811981
* @throws Exception
19821982
*/
19831983
@Override
1984-
public JSONObject executeSQL(SQLConfig config, boolean isSubquery) throws Exception {
1984+
public JSONObject executeSQL(SQLConfig<T> config, boolean isSubquery) throws Exception {
19851985
if (config == null) {
19861986
Log.d(TAG, "executeSQL config == null >> return null;");
19871987
return null;
@@ -2029,7 +2029,7 @@ public JSONObject executeSQL(SQLConfig config, boolean isSubquery) throws Except
20292029
else {
20302030
sqlExecutor = getSQLExecutor();
20312031
result = sqlExecutor.execute(config, false);
2032-
// FIXME 改为直接在 sqlExecutor 内加好,最后 Parser 取结果,可以解决并发执行导致内部计算出错
2032+
// FIXME 改为直接在 sqlExecutor 内加好,最后 Parser<T> 取结果,可以解决并发执行导致内部计算出错
20332033
// executedSQLDuration += sqlExecutor.getExecutedSQLDuration() + sqlExecutor.getSqlResultDuration();
20342034
}
20352035

@@ -2150,7 +2150,7 @@ protected void onClose() {
21502150
queryResultMap = null;
21512151
}
21522152

2153-
private void setOpMethod(JSONObject request, ObjectParser op, String key) {
2153+
private void setOpMethod(JSONObject request, ObjectParser<T> op, String key) {
21542154
String _method = key == null ? null : request.getString(apijson.JSONObject.KEY_METHOD);
21552155
if (_method != null) {
21562156
RequestMethod method = RequestMethod.valueOf(_method); // 必须精准匹配,避免缓存命中率低

0 commit comments

Comments
 (0)