Skip to content

Commit 0e947d2

Browse files
committed
新增支持多字段 IN,SQL 函数 作为 表达式 左侧 值 等条件
1 parent c6cba97 commit 0e947d2

File tree

4 files changed

+76
-9
lines changed

4 files changed

+76
-9
lines changed

APIJSONORM/src/main/java/apijson/JSONObject.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ public JSONObject setUserIdIn(List<Object> list) {
150150
public static final String KEY_HAVING = "@having"; //聚合函数条件,一般和@group一起用
151151
public static final String KEY_HAVING_AND = "@having&"; //聚合函数条件,一般和@group一起用
152152
public static final String KEY_ORDER = "@order"; //排序方式
153+
public static final String KEY_KEY = "@key"; // key 映射,year:left(date,4);name_tag:(name,tag)
153154
public static final String KEY_RAW = "@raw"; // 自定义原始 SQL 片段
154155
public static final String KEY_JSON = "@json"; //SQL Server 把字段转为 JSON 输出
155156
public static final String KEY_METHOD = "@method"; // json 对象配置操作方法
@@ -181,6 +182,7 @@ public JSONObject setUserIdIn(List<Object> list) {
181182
TABLE_KEY_LIST.add(KEY_HAVING);
182183
TABLE_KEY_LIST.add(KEY_HAVING_AND);
183184
TABLE_KEY_LIST.add(KEY_ORDER);
185+
TABLE_KEY_LIST.add(KEY_KEY);
184186
TABLE_KEY_LIST.add(KEY_RAW);
185187
TABLE_KEY_LIST.add(KEY_JSON);
186188
TABLE_KEY_LIST.add(KEY_METHOD);
@@ -405,6 +407,14 @@ public JSONObject setOrder(String keys) {
405407
return puts(KEY_ORDER, keys);
406408
}
407409

410+
/**set key map
411+
* @param keyMap "name_tag:(name,tag);year:left(date,1,5)..."
412+
* @return
413+
*/
414+
public JSONObject setKey(String keyMap) {
415+
return puts(KEY_KEY, keyMap);
416+
}
417+
408418
/**set keys to raw
409419
* @param keys "key0,key1,key2..."
410420
* @return

APIJSONORM/src/main/java/apijson/orm/AbstractParser.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,7 @@ else if (childKeys.length == 1 && JSONRequest.isTableKey(childKeys[0])) { //
14451445
JOIN_COPY_KEY_LIST.add(JSONRequest.KEY_HAVING);
14461446
JOIN_COPY_KEY_LIST.add(JSONRequest.KEY_HAVING_AND);
14471447
JOIN_COPY_KEY_LIST.add(JSONRequest.KEY_ORDER);
1448+
JOIN_COPY_KEY_LIST.add(JSONRequest.KEY_KEY);
14481449
JOIN_COPY_KEY_LIST.add(JSONRequest.KEY_RAW);
14491450
}
14501451

APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java

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

1672+
@Override
1673+
public Map<String, String> getKeyMap() {
1674+
return keyMap;
1675+
}
1676+
@Override
1677+
public AbstractSQLConfig setKeyMap(Map<String, String> keyMap) {
1678+
this.keyMap = keyMap;
1679+
return this;
1680+
}
1681+
16661682
@Override
16671683
public List<String> getRaw() {
16681684
return raw;
16691685
}
16701686
@Override
1671-
public SQLConfig setRaw(List<String> raw) {
1687+
public AbstractSQLConfig setRaw(List<String> raw) {
16721688
this.raw = raw;
16731689
return this;
16741690
}
@@ -1967,13 +1983,13 @@ public String parseSQLExpression(String key, String expression, boolean containR
19671983
return parseSQLExpression(key, expression, containRaw, allowAlias, null);
19681984
}
19691985
/**解析@column 中以“;”分隔的表达式("@column":"expression1;expression2;expression2;....")中的expression
1970-
* @param key
1971-
* @param expression
1972-
* @param containRaw
1973-
* @param allowAlias
1974-
* @param example
1975-
* @return
1976-
*/
1986+
* @param key
1987+
* @param expression
1988+
* @param containRaw
1989+
* @param allowAlias
1990+
* @param example
1991+
* @return
1992+
*/
19771993
public String parseSQLExpression(String key, String expression, boolean containRaw, boolean allowAlias, String example) {
19781994
String quote = getQuote();
19791995
int start = expression.indexOf('(');
@@ -3486,7 +3502,18 @@ public String getKey(String key) {
34863502
return getSQLValue(key).toString();
34873503
}
34883504

3489-
return getSQLKey(key);
3505+
Map<String, String> keyMap = getKeyMap();
3506+
String expression = keyMap == null ? null : keyMap.get(key);
3507+
if (expression == null) {
3508+
expression = COLUMN_KEY_MAP == null ? null : COLUMN_KEY_MAP.get(key);
3509+
}
3510+
if (expression == null) {
3511+
return getSQLKey(key);
3512+
}
3513+
3514+
// (name,tag) left(date,4) 等
3515+
List<String> raw = getRaw();
3516+
return parseSQLExpression(KEY_KEY, expression, raw != null && raw.contains(KEY_KEY), false);
34903517
}
34913518
public String getSQLKey(String key) {
34923519
String q = getQuote();
@@ -5075,6 +5102,7 @@ else if (userId instanceof Subquery) {}
50755102
Object having = request.get(KEY_HAVING);
50765103
String havingAnd = request.getString(KEY_HAVING_AND);
50775104
String order = request.getString(KEY_ORDER);
5105+
Object keyMap = request.get(KEY_KEY);
50785106
String raw = request.getString(KEY_RAW);
50795107
String json = request.getString(KEY_JSON);
50805108
String mthd = request.getString(KEY_METHOD);
@@ -5101,6 +5129,7 @@ else if (userId instanceof Subquery) {}
51015129
request.remove(KEY_HAVING);
51025130
request.remove(KEY_HAVING_AND);
51035131
request.remove(KEY_ORDER);
5132+
request.remove(KEY_KEY);
51045133
request.remove(KEY_RAW);
51055134
request.remove(KEY_JSON);
51065135
request.remove(KEY_METHOD);
@@ -5563,6 +5592,27 @@ else if (newHaving != null) {
55635592
}
55645593
// @having, @haivng& >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
55655594

5595+
if (keyMap instanceof Map) {
5596+
config.setKeyMap((Map<String, String>) keyMap);
5597+
}
5598+
else if (keyMap instanceof String) {
5599+
String[] ks = StringUtil.split((String) keyMap, ";");
5600+
if (ks.length > 0) {
5601+
Map<String, String> nkm = new LinkedHashMap<>();
5602+
for (int i = 0; i < ks.length; i++) {
5603+
Entry<String, String> ety = Pair.parseEntry(ks[i]);
5604+
if (ety == null) {
5605+
continue;
5606+
}
5607+
nkm.put(ety.getKey(), ety.getValue());
5608+
}
5609+
config.setKeyMap(nkm);
5610+
}
5611+
}
5612+
else if (keyMap != null) {
5613+
throw new UnsupportedDataTypeException("@key:value 中 value 错误,只能是 String, JSONObject 中的一种!");
5614+
}
5615+
55665616

55675617
config.setExplain(explain != null && explain);
55685618
config.setCache(getCache(cache));
@@ -5649,6 +5699,9 @@ else if (newHaving != null) {
56495699
if (order != null) {
56505700
request.put(KEY_ORDER, order);
56515701
}
5702+
if (keyMap != null) {
5703+
request.put(KEY_KEY, keyMap);
5704+
}
56525705
if (raw != null) {
56535706
request.put(KEY_RAW, raw);
56545707
}

APIJSONORM/src/main/java/apijson/orm/SQLConfig.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,9 @@ default int[] getDBVersionNums() {
242242

243243
String getTablePath();
244244

245+
Map<String, String> getKeyMap();
246+
AbstractSQLConfig setKeyMap(Map<String, String> keyMap);
247+
245248
List<String> getRaw();
246249
SQLConfig setRaw(List<String> raw);
247250

0 commit comments

Comments
 (0)