Skip to content

Commit

Permalink
[ISSUE #9449]Add Tenant Information mapper. (#9454)
Browse files Browse the repository at this point in the history
  • Loading branch information
The-Gamer-01 authored Nov 3, 2022
1 parent bd2b026 commit 0aeb2bd
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -2337,21 +2340,27 @@ public void updateTenantNameAtomic(String kp, String tenantId, String tenantName

@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 {
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -2422,7 +2415,9 @@ public void updateTenantNameAtomic(String kp, String tenantId, String tenantName

@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) {
Expand All @@ -2438,7 +2433,9 @@ public List<TenantInfo> 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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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 = ?";
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0aeb2bd

Please sign in to comment.