Skip to content

Commit

Permalink
AbstractSQLExecutor 优化增删改未成功也未抛异常的 code 和 msg;AbstractParser 优化请求及响应的…
Browse files Browse the repository at this point in the history
…日志打印;AbstractSQLConfig 优化 key$ 的格式校验
  • Loading branch information
TommyLemon committed Apr 6, 2021
1 parent 1a75bfb commit 8cf170c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
29 changes: 14 additions & 15 deletions APIJSONORM/src/main/java/apijson/orm/AbstractParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@
public abstract class AbstractParser<T> implements Parser<T>, ParserCreator<T>, VerifierCreator<T>, SQLCreator {
protected static final String TAG = "AbstractParser";

/**
* 打印大数据量日志的标识。线上环境比较敏感,可以通过切换该变量来控制异常栈抛出、错误日志打印。保守起见,该值默认为false。
* 与 {@link Log#DEBUG} 任何一个为 true 都会打印关键的接口请求及响应信息。
*/
public static boolean IS_PRINT_BIG_LOG = false;

/**
* method = null
Expand Down Expand Up @@ -301,9 +306,6 @@ public JSONObject parseResponse(String request) {

private int queryDepth;

// 打印异常日志的标识。线上环境比较敏感,可以通过切换该变量来控制异常栈抛出、错误日志打印。保守起见,该值默认为false。
public static boolean isPrintErrorLog = false;

/**解析请求json并获取对应结果
* @param request
* @return requestObject
Expand Down Expand Up @@ -386,30 +388,27 @@ public JSONObject parseResponse(JSONObject request) {
long endTime = System.currentTimeMillis();
long duration = endTime - startTime;

if (isPrintErrorLog) { //用 | 替代 /,避免 APIJSON ORM,APIAuto 等解析路径错误
if (Log.DEBUG) {
requestObject.put("sql:generate|cache|execute|maxExecute", getSQLExecutor().getGeneratedSQLCount() + "|" + getSQLExecutor().getCachedSQLCount() + "|" + getSQLExecutor().getExecutedSQLCount() + "|" + getMaxSQLCount());
requestObject.put("depth:count|max", queryDepth + "|" + getMaxQueryDepth());
requestObject.put("time:start|duration|end", startTime + "|" + duration + "|" + endTime);
if (error != null) {
Log.d(TAG, String.format("onObjectParse error, error is %s", error.getMessage()));
requestObject.put("throw", error.getClass().getName());
requestObject.put("trace", error.getStackTrace());
}
}

onClose();

//会不会导致原来的session = null? session = null;

if (isPrintErrorLog) {
Log.d(TAG, "\n\n\n\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n "
+ requestMethod + "/parseResponse request = \n" + requestString + "\n\n");

Log.d(TAG, "parseResponse return response = \n" + JSON.toJSONString(requestObject)
+ "\n >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> \n\n\n");
System.err.println("\n\n\n\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n "
+ TAG + ".DEBUG: " + requestMethod + "/parseResponse request = \n" + requestString + "\n\n");

if (Log.DEBUG || IS_PRINT_BIG_LOG || error != null) { // 日志仅存服务器,所以不太敏感,而且这些日志虽然量大但非常重要,对排查 bug 很关键
System.err.println(TAG + ".DEBUG: " + requestMethod + "/parseResponse return response = \n" + JSON.toJSONString(requestObject) + "\n\n");
}
Log.d(TAG, "parseResponse endTime = " + endTime + "; duration = " + duration
+ ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n\n");

System.err.println(TAG + ".DEBUG: " + requestMethod + "/parseResponse endTime = " + endTime + "; duration = " + duration
+ "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> \n\n\n");

return res;
}
Expand Down
7 changes: 5 additions & 2 deletions APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2018,9 +2018,12 @@ public String getSearchString(String key, Object[] values, int type) throws Ille
if (v instanceof String == false) {
throw new IllegalArgumentException(key + "$:value 中 value 的类型只能为 String 或 String[]!");
}
if (((String) v).contains("%%")) {
throw new IllegalArgumentException(key + "$:value 中 value 值 " + v + " 中包含 %% !不允许有连续的 % !");
if (((String) v).isEmpty()) { // 允许查空格 StringUtil.isEmpty((String) v, true)
throw new IllegalArgumentException(key + "$:value 中 value 值 " + v + "是空字符串,没有意义,不允许这样传!");
}
// if (((String) v).contains("%%")) { // 需要通过 %\%% 来模糊搜索 %
// throw new IllegalArgumentException(key + "$:value 中 value 值 " + v + " 中包含 %% !不允许有连续的 % !");
// }

condition += (i <= 0 ? "" : (Logic.isAnd(type) ? AND : OR)) + getLikeString(key, v);
}
Expand Down
5 changes: 2 additions & 3 deletions APIJSONORM/src/main/java/apijson/orm/AbstractSQLExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.Map.Entry;
import java.util.Set;

import apijson.orm.exception.NotExistException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

Expand Down Expand Up @@ -211,11 +210,11 @@ public JSONObject execute(@NotNull SQLConfig config, boolean unknowType) throws

int updateCount = executeUpdate(config);
if (updateCount <= 0) {
throw new NotExistException("没权限访问或对象不存在!");
throw new IllegalAccessException("没权限访问或对象不存在!"); // NotExistException 会被 catch 转为成功状态
}

// updateCount>0时收集结果。例如更新操作成功时,返回count(affected rows)、id字段
result = new JSONObject(true);
result = AbstractParser.newSuccessResult(); // TODO 对 APIAuto 及其它现有的前端/客户端影响比较大,暂时还是返回 code 和 msg,5.0 再移除 new JSONObject(true);

//id,id{}至少一个会有,一定会返回,不用抛异常来阻止关联写操作时前面错误导致后面无条件执行!
result.put(JSONResponse.KEY_COUNT, updateCount);//返回修改的记录数
Expand Down

0 comments on commit 8cf170c

Please sign in to comment.