From de5565946a31558b34114f58bd0e34e1603c964d Mon Sep 17 00:00:00 2001 From: TommyLemon <1184482681@qq.com> Date: Sun, 29 Jan 2023 15:10:20 +0800 Subject: [PATCH 01/17] =?UTF-8?q?=E5=8D=87=E7=BA=A7=20APIJSON=206.0.0,=20?= =?UTF-8?q?=E8=87=AA=E8=BA=AB=201.5.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index e5816d0..f50a919 100755 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ apijson.column apijson-column - 1.2.9 + 1.5.0 jar apijson-column @@ -29,7 +29,7 @@ com.github.Tencent APIJSON - 5.4.0 + 6.0.0 From 0933b813907002c87d56ea4871423d44a1072be5 Mon Sep 17 00:00:00 2001 From: TommyLemon Date: Fri, 28 Apr 2023 04:06:02 +0800 Subject: [PATCH 02/17] =?UTF-8?q?=E5=8D=87=E7=BA=A7=20APIJSON=206.1.0?= =?UTF-8?q?=EF=BC=8C=E8=87=AA=E8=BA=AB=201.6.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index f50a919..7423be4 100755 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ apijson.column apijson-column - 1.5.0 + 1.6.0 jar apijson-column @@ -29,7 +29,7 @@ com.github.Tencent APIJSON - 6.0.0 + 6.1.0 @@ -40,6 +40,7 @@ org.apache.maven.plugins maven-compiler-plugin + 3.8.1 1.8 1.8 From 2a0c26380d8f054e32f3a31173e4e100ae70916b Mon Sep 17 00:00:00 2001 From: TommyLemon <1184482681@qq.com> Date: Sat, 3 Jun 2023 22:55:27 +0800 Subject: [PATCH 03/17] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E7=A4=BA=E4=BE=8B=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/APIJSON/apijson-column#3override-getkey-in-your-sqlexecutor-extends-abstractsqlexecutor --- README.md | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/README.md b/README.md index eaa3423..286ae35 100755 --- a/README.md +++ b/README.md @@ -130,6 +130,35 @@ https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONB #### See document in [ColumnUtil](/src/main/java/apijson/column/ColumnUtil.java) and [DemoSQLConfig](https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot/src/main/java/apijson/demo/DemoSQLConfig.java), [DemoSQLExecutor](https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot/src/main/java/apijson/demo/DemoSQLExecutor.java) in [APIJSONBoot](https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot) +```java + static { + // 反选字段配置 + Map> tableColumnMap = new HashMap<>(); + tableColumnMap.put("User", Arrays.asList(StringUtil.split("id,sex,name,tag,head,contactIdList,pictureList,date"))); + // 需要对应方法传参也是这样拼接才行,例如 ColumnUtil.compatInputColumn(column, getSQLDatabase() + "-" + getSQLSchema() + "-" + getTable(), getMethod()); + tableColumnMap.put("MYSQL-sys-Privacy", Arrays.asList(StringUtil.split("id,certified,phone,balance,_password,_payPassword"))); + ColumnUtil.VERSIONED_TABLE_COLUMN_MAP.put(null, tableColumnMap); + + // 字段名映射配置 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< + Map> tableKeyColumnMap = new HashMap<>(); + + Map userKeyColumnMap = new HashMap<>(); + userKeyColumnMap.put("gender", "sex"); + userKeyColumnMap.put("createTime", "date"); + tableKeyColumnMap.put("User", userKeyColumnMap); + + Map privacyKeyColumnMap = new HashMap<>(); + privacyKeyColumnMap.put("rest", "balance"); + // 需要对应方法传参也是这样拼接才行,例如 ColumnUtil.compatInputKey(super.getKey(key), getSQLDatabase() + "-" + getSQLSchema() + "-" + getTable(), getMethod()); + tableKeyColumnMap.put("MYSQL-sys-Privacy", privacyKeyColumnMap); + + ColumnUtil.VERSIONED_KEY_COLUMN_MAP.put(null, tableKeyColumnMap); + // 字段名映射配置 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> + + ColumnUtil.init(); + } +``` +


