diff --git a/APIJSONORM/src/main/java/apijson/JSONObject.java b/APIJSONORM/src/main/java/apijson/JSONObject.java index 571c0aed..5d507740 100755 --- a/APIJSONORM/src/main/java/apijson/JSONObject.java +++ b/APIJSONORM/src/main/java/apijson/JSONObject.java @@ -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 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> diff --git a/APIJSONORM/src/main/java/apijson/orm/AbstractFunctionParser.java b/APIJSONORM/src/main/java/apijson/orm/AbstractFunctionParser.java index 75f0d261..25739bf1 100755 --- a/APIJSONORM/src/main/java/apijson/orm/AbstractFunctionParser.java +++ b/APIJSONORM/src/main/java/apijson/orm/AbstractFunctionParser.java @@ -25,7 +25,7 @@ /**可远程调用的函数类 * @author Lemon */ -public class AbstractFunctionParser implements FunctionParser { +public class AbstractFunctionParser implements FunctionParser { private static final String TAG = "AbstractFunctionParser"; /**是否解析参数 key 的对应的值,不用手动编码 curObj.getString(key) @@ -65,15 +65,15 @@ public AbstractFunctionParser(RequestMethod method, String tag, int version, @No setRequest(request); } - private Parser parser; + private Parser parser; @Override - public Parser getParser() { + public Parser getParser() { return parser; } @Override - public AbstractFunctionParser setParser(Parser parser) { + public AbstractFunctionParser setParser(Parser parser) { this.parser = parser; return this; } @@ -84,7 +84,7 @@ public RequestMethod getMethod() { } @Override - public AbstractFunctionParser setMethod(RequestMethod method) { + public AbstractFunctionParser setMethod(RequestMethod method) { this.method = method; return this; } @@ -95,7 +95,7 @@ public String getTag() { } @Override - public AbstractFunctionParser setTag(String tag) { + public AbstractFunctionParser setTag(String tag) { this.tag = tag; return this; } @@ -106,7 +106,7 @@ public int getVersion() { } @Override - public AbstractFunctionParser setVersion(int version) { + public AbstractFunctionParser setVersion(int version) { this.version = version; return this; } @@ -119,7 +119,7 @@ public String getKey() { } @Override - public AbstractFunctionParser setKey(String key) { + public AbstractFunctionParser setKey(String key) { this.key = key; return this; } @@ -132,7 +132,7 @@ public String getParentPath() { } @Override - public AbstractFunctionParser setParentPath(String parentPath) { + public AbstractFunctionParser setParentPath(String parentPath) { this.parentPath = parentPath; return this; } @@ -145,7 +145,7 @@ public String getCurrentName() { } @Override - public AbstractFunctionParser setCurrentName(String currentName) { + public AbstractFunctionParser setCurrentName(String currentName) { this.currentName = currentName; return this; } @@ -157,7 +157,7 @@ public JSONObject getRequest() { } @Override - public AbstractFunctionParser setRequest(@NotNull JSONObject request) { + public AbstractFunctionParser setRequest(@NotNull JSONObject request) { this.request = request; return this; } @@ -171,7 +171,7 @@ public JSONObject getCurrentObject() { } @Override - public AbstractFunctionParser setCurrentObject(@NotNull JSONObject currentObject) { + public AbstractFunctionParser setCurrentObject(@NotNull JSONObject currentObject) { this.currentObject = currentObject; return this; } @@ -294,7 +294,7 @@ public T getArgVal(String path, Class clazz) { /**根据路径取值 * @param path * @param clazz - * @param tryAll false-仅当前对象,true-本次请求的全局对象以及 Parser 缓存值 + * @param tryAll false-仅当前对象,true-本次请求的全局对象以及 Parser 缓存值 * @return * @param */ @@ -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 Object invoke(@NotNull AbstractFunctionParser 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 !"); @@ -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 中注册!"); } int version = row.getIntValue("version"); @@ -413,7 +413,7 @@ public static Object invoke(@NotNull AbstractFunctionParser parser, @NotNull Str } } - + /**反射调用 * @param parser * @param methodName @@ -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 Object invoke(@NotNull AbstractFunctionParser parser, @NotNull String methodName , @NotNull Class[] parameterTypes, @NotNull Object[] args) throws Exception { return invoke(parser, methodName, parameterTypes, args, null, null, null); } @@ -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 Object invoke(@NotNull AbstractFunctionParser parser, @NotNull String methodName , @NotNull Class[] parameterTypes, @NotNull Object[] args, String returnType , JSONObject currentObject, ScriptExecutor scriptExecutor) throws Exception { if (scriptExecutor != null) { @@ -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 Object invokeScript(@NotNull AbstractFunctionParser 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) { diff --git a/APIJSONORM/src/main/java/apijson/orm/AbstractObjectParser.java b/APIJSONORM/src/main/java/apijson/orm/AbstractObjectParser.java index bd4a6846..e6442013 100755 --- a/APIJSONORM/src/main/java/apijson/orm/AbstractObjectParser.java +++ b/APIJSONORM/src/main/java/apijson/orm/AbstractObjectParser.java @@ -35,20 +35,24 @@ /**简化Parser,getObject和getArray(getArrayConfig)都能用 * @author Lemon */ -public abstract class AbstractObjectParser implements ObjectParser { +public abstract class AbstractObjectParser implements ObjectParser { private static final String TAG = "AbstractObjectParser"; @NotNull - protected AbstractParser parser; - public AbstractObjectParser setParser(AbstractParser parser) { - this.parser = parser; + protected AbstractParser parser; + @Override + public AbstractParser getParser() { + return parser; + } + @Override + public AbstractObjectParser setParser(Parser parser) { + this.parser = (AbstractParser) parser; return this; } - protected JSONObject request;//不用final是为了recycle protected String parentPath;//不用final是为了recycle - protected SQLConfig arrayConfig;//不用final是为了recycle + protected SQLConfig arrayConfig;//不用final是为了recycle protected boolean isSubquery; protected final int type; @@ -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无影响 } @@ -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 joinList, boolean isProcedure) throws Exception { - SQLConfig config = newSQLConfig(method, table, alias, request, joinList, isProcedure) + SQLConfig 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 config, boolean isProcedure) throws Exception { if (parser.getSQLExecutor() == null) { parser.createSQLExecutor(); } @@ -1217,13 +1222,13 @@ public String getAlias() { return alias; } @Override - public SQLConfig getArrayConfig() { + public SQLConfig getArrayConfig() { return arrayConfig; } @Override - public SQLConfig getSQLConfig() { + public SQLConfig getSQLConfig() { return sqlConfig; } diff --git a/APIJSONORM/src/main/java/apijson/orm/AbstractParser.java b/APIJSONORM/src/main/java/apijson/orm/AbstractParser.java index 4a5ed4bc..c552888b 100755 --- a/APIJSONORM/src/main/java/apijson/orm/AbstractParser.java +++ b/APIJSONORM/src/main/java/apijson/orm/AbstractParser.java @@ -39,7 +39,7 @@ import static apijson.RequestMethod.CRUD; import static apijson.RequestMethod.GET; -/**parser for parsing request to JSONObject +/**Parser for parsing request to JSONObject * @author Lemon */ public abstract class AbstractParser implements Parser, ParserCreator, VerifierCreator, SQLCreator { @@ -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 config) throws Exception { if (Log.DEBUG) { Log.i(TAG, "onVerifyRole config = " + JSON.toJSONString(config)); } @@ -1036,7 +1036,7 @@ public JSONObject getStructure(@NotNull String table, String method, String tag, } // 获取指定的JSON结构 <<<<<<<<<<<<<< - SQLConfig config = createSQLConfig().setMethod(GET).setTable(table); + SQLConfig config = createSQLConfig().setMethod(GET).setTable(table); config.setPrepared(false); config.setColumn(Arrays.asList("structure")); @@ -1066,7 +1066,7 @@ public JSONObject getStructure(@NotNull String table, String method, String tag, protected Map arrayObjectParserCacheMap = new HashMap<>(); - // protected SQLConfig itemConfig; + // protected SQLConfig itemConfig; /**获取单个对象,该对象处于parentObject内 * @param request parentObject 的 value * @param parentPath parentObject 的路径 @@ -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 arrayConfig, boolean isSubquery) throws Exception { if (Log.DEBUG) { Log.i(TAG, "\ngetObject: parentPath = " + parentPath @@ -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 op = null; if (isReuse) { // 数组主表使用专门的缓存数据 op = arrayObjectParserCacheMap.get(parentPath.substring(0, parentPath.lastIndexOf("[]") + 2)); op.setParentPath(parentPath); @@ -1143,7 +1143,7 @@ public JSONObject onObjectParse(final JSONObject request if (compat != null && compat) { // 解决对聚合函数字段通过 query:2 分页查总数返回值错误 // 这里可能改变了内部的一些数据,下方通过 arrayConfig 还原 - SQLConfig cfg = op.setSQLConfig(0, 0, 0).getSQLConfig(); + SQLConfig cfg = op.setSQLConfig(0, 0, 0).getSQLConfig(); boolean isExplain = cfg.isExplain(); cfg.setExplain(false); @@ -1151,7 +1151,7 @@ public JSONObject onObjectParse(final JSONObject request subqy.setFrom(cfg.getTable()); subqy.setConfig(cfg); - SQLConfig countSQLCfg = createSQLConfig(); + SQLConfig countSQLCfg = createSQLConfig(); countSQLCfg.setColumn(Arrays.asList("count(*):count")); countSQLCfg.setFrom(subqy); @@ -1336,7 +1336,7 @@ else if (childKeys.length == 1 && JSONRequest.isTableKey(childKeys[0])) { // //Table<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< response = new JSONArray(); - SQLConfig config = createSQLConfig() + SQLConfig config = createSQLConfig() .setMethod(requestMethod) .setCount(size) .setPage(page2) @@ -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 中强制把 id, id{}, userId, userId{} 放到了最前面 tableObj.put(key, tableObj.remove(key)); if (refObj.size() != tableObj.size()) { // 把 key 强制放最前,AbstractSQLExcecutor 中 config.putWhere 也是放尽可能最前 refObj.putAll(tableObj); @@ -1757,8 +1757,8 @@ else if (join != null){ // 保证和 SQLExcecutor 缓存的 Config 里 where 顺序一致,生成的 SQL 也就一致 >>>>>>>>> } - //拼接多个 SQLConfig 的SQL语句,然后执行,再把结果分别缓存(Moment, User等)到 SQLExecutor 的 cacheMap - // AbstractSQLConfig config0 = null; + //拼接多个 SQLConfig 的SQL语句,然后执行,再把结果分别缓存(Moment, User等)到 SQLExecutor 的 cacheMap + // AbstractSQLConfig config0 = null; // String sql = "SELECT " + config0.getColumnString() + " FROM " + config0.getTable() + " INNER JOIN " + targetTable + " ON " // + onList.get(0) + config0.getGroupString() + config0.getHavingString() + config0.getOrderString(); @@ -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 config, boolean isSubquery) throws Exception { if (config == null) { Log.d(TAG, "executeSQL config == null >> return null;"); return null; @@ -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 取结果,可以解决并发执行导致内部计算出错 // executedSQLDuration += sqlExecutor.getExecutedSQLDuration() + sqlExecutor.getSqlResultDuration(); } @@ -2150,7 +2150,7 @@ protected void onClose() { queryResultMap = null; } - private void setOpMethod(JSONObject request, ObjectParser op, String key) { + private void setOpMethod(JSONObject request, ObjectParser op, String key) { String _method = key == null ? null : request.getString(apijson.JSONObject.KEY_METHOD); if (_method != null) { RequestMethod method = RequestMethod.valueOf(_method); // 必须精准匹配,避免缓存命中率低 diff --git a/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java b/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java index d162f6a7..887f721d 100755 --- a/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java +++ b/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java @@ -4704,7 +4704,7 @@ protected String concatJoinOn(@NotNull String sql, @NotNull String quote, @NotNu + quote + on.getTargetTable() + quote + "." + quote + on.getTargetKey() + quote; } else { - onJoinComplextRelation(sql, quote, j, jt, onList, on); + onJoinComplexRelation(sql, quote, j, jt, onList, on); if (">=".equals(rt) || "<=".equals(rt) || ">".equals(rt) || "<".equals(rt)) { if (isNot) { @@ -4892,13 +4892,6 @@ protected void onJoinComplexRelation(String sql, String quote, Join join, String "性能很差、需求极少,默认只允许 = 等价关联,如要取消禁用可在后端重写相关方法!"); } - /**已废弃,最早 6.2.0 移除,请改用 onJoinComplexRelation - */ - @Deprecated - protected void onJoinComplextRelation(String sql, String quote, Join join, String table, List onList, On on) { - onJoinComplexRelation(sql, quote, join, table, onList, on); - } - protected void onGetJoinString(Join join) throws UnsupportedOperationException { } protected void onGetCrossJoinString(Join join) throws UnsupportedOperationException { @@ -4914,7 +4907,7 @@ protected void onGetCrossJoinString(Join join) throws UnsupportedOperationExcept * @return * @throws Exception */ - public static SQLConfig newSQLConfig(RequestMethod method, String table, String alias + public static SQLConfig newSQLConfig(RequestMethod method, String table, String alias , JSONObject request, List joinList, boolean isProcedure, Callback callback) throws Exception { if (request == null) { // User:{} 这种空内容在查询时也有效 throw new NullPointerException(TAG + ": newSQLConfig request == null!"); @@ -4934,7 +4927,7 @@ public static SQLConfig newSQLConfig(RequestMethod method, St String schema = request.getString(KEY_SCHEMA); String datasource = request.getString(KEY_DATASOURCE); - SQLConfig config = callback.getSQLConfig(method, database, schema, datasource, table); + SQLConfig config = callback.getSQLConfig(method, database, schema, datasource, table); config.setAlias(alias); config.setDatabase(database); // 不删,后面表对象还要用的,必须放在 parseJoin 前 @@ -5680,7 +5673,7 @@ else if (newHaving != null) { * @return * @throws Exception */ - public static SQLConfig parseJoin(RequestMethod method, SQLConfig config + public static SQLConfig parseJoin(RequestMethod method, SQLConfig config , List joinList, Callback callback) throws Exception { boolean isQuery = RequestMethod.isQueryMethod(method); config.setKeyPrefix(isQuery && config.isMain() == false); @@ -5697,8 +5690,8 @@ public static SQLConfig parseJoin(RequestMethod method, SQLCo table = j.getTable(); alias = j.getAlias(); //JOIN子查询不能设置LIMIT,因为ON关系是在子查询后处理的,会导致结果会错误 - SQLConfig joinConfig = newSQLConfig(method, table, alias, j.getRequest(), null, false, callback); - SQLConfig cacheConfig = j.canCacheViceTable() == false ? null : newSQLConfig(method, table, alias + SQLConfig joinConfig = newSQLConfig(method, table, alias, j.getRequest(), null, false, callback); + SQLConfig cacheConfig = j.canCacheViceTable() == false ? null : newSQLConfig(method, table, alias , j.getRequest(), null, false, callback).setCount(j.getCount()); if (j.isAppJoin() == false) { //除了 @ APP JOIN,其它都是 SQL JOIN,则副表要这样配置 @@ -5725,7 +5718,7 @@ else if (joinConfig.getDatabase().equals(config.getDatabase()) == false) { joinConfig.setMain(false).setKeyPrefix(true); if (j.getOuter() != null) { - SQLConfig outterConfig = newSQLConfig(method, table, alias, j.getOuter(), null, false, callback); + SQLConfig outterConfig = newSQLConfig(method, table, alias, j.getOuter(), null, false, callback); outterConfig.setMain(false) .setKeyPrefix(true) .setDatabase(joinConfig.getDatabase()) @@ -5934,7 +5927,7 @@ public static interface Callback extends IdCallback { * @param table * @return */ - SQLConfig getSQLConfig(RequestMethod method, String database, String schema, String datasource, String table); + SQLConfig getSQLConfig(RequestMethod method, String database, String schema, String datasource, String table); /**combine 里的 key 在 request 中 value 为 null 或不存在,即 request 中缺少用来作为 combine 条件的 key: value * @param combine diff --git a/APIJSONORM/src/main/java/apijson/orm/AbstractSQLExecutor.java b/APIJSONORM/src/main/java/apijson/orm/AbstractSQLExecutor.java index d11638bd..e4b38d0b 100755 --- a/APIJSONORM/src/main/java/apijson/orm/AbstractSQLExecutor.java +++ b/APIJSONORM/src/main/java/apijson/orm/AbstractSQLExecutor.java @@ -50,7 +50,7 @@ public Parser getParser() { return parser; } @Override - public AbstractSQLExecutor setParser(Parser parser) { + public AbstractSQLExecutor setParser(Parser parser) { this.parser = parser; return this; } @@ -93,10 +93,10 @@ public long getSqlResultDuration() { /**保存缓存 * @param sql key * @param list value - * @param config 一般主表 SQLConfig 不为 null,JOIN 副表的为 null + * @param config 一般主表 SQLConfig 不为 null,JOIN 副表的为 null */ @Override - public void putCache(String sql, List list, SQLConfig config) { + public void putCache(String sql, List list, SQLConfig config) { if (sql == null || list == null) { // 空 list 有效,说明查询过 sql 了 || list.isEmpty()) { Log.i(TAG, "saveList sql == null || list == null >> return;"); return; @@ -107,26 +107,26 @@ public void putCache(String sql, List list, SQLConfig config) { /**获取缓存 * @param sql key - * @param config 一般主表 SQLConfig 不为 null,JOIN 副表的为 null + * @param config 一般主表 SQLConfig 不为 null,JOIN 副表的为 null */ @Override - public List getCache(String sql, SQLConfig config) { + public List getCache(String sql, SQLConfig config) { return cacheMap.get(sql); } /**获取缓存 * @param sql key * @param position - * @param config 一般主表 SQLConfig 不为 null,JOIN 副表的为 null + * @param config 一般主表 SQLConfig 不为 null,JOIN 副表的为 null * @return */ @Override - public JSONObject getCacheItem(String sql, int position, SQLConfig config) { + public JSONObject getCacheItem(String sql, int position, SQLConfig config) { List list = getCache(sql, config); return getCacheItem(list, position, config); } - public JSONObject getCacheItem(List list, int position, SQLConfig config) { + public JSONObject getCacheItem(List list, int position, SQLConfig config) { // 只要 list 不为 null,则如果 list.get(position) == null,则返回 {} ,避免再次 SQL 查询 if (list == null) { return null; @@ -145,7 +145,7 @@ public JSONObject getCacheItem(List list, int position, SQLConfig co * @param config */ @Override - public void removeCache(String sql, SQLConfig config) { + public void removeCache(String sql, SQLConfig config) { if (sql == null) { Log.i(TAG, "removeList sql == null >> return;"); return; @@ -177,9 +177,8 @@ public ResultSet execute(@NotNull Statement statement, String sql) throws Except * @throws Exception */ @Override - public JSONObject execute(@NotNull SQLConfig config, boolean unknownType) throws Exception { + public JSONObject execute(@NotNull SQLConfig config, boolean unknownType) throws Exception { long executedSQLStartTime = System.currentTimeMillis(); - final String sql = config.getSQL(false); if (StringUtil.isEmpty(sql, true)) { @@ -433,7 +432,7 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknownType) throws // 为什么 isExplain == false 不用判断?因为所有字段都在一张 Query Plan 表 if (index <= 0 && columnIndexAndJoinMap != null) { // && viceColumnStart > length) { - SQLConfig curConfig = curJoin == null || ! curJoin.isSQLJoin() ? null : curJoin.getCacheConfig(); + SQLConfig curConfig = curJoin == null || ! curJoin.isSQLJoin() ? null : curJoin.getCacheConfig(); List curColumn = curConfig == null ? null : curConfig.getColumn(); String sqlTable = curConfig == null ? null : curConfig.getSQLTable(); String sqlAlias = curConfig == null ? null : curConfig.getAlias(); @@ -462,7 +461,7 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknownType) throws int nextViceColumnStart = lastViceColumnStart; // 主表没有 @column 时会偏小 lastViceColumnStart for (int j = lastViceTableStart; j < joinList.size(); j++) { // 查找副表 @column,定位字段所在表 Join join = joinList.get(j); - SQLConfig cfg = join == null || ! join.isSQLJoin() ? null : join.getJoinConfig(); + SQLConfig cfg = join == null || ! join.isSQLJoin() ? null : join.getJoinConfig(); List c = cfg == null ? null : cfg.getColumn(); nextViceColumnStart += (c != null && ! c.isEmpty() ? @@ -507,7 +506,7 @@ else if (config.isClickHouse() && (sqlTable.startsWith("`") || sqlTable.startsWi if (toFindJoin) { // 找到对应的副表 JOIN 配置 for (int j = lastViceTableStart; j < joinList.size(); j++) { // 查找副表 @column,定位字段所在表 Join join = joinList.get(j); - SQLConfig cfg = join == null || ! join.isSQLJoin() ? null : join.getJoinConfig(); + SQLConfig cfg = join == null || ! join.isSQLJoin() ? null : join.getJoinConfig(); if (cfg != null && StringUtil.equalsIgnoreCase(sqlTable, cfg.getSQLTable()) ) { // FIXME 导致副表字段错放到主表 && StringUtil.equals(sqlAlias, cfg.getAlias())) { @@ -552,7 +551,7 @@ else if (config.isClickHouse() && (sqlTable.startsWith("`") || sqlTable.startsWi // 如果是主表则直接用主表对应的 item,否则缓存副表数据到 childMap Join prevJoin = columnIndexAndJoinMap == null || i < 2 ? null : columnIndexAndJoinMap[i - 2]; if (curJoin != prevJoin) { // 前后字段不在同一个表对象,即便后面出现 null,也不该是主表数据,而是逻辑 bug 导致 - SQLConfig viceConfig = curJoin != null && curJoin.isSQLJoin() ? curJoin.getCacheConfig() : null; + SQLConfig viceConfig = curJoin != null && curJoin.isSQLJoin() ? curJoin.getCacheConfig() : null; if (viceConfig != null) { //FIXME 只有和主表关联才能用 item,否则应该从 childMap 查其它副表数据 List onList = curJoin.getOnList(); int size = onList == null ? 0 : onList.size(); @@ -568,7 +567,7 @@ else if (config.isClickHouse() && (sqlTable.startsWith("`") || sqlTable.startsWi } } } - String viceSql = viceConfig == null ? null : viceConfig.getSQL(false); //TODO 在 SQLConfig 缓存 SQL,减少大量的重复生成 + String viceSql = viceConfig == null ? null : viceConfig.getSQL(false); //TODO 在 SQLConfig 缓存 SQL,减少大量的重复生成 if (StringUtil.isEmpty(viceSql, true)) { Log.i(TAG, "execute StringUtil.isEmpty(viceSql, true) >> item = null; >> "); @@ -676,7 +675,7 @@ else if (curJoin.isOuterJoin() || curJoin.isAntiJoin()) { * @param childMap * @throws Exception */ - protected void executeAppJoin(SQLConfig config, List resultList, Map> childMap) throws Exception { + protected void executeAppJoin(SQLConfig config, List resultList, Map> childMap) throws Exception { List joinList = config.getJoinList(); if (joinList != null) { @@ -686,7 +685,7 @@ protected void executeAppJoin(SQLConfig config, List resultList, Map continue; } - SQLConfig cc = join.getCacheConfig(); //这里用config改了getSQL后再还原很麻烦,所以提前给一个config2更好 + SQLConfig cc = join.getCacheConfig(); //这里用config改了getSQL后再还原很麻烦,所以提前给一个config2更好 if (cc == null) { if (Log.DEBUG) { throw new NullPointerException("服务器内部错误, executeAppJoin cc == null ! 导致不能缓存 @ APP JOIN 的副表数据!"); @@ -694,7 +693,7 @@ protected void executeAppJoin(SQLConfig config, List resultList, Map continue; } - SQLConfig jc = join.getJoinConfig(); + SQLConfig jc = join.getJoinConfig(); List onList = join.getOnList(); On on = onList == null || onList.isEmpty() ? null : onList.get(0); // APP JOIN 应该有且只有一个 ON 条件 @@ -901,7 +900,7 @@ protected void executeAppJoin(SQLConfig config, List resultList, Map * @return result * @throws Exception */ - protected JSONObject onPutColumn(@NotNull SQLConfig config, @NotNull ResultSet rs, @NotNull ResultSetMetaData rsmd + protected JSONObject onPutColumn(@NotNull SQLConfig config, @NotNull ResultSet rs, @NotNull ResultSetMetaData rsmd , final int tablePosition, @NotNull JSONObject table, final int columnIndex, Join join, Map childMap) throws Exception { if (table == null) { // 对应副表 viceSql 不能生成正常 SQL, 或者是 ! - Outer, ( - ANTI JOIN 的副表这种不需要缓存及返回的数据 Log.i(TAG, "onPutColumn table == null >> return table;"); @@ -935,7 +934,7 @@ protected JSONObject onPutColumn(@NotNull SQLConfig config, @NotNull ResultSet r * @return * @throws SQLException */ - protected boolean isHideColumn(@NotNull SQLConfig config, @NotNull ResultSet rs, @NotNull ResultSetMetaData rsmd + protected boolean isHideColumn(@NotNull SQLConfig config, @NotNull ResultSet rs, @NotNull ResultSetMetaData rsmd , final int tablePosition, @NotNull JSONObject table, final int columnIndex, Map childMap) throws SQLException { return rsmd.getColumnName(columnIndex).startsWith("_"); } @@ -949,7 +948,7 @@ protected boolean isHideColumn(@NotNull SQLConfig config, @NotNull ResultSet rs, * @param table * @return resultList */ - protected List onPutTable(@NotNull SQLConfig config, @NotNull ResultSet rs, @NotNull ResultSetMetaData rsmd + protected List onPutTable(@NotNull SQLConfig config, @NotNull ResultSet rs, @NotNull ResultSetMetaData rsmd , @NotNull List resultList, int position, @NotNull JSONObject table) { resultList.add(table); @@ -958,7 +957,7 @@ protected List onPutTable(@NotNull SQLConfig config, @NotNull Result - protected String getKey(@NotNull SQLConfig config, @NotNull ResultSet rs, @NotNull ResultSetMetaData rsmd + protected String getKey(@NotNull SQLConfig config, @NotNull ResultSet rs, @NotNull ResultSetMetaData rsmd , final int tablePosition, @NotNull JSONObject table, final int columnIndex, Map childMap) throws Exception { long startTime = System.currentTimeMillis(); String key = rsmd.getColumnLabel(columnIndex); // dotIndex < 0 ? lable : lable.substring(dotIndex + 1); @@ -978,7 +977,7 @@ protected String getKey(@NotNull SQLConfig config, @NotNull ResultSet rs, @NotNu return key; } - protected Object getValue(@NotNull SQLConfig config, @NotNull ResultSet rs, @NotNull ResultSetMetaData rsmd + protected Object getValue(@NotNull SQLConfig config, @NotNull ResultSet rs, @NotNull ResultSetMetaData rsmd , final int tablePosition, @NotNull JSONObject table, final int columnIndex, String lable, Map childMap) throws Exception { long startTime = System.currentTimeMillis(); @@ -1068,7 +1067,7 @@ else if (value instanceof Clob) { //SQL Server TEXT 类型 居然走这个 * @return */ @Override - public boolean isJSONType(@NotNull SQLConfig config, ResultSetMetaData rsmd, int position, String lable) { + public boolean isJSONType(@NotNull SQLConfig config, ResultSetMetaData rsmd, int position, String lable) { try { long startTime = System.currentTimeMillis(); String column = rsmd.getColumnTypeName(position); @@ -1094,11 +1093,11 @@ public boolean isJSONType(@NotNull SQLConfig config, ResultSetMetaData rsmd, int @Override // 重写是为了返回类型从 Statement 改为 PreparedStatement,避免其它方法出错 - public PreparedStatement getStatement(@NotNull SQLConfig config) throws Exception { + public PreparedStatement getStatement(@NotNull SQLConfig config) throws Exception { return getStatement(config, null); } @Override - public PreparedStatement getStatement(@NotNull SQLConfig config, String sql) throws Exception { + public PreparedStatement getStatement(@NotNull SQLConfig config, String sql) throws Exception { if (StringUtil.isEmpty(sql)) { sql = config.getSQL(config.isPrepared()); } @@ -1150,7 +1149,7 @@ else if (RequestMethod.isGetMethod(config.getMethod(), true)) { return statement; } - public PreparedStatement setArgument(@NotNull SQLConfig config, @NotNull PreparedStatement statement, int index, Object value) throws SQLException { + public PreparedStatement setArgument(@NotNull SQLConfig config, @NotNull PreparedStatement statement, int index, Object value) throws SQLException { //JSON.isBooleanOrNumberOrString(v) 解决 PostgreSQL: Can't infer the SQL type to use for an instance of com.alibaba.fastjson.JSONArray if (apijson.JSON.isBooleanOrNumberOrString(value)) { statement.setObject(index + 1, value); //PostgreSQL JDBC 不支持隐式类型转换 tinyint = varchar 报错 @@ -1165,7 +1164,7 @@ public PreparedStatement setArgument(@NotNull SQLConfig config, @NotNull Prepare protected Connection connection; @NotNull @Override - public Connection getConnection(@NotNull SQLConfig config) throws Exception { + public Connection getConnection(@NotNull SQLConfig config) throws Exception { String connectionKey = config.getDatasource() + "-" + config.getDatabase(); connection = connectionMap.get(connectionKey); if (connection == null || connection.isClosed()) { @@ -1346,7 +1345,7 @@ public void close() { } @Override - public ResultSet executeQuery(@NotNull SQLConfig config, String sql) throws Exception { + public ResultSet executeQuery(@NotNull SQLConfig config, String sql) throws Exception { if (config.isPrepared() == false || config.isTDengine() // TDengine JDBC 不支持 PreparedStatement || (config.isExplain() && (config.isPresto() || config.isTrino()))) { // Presto JDBC 0.277 在 EXPLAIN 模式下预编译值不会替代 ? 占位导致报错 @@ -1371,7 +1370,7 @@ public ResultSet executeQuery(@NotNull SQLConfig config, String sql) throws Exce @Override - public int executeUpdate(@NotNull SQLConfig config, String sql) throws Exception { + public int executeUpdate(@NotNull SQLConfig config, String sql) throws Exception { Statement stt; int count; if (config.isTDengine()) { diff --git a/APIJSONORM/src/main/java/apijson/orm/FunctionParser.java b/APIJSONORM/src/main/java/apijson/orm/FunctionParser.java index 189d9d44..ec5aefbd 100644 --- a/APIJSONORM/src/main/java/apijson/orm/FunctionParser.java +++ b/APIJSONORM/src/main/java/apijson/orm/FunctionParser.java @@ -14,41 +14,41 @@ /**远程函数解析器 * @author Lemon */ -public interface FunctionParser { +public interface FunctionParser { Object invoke(@NotNull String function, @NotNull JSONObject currentObject) throws Exception; Object invoke(@NotNull String function, @NotNull JSONObject currentObject, boolean containRaw) throws Exception; - Parser getParser(); + Parser getParser(); - AbstractFunctionParser setParser(Parser parser); + FunctionParser setParser(Parser parser); RequestMethod getMethod(); - FunctionParser setMethod(RequestMethod method); + FunctionParser setMethod(RequestMethod method); String getTag(); - FunctionParser setTag(String tag); + FunctionParser setTag(String tag); int getVersion(); - AbstractFunctionParser setVersion(int version); + FunctionParser setVersion(int version); @NotNull JSONObject getRequest(); - FunctionParser setRequest(@NotNull JSONObject request); + FunctionParser setRequest(@NotNull JSONObject request); String getKey(); - FunctionParser setKey(String key); + FunctionParser setKey(String key); String getParentPath(); - FunctionParser setParentPath(String parentPath); + FunctionParser setParentPath(String parentPath); String getCurrentName(); - FunctionParser setCurrentName(String currentName); + FunctionParser setCurrentName(String currentName); @NotNull JSONObject getCurrentObject(); - FunctionParser setCurrentObject(@NotNull JSONObject currentObject); + FunctionParser setCurrentObject(@NotNull JSONObject currentObject); diff --git a/APIJSONORM/src/main/java/apijson/orm/ObjectParser.java b/APIJSONORM/src/main/java/apijson/orm/ObjectParser.java index 2f0b3a3c..6df050e0 100755 --- a/APIJSONORM/src/main/java/apijson/orm/ObjectParser.java +++ b/APIJSONORM/src/main/java/apijson/orm/ObjectParser.java @@ -18,20 +18,22 @@ /**简化Parser,getObject和getArray(getArrayConfig)都能用 * @author Lemon */ -public interface ObjectParser { +public interface ObjectParser { + + Parser getParser(); + ObjectParser setParser(Parser parser); String getParentPath(); - ObjectParser setParentPath(String parentPath); + ObjectParser setParentPath(String parentPath); /**解析成员 * response重新赋值 - * @param parentPath - * @param name + * @param name * @param isReuse * @return null or this * @throws Exception */ - ObjectParser parse(String name, boolean isReuse) throws Exception; + ObjectParser parse(String name, boolean isReuse) throws Exception; /**调用 parser 的 sqlExecutor 来解析结果 * @param method @@ -43,14 +45,14 @@ public interface ObjectParser { * @return * @throws Exception */ - public JSONObject parseResponse(RequestMethod method, String table, String alias, JSONObject request, List joinList, boolean isProcedure) throws Exception; + JSONObject parseResponse(RequestMethod method, String table, String alias, JSONObject request, List joinList, boolean isProcedure) throws Exception; /**调用 parser 的 sqlExecutor 来解析结果 * @param config * @param isProcedure * @return * @throws Exception */ - public JSONObject parseResponse(SQLConfig config, boolean isProcedure) throws Exception; + JSONObject parseResponse(SQLConfig config, boolean isProcedure) throws Exception; @@ -95,20 +97,20 @@ public interface ObjectParser { * @return {@link #setSQLConfig(int, int, int)} * @throws Exception */ - ObjectParser setSQLConfig() throws Exception; + ObjectParser setSQLConfig() throws Exception; /**SQL 配置 * @return * @throws Exception */ - ObjectParser setSQLConfig(int count, int page, int position) throws Exception; + ObjectParser setSQLConfig(int count, int page, int position) throws Exception; /**执行 SQL * @return * @throws Exception */ - ObjectParser executeSQL() throws Exception; + ObjectParser executeSQL() throws Exception; /** @@ -129,8 +131,8 @@ public interface ObjectParser { void onChildResponse() throws Exception; - SQLConfig newSQLConfig(boolean isProcedure) throws Exception; - SQLConfig newSQLConfig(RequestMethod method, String table, String alias, JSONObject request, List joinList, boolean isProcedure) throws Exception; + SQLConfig newSQLConfig(boolean isProcedure) throws Exception; + SQLConfig newSQLConfig(RequestMethod method, String table, String alias, JSONObject request, List joinList, boolean isProcedure) throws Exception; /** * response has the final value after parse (and query if isTableKey) @@ -143,7 +145,7 @@ public interface ObjectParser { void recycle(); - ObjectParser setMethod(RequestMethod method); + ObjectParser setMethod(RequestMethod method); RequestMethod getMethod(); @@ -151,9 +153,9 @@ public interface ObjectParser { String getPath(); String getTable(); String getAlias(); - SQLConfig getArrayConfig(); + SQLConfig getArrayConfig(); - SQLConfig getSQLConfig(); + SQLConfig getSQLConfig(); JSONObject getResponse(); JSONObject getSqlRequest(); JSONObject getSqlResponse(); diff --git a/APIJSONORM/src/main/java/apijson/orm/Parser.java b/APIJSONORM/src/main/java/apijson/orm/Parser.java index 65613e8c..969dff95 100755 --- a/APIJSONORM/src/main/java/apijson/orm/Parser.java +++ b/APIJSONORM/src/main/java/apijson/orm/Parser.java @@ -66,7 +66,7 @@ JSONObject parseCorrectRequest(RequestMethod method, String tag, int version, St JSONObject getStructure(String table, String method, String tag, int version) throws Exception; - JSONObject onObjectParse(JSONObject request, String parentPath, String name, SQLConfig arrayConfig, boolean isSubquery) throws Exception; + JSONObject onObjectParse(JSONObject request, String parentPath, String name, SQLConfig arrayConfig, boolean isSubquery) throws Exception; JSONArray onArrayParse(JSONObject request, String parentPath, String name, boolean isSubquery) throws Exception; @@ -81,7 +81,7 @@ JSONObject parseCorrectRequest(RequestMethod method, String tag, int version, St */ Object onFunctionParse(String key, String function, String parentPath, String currentName, JSONObject currentObject, boolean containRaw) throws Exception; - ObjectParser createObjectParser(JSONObject request, String parentPath, SQLConfig arrayConfig, boolean isSubquery, boolean isTable, boolean isArrayMainTable) throws Exception; + ObjectParser createObjectParser(JSONObject request, String parentPath, SQLConfig arrayConfig, boolean isSubquery, boolean isTable, boolean isArrayMainTable) throws Exception; int getDefaultQueryCount(); int getMaxQueryPage(); @@ -100,11 +100,11 @@ JSONObject parseCorrectRequest(RequestMethod method, String tag, int version, St void onVerifyLogin() throws Exception; void onVerifyContent() throws Exception; - void onVerifyRole(SQLConfig config) throws Exception; + void onVerifyRole(SQLConfig config) throws Exception; - JSONObject executeSQL(SQLConfig config, boolean isSubquery) throws Exception; + JSONObject executeSQL(SQLConfig config, boolean isSubquery) throws Exception; - SQLExecutor getSQLExecutor(); + SQLExecutor getSQLExecutor(); Verifier getVerifier(); diff --git a/APIJSONORM/src/main/java/apijson/orm/SQLExecutor.java b/APIJSONORM/src/main/java/apijson/orm/SQLExecutor.java index 0c11b075..2805682e 100755 --- a/APIJSONORM/src/main/java/apijson/orm/SQLExecutor.java +++ b/APIJSONORM/src/main/java/apijson/orm/SQLExecutor.java @@ -22,21 +22,21 @@ */ public interface SQLExecutor { Parser getParser(); - SQLExecutor setParser(Parser parser); + SQLExecutor setParser(Parser parser); /**保存缓存 * @param sql * @param list * @param config */ - void putCache(String sql, List list, SQLConfig config); + void putCache(String sql, List list, SQLConfig config); /**获取缓存 * @param sql * @param config * @return */ - List getCache(String sql, SQLConfig config); + List getCache(String sql, SQLConfig config); /**获取缓存 * @param sql @@ -44,13 +44,13 @@ public interface SQLExecutor { * @param config * @return */ - JSONObject getCacheItem(String sql, int position, SQLConfig config); + JSONObject getCacheItem(String sql, int position, SQLConfig config); /**移除缓存 * @param sql * @param config */ - void removeCache(String sql, SQLConfig config); + void removeCache(String sql, SQLConfig config); /**执行SQL * @param config @@ -58,7 +58,7 @@ public interface SQLExecutor { * @return * @throws Exception */ - JSONObject execute(@NotNull SQLConfig config, boolean unknownType) throws Exception; + JSONObject execute(@NotNull SQLConfig config, boolean unknownType) throws Exception; //executeQuery和executeUpdate这两个函数因为返回类型不同,所以不好合并 /**执行查询 @@ -66,20 +66,20 @@ public interface SQLExecutor { * @return * @throws SQLException */ - default ResultSet executeQuery(@NotNull SQLConfig config) throws Exception { + default ResultSet executeQuery(@NotNull SQLConfig config) throws Exception { return executeQuery(config, null); } - ResultSet executeQuery(@NotNull SQLConfig config, String sql) throws Exception; + ResultSet executeQuery(@NotNull SQLConfig config, String sql) throws Exception; /**执行增、删、改 * @param config * @return * @throws SQLException */ - default int executeUpdate(@NotNull SQLConfig config) throws Exception { + default int executeUpdate(@NotNull SQLConfig config) throws Exception { return executeUpdate(config, null); } - int executeUpdate(@NotNull SQLConfig config, String sql) throws Exception; + int executeUpdate(@NotNull SQLConfig config, String sql) throws Exception; /**判断是否为JSON类型 @@ -89,14 +89,14 @@ default int executeUpdate(@NotNull SQLConfig config) throws Exception { * @param lable * @return */ - boolean isJSONType(@NotNull SQLConfig config, ResultSetMetaData rsmd, int position, String lable); + boolean isJSONType(@NotNull SQLConfig config, ResultSetMetaData rsmd, int position, String lable); - Connection getConnection(@NotNull SQLConfig config) throws Exception; - default Statement getStatement(@NotNull SQLConfig config) throws Exception { + Connection getConnection(@NotNull SQLConfig config) throws Exception; + default Statement getStatement(@NotNull SQLConfig config) throws Exception { return getStatement(config, null); } - Statement getStatement(@NotNull SQLConfig config, String sql) throws Exception; + Statement getStatement(@NotNull SQLConfig config, String sql) throws Exception; int getTransactionIsolation(); void setTransactionIsolation(int transactionIsolation);