Skip to content

Commit a94fb96

Browse files
committed
优化代码及注释的格式;完善用异常转换状态码 code
1 parent e0d9a7c commit a94fb96

File tree

4 files changed

+726
-643
lines changed

4 files changed

+726
-643
lines changed

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

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -196,48 +196,48 @@ public AbstractObjectParser parse(String name, boolean isReuse) throws Exception
196196

197197
breakParse = false;
198198

199-
response = new JSONObject(true);//must init
200-
sqlResponse = null;//must init
199+
response = new JSONObject(true); // must init
200+
sqlResponse = null; // must init
201201

202202
if (isReuse == false) {
203-
sqlRequest = new JSONObject(true);//must init
203+
sqlRequest = new JSONObject(true); // must init
204204

205-
customMap = null;//must init
206-
functionMap = null;//must init
207-
childMap = null;//must init
205+
customMap = null; // must init
206+
functionMap = null; // must init
207+
childMap = null; // must init
208208

209209
Set<Entry<String, Object>> set = request.isEmpty() ? null : new LinkedHashSet<>(request.entrySet());
210-
if (set != null && set.isEmpty() == false) {//判断换取少几个变量的初始化是否值得?
211-
if (isTable) {//非Table下必须保证原有顺序!否则 count,page 会丢, total@:"/[]/total" 会在[]:{}前执行!
210+
if (set != null && set.isEmpty() == false) { // 判断换取少几个变量的初始化是否值得?
211+
if (isTable) { // 非Table下必须保证原有顺序!否则 count,page 会丢, total@:"/[]/total" 会在[]:{}前执行!
212212
customMap = new LinkedHashMap<String, Object>();
213213
childMap = new LinkedHashMap<String, JSONObject>();
214214
}
215215
functionMap = new LinkedHashMap<String, Map<String, String>>();//必须执行
216216

217-
//条件<<<<<<<<<<<<<<<<<<<
217+
// 条件 <<<<<<<<<<<<<<<<<<<
218218
List<String> whereList = null;
219-
if (method == PUT) { //这里只有PUTArray需要处理 || method == DELETE) {
219+
if (method == PUT) { // 这里只有PUTArray需要处理 || method == DELETE) {
220220
String[] combine = StringUtil.split(request.getString(KEY_COMBINE));
221221
if (combine != null) {
222222
String w;
223-
for (int i = 0; i < combine.length; i++) { //去除 &,|,! 前缀
223+
for (int i = 0; i < combine.length; i++) { // 去除 &,|,! 前缀
224224
w = combine[i];
225225
if (w != null && (w.startsWith("&") || w.startsWith("|") || w.startsWith("!"))) {
226226
combine[i] = w.substring(1);
227227
}
228228
}
229229
}
230-
//Arrays.asList()返回值不支持add方法
230+
// Arrays.asList() 返回值不支持 add 方法
231231
whereList = new ArrayList<String>(Arrays.asList(combine != null ? combine : new String[]{}));
232232
whereList.add(apijson.JSONRequest.KEY_ID);
233233
whereList.add(apijson.JSONRequest.KEY_ID_IN);
234234
// whereList.add(apijson.JSONRequest.KEY_USER_ID);
235235
// whereList.add(apijson.JSONRequest.KEY_USER_ID_IN);
236236
}
237-
//条件>>>>>>>>>>>>>>>>>>>
237+
// 条件>>>>>>>>>>>>>>>>>>>
238238

239239
int index = 0;
240-
//hasOtherKeyNotFun = false;
240+
// hasOtherKeyNotFun = false;
241241

242242
for (Entry<String, Object> entry : set) {
243243
if (isBreakParse()) {
@@ -261,9 +261,9 @@ public AbstractObjectParser parse(String name, boolean isReuse) throws Exception
261261

262262
try {
263263
boolean startsWithAt = key.startsWith("@");
264-
//if (startsWithAt || (key.endsWith("()") == false)) {
265-
// hasOtherKeyNotFun = true;
266-
//}
264+
// if (startsWithAt || (key.endsWith("()") == false)) {
265+
// hasOtherKeyNotFun = true;
266+
// }
267267

268268
if (startsWithAt || key.endsWith("@") || (key.endsWith("<>") && value instanceof JSONObject)) {
269269
if (onParse(key, value) == false) {
@@ -287,7 +287,7 @@ else if (_method == PUT && value instanceof JSONArray && (whereList == null || w
287287
&& StringUtil.isName(key.replaceFirst("[+-]$", ""))) { // PUT JSONArray
288288
onPUTArrayParse(key, (JSONArray) value);
289289
}
290-
else { // JSONArray或其它Object,直接填充
290+
else { // JSONArray 或其它 Object,直接填充
291291
if (onParse(key, value) == false) {
292292
invalidate();
293293
}
@@ -313,7 +313,7 @@ else if (_method == PUT && value instanceof JSONArray && (whereList == null || w
313313
sqlRequest.put(JSONRequest.KEY_DATASOURCE, parser.getGlobalDatasource());
314314
}
315315

316-
if (isSubquery == false) { //解决 SQL 语法报错,子查询不能 EXPLAIN
316+
if (isSubquery == false) { // 解决 SQL 语法报错,子查询不能 EXPLAIN
317317
if (parser.getGlobalExplain() != null && sqlRequest.get(JSONRequest.KEY_EXPLAIN) == null) {
318318
sqlRequest.put(JSONRequest.KEY_EXPLAIN, parser.getGlobalExplain());
319319
}
@@ -363,7 +363,6 @@ public boolean onParse(@NotNull String key, @NotNull Object value) throws Except
363363
+ JSONRequest.SUBQUERY_RANGE_ALL + ", " + JSONRequest.SUBQUERY_RANGE_ANY + "] 中的一个!");
364364
}
365365

366-
367366
JSONArray arr = parser.onArrayParse(subquery, path, key, true);
368367

369368
JSONObject obj = arr == null || arr.isEmpty() ? null : arr.getJSONObject(0);
@@ -391,7 +390,7 @@ public boolean onParse(@NotNull String key, @NotNull Object value) throws Except
391390
throw new IllegalArgumentException("子查询 " + path + "/"
392391
+ key + ":{ from:value } 中 value 对应的主表对象 " + from + ":{} 不存在!");
393392
}
394-
//
393+
395394
SQLConfig cfg = (SQLConfig) arrObj.get(AbstractParser.KEY_CONFIG);
396395
if (cfg == null) {
397396
throw new NotExistException(TAG + ".onParse cfg == null");
@@ -415,38 +414,41 @@ public boolean onParse(@NotNull String key, @NotNull Object value) throws Except
415414
else if (value instanceof String) { // //key{}@ getRealKey, 引用赋值路径
416415
String replaceKey = key.substring(0, key.length() - 1);
417416

418-
// System.out.println("getObject key.endsWith(@) >> parseRelation = " + parseRelation);
417+
// System.out.println("getObject key.endsWith(@) >> parseRelation = " + parseRelation);
419418
String targetPath = AbstractParser.getValuePath(type == TYPE_ITEM ? path : parentPath, (String) value);
420419

421-
//先尝试获取,尽量保留缺省依赖路径,这样就不需要担心路径改变
420+
// 先尝试获取,尽量保留缺省依赖路径,这样就不需要担心路径改变
422421
Object target = onReferenceParse(targetPath);
423422
Log.i(TAG, "onParse targetPath = " + targetPath + "; target = " + target);
424423

425-
if (target == null) {//String#equals(null)会出错
424+
if (target == null) { // String#equals(null)会出错
426425
Log.d(TAG, "onParse target == null >> return true;");
427426
return true;
428427
}
429-
if (target instanceof Map) { //target可能是从requestObject里取出的 {}
430-
if (isTable || targetPath.endsWith("[]/" + JSONResponse.KEY_INFO) == false) {
431-
Log.d(TAG, "onParse target instanceof Map >> return false;");
432-
return false; //FIXME 这个判断现在来看是否还有必要?为啥不允许为 JSONObject ?以前可能因为防止二次遍历再解析,现在只有一次遍历
433-
}
434-
}
435-
if (targetPath.equals(target)) {//必须valuePath和保证getValueByPath传进去的一致!
436-
Log.d(TAG, "onParse targetPath.equals(target) >>");
437-
438-
//非查询关键词 @key 不影响查询,直接跳过
439-
if (isTable && (key.startsWith("@") == false || JSONRequest.TABLE_KEY_LIST.contains(key))) {
440-
Log.e(TAG, "onParse isTable && (key.startsWith(@) == false"
441-
+ " || JSONRequest.TABLE_KEY_LIST.contains(key)) >> return null;");
442-
return false;//获取不到就不用再做无效的query了。不考虑 Table:{Table:{}}嵌套
443-
} else {
444-
Log.d(TAG, "onParse isTable(table) == false >> return true;");
445-
return true;//舍去,对Table无影响
446-
}
447-
}
448428

449-
//直接替换原来的key@:path为key:target
429+
// if (target instanceof Map) { // target 可能是从 requestObject 里取出的 {}
430+
// if (isTable || targetPath.endsWith("[]/" + JSONResponse.KEY_INFO) == false) {
431+
// Log.d(TAG, "onParse target instanceof Map >> return false;");
432+
// return false; // FIXME 这个判断现在来看是否还有必要?为啥不允许为 JSONObject ?以前可能因为防止二次遍历再解析,现在只有一次遍历
433+
// }
434+
// }
435+
//
436+
// // FIXME 这个判断现在来看是否还有必要?为啥不允许为 JSONObject ?以前可能因为防止二次遍历再解析,现在只有一次遍历
437+
// if (targetPath.equals(target)) { // 必须 valuePath 和保证 getValueByPath 传进去的一致!
438+
// Log.d(TAG, "onParse targetPath.equals(target) >>");
439+
//
440+
// //非查询关键词 @key 不影响查询,直接跳过
441+
// if (isTable && (key.startsWith("@") == false || JSONRequest.TABLE_KEY_LIST.contains(key))) {
442+
// Log.e(TAG, "onParse isTable && (key.startsWith(@) == false"
443+
// + " || JSONRequest.TABLE_KEY_LIST.contains(key)) >> return null;");
444+
// return false;//获取不到就不用再做无效的query了。不考虑 Table:{Table:{}}嵌套
445+
// } else {
446+
// Log.d(TAG, "onParse isTable(table) == false >> return true;");
447+
// return true;//舍去,对Table无影响
448+
// }
449+
// }
450+
451+
// 直接替换原来的 key@: path 为 key: target
450452
Log.i(TAG, "onParse >> key = replaceKey; value = target;");
451453
key = replaceKey;
452454
value = target;

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -863,11 +863,11 @@ public static JSONObject newErrorResult(Exception e) {
863863
*/
864864
public static JSONObject newErrorResult(Exception e, boolean isRoot) {
865865
if (e != null) {
866-
// if (Log.DEBUG) {
867-
e.printStackTrace();
868-
// }
866+
// if (Log.DEBUG) {
867+
e.printStackTrace();
868+
// }
869869

870-
String msg = CommonException.getMsg(e);
870+
String msg = CommonException.getMsg(e);
871871
Integer code = CommonException.getCode(e);
872872

873873
return newResult(code, msg, isRoot);
@@ -1978,7 +1978,7 @@ public void setTransactionIsolation(int transactionIsolation) {
19781978
@Override
19791979
public void begin(int transactionIsolation) {
19801980
Log.d("\n\n" + TAG, "<<<<<<<<<<<<<<<<<<<<<<< begin transactionIsolation = " + transactionIsolation + " >>>>>>>>>>>>>>>>>>>>>>> \n\n");
1981-
getSQLExecutor().setTransactionIsolation(transactionIsolation); //不知道 connection 什么时候创建,不能在这里准确控制,getSqlExecutor().begin(transactionIsolation);
1981+
getSQLExecutor().setTransactionIsolation(transactionIsolation); // 不知道 connection 什么时候创建,不能在这里准确控制,getSqlExecutor().begin(transactionIsolation);
19821982
}
19831983
@Override
19841984
public void rollback() throws SQLException {
@@ -2016,7 +2016,8 @@ protected void onBegin() {
20162016
protected void onCommit() {
20172017
// Log.d(TAG, "onCommit >>");
20182018
// this.sqlExecutor.getTransactionIsolation() 只有json第一次执行才会设置, get请求=0
2019-
if (RequestMethod.isQueryMethod(requestMethod) && this.sqlExecutor.getTransactionIsolation() == Connection.TRANSACTION_NONE ) {
2019+
if (RequestMethod.isQueryMethod(requestMethod)
2020+
&& getSQLExecutor().getTransactionIsolation() == Connection.TRANSACTION_NONE) {
20202021
return;
20212022
}
20222023

@@ -2065,7 +2066,7 @@ protected void onClose() {
20652066
private void setOpMethod(JSONObject request, ObjectParser op, String key) {
20662067
String _method = key == null ? null : request.getString(apijson.JSONObject.KEY_METHOD);
20672068
if (_method != null) {
2068-
RequestMethod method = RequestMethod.valueOf(_method.toUpperCase());
2069+
RequestMethod method = RequestMethod.valueOf(_method); // 必须精准匹配,避免缓存命中率低
20692070
this.setMethod(method);
20702071
op.setMethod(method);
20712072
}

0 commit comments

Comments
 (0)