From 40519a22d50cf487c6030793c3f83fbbbd78669c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=99=93=E5=8F=8C=20Li=20Xiao=20Shuang?= <644968328@qq.com> Date: Sun, 6 Nov 2022 15:53:05 +0800 Subject: [PATCH] [ISSUE #9475] Abstract common count method (#9481) * Abstract common count method * Abstract common count method * Abstract common count method * Abstract common count method * Abstract common count method * Abstract common count method --- .../embedded/EmbeddedStoragePersistServiceImpl.java | 6 +++--- .../extrnal/ExternalStoragePersistServiceImpl.java | 4 ++-- .../impl/derby/ConfigInfoMapperByDerby.java | 5 ----- .../impl/derby/TenantInfoMapperByDerby.java | 5 ----- .../impl/mysql/ConfigInfoMapperByMySql.java | 7 +------ .../impl/mysql/TenantInfoMapperByMySql.java | 5 ----- .../plugin/datasource/mapper/ConfigInfoMapper.java | 9 --------- .../plugin/datasource/mapper/TenantInfoMapper.java | 12 ++---------- 8 files changed, 8 insertions(+), 45 deletions(-) diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedStoragePersistServiceImpl.java b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedStoragePersistServiceImpl.java index f7cc06387..c8eca2e82 100755 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedStoragePersistServiceImpl.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedStoragePersistServiceImpl.java @@ -1092,7 +1092,7 @@ public class EmbeddedStoragePersistServiceImpl implements PersistService { PaginationHelper helper = createPaginationHelper(); ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSource, TableConstant.CONFIG_INFO); final int startRow = (pageNo - 1) * pageSize; - return helper.fetchPage(configInfoMapper.findConfigInfoBaseByDataIdCountRows(), + return helper.fetchPage(configInfoMapper.count(Arrays.asList("data_id", "tenant_id")), configInfoMapper.findConfigInfoBaseByDataIdFetchRows(startRow, pageSize), new Object[] {dataId, StringUtils.EMPTY}, pageNo, pageSize, CONFIG_INFO_BASE_ROW_MAPPER); @@ -2587,8 +2587,8 @@ public class EmbeddedStoragePersistServiceImpl implements PersistService { if (Objects.isNull(tenantId)) { throw new IllegalArgumentException("tenantId can not be null"); } - TenantInfoMapper tenantInfoMapper = mapperManager.findMapper(dataSource, TableConstant.TENANT_INFO); - String sql = tenantInfoMapper.getCountByTenantId(); + TenantInfoMapper tenantInfoMapper = mapperManager.findMapper(dataSource, TableConstant.TENANT_INFO); + String sql = tenantInfoMapper.count(Arrays.asList("tenant_id")); Integer result = databaseOperate .queryOne(sql, new String[] {tenantId}, Integer.class); if (result == null) { diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalStoragePersistServiceImpl.java b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalStoragePersistServiceImpl.java index 8fd91b85a..6ec131716 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalStoragePersistServiceImpl.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalStoragePersistServiceImpl.java @@ -1028,7 +1028,7 @@ public class ExternalStoragePersistServiceImpl implements PersistService { try { final int startRow = (pageNo - 1) * pageSize; ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSource, TableConstant.CONFIG_INFO); - return helper.fetchPage(configInfoMapper.findConfigInfoBaseByDataIdCountRows(), + return helper.fetchPage(configInfoMapper.count(Arrays.asList("data_id", "tenant_id")), configInfoMapper.findConfigInfoBaseByDataIdFetchRows(startRow, pageSize), new Object[] {dataId, StringUtils.EMPTY}, pageNo, pageSize, CONFIG_INFO_BASE_ROW_MAPPER); } catch (CannotGetJdbcConnectionException e) { @@ -2730,7 +2730,7 @@ public class ExternalStoragePersistServiceImpl implements PersistService { throw new IllegalArgumentException("tenantId can not be null"); } TenantInfoMapper tenantInfoMapper = mapperManager.findMapper(dataSource, TableConstant.TENANT_INFO); - String sql = tenantInfoMapper.getCountByTenantId(); + String sql = tenantInfoMapper.count(Arrays.asList("tenant_id")); Integer result = this.jt .queryForObject(sql, new String[] {tenantId}, Integer.class); if (result == null) { diff --git a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/impl/derby/ConfigInfoMapperByDerby.java b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/impl/derby/ConfigInfoMapperByDerby.java index 6d2f97ece..e7f290b2f 100644 --- a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/impl/derby/ConfigInfoMapperByDerby.java +++ b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/impl/derby/ConfigInfoMapperByDerby.java @@ -381,11 +381,6 @@ public class ConfigInfoMapperByDerby extends AbstractMapper implements ConfigInf return sql + where + " OFFSET " + startRow + " ROWS FETCH NEXT " + pageSize + " ROWS ONLY"; } - @Override - public String findConfigInfoBaseByDataIdCountRows() { - return "SELECT count(*) FROM config_info WHERE data_id=? AND tenant_id=?"; - } - @Override public String findConfigInfoBaseByDataIdFetchRows(int startRow, int pageSize) { return "SELECT id,data_id,group_id,content FROM config_info WHERE data_id=? " + "AND tenant_id=?" + " OFFSET " diff --git a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/impl/derby/TenantInfoMapperByDerby.java b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/impl/derby/TenantInfoMapperByDerby.java index c6c02f367..f34122685 100644 --- a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/impl/derby/TenantInfoMapperByDerby.java +++ b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/impl/derby/TenantInfoMapperByDerby.java @@ -29,11 +29,6 @@ import com.alibaba.nacos.plugin.datasource.mapper.TenantInfoMapper; public class TenantInfoMapperByDerby extends AbstractMapper implements TenantInfoMapper { - @Override - public String getCountByTenantId() { - return "SELECT count(1) FROM tenant_info WHERE tenant_id = ?"; - } - @Override public String getTableName() { return TableConstant.TENANT_INFO; diff --git a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/impl/mysql/ConfigInfoMapperByMySql.java b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/impl/mysql/ConfigInfoMapperByMySql.java index 023cb37b4..f77160f61 100644 --- a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/impl/mysql/ConfigInfoMapperByMySql.java +++ b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/impl/mysql/ConfigInfoMapperByMySql.java @@ -385,12 +385,7 @@ public class ConfigInfoMapperByMySql extends AbstractMapper implements ConfigInf } return sql + where + " LIMIT " + startRow + "," + pageSize; } - - @Override - public String findConfigInfoBaseByDataIdCountRows() { - return "SELECT count(*) FROM config_info WHERE data_id=? AND tenant_id=?"; - } - + @Override public String findConfigInfoBaseByDataIdFetchRows(int startRow, int pageSize) { return "SELECT id,data_id,group_id,content FROM config_info WHERE data_id=? AND tenant_id=?" + " LIMIT " diff --git a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/impl/mysql/TenantInfoMapperByMySql.java b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/impl/mysql/TenantInfoMapperByMySql.java index 697c095ad..cb640b35f 100644 --- a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/impl/mysql/TenantInfoMapperByMySql.java +++ b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/impl/mysql/TenantInfoMapperByMySql.java @@ -38,9 +38,4 @@ public class TenantInfoMapperByMySql extends AbstractMapper implements TenantInf public String getDataSource() { return DataSourceConstant.MYSQL; } - - @Override - public String getCountByTenantId() { - return "SELECT count(*) FROM tenant_info WHERE tenant_id = ?"; - } } diff --git a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/ConfigInfoMapper.java b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/ConfigInfoMapper.java index 98f774581..4228e2af1 100644 --- a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/ConfigInfoMapper.java +++ b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/ConfigInfoMapper.java @@ -330,15 +330,6 @@ public interface ConfigInfoMapper extends Mapper { */ String findConfigInfo4PageFetchRows(Map params, int startRow, int pageSize); - /** - * The count of querying configuration information based on dataId. - * The default sql: - * SELECT count(*) FROM config_info WHERE data_id=? AND tenant_id=? - * - * @return The sql of query count. - */ - String findConfigInfoBaseByDataIdCountRows(); - /** * Query configuration information based on dataId. * The default sql: diff --git a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/TenantInfoMapper.java b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/TenantInfoMapper.java index 217614039..281ca2a79 100644 --- a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/TenantInfoMapper.java +++ b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/TenantInfoMapper.java @@ -17,19 +17,11 @@ package com.alibaba.nacos.plugin.datasource.mapper; /** - * The tenant info mapper. + * The tenant info mapper. If there is a new non-universal sql, you can add it here. * * @author hyx **/ public interface TenantInfoMapper extends Mapper { - - /** - * Get the count of tenant information by id. - * The defaut sql: - * SELECT count(*) FROM tenant_info WHERE tenant_id = ? - * - * @return The sql of get the count of tenant information. - */ - String getCountByTenantId(); + }