Skip to content

Commit 8cf170c

Browse files
committed
AbstractSQLExecutor 优化增删改未成功也未抛异常的 code 和 msg;AbstractParser 优化请求及响应的日志打印;AbstractSQLConfig 优化 key$ 的格式校验
1 parent 1a75bfb commit 8cf170c

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

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

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
4848
public abstract class AbstractParser<T> implements Parser<T>, ParserCreator<T>, VerifierCreator<T>, SQLCreator {
4949
protected static final String TAG = "AbstractParser";
5050

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

5257
/**
5358
* method = null
@@ -301,9 +306,6 @@ public JSONObject parseResponse(String request) {
301306

302307
private int queryDepth;
303308

304-
// 打印异常日志的标识。线上环境比较敏感,可以通过切换该变量来控制异常栈抛出、错误日志打印。保守起见,该值默认为false。
305-
public static boolean isPrintErrorLog = false;
306-
307309
/**解析请求json并获取对应结果
308310
* @param request
309311
* @return requestObject
@@ -386,30 +388,27 @@ public JSONObject parseResponse(JSONObject request) {
386388
long endTime = System.currentTimeMillis();
387389
long duration = endTime - startTime;
388390

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

400401
onClose();
401402

402-
//会不会导致原来的session = null? session = null;
403-
404-
if (isPrintErrorLog) {
405-
Log.d(TAG, "\n\n\n\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n "
406-
+ requestMethod + "/parseResponse request = \n" + requestString + "\n\n");
407-
408-
Log.d(TAG, "parseResponse return response = \n" + JSON.toJSONString(requestObject)
409-
+ "\n >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> \n\n\n");
403+
System.err.println("\n\n\n\n<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n "
404+
+ TAG + ".DEBUG: " + requestMethod + "/parseResponse request = \n" + requestString + "\n\n");
405+
406+
if (Log.DEBUG || IS_PRINT_BIG_LOG || error != null) { // 日志仅存服务器,所以不太敏感,而且这些日志虽然量大但非常重要,对排查 bug 很关键
407+
System.err.println(TAG + ".DEBUG: " + requestMethod + "/parseResponse return response = \n" + JSON.toJSONString(requestObject) + "\n\n");
410408
}
411-
Log.d(TAG, "parseResponse endTime = " + endTime + "; duration = " + duration
412-
+ ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\n\n");
409+
410+
System.err.println(TAG + ".DEBUG: " + requestMethod + "/parseResponse endTime = " + endTime + "; duration = " + duration
411+
+ "\n>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> \n\n\n");
413412

414413
return res;
415414
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2018,9 +2018,12 @@ public String getSearchString(String key, Object[] values, int type) throws Ille
20182018
if (v instanceof String == false) {
20192019
throw new IllegalArgumentException(key + "$:value 中 value 的类型只能为 String 或 String[]!");
20202020
}
2021-
if (((String) v).contains("%%")) {
2022-
throw new IllegalArgumentException(key + "$:value 中 value 值 " + v + " 中包含 %% !不允许有连续的 % !");
2021+
if (((String) v).isEmpty()) { // 允许查空格 StringUtil.isEmpty((String) v, true)
2022+
throw new IllegalArgumentException(key + "$:value 中 value 值 " + v + "是空字符串,没有意义,不允许这样传!");
20232023
}
2024+
// if (((String) v).contains("%%")) { // 需要通过 %\%% 来模糊搜索 %
2025+
// throw new IllegalArgumentException(key + "$:value 中 value 值 " + v + " 中包含 %% !不允许有连续的 % !");
2026+
// }
20242027

20252028
condition += (i <= 0 ? "" : (Logic.isAnd(type) ? AND : OR)) + getLikeString(key, v);
20262029
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import java.util.Map.Entry;
2828
import java.util.Set;
2929

30-
import apijson.orm.exception.NotExistException;
3130
import com.alibaba.fastjson.JSON;
3231
import com.alibaba.fastjson.JSONObject;
3332

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

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

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

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

0 commit comments

Comments
 (0)