├── .gitignore ├── LICENSE ├── README.md ├── pom.xml └── src └── main └── java └── apijson └── column └── ColumnUtil.java /.gitignore: -------------------------------------------------------------------------------- 1 | /target/ 2 | .classpath 3 | .project 4 | .settings 5 | .idea 6 | *.iml 7 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright ©2021 APIJSON(https://github.com/APIJSON) 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # apijson-column [![](https://jitpack.io/v/APIJSON/apijson-column.svg)](https://jitpack.io/#APIJSON/apijson-column) 2 | 腾讯 [APIJSON](https://github.com/Tencent/APIJSON) 4.6.6+ 的字段插件,支持 !key 反选字段 和 字段名映射,可通过 Maven, Gradle 等远程依赖。
3 | A column plugin for Tencent [APIJSON](https://github.com/Tencent/APIJSON) 4.6.6+ , support Column Inverse and Column Mapping. 4 | 5 | ![image](https://user-images.githubusercontent.com/5738175/113572899-ab903380-964b-11eb-9f3c-69f3437d8a54.png) 6 | 7 | ![image](https://user-images.githubusercontent.com/5738175/113572926-b77bf580-964b-11eb-8a17-10917669c2aa.png) 8 | 9 | ## 添加依赖 10 | ## Add Dependency 11 | 12 | ### Maven 13 | #### 1. 在 pom.xml 中添加 JitPack 仓库 14 | #### 1. Add the JitPack repository to pom.xml 15 | ```xml 16 | 17 | 18 | jitpack.io 19 | https://jitpack.io 20 | 21 | 22 | ``` 23 | 24 | ![image](https://user-images.githubusercontent.com/5738175/167261814-d75d8fff-0e64-4534-a840-60ef628a8873.png) 25 | 26 |
27 | 28 | #### 2. 在 pom.xml 中添加 apijson-column 依赖 29 | #### 2. Add the apijson-column dependency to pom.xml 30 | ```xml 31 | 32 | com.github.APIJSON 33 | apijson-column 34 | LATEST 35 | 36 | ``` 37 | 38 | ![image](https://user-images.githubusercontent.com/5738175/167261792-7635c4b6-83a4-4d37-b0e5-e8455fdbed62.png) 39 | 40 |
41 | 42 | https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot/pom.xml 43 | 44 |
45 |
46 | 47 | ### Gradle 48 | #### 1. 在项目根目录 build.gradle 中最后添加 JitPack 仓库 49 | #### 1. Add the JitPack repository in your root build.gradle at the end of repositories 50 | ```gradle 51 | allprojects { 52 | repositories { 53 | maven { url 'https://jitpack.io' } 54 | } 55 | } 56 | ``` 57 |
58 | 59 | #### 2. 在项目某个 module 目录(例如 `app`) build.gradle 中添加 apijson-column 依赖 60 | #### 2. Add the apijson-column dependency in one of your modules(such as `app`) 61 | ```gradle 62 | dependencies { 63 | implementation 'com.github.APIJSON:apijson-column:latest' 64 | } 65 | ``` 66 | 67 |
68 |
69 |
70 | 71 | ## 初始化 72 | ## Initialization 73 | 74 | #### 1.在你项目继承 AbstractSQLConfig 的子类 static {} 代码块配置映射关系 75 | #### 1.Configure mappings in static {} of your SQLConfig extends AbstractSQLConfig 76 | ```java 77 | static { 78 | Map> tableColumnMap = new HashMap<>(); 79 | tableColumnMap.put("User", Arrays.asList(StringUtil.split("id,sex,name,tag,head,contactIdList,pictureList,date"))); 80 | ColumnUtil.VERSIONED_TABLE_COLUMN_MAP.put(null, tableColumnMap); 81 | 82 | Map userKeyColumnMap = new HashMap<>(); 83 | userKeyColumnMap.put("gender", "sex"); // gender -> sex 84 | 85 | Map> keyColumnMap = new HashMap<>(); 86 | keyColumnMap.put("User", userKeyColumnMap); 87 | 88 | ColumnUtil.VERSIONED_KEY_COLUMN_MAP.put(null, keyColumnMap); 89 | 90 | ColumnUtil.init(); 91 | } 92 | ``` 93 | 94 | ![image](https://user-images.githubusercontent.com/5738175/167261660-f22a65a1-41ec-41c2-a97e-f4e809a3ddc9.png) 95 | 96 |
97 | 98 | #### 2.在你项目继承 AbstractSQLConfig 的子类重写方法 setColumn, getKey 99 | #### 2.Override setColumn, getKey in your SQLConfig extends AbstractSQLConfig 100 | ```java 101 | @Override 102 | public AbstractSQLConfig setColumn(List column) { 103 | return super.setColumn(ColumnUtil.compatInputColumn(column, getTable(), getMethod())); 104 | } 105 | @Override 106 | public String getKey(String key) { 107 | return super.getKey(ColumnUtil.compatInputKey(key, getTable(), getMethod())); 108 | } 109 | ``` 110 | 111 | ![image](https://user-images.githubusercontent.com/5738175/167261697-48d54c3f-2913-4e07-8e41-80058688ac8b.png) 112 | 113 |
114 | 115 | #### 3.在你项目继承 AbstractSQLExecutor 的子类重写方法 getKey 116 | #### 3.Override getKey in your SQLExecutor extends AbstractSQLExecutor 117 | ```java 118 | @Override 119 | protected String getKey(SQLConfig config, ResultSet rs, ResultSetMetaData rsmd, int tablePosition, JSONObject table, 120 | int columnIndex, Map childMap) throws Exception { 121 | return ColumnUtil.compatOutputKey(super.getKey(config, rs, rsmd, tablePosition, table, columnIndex, childMap), config.getTable(), config.getMethod()); 122 | } 123 | ``` 124 | 125 | ![image](https://user-images.githubusercontent.com/5738175/167261741-7d9436bc-bd12-447c-bfa5-20631497164f.png) 126 | 127 |

128 | 129 | #### 见 [ColumnUtil](/src/main/java/apijson/column/ColumnUtil.java) 的注释及 [APIJSONBoot](https://github.com/APIJSON/APIJSON-Demo/blob/master/APIJSON-Java-Server/APIJSONBoot) 的 [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)
130 | 131 | #### 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) 132 | 133 | ```java 134 | static { 135 | // 反选字段配置 136 | Map> tableColumnMap = new HashMap<>(); 137 | tableColumnMap.put("User", Arrays.asList(StringUtil.split("id,sex,name,tag,head,contactIdList,pictureList,date"))); 138 | // 需要对应方法传参也是这样拼接才行,例如 ColumnUtil.compatInputColumn(column, getSQLDatabase() + "-" + getSQLSchema() + "-" + getTable(), getMethod()); 139 | tableColumnMap.put("MYSQL-sys-Privacy", Arrays.asList(StringUtil.split("id,certified,phone,balance,_password,_payPassword"))); 140 | ColumnUtil.VERSIONED_TABLE_COLUMN_MAP.put(null, tableColumnMap); 141 | 142 | // 字段名映射配置 <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 143 | Map> tableKeyColumnMap = new HashMap<>(); 144 | 145 | Map userKeyColumnMap = new HashMap<>(); 146 | userKeyColumnMap.put("gender", "sex"); 147 | userKeyColumnMap.put("createTime", "date"); 148 | tableKeyColumnMap.put("User", userKeyColumnMap); 149 | 150 | Map privacyKeyColumnMap = new HashMap<>(); 151 | privacyKeyColumnMap.put("rest", "balance"); 152 | // 需要对应方法传参也是这样拼接才行,例如 ColumnUtil.compatInputKey(super.getKey(key), getSQLDatabase() + "-" + getSQLSchema() + "-" + getTable(), getMethod()); 153 | tableKeyColumnMap.put("MYSQL-sys-Privacy", privacyKeyColumnMap); 154 | 155 | ColumnUtil.VERSIONED_KEY_COLUMN_MAP.put(null, tableKeyColumnMap); 156 | // 字段名映射配置 >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> 157 | 158 | ColumnUtil.init(); 159 | } 160 | ``` 161 | 162 |
163 |
164 |
165 | 166 | ## 使用 167 | ## Usage 168 | 169 | ### 1.反选字段 170 | ### 1.Column Inverse 171 | "@column": "!columnKey" // 返回排除 columnKey 后的全部其它字段
172 | "@column": "!columnKey" // return all columns except for columnKey 173 | ```js 174 | { 175 | "User": { // id,sex,name,tag,head,contactIdList,pictureList,date 176 | "id": 82001, 177 | "@column": "!contactIdList" // -> id,sex,name,tag,head,pictureList,date 178 | } 179 | } 180 | ``` 181 | 182 | ![image](https://user-images.githubusercontent.com/5738175/113572899-ab903380-964b-11eb-9f3c-69f3437d8a54.png) 183 | 184 | 185 | ### 2.字段名映射 186 | ### 2.Column Mapping 187 | "@column": "showKey" // 隐藏了数据库的对应真实字段名
188 | "@column": "showKey" // the real column name is hidden 189 | ```js 190 | { 191 | "User": { // id,sex,name,tag,head,contactIdList,pictureList,date 192 | "id": 82001, 193 | "@column": "gender" // -> sex 194 | } 195 | } 196 | ``` 197 | 198 | ![image](https://user-images.githubusercontent.com/5738175/113572926-b77bf580-964b-11eb-8a17-10917669c2aa.png) 199 | 200 | 注意:[APIAuto](https://github.com/TommyLemon/APIAuto) 不能自动获取并展示对应映射字段 showKey 的类型、长度、注释等文档,只能通过手写注释来实现
201 | Note: [APIAuto](https://github.com/TommyLemon/APIAuto) cannot automatically get and show the document for the showKey, you can add comment manually. 202 | 203 |
204 | 有问题可以去 Tencent/APIJSON 提 issue
205 | https://github.com/Tencent/APIJSON/issues/36 206 | 207 |

208 | 209 | #### 点右上角 ⭐Star 支持一下,谢谢 ^_^ 210 | #### Please ⭐Star this project ^_^ 211 | https://github.com/APIJSON/apijson-column 212 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | apijson.column 7 | apijson-column 8 | 2.1.5 9 | jar 10 | 11 | apijson-column 12 | A column plugin for APIJSON 13 | 14 | 15 | UTF-8 16 | UTF-8 17 | 1.8 18 | UTF-8 19 | 20 | 21 | 22 | 23 | 24 | com.alibaba 25 | fastjson 26 | 1.2.83 27 | 28 | 29 | 30 | 31 | com.github.Tencent 32 | APIJSON 33 | 7.5.5 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | org.apache.maven.plugins 43 | maven-compiler-plugin 44 | 3.12.1 45 | 46 | 1.8 47 | 1.8 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | jitpack.io 57 | https://jitpack.io 58 | 59 | true 60 | 61 | 62 | 63 | 64 | 65 | -------------------------------------------------------------------------------- /src/main/java/apijson/column/ColumnUtil.java: -------------------------------------------------------------------------------- 1 | /*Copyright ©2021 TommyLemon(https://github.com/APIJSON/apijson-column) 2 | 3 | Licensed under the Apache License, Version 2.0 (the "License"); 4 | you may not use this file except in compliance with the License. 5 | You may obtain a copy of the License at 6 | 7 | http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | Unless required by applicable law or agreed to in writing, software 10 | distributed under the License is distributed on an "AS IS" BASIS, 11 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | See the License for the specific language governing permissions and 13 | limitations under the License.*/ 14 | 15 | package apijson.column; 16 | 17 | import java.util.*; 18 | import java.util.Map.Entry; 19 | 20 | import apijson.RequestMethod; 21 | import apijson.StringUtil; 22 | import apijson.orm.AbstractSQLConfig; 23 | import apijson.orm.AbstractSQLExecutor; 24 | 25 | 26 | /**表字段相关工具类 27 | * @author Lemon 28 | * @see 先提前配置 {@link #VERSIONED_TABLE_COLUMN_MAP}, {@link #VERSIONED_KEY_COLUMN_MAP} 等,然后调用相关方法。 29 | * 不支持直接关联 database, schema, datasource,可以把这些与 table 拼接为一个字符串传给参数 table,格式可以是 database-schema-datasource-table 30 | */ 31 | public class ColumnUtil { 32 | 33 | /**带版本的表和字段一对多对应关系,用来做 反选字段 34 | * Map> 35 | */ 36 | public static SortedMap>> VERSIONED_TABLE_COLUMN_MAP; 37 | 38 | /**带版本的 JSON key 和表字段一对一对应关系,用来做字段名映射 39 | * Map>> 40 | */ 41 | public static SortedMap>> VERSIONED_KEY_COLUMN_MAP; 42 | 43 | /**带版本的 JSON key 和表字段一对一对应关系,用来做字段名映射,与 VERSIONED_KEY_COLUMN_MAP 相反 44 | * Map>> 45 | */ 46 | private static SortedMap>> VERSIONED_COLUMN_KEY_MAP; 47 | 48 | public static final Comparator DESC_COMPARATOR = new Comparator() { 49 | @Override 50 | public int compare(Integer o1, Integer o2) { 51 | return o2.compareTo(o1); 52 | } 53 | }; 54 | 55 | static { 56 | VERSIONED_TABLE_COLUMN_MAP = new TreeMap<>(DESC_COMPARATOR); 57 | VERSIONED_KEY_COLUMN_MAP = new TreeMap<>(DESC_COMPARATOR); 58 | VERSIONED_COLUMN_KEY_MAP = new TreeMap<>(DESC_COMPARATOR); 59 | } 60 | 61 | /**初始化 62 | */ 63 | public static void init() { 64 | VERSIONED_COLUMN_KEY_MAP.clear(); 65 | 66 | // 反过来补全 column -> key 的配置,以空间换时间 67 | Set>>> set = VERSIONED_KEY_COLUMN_MAP.entrySet(); 68 | if (set != null && set.isEmpty() == false) { 69 | 70 | SortedMap>> map = new TreeMap<>(DESC_COMPARATOR); 71 | 72 | for (Entry>> entry : set) { 73 | 74 | Map> tableKeyColumnMap = entry == null ? null : entry.getValue(); 75 | Set>> tableKeyColumnSet = tableKeyColumnMap == null ? null : tableKeyColumnMap.entrySet(); 76 | 77 | if (tableKeyColumnSet != null && tableKeyColumnSet.isEmpty() == false) { 78 | 79 | Map> tableColumnKeyMap = new HashMap<>(); 80 | 81 | for (Entry> tableKeyColumnEntry : tableKeyColumnSet) { 82 | 83 | Map keyColumnMap = tableKeyColumnEntry == null ? null : tableKeyColumnEntry.getValue(); 84 | Set> keyColumnSet = keyColumnMap == null ? null : keyColumnMap.entrySet(); 85 | 86 | if (keyColumnSet != null && keyColumnSet.isEmpty() == false) { 87 | Map columnKeyMap = new HashMap<>(); 88 | for (Entry keyColumnEntry : keyColumnSet) { 89 | if (keyColumnEntry == null) { 90 | continue; 91 | } 92 | 93 | columnKeyMap.put(keyColumnEntry.getValue(), keyColumnEntry.getKey()); 94 | } 95 | 96 | tableColumnKeyMap.put(tableKeyColumnEntry.getKey(), columnKeyMap); 97 | } 98 | } 99 | 100 | map.put(entry.getKey(), tableColumnKeyMap); 101 | } 102 | } 103 | 104 | VERSIONED_COLUMN_KEY_MAP = map; 105 | } 106 | 107 | 108 | // 补全剩下未定义别名的 key,以空间换时间 109 | Set>>> allSet = VERSIONED_TABLE_COLUMN_MAP.entrySet(); 110 | if (allSet != null && allSet.isEmpty() == false) { 111 | 112 | for (Entry>> entry : allSet) { 113 | Map> keyColumnMap = VERSIONED_KEY_COLUMN_MAP.get(entry.getKey()); 114 | // 没必要,没特殊配置的就原样返回,没有安全隐患,还能减少性能浪费 Map> columnKeyMap = VERSIONED_COLUMN_KEY_MAP.get(entry.getKey()); 115 | if (keyColumnMap == null) { 116 | keyColumnMap = new LinkedHashMap<>(); 117 | VERSIONED_KEY_COLUMN_MAP.put(entry.getKey(), keyColumnMap); 118 | } 119 | // if (columnKeyMap == null) { 120 | // columnKeyMap = new LinkedHashMap<>(); 121 | // VERSIONED_COLUMN_KEY_MAP.put(entry.getKey(), columnKeyMap); 122 | // } 123 | 124 | Map> tableKeyColumnMap = entry == null ? null : entry.getValue(); 125 | Set>> tableKeyColumnSet = tableKeyColumnMap == null ? null : tableKeyColumnMap.entrySet(); 126 | 127 | if (tableKeyColumnSet != null && tableKeyColumnSet.isEmpty() == false) { 128 | 129 | for (Entry> tableKeyColumnEntry : tableKeyColumnSet) { 130 | 131 | List list = tableKeyColumnEntry == null ? null : tableKeyColumnEntry.getValue(); 132 | 133 | if (list != null && list.isEmpty() == false) { 134 | 135 | Map kcm = keyColumnMap.get(tableKeyColumnEntry.getKey()); 136 | // Map ckm = columnKeyMap.get(tableKeyColumnEntry.getKey()); 137 | if (kcm == null) { 138 | kcm = new LinkedHashMap<>(); 139 | keyColumnMap.put(tableKeyColumnEntry.getKey(), kcm); 140 | } 141 | // if (ckm == null) { 142 | // ckm = new LinkedHashMap<>(); 143 | // columnKeyMap.put(tableKeyColumnEntry.getKey(), ckm); 144 | // } 145 | 146 | for (String column : list) { 147 | if (column == null) { 148 | continue; 149 | } 150 | 151 | // ckm.putIfAbsent(column, column); 152 | //FIXME 对 Comment.toId (多版本) 居然不起作用 153 | // if (kcm.containsValue(column) == false) { 154 | kcm.putIfAbsent(column, column); 155 | // } 156 | } 157 | 158 | // for (String column : list) { 159 | // if (column == null || ckm.get(column) != null) { 160 | // continue; 161 | // } 162 | // 163 | // kcm.putIfAbsent(column, column); 164 | // } 165 | 166 | } 167 | } 168 | 169 | } 170 | } 171 | 172 | } 173 | 174 | } 175 | 176 | /**适配请求参数 JSON 中 @column:value 的 value 中的 key。支持 !key 反选字段 和 字段名映射 177 | * @param columns 178 | * @param table 179 | * @param method 180 | * @return 181 | */ 182 | public static List compatInputColumn(List columns, String table, RequestMethod method) { 183 | return compatInputColumn(columns, table, method, null, false); 184 | } 185 | 186 | /**适配请求参数 JSON 中 @column:value 的 value 中的 key。支持 !key 反选字段 和 字段名映射 187 | * @param columns 188 | * @param table 189 | * @param method 190 | * @param version 191 | * @return 192 | * @see 先提前配置 {@link #VERSIONED_TABLE_COLUMN_MAP},然后在 {@link AbstractSQLConfig} 的子类重写 {@link AbstractSQLConfig#setColumn } 并调用这个方法,例如 193 | *
194 | 	public AbstractSQLConfig setColumn(List column) { 
195 | return super.setColumn(ColumnUtil.compatInputColumn(column, getTable(), version));
196 | } 197 | *
198 | */ 199 | public static List compatInputColumn(List columns, String table, RequestMethod method, Integer version, boolean throwWhenNoKey) { 200 | String[] keys = columns == null ? null : columns.toArray(new String[]{}); // StringUtil.split(c, ";"); 201 | if (keys == null || keys.length <= 0) { // JOIN 副表可以设置 @column:"" 来指定不返回字段 202 | return columns != null ? columns : getClosestValue(VERSIONED_TABLE_COLUMN_MAP, version, table); 203 | } 204 | 205 | // boolean isQueryMethod = RequestMethod.isQueryMethod(method); 206 | 207 | List exceptColumns = new ArrayList<>(); // Map exceptColumnMap = new HashMap<>(); 208 | List newColumns = new ArrayList<>(); 209 | 210 | Map keyColumnMap = getClosestValue(VERSIONED_KEY_COLUMN_MAP, version, table); 211 | boolean isEmpty = keyColumnMap == null || keyColumnMap.isEmpty(); 212 | 213 | String q = "`"; 214 | 215 | String expression; 216 | //...;fun0(arg0,arg1,...):fun0;fun1(arg0,arg1,...):fun1;... 217 | for (int i = 0; i < keys.length; i++) { 218 | 219 | //!column,column2,!column3,column4:alias4;fun(arg0,arg1,...) 220 | expression = keys[i]; 221 | int start = expression.indexOf("("); 222 | int end = expression.lastIndexOf(")"); 223 | if (start >= 0 && start < end) { 224 | String[] ks = StringUtil.split(expression.substring(start + 1, end)); 225 | 226 | String expr = expression.substring(0, start + 1); 227 | for (int j = 0; j < ks.length; j++) { 228 | String ck = ks[j]; 229 | boolean hasQuote = false; 230 | if (ck.endsWith("`")) { 231 | String nck = ck.substring(0, ck.length() - 1); 232 | if (nck.lastIndexOf("`") == 0) { 233 | ck = nck.substring(1); 234 | hasQuote = true; 235 | } 236 | } 237 | 238 | String rc = null; 239 | if (hasQuote || StringUtil.isName(ck)) { 240 | rc = isEmpty ? null : keyColumnMap.get(ck); 241 | if (rc == null && isEmpty == false && throwWhenNoKey) { 242 | throw new NullPointerException(table + ":{ @column: value } 的 value 中 " + ck + " 不合法!不允许传后端未授权访问的字段名!"); 243 | } 244 | } 245 | 246 | expr += (j <= 0 ? "" : ",") + (hasQuote ? q : "") + (rc == null ? ck : rc) + (hasQuote ? q : ""); 247 | } 248 | 249 | newColumns.add(expr + expression.substring(end)); 250 | 251 | // newColumns.add(expression); 252 | continue; 253 | } 254 | 255 | String[] ckeys = StringUtil.split(expression); 256 | if (ckeys != null && ckeys.length > 0) { 257 | for (int j = 0; j < ckeys.length; j++) { 258 | String ck = ckeys[j]; 259 | 260 | if (ck.startsWith("!")) { 261 | if (ck.length() <= 1) { 262 | throw new IllegalArgumentException("@column:value 的 value 中 " + ck 263 | + " 不合法! !column 不允许 column 为空字符串!column,!column2,!column3,column4:alias4 中所有 column 必须符合变量名格式!"); 264 | } 265 | String c = ck.substring(1); 266 | if (StringUtil.isName(c) == false) { 267 | throw new IllegalArgumentException("@column:value 的 value 中 " + c 268 | + " 不合法! column,!column2,!column3,column4:alias4 中所有 column 必须符合变量名格式!"); 269 | } 270 | 271 | String rc = isEmpty ? null : keyColumnMap.get(c); 272 | exceptColumns.add(rc == null ? c : rc); // 不使用数据库别名,以免 JOIN 等复杂查询情况下报错字段不存在 exceptColumnMap.put(nc == null ? c : nc, c); // column:alias 273 | } else { 274 | boolean hasQuote = false; 275 | if (ck.endsWith("`")) { 276 | String nck = ck.substring(0, ck.length() - 1); 277 | if (nck.lastIndexOf("`") == 0) { 278 | ck = nck.substring(1); 279 | hasQuote = true; 280 | } 281 | } 282 | 283 | String rc = null; 284 | if (hasQuote || StringUtil.isName(ck)) { 285 | rc = isEmpty ? null : keyColumnMap.get(ck); 286 | if (rc == null && isEmpty == false && throwWhenNoKey) { 287 | throw new NullPointerException(table + ":{ @column: value } 的 value 中 " + ck + " 不合法!不允许传后端未授权访问的字段名!"); 288 | } 289 | } 290 | 291 | newColumns.add(rc == null ? ck : rc); // 不使用数据库别名,以免 JOIN 等复杂查询情况下报错字段不存在 newColumns.add(rc == null ? ck : (isQueryMethod ? (rc + ":" + ck) : rc)); 292 | } 293 | } 294 | } 295 | } 296 | 297 | List allColumns = exceptColumns == null || exceptColumns.isEmpty() ? null : getClosestValue(VERSIONED_TABLE_COLUMN_MAP, version, table); 298 | 299 | if (allColumns != null && allColumns.isEmpty() == false) { 300 | 301 | // 不使用数据库别名,以免 JOIN 等复杂查询情况下报错字段不存在 302 | // Map> tableColumnKeyMap = VERSIONED_COLUMN_KEY_MAP == null || VERSIONED_COLUMN_KEY_MAP.isEmpty() ? null : VERSIONED_COLUMN_KEY_MAP.get(version); 303 | // Map columnKeyMap = tableColumnKeyMap == null || tableColumnKeyMap.isEmpty() ? null : tableColumnKeyMap.get(table); 304 | 305 | for (String c : allColumns) { 306 | if (c != null && exceptColumns.contains(c) == false) { // column:alias 307 | // 不使用数据库别名,以免 JOIN 等复杂查询情况下报错字段不存在 String alias = isQueryMethod == false || columnKeyMap == null || columnKeyMap.isEmpty() ? null : columnKeyMap.get(c); 308 | newColumns.add(c); // newColumns.add(alias == null ? c : (c + ":" + alias)); 309 | } 310 | } 311 | } 312 | 313 | return newColumns; 314 | } 315 | 316 | 317 | /**适配请求参数 JSON 中 条件/赋值 键值对的 key 318 | * @param key 319 | * @param table 320 | * @param method 321 | * @return 322 | */ 323 | public static String compatInputKey(String key, String table, RequestMethod method) { 324 | return compatInputKey(key, table, method, null, false); 325 | } 326 | 327 | /**适配请求参数 JSON 中 条件/赋值 键值对的 key 328 | * @param key 329 | * @param table 330 | * @param method 331 | * @param version 332 | * @return 333 | * @see 先提前配置 {@link #VERSIONED_KEY_COLUMN_MAP},然后在 {@link AbstractSQLConfig} 的子类重写 {@link AbstractSQLConfig#getKey } 并调用这个方法,例如 334 | *
335 | 	public String getKey(String key) { 
336 | return super.getKey(ColumnUtil.compatInputKey(key, getTable(), version));
337 | } 338 | *
339 | */ 340 | public static String compatInputKey(String key, String table, RequestMethod method, Integer version, boolean throwWhenNoKey) { 341 | Map keyColumnMap = getClosestValue(VERSIONED_KEY_COLUMN_MAP, version, table); 342 | boolean isEmpty = keyColumnMap == null || keyColumnMap.isEmpty(); 343 | String alias = isEmpty ? null : keyColumnMap.get(key); 344 | if (alias == null) { 345 | if (isEmpty == false && throwWhenNoKey) { 346 | throw new NullPointerException(table + ":{} 中不允许传 " + key + " !"); 347 | } 348 | return key; 349 | } 350 | 351 | return alias; 352 | } 353 | 354 | /**适配返回结果 JSON 中键值对的 key。可能通过不传 @column 等方式来返回原始字段名,这样就达不到隐藏真实字段名的需求了,所以只有最终这个兜底方式靠谱。 355 | * @param key 356 | * @param table 357 | * @param method 358 | * @return 359 | */ 360 | public static String compatOutputKey(String key, String table, RequestMethod method) { 361 | return compatOutputKey(key, table, method, null); 362 | } 363 | 364 | /**适配返回结果 JSON 中键值对的 key。可能通过不传 @column 等方式来返回原始字段名,这样就达不到隐藏真实字段名的需求了,所以只有最终这个兜底方式靠谱。 365 | * @param key 366 | * @param table 367 | * @param method 368 | * @param version 369 | * @return 370 | * @see 先提前配置 {@link #VERSIONED_COLUMN_KEY_MAP},然后在 {@link AbstractSQLExecutor} 的子类重写 {@link AbstractSQLExecutor#getKey } 并调用这个方法,例如 371 | *
372 | 	protected String getKey(SQLConfig config, ResultSet rs, ResultSetMetaData rsmd, int tablePosition, JSONObject table,
373 | 	int columnIndex, Map childMap) throws Exception { 
374 | return ColumnUtil.compatOutputKey(super.getKey(config, rs, rsmd, tablePosition, table, columnIndex, childMap), config.getTable(), config.getMethod(), version);
375 | } 376 | *
377 | */ 378 | public static String compatOutputKey(String key, String table, RequestMethod method, Integer version) { 379 | Map columnKeyMap = getClosestValue(VERSIONED_COLUMN_KEY_MAP, version, table); 380 | String alias = columnKeyMap == null || columnKeyMap.isEmpty() ? null : columnKeyMap.get(key); 381 | return alias == null ? key : alias; 382 | } 383 | 384 | public static T getClosestValue(SortedMap> versionedMap, Integer version, String table) { 385 | boolean isEmpty = versionedMap == null || versionedMap.isEmpty(); 386 | 387 | Map map = isEmpty || version == null ? null : versionedMap.get(version); 388 | T m = map == null ? null : map.get(table); 389 | if (isEmpty == false && m == null) { 390 | Set>> set = versionedMap.entrySet(); 391 | 392 | T lm = null; 393 | for (Entry> entry : set) { 394 | Map val = entry.getValue(); 395 | m = val == null ? null : val.get(table); 396 | if (m == null) { 397 | continue; 398 | } 399 | 400 | if (version == null || version == 0) { 401 | // versionedMap.put(null, val); 402 | return m; 403 | } 404 | 405 | Integer key = entry.getKey(); 406 | if (key == null) { 407 | lm = m; 408 | map = val; 409 | continue; 410 | } 411 | 412 | if (version >= key) { 413 | versionedMap.put(version, val); 414 | return m; 415 | } 416 | 417 | break; 418 | } 419 | 420 | if (lm != null) { 421 | m = lm; 422 | } 423 | 424 | if (map != null) { 425 | versionedMap.put(version, map); 426 | } 427 | } 428 | 429 | return m; 430 | } 431 | 432 | 433 | /**把多个表名相关属性拼接成一个表名 434 | * @param database 435 | * @param schema 436 | * @param datasource 437 | * @param table 438 | * @return 439 | */ 440 | public static String concat(String database, String schema, String datasource, String table) { 441 | return database + "-" + schema + "-" + datasource + "-" + table; 442 | } 443 | 444 | 445 | } 446 | --------------------------------------------------------------------------------