17
17
import static apijson .JSONObject .KEY_ID ;
18
18
import static apijson .JSONObject .KEY_USER_ID ;
19
19
import static apijson .orm .Operation .DISALLOW ;
20
+ import static apijson .orm .Operation .EXIST ;
20
21
import static apijson .orm .Operation .INSERT ;
21
22
import static apijson .orm .Operation .NECESSARY ;
22
23
import static apijson .orm .Operation .REMOVE ;
@@ -257,6 +258,7 @@ public static JSONObject parse(@NotNull final RequestMethod method, String name,
257
258
JSONObject update = target .getJSONObject (UPDATE .name ());
258
259
JSONObject replace = target .getJSONObject (REPLACE .name ());
259
260
261
+ String exist = StringUtil .getNoBlankString (target .getString (EXIST .name ()));
260
262
String unique = StringUtil .getNoBlankString (target .getString (UNIQUE .name ()));
261
263
String remove = StringUtil .getNoBlankString (target .getString (REMOVE .name ()));
262
264
String necessary = StringUtil .getNoBlankString (target .getString (NECESSARY .name ()));
@@ -268,6 +270,7 @@ public static JSONObject parse(@NotNull final RequestMethod method, String name,
268
270
target .remove (UPDATE .name ());
269
271
target .remove (REPLACE .name ());
270
272
273
+ target .remove (EXIST .name ());
271
274
target .remove (UNIQUE .name ());
272
275
target .remove (REMOVE .name ());
273
276
target .remove (NECESSARY .name ());
@@ -411,7 +414,18 @@ public static JSONObject parse(@NotNull final RequestMethod method, String name,
411
414
//校验与修改Request>>>>>>>>>>>>>>>>>
412
415
413
416
//TODO放在operate前?考虑性能、operate修改后再验证的值是否和原来一样
414
- //校验重复<<<<<<<<<<<<<<<<<<<
417
+ //校验存在<<<<<<<<<<<<<<<<<<< TODO 格式改为 id;version,tag 兼容多个字段联合主键
418
+ String [] exists = StringUtil .split (exist );
419
+ if (exists != null && exists .length > 0 ) {
420
+ long exceptId = real .getLongValue (KEY_ID );
421
+ for (String e : exists ) {
422
+ verifyExist (name , e , real .get (e ), exceptId , creator );
423
+ }
424
+ }
425
+ //校验存在>>>>>>>>>>>>>>>>>>>
426
+
427
+ //TODO放在operate前?考虑性能、operate修改后再验证的值是否和原来一样
428
+ //校验重复<<<<<<<<<<<<<<<<<<< TODO 格式改为 id;version,tag 兼容多个字段联合主键
415
429
String [] uniques = StringUtil .split (unique );
416
430
if (uniques != null && uniques .length > 0 ) {
417
431
long exceptId = real .getLongValue (KEY_ID );
@@ -429,6 +443,7 @@ public static JSONObject parse(@NotNull final RequestMethod method, String name,
429
443
target .put (UPDATE .name (), update );
430
444
target .put (REPLACE .name (), replace );
431
445
446
+ target .put (EXIST .name (), exist );
432
447
target .put (UNIQUE .name (), unique );
433
448
target .put (REMOVE .name (), remove );
434
449
target .put (NECESSARY .name (), necessary );
@@ -779,6 +794,40 @@ private static void sqlVerify(@NotNull String funChar, @NotNull JSONObject real,
779
794
}
780
795
}
781
796
797
+
798
+ /**验证是否存在
799
+ * @param table
800
+ * @param key
801
+ * @param value
802
+ * @throws Exception
803
+ */
804
+ public static void verifyExist (String table , String key , Object value , @ NotNull SQLCreator creator ) throws Exception {
805
+ if (key == null || value == null ) {
806
+ Log .e (TAG , "verifyExist key == null || value == null >> return;" );
807
+ return ;
808
+ }
809
+ if (value instanceof JSON ) {
810
+ throw new UnsupportedDataTypeException (key + ":value 中value的类型不能为JSON!" );
811
+ }
812
+
813
+
814
+ SQLConfig config = creator .createSQLConfig ().setMethod (RequestMethod .HEAD ).setCount (1 ).setPage (0 );
815
+ config .setTable (table );
816
+ config .putWhere (key , value , false );
817
+
818
+ SQLExecutor executor = creator .createSQLExecutor ();
819
+ try {
820
+ JSONObject result = executor .execute (config , false );
821
+ if (result == null ) {
822
+ throw new Exception ("服务器内部错误 verifyExist result == null" );
823
+ }
824
+ if (result .getIntValue (JSONResponse .KEY_COUNT ) <= 0 ) {
825
+ throw new ConflictException (key + ": " + value + " 不存在!如果必要请先创建!" );
826
+ }
827
+ } finally {
828
+ executor .close ();
829
+ }
830
+ }
782
831
783
832
/**验证是否重复
784
833
* @param table
@@ -789,6 +838,7 @@ private static void sqlVerify(@NotNull String funChar, @NotNull JSONObject real,
789
838
public static void verifyRepeat (String table , String key , Object value , @ NotNull SQLCreator creator ) throws Exception {
790
839
verifyRepeat (table , key , value , 0 , creator );
791
840
}
841
+
792
842
/**验证是否重复
793
843
* @param table
794
844
* @param key
@@ -804,15 +854,15 @@ public static void verifyRepeat(String table, String key, Object value, long exc
804
854
if (value instanceof JSON ) {
805
855
throw new UnsupportedDataTypeException (key + ":value 中value的类型不能为JSON!" );
806
856
}
807
-
808
-
857
+
858
+
809
859
SQLConfig config = creator .createSQLConfig ().setMethod (RequestMethod .HEAD ).setCount (1 ).setPage (0 );
810
860
config .setTable (table );
811
861
if (exceptId > 0 ) {//允许修改自己的属性为该属性原来的值
812
862
config .putWhere (JSONRequest .KEY_ID + "!" , exceptId , false );
813
863
}
814
864
config .putWhere (key , value , false );
815
-
865
+
816
866
SQLExecutor executor = creator .createSQLExecutor ();
817
867
try {
818
868
JSONObject result = executor .execute (config , false );
0 commit comments