Skip to content

Commit

Permalink
新增支持多字段 IN,SQL 函数 作为 表达式 左侧 值 等条件
Browse files Browse the repository at this point in the history
  • Loading branch information
TommyLemon committed Nov 12, 2023
1 parent c6cba97 commit 0e947d2
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 9 deletions.
10 changes: 10 additions & 0 deletions APIJSONORM/src/main/java/apijson/JSONObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public JSONObject setUserIdIn(List<Object> list) {
public static final String KEY_HAVING = "@having"; //聚合函数条件,一般和@group一起用
public static final String KEY_HAVING_AND = "@having&"; //聚合函数条件,一般和@group一起用
public static final String KEY_ORDER = "@order"; //排序方式
public static final String KEY_KEY = "@key"; // key 映射,year:left(date,4);name_tag:(name,tag)
public static final String KEY_RAW = "@raw"; // 自定义原始 SQL 片段
public static final String KEY_JSON = "@json"; //SQL Server 把字段转为 JSON 输出
public static final String KEY_METHOD = "@method"; // json 对象配置操作方法
Expand Down Expand Up @@ -181,6 +182,7 @@ public JSONObject setUserIdIn(List<Object> list) {
TABLE_KEY_LIST.add(KEY_HAVING);
TABLE_KEY_LIST.add(KEY_HAVING_AND);
TABLE_KEY_LIST.add(KEY_ORDER);
TABLE_KEY_LIST.add(KEY_KEY);
TABLE_KEY_LIST.add(KEY_RAW);
TABLE_KEY_LIST.add(KEY_JSON);
TABLE_KEY_LIST.add(KEY_METHOD);
Expand Down Expand Up @@ -405,6 +407,14 @@ public JSONObject setOrder(String keys) {
return puts(KEY_ORDER, keys);
}

/**set key map
* @param keyMap "name_tag:(name,tag);year:left(date,1,5)..."
* @return
*/
public JSONObject setKey(String keyMap) {
return puts(KEY_KEY, keyMap);
}

/**set keys to raw
* @param keys "key0,key1,key2..."
* @return
Expand Down
1 change: 1 addition & 0 deletions APIJSONORM/src/main/java/apijson/orm/AbstractParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -1445,6 +1445,7 @@ else if (childKeys.length == 1 && JSONRequest.isTableKey(childKeys[0])) { //
JOIN_COPY_KEY_LIST.add(JSONRequest.KEY_HAVING);
JOIN_COPY_KEY_LIST.add(JSONRequest.KEY_HAVING_AND);
JOIN_COPY_KEY_LIST.add(JSONRequest.KEY_ORDER);
JOIN_COPY_KEY_LIST.add(JSONRequest.KEY_KEY);
JOIN_COPY_KEY_LIST.add(JSONRequest.KEY_RAW);
}

Expand Down
71 changes: 62 additions & 9 deletions APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import static apijson.JSONObject.KEY_JSON;
import static apijson.JSONObject.KEY_NULL;
import static apijson.JSONObject.KEY_ORDER;
import static apijson.JSONObject.KEY_KEY;
import static apijson.JSONObject.KEY_RAW;
import static apijson.JSONObject.KEY_ROLE;
import static apijson.JSONObject.KEY_SCHEMA;
Expand Down Expand Up @@ -133,6 +134,10 @@ public abstract class AbstractSQLConfig<T extends Object> implements SQLConfig<T
* 表名映射,隐藏真实表名,对安全要求很高的表可以这么做
*/
public static Map<String, String> TABLE_KEY_MAP;
/**
* 字段名映射,隐藏真实字段名,对安全要求很高的表可以这么做,另外可以配置 name_tag:(name,tag) 来实现多字段 IN,length_tag:length(tag) 来实现 SQL 函数复杂条件
*/
public static Map<String, String> COLUMN_KEY_MAP;
/**
* 允许批量增删改部分记录失败的表
*/
Expand Down Expand Up @@ -907,6 +912,7 @@ public String getUserIdKey() {
private String havingCombine; //聚合函数的字符串数组,','分隔
private Map<String, Object> having; //聚合函数的字符串数组,','分隔
private String order; //排序方式的字符串数组,','分隔
private Map<String, String> keyMap; //字段名映射,支持 name_tag:(name,tag) 多字段 IN,year:left(date,4) 截取日期年份等
private List<String> raw; //需要保留原始 SQL 的字段,','分隔
private List<String> json; //需要转为 JSON 的字段,','分隔
private Subquery from; //子查询临时表
Expand Down Expand Up @@ -1663,12 +1669,22 @@ public String getOrderString(boolean hasPrefix) {
return (hasPrefix ? " ORDER BY " : "") + StringUtil.concat(StringUtil.getString(keys), joinOrder, ", ");
}

@Override
public Map<String, String> getKeyMap() {
return keyMap;
}
@Override
public AbstractSQLConfig setKeyMap(Map<String, String> keyMap) {
this.keyMap = keyMap;
return this;
}

@Override
public List<String> getRaw() {
return raw;
}
@Override
public SQLConfig setRaw(List<String> raw) {
public AbstractSQLConfig setRaw(List<String> raw) {
this.raw = raw;
return this;
}
Expand Down Expand Up @@ -1967,13 +1983,13 @@ public String parseSQLExpression(String key, String expression, boolean containR
return parseSQLExpression(key, expression, containRaw, allowAlias, null);
}
/**解析@column 中以“;”分隔的表达式("@column":"expression1;expression2;expression2;....")中的expression
* @param key
* @param expression
* @param containRaw
* @param allowAlias
* @param example
* @return
*/
* @param key
* @param expression
* @param containRaw
* @param allowAlias
* @param example
* @return
*/
public String parseSQLExpression(String key, String expression, boolean containRaw, boolean allowAlias, String example) {
String quote = getQuote();
int start = expression.indexOf('(');
Expand Down Expand Up @@ -3486,7 +3502,18 @@ public String getKey(String key) {
return getSQLValue(key).toString();
}

return getSQLKey(key);
Map<String, String> keyMap = getKeyMap();
String expression = keyMap == null ? null : keyMap.get(key);
if (expression == null) {
expression = COLUMN_KEY_MAP == null ? null : COLUMN_KEY_MAP.get(key);
}
if (expression == null) {
return getSQLKey(key);
}

// (name,tag) left(date,4) 等
List<String> raw = getRaw();
return parseSQLExpression(KEY_KEY, expression, raw != null && raw.contains(KEY_KEY), false);
}
public String getSQLKey(String key) {
String q = getQuote();
Expand Down Expand Up @@ -5075,6 +5102,7 @@ else if (userId instanceof Subquery) {}
Object having = request.get(KEY_HAVING);
String havingAnd = request.getString(KEY_HAVING_AND);
String order = request.getString(KEY_ORDER);
Object keyMap = request.get(KEY_KEY);
String raw = request.getString(KEY_RAW);
String json = request.getString(KEY_JSON);
String mthd = request.getString(KEY_METHOD);
Expand All @@ -5101,6 +5129,7 @@ else if (userId instanceof Subquery) {}
request.remove(KEY_HAVING);
request.remove(KEY_HAVING_AND);
request.remove(KEY_ORDER);
request.remove(KEY_KEY);
request.remove(KEY_RAW);
request.remove(KEY_JSON);
request.remove(KEY_METHOD);
Expand Down Expand Up @@ -5563,6 +5592,27 @@ else if (newHaving != null) {
}
// @having, @haivng& >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

if (keyMap instanceof Map) {
config.setKeyMap((Map<String, String>) keyMap);
}
else if (keyMap instanceof String) {
String[] ks = StringUtil.split((String) keyMap, ";");
if (ks.length > 0) {
Map<String, String> nkm = new LinkedHashMap<>();
for (int i = 0; i < ks.length; i++) {
Entry<String, String> ety = Pair.parseEntry(ks[i]);
if (ety == null) {
continue;
}
nkm.put(ety.getKey(), ety.getValue());
}
config.setKeyMap(nkm);
}
}
else if (keyMap != null) {
throw new UnsupportedDataTypeException("@key:value 中 value 错误,只能是 String, JSONObject 中的一种!");
}


config.setExplain(explain != null && explain);
config.setCache(getCache(cache));
Expand Down Expand Up @@ -5649,6 +5699,9 @@ else if (newHaving != null) {
if (order != null) {
request.put(KEY_ORDER, order);
}
if (keyMap != null) {
request.put(KEY_KEY, keyMap);
}
if (raw != null) {
request.put(KEY_RAW, raw);
}
Expand Down
3 changes: 3 additions & 0 deletions APIJSONORM/src/main/java/apijson/orm/SQLConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ default int[] getDBVersionNums() {

String getTablePath();

Map<String, String> getKeyMap();
AbstractSQLConfig setKeyMap(Map<String, String> keyMap);

List<String> getRaw();
SQLConfig setRaw(List<String> raw);

Expand Down

0 comments on commit 0e947d2

Please sign in to comment.