From 143765a8bb511c7d32169295b772c24515ed5104 Mon Sep 17 00:00:00 2001 From: TommyLemon Date: Sun, 4 Jun 2023 02:17:24 +0800 Subject: [PATCH 04/17] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=88=9D=E5=A7=8B?= =?UTF-8?q?=E5=8C=96=EF=BC=9B=E8=A7=A3=E5=86=B3=E7=89=88=E6=9C=AC=E6=9C=AA?= =?UTF-8?q?=E7=B2=BE=E5=87=86=E5=8C=B9=E9=85=8D=E5=AF=BC=E8=87=B4=E6=89=BE?= =?UTF-8?q?=E4=B8=8D=E5=88=B0=E5=AF=B9=E5=BA=94=E5=AD=97=E6=AE=B5=E6=98=A0?= =?UTF-8?q?=E5=B0=84=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/apijson/column/ColumnUtil.java | 201 +++++++++++++++---- 1 file changed, 164 insertions(+), 37 deletions(-) diff --git a/src/main/java/apijson/column/ColumnUtil.java b/src/main/java/apijson/column/ColumnUtil.java index 3508d46..4de6f86 100644 --- a/src/main/java/apijson/column/ColumnUtil.java +++ b/src/main/java/apijson/column/ColumnUtil.java @@ -14,12 +14,8 @@ package apijson.column; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.Map.Entry; -import java.util.Set; import apijson.RequestMethod; import apijson.StringUtil; @@ -37,21 +33,29 @@ public class ColumnUtil { /**带版本的表和字段一对多对应关系,用来做 反选字段 * Map> */ - public static final Map>> VERSIONED_TABLE_COLUMN_MAP; + public static SortedMap>> VERSIONED_TABLE_COLUMN_MAP; /**带版本的 JSON key 和表字段一对一对应关系,用来做字段名映射 * Map>> */ - public static final Map>> VERSIONED_KEY_COLUMN_MAP; + public static SortedMap>> VERSIONED_KEY_COLUMN_MAP; /**带版本的 JSON key 和表字段一对一对应关系,用来做字段名映射,与 VERSIONED_KEY_COLUMN_MAP 相反 * Map>> */ - private static Map>> VERSIONED_COLUMN_KEY_MAP; + private static SortedMap>> VERSIONED_COLUMN_KEY_MAP; + + public static final Comparator DESC_COMPARATOR = new Comparator() { + @Override + public int compare(Integer o1, Integer o2) { + return o2.compareTo(o1); + } + }; + static { - VERSIONED_TABLE_COLUMN_MAP = new HashMap<>(); - VERSIONED_KEY_COLUMN_MAP = new HashMap<>(); - VERSIONED_COLUMN_KEY_MAP = new HashMap<>(); + VERSIONED_TABLE_COLUMN_MAP = new TreeMap<>(DESC_COMPARATOR); + VERSIONED_KEY_COLUMN_MAP = new TreeMap<>(DESC_COMPARATOR); + VERSIONED_COLUMN_KEY_MAP = new TreeMap<>(DESC_COMPARATOR); } /**初始化 @@ -59,10 +63,11 @@ public class ColumnUtil { public static void init() { VERSIONED_COLUMN_KEY_MAP.clear(); + // 反过来补全 column -> key 的配置,以空间换时间 Set>>> set = VERSIONED_KEY_COLUMN_MAP.entrySet(); if (set != null && set.isEmpty() == false) { - Map>> map = new HashMap<>(); + SortedMap>> map = new TreeMap<>(DESC_COMPARATOR); for (Entry>> entry : set) { @@ -98,6 +103,74 @@ public static void init() { VERSIONED_COLUMN_KEY_MAP = map; } + + + // 补全剩下未定义别名的 key,以空间换时间 + Set>>> allSet = VERSIONED_TABLE_COLUMN_MAP.entrySet(); + if (allSet != null && allSet.isEmpty() == false) { + + for (Entry>> entry : allSet) { + Map> keyColumnMap = VERSIONED_KEY_COLUMN_MAP.get(entry.getKey()); + Map> columnKeyMap = VERSIONED_COLUMN_KEY_MAP.get(entry.getKey()); + if (keyColumnMap == null) { + keyColumnMap = new LinkedHashMap<>(); + VERSIONED_KEY_COLUMN_MAP.put(entry.getKey(), keyColumnMap); + } + if (columnKeyMap == null) { + columnKeyMap = new LinkedHashMap<>(); + VERSIONED_COLUMN_KEY_MAP.put(entry.getKey(), columnKeyMap); + } + + Map> tableKeyColumnMap = entry == null ? null : entry.getValue(); + Set>> tableKeyColumnSet = tableKeyColumnMap == null ? null : tableKeyColumnMap.entrySet(); + + if (tableKeyColumnSet != null && tableKeyColumnSet.isEmpty() == false) { + + for (Entry> tableKeyColumnEntry : tableKeyColumnSet) { + + List list = tableKeyColumnEntry == null ? null : tableKeyColumnEntry.getValue(); + + if (list != null && list.isEmpty() == false) { + + Map kcm = keyColumnMap.get(tableKeyColumnEntry.getKey()); + Map ckm = columnKeyMap.get(tableKeyColumnEntry.getKey()); + if (kcm == null) { + kcm = new LinkedHashMap<>(); + keyColumnMap.put(tableKeyColumnEntry.getKey(), kcm); + } + if (ckm == null) { + ckm = new LinkedHashMap<>(); + columnKeyMap.put(tableKeyColumnEntry.getKey(), ckm); + } + + for (String column : list) { + if (column == null) { + continue; + } + + ckm.putIfAbsent(column, column); + //FIXME 对 Comment.toId (多版本) 居然不起作用 +// if (kcm.containsValue(column) == false) { + kcm.putIfAbsent(column, column); +// } + } + +// for (String column : list) { +// if (column == null || ckm.get(column) != null) { +// continue; +// } +// +// kcm.putIfAbsent(column, column); +// } + + } + } + + } + } + + } + } /**适配请求参数 JSON 中 @column:value 的 value 中的 key。支持 !key 反选字段 和 字段名映射 @@ -118,15 +191,15 @@ public static List compatInputColumn(List columns, String table, * @return * @see 先提前配置 {@link #VERSIONED_TABLE_COLUMN_MAP},然后在 {@link AbstractSQLConfig} 的子类重写 {@link AbstractSQLConfig#setColumn } 并调用这个方法,例如 *
-	   public AbstractSQLConfig setColumn(List column) { 
- return super.setColumn(ColumnUtil.compatInputColumn(column, getTable(), version));
- } + public AbstractSQLConfig setColumn(List column) {
+ return super.setColumn(ColumnUtil.compatInputColumn(column, getTable(), version));
+ } *
*/ public static List compatInputColumn(List columns, String table, RequestMethod method, Integer version) { String[] keys = columns == null ? null : columns.toArray(new String[]{}); // StringUtil.split(c, ";"); - if (keys == null || keys.length <= 0) { - return columns; + if (keys == null || keys.length <= 0) { // JOIN 副表可以设置 @column:"" 来指定不返回字段 + return columns != null ? columns : getClosestValue(VERSIONED_TABLE_COLUMN_MAP, version, table); } // boolean isQueryMethod = RequestMethod.isQueryMethod(method); @@ -134,8 +207,7 @@ public static List compatInputColumn(List columns, String table, List exceptColumns = new ArrayList<>(); // Map exceptColumnMap = new HashMap<>(); List newColumns = new ArrayList<>(); - Map> tableKeyColumnMap = VERSIONED_KEY_COLUMN_MAP == null || VERSIONED_KEY_COLUMN_MAP.isEmpty() ? null : VERSIONED_KEY_COLUMN_MAP.get(version); - Map keyColumnMap = tableKeyColumnMap == null || tableKeyColumnMap.isEmpty() ? null : tableKeyColumnMap.get(table); + Map keyColumnMap = getClosestValue(VERSIONED_KEY_COLUMN_MAP, version, table); String expression; //...;fun0(arg0,arg1,...):fun0;fun1(arg0,arg1,...):fun1;... @@ -164,8 +236,7 @@ public static List compatInputColumn(List columns, String table, String rc = keyColumnMap == null || keyColumnMap.isEmpty() ? null : keyColumnMap.get(c); exceptColumns.add(rc == null ? c : rc); // 不使用数据库别名,以免 JOIN 等复杂查询情况下报错字段不存在 exceptColumnMap.put(nc == null ? c : nc, c); // column:alias - } - else { + } else { String rc = keyColumnMap == null || keyColumnMap.isEmpty() ? null : keyColumnMap.get(ck); newColumns.add(rc == null ? ck : rc); // 不使用数据库别名,以免 JOIN 等复杂查询情况下报错字段不存在 newColumns.add(rc == null ? ck : (isQueryMethod ? (rc + ":" + ck) : rc)); } @@ -202,8 +273,9 @@ public static List compatInputColumn(List columns, String table, * @return */ public static String compatInputKey(String key, String table, RequestMethod method) { - return compatInputKey(key, table, method, null); + return compatInputKey(key, table, method, null, false); } + /**适配请求参数 JSON 中 条件/赋值 键值对的 key * @param key * @param table @@ -212,18 +284,25 @@ public static String compatInputKey(String key, String table, RequestMethod meth * @return * @see 先提前配置 {@link #VERSIONED_KEY_COLUMN_MAP},然后在 {@link AbstractSQLConfig} 的子类重写 {@link AbstractSQLConfig#getKey } 并调用这个方法,例如 *
-	   public String getKey(String key) { 
- return super.getKey(ColumnUtil.compatInputKey(key, getTable(), version));
- } + public String getKey(String key) {
+ return super.getKey(ColumnUtil.compatInputKey(key, getTable(), version));
+ } *
*/ - public static String compatInputKey(String key, String table, RequestMethod method, Integer version) { - Map> tableKeyColumnMap = VERSIONED_KEY_COLUMN_MAP == null || VERSIONED_KEY_COLUMN_MAP.isEmpty() ? null : VERSIONED_KEY_COLUMN_MAP.get(version); - Map keyColumnMap = tableKeyColumnMap == null || tableKeyColumnMap.isEmpty() ? null : tableKeyColumnMap.get(table); - String alias = keyColumnMap == null || keyColumnMap.isEmpty() ? null : keyColumnMap.get(key); - return alias == null ? key : alias; + public static String compatInputKey(String key, String table, RequestMethod method, Integer version, boolean throwWhenNoKey) { + Map keyColumnMap = getClosestValue(VERSIONED_KEY_COLUMN_MAP, version, table); + boolean isEmpty = keyColumnMap == null || keyColumnMap.isEmpty(); + String alias = isEmpty ? null : keyColumnMap.get(key); + if (alias == null) { + if (isEmpty == false && throwWhenNoKey) { + throw new NullPointerException(table + ":{} 中不允许传 " + key + " !"); + } + return key; + } + + return alias; } - + /**适配返回结果 JSON 中键值对的 key。可能通过不传 @column 等方式来返回原始字段名,这样就达不到隐藏真实字段名的需求了,所以只有最终这个兜底方式靠谱。 * @param key * @param table @@ -233,6 +312,7 @@ public static String compatInputKey(String key, String table, RequestMethod meth public static String compatOutputKey(String key, String table, RequestMethod method) { return compatOutputKey(key, table, method, null); } + /**适配返回结果 JSON 中键值对的 key。可能通过不传 @column 等方式来返回原始字段名,这样就达不到隐藏真实字段名的需求了,所以只有最终这个兜底方式靠谱。 * @param key * @param table @@ -241,19 +321,66 @@ public static String compatOutputKey(String key, String table, RequestMethod met * @return * @see 先提前配置 {@link #VERSIONED_COLUMN_KEY_MAP},然后在 {@link AbstractSQLExecutor} 的子类重写 {@link AbstractSQLExecutor#getKey } 并调用这个方法,例如 *
-		protected String getKey(SQLConfig config, ResultSet rs, ResultSetMetaData rsmd, int tablePosition, JSONObject table,
-				int columnIndex, Map childMap) throws Exception { 
- return ColumnUtil.compatOutputKey(super.getKey(config, rs, rsmd, tablePosition, table, columnIndex, childMap), config.getTable(), config.getMethod(), version);
- } + protected String getKey(SQLConfig config, ResultSet rs, ResultSetMetaData rsmd, int tablePosition, JSONObject table, + int columnIndex, Map childMap) throws Exception {
+ return ColumnUtil.compatOutputKey(super.getKey(config, rs, rsmd, tablePosition, table, columnIndex, childMap), config.getTable(), config.getMethod(), version);
+ } *
*/ public static String compatOutputKey(String key, String table, RequestMethod method, Integer version) { - Map> tableColumnKeyMap = VERSIONED_COLUMN_KEY_MAP == null || VERSIONED_COLUMN_KEY_MAP.isEmpty() ? null : VERSIONED_COLUMN_KEY_MAP.get(version); - Map columnKeyMap = tableColumnKeyMap == null || tableColumnKeyMap.isEmpty() ? null : tableColumnKeyMap.get(table); + Map columnKeyMap = getClosestValue(VERSIONED_COLUMN_KEY_MAP, version, table); String alias = columnKeyMap == null || columnKeyMap.isEmpty() ? null : columnKeyMap.get(key); return alias == null ? key : alias; } + public static T getClosestValue(SortedMap> versionedMap, Integer version, String table) { + boolean isEmpty = versionedMap == null || versionedMap.isEmpty(); + + Map map = isEmpty || version == null ? null : versionedMap.get(version); + T m = map == null ? null : map.get(table); + if (isEmpty == false && m == null) { + Set>> set = versionedMap.entrySet(); + + T lm = null; + for (Entry> entry : set) { + Map val = entry.getValue(); + m = val == null ? null : val.get(table); + if (m == null) { + continue; + } + + if (version == null || version == 0) { + // versionedMap.put(null, val); + return m; + } + + Integer key = entry.getKey(); + if (key == null) { + lm = m; + map = val; + continue; + } + + if (version >= key) { + versionedMap.put(version, val); + return m; + } + + break; + } + + if (lm != null) { + m = lm; + } + + if (map != null) { + versionedMap.put(version, map); + } + } + + return m; + } + /**把多个表名相关属性拼接成一个表名 * @param database From 89079f97936dbb15498025affc578a23ebc67d03 Mon Sep 17 00:00:00 2001 From: TommyLemon Date: Sun, 4 Jun 2023 05:06:49 +0800 Subject: [PATCH 05/17] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E6=9C=AA=E7=B2=BE=E5=87=86=E5=8C=B9=E9=85=8D=E6=97=B6=20!key?= =?UTF-8?q?=20=E5=8F=8D=E9=80=89=E6=97=A0=E6=95=88=EF=BC=9B=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/apijson/column/ColumnUtil.java | 25 ++++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/main/java/apijson/column/ColumnUtil.java b/src/main/java/apijson/column/ColumnUtil.java index 4de6f86..d19da44 100644 --- a/src/main/java/apijson/column/ColumnUtil.java +++ b/src/main/java/apijson/column/ColumnUtil.java @@ -111,15 +111,15 @@ public static void init() { for (Entry>> entry : allSet) { Map> keyColumnMap = VERSIONED_KEY_COLUMN_MAP.get(entry.getKey()); - Map> columnKeyMap = VERSIONED_COLUMN_KEY_MAP.get(entry.getKey()); +// 没必要,没特殊配置的就原样返回,没有安全隐患,还能减少性能浪费 Map> columnKeyMap = VERSIONED_COLUMN_KEY_MAP.get(entry.getKey()); if (keyColumnMap == null) { keyColumnMap = new LinkedHashMap<>(); VERSIONED_KEY_COLUMN_MAP.put(entry.getKey(), keyColumnMap); } - if (columnKeyMap == null) { - columnKeyMap = new LinkedHashMap<>(); - VERSIONED_COLUMN_KEY_MAP.put(entry.getKey(), columnKeyMap); - } +// if (columnKeyMap == null) { +// columnKeyMap = new LinkedHashMap<>(); +// VERSIONED_COLUMN_KEY_MAP.put(entry.getKey(), columnKeyMap); +// } Map> tableKeyColumnMap = entry == null ? null : entry.getValue(); Set>> tableKeyColumnSet = tableKeyColumnMap == null ? null : tableKeyColumnMap.entrySet(); @@ -133,22 +133,22 @@ public static void init() { if (list != null && list.isEmpty() == false) { Map kcm = keyColumnMap.get(tableKeyColumnEntry.getKey()); - Map ckm = columnKeyMap.get(tableKeyColumnEntry.getKey()); +// Map ckm = columnKeyMap.get(tableKeyColumnEntry.getKey()); if (kcm == null) { kcm = new LinkedHashMap<>(); keyColumnMap.put(tableKeyColumnEntry.getKey(), kcm); } - if (ckm == null) { - ckm = new LinkedHashMap<>(); - columnKeyMap.put(tableKeyColumnEntry.getKey(), ckm); - } +// if (ckm == null) { +// ckm = new LinkedHashMap<>(); +// columnKeyMap.put(tableKeyColumnEntry.getKey(), ckm); +// } for (String column : list) { if (column == null) { continue; } - ckm.putIfAbsent(column, column); +// ckm.putIfAbsent(column, column); //FIXME 对 Comment.toId (多版本) 居然不起作用 // if (kcm.containsValue(column) == false) { kcm.putIfAbsent(column, column); @@ -245,8 +245,7 @@ public static List compatInputColumn(List columns, String table, } boolean isEmpty = exceptColumns == null || exceptColumns.isEmpty(); // exceptColumnMap == null || exceptColumnMap.isEmpty(); - Map> map = isEmpty || VERSIONED_TABLE_COLUMN_MAP == null || VERSIONED_TABLE_COLUMN_MAP.isEmpty() ? null : VERSIONED_TABLE_COLUMN_MAP.get(version); - List allColumns = map == null || map.isEmpty() ? null : map.get(table); + List allColumns = isEmpty ? null : getClosestValue(VERSIONED_TABLE_COLUMN_MAP, version, table); if (allColumns != null && allColumns.isEmpty() == false) { From 00f79612c6792885d7b093fc91699c78a0574e4e Mon Sep 17 00:00:00 2001 From: TommyLemon Date: Sun, 4 Jun 2023 06:29:31 +0800 Subject: [PATCH 06/17] =?UTF-8?q?=E8=A7=A3=E5=86=B3=20@column:"`toId`;(par?= =?UTF-8?q?entId)"=20=E7=AD=89=E5=86=99=E6=B3=95=E5=8F=AF=E8=83=BD?= =?UTF-8?q?=E7=BB=95=E8=BF=87=E5=AD=97=E6=AE=B5=E6=8F=92=E4=BB=B6=E7=9A=84?= =?UTF-8?q?=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/apijson/column/ColumnUtil.java | 69 +++++++++++++++++--- 1 file changed, 59 insertions(+), 10 deletions(-) diff --git a/src/main/java/apijson/column/ColumnUtil.java b/src/main/java/apijson/column/ColumnUtil.java index d19da44..2bc7ed6 100644 --- a/src/main/java/apijson/column/ColumnUtil.java +++ b/src/main/java/apijson/column/ColumnUtil.java @@ -180,7 +180,7 @@ public static void init() { * @return */ public static List compatInputColumn(List columns, String table, RequestMethod method) { - return compatInputColumn(columns, table, method, null); + return compatInputColumn(columns, table, method, null, false); } /**适配请求参数 JSON 中 @column:value 的 value 中的 key。支持 !key 反选字段 和 字段名映射 @@ -196,7 +196,7 @@ public AbstractSQLConfig setColumn(List column) {
} * */ - public static List compatInputColumn(List columns, String table, RequestMethod method, Integer version) { + public static List compatInputColumn(List columns, String table, RequestMethod method, Integer version, boolean throwWhenNoKey) { String[] keys = columns == null ? null : columns.toArray(new String[]{}); // StringUtil.split(c, ";"); if (keys == null || keys.length <= 0) { // JOIN 副表可以设置 @column:"" 来指定不返回字段 return columns != null ? columns : getClosestValue(VERSIONED_TABLE_COLUMN_MAP, version, table); @@ -208,6 +208,9 @@ public static List compatInputColumn(List columns, String table, List newColumns = new ArrayList<>(); Map keyColumnMap = getClosestValue(VERSIONED_KEY_COLUMN_MAP, version, table); + boolean isEmpty = keyColumnMap == null || keyColumnMap.isEmpty(); + + String q = "`"; String expression; //...;fun0(arg0,arg1,...):fun0;fun1(arg0,arg1,...):fun1;... @@ -215,8 +218,37 @@ public static List compatInputColumn(List columns, String table, //!column,column2,!column3,column4:alias4;fun(arg0,arg1,...) expression = keys[i]; - if (expression.contains("(") || expression.contains(")")) { - newColumns.add(expression); + int start = expression.indexOf("("); + int end = expression.lastIndexOf(")"); + if (start >= 0 && start < end) { + String[] ks = StringUtil.split(expression.substring(start + 1, end)); + + String expr = expression.substring(0, start + 1); + for (int j = 0; j < ks.length; j++) { + String ck = ks[j]; + boolean hasQuote = false; + if (ck.endsWith("`")) { + String nck = ck.substring(0, ck.length() - 1); + if (nck.lastIndexOf("`") == 0) { + ck = nck.substring(1); + hasQuote = true; + } + } + + String rc = null; + if (hasQuote || StringUtil.isName(ck)) { + rc = isEmpty ? null : keyColumnMap.get(ck); + if (rc == null && isEmpty == false && throwWhenNoKey) { + throw new NullPointerException(table + ":{ @column: value } 的 value 中 " + ck + " 不合法!不允许传后端未授权访问的字段名!"); + } + } + + expr += (j <= 0 ? "" : ",") + (hasQuote ? q : "") + (rc == null ? ck : rc) + (hasQuote ? q : ""); + } + + newColumns.add(expr + expression.substring(end)); + +// newColumns.add(expression); continue; } @@ -227,25 +259,42 @@ public static List compatInputColumn(List columns, String table, if (ck.startsWith("!")) { if (ck.length() <= 1) { - throw new IllegalArgumentException("@column:value 的 value 中 " + ck + " 不合法! !column 不允许 column 为空字符串!column,!column2,!column3,column4:alias4 中所有 column 必须符合变量名格式!"); + throw new IllegalArgumentException("@column:value 的 value 中 " + ck + + " 不合法! !column 不允许 column 为空字符串!column,!column2,!column3,column4:alias4 中所有 column 必须符合变量名格式!"); } String c = ck.substring(1); if (StringUtil.isName(c) == false) { - throw new IllegalArgumentException("@column:value 的 value 中 " + c + " 不合法! column,!column2,!column3,column4:alias4 中所有 column 必须符合变量名格式!"); + throw new IllegalArgumentException("@column:value 的 value 中 " + c + + " 不合法! column,!column2,!column3,column4:alias4 中所有 column 必须符合变量名格式!"); } - String rc = keyColumnMap == null || keyColumnMap.isEmpty() ? null : keyColumnMap.get(c); + String rc = isEmpty ? null : keyColumnMap.get(c); exceptColumns.add(rc == null ? c : rc); // 不使用数据库别名,以免 JOIN 等复杂查询情况下报错字段不存在 exceptColumnMap.put(nc == null ? c : nc, c); // column:alias } else { - String rc = keyColumnMap == null || keyColumnMap.isEmpty() ? null : keyColumnMap.get(ck); + boolean hasQuote = false; + if (ck.endsWith("`")) { + String nck = ck.substring(0, ck.length() - 1); + if (nck.lastIndexOf("`") == 0) { + ck = nck.substring(1); + hasQuote = true; + } + } + + String rc = null; + if (hasQuote || StringUtil.isName(ck)) { + rc = isEmpty ? null : keyColumnMap.get(ck); + if (rc == null && isEmpty == false && throwWhenNoKey) { + throw new NullPointerException(table + ":{ @column: value } 的 value 中 " + ck + " 不合法!不允许传后端未授权访问的字段名!"); + } + } + newColumns.add(rc == null ? ck : rc); // 不使用数据库别名,以免 JOIN 等复杂查询情况下报错字段不存在 newColumns.add(rc == null ? ck : (isQueryMethod ? (rc + ":" + ck) : rc)); } } } } - boolean isEmpty = exceptColumns == null || exceptColumns.isEmpty(); // exceptColumnMap == null || exceptColumnMap.isEmpty(); - List allColumns = isEmpty ? null : getClosestValue(VERSIONED_TABLE_COLUMN_MAP, version, table); + List allColumns = exceptColumns == null || exceptColumns.isEmpty() ? null : getClosestValue(VERSIONED_TABLE_COLUMN_MAP, version, table); if (allColumns != null && allColumns.isEmpty() == false) { From af44a0a9c2f76771caa79de6d38dc528e31cdb80 Mon Sep 17 00:00:00 2001 From: TommyLemon Date: Wed, 9 Aug 2023 23:08:08 +0800 Subject: [PATCH 07/17] =?UTF-8?q?=E5=8D=87=E7=BA=A7=20APIJSON,=20=E8=87=AA?= =?UTF-8?q?=E8=BA=AB=20=E7=89=88=E6=9C=AC=E5=88=86=E5=88=AB=E4=B8=BA=206.2?= =?UTF-8?q?.0,=201.7.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 7423be4..95c3008 100755 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ apijson.column apijson-column - 1.6.0 + 1.7.0 jar apijson-column @@ -29,7 +29,7 @@ com.github.Tencent APIJSON - 6.1.0 + 6.2.0 From 77c5bc7d728403c3929ce7038cccb361431218ca Mon Sep 17 00:00:00 2001 From: TommyLemon Date: Tue, 12 Dec 2023 23:19:01 +0800 Subject: [PATCH 08/17] =?UTF-8?q?=E5=8D=87=E7=BA=A7=20APIJSON=206.3.0?= =?UTF-8?q?=EF=BC=8C=E8=87=AA=E8=BA=AB=201.8.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 95c3008..6be9863 100755 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ apijson.column apijson-column - 1.7.0 + 1.8.0 jar apijson-column @@ -29,7 +29,7 @@ com.github.Tencent APIJSON - 6.2.0 + 6.3.0 From 10196231307d0679435277b0ddc998803b563e1a Mon Sep 17 00:00:00 2001 From: TommyLemon <1184482681@qq.com> Date: Tue, 12 Mar 2024 20:26:30 +0800 Subject: [PATCH 09/17] Create jitpack.yml --- jitpack.yml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 jitpack.yml diff --git a/jitpack.yml b/jitpack.yml new file mode 100644 index 0000000..d518397 --- /dev/null +++ b/jitpack.yml @@ -0,0 +1,6 @@ +jdk: + - openjdk17 + +before_install: + - sdk install java 17.0.6-open + - sdk use java 17.0.6-open From d9a101bf6d37f94c03d821b8c665380689df6913 Mon Sep 17 00:00:00 2001 From: TommyLemon <1184482681@qq.com> Date: Tue, 12 Mar 2024 20:29:40 +0800 Subject: [PATCH 10/17] =?UTF-8?q?=E5=8D=87=E7=BA=A7=20APIJSON=206.4.3-spri?= =?UTF-8?q?ngboot3=20=E5=8F=8A=E8=87=AA=E8=BA=AB=201.9.0-springboot3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 6be9863..133ba5a 100755 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ apijson.column apijson-column - 1.8.0 + 1.9.0-springboot3 jar apijson-column @@ -29,7 +29,7 @@ com.github.Tencent APIJSON - 6.3.0 + 6.4.3-springboot3 From 51ce9ead176595693637889c3efd87d79e2636f0 Mon Sep 17 00:00:00 2001 From: TommyLemon <1184482681@qq.com> Date: Tue, 12 Mar 2024 20:34:41 +0800 Subject: [PATCH 11/17] =?UTF-8?q?=E5=8D=87=E7=BA=A7=20maven-compiler-plugi?= =?UTF-8?q?n=203.12.1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 133ba5a..8896b0e 100755 --- a/pom.xml +++ b/pom.xml @@ -40,7 +40,7 @@ org.apache.maven.plugins maven-compiler-plugin - 3.8.1 + 3.12.1 1.8 1.8 From caef09c5ca704dc1a352694f94da9a250ba14e42 Mon Sep 17 00:00:00 2001 From: TommyLemon <1184482681@qq.com> Date: Tue, 12 Mar 2024 20:49:41 +0800 Subject: [PATCH 12/17] =?UTF-8?q?=E5=8D=87=E7=BA=A7=20Java=2017?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 8896b0e..0567a26 100755 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,10 @@ UTF-8 UTF-8 - 1.8 + 17 + UTF-8 + 17 + 17 @@ -42,8 +45,8 @@ maven-compiler-plugin 3.12.1 - 1.8 - 1.8 + 17 + 17 From b6ce613e8c9610345f1e10b1fa6382d9442a4a8e Mon Sep 17 00:00:00 2001 From: TommyLemon <1184482681@qq.com> Date: Tue, 12 Mar 2024 20:59:35 +0800 Subject: [PATCH 13/17] Update pom.xml --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 0567a26..5f09dff 100755 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ apijson.column apijson-column - 1.9.0-springboot3 + 1.9.2-springboot3 jar apijson-column From 0956f76f8140e3d95957c76c7e8d8aa2cf5cd469 Mon Sep 17 00:00:00 2001 From: TommyLemon Date: Sun, 19 May 2024 18:04:40 +0800 Subject: [PATCH 14/17] =?UTF-8?q?=E5=8D=87=E7=BA=A7=20APIJSON=207.0.0?= =?UTF-8?q?=EF=BC=8CJDK=2017=EF=BC=8C=E8=87=AA=E8=BA=AB=202.0.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 5f09dff..55132f4 100755 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ apijson.column apijson-column - 1.9.2-springboot3 + 2.0.0 jar apijson-column @@ -32,7 +32,7 @@ com.github.Tencent APIJSON - 6.4.3-springboot3 + 7.0.0 From 7666ff6a62315a4131a15972745521098af521fc Mon Sep 17 00:00:00 2001 From: TommyLemon <1184482681@qq.com> Date: Sun, 30 Jun 2024 23:31:48 +0800 Subject: [PATCH 15/17] =?UTF-8?q?=E5=8D=87=E7=BA=A7=20APIJSON=207.0.3?= =?UTF-8?q?=EF=BC=8C=E8=87=AA=E8=BA=AB=202.0.3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 55132f4..6b768cd 100755 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ apijson.column apijson-column - 2.0.0 + 2.0.3 jar apijson-column @@ -32,7 +32,7 @@ com.github.Tencent APIJSON - 7.0.0 + 7.0.3 From bf8dc2b29290d657c6c05aa34165ff3ec12c46b2 Mon Sep 17 00:00:00 2001 From: TommyLemon <1184482681@qq.com> Date: Fri, 31 Jan 2025 17:36:30 +0800 Subject: [PATCH 16/17] =?UTF-8?q?=E5=8D=87=E7=BA=A7=20APIJSON=207.1.0?= =?UTF-8?q?=EF=BC=8C=E8=87=AA=E8=BA=AB=202.1.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 6b768cd..326bbb3 100755 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ apijson.column apijson-column - 2.0.3 + 2.1.0 jar apijson-column @@ -32,7 +32,7 @@ com.github.Tencent APIJSON - 7.0.3 + 7.1.0 From 516c31937bb7bf0cbd5bebc832f3cacc4923ea2f Mon Sep 17 00:00:00 2001 From: TommyLemon Date: Sat, 15 Feb 2025 23:54:50 +0800 Subject: [PATCH 17/17] =?UTF-8?q?=E6=89=93=E5=8C=85=E7=94=A8=E7=9A=84=20JD?= =?UTF-8?q?K=2017=20=E6=94=B9=E4=B8=BA=201.8=EF=BC=8C=E5=85=BC=E5=AE=B9?= =?UTF-8?q?=E4=BD=8E=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- jitpack.yml | 6 ------ pom.xml | 12 +++++------- 2 files changed, 5 insertions(+), 13 deletions(-) delete mode 100644 jitpack.yml diff --git a/jitpack.yml b/jitpack.yml deleted file mode 100644 index d518397..0000000 --- a/jitpack.yml +++ /dev/null @@ -1,6 +0,0 @@ -jdk: - - openjdk17 - -before_install: - - sdk install java 17.0.6-open - - sdk use java 17.0.6-open diff --git a/pom.xml b/pom.xml index 326bbb3..499174a 100755 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ apijson.column apijson-column - 2.1.0 + 2.1.5 jar apijson-column @@ -14,10 +14,8 @@ UTF-8 UTF-8 - 17 + 1.8 UTF-8 - 17 - 17 @@ -32,7 +30,7 @@ com.github.Tencent APIJSON - 7.1.0 + 7.5.5 @@ -45,8 +43,8 @@ maven-compiler-plugin 3.12.1 - 17 - 17 + 1.8 + 1.8