From 0e947d2d851bcd13cc60d957d4321ea5336e6d66 Mon Sep 17 00:00:00 2001 From: TommyLemon Date: Sun, 12 Nov 2023 17:25:25 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=94=AF=E6=8C=81=E5=A4=9A?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=20IN=EF=BC=8CSQL=20=E5=87=BD=E6=95=B0=20?= =?UTF-8?q?=E4=BD=9C=E4=B8=BA=20=E8=A1=A8=E8=BE=BE=E5=BC=8F=20=E5=B7=A6?= =?UTF-8?q?=E4=BE=A7=20=E5=80=BC=20=E7=AD=89=E6=9D=A1=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/apijson/JSONObject.java | 10 +++ .../main/java/apijson/orm/AbstractParser.java | 1 + .../java/apijson/orm/AbstractSQLConfig.java | 71 ++++++++++++++++--- .../src/main/java/apijson/orm/SQLConfig.java | 3 + 4 files changed, 76 insertions(+), 9 deletions(-) diff --git a/APIJSONORM/src/main/java/apijson/JSONObject.java b/APIJSONORM/src/main/java/apijson/JSONObject.java index 5d507740..8aa8eac3 100755 --- a/APIJSONORM/src/main/java/apijson/JSONObject.java +++ b/APIJSONORM/src/main/java/apijson/JSONObject.java @@ -150,6 +150,7 @@ public JSONObject setUserIdIn(List 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 对象配置操作方法 @@ -181,6 +182,7 @@ public JSONObject setUserIdIn(List 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); @@ -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 diff --git a/APIJSONORM/src/main/java/apijson/orm/AbstractParser.java b/APIJSONORM/src/main/java/apijson/orm/AbstractParser.java index 81434a31..1a744108 100755 --- a/APIJSONORM/src/main/java/apijson/orm/AbstractParser.java +++ b/APIJSONORM/src/main/java/apijson/orm/AbstractParser.java @@ -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); } diff --git a/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java b/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java index 83661e3e..e95fb46f 100755 --- a/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java +++ b/APIJSONORM/src/main/java/apijson/orm/AbstractSQLConfig.java @@ -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; @@ -133,6 +134,10 @@ public abstract class AbstractSQLConfig implements SQLConfig TABLE_KEY_MAP; + /** + * 字段名映射,隐藏真实字段名,对安全要求很高的表可以这么做,另外可以配置 name_tag:(name,tag) 来实现多字段 IN,length_tag:length(tag) 来实现 SQL 函数复杂条件 + */ + public static Map COLUMN_KEY_MAP; /** * 允许批量增删改部分记录失败的表 */ @@ -907,6 +912,7 @@ public String getUserIdKey() { private String havingCombine; //聚合函数的字符串数组,','分隔 private Map having; //聚合函数的字符串数组,','分隔 private String order; //排序方式的字符串数组,','分隔 + private Map keyMap; //字段名映射,支持 name_tag:(name,tag) 多字段 IN,year:left(date,4) 截取日期年份等 private List raw; //需要保留原始 SQL 的字段,','分隔 private List json; //需要转为 JSON 的字段,','分隔 private Subquery from; //子查询临时表 @@ -1663,12 +1669,22 @@ public String getOrderString(boolean hasPrefix) { return (hasPrefix ? " ORDER BY " : "") + StringUtil.concat(StringUtil.getString(keys), joinOrder, ", "); } + @Override + public Map getKeyMap() { + return keyMap; + } + @Override + public AbstractSQLConfig setKeyMap(Map keyMap) { + this.keyMap = keyMap; + return this; + } + @Override public List getRaw() { return raw; } @Override - public SQLConfig setRaw(List raw) { + public AbstractSQLConfig setRaw(List raw) { this.raw = raw; return this; } @@ -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('('); @@ -3486,7 +3502,18 @@ public String getKey(String key) { return getSQLValue(key).toString(); } - return getSQLKey(key); + Map 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 raw = getRaw(); + return parseSQLExpression(KEY_KEY, expression, raw != null && raw.contains(KEY_KEY), false); } public String getSQLKey(String key) { String q = getQuote(); @@ -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); @@ -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); @@ -5563,6 +5592,27 @@ else if (newHaving != null) { } // @having, @haivng& >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> + if (keyMap instanceof Map) { + config.setKeyMap((Map) keyMap); + } + else if (keyMap instanceof String) { + String[] ks = StringUtil.split((String) keyMap, ";"); + if (ks.length > 0) { + Map nkm = new LinkedHashMap<>(); + for (int i = 0; i < ks.length; i++) { + Entry 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)); @@ -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); } diff --git a/APIJSONORM/src/main/java/apijson/orm/SQLConfig.java b/APIJSONORM/src/main/java/apijson/orm/SQLConfig.java index 4e47c9d1..a453d9a8 100755 --- a/APIJSONORM/src/main/java/apijson/orm/SQLConfig.java +++ b/APIJSONORM/src/main/java/apijson/orm/SQLConfig.java @@ -242,6 +242,9 @@ default int[] getDBVersionNums() { String getTablePath(); + Map getKeyMap(); + AbstractSQLConfig setKeyMap(Map keyMap); + List getRaw(); SQLConfig setRaw(List raw);