Skip to content

Commit 4960808

Browse files
committed
Server:TypeValueKeyEntry改为Pair;调整RequestMethod的内部顺序;Client:与服务端同步RequestMethod
1 parent 4753436 commit 4960808

6 files changed

Lines changed: 69 additions & 54 deletions

File tree

APIJSON(Android)/APIJSON(ADT)/APIJSONLibrary/src/zuo/biao/apijson/RequestMethod.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,20 @@ public enum RequestMethod {
2525
GET,
2626

2727
/**
28-
* 通过POST来HEAD数据,不显示请求内容和返回结果,一般用于对安全要求比较高的请求
28+
* 检查,默认是非空检查,返回数据总数
2929
*/
30-
POST_HEAD,
30+
HEAD,
3131

3232
/**
3333
* 通过POST来GET数据,不显示请求内容和返回结果,一般用于对安全要求比较高的请求
3434
*/
3535
POST_GET,
3636

37+
/**
38+
* 通过POST来HEAD数据,不显示请求内容和返回结果,一般用于对安全要求比较高的请求
39+
*/
40+
POST_HEAD,
41+
3742
/**
3843
* 新增(或者说插入)数据
3944
*/
@@ -47,10 +52,5 @@ public enum RequestMethod {
4752
/**
4853
* 删除数据
4954
*/
50-
DELETE,
51-
52-
/**
53-
* 检查,默认是非空检查,返回数据总数
54-
*/
55-
HEAD
55+
DELETE
5656
}

APIJSON(Server)/APIJSON(Eclipse_JEE)/src/main/java/zuo/biao/apijson/TypeValueKeyEntry.java renamed to APIJSON(Server)/APIJSON(Eclipse_JEE)/src/main/java/zuo/biao/apijson/Pair.java

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
/**type:value
2222
* @author Lemon
2323
*/
24-
public class TypeValueKeyEntry extends Entry<String, String> {
24+
public class Pair extends Entry<String, String> {
2525

2626
private static Map<String, Class<?>> classMap;
2727
static {
@@ -43,77 +43,87 @@ public class TypeValueKeyEntry extends Entry<String, String> {
4343
classMap.put(Collection.class.getSimpleName(), Collection.class);//不允许指定<T>
4444
classMap.put(Map.class.getSimpleName(), Map.class);//不允许指定<T>
4545
}
46-
47-
48-
public TypeValueKeyEntry() {
46+
47+
48+
public Pair() {
4949
super();
5050
}
51-
52-
51+
52+
5353
/**
5454
* @param <K>
55-
* @param typeValue
55+
* @param pair
5656
* @return
5757
*/
58-
public static <K, V> boolean isCorrect(Entry<K, V> typeValue) {
59-
return typeValue != null && StringUtil.isNotEmpty(typeValue.getValue(), true);
58+
public static <K, V> boolean isCorrect(Entry<K, V> pair) {
59+
return pair != null && StringUtil.isNotEmpty(pair.getValue(), true);
6060
}
6161

6262
/**
63-
* @param typeValue
63+
* @param pair
6464
* @return
6565
*/
66-
public String toEntryString() {
67-
return toEntryString(getKey(), getValue());
66+
public String toPairString() {
67+
return toPairString(getKey(), getValue());
6868
}
6969
/**
70-
* @param typeValue
70+
* @param pair
7171
* @return
7272
*/
73-
public static String toEntryString(String typeKey, String valueKey) {
73+
public static String toPairString(String typeKey, String valueKey) {
7474
return (typeKey == null ? "" : typeKey + ":") + valueKey;
7575
}
7676
/**
7777
* @param type
7878
* @param value
7979
* @return
8080
*/
81-
public static String toEntryString(Class<?> type, Object value) {
82-
return toEntryString(type == null ? null : type.getSimpleName(), StringUtil.getString(value));
81+
public static String toPairString(Class<?> type, Object value) {
82+
return toPairString(type == null ? null : type.getSimpleName(), StringUtil.getString(value));
8383
}
84+
8485
/**
85-
* @param typeValue
86+
* "key":null不应该出现?因为FastJSON内默认不存null
87+
* @param pair
88+
* @param defaultKey
8689
* @return
8790
*/
88-
public static Entry<String, String> parseKeyEntry(String typeValue) {
89-
typeValue = StringUtil.getString(typeValue);//让客户端去掉所有空格 getNoBlankString(typeValue);
90-
if (typeValue.isEmpty()) {
91+
public static Entry<String, String> parseEntry(String pair, String defaultKey) {
92+
pair = StringUtil.getString(pair);//让客户端去掉所有空格 getNoBlankString(pair);
93+
if (pair.isEmpty()) {
9194
return null;
9295
}
93-
int index = typeValue.contains(":") ? typeValue.indexOf(":") : -1;
96+
int index = pair.indexOf(":");
9497

9598
Entry<String, String> entry = new Entry<String, String>();
96-
entry.setKey(index < 0 ? Object.class.getSimpleName() : typeValue.substring(0, index));
97-
entry.setValue(typeValue.substring(index + 1, typeValue.length()));
99+
entry.setKey(index < 0 ? defaultKey : pair.substring(0, index));
100+
entry.setValue(pair.substring(index + 1, pair.length()));
98101

99102
return entry;
100103
}
101104
/**
105+
* @param pair
106+
* @return
107+
*/
108+
public static Entry<String, String> parseVariableEntry(String pair) {
109+
return parseEntry(pair, Object.class.getSimpleName());
110+
}
111+
/**
112+
* @param pair
102113
* @param valueMap
103-
* @param typeValue
104114
* @return
105115
*/
106-
public static Entry<Class<?>, Object> parseEntry(Map<String, Object> valueMap, String typeValue) {
107-
typeValue = StringUtil.getString(typeValue);//让客户端去掉所有空格 getNoBlankString(typeValue);
108-
if (typeValue.isEmpty()) {
116+
public static Entry<Class<?>, Object> parseVariableEntry(String pair, Map<String, Object> valueMap) {
117+
pair = StringUtil.getString(pair);//让客户端去掉所有空格 getNoBlankString(pair);
118+
if (pair.isEmpty()) {
109119
return null;
110120
}
111-
int index = typeValue.contains(":") ? typeValue.indexOf(":") : -1;
112-
121+
int index = pair.contains(":") ? pair.indexOf(":") : -1;
122+
113123
Entry<Class<?>, Object> entry = new Entry<Class<?>, Object>();
114-
entry.setKey(classMap.get(index < 0 ? Object.class.getSimpleName() : typeValue.substring(0, index)));
115-
entry.setValue(valueMap == null ? null : valueMap.get(typeValue.substring(index + 1, typeValue.length())));
116-
124+
entry.setKey(classMap.get(index < 0 ? Object.class.getSimpleName() : pair.substring(0, index)));
125+
entry.setValue(valueMap == null ? null : valueMap.get(pair.substring(index + 1, pair.length())));
126+
117127
return entry;
118128
}
119129
}

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

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,20 @@ public enum RequestMethod {
2525
GET,
2626

2727
/**
28-
* 通过POST来HEAD数据,不显示请求内容和返回结果,一般用于对安全要求比较高的请求
28+
* 检查,默认是非空检查,返回数据总数
2929
*/
30-
POST_HEAD,
30+
HEAD,
3131

3232
/**
3333
* 通过POST来GET数据,不显示请求内容和返回结果,一般用于对安全要求比较高的请求
3434
*/
3535
POST_GET,
3636

37+
/**
38+
* 通过POST来HEAD数据,不显示请求内容和返回结果,一般用于对安全要求比较高的请求
39+
*/
40+
POST_HEAD,
41+
3742
/**
3843
* 新增(或者说插入)数据
3944
*/
@@ -47,10 +52,5 @@ public enum RequestMethod {
4752
/**
4853
* 删除数据
4954
*/
50-
DELETE,
51-
52-
/**
53-
* 检查,默认是非空检查,返回数据总数
54-
*/
55-
HEAD
55+
DELETE
5656
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import zuo.biao.apijson.Entry;
1111
import zuo.biao.apijson.FunctionList;
1212
import zuo.biao.apijson.StringUtil;
13-
import zuo.biao.apijson.TypeValueKeyEntry;
13+
import zuo.biao.apijson.Pair;
1414
import zuo.biao.apijson.server.model.BaseModel;
1515

1616
/**
@@ -75,7 +75,7 @@ public static Object invoke(Map<String, Object> jsonMap, String function) throws
7575

7676
int start = function.indexOf("(");
7777
int end = function.lastIndexOf(")");
78-
Entry<String, String> method = TypeValueKeyEntry.parseKeyEntry(function.substring(0, start));
78+
Entry<String, String> method = Pair.parseVariableEntry(function.substring(0, start));
7979
if (method == null || StringUtil.isNotEmpty(method.getValue(), true) == false) {
8080
System.out.println(TAG + "invoke method == null || StringUtil.isNotEmpty(method.getValue(), true) == false"
8181
+ " >> return null;");
@@ -92,7 +92,7 @@ public static Object invoke(Map<String, Object> jsonMap, String function) throws
9292

9393
Entry<Class<?>, Object> entry;
9494
for (int i = 0; i < typeValues.length; i++) {
95-
entry = TypeValueKeyEntry.parseEntry(jsonMap, typeValues[i]);
95+
entry = Pair.parseVariableEntry(typeValues[i], jsonMap);
9696
if (entry != null) {
9797
types[i] = entry.getKey();
9898
values[i] = entry.getValue();

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
import zuo.biao.apijson.RequestMethod;
4343
import zuo.biao.apijson.StringUtil;
4444
import zuo.biao.apijson.Table;
45-
import zuo.biao.apijson.TypeValueKeyEntry;
45+
import zuo.biao.apijson.Pair;
4646
import zuo.biao.apijson.server.sql.AccessVerifier;
4747
import zuo.biao.apijson.server.sql.QueryHelper;
4848

@@ -654,6 +654,7 @@ private JSONObject getArray(String parentPath, QueryConfig parentConfig, String
654654
count = 100;
655655
}
656656

657+
String firstTableKey = null;
657658
//最好先获取第一个table的所有项(where条件),填充一个列表?
658659
Set<String> set = new LinkedHashSet<>(request.keySet());
659660
if (count <= 0 || count > 10) {//10以下不优化长度
@@ -663,6 +664,7 @@ private JSONObject getArray(String parentPath, QueryConfig parentConfig, String
663664
object = isTableKey(key) ? request.get(key) : null;
664665
if (object != null && object instanceof JSONObject) {// && object.isEmpty() == false) {
665666
// totalCount = QueryHelper.getInstance().getCount(key);
667+
firstTableKey = key;
666668
JSONObject response = new RequestParser(HEAD)
667669
.parseResponse(new JSONRequest(key, object));
668670
JSONObject target = response == null ? null : response.getJSONObject(key);
@@ -719,7 +721,7 @@ private JSONObject getArray(String parentPath, QueryConfig parentConfig, String
719721
}
720722
}
721723
} else {
722-
for (String key : set) {
724+
for (String key : set) {//0:{},1:{}...
723725
value = request.get(key);
724726
if (value instanceof JSONObject) {//JSONObject,往下一级提取
725727
config.setPosition(Integer.valueOf(0 + StringUtil.getNumber(key, true)));
@@ -729,7 +731,9 @@ private JSONObject getArray(String parentPath, QueryConfig parentConfig, String
729731
result = getObject(path, config, key, (JSONObject) value);
730732
}
731733
if (result != null && result.isEmpty() == false) {//只添加!=null的值,可能数据库返回数据不够count
734+
//先实现功能,后续再优化 if (result.containsKey(firstTableKey)) {//第一个Table都没有,后面的也无效
732735
transferredRequest.put(key, result);
736+
// }
733737
}
734738
} else {//JSONArray或其它Object
735739
//array里不允许关联,只能在object中关联
@@ -1015,7 +1019,7 @@ public static String getRealKey(RequestMethod method, String originKey, boolean
10151019

10161020
//"User:toUser":User转换"toUser":User, User为查询同名Table得到的JSONObject。交给客户端处理更好?不,查询就得截取
10171021
if (isTableKey) {//不允许在column key中使用Type:key形式
1018-
key = TypeValueKeyEntry.parseKeyEntry(key).getKey();
1022+
key = Pair.parseEntry(key, null).getKey();
10191023
}
10201024

10211025
if (isWord(key.startsWith("@") ? key.substring(1) : key) == false) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public Connection getConnection() throws Exception {
5555
return DriverManager.getConnection(YOUR_MYSQL_URL + YOUR_MYSQL_SCHEMA, YOUR_MYSQL_ACCOUNT, YOUR_MYSQL_PASSWORD);
5656
}
5757

58+
//TODO key应该改成SQL
5859
private void saveCache(String key, List<JSONObject> list) {
5960
if (key == null) {
6061
System.out.println("saveList key == null >> return;");

0 commit comments

Comments
 (0)