Skip to content

Commit 5a67559

Browse files
committed
Server:解决自动化校验 UNIQUE 失效
1 parent d8cfc9e commit 5a67559

File tree

1 file changed

+24
-19
lines changed
  • APIJSON-Java-Server/APIJSONLibrary/src/main/java/zuo/biao/apijson/server

1 file changed

+24
-19
lines changed

APIJSON-Java-Server/APIJSONLibrary/src/main/java/zuo/biao/apijson/server/Structure.java

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import zuo.biao.apijson.Log;
4848
import zuo.biao.apijson.RequestMethod;
4949
import zuo.biao.apijson.StringUtil;
50+
import zuo.biao.apijson.server.exception.ConflictException;
5051
import zuo.biao.apijson.server.model.Test;
5152

5253
/**结构类
@@ -369,7 +370,7 @@ public static JSONObject parse(String name, JSONObject target, JSONObject real
369370
if (uniques != null && uniques.length > 0) {
370371
long exceptId = real.getLongValue(KEY_ID);
371372
for (String u : uniques) {
372-
verifyRepeat(name, u, real.get(u), exceptId);
373+
verifyRepeat(name, u, real.get(u), exceptId, creator);
373374
}
374375
}
375376
//校验重复>>>>>>>>>>>>>>>>>>>
@@ -655,8 +656,6 @@ private static void sqlVerify(@NotNull String funChar, @NotNull JSONObject real,
655656
JSONObject result = null;
656657
try {
657658
result = executor.execute(config);
658-
} catch (Exception e) {
659-
throw e;
660659
} finally {
661660
executor.close();
662661
}
@@ -672,8 +671,8 @@ private static void sqlVerify(@NotNull String funChar, @NotNull JSONObject real,
672671
* @param value
673672
* @throws Exception
674673
*/
675-
public static void verifyRepeat(String table, String key, Object value) throws Exception {
676-
verifyRepeat(table, key, value, 0);
674+
public static void verifyRepeat(String table, String key, Object value, @NotNull SQLCreator creator) throws Exception {
675+
verifyRepeat(table, key, value, 0, creator);
677676
}
678677
/**验证是否重复
679678
* @param table
@@ -682,29 +681,35 @@ public static void verifyRepeat(String table, String key, Object value) throws E
682681
* @param exceptId 不包含id
683682
* @throws Exception
684683
*/
685-
public static void verifyRepeat(String table, String key, Object value, long exceptId) throws Exception {
684+
public static void verifyRepeat(String table, String key, Object value, long exceptId, @NotNull SQLCreator creator) throws Exception {
686685
if (key == null || value == null) {
687686
Log.e(TAG, "verifyRepeat key == null || value == null >> return;");
688687
return;
689688
}
690689
if (value instanceof JSON) {
691690
throw new UnsupportedDataTypeException(key + ":value 中value的类型不能为JSON!");
692691
}
693-
694-
JSONRequest request = new JSONRequest(key, value);
692+
693+
694+
SQLConfig config = creator.createSQLConfig().setMethod(RequestMethod.HEAD).setCount(1).setPage(0);
695+
config.setTable(table);
695696
if (exceptId > 0) {//允许修改自己的属性为该属性原来的值
696-
request.put(JSONRequest.KEY_ID + "!", exceptId);
697+
config.putWhere(JSONRequest.KEY_ID + "!", exceptId, false);
698+
}
699+
config.putWhere(key, value, false);
700+
701+
SQLExecutor executor = creator.createSQLExecutor();
702+
try {
703+
JSONObject result = executor.execute(config);
704+
if (result == null) {
705+
throw new Exception("服务器内部错误 verifyRepeat result == null");
706+
}
707+
if (result.getIntValue(JSONResponse.KEY_COUNT) > 0) {
708+
throw new ConflictException(key + ": " + value + " 已经存在,不能重复!");
709+
}
710+
} finally {
711+
executor.close();
697712
}
698-
// JSONObject repeat = new AbstractParser(HEAD, true).parseResponse(
699-
// new JSONRequest(table, request)
700-
// );
701-
// repeat = repeat == null ? null : repeat.getJSONObject(table);
702-
// if (repeat == null) {
703-
// throw new Exception("服务器内部错误 verifyRepeat repeat == null");
704-
// }
705-
// if (repeat.getIntValue(JSONResponse.KEY_COUNT) > 0) {
706-
// throw new ConflictException(key + ": " + value + " 已经存在,不能重复!");
707-
// }
708713
}
709714

710715

0 commit comments

Comments
 (0)