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 7d20d671e12..d7c3ab24b68 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 @@ -58,6 +58,7 @@ import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoTagMapper; import com.alibaba.nacos.plugin.datasource.mapper.ConfigTagsRelationMapper; import com.alibaba.nacos.plugin.datasource.mapper.HistoryConfigInfoMapper; +import com.alibaba.nacos.plugin.datasource.mapper.TenantInfoMapper; import com.alibaba.nacos.plugin.encryption.handler.EncryptionHandler; import com.alibaba.nacos.sys.env.EnvUtil; import org.apache.commons.collections.CollectionUtils; @@ -2302,7 +2303,8 @@ public ConfigHistoryInfo detailPreviousConfigHistory(Long id) { public void insertTenantInfoAtomic(String kp, String tenantId, String tenantName, String tenantDesc, String createResoure, final long time) { - final String sql = "INSERT INTO tenant_info(kp,tenant_id,tenant_name,tenant_desc,create_source,gmt_create,gmt_modified) VALUES(?,?,?,?,?,?,?)"; + TenantInfoMapper tenantInfoMapper = (TenantInfoMapper) mapperManager.findMapper(dataSource, TableConstant.TENANT_INFO).get(); + final String sql = tenantInfoMapper.insert(Arrays.asList("kp", "tenant_id", "tenant_name", "tenant_desc", "create_source", "gmt_create", "gmt_modified")); final Object[] args = new Object[] {kp, tenantId, tenantName, tenantDesc, createResoure, time, time}; EmbeddedStorageContextUtils.addSqlContext(sql, args); @@ -2319,8 +2321,9 @@ public void insertTenantInfoAtomic(String kp, String tenantId, String tenantName @Override public void updateTenantNameAtomic(String kp, String tenantId, String tenantName, String tenantDesc) { - - final String sql = "UPDATE tenant_info SET tenant_name = ?, tenant_desc = ?, gmt_modified= ? WHERE kp=? AND tenant_id=?"; + + TenantInfoMapper tenantInfoMapper = (TenantInfoMapper) mapperManager.findMapper(dataSource, TableConstant.TENANT_INFO).get(); + final String sql = tenantInfoMapper.update(Arrays.asList("tenant_name", "tenant_desc", "gmt_modified"), Arrays.asList("kp", "tenant_id")); final Object[] args = new Object[] {tenantName, tenantDesc, System.currentTimeMillis(), kp, tenantId}; EmbeddedStorageContextUtils.addSqlContext(sql, args); @@ -2337,21 +2340,27 @@ public void updateTenantNameAtomic(String kp, String tenantId, String tenantName @Override public List findTenantByKp(String kp) { - String sql = "SELECT tenant_id,tenant_name,tenant_desc FROM tenant_info WHERE kp=?"; + TenantInfoMapper tenantInfoMapper = (TenantInfoMapper) mapperManager.findMapper(dataSource, TableConstant.TENANT_INFO).get(); + String sql = tenantInfoMapper.select(Arrays.asList("tenant_id", "tenant_name", "tenant_desc"), + Collections.singletonList("kp")); return databaseOperate.queryMany(sql, new Object[] {kp}, TENANT_INFO_ROW_MAPPER); } @Override public TenantInfo findTenantByKp(String kp, String tenantId) { - String sql = "SELECT tenant_id,tenant_name,tenant_desc FROM tenant_info WHERE kp=? AND tenant_id=?"; + TenantInfoMapper tenantInfoMapper = (TenantInfoMapper) mapperManager.findMapper(dataSource, TableConstant.TENANT_INFO).get(); + String sql = tenantInfoMapper.select(Arrays.asList("tenant_id", "tenant_name", "tenant_desc"), + Arrays.asList("kp", "tenant_id")); return databaseOperate.queryOne(sql, new Object[] {kp, tenantId}, TENANT_INFO_ROW_MAPPER); } @Override public void removeTenantInfoAtomic(final String kp, final String tenantId) { - EmbeddedStorageContextUtils.addSqlContext("DELETE FROM tenant_info WHERE kp=? AND tenant_id=?", kp, tenantId); + TenantInfoMapper tenantInfoMapper = (TenantInfoMapper) mapperManager.findMapper(dataSource, TableConstant.TENANT_INFO).get(); + + EmbeddedStorageContextUtils.addSqlContext(tenantInfoMapper.delete(Arrays.asList("kp", "tenant_id")), kp, tenantId); try { databaseOperate.update(EmbeddedStorageContextUtils.getCurrentSqlContext()); } finally { @@ -2634,8 +2643,10 @@ public int tenantInfoCountByTenantId(String tenantId) { if (Objects.isNull(tenantId)) { throw new IllegalArgumentException("tenantId can not be null"); } + TenantInfoMapper tenantInfoMapper = (TenantInfoMapper) mapperManager.findMapper(dataSource, TableConstant.TENANT_INFO).get(); + String sql = tenantInfoMapper.getCountByTenantId(); Integer result = databaseOperate - .queryOne(SQL_TENANT_INFO_COUNT_BY_TENANT_ID, new String[] {tenantId}, Integer.class); + .queryOne(sql, new String[] {tenantId}, Integer.class); if (result == null) { return 0; } 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 076003564fc..6da0bb61306 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 @@ -52,6 +52,7 @@ import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoTagMapper; import com.alibaba.nacos.plugin.datasource.mapper.ConfigTagsRelationMapper; import com.alibaba.nacos.plugin.datasource.mapper.HistoryConfigInfoMapper; +import com.alibaba.nacos.plugin.datasource.mapper.TenantInfoMapper; import com.alibaba.nacos.plugin.encryption.handler.EncryptionHandler; import com.alibaba.nacos.sys.env.EnvUtil; import org.apache.commons.collections.CollectionUtils; @@ -115,14 +116,6 @@ public class ExternalStoragePersistServiceImpl implements PersistService { private DataSourceService dataSourceService; - private static final String SQL_FIND_ALL_CONFIG_INFO = "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"; - - private static final String SQL_TENANT_INFO_COUNT_BY_TENANT_ID = "SELECT count(*) FROM tenant_info WHERE tenant_id = ?"; - - private static final String SQL_FIND_CONFIG_INFO_BY_IDS = "SELECT ID,data_id,group_id,tenant_id,app_name,content,md5 FROM config_info WHERE "; - - private static final String SQL_DELETE_CONFIG_INFO_BY_IDS = "DELETE FROM config_info WHERE "; - private static final String PATTERN_STR = "*"; private static final int QUERY_LIMIT_SIZE = 50; @@ -2399,8 +2392,8 @@ public ConfigHistoryInfo detailPreviousConfigHistory(Long id) { public void insertTenantInfoAtomic(String kp, String tenantId, String tenantName, String tenantDesc, String createResoure, final long time) { try { - jt.update( - "INSERT INTO tenant_info(kp,tenant_id,tenant_name,tenant_desc,create_source,gmt_create,gmt_modified) VALUES(?,?,?,?,?,?,?)", + TenantInfoMapper tenantInfoMapper = (TenantInfoMapper) mapperManager.findMapper(dataSource, TableConstant.TENANT_INFO).get(); + jt.update(tenantInfoMapper.insert(Arrays.asList("kp", "tenant_id", "tenant_name", "tenant_desc", "create_source", "gmt_create", "gmt_modified")), kp, tenantId, tenantName, tenantDesc, createResoure, time, time); } catch (DataAccessException e) { LogUtil.FATAL_LOG.error("[db-error] " + e.toString(), e); @@ -2411,8 +2404,8 @@ public void insertTenantInfoAtomic(String kp, String tenantId, String tenantName @Override public void updateTenantNameAtomic(String kp, String tenantId, String tenantName, String tenantDesc) { try { - jt.update( - "UPDATE tenant_info SET tenant_name = ?, tenant_desc = ?, gmt_modified= ? WHERE kp=? AND tenant_id=?", + TenantInfoMapper tenantInfoMapper = (TenantInfoMapper) mapperManager.findMapper(dataSource, TableConstant.TENANT_INFO).get(); + jt.update(tenantInfoMapper.update(Arrays.asList("tenant_name", "tenant_desc", "gmt_modified"), Arrays.asList("kp", "tenant_id")), tenantName, tenantDesc, System.currentTimeMillis(), kp, tenantId); } catch (DataAccessException e) { LogUtil.FATAL_LOG.error("[db-error] " + e.toString(), e); @@ -2422,7 +2415,9 @@ public void updateTenantNameAtomic(String kp, String tenantId, String tenantName @Override public List findTenantByKp(String kp) { - String sql = "SELECT tenant_id,tenant_name,tenant_desc FROM tenant_info WHERE kp=?"; + TenantInfoMapper tenantInfoMapper = (TenantInfoMapper) mapperManager.findMapper(dataSource, TableConstant.TENANT_INFO).get(); + String sql = tenantInfoMapper.select(Arrays.asList("tenant_id", "tenant_name", "tenant_desc"), + Collections.singletonList("kp")); try { return this.jt.query(sql, new Object[] {kp}, TENANT_INFO_ROW_MAPPER); } catch (CannotGetJdbcConnectionException e) { @@ -2438,7 +2433,9 @@ public List findTenantByKp(String kp) { @Override public TenantInfo findTenantByKp(String kp, String tenantId) { - String sql = "SELECT tenant_id,tenant_name,tenant_desc FROM tenant_info WHERE kp=? AND tenant_id=?"; + TenantInfoMapper tenantInfoMapper = (TenantInfoMapper) mapperManager.findMapper(dataSource, TableConstant.TENANT_INFO).get(); + String sql = tenantInfoMapper.select(Arrays.asList("tenant_id", "tenant_name", "tenant_desc"), + Arrays.asList("kp", "tenant_id")); try { return jt.queryForObject(sql, new Object[] {kp, tenantId}, TENANT_INFO_ROW_MAPPER); } catch (CannotGetJdbcConnectionException e) { @@ -2455,7 +2452,8 @@ public TenantInfo findTenantByKp(String kp, String tenantId) { @Override public void removeTenantInfoAtomic(final String kp, final String tenantId) { try { - jt.update("DELETE FROM tenant_info WHERE kp=? AND tenant_id=?", kp, tenantId); + TenantInfoMapper tenantInfoMapper = (TenantInfoMapper) mapperManager.findMapper(dataSource, TableConstant.TENANT_INFO).get(); + jt.update(tenantInfoMapper.delete(Arrays.asList("kp", "tenant_id")), kp, tenantId); } catch (CannotGetJdbcConnectionException e) { LogUtil.FATAL_LOG.error("[db-error] " + e.toString(), e); throw e; @@ -2735,8 +2733,10 @@ public int tenantInfoCountByTenantId(String tenantId) { if (Objects.isNull(tenantId)) { throw new IllegalArgumentException("tenantId can not be null"); } + TenantInfoMapper tenantInfoMapper = (TenantInfoMapper) mapperManager.findMapper(dataSource, TableConstant.TENANT_INFO).get(); + String sql = tenantInfoMapper.getCountByTenantId(); Integer result = this.jt - .queryForObject(SQL_TENANT_INFO_COUNT_BY_TENANT_ID, new String[] {tenantId}, Integer.class); + .queryForObject(sql, new String[] {tenantId}, Integer.class); if (result == null) { return 0; } 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 new file mode 100644 index 00000000000..c6c02f367e1 --- /dev/null +++ b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/impl/derby/TenantInfoMapperByDerby.java @@ -0,0 +1,46 @@ +/* + * 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.alibaba.nacos.plugin.datasource.impl.derby; + +import com.alibaba.nacos.plugin.datasource.constants.DataSourceConstant; +import com.alibaba.nacos.plugin.datasource.constants.TableConstant; +import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper; +import com.alibaba.nacos.plugin.datasource.mapper.TenantInfoMapper; + +/** + * The derby implementation of TenantInfoMapper. + * + * @author hyx + **/ + +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; + } + + @Override + public String getDataSource() { + return DataSourceConstant.DERBY; + } +} 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 new file mode 100644 index 00000000000..697c095ad48 --- /dev/null +++ b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/impl/mysql/TenantInfoMapperByMySql.java @@ -0,0 +1,46 @@ +/* + * 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.alibaba.nacos.plugin.datasource.impl.mysql; + +import com.alibaba.nacos.plugin.datasource.constants.DataSourceConstant; +import com.alibaba.nacos.plugin.datasource.constants.TableConstant; +import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper; +import com.alibaba.nacos.plugin.datasource.mapper.TenantInfoMapper; + +/** + * The mysql implementation of TenantInfoMapper. + * + * @author hyx + **/ + +public class TenantInfoMapperByMySql extends AbstractMapper implements TenantInfoMapper { + + @Override + public String getTableName() { + return TableConstant.TENANT_INFO; + } + + @Override + 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/TenantInfoMapper.java b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/TenantInfoMapper.java new file mode 100644 index 00000000000..2176140391f --- /dev/null +++ b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/TenantInfoMapper.java @@ -0,0 +1,35 @@ +/* + * 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.alibaba.nacos.plugin.datasource.mapper; + +/** + * The tenant info mapper. + * + * @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(); +} diff --git a/plugin/datasource/src/main/resources/META-INF/services/com.alibaba.nacos.plugin.datasource.mapper.Mapper b/plugin/datasource/src/main/resources/META-INF/services/com.alibaba.nacos.plugin.datasource.mapper.Mapper index 8db94fc0349..f8d759444c1 100644 --- a/plugin/datasource/src/main/resources/META-INF/services/com.alibaba.nacos.plugin.datasource.mapper.Mapper +++ b/plugin/datasource/src/main/resources/META-INF/services/com.alibaba.nacos.plugin.datasource.mapper.Mapper @@ -20,10 +20,12 @@ com.alibaba.nacos.plugin.datasource.impl.mysql.ConfigInfoMapperByMySql com.alibaba.nacos.plugin.datasource.impl.mysql.ConfigInfoTagMapperByMySql com.alibaba.nacos.plugin.datasource.impl.mysql.ConfigTagsRelationMapperByMySql com.alibaba.nacos.plugin.datasource.impl.mysql.HistoryConfigInfoMapperByMySql +com.alibaba.nacos.plugin.datasource.impl.mysql.TenantInfoMapperByMySql com.alibaba.nacos.plugin.datasource.impl.derby.ConfigInfoAggrMapperByDerby com.alibaba.nacos.plugin.datasource.impl.derby.ConfigInfoBetaMapperByDerby com.alibaba.nacos.plugin.datasource.impl.derby.ConfigInfoMapperByDerby com.alibaba.nacos.plugin.datasource.impl.derby.ConfigInfoTagMapperByDerby com.alibaba.nacos.plugin.datasource.impl.derby.ConfigInfoTagsRelationMapperByDerby -com.alibaba.nacos.plugin.datasource.impl.derby.HistoryConfigInfoMapperByDerby \ No newline at end of file +com.alibaba.nacos.plugin.datasource.impl.derby.HistoryConfigInfoMapperByDerby +com.alibaba.nacos.plugin.datasource.impl.derby.TenantInfoMapperByDerby \ No newline at end of file