Skip to content

Commit ed00c1c

Browse files
committed
支持了验证是否存在联合校验、验证是否不存在联合校验
1 parent 0d45232 commit ed00c1c

File tree

2 files changed

+54
-10
lines changed

2 files changed

+54
-10
lines changed

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

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1113,24 +1113,28 @@ public static <T extends Object> JSONObject parse(@NotNull final RequestMethod m
11131113
String finalIdKey = StringUtil.isEmpty(idKey, false) ? apijson.JSONObject.KEY_ID : idKey;
11141114

11151115
// TODO 放在operate前?考虑性能、operate修改后再验证的值是否和原来一样
1116-
// 校验存在<<<<<<<<<<<<<<<<<<< TODO 格式改为 id;version,tag 兼容多个字段联合主键
1116+
// 校验存在<<<<<<<<<<<<<<<<<<<
11171117
String[] exists = StringUtil.split(exist);
11181118
if (exists != null && exists.length > 0) {
11191119
long exceptId = real.getLongValue(finalIdKey);
1120+
Map<String,Object> map = new HashMap<>();
11201121
for (String e : exists) {
1121-
verifyExist(name, e, real.get(e), exceptId, creator);
1122+
map.put(e,real.get(e));
11221123
}
1124+
verifyExist(name, map, exceptId, creator);
11231125
}
11241126
// 校验存在>>>>>>>>>>>>>>>>>>>
11251127

11261128
// TODO 放在operate前?考虑性能、operate修改后再验证的值是否和原来一样
1127-
// 校验重复<<<<<<<<<<<<<<<<<<< TODO 格式改为 id;version,tag 兼容多个字段联合主键
1129+
// 校验重复<<<<<<<<<<<<<<<<<<<
11281130
String[] uniques = StringUtil.split(unique);
11291131
if (uniques != null && uniques.length > 0) {
11301132
long exceptId = real.getLongValue(finalIdKey);
1133+
Map<String,Object> map = new HashMap<>();
11311134
for (String u : uniques) {
1132-
verifyRepeat(name, u, real.get(u), exceptId, finalIdKey, creator);
1135+
map.put(u,real.get(u));
11331136
}
1137+
verifyRepeat(name, map, exceptId, finalIdKey, creator);
11341138
}
11351139
// 校验重复>>>>>>>>>>>>>>>>>>>
11361140

@@ -1595,11 +1599,25 @@ public static void verifyExist(String table, String key, Object value, long exce
15951599
if (value instanceof JSON) {
15961600
throw new UnsupportedDataTypeException(key + ":value 中value的类型不能为JSON!");
15971601
}
1602+
Map<String,Object> map = new HashMap<>();
1603+
map.put(key,value);
1604+
verifyExist(table,map,exceptId,creator);
1605+
}
15981606

1607+
/**验证是否存在
1608+
* @param table
1609+
* @param param
1610+
* @throws Exception
1611+
*/
1612+
public static void verifyExist(String table, Map<String,Object> param, long exceptId, @NotNull SQLCreator creator) throws Exception {
1613+
if (param.isEmpty()) {
1614+
Log.e(TAG, "verifyExist is empty >> return;");
1615+
return;
1616+
}
15991617

16001618
SQLConfig config = creator.createSQLConfig().setMethod(RequestMethod.HEAD).setCount(1).setPage(0);
16011619
config.setTable(table);
1602-
config.putWhere(key, value, false);
1620+
param.forEach((key,value) -> config.putWhere(key, value, false));
16031621

16041622
SQLExecutor executor = creator.createSQLExecutor();
16051623
try {
@@ -1608,7 +1626,9 @@ public static void verifyExist(String table, String key, Object value, long exce
16081626
throw new Exception("服务器内部错误 verifyExist result == null");
16091627
}
16101628
if (result.getIntValue(JSONResponse.KEY_COUNT) <= 0) {
1611-
throw new ConflictException(key + ": " + value + " 不存在!如果必要请先创建!");
1629+
StringBuilder sb = new StringBuilder();
1630+
param.forEach((key,value) -> sb.append("key:").append(key).append(" value:").append(value).append(" "));
1631+
throw new ConflictException(sb + "的数据不存在!如果必要请先创建!");
16121632
}
16131633
} finally {
16141634
executor.close();
@@ -1655,6 +1675,25 @@ public static void verifyRepeat(String table, String key, Object value
16551675
if (value instanceof JSON) {
16561676
throw new UnsupportedDataTypeException(key + ":value 中value的类型不能为JSON!");
16571677
}
1678+
Map<String,Object> map = new HashMap<>();
1679+
map.put(key,value);
1680+
verifyRepeat(table,map,exceptId,idKey,creator);
1681+
}
1682+
1683+
/**验证是否重复
1684+
* TODO 与 AbstractVerifier.verifyRepeat 代码重复,需要简化
1685+
* @param table
1686+
* @param param
1687+
* @param exceptId 不包含id
1688+
* @param idKey
1689+
* @param creator
1690+
* @throws Exception
1691+
*/
1692+
public static void verifyRepeat(String table, Map<String,Object> param, long exceptId, String idKey, @NotNull SQLCreator creator) throws Exception {
1693+
if (param.isEmpty()) {
1694+
Log.e(TAG, "verifyRepeat is empty >> return;");
1695+
return;
1696+
}
16581697

16591698
String finalIdKey = StringUtil.isEmpty(idKey, false) ? apijson.JSONObject.KEY_ID : idKey;
16601699

@@ -1663,7 +1702,7 @@ public static void verifyRepeat(String table, String key, Object value
16631702
if (exceptId > 0) { //允许修改自己的属性为该属性原来的值
16641703
config.putWhere(finalIdKey + "!", exceptId, false);
16651704
}
1666-
config.putWhere(key, value, false);
1705+
param.forEach((key,value) -> config.putWhere(key,value, false));
16671706

16681707
SQLExecutor executor = creator.createSQLExecutor();
16691708
try {
@@ -1672,13 +1711,16 @@ public static void verifyRepeat(String table, String key, Object value
16721711
throw new Exception("服务器内部错误 verifyRepeat result == null");
16731712
}
16741713
if (result.getIntValue(JSONResponse.KEY_COUNT) > 0) {
1675-
throw new ConflictException(key + ": " + value + " 已经存在,不能重复!");
1714+
StringBuilder sb = new StringBuilder();
1715+
param.forEach((key,value) -> sb.append("key:").append(key).append(" value:").append(value).append(" "));
1716+
throw new ConflictException(sb + "的数据已经存在,不能重复!");
16761717
}
16771718
} finally {
16781719
executor.close();
16791720
}
16801721
}
16811722

1723+
16821724
public static String getCacheKeyForRequest(String method, String tag) {
16831725
return method + "/" + tag;
16841726
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,17 @@ public enum Operation {
6060
*/
6161
VERIFY,
6262

63-
/**TODO 格式改为 id;version,tag 兼容多个字段联合主键。 ["id", "version,tag"] 也行
63+
/**
6464
* 验证是否存在,结构是
6565
* "key0,key1,key2..."
66+
* 多个字段用逗号隔开,联合校验
6667
*/
6768
EXIST,
6869

69-
/**TODO 格式改为 id;version,tag 兼容多个字段联合主键。 ["id", "version,tag"] 也行
70+
/**
7071
* 验证是否不存在,除了本身的记录,结构是
7172
* "key0,key1,key2..."
73+
* 多个字段用逗号隔开,联合校验
7274
*/
7375
UNIQUE,
7476

0 commit comments

Comments
 (0)