Skip to content

Commit

Permalink
主键类型相关代码全面使用泛型;删除 @deprecated 代码
Browse files Browse the repository at this point in the history
  • Loading branch information
TommyLemon committed Sep 9, 2023
1 parent 74f4e73 commit 72524b2
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 149 deletions.
12 changes: 0 additions & 12 deletions APIJSONORM/src/main/java/apijson/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -421,18 +421,6 @@ public JSONObject setJson(String keys) {
return puts(KEY_JSON, keys);
}

/**用 setJson 替代。
* set keys to cast to json
* @param keys "key0,key1,key2..."
* @return
* @see #{@link #setJson(String)}
*/
@Deprecated
public JSONObject setJSON(String keys) {
return puts(KEY_JSON, keys);
}


//JSONObject内关键词 key >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


Expand Down
42 changes: 21 additions & 21 deletions APIJSONORM/src/main/java/apijson/orm/AbstractFunctionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/**可远程调用的函数类
* @author Lemon
*/
public class AbstractFunctionParser implements FunctionParser {
public class AbstractFunctionParser<T extends Object> implements FunctionParser<T> {
private static final String TAG = "AbstractFunctionParser";

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

private Parser<?> parser;
private Parser<T> parser;

@Override
public Parser<?> getParser() {
public Parser<T> getParser() {
return parser;
}

@Override
public AbstractFunctionParser setParser(Parser<?> parser) {
public AbstractFunctionParser<T> setParser(Parser<T> parser) {
this.parser = parser;
return this;
}
Expand All @@ -84,7 +84,7 @@ public RequestMethod getMethod() {
}

@Override
public AbstractFunctionParser setMethod(RequestMethod method) {
public AbstractFunctionParser<T> setMethod(RequestMethod method) {
this.method = method;
return this;
}
Expand All @@ -95,7 +95,7 @@ public String getTag() {
}

@Override
public AbstractFunctionParser setTag(String tag) {
public AbstractFunctionParser<T> setTag(String tag) {
this.tag = tag;
return this;
}
Expand All @@ -106,7 +106,7 @@ public int getVersion() {
}

@Override
public AbstractFunctionParser setVersion(int version) {
public AbstractFunctionParser<T> setVersion(int version) {
this.version = version;
return this;
}
Expand All @@ -119,7 +119,7 @@ public String getKey() {
}

@Override
public AbstractFunctionParser setKey(String key) {
public AbstractFunctionParser<T> setKey(String key) {
this.key = key;
return this;
}
Expand All @@ -132,7 +132,7 @@ public String getParentPath() {
}

@Override
public AbstractFunctionParser setParentPath(String parentPath) {
public AbstractFunctionParser<T> setParentPath(String parentPath) {
this.parentPath = parentPath;
return this;
}
Expand All @@ -145,7 +145,7 @@ public String getCurrentName() {
}

@Override
public AbstractFunctionParser setCurrentName(String currentName) {
public AbstractFunctionParser<T> setCurrentName(String currentName) {
this.currentName = currentName;
return this;
}
Expand All @@ -157,7 +157,7 @@ public JSONObject getRequest() {
}

@Override
public AbstractFunctionParser setRequest(@NotNull JSONObject request) {
public AbstractFunctionParser<T> setRequest(@NotNull JSONObject request) {
this.request = request;
return this;
}
Expand All @@ -171,7 +171,7 @@ public JSONObject getCurrentObject() {
}

@Override
public AbstractFunctionParser setCurrentObject(@NotNull JSONObject currentObject) {
public AbstractFunctionParser<T> setCurrentObject(@NotNull JSONObject currentObject) {
this.currentObject = currentObject;
return this;
}
Expand Down Expand Up @@ -294,7 +294,7 @@ public <T extends Object> T getArgVal(String path, Class<T> clazz) {
/**根据路径取值
* @param path
* @param clazz
* @param tryAll false-仅当前对象,true-本次请求的全局对象以及 Parser 缓存值
* @param tryAll false-仅当前对象,true-本次请求的全局对象以及 Parser<T> 缓存值
* @return
* @param <T>
*/
Expand Down Expand Up @@ -342,14 +342,14 @@ public Object invoke(@NotNull String function, @NotNull JSONObject currentObject
public Object invoke(@NotNull String function, @NotNull JSONObject currentObject, boolean containRaw) throws Exception {
return invoke(this, function, currentObject, containRaw);
}

/**反射调用
* @param parser
* @param function 例如get(Map:map,key),参数只允许引用,不能直接传值
* @param currentObject
* @return {@link #invoke(AbstractFunctionParser, String, Class[], Object[])}
*/
public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull String function, @NotNull JSONObject currentObject, boolean containRaw) throws Exception {
public static <T extends Object> Object invoke(@NotNull AbstractFunctionParser<T> parser, @NotNull String function, @NotNull JSONObject currentObject, boolean containRaw) throws Exception {
if (ENABLE_REMOTE_FUNCTION == false) {
throw new UnsupportedOperationException("AbstractFunctionParser.ENABLE_REMOTE_FUNCTION" +
" == false 时不支持远程函数!如需支持则设置 AbstractFunctionParser.ENABLE_REMOTE_FUNCTION = true !");
Expand All @@ -369,9 +369,9 @@ public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull Str
throw new UnsupportedOperationException("language = " + language + " 不合法!AbstractFunctionParser.ENABLE_SCRIPT_FUNCTION" +
" == false 时不支持远程函数中的脚本形式!如需支持则设置 AbstractFunctionParser.ENABLE_SCRIPT_FUNCTION = true !");
}

if (lang != null && SCRIPT_EXECUTOR_MAP.get(lang) == null) {
throw new ClassNotFoundException("找不到脚本语言 " + lang + " 对应的执行引擎!请先依赖相关库并在后端 APIJSONFunctionParser 中注册!");
throw new ClassNotFoundException("找不到脚本语言 " + lang + " 对应的执行引擎!请先依赖相关库并在后端 APIJSONFunctionParser<T> 中注册!");
}

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

}

/**反射调用
* @param parser
* @param methodName
Expand All @@ -422,7 +422,7 @@ public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull Str
* @return {@link #invoke(AbstractFunctionParser, String, Class[], Object[], String, JSONObject, ScriptExecutor)}
* @throws Exception
*/
public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull String methodName
public static <T extends Object> Object invoke(@NotNull AbstractFunctionParser<T> parser, @NotNull String methodName
, @NotNull Class<?>[] parameterTypes, @NotNull Object[] args) throws Exception {
return invoke(parser, methodName, parameterTypes, args, null, null, null);
}
Expand All @@ -437,7 +437,7 @@ public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull Str
* @return
* @throws Exception
*/
public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull String methodName
public static <T extends Object> Object invoke(@NotNull AbstractFunctionParser<T> parser, @NotNull String methodName
, @NotNull Class<?>[] parameterTypes, @NotNull Object[] args, String returnType
, JSONObject currentObject, ScriptExecutor scriptExecutor) throws Exception {
if (scriptExecutor != null) {
Expand Down Expand Up @@ -474,7 +474,7 @@ public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull Str
* @return
* @throws Exception
*/
public static Object invokeScript(@NotNull AbstractFunctionParser parser, @NotNull String methodName
public static <T extends Object> Object invokeScript(@NotNull AbstractFunctionParser<T> parser, @NotNull String methodName
, @NotNull Class<?>[] parameterTypes, @NotNull Object[] args, String returnType, JSONObject currentObject, ScriptExecutor scriptExecutor) throws Exception {
Object result = scriptExecutor.execute(parser, currentObject, methodName, args);
if (Log.DEBUG && result != null) {
Expand Down
25 changes: 15 additions & 10 deletions APIJSONORM/src/main/java/apijson/orm/AbstractObjectParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,24 @@
/**简化Parser,getObject和getArray(getArrayConfig)都能用
* @author Lemon
*/
public abstract class AbstractObjectParser implements ObjectParser {
public abstract class AbstractObjectParser<T extends Object> implements ObjectParser<T> {
private static final String TAG = "AbstractObjectParser";

@NotNull
protected AbstractParser<?> parser;
public AbstractObjectParser setParser(AbstractParser<?> parser) {
this.parser = parser;
protected AbstractParser<T> parser;
@Override
public AbstractParser<T> getParser() {
return parser;
}
@Override
public AbstractObjectParser<T> setParser(Parser<T> parser) {
this.parser = (AbstractParser<T>) parser;
return this;
}


protected JSONObject request;//不用final是为了recycle
protected String parentPath;//不用final是为了recycle
protected SQLConfig arrayConfig;//不用final是为了recycle
protected SQLConfig<T> arrayConfig;//不用final是为了recycle
protected boolean isSubquery;

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


Log.d(TAG, "onParse isTable(table) == false >> return true;");
return true; // 舍去,对Table无影响
}
Expand Down Expand Up @@ -828,13 +833,13 @@ public void onTableArrayParse(String key, JSONArray valueArray) throws Exception
@Override
public JSONObject parseResponse(RequestMethod method, String table, String alias
, JSONObject request, List<Join> joinList, boolean isProcedure) throws Exception {
SQLConfig config = newSQLConfig(method, table, alias, request, joinList, isProcedure)
SQLConfig<T> config = newSQLConfig(method, table, alias, request, joinList, isProcedure)
.setParser(parser)
.setObjectParser(this);
return parseResponse(config, isProcedure);
}
@Override
public JSONObject parseResponse(SQLConfig config, boolean isProcedure) throws Exception {
public JSONObject parseResponse(SQLConfig<T> config, boolean isProcedure) throws Exception {
if (parser.getSQLExecutor() == null) {
parser.createSQLExecutor();
}
Expand Down Expand Up @@ -1217,13 +1222,13 @@ public String getAlias() {
return alias;
}
@Override
public SQLConfig getArrayConfig() {
public SQLConfig<T> getArrayConfig() {
return arrayConfig;
}


@Override
public SQLConfig getSQLConfig() {
public SQLConfig<T> getSQLConfig() {
return sqlConfig;
}

Expand Down
30 changes: 15 additions & 15 deletions APIJSONORM/src/main/java/apijson/orm/AbstractParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import static apijson.RequestMethod.CRUD;
import static apijson.RequestMethod.GET;

/**parser for parsing request to JSONObject
/**Parser<T> for parsing request to JSONObject
* @author Lemon
*/
public abstract class AbstractParser<T extends Object> implements Parser<T>, ParserCreator<T>, VerifierCreator<T>, SQLCreator {
Expand Down Expand Up @@ -595,7 +595,7 @@ public void onVerifyContent() throws Exception {
* @throws Exception
*/
@Override
public void onVerifyRole(@NotNull SQLConfig config) throws Exception {
public void onVerifyRole(@NotNull SQLConfig<T> config) throws Exception {
if (Log.DEBUG) {
Log.i(TAG, "onVerifyRole config = " + JSON.toJSONString(config));
}
Expand Down Expand Up @@ -1036,7 +1036,7 @@ public JSONObject getStructure(@NotNull String table, String method, String tag,
}

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

Expand Down Expand Up @@ -1066,7 +1066,7 @@ public JSONObject getStructure(@NotNull String table, String method, String tag,

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

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

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

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

Subquery subqy = new Subquery();
subqy.setFrom(cfg.getTable());
subqy.setConfig(cfg);

SQLConfig countSQLCfg = createSQLConfig();
SQLConfig<T> countSQLCfg = createSQLConfig();
countSQLCfg.setColumn(Arrays.asList("count(*):count"));
countSQLCfg.setFrom(subqy);

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

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

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

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

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

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

Expand Down Expand Up @@ -2150,7 +2150,7 @@ protected void onClose() {
queryResultMap = null;
}

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

0 comments on commit 72524b2

Please sign in to comment.