diff --git a/.github/workflows/github-release.yml b/.github/workflows/github-release.yml new file mode 100644 index 0000000..a047676 --- /dev/null +++ b/.github/workflows/github-release.yml @@ -0,0 +1,35 @@ +name: publish github release + +on: + workflow_dispatch: + inputs: + releaseversion: + description: 'Release version' + required: true + default: '0.3.0' + + +jobs: + publish-github-release: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Generate changelog + id: changelog + uses: metcalfc/changelog-generator@v4.3.1 + with: + myToken: ${{ secrets.GH_TOKEN }} + + - name: Create GitHub Release + id: create_release + uses: actions/create-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} + with: + tag_name: ${{ github.event.inputs.releaseversion }} + release_name: ${{ github.event.inputs.releaseversion }} + body: | + ### Things that changed in this release + ${{ steps.changelog.outputs.changelog }} + draft: false + prerelease: ${{ contains(github.event.inputs.releaseversion, '-') }} diff --git a/.github/workflows/oss-release-deploy.yml b/.github/workflows/oss-release-deploy.yml deleted file mode 100644 index 85d1b55..0000000 --- a/.github/workflows/oss-release-deploy.yml +++ /dev/null @@ -1,36 +0,0 @@ -name: publish SNAPSHOT maven package -on: - workflow_dispatch: - push: - branches: [ release ] - -jobs: - oss-release-deploy: - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v3 - - name: Set up JDK 8 - uses: actions/setup-java@v2 - with: - java-version: '8' - distribution: 'adopt' - cache: maven - - - name: Setup Maven Central - uses: actions/setup-java@v3 - with: # overwrite settings.xml - java-version: '8' - distribution: 'adopt' - server-id: sonatype - server-username: OSSRH_USERNAME - server-password: OSSRH_PASSWORD - gpg-private-key: ${{ secrets.MAVEN_GPG_KEY }} - gpg-passphrase: MAVEN_GPG_PASSPHRASE - - - name: Publish to Maven Central - run: mvn clean deploy -P release -Dmaven.test.skip=true - env: - MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }} - OSSRH_USERNAME: ${{ secrets.OSSRH_USERNAME }} - OSSRH_PASSWORD: ${{ secrets.OSSRH_PASSWORD }} diff --git a/.github/workflows/oss-snapshot-deploy.yml b/.github/workflows/oss-snapshot-deploy.yml deleted file mode 100644 index fc60c5f..0000000 --- a/.github/workflows/oss-snapshot-deploy.yml +++ /dev/null @@ -1,36 +0,0 @@ -name: publish SNAPSHOT maven package -on: - workflow_dispatch: - push: - branches: [ master ] - -jobs: - oss-snapshot-deploy: - runs-on: ubuntu-latest - steps: - - name: Checkout - uses: actions/checkout@v3 - - name: Set up JDK 17 - uses: actions/setup-java@v2 - with: - java-version: '17' - distribution: 'adopt' - cache: maven - - - name: Setup Maven Central - uses: actions/setup-java@v3 - with: # overwrite settings.xml - java-version: '8' - distribution: 'adopt' - server-id: sonatype - server-username: OSSRH_USERNAME - server-password: OSSRH_PASSWORD - gpg-private-key: ${{ secrets.MAVEN_GPG_KEY }} - gpg-passphrase: MAVEN_GPG_PASSPHRASE - - - name: Publish to Maven Central - run: mvn clean deploy -P snapshot -Dmaven.test.skip=true - env: - MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }} - OSSRH_USERNAME: ${{ secrets.OSSRH_USERNAME }} - OSSRH_PASSWORD: ${{ secrets.OSSRH_PASSWORD }} diff --git a/README.md b/README.md index bb4008d..c72c92d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ -Nacos 从 2.2.0 版本开始,可通过 SPI 机制注入多数据源实现插件,并在引入对应数据源实现后,便可在 Nacos 启动时通过读取 application.properties 配置文件中 spring.datasource.platform 配置项选择加载对应多数据源插件. +Nacos 从 2.2.0 版本开始,可通过 SPI 机制注入多数据源实现插件,并在引入对应数据源实现后,便可在 Nacos 启动时通过读取 +application.properties 配置文件中 spring.datasource.platform 配置项选择加载对应多数据源插件. ![Nacos 插件化实现 ](https://minio.pigx.top/oss/202212/1671179590.jpg) @@ -13,27 +14,34 @@ Nacos 从 2.2.0 版本开始,可通过 SPI 机制注入多数据源实现插件, > 依赖已上传 maven 中央仓库,请勿使用阿里云代理 +| NACOS 版本 | 插件版本 | +|---------------|-------| +| 2.2.0 - 2.3.0 | 0.2.0 | +| 2.3.1 - 2.3.2 | 0.3.0 | ```xml com.pig4cloud.plugin nacos-datasource-plugin-dm8 - 0.0.2 + ${VERSION} - com.dameng - DmJdbcDriver18 - 8.1.1.193 +com.dameng +DmJdbcDriver18 +8.1.1.193 ``` -## 2.导入 nacos dm8 数据库脚本 -在达梦数据库管理工具上创建名为 `nacos` 的表空间,导入一下数据库脚本 -[数据库脚本](./sql/nacos.sql) +## 2. 使用达梦DTS迁移工具 + +达梦DTS能够支持将Nacos原版的MYSQL数据库迁移至支持达梦数据库的脚本。 + +1718112771 ## 3.配置 nacos 数据源链接信息 + ``` db: num: 1 @@ -46,8 +54,8 @@ db: driver-class-name: dm.jdbc.driver.DmDriver ``` - ## 4.执行数据库平台 + ``` spring: datasource: diff --git a/pom.xml b/pom.xml index 6fd856c..e6637ab 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ 4.0.0 com.pig4cloud.plugin nacos-datasource-plugin-dm8 - 0.0.2 + 0.0.3 nacos-datasource-plugin-dm8 nacos-datasource-plugin-dm8 https://pig4cloud.com @@ -25,7 +25,7 @@ - 2.2.4 + 2.3.2 0.0.32 3.8.1 1.8 diff --git a/sql/nacos.sql b/sql/nacos.sql deleted file mode 100644 index cb7baa3..0000000 --- a/sql/nacos.sql +++ /dev/null @@ -1,198 +0,0 @@ -CREATE TABLE "CONFIG_INFO" -( - "ID" INT IDENTITY(1, 1) NOT NULL, - "DATA_ID" NVARCHAR2(255 CHAR) NOT NULL, - "GROUP_ID" NVARCHAR2(255 CHAR), - "CONTENT" TEXT NOT NULL, - "MD5" NVARCHAR2(32 CHAR), - "GMT_CREATE" DATE DEFAULT SYSDATE() NOT NULL, - "GMT_MODIFIED" DATE DEFAULT SYSDATE() NOT NULL, - "SRC_USER" TEXT, - "SRC_IP" NVARCHAR2(50 CHAR), - "APP_NAME" NVARCHAR2(128 CHAR), - "TENANT_ID" NVARCHAR2(128 CHAR) DEFAULT '', - "C_DESC" NVARCHAR2(256 CHAR), - "C_USE" NVARCHAR2(64 CHAR), - "EFFECT" NVARCHAR2(64 CHAR), - "TYPE" NVARCHAR2(64 CHAR), - "C_SCHEMA" TEXT, - "ENCRYPTED_DATA_KEY" TEXT NOT NULL, - NOT CLUSTER PRIMARY KEY("ID"), - UNIQUE("DATA_ID", "GROUP_ID", "TENANT_ID")) STORAGE(ON "MAIN", CLUSTERBTR) ; - -CREATE TABLE "CONFIG_INFO_AGGR" -( - "ID" INT IDENTITY(1, 1) NOT NULL, - "DATA_ID" NVARCHAR2(255 CHAR) NOT NULL, - "GROUP_ID" NVARCHAR2(255 CHAR) NOT NULL, - "DATUM_ID" NVARCHAR2(255 CHAR) NOT NULL, - "CONTENT" TEXT NOT NULL, - "GMT_MODIFIED" DATE NOT NULL, - "APP_NAME" NVARCHAR2(128 CHAR), - "TENANT_ID" NVARCHAR2(128 CHAR), - NOT CLUSTER PRIMARY KEY("ID")) STORAGE(ON "MAIN", CLUSTERBTR) ; - -CREATE TABLE "CONFIG_INFO_BETA" -( - "ID" INT IDENTITY(1, 1) NOT NULL, - "DATA_ID" NVARCHAR2(255 CHAR) NOT NULL, - "GROUP_ID" NVARCHAR2(128 CHAR) NOT NULL, - "APP_NAME" NVARCHAR2(128 CHAR), - "CONTENT" TEXT NOT NULL, - "BETA_IPS" TEXT, - "MD5" NVARCHAR2(32 CHAR), - "GMT_CREATE" DATE NOT NULL, - "GMT_MODIFIED" DATE NOT NULL, - "SRC_USER" TEXT, - "SRC_IP" NVARCHAR2(50 CHAR), - "TENANT_ID" NVARCHAR2(128 CHAR), - "ENCRYPTED_DATA_KEY" TEXT NOT NULL, - NOT CLUSTER PRIMARY KEY("ID")) STORAGE(ON "MAIN", CLUSTERBTR) ; - -CREATE TABLE "CONFIG_INFO_TAG" -( - "ID" INT IDENTITY(1, 1) NOT NULL, - "DATA_ID" NVARCHAR2(255 CHAR) NOT NULL, - "GROUP_ID" NVARCHAR2(128 CHAR) NOT NULL, - "TENANT_ID" NVARCHAR2(128 CHAR), - "TAG_ID" NVARCHAR2(128 CHAR) NOT NULL, - "APP_NAME" NVARCHAR2(128 CHAR), - "CONTENT" TEXT NOT NULL, - "MD5" NVARCHAR2(32 CHAR), - "GMT_CREATE" DATE NOT NULL, - "GMT_MODIFIED" DATE NOT NULL, - "SRC_USER" TEXT, - "SRC_IP" NVARCHAR2(50 CHAR), - NOT CLUSTER PRIMARY KEY("ID")) STORAGE(ON "MAIN", CLUSTERBTR) ; - -CREATE TABLE "CONFIG_TAGS_RELATION" -( - "ID" NUMBER(20,0) NOT NULL, - "TAG_NAME" NVARCHAR2(128 CHAR) NOT NULL, - "TAG_TYPE" NVARCHAR2(64 CHAR), - "DATA_ID" NVARCHAR2(255 CHAR) NOT NULL, - "GROUP_ID" NVARCHAR2(128 CHAR) NOT NULL, - "TENANT_ID" NVARCHAR2(128 CHAR), - "NID" INT IDENTITY(1, 1) NOT NULL, - NOT CLUSTER PRIMARY KEY("NID")) STORAGE(ON "MAIN", CLUSTERBTR) ; - -CREATE TABLE "GROUP_CAPACITY" -( - "ID" INT IDENTITY(1, 1) NOT NULL, - "GROUP_ID" NVARCHAR2(128 CHAR) NOT NULL, - "QUOTA" NUMBER(11,0) NOT NULL, - "USAGE" NUMBER(11,0) NOT NULL, - "MAX_SIZE" NUMBER(11,0) NOT NULL, - "MAX_AGGR_COUNT" NUMBER(11,0) NOT NULL, - "MAX_AGGR_SIZE" NUMBER(11,0) NOT NULL, - "MAX_HISTORY_COUNT" NUMBER(11,0) NOT NULL, - "GMT_CREATE" DATE NOT NULL, - "GMT_MODIFIED" DATE NOT NULL, - NOT CLUSTER PRIMARY KEY("ID")) STORAGE(ON "MAIN", CLUSTERBTR) ; - -COMMENT ON TABLE "GROUP_CAPACITY" IS '集群、各Group容量信息表'; -COMMENT ON COLUMN "GROUP_CAPACITY"."ID" IS '主键ID'; -COMMENT ON COLUMN "GROUP_CAPACITY"."GROUP_ID" IS 'Group ID,空字符表示整个集群'; -COMMENT ON COLUMN "GROUP_CAPACITY"."QUOTA" IS '配额,0表示使用默认值'; -COMMENT ON COLUMN "GROUP_CAPACITY"."USAGE" IS '使用量'; -COMMENT ON COLUMN "GROUP_CAPACITY"."MAX_SIZE" IS '单个配置大小上限,单位为字节,0表示使用默认值'; -COMMENT ON COLUMN "GROUP_CAPACITY"."MAX_AGGR_COUNT" IS '聚合子配置最大个数,,0表示使用默认值'; -COMMENT ON COLUMN "GROUP_CAPACITY"."MAX_AGGR_SIZE" IS '单个聚合数据的子配置大小上限,单位为字节,0表示使用默认值'; -COMMENT ON COLUMN "GROUP_CAPACITY"."MAX_HISTORY_COUNT" IS '最大变更历史数量'; -COMMENT ON COLUMN "GROUP_CAPACITY"."GMT_CREATE" IS '创建时间'; -COMMENT ON COLUMN "GROUP_CAPACITY"."GMT_MODIFIED" IS '修改时间'; - - -CREATE TABLE "HIS_CONFIG_INFO" -( - "ID" INT NOT NULL, - "NID" INT IDENTITY(1, 1) NOT NULL, - "DATA_ID" NVARCHAR2(255 CHAR) NOT NULL, - "GROUP_ID" NVARCHAR2(128 CHAR) NOT NULL, - "APP_NAME" NVARCHAR2(128 CHAR), - "CONTENT" TEXT NOT NULL, - "MD5" NVARCHAR2(32 CHAR), - "GMT_CREATE" DATE DEFAULT SYSDATE() NOT NULL, - "GMT_MODIFIED" DATE DEFAULT SYSDATE() NOT NULL, - "SRC_USER" TEXT, - "SRC_IP" NVARCHAR2(50 CHAR), - "OP_TYPE" NCHAR(10), - "TENANT_ID" NVARCHAR2(128 CHAR), - "ENCRYPTED_DATA_KEY" TEXT NOT NULL, - NOT CLUSTER PRIMARY KEY("NID")) STORAGE(ON "MAIN", CLUSTERBTR) ; - -COMMENT ON TABLE "HIS_CONFIG_INFO" IS '多租户改造'; -COMMENT ON COLUMN "HIS_CONFIG_INFO"."APP_NAME" IS 'app_name'; -COMMENT ON COLUMN "HIS_CONFIG_INFO"."TENANT_ID" IS '租户字段'; -COMMENT ON COLUMN "HIS_CONFIG_INFO"."ENCRYPTED_DATA_KEY" IS '秘钥'; - - -CREATE TABLE "PERMISSIONS" -( - "ROLE" NVARCHAR2(50 CHAR) NOT NULL, - "RESOURCE" NVARCHAR2(255 CHAR) NOT NULL, - "ACTION" NVARCHAR2(8 CHAR) NOT NULL) STORAGE(ON "MAIN", CLUSTERBTR) ; - -CREATE TABLE "ROLES" -( - "USERNAME" NVARCHAR2(50 CHAR) NOT NULL, - "ROLE" NVARCHAR2(50 CHAR) NOT NULL) STORAGE(ON "MAIN", CLUSTERBTR) ; - -CREATE TABLE "TENANT_CAPACITY" -( - "ID" INT IDENTITY(1, 1) NOT NULL, - "TENANT_ID" NVARCHAR2(128 CHAR) NOT NULL, - "QUOTA" NUMBER(11,0) NOT NULL, - "USAGE" NUMBER(11,0) NOT NULL, - "MAX_SIZE" NUMBER(11,0) NOT NULL, - "MAX_AGGR_COUNT" NUMBER(11,0) NOT NULL, - "MAX_AGGR_SIZE" NUMBER(11,0) NOT NULL, - "MAX_HISTORY_COUNT" NUMBER(11,0) NOT NULL, - "GMT_CREATE" DATE NOT NULL, - "GMT_MODIFIED" DATE NOT NULL, - NOT CLUSTER PRIMARY KEY("ID")) STORAGE(ON "MAIN", CLUSTERBTR) ; - -COMMENT ON TABLE "TENANT_CAPACITY" IS '租户容量信息表'; -COMMENT ON COLUMN "TENANT_CAPACITY"."ID" IS '主键ID'; -COMMENT ON COLUMN "TENANT_CAPACITY"."TENANT_ID" IS 'Tenant ID'; -COMMENT ON COLUMN "TENANT_CAPACITY"."QUOTA" IS '配额,0表示使用默认值'; -COMMENT ON COLUMN "TENANT_CAPACITY"."USAGE" IS '使用量'; -COMMENT ON COLUMN "TENANT_CAPACITY"."MAX_SIZE" IS '单个配置大小上限,单位为字节,0表示使用默认值'; -COMMENT ON COLUMN "TENANT_CAPACITY"."MAX_AGGR_COUNT" IS '聚合子配置最大个数'; -COMMENT ON COLUMN "TENANT_CAPACITY"."MAX_AGGR_SIZE" IS '单个聚合数据的子配置大小上限,单位为字节,0表示使用默认值'; -COMMENT ON COLUMN "TENANT_CAPACITY"."MAX_HISTORY_COUNT" IS '最大变更历史数量'; -COMMENT ON COLUMN "TENANT_CAPACITY"."GMT_CREATE" IS '创建时间'; -COMMENT ON COLUMN "TENANT_CAPACITY"."GMT_MODIFIED" IS '修改时间'; - - -CREATE TABLE "TENANT_INFO" -( - "ID" INT IDENTITY(1, 1) NOT NULL, - "KP" NVARCHAR2(128 CHAR) NOT NULL, - "TENANT_ID" NVARCHAR2(128 CHAR), - "TENANT_NAME" NVARCHAR2(128 CHAR), - "TENANT_DESC" NVARCHAR2(256 CHAR), - "CREATE_SOURCE" NVARCHAR2(32 CHAR), - "GMT_CREATE" NUMBER(20,0) NOT NULL, - "GMT_MODIFIED" NUMBER(20,0) NOT NULL, - NOT CLUSTER PRIMARY KEY("ID")) STORAGE(ON "MAIN", CLUSTERBTR) ; - -COMMENT ON TABLE "TENANT_INFO" IS 'tenant_info'; -COMMENT ON COLUMN "TENANT_INFO"."ID" IS 'id'; -COMMENT ON COLUMN "TENANT_INFO"."KP" IS 'kp'; -COMMENT ON COLUMN "TENANT_INFO"."TENANT_ID" IS 'tenant_id'; -COMMENT ON COLUMN "TENANT_INFO"."TENANT_NAME" IS 'tenant_name'; -COMMENT ON COLUMN "TENANT_INFO"."TENANT_DESC" IS 'tenant_desc'; -COMMENT ON COLUMN "TENANT_INFO"."CREATE_SOURCE" IS 'create_source'; -COMMENT ON COLUMN "TENANT_INFO"."GMT_CREATE" IS '创建时间'; -COMMENT ON COLUMN "TENANT_INFO"."GMT_MODIFIED" IS '修改时间'; - - -CREATE TABLE "USERS" -( - "USERNAME" NVARCHAR2(50 CHAR) NOT NULL, - "PASSWORD" NVARCHAR2(500 CHAR) NOT NULL, - "ENABLED" NUMBER(4,0) NOT NULL) STORAGE(ON "MAIN", CLUSTERBTR) ; - -INSERT INTO USERS(USERNAME, PASSWORD, ENABLED) VALUES ( 'nacos', '$2a$10$EuWPZHzz32dJN7jexM34MOeYirDdFAZm2kuWj7VEOJhhZkDrxfvUu', 1); -INSERT INTO ROLES(USERNAME,ROLE) VALUES ( 'nacos', 'ROLE_ADMIN'); diff --git a/src/main/java/com/pig4cloud/plugin/impl/dm/ConfigInfoAggrMapperByDm.java b/src/main/java/com/pig4cloud/plugin/impl/dm/ConfigInfoAggrMapperByDm.java index 049ea0e..4b25b23 100644 --- a/src/main/java/com/pig4cloud/plugin/impl/dm/ConfigInfoAggrMapperByDm.java +++ b/src/main/java/com/pig4cloud/plugin/impl/dm/ConfigInfoAggrMapperByDm.java @@ -1,65 +1,29 @@ package com.pig4cloud.plugin.impl.dm; -import com.alibaba.nacos.plugin.datasource.constants.TableConstant; +import com.alibaba.nacos.common.utils.CollectionUtils; +import com.alibaba.nacos.plugin.datasource.constants.FieldConstant; +import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper; import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoAggrMapper; +import com.alibaba.nacos.plugin.datasource.model.MapperContext; +import com.alibaba.nacos.plugin.datasource.model.MapperResult; import com.pig4cloud.plugin.constants.DataSourceConstant; import java.util.List; -public class ConfigInfoAggrMapperByDm extends DmAbstractMapper implements ConfigInfoAggrMapper { - - public String batchRemoveAggr(List datumList) { - final StringBuilder datumString = new StringBuilder(); - for (String datum : datumList) { - datumString.append('\'').append(datum).append("',"); - } - datumString.deleteCharAt(datumString.length() - 1); - return "DELETE FROM config_info_aggr WHERE data_id = ? AND group_id = ? AND (tenant_id = ? OR tenant_id IS NULL) AND datum_id IN (" - + datumString + ")"; - } +public class ConfigInfoAggrMapperByDm extends AbstractMapper implements ConfigInfoAggrMapper { @Override - public String aggrConfigInfoCount(int size, boolean isIn) { - StringBuilder sql = new StringBuilder( - "SELECT count(*) FROM config_info_aggr WHERE data_id = ? AND group_id = ? AND (tenant_id = ? OR tenant_id IS NULL) AND datum_id"); - if (isIn) { - sql.append(" IN ("); - } - else { - sql.append(" NOT IN ("); - } - for (int i = 0; i < size; i++) { - if (i > 0) { - sql.append(", "); - } - sql.append('?'); - } - sql.append(')'); - - return sql.toString(); - } - - @Override - public String findConfigInfoAggrIsOrdered() { - return "SELECT data_id,group_id,tenant_id,datum_id,app_name,content FROM " - + "config_info_aggr WHERE data_id = ? AND group_id = ? AND (tenant_id = ? OR tenant_id IS NULL) ORDER BY datum_id"; - } + public MapperResult findConfigInfoAggrByPageFetchRows(MapperContext context) { + int startRow = context.getStartRow(); + int pageSize = context.getPageSize(); + String dataId = (String) context.getWhereParameter(FieldConstant.DATA_ID); + String groupId = (String) context.getWhereParameter(FieldConstant.GROUP_ID); + String tenantId = (String) context.getWhereParameter(FieldConstant.TENANT_ID); - @Override - public String findConfigInfoAggrByPageFetchRows(int startRow, int pageSize) { String sql = "SELECT data_id,group_id,tenant_id,datum_id,app_name,content FROM config_info_aggr WHERE data_id= ? AND " - + "group_id= ? AND (tenant_id= ? OR tenant_id IS NULL) ORDER BY datum_id"; - return buildPaginationSql(sql, startRow, pageSize); - } - - @Override - public String findAllAggrGroupByDistinct() { - return "SELECT DISTINCT data_id, group_id, tenant_id FROM config_info_aggr"; - } - - @Override - public String getTableName() { - return TableConstant.CONFIG_INFO_AGGR; + + "group_id= ? AND tenant_id= ? ORDER BY datum_id LIMIT " + startRow + "," + pageSize; + List paramList = CollectionUtils.list(dataId, groupId, tenantId); + return new MapperResult(sql, paramList); } @Override diff --git a/src/main/java/com/pig4cloud/plugin/impl/dm/ConfigInfoBetaMapperByDm.java b/src/main/java/com/pig4cloud/plugin/impl/dm/ConfigInfoBetaMapperByDm.java index 851d0d5..8ac73c7 100644 --- a/src/main/java/com/pig4cloud/plugin/impl/dm/ConfigInfoBetaMapperByDm.java +++ b/src/main/java/com/pig4cloud/plugin/impl/dm/ConfigInfoBetaMapperByDm.java @@ -1,27 +1,28 @@ package com.pig4cloud.plugin.impl.dm; -import com.alibaba.nacos.plugin.datasource.constants.TableConstant; +import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper; import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoBetaMapper; +import com.alibaba.nacos.plugin.datasource.model.MapperContext; +import com.alibaba.nacos.plugin.datasource.model.MapperResult; import com.pig4cloud.plugin.constants.DataSourceConstant; -public class ConfigInfoBetaMapperByDm extends DmAbstractMapper implements ConfigInfoBetaMapper { +import java.util.ArrayList; +import java.util.List; - @Override - public String updateConfigInfo4BetaCas() { - return "UPDATE config_info_beta SET content = ?,md5 = ?,beta_ips = ?,src_ip = ?,src_user = ?,gmt_modified = ?,app_name = ? " - + "WHERE data_id = ? AND group_id = ? AND (tenant_id = ? OR tenant_id IS NULL) AND (md5 = ? or md5 is null or md5 = '')"; - } +public class ConfigInfoBetaMapperByDm extends AbstractMapper implements ConfigInfoBetaMapper { @Override - public String findAllConfigInfoBetaForDumpAllFetchRows(int startRow, int pageSize) { - return " SELECT t.id,data_id,group_id,tenant_id,app_name,content,md5,gmt_modified,beta_ips,encrypted_data_key " - + " FROM ( SELECT rownum ROW_ID,id FROM config_info_beta WHERE ROW_ID<= " + (startRow + pageSize) - + " ORDER BY id )" + " g, config_info_beta t WHERE g.id = t.id AND g.ROW_ID >" + startRow; - } + public MapperResult findAllConfigInfoBetaForDumpAllFetchRows(MapperContext context) { + int startRow = context.getStartRow(); + int pageSize = context.getPageSize(); + String sql = " SELECT t.id,data_id,group_id,tenant_id,app_name,content,md5,gmt_modified,beta_ips,encrypted_data_key " + + " FROM ( SELECT id FROM config_info_beta ORDER BY id LIMIT " + startRow + "," + pageSize + " )" + + " g, config_info_beta t WHERE g.id = t.id "; + List paramList = new ArrayList<>(); + paramList.add(startRow); + paramList.add(pageSize); - @Override - public String getTableName() { - return TableConstant.CONFIG_INFO_BETA; + return new MapperResult(sql, paramList); } @Override diff --git a/src/main/java/com/pig4cloud/plugin/impl/dm/ConfigInfoMapperByDm.java b/src/main/java/com/pig4cloud/plugin/impl/dm/ConfigInfoMapperByDm.java index 76a5eba..ce6ff13 100644 --- a/src/main/java/com/pig4cloud/plugin/impl/dm/ConfigInfoMapperByDm.java +++ b/src/main/java/com/pig4cloud/plugin/impl/dm/ConfigInfoMapperByDm.java @@ -1,17 +1,44 @@ +/* + * Copyright 1999-2022 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package com.pig4cloud.plugin.impl.dm; import com.alibaba.nacos.common.utils.CollectionUtils; +import com.alibaba.nacos.common.utils.NamespaceUtil; import com.alibaba.nacos.common.utils.StringUtils; -import com.alibaba.nacos.plugin.datasource.constants.TableConstant; +import com.alibaba.nacos.plugin.datasource.constants.ContextConstant; +import com.alibaba.nacos.plugin.datasource.constants.FieldConstant; +import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper; import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoMapper; +import com.alibaba.nacos.plugin.datasource.model.MapperContext; +import com.alibaba.nacos.plugin.datasource.model.MapperResult; import com.pig4cloud.plugin.constants.DataSourceConstant; import java.sql.Timestamp; import java.util.ArrayList; +import java.util.Collections; import java.util.List; -import java.util.Map; -public class ConfigInfoMapperByDm extends DmAbstractMapper implements ConfigInfoMapper { +/** + * The mysql implementation of ConfigInfoMapper. + * + * @author hyx + **/ + +public class ConfigInfoMapperByDm extends AbstractMapper implements ConfigInfoMapper { private static final String DATA_ID = "dataId"; @@ -24,352 +51,221 @@ public class ConfigInfoMapperByDm extends DmAbstractMapper implements ConfigInfo private static final String TENANT = "tenant"; @Override - public String findConfigMaxId() { - return "SELECT MAX(id) FROM config_info"; - } - - @Override - public String findAllDataIdAndGroup() { - return "SELECT DISTINCT data_id, group_id FROM config_info"; - } - - @Override - public String findConfigInfoByAppCountRows() { - return "SELECT count(*) FROM config_info WHERE (tenant_id LIKE ? OR tenant_id IS NULL) AND app_name= ?"; - } - - @Override - public String findConfigInfoByAppFetchRows(int startRow, int pageSize) { + public MapperResult findConfigInfoByAppFetchRows(MapperContext context) { + final String appName = (String) context.getWhereParameter(FieldConstant.APP_NAME); + final String tenantId = (String) context.getWhereParameter(FieldConstant.TENANT_ID); String sql = "SELECT id,data_id,group_id,tenant_id,app_name,content FROM config_info" - + " WHERE (tenant_id LIKE ? OR tenant_id IS NULL) AND app_name= ?"; - return buildPaginationSql(sql, startRow, pageSize); + + " WHERE tenant_id LIKE ? AND app_name= ?" + " LIMIT " + context.getStartRow() + "," + + context.getPageSize(); + return new MapperResult(sql, CollectionUtils.list(tenantId, appName)); } @Override - public String configInfoLikeTenantCount() { - return "SELECT count(*) FROM config_info WHERE (tenant_id LIKE ? OR tenant_id IS NULL)"; + public MapperResult getTenantIdList(MapperContext context) { + String sql = "SELECT tenant_id FROM config_info WHERE tenant_id != '" + NamespaceUtil.getNamespaceDefaultId() + + "' GROUP BY tenant_id LIMIT " + context.getStartRow() + "," + context.getPageSize(); + return new MapperResult(sql, Collections.emptyList()); } @Override - public String getTenantIdList(int startRow, int pageSize) { - String sql = "SELECT tenant_id FROM config_info WHERE tenant_id IS NOT NULL GROUP BY tenant_id "; - return buildPaginationSql(sql, startRow, pageSize); + public MapperResult getGroupIdList(MapperContext context) { + String sql = "SELECT group_id FROM config_info WHERE tenant_id ='" + NamespaceUtil.getNamespaceDefaultId() + + "' GROUP BY group_id LIMIT " + context.getStartRow() + "," + context.getPageSize(); + return new MapperResult(sql, Collections.emptyList()); } @Override - public String getGroupIdList(int startRow, int pageSize) { - String sql = "SELECT group_id FROM config_info WHERE tenant_id IS NULL GROUP BY group_id "; - return buildPaginationSql(sql, startRow, pageSize); + public MapperResult findAllConfigKey(MapperContext context) { + String sql = " SELECT data_id,group_id,app_name FROM ( " + + " SELECT id FROM config_info WHERE tenant_id LIKE ? ORDER BY id LIMIT " + context.getStartRow() + "," + + context.getPageSize() + " )" + " g, config_info t WHERE g.id = t.id "; + return new MapperResult(sql, CollectionUtils.list(context.getWhereParameter(FieldConstant.TENANT_ID))); } @Override - public String findAllConfigKey(int startRow, int pageSize) { - String sql = " SELECT id,data_id,group_id,app_name FROM config_info WHERE (tenant_id LIKE ? OR tenant_id IS NULL) ORDER BY id "; - return buildPaginationSql(sql, startRow, pageSize); + public MapperResult findAllConfigInfoBaseFetchRows(MapperContext context) { + String sql = "SELECT t.id,data_id,group_id,content,md5" + + " FROM ( SELECT id FROM config_info ORDER BY id LIMIT " + context.getStartRow() + "," + + context.getPageSize() + " )" + " g, config_info t WHERE g.id = t.id "; + return new MapperResult(sql, Collections.emptyList()); } @Override - public String findAllConfigInfoBaseFetchRows(int startRow, int pageSize) { - String sql = "SELECT id,data_id,group_id,content,md5 FROM config_info ORDER BY id "; - return buildPaginationSql(sql, startRow, pageSize); + public MapperResult findAllConfigInfoFragment(MapperContext context) { + String contextParameter = context.getContextParameter(ContextConstant.NEED_CONTENT); + boolean needContent = contextParameter != null && Boolean.parseBoolean(contextParameter); + String sql = "SELECT id,data_id,group_id,tenant_id,app_name," + (needContent ? "content," : "") + + "md5,gmt_modified,type,encrypted_data_key FROM config_info WHERE id > ? ORDER BY id ASC LIMIT " + + context.getStartRow() + "," + context.getPageSize(); + return new MapperResult(sql, CollectionUtils.list(context.getWhereParameter(FieldConstant.ID))); } @Override - public String findAllConfigInfoFragment(int startRow, int pageSize) { - String sql = "SELECT id,data_id,group_id,tenant_id,app_name,content,md5,gmt_modified,type,encrypted_data_key " - + "FROM config_info WHERE id > ? ORDER BY id ASC "; - return buildPaginationSql(sql, startRow, pageSize); - } - - @Override - public String findChangeConfig() { - return "SELECT data_id, group_id, tenant_id, app_name, content, gmt_modified,encrypted_data_key " - + "FROM config_info WHERE gmt_modified >= ? AND gmt_modified <= ?"; - } - - @Override - public String findChangeConfigCountRows(Map params, final Timestamp startTime, - final Timestamp endTime) { - final String tenant = params.get(TENANT); - final String dataId = params.get(DATA_ID); - final String group = params.get(GROUP); - final String appName = params.get(APP_NAME); + public MapperResult findChangeConfigFetchRows(MapperContext context) { + final String tenant = (String) context.getWhereParameter(FieldConstant.TENANT_ID); + final String dataId = (String) context.getWhereParameter(FieldConstant.DATA_ID); + final String group = (String) context.getWhereParameter(FieldConstant.GROUP_ID); + final String appName = (String) context.getWhereParameter(FieldConstant.APP_NAME); final String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant; - final String sqlCountRows = "SELECT count(*) FROM config_info WHERE "; - String where = " 1=1 "; - if (!StringUtils.isBlank(dataId)) { - where += " AND data_id LIKE ? "; - } - if (!StringUtils.isBlank(group)) { - where += " AND group_id LIKE ? "; - } + final Timestamp startTime = (Timestamp) context.getWhereParameter(FieldConstant.START_TIME); + final Timestamp endTime = (Timestamp) context.getWhereParameter(FieldConstant.END_TIME); - if (!StringUtils.isBlank(tenantTmp)) { - where += " AND (tenant_id = ? OR tenant_id IS NULL) "; - } - - if (!StringUtils.isBlank(appName)) { - where += " AND app_name = ? "; - } - if (startTime != null) { - where += " AND gmt_modified >=? "; - } - if (endTime != null) { - where += " AND gmt_modified <=? "; - } - return sqlCountRows + where; - } + List paramList = new ArrayList<>(); - @Override - public String findChangeConfigFetchRows(Map params, final Timestamp startTime, - final Timestamp endTime, int startRow, int pageSize, long lastMaxId) { - final String tenant = params.get(TENANT); - final String dataId = params.get(DATA_ID); - final String group = params.get(GROUP); - final String appName = params.get(APP_NAME); - final String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant; - final String sqlFetchRows = "SELECT id,data_id,group_id,tenant_id,app_name,content,type,md5,gmt_modified FROM config_info WHERE "; + final String sqlFetchRows = "SELECT id,data_id,group_id,tenant_id,app_name,type,md5,gmt_modified FROM config_info WHERE "; String where = " 1=1 "; if (!StringUtils.isBlank(dataId)) { where += " AND data_id LIKE ? "; + paramList.add(dataId); } if (!StringUtils.isBlank(group)) { where += " AND group_id LIKE ? "; + paramList.add(group); } if (!StringUtils.isBlank(tenantTmp)) { - where += " AND (tenant_id = ? OR tenant_id IS NULL) "; + where += " AND tenant_id = ? "; + paramList.add(tenantTmp); } if (!StringUtils.isBlank(appName)) { where += " AND app_name = ? "; + paramList.add(appName); } if (startTime != null) { where += " AND gmt_modified >=? "; + paramList.add(startTime); } if (endTime != null) { where += " AND gmt_modified <=? "; + paramList.add(endTime); } - - String sql = sqlFetchRows + where + " AND id > " + lastMaxId + " ORDER BY id ASC"; - return buildPaginationSql(sql, 0, pageSize); + return new MapperResult( + sqlFetchRows + where + " AND id > " + context.getWhereParameter(FieldConstant.LAST_MAX_ID) + + " ORDER BY id ASC" + " LIMIT " + 0 + "," + context.getPageSize(), + paramList); } @Override - public String listGroupKeyMd5ByPageFetchRows(int startRow, int pageSize) { - String sql = "SELECT id,data_id,group_id,tenant_id,app_name,md5,type,gmt_modified,encrypted_data_key config_info ORDER BY id "; - return buildPaginationSql(sql, startRow, pageSize); + public MapperResult listGroupKeyMd5ByPageFetchRows(MapperContext context) { + String sql = "SELECT t.id,data_id,group_id,tenant_id,app_name,md5,type,gmt_modified,encrypted_data_key FROM " + + "( SELECT id FROM config_info ORDER BY id LIMIT " + context.getStartRow() + "," + + context.getPageSize() + " ) g, config_info t WHERE g.id = t.id"; + return new MapperResult(sql, Collections.emptyList()); } @Override - public String findAllConfigInfo4Export(List ids, Map params) { - String tenant = params.get("tenant"); - String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant; - String sql = "SELECT id,data_id,group_id,tenant_id,app_name,content,type,md5,gmt_create,gmt_modified,src_user,src_ip," - + "c_desc,c_use,effect,c_schema,encrypted_data_key FROM config_info"; - StringBuilder where = new StringBuilder(" WHERE "); - List paramList = new ArrayList<>(); - if (!CollectionUtils.isEmpty(ids)) { - where.append(" id IN ("); - for (int i = 0; i < ids.size(); i++) { - if (i != 0) { - where.append(", "); - } - where.append('?'); - paramList.add(ids.get(i)); - } - where.append(") "); - } - else { - where.append(" (tenant_id= ? OR tenant_id IS NULL)"); - paramList.add(tenantTmp); - if (!StringUtils.isBlank(params.get(DATA_ID))) { - where.append(" AND data_id LIKE ? "); - } - if (StringUtils.isNotBlank(params.get(GROUP))) { - where.append(" AND group_id= ? "); - } - if (StringUtils.isNotBlank(params.get(APP_NAME))) { - where.append(" AND app_name= ? "); - } - } - return sql + where; - } + public MapperResult findConfigInfoBaseLikeFetchRows(MapperContext context) { + final String dataId = (String) context.getWhereParameter(FieldConstant.DATA_ID); + final String group = (String) context.getWhereParameter(FieldConstant.GROUP_ID); + final String content = (String) context.getWhereParameter(FieldConstant.CONTENT); - @Override - public String findConfigInfoBaseLikeCountRows(Map params) { - final String sqlCountRows = "SELECT count(*) FROM config_info WHERE "; - String where = " 1=1 AND (tenant_id='' OR tenant_id IS NULL) "; + final String sqlFetchRows = "SELECT id,data_id,group_id,tenant_id,content FROM config_info WHERE "; + String where = " 1=1 AND tenant_id='" + NamespaceUtil.getNamespaceDefaultId() + "' "; - if (!StringUtils.isBlank(params.get(DATA_ID))) { - where += " AND data_id LIKE ? "; - } - if (!StringUtils.isBlank(params.get(GROUP))) { - where += " AND group_id LIKE "; - } - if (!StringUtils.isBlank(params.get(CONTENT))) { - where += " AND content LIKE ? "; - } - return sqlCountRows + where; - } + List paramList = new ArrayList<>(); - @Override - public String findConfigInfoBaseLikeFetchRows(Map params, int startRow, int pageSize) { - final String sqlFetchRows = "SELECT id,data_id,group_id,tenant_id,content FROM config_info WHERE "; - String where = " 1=1 AND (tenant_id='' OR tenant_id IS NULL) "; - if (!StringUtils.isBlank(params.get(DATA_ID))) { + if (!StringUtils.isBlank(dataId)) { where += " AND data_id LIKE ? "; + paramList.add(dataId); } - if (!StringUtils.isBlank(params.get(GROUP))) { + if (!StringUtils.isBlank(group)) { where += " AND group_id LIKE "; + paramList.add(group); } - if (!StringUtils.isBlank(params.get(CONTENT))) { + if (!StringUtils.isBlank(content)) { where += " AND content LIKE ? "; + paramList.add(content); } - String sql = sqlFetchRows + where; - return buildPaginationSql(sql, startRow, pageSize); + return new MapperResult(sqlFetchRows + where + " LIMIT " + context.getStartRow() + "," + context.getPageSize(), + paramList); } @Override - public String findConfigInfo4PageCountRows(Map params) { - final String appName = params.get(APP_NAME); - final String dataId = params.get(DATA_ID); - final String group = params.get(GROUP); - final String sqlCount = "SELECT count(*) FROM config_info"; - StringBuilder where = new StringBuilder(" WHERE "); - where.append(" ( tenant_id= ? or tenant_id is NULL )"); - if (StringUtils.isNotBlank(dataId)) { - where.append(" AND data_id=? "); - } - if (StringUtils.isNotBlank(group)) { - where.append(" AND group_id=? "); - } - if (StringUtils.isNotBlank(appName)) { - where.append(" AND app_name=? "); - } - return sqlCount + where; - } + public MapperResult findConfigInfo4PageFetchRows(MapperContext context) { + final String tenant = (String) context.getWhereParameter(FieldConstant.TENANT_ID); + final String dataId = (String) context.getWhereParameter(FieldConstant.DATA_ID); + final String group = (String) context.getWhereParameter(FieldConstant.GROUP_ID); + final String appName = (String) context.getWhereParameter(FieldConstant.APP_NAME); + final String content = (String) context.getWhereParameter(FieldConstant.CONTENT); + + List paramList = new ArrayList<>(); - @Override - public String findConfigInfo4PageFetchRows(Map params, int startRow, int pageSize) { - final String appName = params.get(APP_NAME); - final String dataId = params.get(DATA_ID); - final String group = params.get(GROUP); final String sql = "SELECT id,data_id,group_id,tenant_id,app_name,content,type,encrypted_data_key FROM config_info"; StringBuilder where = new StringBuilder(" WHERE "); - where.append(" ( tenant_id= ? or tenant_id is NULL ) "); + where.append(" tenant_id=? "); + paramList.add(tenant); if (StringUtils.isNotBlank(dataId)) { where.append(" AND data_id=? "); + paramList.add(dataId); } if (StringUtils.isNotBlank(group)) { where.append(" AND group_id=? "); + paramList.add(group); } if (StringUtils.isNotBlank(appName)) { where.append(" AND app_name=? "); + paramList.add(appName); + } + if (!StringUtils.isBlank(content)) { + where.append(" AND content LIKE ? "); + paramList.add(content); } - return buildPaginationSql(sql + where, startRow, pageSize); + return new MapperResult(sql + where + " LIMIT " + context.getStartRow() + "," + context.getPageSize(), + paramList); } @Override - public String findConfigInfoBaseByGroupFetchRows(int startRow, int pageSize) { - String sql = "SELECT id,data_id,group_id,content FROM config_info WHERE group_id=? AND ( tenant_id= ? or tenant_id is NULL )"; - return buildPaginationSql(sql, startRow, pageSize); + public MapperResult findConfigInfoBaseByGroupFetchRows(MapperContext context) { + String sql = "SELECT id,data_id,group_id,content FROM config_info WHERE group_id=? AND tenant_id=?" + " LIMIT " + + context.getStartRow() + "," + context.getPageSize(); + return new MapperResult(sql, CollectionUtils.list(context.getWhereParameter(FieldConstant.GROUP_ID), + context.getWhereParameter(FieldConstant.TENANT_ID))); } @Override - public String findConfigInfoLike4PageCountRows(Map params) { - String dataId = params.get(DATA_ID); - String group = params.get(GROUP); - final String appName = params.get(APP_NAME); - final String content = params.get(CONTENT); - final String sqlCountRows = "SELECT count(*) FROM config_info"; - StringBuilder where = new StringBuilder(" WHERE "); - where.append(" (tenant_id LIKE ? OR tenant_id IS NULL) "); - if (!StringUtils.isBlank(dataId)) { - where.append(" AND data_id LIKE ? "); - } - if (!StringUtils.isBlank(group)) { - where.append(" AND group_id LIKE ? "); - } - if (!StringUtils.isBlank(appName)) { - where.append(" AND app_name = ? "); - } - if (!StringUtils.isBlank(content)) { - where.append(" AND content LIKE ? "); - } - return sqlCountRows + where; - } + public MapperResult findConfigInfoLike4PageFetchRows(MapperContext context) { + final String tenant = (String) context.getWhereParameter(FieldConstant.TENANT_ID); + final String dataId = (String) context.getWhereParameter(FieldConstant.DATA_ID); + final String group = (String) context.getWhereParameter(FieldConstant.GROUP_ID); + final String appName = (String) context.getWhereParameter(FieldConstant.APP_NAME); + final String content = (String) context.getWhereParameter(FieldConstant.CONTENT); + + List paramList = new ArrayList<>(); - @Override - public String findConfigInfoLike4PageFetchRows(Map params, int startRow, int pageSize) { - String dataId = params.get(DATA_ID); - String group = params.get(GROUP); - final String appName = params.get(APP_NAME); - final String content = params.get(CONTENT); final String sqlFetchRows = "SELECT id,data_id,group_id,tenant_id,app_name,content,encrypted_data_key FROM config_info"; StringBuilder where = new StringBuilder(" WHERE "); - where.append(" (tenant_id LIKE ? OR tenant_id IS NULL) "); + where.append(" tenant_id LIKE ? "); + paramList.add(tenant); + if (!StringUtils.isBlank(dataId)) { where.append(" AND data_id LIKE ? "); + paramList.add(dataId); + } if (!StringUtils.isBlank(group)) { where.append(" AND group_id LIKE ? "); + paramList.add(group); } if (!StringUtils.isBlank(appName)) { where.append(" AND app_name = ? "); + paramList.add(appName); } if (!StringUtils.isBlank(content)) { where.append(" AND content LIKE ? "); + paramList.add(content); } - return buildPaginationSql(sqlFetchRows + where, startRow, pageSize); - } - - @Override - public String findAllConfigInfoFetchRows(int startRow, int pageSize) { - String sql = "SELECT id,data_id,group_id,tenant_id,app_name,content,md5 " - + " FROM config_info WHERE (tenant_id LIKE ? OR tenant_id IS NULL) ORDER BY id "; - return buildPaginationSql(sql, startRow, pageSize); - } - - @Override - public String findConfigInfosByIds(int idSize) { - StringBuilder sql = new StringBuilder( - "SELECT ID,data_id,group_id,tenant_id,app_name,content,md5 FROM config_info WHERE "); - sql.append("id IN ("); - for (int i = 0; i < idSize; i++) { - if (i != 0) { - sql.append(", "); - } - sql.append('?'); - } - sql.append(") "); - return sql.toString(); - } - - @Override - public String removeConfigInfoByIdsAtomic(int size) { - StringBuilder sql = new StringBuilder("DELETE FROM config_info WHERE "); - sql.append("id IN ("); - for (int i = 0; i < size; i++) { - if (i != 0) { - sql.append(", "); - } - sql.append('?'); - } - sql.append(") "); - return sql.toString(); - } - - @Override - public String updateConfigInfoAtomicCas() { - return "UPDATE config_info SET " - + "content=?, md5 = ?, src_ip=?,src_user=?,gmt_modified=?, app_name=?,c_desc=?,c_use=?,effect=?,type=?,c_schema=? " - + "WHERE data_id=? AND group_id=? AND (tenant_id=? OR tenant_id IS NULL) AND (md5=? OR md5 IS NULL OR md5='')"; + return new MapperResult(sqlFetchRows + where + " LIMIT " + context.getStartRow() + "," + context.getPageSize(), + paramList); } @Override - public String getTableName() { - return TableConstant.CONFIG_INFO; + public MapperResult findAllConfigInfoFetchRows(MapperContext context) { + String sql = "SELECT t.id,data_id,group_id,tenant_id,app_name,content,md5 " + + " FROM ( SELECT id FROM config_info WHERE tenant_id LIKE ? ORDER BY id LIMIT ?,? )" + + " g, config_info t WHERE g.id = t.id "; + return new MapperResult(sql, CollectionUtils.list(context.getWhereParameter(FieldConstant.TENANT_ID), + context.getStartRow(), context.getPageSize())); } @Override diff --git a/src/main/java/com/pig4cloud/plugin/impl/dm/ConfigInfoTagMapperByDm.java b/src/main/java/com/pig4cloud/plugin/impl/dm/ConfigInfoTagMapperByDm.java index 42f23f9..9f3eb69 100644 --- a/src/main/java/com/pig4cloud/plugin/impl/dm/ConfigInfoTagMapperByDm.java +++ b/src/main/java/com/pig4cloud/plugin/impl/dm/ConfigInfoTagMapperByDm.java @@ -1,27 +1,21 @@ package com.pig4cloud.plugin.impl.dm; -import com.alibaba.nacos.plugin.datasource.constants.TableConstant; +import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper; import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoTagMapper; +import com.alibaba.nacos.plugin.datasource.model.MapperContext; +import com.alibaba.nacos.plugin.datasource.model.MapperResult; import com.pig4cloud.plugin.constants.DataSourceConstant; -public class ConfigInfoTagMapperByDm extends DmAbstractMapper implements ConfigInfoTagMapper { +import java.util.Collections; - @Override - public String updateConfigInfo4TagCas() { - return "UPDATE config_info_tag SET content = ?, md5 = ?, src_ip = ?,src_user = ?,gmt_modified = ?,app_name = ? " - + "WHERE data_id = ? AND group_id = ? AND (tenant_id = ? OR tenant_id IS NULL) AND tag_id = ? AND (md5 = ? OR md5 IS NULL OR md5 = '')"; - } - - @Override - public String findAllConfigInfoTagForDumpAllFetchRows(int startRow, int pageSize) { - return " SELECT t.id,data_id,group_id,tenant_id,tag_id,app_name,content,md5,gmt_modified " - + " FROM ( SELECT id FROM config_info_tag WHERE ROWNUM > " + startRow + " AND ROWNUM <=" - + (startRow + pageSize) + "ORDER BY id " + " ) " + "g, config_info_tag t WHERE g.id = t.id "; - } +public class ConfigInfoTagMapperByDm extends AbstractMapper implements ConfigInfoTagMapper { @Override - public String getTableName() { - return TableConstant.CONFIG_INFO_TAG; + public MapperResult findAllConfigInfoTagForDumpAllFetchRows(MapperContext context) { + String sql = " SELECT t.id,data_id,group_id,tenant_id,tag_id,app_name,content,md5,gmt_modified " + + " FROM ( SELECT id FROM config_info_tag ORDER BY id LIMIT " + context.getStartRow() + "," + + context.getPageSize() + " ) " + "g, config_info_tag t WHERE g.id = t.id "; + return new MapperResult(sql, Collections.emptyList()); } @Override diff --git a/src/main/java/com/pig4cloud/plugin/impl/dm/ConfigTagsRelationMapperByDm.java b/src/main/java/com/pig4cloud/plugin/impl/dm/ConfigTagsRelationMapperByDm.java index 8e78eb2..db82dd5 100644 --- a/src/main/java/com/pig4cloud/plugin/impl/dm/ConfigTagsRelationMapperByDm.java +++ b/src/main/java/com/pig4cloud/plugin/impl/dm/ConfigTagsRelationMapperByDm.java @@ -1,147 +1,109 @@ package com.pig4cloud.plugin.impl.dm; import com.alibaba.nacos.common.utils.StringUtils; -import com.alibaba.nacos.plugin.datasource.constants.TableConstant; +import com.alibaba.nacos.plugin.datasource.constants.FieldConstant; +import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper; import com.alibaba.nacos.plugin.datasource.mapper.ConfigTagsRelationMapper; +import com.alibaba.nacos.plugin.datasource.model.MapperContext; +import com.alibaba.nacos.plugin.datasource.model.MapperResult; import com.pig4cloud.plugin.constants.DataSourceConstant; -import java.util.Map; +import java.util.ArrayList; +import java.util.List; -public class ConfigTagsRelationMapperByDm extends DmAbstractMapper implements ConfigTagsRelationMapper { +public class ConfigTagsRelationMapperByDm extends AbstractMapper implements ConfigTagsRelationMapper { @Override - public String findConfigInfo4PageCountRows(final Map params, final int tagSize) { - final String appName = params.get("appName"); - final String dataId = params.get("dataId"); - final String group = params.get("group"); - StringBuilder where = new StringBuilder(" WHERE "); - final String sqlCount = "SELECT count(*) FROM config_info a LEFT JOIN config_tags_relation b ON a.id=b.id"; - where.append(" (a.tenant_id=? OR a.tenant_id IS NULL)"); - if (StringUtils.isNotBlank(dataId)) { - where.append(" AND a.data_id=? "); - } - if (StringUtils.isNotBlank(group)) { - where.append(" AND a.group_id=? "); - } - if (StringUtils.isNotBlank(appName)) { - where.append(" AND a.app_name=? "); - } - where.append(" AND b.tag_name IN ("); - for (int i = 0; i < tagSize; i++) { - if (i != 0) { - where.append(", "); - } - where.append('?'); - } - where.append(") "); - return sqlCount + where; - } - - @Override - public String findConfigInfo4PageFetchRows(Map params, int tagSize, int startRow, int pageSize) { - final String appName = params.get("appName"); - final String dataId = params.get("dataId"); - final String group = params.get("group"); + public MapperResult findConfigInfo4PageFetchRows(MapperContext context) { + final String tenant = (String) context.getWhereParameter(FieldConstant.TENANT_ID); + final String dataId = (String) context.getWhereParameter(FieldConstant.DATA_ID); + final String group = (String) context.getWhereParameter(FieldConstant.GROUP_ID); + final String appName = (String) context.getWhereParameter(FieldConstant.APP_NAME); + final String content = (String) context.getWhereParameter(FieldConstant.CONTENT); + final String[] tagArr = (String[]) context.getWhereParameter(FieldConstant.TAG_ARR); + + List paramList = new ArrayList<>(); StringBuilder where = new StringBuilder(" WHERE "); 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 " + "config_tags_relation b ON a.id=b.id"; - where.append("( a.tenant_id=? OR tenant_id IS NULL)"); + where.append(" a.tenant_id=? "); + paramList.add(tenant); if (StringUtils.isNotBlank(dataId)) { where.append(" AND a.data_id=? "); + paramList.add(dataId); } if (StringUtils.isNotBlank(group)) { where.append(" AND a.group_id=? "); + paramList.add(group); } if (StringUtils.isNotBlank(appName)) { where.append(" AND a.app_name=? "); - } - - where.append(" AND b.tag_name IN ("); - for (int i = 0; i < tagSize; i++) { - if (i != 0) { - where.append(", "); - } - where.append('?'); - } - where.append(") "); - return sql + where + " AND ROWNUM > " + sql + " AND ROWNUM <= " + (startRow + pageSize); - } - - @Override - public String findConfigInfoLike4PageCountRows(final Map params, int tagSize) { - final String appName = params.get("appName"); - final String content = params.get("content"); - final String dataId = params.get("dataId"); - final String group = params.get("group"); - StringBuilder where = new StringBuilder(" WHERE "); - final String sqlCountRows = "SELECT count(*) FROM config_info a LEFT JOIN config_tags_relation b ON a.id=b.id "; - - where.append(" a.(tenant_id LIKE ? OR tenant_id IS NULL) "); - if (!StringUtils.isBlank(dataId)) { - where.append(" AND a.data_id LIKE ? "); - } - if (!StringUtils.isBlank(group)) { - where.append(" AND a.group_id LIKE ? "); - } - if (!StringUtils.isBlank(appName)) { - where.append(" AND a.app_name = ? "); + paramList.add(appName); } if (!StringUtils.isBlank(content)) { where.append(" AND a.content LIKE ? "); + paramList.add(content); } - where.append(" AND b.tag_name IN ("); - for (int i = 0; i < tagSize; i++) { + for (int i = 0; i < tagArr.length; i++) { if (i != 0) { where.append(", "); } where.append('?'); + paramList.add(tagArr[i]); } where.append(") "); - return sqlCountRows + where; + return new MapperResult(sql + where + " LIMIT " + context.getStartRow() + "," + context.getPageSize(), + paramList); } @Override - public String findConfigInfoLike4PageFetchRows(final Map params, int tagSize, int startRow, - int pageSize) { - final String appName = params.get("appName"); - final String content = params.get("content"); - final String dataId = params.get("dataId"); - final String group = params.get("group"); + public MapperResult findConfigInfoLike4PageFetchRows(MapperContext context) { + final String tenant = (String) context.getWhereParameter(FieldConstant.TENANT_ID); + final String dataId = (String) context.getWhereParameter(FieldConstant.DATA_ID); + final String group = (String) context.getWhereParameter(FieldConstant.GROUP_ID); + final String appName = (String) context.getWhereParameter(FieldConstant.APP_NAME); + final String content = (String) context.getWhereParameter(FieldConstant.CONTENT); + final String[] tagArr = (String[]) context.getWhereParameter(FieldConstant.TAG_ARR); + + List paramList = new ArrayList<>(); + StringBuilder where = new StringBuilder(" WHERE "); final String sqlFetchRows = "SELECT a.id,a.data_id,a.group_id,a.tenant_id,a.app_name,a.content " + "FROM config_info a LEFT JOIN config_tags_relation b ON a.id=b.id "; - where.append(" a.(tenant_id LIKE ? OR tenant_id IS NULL) "); + where.append(" a.tenant_id LIKE ? "); + paramList.add(tenant); if (!StringUtils.isBlank(dataId)) { where.append(" AND a.data_id LIKE ? "); + paramList.add(dataId); } if (!StringUtils.isBlank(group)) { where.append(" AND a.group_id LIKE ? "); + paramList.add(group); } if (!StringUtils.isBlank(appName)) { where.append(" AND a.app_name = ? "); + paramList.add(appName); } if (!StringUtils.isBlank(content)) { where.append(" AND a.content LIKE ? "); + paramList.add(content); } where.append(" AND b.tag_name IN ("); - for (int i = 0; i < tagSize; i++) { + for (int i = 0; i < tagArr.length; i++) { if (i != 0) { where.append(", "); } where.append('?'); + paramList.add(tagArr[i]); } where.append(") "); - return sqlFetchRows + where + " AND ROWNUM > " + startRow + " AND ROWNUM <= " + (startRow + pageSize); - } - - @Override - public String getTableName() { - return TableConstant.CONFIG_TAGS_RELATION; + return new MapperResult(sqlFetchRows + where + " LIMIT " + context.getStartRow() + "," + context.getPageSize(), + paramList); } @Override diff --git a/src/main/java/com/pig4cloud/plugin/impl/dm/DmAbstractMapper.java b/src/main/java/com/pig4cloud/plugin/impl/dm/DmAbstractMapper.java deleted file mode 100644 index fac89a6..0000000 --- a/src/main/java/com/pig4cloud/plugin/impl/dm/DmAbstractMapper.java +++ /dev/null @@ -1,164 +0,0 @@ -package com.pig4cloud.plugin.impl.dm; - -import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper; - -import java.util.List; - -/** - * @author aeizzz - */ -public abstract class DmAbstractMapper extends AbstractMapper { - - @Override - public String select(List columns, List where) { - StringBuilder sql = new StringBuilder("SELECT "); - - for (int i = 0; i < columns.size(); i++) { - sql.append(columns.get(i)); - if (i == columns.size() - 1) { - sql.append(" "); - } - else { - sql.append(","); - } - } - sql.append("FROM "); - sql.append(getTableName()); - sql.append(" "); - - if (where.size() == 0) { - return sql.toString(); - } - - sql.append("WHERE "); - for (int i = 0; i < where.size(); i++) { - String column = where.get(i); - - // 租户列特殊处理 避免前端传空字符串是Oracle查询不到数据 - if ("tenant_id".equalsIgnoreCase(column)) { - sql.append("("); - sql.append(column).append(" = ").append("?"); - sql.append(" OR "); - sql.append(column).append(" IS NULL "); - sql.append(")"); - } - else { - sql.append(column).append(" = ").append("?"); - } - - if (i != where.size() - 1) { - sql.append(" AND "); - } - } - return sql.toString(); - } - - @Override - public String update(List columns, List where) { - StringBuilder sql = new StringBuilder(); - String method = "UPDATE "; - sql.append(method); - sql.append(getTableName()).append(" ").append("SET "); - - for (int i = 0; i < columns.size(); i++) { - sql.append(columns.get(i)).append(" = ").append("?"); - if (i != columns.size() - 1) { - sql.append(","); - } - } - - if (where.size() == 0) { - return sql.toString(); - } - - sql.append(" WHERE "); - - for (int i = 0; i < where.size(); i++) { - String column = where.get(i); - if ("tenant_id".equalsIgnoreCase(column)) { - sql.append("("); - sql.append(column).append(" = ").append("?"); - sql.append(" OR "); - sql.append(column).append(" IS NULL "); - sql.append(")"); - } - else { - sql.append(column).append(" = ").append("?"); - } - if (i != where.size() - 1) { - sql.append(" AND "); - } - } - return sql.toString(); - } - - @Override - public String delete(List params) { - StringBuilder sql = new StringBuilder(); - String method = "DELETE "; - sql.append(method).append("FROM ").append(getTableName()).append(" ").append("WHERE "); - for (int i = 0; i < params.size(); i++) { - String column = params.get(i); - if ("tenant_id".equalsIgnoreCase(column)) { - sql.append("("); - sql.append(column).append(" = ").append("?"); - sql.append(" OR "); - sql.append(column).append(" IS NULL "); - sql.append(")"); - } - else { - sql.append(column).append(" = ").append("?"); - } - if (i != params.size() - 1) { - sql.append("AND "); - } - } - - return sql.toString(); - } - - @Override - public String count(List where) { - StringBuilder sql = new StringBuilder(); - String method = "SELECT "; - sql.append(method); - sql.append("COUNT(*) FROM "); - sql.append(getTableName()); - sql.append(" "); - - if (null == where || where.size() == 0) { - return sql.toString(); - } - - sql.append("WHERE "); - for (int i = 0; i < where.size(); i++) { - String column = where.get(i); - if ("tenant_id".equalsIgnoreCase(column)) { - sql.append("("); - sql.append(column).append(" = ").append("?"); - sql.append(" OR "); - sql.append(column).append(" IS NULL "); - sql.append(")"); - } - else { - sql.append(column).append(" = ").append("?"); - } - if (i != where.size() - 1) { - sql.append(" AND "); - } - } - return sql.toString(); - } - - public String buildPaginationSql(String originalSql, int startRow, int pageSize) { - return "SELECT * FROM ( SELECT TMP2.* FROM (SELECT TMP.*, ROWNUM ROW_ID FROM ( " + originalSql - + " ) TMP) TMP2 WHERE ROWNUM <=" + (startRow + pageSize) + ") WHERE ROW_ID > " + startRow; - } - - @Override - public abstract String getTableName(); - - @Override - public abstract String getDataSource(); - -} diff --git a/src/main/java/com/pig4cloud/plugin/impl/dm/GroupCapacityMapperByDm.java b/src/main/java/com/pig4cloud/plugin/impl/dm/GroupCapacityMapperByDm.java index 1e2a118..96176a0 100644 --- a/src/main/java/com/pig4cloud/plugin/impl/dm/GroupCapacityMapperByDm.java +++ b/src/main/java/com/pig4cloud/plugin/impl/dm/GroupCapacityMapperByDm.java @@ -1,62 +1,20 @@ package com.pig4cloud.plugin.impl.dm; -import com.alibaba.nacos.plugin.datasource.constants.TableConstant; +import com.alibaba.nacos.common.utils.CollectionUtils; +import com.alibaba.nacos.plugin.datasource.constants.FieldConstant; +import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper; import com.alibaba.nacos.plugin.datasource.mapper.GroupCapacityMapper; +import com.alibaba.nacos.plugin.datasource.model.MapperContext; +import com.alibaba.nacos.plugin.datasource.model.MapperResult; import com.pig4cloud.plugin.constants.DataSourceConstant; -public class GroupCapacityMapperByDm extends DmAbstractMapper implements GroupCapacityMapper { +public class GroupCapacityMapperByDm extends AbstractMapper implements GroupCapacityMapper { @Override - public String insertIntoSelect() { - return "INSERT INTO group_capacity (group_id, quota, `usage`, `max_size`, max_aggr_count, max_aggr_size,gmt_create," - + " gmt_modified) SELECT ?, ?, count(*), ?, ?, ?, ?, ? FROM config_info"; - } - - @Override - public String insertIntoSelectByWhere() { - return "INSERT INTO group_capacity (group_id, quota,`usage`, `max_size`, max_aggr_count, max_aggr_size, gmt_create," - + " gmt_modified) SELECT ?, ?, count(*), ?, ?, ?, ?, ? FROM config_info WHERE group_id=? AND tenant_id = ''"; - } - - @Override - public String incrementUsageByWhereQuotaEqualZero() { - return "UPDATE group_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE group_id = ? AND `usage` < ? AND quota = 0"; - } - - @Override - public String incrementUsageByWhereQuotaNotEqualZero() { - return "UPDATE group_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE group_id = ? AND `usage` < quota AND quota != 0"; - } - - @Override - public String incrementUsageByWhere() { - return "UPDATE group_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE group_id = ?"; - } - - @Override - public String decrementUsageByWhere() { - return "UPDATE group_capacity SET `usage` = `usage` - 1, gmt_modified = ? WHERE group_id = ? AND `usage` > 0"; - } - - @Override - public String updateUsage() { - return "UPDATE group_capacity SET `usage` = (SELECT count(*) FROM config_info), gmt_modified = ? WHERE group_id = ?"; - } - - @Override - public String updateUsageByWhere() { - return "UPDATE group_capacity SET `usage` = (SELECT count(*) FROM config_info WHERE group_id=? AND tenant_id = '')," - + " gmt_modified = ? WHERE group_id= ?"; - } - - @Override - public String selectGroupInfoBySize() { - return "SELECT id, group_id FROM group_capacity WHERE id > ? ROWNUM > ?"; - } - - @Override - public String getTableName() { - return TableConstant.GROUP_CAPACITY; + public MapperResult selectGroupInfoBySize(MapperContext context) { + String sql = "SELECT id, group_id FROM group_capacity WHERE id > ? LIMIT ?"; + return new MapperResult(sql, + CollectionUtils.list(context.getWhereParameter(FieldConstant.ID), context.getPageSize())); } @Override diff --git a/src/main/java/com/pig4cloud/plugin/impl/dm/HistoryConfigInfoMapperByDm.java b/src/main/java/com/pig4cloud/plugin/impl/dm/HistoryConfigInfoMapperByDm.java index 1706ca3..e59cc7e 100644 --- a/src/main/java/com/pig4cloud/plugin/impl/dm/HistoryConfigInfoMapperByDm.java +++ b/src/main/java/com/pig4cloud/plugin/impl/dm/HistoryConfigInfoMapperByDm.java @@ -1,46 +1,29 @@ package com.pig4cloud.plugin.impl.dm; -import com.alibaba.nacos.plugin.datasource.constants.TableConstant; +import com.alibaba.nacos.common.utils.CollectionUtils; +import com.alibaba.nacos.plugin.datasource.constants.FieldConstant; +import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper; import com.alibaba.nacos.plugin.datasource.mapper.HistoryConfigInfoMapper; +import com.alibaba.nacos.plugin.datasource.model.MapperContext; +import com.alibaba.nacos.plugin.datasource.model.MapperResult; import com.pig4cloud.plugin.constants.DataSourceConstant; -public class HistoryConfigInfoMapperByDm extends DmAbstractMapper implements HistoryConfigInfoMapper { +public class HistoryConfigInfoMapperByDm extends AbstractMapper implements HistoryConfigInfoMapper { @Override - public String removeConfigHistory() { - return "DELETE FROM his_config_info WHERE gmt_modified < ? AND ROWNUM > ?"; + public MapperResult removeConfigHistory(MapperContext context) { + String sql = "DELETE FROM his_config_info WHERE gmt_modified < ? LIMIT ?"; + return new MapperResult(sql, CollectionUtils.list(context.getWhereParameter(FieldConstant.START_TIME), + context.getWhereParameter(FieldConstant.LIMIT_SIZE))); } @Override - public String findConfigHistoryCountByTime() { - return "SELECT count(*) FROM his_config_info WHERE gmt_modified < ?"; - } - - @Override - public String findDeletedConfig() { - return "SELECT DISTINCT data_id, group_id, tenant_id FROM his_config_info WHERE op_type = 'D' AND gmt_modified >= ? AND gmt_modified <= ?"; - } - - @Override - public String findConfigHistoryFetchRows() { - return "SELECT nid,data_id,group_id,tenant_id,app_name,src_ip,src_user,op_type,gmt_create,gmt_modified FROM his_config_info " - + "WHERE data_id = ? AND group_id = ? AND (tenant_id = ? OR tenant_id IS NULL) ORDER BY nid DESC"; - } - - @Override - public String pageFindConfigHistoryFetchRows(int i, int i1) { - throw new UnsupportedOperationException(); - } - - @Override - public String detailPreviousConfigHistory() { - return "SELECT nid,data_id,group_id,tenant_id,app_name,content,md5,src_user,src_ip,op_type,gmt_create,gmt_modified " - + "FROM his_config_info WHERE nid = (SELECT max(nid) FROM his_config_info WHERE id = ?) "; - } - - @Override - public String getTableName() { - return TableConstant.HIS_CONFIG_INFO; + public MapperResult pageFindConfigHistoryFetchRows(MapperContext context) { + String sql = "SELECT nid,data_id,group_id,tenant_id,app_name,src_ip,src_user,op_type,gmt_create,gmt_modified FROM his_config_info " + + "WHERE data_id = ? AND group_id = ? AND tenant_id = ? ORDER BY nid DESC LIMIT " + + context.getStartRow() + "," + context.getPageSize(); + return new MapperResult(sql, CollectionUtils.list(context.getWhereParameter(FieldConstant.DATA_ID), + context.getWhereParameter(FieldConstant.GROUP_ID), context.getWhereParameter(FieldConstant.TENANT_ID))); } @Override diff --git a/src/main/java/com/pig4cloud/plugin/impl/dm/TenantCapacityMapperByDm.java b/src/main/java/com/pig4cloud/plugin/impl/dm/TenantCapacityMapperByDm.java index 98427f5..98ad076 100644 --- a/src/main/java/com/pig4cloud/plugin/impl/dm/TenantCapacityMapperByDm.java +++ b/src/main/java/com/pig4cloud/plugin/impl/dm/TenantCapacityMapperByDm.java @@ -1,53 +1,20 @@ package com.pig4cloud.plugin.impl.dm; -import com.alibaba.nacos.plugin.datasource.constants.TableConstant; +import com.alibaba.nacos.common.utils.CollectionUtils; +import com.alibaba.nacos.plugin.datasource.constants.FieldConstant; +import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper; import com.alibaba.nacos.plugin.datasource.mapper.TenantCapacityMapper; +import com.alibaba.nacos.plugin.datasource.model.MapperContext; +import com.alibaba.nacos.plugin.datasource.model.MapperResult; import com.pig4cloud.plugin.constants.DataSourceConstant; -public class TenantCapacityMapperByDm extends DmAbstractMapper implements TenantCapacityMapper { +public class TenantCapacityMapperByDm extends AbstractMapper implements TenantCapacityMapper { @Override - public String incrementUsageWithDefaultQuotaLimit() { - return "UPDATE tenant_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE ((tenant_id = ? OR tenant_id IS NULL) OR tenant_id IS NULL) AND `usage` <" - + " ? AND quota = 0"; - } - - @Override - public String incrementUsageWithQuotaLimit() { - return "UPDATE tenant_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE (tenant_id = ? OR tenant_id IS NULL) AND `usage` < " - + "quota AND quota != 0"; - } - - @Override - public String incrementUsage() { - return "UPDATE tenant_capacity SET `usage` = `usage` + 1, gmt_modified = ? WHERE (tenant_id = ? OR tenant_id IS NULL)"; - } - - @Override - public String decrementUsage() { - return "UPDATE tenant_capacity SET `usage` = `usage` - 1, gmt_modified = ? WHERE (tenant_id = ? OR tenant_id IS NULL) AND `usage` > 0"; - } - - @Override - public String correctUsage() { - return "UPDATE tenant_capacity SET `usage` = (SELECT count(*) FROM config_info WHERE (tenant_id = ? OR tenant_id IS NULL)), " - + "gmt_modified = ? WHERE (tenant_id = ? OR tenant_id IS NULL)"; - } - - @Override - public String getCapacityList4CorrectUsage() { - return "SELECT id, tenant_id FROM tenant_capacity WHERE id> AND ROWNUM > ?"; - } - - @Override - public String insertTenantCapacity() { - return "INSERT INTO tenant_capacity (tenant_id, quota, `usage`, `max_size`, max_aggr_count, max_aggr_size, " - + "gmt_create, gmt_modified) SELECT ?, ?, count(*), ?, ?, ?, ?, ? FROM config_info WHERE tenant_id=? OR tenant_id IS NULL;"; - } - - @Override - public String getTableName() { - return TableConstant.TENANT_CAPACITY; + public MapperResult getCapacityList4CorrectUsage(MapperContext context) { + String sql = "SELECT id, tenant_id FROM tenant_capacity WHERE id>? LIMIT ?"; + return new MapperResult(sql, CollectionUtils.list(context.getWhereParameter(FieldConstant.ID), + context.getWhereParameter(FieldConstant.LIMIT_SIZE))); } @Override diff --git a/src/main/java/com/pig4cloud/plugin/impl/dm/TenantInfoMapperByDm.java b/src/main/java/com/pig4cloud/plugin/impl/dm/TenantInfoMapperByDm.java index 0035a71..ec805bf 100644 --- a/src/main/java/com/pig4cloud/plugin/impl/dm/TenantInfoMapperByDm.java +++ b/src/main/java/com/pig4cloud/plugin/impl/dm/TenantInfoMapperByDm.java @@ -1,10 +1,11 @@ package com.pig4cloud.plugin.impl.dm; import com.alibaba.nacos.plugin.datasource.constants.TableConstant; +import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper; import com.alibaba.nacos.plugin.datasource.mapper.TenantInfoMapper; import com.pig4cloud.plugin.constants.DataSourceConstant; -public class TenantInfoMapperByDm extends DmAbstractMapper implements TenantInfoMapper { +public class TenantInfoMapperByDm extends AbstractMapper implements TenantInfoMapper { @Override public String getTableName() {