Skip to content

Commit

Permalink
主键类型相关代码全面使用泛型
Browse files Browse the repository at this point in the history
  • Loading branch information
TommyLemon committed Sep 9, 2023
1 parent fbcf3a5 commit 550fd94
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 41 deletions.
29 changes: 7 additions & 22 deletions src/main/java/apijson/framework/APIJSONFunctionParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
/**可远程调用的函数类
* @author Lemon
*/
public class APIJSONFunctionParser extends AbstractFunctionParser {
public class APIJSONFunctionParser<T extends Object> extends AbstractFunctionParser<T> {
public static final String TAG = "APIJSONFunctionParser";

@NotNull
Expand All @@ -79,23 +79,23 @@ public APIJSONFunctionParser(RequestMethod method, String tag, int version, JSON
public HttpSession getSession() {
return session;
}
public APIJSONFunctionParser setSession(HttpSession session) {
public APIJSONFunctionParser<T> setSession(HttpSession session) {
this.session = session;
return this;
}

@Override
public APIJSONFunctionParser setMethod(RequestMethod method) {
public APIJSONFunctionParser<T> setMethod(RequestMethod method) {
super.setMethod(method);
return this;
}
@Override
public APIJSONFunctionParser setTag(String tag) {
public APIJSONFunctionParser<T> setTag(String tag) {
super.setTag(tag);
return this;
}
@Override
public APIJSONFunctionParser setVersion(int version) {
public APIJSONFunctionParser<T> setVersion(int version) {
super.setVersion(version);
return this;
}
Expand Down Expand Up @@ -283,7 +283,7 @@ public static <T extends Object> JSONObject init(boolean shutdownWhenServerError
if (item.get("language") != null) {
String language = item.getString("language");
if (SCRIPT_EXECUTOR_MAP.get(language) == null) {
onServerError("找不到脚本语言 " + language + " 对应的执行引擎!请先依赖相关库并在后端 APIJSONFunctionParser 中注册!", shutdownWhenServerError);
onServerError("找不到脚本语言 " + language + " 对应的执行引擎!请先依赖相关库并在后端 APIJSONFunctionParser<T> 中注册!", shutdownWhenServerError);
}
ScriptExecutor scriptExecutor = SCRIPT_EXECUTOR_MAP.get(language);
scriptExecutor.load(name, scriptMap.get(name).getString("script"));
Expand Down Expand Up @@ -337,7 +337,7 @@ protected static void onServerError(String msg, boolean shutdown) throws ServerE
public static void test() throws Exception {
test(null);
}
public static void test(APIJSONFunctionParser function) throws Exception {
public static <T extends Object> void test(APIJSONFunctionParser<T> function) throws Exception {
int i0 = 1, i1 = -2;
JSONObject request = new JSONObject();
request.put("id", 10);
Expand Down Expand Up @@ -710,14 +710,6 @@ public String getMethodArguments(@NotNull JSONObject curObj, String methodArgsKe
}


/**改用 getMethodDefinition
*/
@Deprecated
public String getMethodDefination(@NotNull JSONObject curObj) throws IllegalArgumentException {
// curObj.put("arguments", removeComment(curObj.getString("methodArgs")));
return getMethodDefination(curObj, "method", "arguments", "genericType", "genericExceptions", "Java");
}

/**获取方法的定义
* @param curObj
* @return
Expand All @@ -729,13 +721,6 @@ public String getMethodDefinition(@NotNull JSONObject curObj) throws IllegalArgu
// curObj.put("arguments", removeComment(curObj.getString("methodArgs")));
return getMethodDefinition(curObj, "method", "arguments", "genericType", "genericExceptions", "Java");
}
/**改用 getMethodDefinition
*/
@Deprecated
public String getMethodDefination(@NotNull JSONObject curObj, String method, String arguments
, String type, String exceptions, String language) throws IllegalArgumentException {
return getMethodDefinition(curObj, method, arguments, type, exceptions, language);
}
/**获取方法的定义
* @param curObj
* @param method
Expand Down
15 changes: 6 additions & 9 deletions src/main/java/apijson/framework/APIJSONObjectParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,17 @@

import javax.servlet.http.HttpSession;

import apijson.orm.*;
import com.alibaba.fastjson.JSONObject;

import apijson.NotNull;
import apijson.RequestMethod;
import apijson.orm.AbstractObjectParser;
import apijson.orm.AbstractParser;
import apijson.orm.Join;
import apijson.orm.SQLConfig;


/**简化Parser,getObject和getArray(getArrayConfig)都能用
* @author Lemon
*/
public class APIJSONObjectParser extends AbstractObjectParser {
public class APIJSONObjectParser<T extends Object> extends AbstractObjectParser<T> {
public static final String TAG = "APIJSONObjectParser";

/**for single object
Expand All @@ -44,26 +41,26 @@ public class APIJSONObjectParser extends AbstractObjectParser {
* @param isArrayMainTable
* @throws Exception
*/
public APIJSONObjectParser(HttpSession session, @NotNull JSONObject request, String parentPath, SQLConfig arrayConfig
public APIJSONObjectParser(HttpSession session, @NotNull JSONObject request, String parentPath, SQLConfig<T> arrayConfig
, boolean isSubquery, boolean isTable, boolean isArrayMainTable) throws Exception {
super(request, parentPath, arrayConfig, isSubquery, isTable, isArrayMainTable);
}

@Override
public APIJSONObjectParser setMethod(RequestMethod method) {
public APIJSONObjectParser<T> setMethod(RequestMethod method) {
super.setMethod(method);
return this;
}

@Override
public APIJSONObjectParser setParser(AbstractParser<?> parser) {
public APIJSONObjectParser<T> setParser(Parser<T> parser) {
super.setParser(parser);
return this;
}


@Override
public SQLConfig newSQLConfig(RequestMethod method, String table, String alias, JSONObject request, List<Join> joinList, boolean isProcedure) throws Exception {
public SQLConfig<T> newSQLConfig(RequestMethod method, String table, String alias, JSONObject request, List<Join> joinList, boolean isProcedure) throws Exception {
return APIJSONSQLConfig.newSQLConfig(method, table, alias, request, joinList, isProcedure);
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/apijson/framework/APIJSONParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ public Object onFunctionParse(String key, String function, String parentPath, St


@Override
public APIJSONObjectParser createObjectParser(JSONObject request, String parentPath, SQLConfig arrayConfig
public APIJSONObjectParser<T> createObjectParser(JSONObject request, String parentPath, SQLConfig<T> arrayConfig
, boolean isSubquery, boolean isTable, boolean isArrayMainTable) throws Exception {

return new APIJSONObjectParser(getSession(), request, parentPath, arrayConfig, isSubquery, isTable, isArrayMainTable) {
return new APIJSONObjectParser<T>(getSession(), request, parentPath, arrayConfig, isSubquery, isTable, isArrayMainTable) {

// @Override
// protected APIJSONSQLConfig newQueryConfig() {
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/apijson/framework/APIJSONSQLConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public class APIJSONSQLConfig<T extends Object> extends AbstractSQLConfig<T> {
SIMPLE_CALLBACK = new SimpleCallback<Object>() {

@Override
public SQLConfig getSQLConfig(RequestMethod method, String database, String schema,String datasource, String table) {
SQLConfig config = APIJSON_CREATOR.createSQLConfig();
public SQLConfig<Object> getSQLConfig(RequestMethod method, String database, String schema,String datasource, String table) {
SQLConfig<Object> config = APIJSON_CREATOR.createSQLConfig();
config.setMethod(method);
config.setDatabase(database);
config.setDatasource(datasource);
Expand Down Expand Up @@ -228,14 +228,14 @@ public APIJSONSQLConfig(RequestMethod method, int count, int page) {
* @return
* @throws Exception
*/
public static SQLConfig newSQLConfig(RequestMethod method, String table, String alias, JSONObject request, List<Join> joinList, boolean isProcedure) throws Exception {
return newSQLConfig(method, table, alias, request, joinList, isProcedure, SIMPLE_CALLBACK);
public static <T extends Object> SQLConfig<T> newSQLConfig(RequestMethod method, String table, String alias, JSONObject request, List<Join> joinList, boolean isProcedure) throws Exception {
return (SQLConfig<T>) newSQLConfig(method, table, alias, request, joinList, isProcedure, SIMPLE_CALLBACK);
}


// 支持 !key 反选字段 和 字段名映射,依赖插件 https://github.com/APIJSON/apijson-column
@Override
public AbstractSQLConfig setColumn(List<String> column) {
public AbstractSQLConfig<T> setColumn(List<String> column) {
if (ENABLE_COLUMN_CONFIG) {
column = ColumnUtil.compatInputColumn(column, getTable(), getMethod(), getVersion(), ! isConfigTable());
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/apijson/framework/APIJSONSQLExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public class APIJSONSQLExecutor<T extends Object> extends AbstractSQLExecutor<T>


@Override
public PreparedStatement setArgument(@NotNull SQLConfig config, @NotNull PreparedStatement statement, int index, Object value) throws SQLException {
public PreparedStatement setArgument(@NotNull SQLConfig<T> config, @NotNull PreparedStatement statement, int index, Object value) throws SQLException {
if (config.isPostgreSQL() && JSON.isBooleanOrNumberOrString(value) == false) {
PGobject o = new PGobject();
o.setType("jsonb");
Expand All @@ -87,7 +87,7 @@ public PreparedStatement setArgument(@NotNull SQLConfig config, @NotNull Prepare


@Override
protected Object getValue(SQLConfig config, ResultSet rs, ResultSetMetaData rsmd, int tablePosition,
protected Object getValue(SQLConfig<T> config, ResultSet rs, ResultSetMetaData rsmd, int tablePosition,
JSONObject table, int columnIndex, String lable, Map<String, JSONObject> childMap) throws Exception {

Object value = super.getValue(config, rs, rsmd, tablePosition, table, columnIndex, lable, childMap);
Expand All @@ -97,7 +97,7 @@ protected Object getValue(SQLConfig config, ResultSet rs, ResultSetMetaData rsmd

// 支持 !key 反选字段 和 字段名映射,依赖插件 https://github.com/APIJSON/apijson-column
@Override
protected String getKey(SQLConfig config, ResultSet rs, ResultSetMetaData rsmd, int tablePosition, JSONObject table,
protected String getKey(SQLConfig<T> config, ResultSet rs, ResultSetMetaData rsmd, int tablePosition, JSONObject table,
int columnIndex, Map<String, JSONObject> childMap) throws Exception {

String key = super.getKey(config, rs, rsmd, tablePosition, table, columnIndex, childMap);
Expand Down

0 comments on commit 550fd94

Please sign in to comment.