├── .github └── workflows │ ├── github-release.yml │ └── maven.yml ├── .gitignore ├── .springjavaformatconfig ├── README.md ├── pom.xml └── src └── main ├── java └── com │ └── pig4cloud │ └── plugin │ ├── constants │ └── DataSourceConstant.java │ └── impl │ └── dm │ ├── ConfigInfoBetaMapperByDm.java │ ├── ConfigInfoGrayMapperByDm.java │ ├── ConfigInfoMapperByDm.java │ ├── ConfigInfoTagMapperByDm.java │ ├── ConfigTagsRelationMapperByDm.java │ ├── DmAbstractMapper.java │ ├── GroupCapacityMapperByDm.java │ ├── HistoryConfigInfoMapperByDm.java │ ├── TenantCapacityMapperByDm.java │ └── TenantInfoMapperByDm.java └── resources └── META-INF └── services └── com.alibaba.nacos.plugin.datasource.mapper.Mapper /.github/workflows/github-release.yml: -------------------------------------------------------------------------------- 1 | name: publish github release 2 | 3 | on: 4 | workflow_dispatch: 5 | inputs: 6 | releaseversion: 7 | description: 'Release version' 8 | required: true 9 | default: '0.4.0' 10 | 11 | 12 | jobs: 13 | publish-github-release: 14 | runs-on: ubuntu-latest 15 | steps: 16 | - uses: actions/checkout@v4 17 | 18 | - name: Create GitHub Release 19 | id: create_release 20 | uses: actions/create-release@v1 21 | env: 22 | GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} 23 | with: 24 | tag_name: ${{ github.event.inputs.releaseversion }} 25 | release_name: ${{ github.event.inputs.releaseversion }} 26 | body: | 27 | ### Things that changed in this release 28 | ${{ steps.changelog.outputs.changelog }} 29 | draft: false 30 | prerelease: ${{ contains(github.event.inputs.releaseversion, '-') }} 31 | -------------------------------------------------------------------------------- /.github/workflows/maven.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a Java project with Maven 2 | # For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven 3 | 4 | name: nacos-datasource-plugin-pg 5 | 6 | on: 7 | push: 8 | branches: [ master,dev ] 9 | pull_request: 10 | branches: [ master,dev ] 11 | 12 | jobs: 13 | build: 14 | runs-on: ubuntu-latest 15 | steps: 16 | - uses: actions/checkout@v2 17 | - name: Set up JDK 8 18 | uses: actions/setup-java@v2 19 | with: 20 | java-version: '8' 21 | distribution: 'adopt' 22 | 23 | - name: mvn clean install 24 | run: mvn clean install 25 | 26 | - name: mvn spring-javaformat:validate 27 | run: mvn spring-javaformat:validate 28 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | HELP.md 2 | target/ 3 | !.mvn/wrapper/maven-wrapper.jar 4 | !**/src/main/**/target/ 5 | !**/src/test/**/target/ 6 | 7 | ### STS ### 8 | .apt_generated 9 | .classpath 10 | .factorypath 11 | .project 12 | .settings 13 | .springBeans 14 | .sts4-cache 15 | 16 | ### IntelliJ IDEA ### 17 | .idea 18 | *.iws 19 | *.iml 20 | *.ipr 21 | 22 | ### NetBeans ### 23 | /nbproject/private/ 24 | /nbbuild/ 25 | /dist/ 26 | /nbdist/ 27 | /.nb-gradle/ 28 | build/ 29 | !**/src/main/**/build/ 30 | !**/src/test/**/build/ 31 | 32 | ### VS Code ### 33 | .vscode/ 34 | -------------------------------------------------------------------------------- /.springjavaformatconfig: -------------------------------------------------------------------------------- 1 | java-baseline=8 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Nacos 达梦数据库插件 2 | 3 | [![Maven Central](https://img.shields.io/maven-central/v/com.pig4cloud.plugin/nacos-datasource-plugin-dm8.svg?style=flat-square)](https://maven.badges.herokuapp.com/maven-central/com.pig4cloud.plugin/nacos-datasource-plugin-dm8) 4 | [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0) 5 | 6 | ## 项目介绍 7 | 8 | 本插件为 Nacos(2.2.0 版本及以上)提供达梦数据库(DM8)的数据源支持。通过 SPI 机制实现,您只需在 `application.properties` 配置文件中修改 `spring.datasource.platform` 属性即可启用达梦数据库。 9 | 10 | ![Nacos 插件架构](https://minio.pigx.top/oss/202212/1671179590.jpg) 11 | 12 | > Nacos 官方默认支持 MySQL 和 Derby 数据库,本插件扩展了对达梦数据库的支持。 13 | 14 | ## 版本兼容性 15 | 16 | | Nacos 版本 | 插件版本 | 17 | |--------------|---------| 18 | | 2.2.0 - 2.3.0 | 0.0.2 | 19 | | 2.3.1 - 2.3.2 | 0.0.3 | 20 | | 2.4.0 - 2.4.3 | 0.0.4 | 21 | | 2.5.0+ | 0.0.5 | 22 | 23 | ## 快速开始 24 | 25 | ### 1. 添加依赖 26 | 27 | 在项目的 `pom.xml` 中添加以下依赖(注意:依赖已上传至 Maven 中央仓库,请勿使用阿里云代理): 28 | 29 | ```xml 30 | 31 | 32 | com.pig4cloud.plugin 33 | nacos-datasource-plugin-dm8 34 | ${plugin.version} 35 | 36 | 37 | 38 | 39 | com.dameng 40 | DmJdbcDriver18 41 | 8.1.1.193 42 | 43 | ``` 44 | 45 | ### 2. 数据库迁移 46 | 47 | 从 MySQL 迁移到达梦数据库有以下两种推荐方案: 48 | 49 | #### 方案一:使用 SQLark 迁移工具 50 | SQLark 提供全流程的异构数据库迁移服务,通过自动化语法解析,提前识别可能存在的改造工作,生成最佳迁移策略。 51 | 52 | ![SQLark 迁移工具](https://minio.pigx.vip/oss/202501/1738162693.png) 53 | 54 | 详细迁移指南请访问 [SQLark 文档](https://www.sqlark.com/docs/zh/v1/data-migration/overview.html) 55 | 56 | #### 方案二:使用达梦 DTS 迁移工具 57 | 达梦 DTS 工具支持将 Nacos 的 MySQL 数据库无缝迁移至达梦数据库。 58 | 59 | ![达梦 DTS 迁移](https://minio.pigx.top/oss/202406/1718112771.png) 60 | 61 | ### 3. 配置数据源 62 | 63 | 在 Nacos 配置文件中添加以下配置: 64 | 65 | ```yaml 66 | db: 67 | num: 1 68 | url: 69 | 0: jdbc:dm://127.0.0.1:5236?schema=PIGXX_CONFIG 70 | user: SYSDBA 71 | password: SYSDBA 72 | pool: 73 | config: 74 | driver-class-name: dm.jdbc.driver.DmDriver 75 | ``` 76 | 77 | ### 4. 启用达梦数据库 78 | 79 | 在应用配置中设置数据库平台为达梦: 80 | 81 | ```yaml 82 | spring: 83 | datasource: 84 | platform: dameng 85 | ``` 86 | 87 | ## 参与贡献 88 | 89 | 我们欢迎所有形式的贡献,如果您有任何改进建议或功能扩展,请提交 Pull Request。 90 | 91 | ## 开源协议 92 | 93 | 本项目采用 Apache License 2.0 开源协议 - 详情请参见 [LICENSE](LICENSE) 文件。 94 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | org.springframework.boot 5 | spring-boot-starter-parent 6 | 2.7.18 7 | 8 | 9 | 10 | 11 | 4.0.0 12 | com.pig4cloud.plugin 13 | nacos-datasource-plugin-dm8 14 | 0.0.5 15 | nacos-datasource-plugin-dm8 16 | nacos-datasource-plugin-dm8 17 | https://pig4cloud.com 18 | 19 | 20 | 21 | The ApacheSoftware License, Version 2.0 22 | http://www.apache.org/licenses/LICENSE-2.0.txt 23 | repo 24 | 25 | 26 | 27 | 28 | 2.5.0 29 | 0.0.32 30 | 3.8.1 31 | 1.8 32 | 1.8 33 | 2.2.5 34 | 1.21 35 | 36 | 37 | 38 | 39 | lengleng 40 | wangiegie@gmail.com 41 | 42 | 43 | 44 | 45 | master 46 | https://gitee.wang/lengleng/pig 47 | https://pigx.top 48 | https://gitee.wang/lengleng/pig.git 49 | 50 | 51 | 52 | 53 | 54 | org.springframework.boot 55 | spring-boot-starter 56 | 57 | 58 | 59 | org.projectlombok 60 | lombok 61 | true 62 | 63 | 64 | 65 | com.alibaba.nacos 66 | nacos-datasource-plugin 67 | ${nacos.version} 68 | 69 | 70 | 71 | com.alibaba.nacos 72 | nacos-common 73 | ${nacos.version} 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | io.spring.javaformat 82 | spring-javaformat-maven-plugin 83 | ${spring.checkstyle.plugin} 84 | 85 | 86 | 87 | org.apache.maven.plugins 88 | maven-compiler-plugin 89 | ${maven.compiler.version} 90 | 91 | ${maven.compiler.target} 92 | ${maven.compiler.source} 93 | UTF-8 94 | true 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | snapshot 103 | 104 | 105 | 106 | 107 | org.apache.maven.plugins 108 | maven-source-plugin 109 | 2.2.1 110 | 111 | 112 | package 113 | 114 | jar-no-fork 115 | 116 | 117 | 118 | 119 | 120 | 121 | org.apache.maven.plugins 122 | maven-javadoc-plugin 123 | 3.3.2 124 | 125 | private 126 | true 127 | UTF-8 128 | UTF-8 129 | UTF-8 130 | none 131 | false 132 | 133 | 134 | 135 | package 136 | 137 | jar 138 | 139 | 140 | 141 | 142 | 143 | 144 | org.apache.maven.plugins 145 | maven-gpg-plugin 146 | 3.0.1 147 | 148 | 149 | sign-artifacts 150 | verify 151 | 152 | sign 153 | 154 | 155 | 156 | 157 | 158 | --pinentry-mode 159 | loopback 160 | 161 | 162 | 163 | 164 | org.sonatype.plugins 165 | nexus-staging-maven-plugin 166 | 1.6.13 167 | true 168 | 169 | sonatype 170 | https://oss.sonatype.org/ 171 | true 172 | 173 | 174 | 175 | 176 | 177 | 178 | sonatype 179 | 180 | https://oss.sonatype.org/content/repositories/snapshots/ 181 | 182 | 183 | 184 | 185 | 186 | release 187 | 188 | 189 | 190 | 191 | org.apache.maven.plugins 192 | maven-source-plugin 193 | 2.2.1 194 | 195 | 196 | package 197 | 198 | jar-no-fork 199 | 200 | 201 | 202 | 203 | 204 | 205 | org.apache.maven.plugins 206 | maven-javadoc-plugin 207 | 3.3.2 208 | 209 | private 210 | true 211 | UTF-8 212 | UTF-8 213 | UTF-8 214 | none 215 | false 216 | 217 | 218 | 219 | package 220 | 221 | jar 222 | 223 | 224 | 225 | 226 | 227 | 228 | org.apache.maven.plugins 229 | maven-gpg-plugin 230 | 3.0.1 231 | 232 | 233 | sign-artifacts 234 | verify 235 | 236 | sign 237 | 238 | 239 | 240 | 241 | 242 | --pinentry-mode 243 | loopback 244 | 245 | 246 | 247 | 248 | org.sonatype.plugins 249 | nexus-staging-maven-plugin 250 | 1.6.13 251 | true 252 | 253 | sonatype 254 | https://oss.sonatype.org/ 255 | true 256 | 257 | 258 | 259 | 260 | 261 | 262 | sonatype 263 | 264 | https://oss.sonatype.org/service/local/staging/deploy/maven2/ 265 | 266 | 267 | 268 | 269 | 270 | 271 | -------------------------------------------------------------------------------- /src/main/java/com/pig4cloud/plugin/constants/DataSourceConstant.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1999-2022 Alibaba Group Holding Ltd. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.pig4cloud.plugin.constants; 18 | 19 | /** 20 | * The data source name. 21 | * 22 | * @author hyx 23 | **/ 24 | public class DataSourceConstant { 25 | 26 | public static final String MYSQL = "mysql"; 27 | 28 | public static final String DERBY = "derby"; 29 | 30 | public static final String POSTGRESQL = "postgresql"; 31 | 32 | public static final String ORACLE = "oracle"; 33 | 34 | public static final String DM = "dameng"; 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/com/pig4cloud/plugin/impl/dm/ConfigInfoBetaMapperByDm.java: -------------------------------------------------------------------------------- 1 | package com.pig4cloud.plugin.impl.dm; 2 | 3 | import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoBetaMapper; 4 | import com.alibaba.nacos.plugin.datasource.model.MapperContext; 5 | import com.alibaba.nacos.plugin.datasource.model.MapperResult; 6 | import com.pig4cloud.plugin.constants.DataSourceConstant; 7 | 8 | import java.util.ArrayList; 9 | import java.util.List; 10 | 11 | public class ConfigInfoBetaMapperByDm extends DmAbstractMapper implements ConfigInfoBetaMapper { 12 | 13 | @Override 14 | public MapperResult findAllConfigInfoBetaForDumpAllFetchRows(MapperContext context) { 15 | int startRow = context.getStartRow(); 16 | int pageSize = context.getPageSize(); 17 | String sql = " SELECT t.id,data_id,group_id,tenant_id,app_name,content,md5,gmt_modified,beta_ips,encrypted_data_key " 18 | + " FROM ( SELECT id FROM config_info_beta ORDER BY id LIMIT " + startRow + "," + pageSize + " )" 19 | + " g, config_info_beta t WHERE g.id = t.id "; 20 | List paramList = new ArrayList<>(); 21 | paramList.add(startRow); 22 | paramList.add(pageSize); 23 | 24 | return new MapperResult(sql, paramList); 25 | } 26 | 27 | @Override 28 | public String getDataSource() { 29 | return DataSourceConstant.DM; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/pig4cloud/plugin/impl/dm/ConfigInfoGrayMapperByDm.java: -------------------------------------------------------------------------------- 1 | package com.pig4cloud.plugin.impl.dm; 2 | 3 | import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoGrayMapper; 4 | import com.alibaba.nacos.plugin.datasource.model.MapperContext; 5 | import com.alibaba.nacos.plugin.datasource.model.MapperResult; 6 | import com.pig4cloud.plugin.constants.DataSourceConstant; 7 | 8 | import java.util.Collections; 9 | 10 | public class ConfigInfoGrayMapperByDm extends DmAbstractMapper implements ConfigInfoGrayMapper { 11 | 12 | @Override 13 | public MapperResult findAllConfigInfoGrayForDumpAllFetchRows(MapperContext context) { 14 | String sql = " SELECT id,data_id,group_id,tenant_id,gray_name,gray_rule,app_name,content,md5,gmt_modified FROM config_info_gray ORDER BY id LIMIT " 15 | + context.getStartRow() + "," + context.getPageSize(); 16 | return new MapperResult(sql, Collections.emptyList()); 17 | } 18 | 19 | @Override 20 | public String getDataSource() { 21 | return DataSourceConstant.DM; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/pig4cloud/plugin/impl/dm/ConfigInfoMapperByDm.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1999-2022 Alibaba Group Holding Ltd. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package com.pig4cloud.plugin.impl.dm; 18 | 19 | import com.alibaba.nacos.common.utils.CollectionUtils; 20 | import com.alibaba.nacos.common.utils.NamespaceUtil; 21 | import com.alibaba.nacos.common.utils.StringUtils; 22 | import com.alibaba.nacos.plugin.datasource.constants.ContextConstant; 23 | import com.alibaba.nacos.plugin.datasource.constants.FieldConstant; 24 | import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoMapper; 25 | import com.alibaba.nacos.plugin.datasource.model.MapperContext; 26 | import com.alibaba.nacos.plugin.datasource.model.MapperResult; 27 | import com.pig4cloud.plugin.constants.DataSourceConstant; 28 | 29 | import java.sql.Timestamp; 30 | import java.util.ArrayList; 31 | import java.util.Collections; 32 | import java.util.List; 33 | 34 | /** 35 | * The mysql implementation of ConfigInfoMapper. 36 | * 37 | * @author hyx 38 | **/ 39 | 40 | public class ConfigInfoMapperByDm extends DmAbstractMapper implements ConfigInfoMapper { 41 | 42 | private static final String DATA_ID = "dataId"; 43 | 44 | private static final String GROUP = "group"; 45 | 46 | private static final String APP_NAME = "appName"; 47 | 48 | private static final String CONTENT = "content"; 49 | 50 | private static final String TENANT = "tenant"; 51 | 52 | @Override 53 | public MapperResult findConfigInfoByAppFetchRows(MapperContext context) { 54 | final String appName = (String) context.getWhereParameter(FieldConstant.APP_NAME); 55 | final String tenantId = (String) context.getWhereParameter(FieldConstant.TENANT_ID); 56 | String sql = "SELECT id,data_id,group_id,tenant_id,app_name,content FROM config_info" 57 | + " WHERE tenant_id LIKE ? AND app_name= ?" + " LIMIT " + context.getStartRow() + "," 58 | + context.getPageSize(); 59 | return new MapperResult(sql, CollectionUtils.list(tenantId, appName)); 60 | } 61 | 62 | @Override 63 | public MapperResult getTenantIdList(MapperContext context) { 64 | String sql = "SELECT tenant_id FROM config_info WHERE tenant_id != '" + NamespaceUtil.getNamespaceDefaultId() 65 | + "' GROUP BY tenant_id LIMIT " + context.getStartRow() + "," + context.getPageSize(); 66 | return new MapperResult(sql, Collections.emptyList()); 67 | } 68 | 69 | @Override 70 | public MapperResult getGroupIdList(MapperContext context) { 71 | String sql = "SELECT group_id FROM config_info WHERE tenant_id ='" + NamespaceUtil.getNamespaceDefaultId() 72 | + "' GROUP BY group_id LIMIT " + context.getStartRow() + "," + context.getPageSize(); 73 | return new MapperResult(sql, Collections.emptyList()); 74 | } 75 | 76 | @Override 77 | public MapperResult findAllConfigKey(MapperContext context) { 78 | String sql = " SELECT data_id,group_id,app_name FROM ( " 79 | + " SELECT id FROM config_info WHERE tenant_id LIKE ? ORDER BY id LIMIT " + context.getStartRow() + "," 80 | + context.getPageSize() + " )" + " g, config_info t WHERE g.id = t.id "; 81 | return new MapperResult(sql, CollectionUtils.list(context.getWhereParameter(FieldConstant.TENANT_ID))); 82 | } 83 | 84 | @Override 85 | public MapperResult findAllConfigInfoBaseFetchRows(MapperContext context) { 86 | String sql = "SELECT t.id,data_id,group_id,content,md5" 87 | + " FROM ( SELECT id FROM config_info ORDER BY id LIMIT " + context.getStartRow() + "," 88 | + context.getPageSize() + " )" + " g, config_info t WHERE g.id = t.id "; 89 | return new MapperResult(sql, Collections.emptyList()); 90 | } 91 | 92 | @Override 93 | public MapperResult findAllConfigInfoFragment(MapperContext context) { 94 | String contextParameter = context.getContextParameter(ContextConstant.NEED_CONTENT); 95 | boolean needContent = contextParameter != null && Boolean.parseBoolean(contextParameter); 96 | String sql = "SELECT id,data_id,group_id,tenant_id,app_name," + (needContent ? "content," : "") 97 | + "md5,gmt_modified,type,encrypted_data_key FROM config_info WHERE id > ? ORDER BY id ASC LIMIT " 98 | + context.getStartRow() + "," + context.getPageSize(); 99 | return new MapperResult(sql, CollectionUtils.list(context.getWhereParameter(FieldConstant.ID))); 100 | } 101 | 102 | @Override 103 | public MapperResult findChangeConfigFetchRows(MapperContext context) { 104 | final String tenant = (String) context.getWhereParameter(FieldConstant.TENANT_ID); 105 | final String dataId = (String) context.getWhereParameter(FieldConstant.DATA_ID); 106 | final String group = (String) context.getWhereParameter(FieldConstant.GROUP_ID); 107 | final String appName = (String) context.getWhereParameter(FieldConstant.APP_NAME); 108 | final String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant; 109 | final Timestamp startTime = (Timestamp) context.getWhereParameter(FieldConstant.START_TIME); 110 | final Timestamp endTime = (Timestamp) context.getWhereParameter(FieldConstant.END_TIME); 111 | 112 | List paramList = new ArrayList<>(); 113 | 114 | final String sqlFetchRows = "SELECT id,data_id,group_id,tenant_id,app_name,type,md5,gmt_modified FROM config_info WHERE "; 115 | String where = " 1=1 "; 116 | if (!StringUtils.isBlank(dataId)) { 117 | where += " AND data_id LIKE ? "; 118 | paramList.add(dataId); 119 | } 120 | if (!StringUtils.isBlank(group)) { 121 | where += " AND group_id LIKE ? "; 122 | paramList.add(group); 123 | } 124 | 125 | if (!StringUtils.isBlank(tenantTmp)) { 126 | where += " AND tenant_id = ? "; 127 | paramList.add(tenantTmp); 128 | } 129 | 130 | if (!StringUtils.isBlank(appName)) { 131 | where += " AND app_name = ? "; 132 | paramList.add(appName); 133 | } 134 | if (startTime != null) { 135 | where += " AND gmt_modified >=? "; 136 | paramList.add(startTime); 137 | } 138 | if (endTime != null) { 139 | where += " AND gmt_modified <=? "; 140 | paramList.add(endTime); 141 | } 142 | return new MapperResult( 143 | sqlFetchRows + where + " AND id > " + context.getWhereParameter(FieldConstant.LAST_MAX_ID) 144 | + " ORDER BY id ASC" + " LIMIT " + 0 + "," + context.getPageSize(), 145 | paramList); 146 | } 147 | 148 | @Override 149 | public MapperResult listGroupKeyMd5ByPageFetchRows(MapperContext context) { 150 | String sql = "SELECT t.id,data_id,group_id,tenant_id,app_name,md5,type,gmt_modified,encrypted_data_key FROM " 151 | + "( SELECT id FROM config_info ORDER BY id LIMIT " + context.getStartRow() + "," 152 | + context.getPageSize() + " ) g, config_info t WHERE g.id = t.id"; 153 | return new MapperResult(sql, Collections.emptyList()); 154 | } 155 | 156 | @Override 157 | public MapperResult findConfigInfoBaseLikeFetchRows(MapperContext context) { 158 | final String dataId = (String) context.getWhereParameter(FieldConstant.DATA_ID); 159 | final String group = (String) context.getWhereParameter(FieldConstant.GROUP_ID); 160 | final String content = (String) context.getWhereParameter(FieldConstant.CONTENT); 161 | 162 | final String sqlFetchRows = "SELECT id,data_id,group_id,tenant_id,content FROM config_info WHERE "; 163 | String where = " 1=1 AND tenant_id='" + NamespaceUtil.getNamespaceDefaultId() + "' "; 164 | 165 | List paramList = new ArrayList<>(); 166 | 167 | if (!StringUtils.isBlank(dataId)) { 168 | where += " AND data_id LIKE ? "; 169 | paramList.add(dataId); 170 | } 171 | if (!StringUtils.isBlank(group)) { 172 | where += " AND group_id LIKE "; 173 | paramList.add(group); 174 | } 175 | if (!StringUtils.isBlank(content)) { 176 | where += " AND content LIKE ? "; 177 | paramList.add(content); 178 | } 179 | return new MapperResult(sqlFetchRows + where + " LIMIT " + context.getStartRow() + "," + context.getPageSize(), 180 | paramList); 181 | } 182 | 183 | @Override 184 | public MapperResult findConfigInfo4PageFetchRows(MapperContext context) { 185 | final String tenant = (String) context.getWhereParameter(FieldConstant.TENANT_ID); 186 | final String dataId = (String) context.getWhereParameter(FieldConstant.DATA_ID); 187 | final String group = (String) context.getWhereParameter(FieldConstant.GROUP_ID); 188 | final String appName = (String) context.getWhereParameter(FieldConstant.APP_NAME); 189 | final String content = (String) context.getWhereParameter(FieldConstant.CONTENT); 190 | 191 | List paramList = new ArrayList<>(); 192 | 193 | final String sql = "SELECT id,data_id,group_id,tenant_id,app_name,content,type,encrypted_data_key FROM config_info"; 194 | StringBuilder where = new StringBuilder(" WHERE "); 195 | where.append(" tenant_id=? "); 196 | paramList.add(tenant); 197 | if (StringUtils.isNotBlank(dataId)) { 198 | where.append(" AND data_id=? "); 199 | paramList.add(dataId); 200 | } 201 | if (StringUtils.isNotBlank(group)) { 202 | where.append(" AND group_id=? "); 203 | paramList.add(group); 204 | } 205 | if (StringUtils.isNotBlank(appName)) { 206 | where.append(" AND app_name=? "); 207 | paramList.add(appName); 208 | } 209 | if (!StringUtils.isBlank(content)) { 210 | where.append(" AND content LIKE ? "); 211 | paramList.add(content); 212 | } 213 | return new MapperResult(sql + where + " LIMIT " + context.getStartRow() + "," + context.getPageSize(), 214 | paramList); 215 | } 216 | 217 | @Override 218 | public MapperResult findConfigInfoBaseByGroupFetchRows(MapperContext context) { 219 | String sql = "SELECT id,data_id,group_id,content FROM config_info WHERE group_id=? AND tenant_id=?" + " LIMIT " 220 | + context.getStartRow() + "," + context.getPageSize(); 221 | return new MapperResult(sql, CollectionUtils.list(context.getWhereParameter(FieldConstant.GROUP_ID), 222 | context.getWhereParameter(FieldConstant.TENANT_ID))); 223 | } 224 | 225 | @Override 226 | public MapperResult findConfigInfoLike4PageFetchRows(MapperContext context) { 227 | final String tenant = (String) context.getWhereParameter(FieldConstant.TENANT_ID); 228 | final String dataId = (String) context.getWhereParameter(FieldConstant.DATA_ID); 229 | final String group = (String) context.getWhereParameter(FieldConstant.GROUP_ID); 230 | final String appName = (String) context.getWhereParameter(FieldConstant.APP_NAME); 231 | final String content = (String) context.getWhereParameter(FieldConstant.CONTENT); 232 | 233 | List paramList = new ArrayList<>(); 234 | 235 | final String sqlFetchRows = "SELECT id,data_id,group_id,tenant_id,app_name,content,encrypted_data_key FROM config_info"; 236 | StringBuilder where = new StringBuilder(" WHERE "); 237 | where.append(" tenant_id LIKE ? "); 238 | paramList.add(tenant); 239 | 240 | if (!StringUtils.isBlank(dataId)) { 241 | where.append(" AND data_id LIKE ? "); 242 | paramList.add(dataId); 243 | 244 | } 245 | if (!StringUtils.isBlank(group)) { 246 | where.append(" AND group_id LIKE ? "); 247 | paramList.add(group); 248 | } 249 | if (!StringUtils.isBlank(appName)) { 250 | where.append(" AND app_name = ? "); 251 | paramList.add(appName); 252 | } 253 | if (!StringUtils.isBlank(content)) { 254 | where.append(" AND content LIKE ? "); 255 | paramList.add(content); 256 | } 257 | return new MapperResult(sqlFetchRows + where + " LIMIT " + context.getStartRow() + "," + context.getPageSize(), 258 | paramList); 259 | } 260 | 261 | @Override 262 | public MapperResult findAllConfigInfoFetchRows(MapperContext context) { 263 | String sql = "SELECT t.id,data_id,group_id,tenant_id,app_name,content,md5 " 264 | + " FROM ( SELECT id FROM config_info WHERE tenant_id LIKE ? ORDER BY id LIMIT ?,? )" 265 | + " g, config_info t WHERE g.id = t.id "; 266 | return new MapperResult(sql, CollectionUtils.list(context.getWhereParameter(FieldConstant.TENANT_ID), 267 | context.getStartRow(), context.getPageSize())); 268 | } 269 | 270 | @Override 271 | public String getDataSource() { 272 | return DataSourceConstant.DM; 273 | } 274 | 275 | } 276 | -------------------------------------------------------------------------------- /src/main/java/com/pig4cloud/plugin/impl/dm/ConfigInfoTagMapperByDm.java: -------------------------------------------------------------------------------- 1 | package com.pig4cloud.plugin.impl.dm; 2 | 3 | import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoTagMapper; 4 | import com.alibaba.nacos.plugin.datasource.model.MapperContext; 5 | import com.alibaba.nacos.plugin.datasource.model.MapperResult; 6 | import com.pig4cloud.plugin.constants.DataSourceConstant; 7 | 8 | import java.util.Collections; 9 | 10 | public class ConfigInfoTagMapperByDm extends DmAbstractMapper implements ConfigInfoTagMapper { 11 | 12 | @Override 13 | public MapperResult findAllConfigInfoTagForDumpAllFetchRows(MapperContext context) { 14 | String sql = " SELECT t.id,data_id,group_id,tenant_id,tag_id,app_name,content,md5,gmt_modified " 15 | + " FROM ( SELECT id FROM config_info_tag ORDER BY id LIMIT " + context.getStartRow() + "," 16 | + context.getPageSize() + " ) " + "g, config_info_tag t WHERE g.id = t.id "; 17 | return new MapperResult(sql, Collections.emptyList()); 18 | } 19 | 20 | @Override 21 | public String getDataSource() { 22 | return DataSourceConstant.DM; 23 | } 24 | 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/com/pig4cloud/plugin/impl/dm/ConfigTagsRelationMapperByDm.java: -------------------------------------------------------------------------------- 1 | package com.pig4cloud.plugin.impl.dm; 2 | 3 | import com.alibaba.nacos.common.utils.StringUtils; 4 | import com.alibaba.nacos.plugin.datasource.constants.FieldConstant; 5 | import com.alibaba.nacos.plugin.datasource.mapper.ConfigTagsRelationMapper; 6 | import com.alibaba.nacos.plugin.datasource.model.MapperContext; 7 | import com.alibaba.nacos.plugin.datasource.model.MapperResult; 8 | import com.pig4cloud.plugin.constants.DataSourceConstant; 9 | 10 | import java.util.ArrayList; 11 | import java.util.List; 12 | 13 | public class ConfigTagsRelationMapperByDm extends DmAbstractMapper implements ConfigTagsRelationMapper { 14 | 15 | @Override 16 | public MapperResult findConfigInfo4PageFetchRows(MapperContext context) { 17 | final String tenant = (String) context.getWhereParameter(FieldConstant.TENANT_ID); 18 | final String dataId = (String) context.getWhereParameter(FieldConstant.DATA_ID); 19 | final String group = (String) context.getWhereParameter(FieldConstant.GROUP_ID); 20 | final String appName = (String) context.getWhereParameter(FieldConstant.APP_NAME); 21 | final String content = (String) context.getWhereParameter(FieldConstant.CONTENT); 22 | final String[] tagArr = (String[]) context.getWhereParameter(FieldConstant.TAG_ARR); 23 | 24 | List paramList = new ArrayList<>(); 25 | StringBuilder where = new StringBuilder(" WHERE "); 26 | final String sql = "SELECT a.id,a.data_id,a.group_id,a.tenant_id,a.app_name,a.content FROM config_info a LEFT JOIN " 27 | + "config_tags_relation b ON a.id=b.id"; 28 | 29 | where.append(" a.tenant_id=? "); 30 | paramList.add(tenant); 31 | 32 | if (StringUtils.isNotBlank(dataId)) { 33 | where.append(" AND a.data_id=? "); 34 | paramList.add(dataId); 35 | } 36 | if (StringUtils.isNotBlank(group)) { 37 | where.append(" AND a.group_id=? "); 38 | paramList.add(group); 39 | } 40 | if (StringUtils.isNotBlank(appName)) { 41 | where.append(" AND a.app_name=? "); 42 | paramList.add(appName); 43 | } 44 | if (!StringUtils.isBlank(content)) { 45 | where.append(" AND a.content LIKE ? "); 46 | paramList.add(content); 47 | } 48 | where.append(" AND b.tag_name IN ("); 49 | for (int i = 0; i < tagArr.length; i++) { 50 | if (i != 0) { 51 | where.append(", "); 52 | } 53 | where.append('?'); 54 | paramList.add(tagArr[i]); 55 | } 56 | where.append(") "); 57 | return new MapperResult(sql + where + " LIMIT " + context.getStartRow() + "," + context.getPageSize(), 58 | paramList); 59 | } 60 | 61 | @Override 62 | public MapperResult findConfigInfoLike4PageFetchRows(MapperContext context) { 63 | final String tenant = (String) context.getWhereParameter(FieldConstant.TENANT_ID); 64 | final String dataId = (String) context.getWhereParameter(FieldConstant.DATA_ID); 65 | final String group = (String) context.getWhereParameter(FieldConstant.GROUP_ID); 66 | final String appName = (String) context.getWhereParameter(FieldConstant.APP_NAME); 67 | final String content = (String) context.getWhereParameter(FieldConstant.CONTENT); 68 | final String[] tagArr = (String[]) context.getWhereParameter(FieldConstant.TAG_ARR); 69 | 70 | List paramList = new ArrayList<>(); 71 | 72 | StringBuilder where = new StringBuilder(" WHERE "); 73 | final String sqlFetchRows = "SELECT a.id,a.data_id,a.group_id,a.tenant_id,a.app_name,a.content " 74 | + "FROM config_info a LEFT JOIN config_tags_relation b ON a.id=b.id "; 75 | 76 | where.append(" a.tenant_id LIKE ? "); 77 | paramList.add(tenant); 78 | if (!StringUtils.isBlank(dataId)) { 79 | where.append(" AND a.data_id LIKE ? "); 80 | paramList.add(dataId); 81 | } 82 | if (!StringUtils.isBlank(group)) { 83 | where.append(" AND a.group_id LIKE ? "); 84 | paramList.add(group); 85 | } 86 | if (!StringUtils.isBlank(appName)) { 87 | where.append(" AND a.app_name = ? "); 88 | paramList.add(appName); 89 | } 90 | if (!StringUtils.isBlank(content)) { 91 | where.append(" AND a.content LIKE ? "); 92 | paramList.add(content); 93 | } 94 | 95 | where.append(" AND b.tag_name IN ("); 96 | for (int i = 0; i < tagArr.length; i++) { 97 | if (i != 0) { 98 | where.append(", "); 99 | } 100 | where.append('?'); 101 | paramList.add(tagArr[i]); 102 | } 103 | where.append(") "); 104 | return new MapperResult(sqlFetchRows + where + " LIMIT " + context.getStartRow() + "," + context.getPageSize(), 105 | paramList); 106 | } 107 | 108 | @Override 109 | public String getDataSource() { 110 | return DataSourceConstant.DM; 111 | } 112 | 113 | } 114 | -------------------------------------------------------------------------------- /src/main/java/com/pig4cloud/plugin/impl/dm/DmAbstractMapper.java: -------------------------------------------------------------------------------- 1 | package com.pig4cloud.plugin.impl.dm; 2 | 3 | import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper; 4 | import com.pig4cloud.plugin.constants.DataSourceConstant; 5 | 6 | /** 7 | * DM 抽象映射器 8 | * 9 | * @author lengleng 10 | * @date 2024/08/25 11 | */ 12 | public abstract class DmAbstractMapper extends AbstractMapper { 13 | 14 | /** 15 | * 获取数据源 16 | * @return {@link String } 17 | */ 18 | @Override 19 | public String getDataSource() { 20 | return DataSourceConstant.DM; 21 | } 22 | 23 | /** 24 | * get 函数 25 | * @param functionName 函数名称 26 | * @return {@link String } 27 | */ 28 | @Override 29 | public String getFunction(String functionName) { 30 | return "NOW()"; 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /src/main/java/com/pig4cloud/plugin/impl/dm/GroupCapacityMapperByDm.java: -------------------------------------------------------------------------------- 1 | package com.pig4cloud.plugin.impl.dm; 2 | 3 | import com.alibaba.nacos.common.utils.CollectionUtils; 4 | import com.alibaba.nacos.plugin.datasource.constants.FieldConstant; 5 | import com.alibaba.nacos.plugin.datasource.mapper.GroupCapacityMapper; 6 | import com.alibaba.nacos.plugin.datasource.model.MapperContext; 7 | import com.alibaba.nacos.plugin.datasource.model.MapperResult; 8 | import com.pig4cloud.plugin.constants.DataSourceConstant; 9 | 10 | public class GroupCapacityMapperByDm extends DmAbstractMapper implements GroupCapacityMapper { 11 | 12 | @Override 13 | public MapperResult selectGroupInfoBySize(MapperContext context) { 14 | String sql = "SELECT id, group_id FROM group_capacity WHERE id > ? LIMIT ?"; 15 | return new MapperResult(sql, 16 | CollectionUtils.list(context.getWhereParameter(FieldConstant.ID), context.getPageSize())); 17 | } 18 | 19 | @Override 20 | public String getDataSource() { 21 | return DataSourceConstant.DM; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/pig4cloud/plugin/impl/dm/HistoryConfigInfoMapperByDm.java: -------------------------------------------------------------------------------- 1 | package com.pig4cloud.plugin.impl.dm; 2 | 3 | import com.alibaba.nacos.common.utils.CollectionUtils; 4 | import com.alibaba.nacos.plugin.datasource.constants.FieldConstant; 5 | import com.alibaba.nacos.plugin.datasource.mapper.HistoryConfigInfoMapper; 6 | import com.alibaba.nacos.plugin.datasource.model.MapperContext; 7 | import com.alibaba.nacos.plugin.datasource.model.MapperResult; 8 | import com.pig4cloud.plugin.constants.DataSourceConstant; 9 | 10 | public class HistoryConfigInfoMapperByDm extends DmAbstractMapper implements HistoryConfigInfoMapper { 11 | 12 | @Override 13 | public MapperResult removeConfigHistory(MapperContext context) { 14 | String sql = "DELETE FROM his_config_info WHERE gmt_modified < ? LIMIT ?"; 15 | return new MapperResult(sql, CollectionUtils.list(context.getWhereParameter(FieldConstant.START_TIME), 16 | context.getWhereParameter(FieldConstant.LIMIT_SIZE))); 17 | } 18 | 19 | @Override 20 | public MapperResult pageFindConfigHistoryFetchRows(MapperContext context) { 21 | String sql = "SELECT nid,data_id,group_id,tenant_id,app_name,src_ip,src_user,op_type,ext_info,publish_type,gray_name,gmt_create,gmt_modified FROM his_config_info WHERE data_id = ? AND group_id = ? AND tenant_id = ? ORDER BY nid DESC LIMIT " 22 | + context.getStartRow() + "," + context.getPageSize(); 23 | return new MapperResult(sql, CollectionUtils.list(new Object[] { context.getWhereParameter("dataId"), 24 | context.getWhereParameter("groupId"), context.getWhereParameter("tenantId") })); 25 | } 26 | 27 | @Override 28 | public String getDataSource() { 29 | return DataSourceConstant.DM; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/pig4cloud/plugin/impl/dm/TenantCapacityMapperByDm.java: -------------------------------------------------------------------------------- 1 | package com.pig4cloud.plugin.impl.dm; 2 | 3 | import com.alibaba.nacos.common.utils.CollectionUtils; 4 | import com.alibaba.nacos.plugin.datasource.constants.FieldConstant; 5 | import com.alibaba.nacos.plugin.datasource.mapper.TenantCapacityMapper; 6 | import com.alibaba.nacos.plugin.datasource.model.MapperContext; 7 | import com.alibaba.nacos.plugin.datasource.model.MapperResult; 8 | import com.pig4cloud.plugin.constants.DataSourceConstant; 9 | 10 | public class TenantCapacityMapperByDm extends DmAbstractMapper implements TenantCapacityMapper { 11 | 12 | @Override 13 | public MapperResult getCapacityList4CorrectUsage(MapperContext context) { 14 | String sql = "SELECT id, tenant_id FROM tenant_capacity WHERE id>? LIMIT ?"; 15 | return new MapperResult(sql, CollectionUtils.list(context.getWhereParameter(FieldConstant.ID), 16 | context.getWhereParameter(FieldConstant.LIMIT_SIZE))); 17 | } 18 | 19 | @Override 20 | public String getDataSource() { 21 | return DataSourceConstant.DM; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /src/main/java/com/pig4cloud/plugin/impl/dm/TenantInfoMapperByDm.java: -------------------------------------------------------------------------------- 1 | package com.pig4cloud.plugin.impl.dm; 2 | 3 | import com.alibaba.nacos.plugin.datasource.constants.TableConstant; 4 | import com.alibaba.nacos.plugin.datasource.mapper.TenantInfoMapper; 5 | 6 | public class TenantInfoMapperByDm extends DmAbstractMapper implements TenantInfoMapper { 7 | 8 | @Override 9 | public String getTableName() { 10 | return TableConstant.TENANT_INFO; 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/main/resources/META-INF/services/com.alibaba.nacos.plugin.datasource.mapper.Mapper: -------------------------------------------------------------------------------- 1 | com.pig4cloud.plugin.impl.dm.ConfigInfoGrayMapperByDm 2 | com.pig4cloud.plugin.impl.dm.ConfigInfoBetaMapperByDm 3 | com.pig4cloud.plugin.impl.dm.ConfigInfoMapperByDm 4 | com.pig4cloud.plugin.impl.dm.ConfigInfoTagMapperByDm 5 | com.pig4cloud.plugin.impl.dm.ConfigTagsRelationMapperByDm 6 | com.pig4cloud.plugin.impl.dm.HistoryConfigInfoMapperByDm 7 | com.pig4cloud.plugin.impl.dm.TenantInfoMapperByDm 8 | com.pig4cloud.plugin.impl.dm.TenantCapacityMapperByDm 9 | com.pig4cloud.plugin.impl.dm.GroupCapacityMapperByDm 10 | --------------------------------------------------------------------------------