Server:新增 name:name2 新建别名

This commit is contained in:
TommyLemon 2017-04-04 12:09:54 +08:00
parent 30e54882de
commit a5d10aa4f3
6 changed files with 79 additions and 40 deletions

View File

@ -23,10 +23,10 @@ package zuo.biao.apijson;
* @warn K,V都需要基本类型时不建议使用判空麻烦不如新建一个Model
*/
public class Entry<K, V> {
public K key;
public V value;
public Entry() {
//default
}
@ -37,8 +37,8 @@ public class Entry<K, V> {
this.key = key;
this.value = value;
}
public K getKey() {
return key;
}
@ -51,5 +51,9 @@ public class Entry<K, V> {
public void setValue(V value) {
this.value = value;
}
public boolean isEmpty() {
return key == null && value == null;
}
}

View File

@ -49,6 +49,9 @@ public class Pair extends Entry<String, String> {
super();
}
public boolean isEmpty(boolean trim) {
return StringUtil.isNotEmpty(key, trim) == false && StringUtil.isNotEmpty(value, trim) == false;
}
/**
* @param <K>
@ -83,22 +86,45 @@ public class Pair extends Entry<String, String> {
}
/**
* leftIsKey = true;
* "key":null不应该出现因为FastJSON内默认不存null
* @param pair
* @param defaultKey
* @return
* @param pair left:right
* @param leftIsKey true-左边为key当pair不包含 : 时默认整个pair为valuefalse-相反
* @return {@link #parseEntry(String, boolean)}
*/
public static Entry<String, String> parseEntry(String pair, String defaultKey) {
public static Entry<String, String> parseEntry(String pair) {
return parseEntry(pair, true);
}
/**
* leftIsKey = true;
* "key":null不应该出现因为FastJSON内默认不存null
* @param pair left:right
* @param leftIsKey true-左边为key当pair不包含 : 时默认整个pair为valuefalse-相反
* @return {@link #parseEntry(String, boolean, String)}
*/
public static Entry<String, String> parseEntry(String pair, boolean isLeftDefault) {
return parseEntry(pair, isLeftDefault, null);
}
/**
* "key":null不应该出现因为FastJSON内默认不存null
* @param pair left:right
* @param leftIsKey true-左边为key当pair不包含 : 时默认整个pair为valuefalse-相反
* @param defaultKey key默认值
* @return @NonNull
*/
public static Entry<String, String> parseEntry(String pair, boolean leftIsKey, String defaultKey) {
pair = StringUtil.getString(pair);//让客户端去掉所有空格 getNoBlankString(pair);
if (pair.isEmpty()) {
return null;
}
int index = pair.indexOf(":");
Entry<String, String> entry = new Entry<String, String>();
entry.setKey(index < 0 ? defaultKey : pair.substring(0, index));
entry.setValue(pair.substring(index + 1, pair.length()));
if (pair.isEmpty() == false) {
int index = pair.indexOf(":");
if (leftIsKey) {
entry.setKey(index < 0 ? defaultKey : pair.substring(0, index));
entry.setValue(pair.substring(index + 1, pair.length()));
} else {
entry.setValue(index < 0 ? defaultKey : pair.substring(0, index));
entry.setKey(pair.substring(index + 1, pair.length()));
}
}
return entry;
}
/**
@ -106,7 +132,7 @@ public class Pair extends Entry<String, String> {
* @return
*/
public static Entry<String, String> parseVariableEntry(String pair) {
return parseEntry(pair, Object.class.getSimpleName());
return parseEntry(pair, true, Object.class.getSimpleName());
}
/**
* @param pair
@ -115,15 +141,13 @@ public class Pair extends Entry<String, String> {
*/
public static Entry<Class<?>, Object> parseVariableEntry(String pair, Map<String, Object> valueMap) {
pair = StringUtil.getString(pair);//让客户端去掉所有空格 getNoBlankString(pair);
if (pair.isEmpty()) {
return null;
}
int index = pair.contains(":") ? pair.indexOf(":") : -1;
Entry<Class<?>, Object> entry = new Entry<Class<?>, Object>();
entry.setKey(classMap.get(index < 0 ? Object.class.getSimpleName() : pair.substring(0, index)));
entry.setValue(valueMap == null ? null : valueMap.get(pair.substring(index + 1, pair.length())));
if (pair.isEmpty() == false) {
int index = pair.contains(":") ? pair.indexOf(":") : -1;
entry.setKey(classMap.get(index < 0 ? Object.class.getSimpleName() : pair.substring(0, index)));
entry.setValue(valueMap == null ? null : valueMap.get(pair.substring(index + 1, pair.length())));
}
return entry;
}
}

View File

@ -94,7 +94,7 @@ public class Function implements FunctionList {
Entry<Class<?>, Object> entry;
for (int i = 0; i < typeValues.length; i++) {
entry = Pair.parseVariableEntry(typeValues[i], jsonMap);
if (entry != null) {
if (entry != null && entry.isEmpty() == false) {
types[i] = entry.getKey();
values[i] = entry.getValue();
}

View File

@ -128,13 +128,17 @@ public class QueryConfig {
}
public String getColumnString() {
switch (getMethod()) {
case POST:
return StringUtil.isNotEmpty(column, true) ? "(" + column + ")" : "";
case HEAD:
case POST_HEAD:
return " COUNT(0) COUNT ";
return "count(0) AS count";
case POST:
return StringUtil.isNotEmpty(column, true) ? "(" + column + ")" : "";
default:
return StringUtil.isNotEmpty(column, true) ? column : "*";
column = StringUtil.getString(column);
if (column.isEmpty()) {
return "*";
}
return column.contains(":") == false ? column : column.replaceAll(":", " AS ");//不能在这里改后续还要用到:
}
}

View File

@ -467,12 +467,15 @@ public class RequestParser {
}
boolean nameIsNumber = StringUtil.isNumer(name);
String table = Pair.parseEntry(name).getValue();
Log.d(TAG, "getObject table = " + table);
QueryConfig config = nameIsNumber ? parentConfig : null;
if (config == null) {
config = new QueryConfig(requestMethod, name).setCount(1);
config = new QueryConfig(requestMethod, table).setCount(1);
}
//避免"[]":{"0":{"1":{}}}这种导致第3层当成[]的直接子Object
if (nameIsNumber && ("" + config.getPosition()).equals(name) == false) {
if (nameIsNumber && ("" + config.getPosition()).equals(table) == false) {
config.setPosition(0).setCount(1).setPage(0);
}
@ -507,7 +510,7 @@ public class RequestParser {
}
} else if (requestMethod == PUT && JSON.isJSONArray(value)) {//PUT JSONArray
JSONArray array = ((JSONArray) value);
if (array != null && array.isEmpty() == false && isTableKey(name)) {
if (array != null && array.isEmpty() == false && isTableKey(table)) {
int putType = 0;
if (key.endsWith("+")) {//add
putType = 1;
@ -526,13 +529,13 @@ public class RequestParser {
arrayRequest.put(Table.ID, request.get(Table.ID));
// arrayRequest.setColumn(realKey);//put请求会对id添加功能符
arrayRequest.put(JSONRequest.KEY_COLUMN, realKey);
JSONRequest getRequest = new JSONRequest(name, arrayRequest);
JSONRequest getRequest = new JSONRequest(table, arrayRequest);
JSONObject response = new RequestParser().parseResponse(getRequest);
//GET >>>>>>>>>>>>>>>>>>>>>>>>>
//add all remove all <<<<<<<<<<<<<<<<<<<<<<<<<
response = response == null ? null : response.getJSONObject(name);
response = response == null ? null : response.getJSONObject(table);
JSONArray targetArray = response == null ? null : response.getJSONArray(realKey);
if (targetArray == null) {
targetArray = new JSONArray();
@ -582,8 +585,8 @@ public class RequestParser {
Object target = getValueByPath(getRelationPath(getAbsPath(path, replaceKey)), true);
Log.d(TAG, "getObject value = " + value + "; target = " + target);
if (((String) value).equals(target) && isTableKey(name)) {
Log.e(TAG, "getObject ((String) value).equals(target) && isTableKey(name) >> return null;");
if (((String) value).equals(target) && isTableKey(table)) {
Log.e(TAG, "getObject ((String) value).equals(target) && isTableKey(table) >> return null;");
return null;//parseRelation时还获取不到就不用再做无效的query了不考虑 Table:{Table:{}}嵌套
}
if (key.startsWith("@")) {
@ -616,10 +619,10 @@ public class RequestParser {
//执行SQL操作数据库
if (containRelation == false && isTableKey(name)) {//提高性能
if (containRelation == false && isTableKey(table)) {//提高性能
if (parseRelation == false || isInRelationMap(path)) {//避免覆盖原来已经获取的
// keyValuePathMap.remove(path);
QueryConfig config2 = newQueryConfig(name, transferredRequest);
QueryConfig config2 = newQueryConfig(table, transferredRequest);
if (parentConfig != null) {
config2.setCount(parentConfig.getCount()).setPage(parentConfig.getPage())
@ -1073,7 +1076,9 @@ public class RequestParser {
//"User:toUser":User转换"toUser":User, User为查询同名Table得到的JSONObject交给客户端处理更好查询就得截取
if (isTableKey) {//不允许在column key中使用Type:key形式
key = Pair.parseEntry(key, null).getKey();
key = Pair.parseEntry(key, false).getKey();//table以左边为准
} else {
key = Pair.parseEntry(key).getValue();//column以右边为准
}
if (isWord(key.startsWith("@") ? key.substring(1) : key) == false) {

View File

@ -27,6 +27,7 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import zuo.biao.apijson.Log;
import zuo.biao.apijson.Pair;
import zuo.biao.apijson.StringUtil;
import zuo.biao.apijson.Table;
import zuo.biao.apijson.server.QueryConfig;
@ -152,6 +153,7 @@ public class QueryHelper {
Object value = null;
Object json;
for (int i = 0; i < columnArray.length; i++) {
columnArray[i] = Pair.parseEntry(columnArray[i]).getValue();
try {
value = rs.getObject(rs.findColumn(columnArray[i]));
} catch (Exception e) {