Skip to content

Commit

Permalink
[ISSUE #12342]: Improve the retrieval of ConfigInfoState to facilitat…
Browse files Browse the repository at this point in the history
…e the extension and implementation of databases like Oracle. (#12343)

* Improve the retrieval of ConfigInfoState to facilitate the extension and implementation of databases like Oracle.

* Add unit tests for the SQL construction part of the `ExternalConfigInfoPersistServiceImpl#findConfigInfoState` method.

* Enhance the construction of the delete statement in AbstractMapper by using a unified appendWhereClause method to construct the WHERE clause. Modify appendWhereClause to be protected, allowing for customization based on different database types, such as adjustments according to column names.
  • Loading branch information
gongycn authored Dec 5, 2024
1 parent 4961191 commit 28548c1
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1030,8 +1030,11 @@ public ConfigAllInfo findConfigAllInfo(final String dataId, final String group,
public ConfigInfoStateWrapper findConfigInfoState(final String dataId, final String group, final String tenant) {
String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant;
try {
return this.jt.queryForObject(
"SELECT id,data_id,group_id,tenant_id,gmt_modified FROM config_info WHERE data_id=? AND group_id=? AND tenant_id=?",
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
return this.jt.queryForObject(configInfoMapper.select(
Arrays.asList("id", "data_id", "group_id", "tenant_id", "gmt_modified"),
Arrays.asList("data_id", "group_id", "tenant_id")),
new Object[] {dataId, group, tenantTmp}, CONFIG_INFO_STATE_WRAPPER_ROW_MAPPER);
} catch (EmptyResultDataAccessException e) { // Indicates that the data does not exist, returns null.
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.alibaba.nacos.persistence.datasource.DataSourceService;
import com.alibaba.nacos.persistence.datasource.DynamicDataSource;
import com.alibaba.nacos.persistence.model.Page;
import com.alibaba.nacos.plugin.datasource.MapperManager;
import com.alibaba.nacos.plugin.datasource.constants.TableConstant;
import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoMapper;
import com.alibaba.nacos.sys.env.EnvUtil;
Expand Down Expand Up @@ -1114,7 +1115,7 @@ void testFindConfigInfoState() {
assertTrue(e.getMessage().endsWith("mock exp"));
}
}

@Test
void testFindAllConfigInfo4Export() {

Expand Down Expand Up @@ -1250,5 +1251,16 @@ void testFindAllConfigInfoFragment() {
}

}

@Test
void testBuildFindConfigInfoStateSql() {
MapperManager mapperManager = MapperManager.instance(false);
ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(),
TableConstant.CONFIG_INFO);
String select = configInfoMapper.select(
Arrays.asList("id", "data_id", "group_id", "tenant_id", "gmt_modified"),
Arrays.asList("data_id", "group_id", "tenant_id"));
assertEquals("SELECT id,data_id,group_id,tenant_id,gmt_modified FROM config_info WHERE data_id = ? AND group_id = ? AND tenant_id = ?", select);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,8 @@ public String update(List<String> columns, List<String> where) {
public String delete(List<String> 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++) {
sql.append(params.get(i)).append(" ").append("=").append(" ? ");
if (i != params.size() - 1) {
sql.append("AND ");
}
}
sql.append(method).append("FROM ").append(getTableName()).append(" ");
appendWhereClause(params, sql);

return sql.toString();
}
Expand Down Expand Up @@ -155,7 +150,7 @@ public String[] getPrimaryKeyGeneratedKeys() {
return new String[]{"id"};
}

private void appendWhereClause(List<String> where, StringBuilder sql) {
protected void appendWhereClause(List<String> where, StringBuilder sql) {
sql.append("WHERE ");
for (int i = 0; i < where.size(); i++) {
sql.append(where.get(i)).append(" = ").append("?");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void testUpdate() {
@Test
void testDelete() {
String sql = abstractMapper.delete(Arrays.asList("id"));
assertEquals("DELETE FROM tenant_info WHERE id = ? ", sql);
assertEquals("DELETE FROM tenant_info WHERE id = ?", sql);
}

@Test
Expand Down

0 comments on commit 28548c1

Please sign in to comment.