1717import static apijson .JSONObject .KEY_ID ;
1818import static apijson .JSONObject .KEY_USER_ID ;
1919import static apijson .orm .Operation .DISALLOW ;
20+ import static apijson .orm .Operation .EXIST ;
2021import static apijson .orm .Operation .INSERT ;
2122import static apijson .orm .Operation .NECESSARY ;
2223import static apijson .orm .Operation .REMOVE ;
@@ -257,6 +258,7 @@ public static JSONObject parse(@NotNull final RequestMethod method, String name,
257258 JSONObject update = target .getJSONObject (UPDATE .name ());
258259 JSONObject replace = target .getJSONObject (REPLACE .name ());
259260
261+ String exist = StringUtil .getNoBlankString (target .getString (EXIST .name ()));
260262 String unique = StringUtil .getNoBlankString (target .getString (UNIQUE .name ()));
261263 String remove = StringUtil .getNoBlankString (target .getString (REMOVE .name ()));
262264 String necessary = StringUtil .getNoBlankString (target .getString (NECESSARY .name ()));
@@ -268,6 +270,7 @@ public static JSONObject parse(@NotNull final RequestMethod method, String name,
268270 target .remove (UPDATE .name ());
269271 target .remove (REPLACE .name ());
270272
273+ target .remove (EXIST .name ());
271274 target .remove (UNIQUE .name ());
272275 target .remove (REMOVE .name ());
273276 target .remove (NECESSARY .name ());
@@ -411,7 +414,18 @@ public static JSONObject parse(@NotNull final RequestMethod method, String name,
411414 //校验与修改Request>>>>>>>>>>>>>>>>>
412415
413416 //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 兼容多个字段联合主键
415429 String [] uniques = StringUtil .split (unique );
416430 if (uniques != null && uniques .length > 0 ) {
417431 long exceptId = real .getLongValue (KEY_ID );
@@ -429,6 +443,7 @@ public static JSONObject parse(@NotNull final RequestMethod method, String name,
429443 target .put (UPDATE .name (), update );
430444 target .put (REPLACE .name (), replace );
431445
446+ target .put (EXIST .name (), exist );
432447 target .put (UNIQUE .name (), unique );
433448 target .put (REMOVE .name (), remove );
434449 target .put (NECESSARY .name (), necessary );
@@ -779,6 +794,40 @@ private static void sqlVerify(@NotNull String funChar, @NotNull JSONObject real,
779794 }
780795 }
781796
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+ }
782831
783832 /**验证是否重复
784833 * @param table
@@ -789,6 +838,7 @@ private static void sqlVerify(@NotNull String funChar, @NotNull JSONObject real,
789838 public static void verifyRepeat (String table , String key , Object value , @ NotNull SQLCreator creator ) throws Exception {
790839 verifyRepeat (table , key , value , 0 , creator );
791840 }
841+
792842 /**验证是否重复
793843 * @param table
794844 * @param key
@@ -804,15 +854,15 @@ public static void verifyRepeat(String table, String key, Object value, long exc
804854 if (value instanceof JSON ) {
805855 throw new UnsupportedDataTypeException (key + ":value 中value的类型不能为JSON!" );
806856 }
807-
808-
857+
858+
809859 SQLConfig config = creator .createSQLConfig ().setMethod (RequestMethod .HEAD ).setCount (1 ).setPage (0 );
810860 config .setTable (table );
811861 if (exceptId > 0 ) {//允许修改自己的属性为该属性原来的值
812862 config .putWhere (JSONRequest .KEY_ID + "!" , exceptId , false );
813863 }
814864 config .putWhere (key , value , false );
815-
865+
816866 SQLExecutor executor = creator .createSQLExecutor ();
817867 try {
818868 JSONObject result = executor .execute (config , false );
0 commit comments