[ISSUE #9449]Add Tenant Information mapper. (#9454)

This commit is contained in:
The-Gamer-01 2022-11-03 13:58:21 +08:00 committed by GitHub
parent bd2b026bb4
commit 0aeb2bd2cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 164 additions and 24 deletions

View File

@ -58,6 +58,7 @@ import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoMapper;
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 class EmbeddedStoragePersistServiceImpl implements PersistService {
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 class EmbeddedStoragePersistServiceImpl implements PersistService {
@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 class EmbeddedStoragePersistServiceImpl implements PersistService {
@Override
public List<TenantInfo> 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 class EmbeddedStoragePersistServiceImpl implements PersistService {
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;
}

View File

@ -52,6 +52,7 @@ import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoMapper;
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 class ExternalStoragePersistServiceImpl implements PersistService {
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 class ExternalStoragePersistServiceImpl implements PersistService {
@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 class ExternalStoragePersistServiceImpl implements PersistService {
@Override
public List<TenantInfo> 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 class ExternalStoragePersistServiceImpl implements PersistService {
@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 class ExternalStoragePersistServiceImpl implements PersistService {
@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 class ExternalStoragePersistServiceImpl implements PersistService {
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;
}

View File

@ -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;
}
}

View File

@ -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 = ?";
}
}

View File

@ -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();
}

View File

@ -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
com.alibaba.nacos.plugin.datasource.impl.derby.HistoryConfigInfoMapperByDerby
com.alibaba.nacos.plugin.datasource.impl.derby.TenantInfoMapperByDerby