Skip to content

Commit 3d7936e

Browse files
committed
Server:同步eclipse版至idea版
1 parent 7d0a866 commit 3d7936e

5 files changed

Lines changed: 70 additions & 31 deletions

File tree

APIJSON(Server)/APIJSON(Eclipse_JEE)/src/main/java/zuo/biao/apijson/server/QueryConfig.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,9 +260,10 @@ public static String getWhereString(RequestMethod method, Map<String, Object> wh
260260
}
261261
return "";
262262
}
263-
/**where key in ('key0', 'key1', ... )
264-
* @param in
265-
* @return in ('key0', 'key1', ... )
263+
/**where key > 'key0' and key <= 'key1' and ...
264+
* @param key
265+
* @param range "condition0,condition1..."
266+
* @return key > 'key0' and key <= 'key1' and ...
266267
*/
267268
public static String getRangeString(String key, Object range) {
268269
if (range instanceof JSONArray) {

APIJSON(Server)/APIJSON(Idea)/src/main/java/zuo/biao/apijson/server/APIJSONApplication.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public class APIJSONApplication {
2626
public static void main(String[] args) {
2727
SpringApplication.run(APIJSONApplication.class, args);
2828

29-
Function.test();
29+
try {
30+
Function.test();
31+
} catch (Exception e) {
32+
e.printStackTrace();
33+
}
3034
}
3135
}

APIJSON(Server)/APIJSON(Idea)/src/main/java/zuo/biao/apijson/server/Function.java

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public class Function {
1818
private static final String TAG = "Function";
1919

2020

21-
public static void test() {
21+
public static void test() throws Exception {
2222
int i0 = 1, i1 = -2;
2323
Map<String, Object> jsonMap = new HashMap<String, Object>();
2424
jsonMap.put("id", 10);
@@ -36,10 +36,11 @@ public static void test() {
3636
jsonMap.put("collection", collection);
3737

3838
jsonMap.put("position", 1);
39+
jsonMap.put("@position", 0);
3940

4041
jsonMap.put("key", "key");
41-
Map<String, Integer> map = new HashMap<String, Integer>();
42-
map.put("key", 111);
42+
Map<String, Boolean> map = new HashMap<String, Boolean>();
43+
map.put("key", true);
4344
jsonMap.put("map", map);
4445

4546
String function = "get(Collection:collection,int:position)";//只允许引用,不能直接传值//"plus(@i0,@i1)";
@@ -58,16 +59,17 @@ public static void test() {
5859
System.out.println("plus = " + invoke(jsonMap, "plus(long:i0,long:i1)"));
5960
System.out.println("count = " + invoke(jsonMap, "count(Collection:collection)"));
6061
System.out.println("contains = " + invoke(jsonMap, "contains(Collection:collection,Object:id)"));
61-
System.out.println("get = " + invoke(jsonMap, "get(Map:map,key)"));
62-
System.out.println("Integer:get = " + invoke(jsonMap, "Integer:get(Map:map,key)"));
62+
System.out.println("get(Map:map,key) = " + invoke(jsonMap, "get(Map:map,key)"));
63+
System.out.println("get(Collection:collection,int:@position) = " + invoke(jsonMap, "get(Collection:collection,int:@position)"));
64+
System.out.println("Integer:get(Map:map,key) = " + invoke(jsonMap, "Integer:get(Map:map,key)"));
6365
}
6466

6567
/**反射调用
6668
* @param jsonMap
6769
* @param function 例如get(Map:map,key),参数只允许引用,不能直接传值
6870
* @return
6971
*/
70-
public static Object invoke(Map<String, Object> jsonMap, String function) {
72+
public static Object invoke(Map<String, Object> jsonMap, String function) throws Exception {
7173

7274
int start = function.indexOf("(");
7375
int end = function.lastIndexOf(")");
@@ -103,15 +105,15 @@ public static Object invoke(Map<String, Object> jsonMap, String function) {
103105
* @param args
104106
* @return
105107
*/
106-
public static Object invoke(String methodName, Class<?>[] parameterTypes, Object[] args) {
108+
public static Object invoke(String methodName, Class<?>[] parameterTypes, Object[] args) throws Exception {
107109
Function obj = new Function();
108110
Class<?> clazz = obj.getClass();
109-
try {
111+
// try {
110112
return clazz.getDeclaredMethod(methodName, parameterTypes).invoke(obj, args);
111-
} catch (Exception e) {
112-
e.printStackTrace();
113-
}
114-
return null;
113+
// } catch (Exception e) {
114+
// e.printStackTrace();
115+
// }
116+
// return null;
115117
}
116118

117119

APIJSON(Server)/APIJSON(Idea)/src/main/java/zuo/biao/apijson/server/QueryConfig.java

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414

1515
package zuo.biao.apijson.server;
1616

17+
import java.util.ArrayList;
1718
import java.util.Collection;
1819
import java.util.HashMap;
20+
import java.util.List;
1921
import java.util.Map;
2022
import java.util.Set;
2123

@@ -33,6 +35,14 @@
3335
public class QueryConfig {
3436
private static final String TAG = "QueryConfig";
3537

38+
public static final List<String> keyList;
39+
static {
40+
keyList = new ArrayList<String>();
41+
keyList.add(JSONRequest.KEY_COLUMNS);
42+
}
43+
44+
45+
3646
private long id;
3747
private RequestMethod method;
3848
private String table;
@@ -220,26 +230,26 @@ public static String getWhereString(RequestMethod method, Map<String, Object> wh
220230
}
221231
if (key.endsWith("@")) {//引用
222232
key = key.substring(0, key.lastIndexOf("@"));
223-
// throw new IllegalArgumentException(TAG + ".getWhereString: 字符 " + key + " 不合法!");
233+
// throw new IllegalArgumentException(TAG + ".getWhereString: 字符 " + key + " 不合法!");
224234
}
225235
if (key.endsWith("$")) {
226236
keyType = 1;
227237
} else if (key.endsWith("{}")) {
228238
keyType = 2;
229239
}
230-
240+
231241
value = where.get(key);
232-
242+
233243
try {
234244
key = RequestParser.getRealKey(key, false);
235245
} catch (Exception e) {
236246
Log.e(TAG, "getObject getWhereString try { key = RequestParser.getRealKey(key, false);"
237247
+ " >> } catch (Exception e) {");
238248
e.printStackTrace();
239249
}
240-
250+
241251
whereString += (key + (keyType == 1 ? " LIKE '" + value + "'" : (keyType == 2
242-
? getInString(((JSONArray)value).toArray()) : "='" + value + "'") ) + " AND ");
252+
? getRangeString(key, value) : "='" + value + "'") ) + " AND ");
243253
}
244254
if (whereString.endsWith("AND ")) {
245255
whereString = whereString.substring(0, whereString.length() - "AND ".length());
@@ -250,6 +260,21 @@ public static String getWhereString(RequestMethod method, Map<String, Object> wh
250260
}
251261
return "";
252262
}
263+
/**where key > 'key0' and key <= 'key1' and ...
264+
* @param key
265+
* @param range "condition0,condition1..."
266+
* @return key > 'key0' and key <= 'key1' and ...
267+
*/
268+
public static String getRangeString(String key, Object range) {
269+
if (range instanceof JSONArray) {
270+
return getInString(((JSONArray) range).toArray());
271+
}
272+
if (range instanceof String) {
273+
return ((String) range).replaceAll(",", " AND " + key);//非Number类型的可能需要客户端拼接成 < 'value0', >= 'value1'这种
274+
}
275+
276+
throw new IllegalArgumentException("\"key{}\":range 中range只能是 用','分隔条件的字符串 或者 可取选项JSONArray!");
277+
}
253278
/**where key in ('key0', 'key1', ... )
254279
* @param in
255280
* @return in ('key0', 'key1', ... )

APIJSON(Server)/APIJSON(Idea)/src/main/java/zuo/biao/apijson/server/RequestParser.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,7 @@ && isInRelationMap(path) == false) {
430430
Set<String> set = request.keySet();
431431
JSONObject transferredRequest = new JSONObject(true);
432432
Map<String, String> functionMap = new LinkedHashMap<>();
433+
Map<String, Object> selfDefineKeyMap = new LinkedHashMap<>();
433434
if (set != null) {
434435
Object value;
435436
JSONObject result;
@@ -450,6 +451,9 @@ && isInRelationMap(path) == false) {
450451
}
451452
} else {//JSONArray或其它Object,直接填充
452453
transferredRequest.put(key, value);
454+
if (key.startsWith("@") && QueryConfig.keyList.contains(key) == false) {
455+
selfDefineKeyMap.put(key, value);
456+
}//可@key@
453457
if (key.endsWith("()")) {
454458
if (value instanceof String == false) {
455459
throw new IllegalArgumentException("\"key()\": 后面必须为函数String!");
@@ -493,22 +497,25 @@ && isInRelationMap(path) == false) {
493497

494498

495499
if (transferredRequest != null && transferredRequest.isEmpty() == false) {//避免返回空的
500+
transferredRequest.putAll(selfDefineKeyMap);
496501
//解析函数function
497502
Set<String> functionSet = functionMap.keySet();
498503
if (functionSet != null && functionSet.isEmpty() == false) {
499504
for (String key : functionSet) {
500-
try {
501-
transferredRequest.put(getRealKey(key, false)
502-
, Function.invoke(transferredRequest, functionMap.get(key)));
503-
} catch (Exception e) {
504-
Log.e(TAG, "getObject containRelation == false && isTableKey(name)"
505-
+ " >> transferredRequest.put(getRealKey(key, false),"
506-
+ " Function.invoke(transferredRequest, functionMap.get(key)));"
507-
+ " >> } catch (Exception e) {");
508-
e.printStackTrace();
509-
}
505+
// try {
506+
transferredRequest.put(getRealKey(key, false)
507+
, Function.invoke(transferredRequest, functionMap.get(key)));
508+
// } catch (Exception e) {
509+
// Log.e(TAG, "getObject containRelation == false && isTableKey(name)"
510+
// + " >> transferredRequest.put(getRealKey(key, false),"
511+
// + " Function.invoke(transferredRequest, functionMap.get(key)));"
512+
// + " >> } catch (Exception e) {");
513+
// e.printStackTrace();
514+
// }
510515
}
511516
}
517+
518+
//可能客户端还有用,客户端自己解决,况且已知Android用fastjson解析不会崩溃 //移除自定义key
512519
}
513520

514521
if (parseRelation) {

0 commit comments

Comments
 (0)