Skip to content

Commit a5d10aa

Browse files
committed
Server:新增 name:name2 新建别名
1 parent 30e5488 commit a5d10aa

6 files changed

Lines changed: 79 additions & 40 deletions

File tree

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@
2323
* @warn K,V都需要基本类型时不建议使用,判空麻烦,不如新建一个Model
2424
*/
2525
public class Entry<K, V> {
26-
26+
2727
public K key;
2828
public V value;
29-
29+
3030
public Entry() {
3131
//default
3232
}
@@ -37,8 +37,8 @@ public Entry(K key, V value) {
3737
this.key = key;
3838
this.value = value;
3939
}
40-
41-
40+
41+
4242
public K getKey() {
4343
return key;
4444
}
@@ -51,5 +51,9 @@ public V getValue() {
5151
public void setValue(V value) {
5252
this.value = value;
5353
}
54-
54+
55+
public boolean isEmpty() {
56+
return key == null && value == null;
57+
}
58+
5559
}

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

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ public Pair() {
4949
super();
5050
}
5151

52+
public boolean isEmpty(boolean trim) {
53+
return StringUtil.isNotEmpty(key, trim) == false && StringUtil.isNotEmpty(value, trim) == false;
54+
}
5255

5356
/**
5457
* @param <K>
@@ -83,30 +86,53 @@ public static String toPairString(Class<?> type, Object value) {
8386
}
8487

8588
/**
89+
* leftIsKey = true;
8690
* "key":null不应该出现?因为FastJSON内默认不存null
87-
* @param pair
88-
* @param defaultKey
89-
* @return
91+
* @param pair left:right
92+
* @param leftIsKey true-左边为key,当pair不包含 : 时默认整个pair为value;false-相反
93+
* @return {@link #parseEntry(String, boolean)}
94+
*/
95+
public static Entry<String, String> parseEntry(String pair) {
96+
return parseEntry(pair, true);
97+
}
98+
/**
99+
* leftIsKey = true;
100+
* "key":null不应该出现?因为FastJSON内默认不存null
101+
* @param pair left:right
102+
* @param leftIsKey true-左边为key,当pair不包含 : 时默认整个pair为value;false-相反
103+
* @return {@link #parseEntry(String, boolean, String)}
104+
*/
105+
public static Entry<String, String> parseEntry(String pair, boolean isLeftDefault) {
106+
return parseEntry(pair, isLeftDefault, null);
107+
}
108+
/**
109+
* "key":null不应该出现?因为FastJSON内默认不存null
110+
* @param pair left:right
111+
* @param leftIsKey true-左边为key,当pair不包含 : 时默认整个pair为value;false-相反
112+
* @param defaultKey key默认值
113+
* @return @NonNull
90114
*/
91-
public static Entry<String, String> parseEntry(String pair, String defaultKey) {
115+
public static Entry<String, String> parseEntry(String pair, boolean leftIsKey, String defaultKey) {
92116
pair = StringUtil.getString(pair);//让客户端去掉所有空格 getNoBlankString(pair);
93-
if (pair.isEmpty()) {
94-
return null;
95-
}
96-
int index = pair.indexOf(":");
97-
98117
Entry<String, String> entry = new Entry<String, String>();
99-
entry.setKey(index < 0 ? defaultKey : pair.substring(0, index));
100-
entry.setValue(pair.substring(index + 1, pair.length()));
101-
118+
if (pair.isEmpty() == false) {
119+
int index = pair.indexOf(":");
120+
if (leftIsKey) {
121+
entry.setKey(index < 0 ? defaultKey : pair.substring(0, index));
122+
entry.setValue(pair.substring(index + 1, pair.length()));
123+
} else {
124+
entry.setValue(index < 0 ? defaultKey : pair.substring(0, index));
125+
entry.setKey(pair.substring(index + 1, pair.length()));
126+
}
127+
}
102128
return entry;
103129
}
104130
/**
105131
* @param pair
106132
* @return
107133
*/
108134
public static Entry<String, String> parseVariableEntry(String pair) {
109-
return parseEntry(pair, Object.class.getSimpleName());
135+
return parseEntry(pair, true, Object.class.getSimpleName());
110136
}
111137
/**
112138
* @param pair
@@ -115,15 +141,13 @@ public static Entry<String, String> parseVariableEntry(String pair) {
115141
*/
116142
public static Entry<Class<?>, Object> parseVariableEntry(String pair, Map<String, Object> valueMap) {
117143
pair = StringUtil.getString(pair);//让客户端去掉所有空格 getNoBlankString(pair);
118-
if (pair.isEmpty()) {
119-
return null;
120-
}
121-
int index = pair.contains(":") ? pair.indexOf(":") : -1;
122-
123144
Entry<Class<?>, Object> entry = new Entry<Class<?>, Object>();
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())));
145+
if (pair.isEmpty() == false) {
146+
int index = pair.contains(":") ? pair.indexOf(":") : -1;
126147

148+
entry.setKey(classMap.get(index < 0 ? Object.class.getSimpleName() : pair.substring(0, index)));
149+
entry.setValue(valueMap == null ? null : valueMap.get(pair.substring(index + 1, pair.length())));
150+
}
127151
return entry;
128152
}
129153
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public static Object invoke(Map<String, Object> jsonMap, String function) throws
9494
Entry<Class<?>, Object> entry;
9595
for (int i = 0; i < typeValues.length; i++) {
9696
entry = Pair.parseVariableEntry(typeValues[i], jsonMap);
97-
if (entry != null) {
97+
if (entry != null && entry.isEmpty() == false) {
9898
types[i] = entry.getKey();
9999
values[i] = entry.getValue();
100100
}

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,17 @@ public QueryConfig setColumn(String column) {
128128
}
129129
public String getColumnString() {
130130
switch (getMethod()) {
131-
case POST:
132-
return StringUtil.isNotEmpty(column, true) ? "(" + column + ")" : "";
133131
case HEAD:
134132
case POST_HEAD:
135-
return " COUNT(0) COUNT ";
133+
return "count(0) AS count";
134+
case POST:
135+
return StringUtil.isNotEmpty(column, true) ? "(" + column + ")" : "";
136136
default:
137-
return StringUtil.isNotEmpty(column, true) ? column : "*";
137+
column = StringUtil.getString(column);
138+
if (column.isEmpty()) {
139+
return "*";
140+
}
141+
return column.contains(":") == false ? column : column.replaceAll(":", " AS ");//不能在这里改,后续还要用到:
138142
}
139143
}
140144

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

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -467,12 +467,15 @@ && isInRelationMap(path) == false) {
467467
}
468468

469469
boolean nameIsNumber = StringUtil.isNumer(name);
470+
String table = Pair.parseEntry(name).getValue();
471+
Log.d(TAG, "getObject table = " + table);
472+
470473
QueryConfig config = nameIsNumber ? parentConfig : null;
471474
if (config == null) {
472-
config = new QueryConfig(requestMethod, name).setCount(1);
475+
config = new QueryConfig(requestMethod, table).setCount(1);
473476
}
474477
//避免"[]":{"0":{"1":{}}}这种导致第3层当成[]的直接子Object
475-
if (nameIsNumber && ("" + config.getPosition()).equals(name) == false) {
478+
if (nameIsNumber && ("" + config.getPosition()).equals(table) == false) {
476479
config.setPosition(0).setCount(1).setPage(0);
477480
}
478481

@@ -507,7 +510,7 @@ && isInRelationMap(path) == false) {
507510
}
508511
} else if (requestMethod == PUT && JSON.isJSONArray(value)) {//PUT JSONArray
509512
JSONArray array = ((JSONArray) value);
510-
if (array != null && array.isEmpty() == false && isTableKey(name)) {
513+
if (array != null && array.isEmpty() == false && isTableKey(table)) {
511514
int putType = 0;
512515
if (key.endsWith("+")) {//add
513516
putType = 1;
@@ -526,13 +529,13 @@ && isInRelationMap(path) == false) {
526529
arrayRequest.put(Table.ID, request.get(Table.ID));
527530
// arrayRequest.setColumn(realKey);//put请求会对id添加功能符?
528531
arrayRequest.put(JSONRequest.KEY_COLUMN, realKey);
529-
JSONRequest getRequest = new JSONRequest(name, arrayRequest);
532+
JSONRequest getRequest = new JSONRequest(table, arrayRequest);
530533
JSONObject response = new RequestParser().parseResponse(getRequest);
531534
//GET >>>>>>>>>>>>>>>>>>>>>>>>>
532535

533536

534537
//add all 或 remove all <<<<<<<<<<<<<<<<<<<<<<<<<
535-
response = response == null ? null : response.getJSONObject(name);
538+
response = response == null ? null : response.getJSONObject(table);
536539
JSONArray targetArray = response == null ? null : response.getJSONArray(realKey);
537540
if (targetArray == null) {
538541
targetArray = new JSONArray();
@@ -582,8 +585,8 @@ && isInRelationMap(path) == false) {
582585

583586
Object target = getValueByPath(getRelationPath(getAbsPath(path, replaceKey)), true);
584587
Log.d(TAG, "getObject value = " + value + ";> + target);
585-
if (((String) value).equals(target) && isTableKey(name)) {
586-
Log.e(TAG, "getObject ((String) value).equals(target) && isTableKey(name) >> return null;");
588+
if (((String) value).equals(target) && isTableKey(table)) {
589+
Log.e(TAG, "getObject ((String) value).equals(target) && isTableKey(table) >> return null;");
587590
return null;//parseRelation时还获取不到就不用再做无效的query了。不考虑 Table:{Table:{}}嵌套
588591
}
589592
if (key.startsWith("@")) {
@@ -616,10 +619,10 @@ && isInRelationMap(path) == false) {
616619

617620

618621
//执行SQL操作数据库
619-
if (containRelation == false && isTableKey(name)) {//提高性能
622+
if (containRelation == false && isTableKey(table)) {//提高性能
620623
if (parseRelation == false || isInRelationMap(path)) {//避免覆盖原来已经获取的
621624
// keyValuePathMap.remove(path);
622-
QueryConfig config2 = newQueryConfig(name, transferredRequest);
625+
QueryConfig config2 = newQueryConfig(table, transferredRequest);
623626

624627
if (parentConfig != null) {
625628
config2.setCount(parentConfig.getCount()).setPage(parentConfig.getPage())
@@ -1073,7 +1076,9 @@ public static String getRealKey(RequestMethod method, String originKey, boolean
10731076

10741077
//"User:toUser":User转换"toUser":User, User为查询同名Table得到的JSONObject。交给客户端处理更好?不,查询就得截取
10751078
if (isTableKey) {//不允许在column key中使用Type:key形式
1076-
key = Pair.parseEntry(key, null).getKey();
1079+
key = Pair.parseEntry(key, false).getKey();//table以左边为准
1080+
} else {
1081+
key = Pair.parseEntry(key).getValue();//column以右边为准
10771082
}
10781083

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

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import com.alibaba.fastjson.JSONObject;
2828

2929
import zuo.biao.apijson.Log;
30+
import zuo.biao.apijson.Pair;
3031
import zuo.biao.apijson.StringUtil;
3132
import zuo.biao.apijson.Table;
3233
import zuo.biao.apijson.server.QueryConfig;
@@ -152,6 +153,7 @@ public JSONObject select(QueryConfig config) throws Exception {
152153
Object value = null;
153154
Object json;
154155
for (int i = 0; i < columnArray.length; i++) {
156+
columnArray[i] = Pair.parseEntry(columnArray[i]).getValue();
155157
try {
156158
value = rs.getObject(rs.findColumn(columnArray[i]));
157159
} catch (Exception e) {

0 commit comments

Comments
 (0)