diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/capacity/GroupCapacityPersistService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/capacity/GroupCapacityPersistService.java index 9d29838e7fe..3e7073a84a8 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/capacity/GroupCapacityPersistService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/capacity/GroupCapacityPersistService.java @@ -19,15 +19,17 @@ import com.alibaba.nacos.common.utils.CollectionUtils; import com.alibaba.nacos.config.server.model.capacity.Capacity; import com.alibaba.nacos.config.server.model.capacity.GroupCapacity; +import com.alibaba.nacos.config.server.utils.TimeUtils; import com.alibaba.nacos.persistence.datasource.DataSourceService; import com.alibaba.nacos.persistence.datasource.DynamicDataSource; -import com.alibaba.nacos.config.server.utils.TimeUtils; import com.alibaba.nacos.plugin.datasource.MapperManager; import com.alibaba.nacos.plugin.datasource.constants.CommonConstant; import com.alibaba.nacos.plugin.datasource.constants.FieldConstant; import com.alibaba.nacos.plugin.datasource.constants.TableConstant; +import com.alibaba.nacos.plugin.datasource.enums.TrustedSqlFunctionEnum; import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoMapper; import com.alibaba.nacos.plugin.datasource.mapper.GroupCapacityMapper; +import com.alibaba.nacos.plugin.datasource.model.ColumnFunctionPair; import com.alibaba.nacos.plugin.datasource.model.MapperContext; import com.alibaba.nacos.plugin.datasource.model.MapperResult; import com.alibaba.nacos.sys.env.EnvUtil; @@ -257,33 +259,33 @@ public boolean decrementUsage(GroupCapacity groupCapacity) { public boolean updateGroupCapacity(String group, Integer quota, Integer maxSize, Integer maxAggrCount, Integer maxAggrSize) { List argList = CollectionUtils.list(); - List columnList = CollectionUtils.list(); + List columnAndFuncctionList = CollectionUtils.list(); if (quota != null) { - columnList.add("quota"); + columnAndFuncctionList.add(ColumnFunctionPair.withColumn("quota")); argList.add(quota); } if (maxSize != null) { - columnList.add("max_size"); + columnAndFuncctionList.add(ColumnFunctionPair.withColumn("max_size")); argList.add(maxSize); } if (maxAggrCount != null) { - columnList.add("max_aggr_count"); + columnAndFuncctionList.add(ColumnFunctionPair.withColumn("max_aggr_count")); argList.add(maxAggrCount); } if (maxAggrSize != null) { - columnList.add("max_aggr_size"); + columnAndFuncctionList.add(ColumnFunctionPair.withColumn("max_aggr_size")); argList.add(maxAggrSize); } - columnList.add("gmt_modified"); + columnAndFuncctionList.add(ColumnFunctionPair.withColumnAndFunction("gmt_modified", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP)); argList.add(TimeUtils.getCurrentTime()); List whereList = CollectionUtils.list(); whereList.add("group_id"); argList.add(group); - + GroupCapacityMapper groupCapacityMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.GROUP_CAPACITY); - String sql = groupCapacityMapper.update(columnList, whereList); + String sql = groupCapacityMapper.update(columnAndFuncctionList, whereList); try { return jdbcTemplate.update(sql, argList.toArray()) == 1; } catch (CannotGetJdbcConnectionException e) { diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/capacity/TenantCapacityPersistService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/capacity/TenantCapacityPersistService.java index 54ff0f13756..4cdcc4758a7 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/capacity/TenantCapacityPersistService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/capacity/TenantCapacityPersistService.java @@ -18,14 +18,16 @@ import com.alibaba.nacos.common.utils.CollectionUtils; import com.alibaba.nacos.config.server.model.capacity.TenantCapacity; +import com.alibaba.nacos.config.server.utils.TimeUtils; import com.alibaba.nacos.persistence.datasource.DataSourceService; import com.alibaba.nacos.persistence.datasource.DynamicDataSource; -import com.alibaba.nacos.config.server.utils.TimeUtils; import com.alibaba.nacos.plugin.datasource.MapperManager; import com.alibaba.nacos.plugin.datasource.constants.CommonConstant; import com.alibaba.nacos.plugin.datasource.constants.FieldConstant; import com.alibaba.nacos.plugin.datasource.constants.TableConstant; +import com.alibaba.nacos.plugin.datasource.enums.TrustedSqlFunctionEnum; import com.alibaba.nacos.plugin.datasource.mapper.TenantCapacityMapper; +import com.alibaba.nacos.plugin.datasource.model.ColumnFunctionPair; import com.alibaba.nacos.plugin.datasource.model.MapperContext; import com.alibaba.nacos.plugin.datasource.model.MapperResult; import com.alibaba.nacos.sys.env.EnvUtil; @@ -238,35 +240,35 @@ public boolean decrementUsage(TenantCapacity tenantCapacity) { public boolean updateTenantCapacity(String tenant, Integer quota, Integer maxSize, Integer maxAggrCount, Integer maxAggrSize) { List argList = CollectionUtils.list(); - - List columns = new ArrayList<>(); + + List columnAndFunctionPairs = new ArrayList<>(); if (quota != null) { - columns.add("quota"); + columnAndFunctionPairs.add(ColumnFunctionPair.withColumn("quota")); argList.add(quota); } if (maxSize != null) { - columns.add("max_size"); + columnAndFunctionPairs.add(ColumnFunctionPair.withColumn("max_size")); argList.add(maxSize); } if (maxAggrCount != null) { - columns.add("max_aggr_count"); + columnAndFunctionPairs.add(ColumnFunctionPair.withColumn("max_aggr_count")); argList.add(maxAggrCount); } if (maxAggrSize != null) { - columns.add("max_aggr_size"); + columnAndFunctionPairs.add(ColumnFunctionPair.withColumn("max_aggr_size")); argList.add(maxAggrSize); } - columns.add("gmt_modified"); + columnAndFunctionPairs.add(ColumnFunctionPair.withColumnAndFunction("gmt_modified", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP)); argList.add(TimeUtils.getCurrentTime()); List where = new ArrayList<>(); where.add("tenant_id"); argList.add(tenant); - + TenantCapacityMapper tenantCapacityMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.TENANT_CAPACITY); - String sql = tenantCapacityMapper.update(columns, where); + String sql = tenantCapacityMapper.update(columnAndFunctionPairs, where); try { return jdbcTemplate.update(sql, argList.toArray()) == 1; } catch (CannotGetJdbcConnectionException e) { diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedConfigInfoAggrPersistServiceImpl.java b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedConfigInfoAggrPersistServiceImpl.java index dab8c013af8..53cd96d6dc9 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedConfigInfoAggrPersistServiceImpl.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedConfigInfoAggrPersistServiceImpl.java @@ -35,14 +35,15 @@ import com.alibaba.nacos.plugin.datasource.constants.CommonConstant; import com.alibaba.nacos.plugin.datasource.constants.FieldConstant; import com.alibaba.nacos.plugin.datasource.constants.TableConstant; +import com.alibaba.nacos.plugin.datasource.enums.TrustedSqlFunctionEnum; import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoAggrMapper; +import com.alibaba.nacos.plugin.datasource.model.ColumnFunctionPair; import com.alibaba.nacos.plugin.datasource.model.MapperContext; import com.alibaba.nacos.plugin.datasource.model.MapperResult; import com.alibaba.nacos.sys.env.EnvUtil; import org.springframework.context.annotation.Conditional; import org.springframework.stereotype.Service; -import java.sql.Timestamp; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -93,28 +94,37 @@ public boolean addAggrConfigInfo(final String dataId, final String group, String String appNameTmp = StringUtils.isBlank(appName) ? StringUtils.EMPTY : appName; String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant; String contentTmp = StringUtils.isBlank(content) ? StringUtils.EMPTY : content; - final Timestamp now = new Timestamp(System.currentTimeMillis()); - + ConfigInfoAggrMapper configInfoAggrMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO_AGGR); final String select = configInfoAggrMapper.select(Collections.singletonList("content"), Arrays.asList("data_id", "group_id", "tenant_id", "datum_id")); final String insert = configInfoAggrMapper.insert( - Arrays.asList("data_id", "group_id", "tenant_id", "datum_id", "app_name", "content", "gmt_modified")); - final String update = configInfoAggrMapper.update(Arrays.asList("content", "gmt_modified"), + Arrays.asList( + ColumnFunctionPair.withColumn("data_id"), + ColumnFunctionPair.withColumn("group_id"), + ColumnFunctionPair.withColumn("tenant_id"), + ColumnFunctionPair.withColumn("datum_id"), + ColumnFunctionPair.withColumn("app_name"), + ColumnFunctionPair.withColumn("content"), + ColumnFunctionPair.withColumnAndFunction("gmt_modified", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP))); + final String update = configInfoAggrMapper.update( + Arrays.asList( + ColumnFunctionPair.withColumn("content"), + ColumnFunctionPair.withColumnAndFunction("gmt_modified", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP)), Arrays.asList("data_id", "group_id", "tenant_id", "datum_id")); - - String dbContent = databaseOperate.queryOne(select, new Object[] {dataId, group, tenantTmp, datumId}, + + String dbContent = databaseOperate.queryOne(select, new Object[]{dataId, group, tenantTmp, datumId}, String.class); - + if (Objects.isNull(dbContent)) { - final Object[] args = new Object[] {dataId, group, tenantTmp, datumId, appNameTmp, contentTmp, now}; + final Object[] args = new Object[]{dataId, group, tenantTmp, datumId, appNameTmp, contentTmp}; EmbeddedStorageContextHolder.addSqlContext(insert, args); } else if (!dbContent.equals(content)) { - final Object[] args = new Object[] {contentTmp, now, dataId, group, tenantTmp, datumId}; + final Object[] args = new Object[]{contentTmp, dataId, group, tenantTmp, datumId}; EmbeddedStorageContextHolder.addSqlContext(update, args); } - + try { boolean result = databaseOperate.update(EmbeddedStorageContextHolder.getCurrentSqlContext()); if (!result) { diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedConfigInfoBetaPersistServiceImpl.java b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedConfigInfoBetaPersistServiceImpl.java index 584df79257c..28f3d7b33a1 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedConfigInfoBetaPersistServiceImpl.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedConfigInfoBetaPersistServiceImpl.java @@ -40,7 +40,9 @@ import com.alibaba.nacos.plugin.datasource.constants.CommonConstant; import com.alibaba.nacos.plugin.datasource.constants.FieldConstant; import com.alibaba.nacos.plugin.datasource.constants.TableConstant; +import com.alibaba.nacos.plugin.datasource.enums.TrustedSqlFunctionEnum; import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoBetaMapper; +import com.alibaba.nacos.plugin.datasource.model.ColumnFunctionPair; import com.alibaba.nacos.plugin.datasource.model.MapperContext; import com.alibaba.nacos.plugin.datasource.model.MapperResult; import com.alibaba.nacos.sys.env.EnvUtil; @@ -126,19 +128,30 @@ public ConfigOperateResult addConfigInfo4Beta(ConfigInfo configInfo, String beta ConfigInfoBetaMapper configInfoBetaMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO_BETA); Timestamp time = new Timestamp(System.currentTimeMillis()); - + final String sql = configInfoBetaMapper.insert( - Arrays.asList("data_id", "group_id", "tenant_id", "app_name", "content", "md5", "beta_ips", - "src_ip", "src_user", "gmt_create", "gmt_modified", "encrypted_data_key")); - final Object[] args = new Object[] {configInfo.getDataId(), configInfo.getGroup(), tenantTmp, appNameTmp, - configInfo.getContent(), md5, betaIps, srcIp, srcUser, time, time, encryptedDataKey}; - + Arrays.asList( + ColumnFunctionPair.withColumn("data_id"), + ColumnFunctionPair.withColumn("group_id"), + ColumnFunctionPair.withColumn("tenant_id"), + ColumnFunctionPair.withColumn("app_name"), + ColumnFunctionPair.withColumn("content"), + ColumnFunctionPair.withColumn("md5"), + ColumnFunctionPair.withColumn("beta_ips"), + ColumnFunctionPair.withColumn("src_ip"), + ColumnFunctionPair.withColumn("src_user"), + ColumnFunctionPair.withColumnAndFunction("gmt_create", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP), + ColumnFunctionPair.withColumnAndFunction("gmt_modified", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP), + ColumnFunctionPair.withColumn("encrypted_data_key"))); + final Object[] args = new Object[]{configInfo.getDataId(), configInfo.getGroup(), tenantTmp, appNameTmp, + configInfo.getContent(), md5, betaIps, srcIp, srcUser, encryptedDataKey}; + EmbeddedStorageContextUtils.onModifyConfigBetaInfo(configInfo, betaIps, srcIp, time); EmbeddedStorageContextHolder.addSqlContext(sql, args); - + databaseOperate.blockUpdate(); return getBetaOperateResult(configInfo.getDataId(), configInfo.getGroup(), tenantTmp); - + } finally { EmbeddedStorageContextHolder.cleanAllContext(); } @@ -197,26 +210,29 @@ public ConfigOperateResult updateConfigInfo4Beta(ConfigInfo configInfo, String b String appNameTmp = StringUtils.defaultEmptyIfBlank(configInfo.getAppName()); String tenantTmp = StringUtils.defaultEmptyIfBlank(configInfo.getTenant()); String encryptedDataKey = StringUtils.defaultEmptyIfBlank(configInfo.getEncryptedDataKey()); - configInfo.setTenant(tenantTmp); try { String md5 = MD5Utils.md5Hex(configInfo.getContent(), Constants.ENCODE); ConfigInfoBetaMapper configInfoBetaMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO_BETA); - Timestamp time = new Timestamp(System.currentTimeMillis()); final String sql = configInfoBetaMapper.update( - Arrays.asList("content", "md5", "beta_ips", "src_ip", "src_user", "gmt_modified", "app_name", - "encrypted_data_key"), Arrays.asList("data_id", "group_id", "tenant_id")); - - final Object[] args = new Object[] {configInfo.getContent(), md5, betaIps, srcIp, srcUser, time, appNameTmp, + Arrays.asList( + ColumnFunctionPair.withColumn("content"), + ColumnFunctionPair.withColumn("md5"), + ColumnFunctionPair.withColumn("beta_ips"), + ColumnFunctionPair.withColumn("src_ip"), + ColumnFunctionPair.withColumn("src_user"), + ColumnFunctionPair.withColumnAndFunction("gmt_modified", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP), + ColumnFunctionPair.withColumn("app_name"), + ColumnFunctionPair.withColumn("encrypted_data_key")), + Arrays.asList("data_id", "group_id", "tenant_id")); + final Object[] args = new Object[]{configInfo.getContent(), md5, betaIps, srcIp, srcUser, appNameTmp, encryptedDataKey, configInfo.getDataId(), configInfo.getGroup(), tenantTmp}; - + Timestamp time = new Timestamp(System.currentTimeMillis()); EmbeddedStorageContextUtils.onModifyConfigBetaInfo(configInfo, betaIps, srcIp, time); EmbeddedStorageContextHolder.addSqlContext(sql, args); - databaseOperate.blockUpdate(); return getBetaOperateResult(configInfo.getDataId(), configInfo.getGroup(), tenantTmp); - } finally { EmbeddedStorageContextHolder.cleanAllContext(); } @@ -234,14 +250,12 @@ public ConfigOperateResult updateConfigInfo4BetaCas(ConfigInfo configInfo, Strin ConfigInfoBetaMapper configInfoBetaMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO_BETA); - Timestamp time = new Timestamp(System.currentTimeMillis()); MapperContext context = new MapperContext(); context.putUpdateParameter(FieldConstant.CONTENT, configInfo.getContent()); context.putUpdateParameter(FieldConstant.MD5, md5); context.putUpdateParameter(FieldConstant.BETA_IPS, betaIps); context.putUpdateParameter(FieldConstant.SRC_IP, srcIp); context.putUpdateParameter(FieldConstant.SRC_USER, srcUser); - context.putUpdateParameter(FieldConstant.GMT_MODIFIED, time); context.putUpdateParameter(FieldConstant.APP_NAME, appNameTmp); context.putWhereParameter(FieldConstant.DATA_ID, configInfo.getDataId()); @@ -253,7 +267,7 @@ public ConfigOperateResult updateConfigInfo4BetaCas(ConfigInfo configInfo, Strin final String sql = mapperResult.getSql(); List paramList = mapperResult.getParamList(); final Object[] args = paramList.toArray(); - + Timestamp time = new Timestamp(System.currentTimeMillis()); EmbeddedStorageContextUtils.onModifyConfigBetaInfo(configInfo, betaIps, srcIp, time); EmbeddedStorageContextHolder.addSqlContext(sql, args); diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedConfigInfoPersistServiceImpl.java b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedConfigInfoPersistServiceImpl.java index ab5b53a58b2..b86592893d0 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedConfigInfoPersistServiceImpl.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedConfigInfoPersistServiceImpl.java @@ -51,8 +51,10 @@ import com.alibaba.nacos.plugin.datasource.constants.ContextConstant; import com.alibaba.nacos.plugin.datasource.constants.FieldConstant; import com.alibaba.nacos.plugin.datasource.constants.TableConstant; +import com.alibaba.nacos.plugin.datasource.enums.TrustedSqlFunctionEnum; import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoMapper; import com.alibaba.nacos.plugin.datasource.mapper.ConfigTagsRelationMapper; +import com.alibaba.nacos.plugin.datasource.model.ColumnFunctionPair; import com.alibaba.nacos.plugin.datasource.model.MapperContext; import com.alibaba.nacos.plugin.datasource.model.MapperResult; import com.alibaba.nacos.plugin.encryption.handler.EncryptionHandler; @@ -264,15 +266,27 @@ public long addConfigInfoAtomic(final long id, final String srcIp, final String configInfo.getEncryptedDataKey() == null ? StringUtils.EMPTY : configInfo.getEncryptedDataKey(); ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO); - Timestamp time = new Timestamp(System.currentTimeMillis()); - final String sql = configInfoMapper.insert( - Arrays.asList("id", "data_id", "group_id", "tenant_id", "app_name", "content", "md5", "src_ip", - "src_user", "gmt_create", "gmt_modified", "c_desc", "c_use", "effect", "type", "c_schema", - "encrypted_data_key")); - final Object[] args = new Object[] {id, configInfo.getDataId(), configInfo.getGroup(), tenantTmp, appNameTmp, - configInfo.getContent(), md5Tmp, srcIp, srcUser, time, time, desc, use, effect, type, schema, - encryptedDataKey}; + Arrays.asList( + ColumnFunctionPair.withColumn("id"), + ColumnFunctionPair.withColumn("data_id"), + ColumnFunctionPair.withColumn("group_id"), + ColumnFunctionPair.withColumn("tenant_id"), + ColumnFunctionPair.withColumn("app_name"), + ColumnFunctionPair.withColumn("content"), + ColumnFunctionPair.withColumn("md5"), + ColumnFunctionPair.withColumn("src_ip"), + ColumnFunctionPair.withColumn("src_user"), + ColumnFunctionPair.withColumnAndFunction("gmt_create", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP), + ColumnFunctionPair.withColumnAndFunction("gmt_modified", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP), + ColumnFunctionPair.withColumn("c_desc"), + ColumnFunctionPair.withColumn("c_use"), + ColumnFunctionPair.withColumn("effect"), + ColumnFunctionPair.withColumn("type"), + ColumnFunctionPair.withColumn("c_schema"), + ColumnFunctionPair.withColumn("encrypted_data_key"))); + final Object[] args = new Object[]{id, configInfo.getDataId(), configInfo.getGroup(), tenantTmp, appNameTmp, + configInfo.getContent(), md5Tmp, srcIp, srcUser, desc, use, effect, type, schema, encryptedDataKey}; EmbeddedStorageContextHolder.addSqlContext(sql, args); return id; } @@ -282,8 +296,14 @@ public void addConfigTagRelationAtomic(long configId, String tagName, String dat ConfigTagsRelationMapper configTagsRelationMapper = mapperManager.findMapper( dataSourceService.getDataSourceType(), TableConstant.CONFIG_TAGS_RELATION); final String sql = configTagsRelationMapper.insert( - Arrays.asList("id", "tag_name", "tag_type", "data_id", "group_id", "tenant_id")); - final Object[] args = new Object[] {configId, tagName, StringUtils.EMPTY, dataId, group, tenant}; + Arrays.asList( + ColumnFunctionPair.withColumn("id"), + ColumnFunctionPair.withColumn("tag_name"), + ColumnFunctionPair.withColumn("tag_type"), + ColumnFunctionPair.withColumn("data_id"), + ColumnFunctionPair.withColumn("group_id"), + ColumnFunctionPair.withColumn("tenant_id"))); + final Object[] args = new Object[]{configId, tagName, StringUtils.EMPTY, dataId, group, tenant}; EmbeddedStorageContextHolder.addSqlContext(sql, args); } @@ -594,13 +614,11 @@ private ConfigOperateResult updateConfigInfoAtomicCas(final ConfigInfo configInf configInfo.getEncryptedDataKey() == null ? StringUtils.EMPTY : configInfo.getEncryptedDataKey(); ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO); - Timestamp time = new Timestamp(System.currentTimeMillis()); MapperContext context = new MapperContext(); context.putUpdateParameter(FieldConstant.CONTENT, configInfo.getContent()); context.putUpdateParameter(FieldConstant.MD5, md5Tmp); context.putUpdateParameter(FieldConstant.SRC_IP, srcIp); context.putUpdateParameter(FieldConstant.SRC_USER, srcUser); - context.putUpdateParameter(FieldConstant.GMT_MODIFIED, time); context.putUpdateParameter(FieldConstant.APP_NAME, appNameTmp); context.putUpdateParameter(FieldConstant.C_DESC, desc); context.putUpdateParameter(FieldConstant.C_USE, use); @@ -632,18 +650,25 @@ public void updateConfigInfoAtomic(final ConfigInfo configInfo, final String src final String schema = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("schema"); final String encryptedDataKey = configInfo.getEncryptedDataKey() == null ? StringUtils.EMPTY : configInfo.getEncryptedDataKey(); - ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO); final String sql = configInfoMapper.update( - Arrays.asList("content", "md5", "src_ip", "src_user", "gmt_modified", "app_name", "c_desc", "c_use", - "effect", "type", "c_schema", "encrypted_data_key"), + Arrays.asList( + ColumnFunctionPair.withColumn("content"), + ColumnFunctionPair.withColumn("md5"), + ColumnFunctionPair.withColumn("src_ip"), + ColumnFunctionPair.withColumn("src_user"), + ColumnFunctionPair.withColumnAndFunction("gmt_modified", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP), + ColumnFunctionPair.withColumn("app_name"), + ColumnFunctionPair.withColumn("c_desc"), + ColumnFunctionPair.withColumn("c_use"), + ColumnFunctionPair.withColumn("effect"), + ColumnFunctionPair.withColumn("type"), + ColumnFunctionPair.withColumn("c_schema"), + ColumnFunctionPair.withColumn("encrypted_data_key")), Arrays.asList("data_id", "group_id", "tenant_id")); - Timestamp time = new Timestamp(System.currentTimeMillis()); - - final Object[] args = new Object[] {configInfo.getContent(), md5Tmp, srcIp, srcUser, time, appNameTmp, desc, + final Object[] args = new Object[]{configInfo.getContent(), md5Tmp, srcIp, srcUser, appNameTmp, desc, use, effect, type, schema, encryptedDataKey, configInfo.getDataId(), configInfo.getGroup(), tenantTmp}; - EmbeddedStorageContextHolder.addSqlContext(sql, args); } diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedConfigInfoTagPersistServiceImpl.java b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedConfigInfoTagPersistServiceImpl.java index a4d9d437fd5..4ac42636ea4 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedConfigInfoTagPersistServiceImpl.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedConfigInfoTagPersistServiceImpl.java @@ -39,7 +39,9 @@ import com.alibaba.nacos.plugin.datasource.constants.CommonConstant; import com.alibaba.nacos.plugin.datasource.constants.FieldConstant; import com.alibaba.nacos.plugin.datasource.constants.TableConstant; +import com.alibaba.nacos.plugin.datasource.enums.TrustedSqlFunctionEnum; import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoTagMapper; +import com.alibaba.nacos.plugin.datasource.model.ColumnFunctionPair; import com.alibaba.nacos.plugin.datasource.model.MapperContext; import com.alibaba.nacos.plugin.datasource.model.MapperResult; import com.alibaba.nacos.sys.env.EnvUtil; @@ -118,27 +120,31 @@ public ConfigOperateResult addConfigInfo4Tag(ConfigInfo configInfo, String tag, String appNameTmp = StringUtils.defaultEmptyIfBlank(configInfo.getAppName()); String tenantTmp = StringUtils.defaultEmptyIfBlank(configInfo.getTenant()); String tagTmp = StringUtils.isBlank(tag) ? StringUtils.EMPTY : tag.trim(); - configInfo.setTenant(tenantTmp); - try { String md5 = MD5Utils.md5Hex(configInfo.getContent(), Constants.ENCODE); ConfigInfoTagMapper configInfoTagMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO_TAG); final String sql = configInfoTagMapper.insert( - Arrays.asList("data_id", "group_id", "tenant_id", "tag_id", "app_name", "content", "md5", "src_ip", - "src_user", "gmt_create", "gmt_modified")); + Arrays.asList( + ColumnFunctionPair.withColumn("data_id"), + ColumnFunctionPair.withColumn("group_id"), + ColumnFunctionPair.withColumn("tenant_id"), + ColumnFunctionPair.withColumn("tag_id"), + ColumnFunctionPair.withColumn("app_name"), + ColumnFunctionPair.withColumn("content"), + ColumnFunctionPair.withColumn("md5"), + ColumnFunctionPair.withColumn("src_ip"), + ColumnFunctionPair.withColumn("src_user"), + ColumnFunctionPair.withColumnAndFunction("gmt_create", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP), + ColumnFunctionPair.withColumnAndFunction("gmt_modified", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP))); + final Object[] args = new Object[]{configInfo.getDataId(), configInfo.getGroup(), tenantTmp, tagTmp, + appNameTmp, configInfo.getContent(), md5, srcIp, srcUser}; Timestamp time = new Timestamp(System.currentTimeMillis()); - - final Object[] args = new Object[] {configInfo.getDataId(), configInfo.getGroup(), tenantTmp, tagTmp, - appNameTmp, configInfo.getContent(), md5, srcIp, srcUser, time, time}; - EmbeddedStorageContextUtils.onModifyConfigTagInfo(configInfo, tagTmp, srcIp, time); EmbeddedStorageContextHolder.addSqlContext(sql, args); - databaseOperate.blockUpdate(); return getTagOperateResult(configInfo.getDataId(), configInfo.getGroup(), tenantTmp, tagTmp); - } finally { EmbeddedStorageContextHolder.cleanAllContext(); } @@ -189,28 +195,27 @@ public ConfigOperateResult updateConfigInfo4Tag(ConfigInfo configInfo, String ta String appNameTmp = StringUtils.defaultEmptyIfBlank(configInfo.getAppName()); String tenantTmp = StringUtils.defaultEmptyIfBlank(configInfo.getTenant()); String tagTmp = StringUtils.isBlank(tag) ? StringUtils.EMPTY : tag.trim(); - configInfo.setTenant(tenantTmp); - try { String md5 = MD5Utils.md5Hex(configInfo.getContent(), Constants.ENCODE); - ConfigInfoTagMapper configInfoTagMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO_TAG); - Timestamp time = new Timestamp(System.currentTimeMillis()); - final String sql = configInfoTagMapper.update( - Arrays.asList("content", "md5", "src_ip", "src_user", "gmt_modified", "app_name"), + Arrays.asList( + ColumnFunctionPair.withColumn("content"), + ColumnFunctionPair.withColumn("md5"), + ColumnFunctionPair.withColumn("src_ip"), + ColumnFunctionPair.withColumn("src_user"), + ColumnFunctionPair.withColumnAndFunction("gmt_modified", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP), + ColumnFunctionPair.withColumn("app_name")), Arrays.asList("data_id", "group_id", "tenant_id", "tag_id")); - final Object[] args = new Object[] {configInfo.getContent(), md5, srcIp, srcUser, time, appNameTmp, + final Object[] args = new Object[]{configInfo.getContent(), md5, srcIp, srcUser, appNameTmp, configInfo.getDataId(), configInfo.getGroup(), tenantTmp, tagTmp}; - + Timestamp time = new Timestamp(System.currentTimeMillis()); EmbeddedStorageContextUtils.onModifyConfigTagInfo(configInfo, tagTmp, srcIp, time); EmbeddedStorageContextHolder.addSqlContext(sql, args); - databaseOperate.blockUpdate(); return getTagOperateResult(configInfo.getDataId(), configInfo.getGroup(), tenantTmp, tagTmp); - } finally { EmbeddedStorageContextHolder.cleanAllContext(); } @@ -229,14 +234,11 @@ public ConfigOperateResult updateConfigInfo4TagCas(ConfigInfo configInfo, String String md5 = MD5Utils.md5Hex(configInfo.getContent(), Constants.ENCODE); ConfigInfoTagMapper configInfoTagMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO_TAG); - Timestamp time = new Timestamp(System.currentTimeMillis()); - MapperContext context = new MapperContext(); context.putUpdateParameter(FieldConstant.CONTENT, configInfo.getContent()); context.putUpdateParameter(FieldConstant.MD5, md5); context.putUpdateParameter(FieldConstant.SRC_IP, srcIp); context.putUpdateParameter(FieldConstant.SRC_USER, srcUser); - context.putUpdateParameter(FieldConstant.GMT_MODIFIED, time); context.putUpdateParameter(FieldConstant.APP_NAME, appNameTmp); context.putWhereParameter(FieldConstant.DATA_ID, configInfo.getDataId()); @@ -246,7 +248,7 @@ public ConfigOperateResult updateConfigInfo4TagCas(ConfigInfo configInfo, String context.putWhereParameter(FieldConstant.MD5, configInfo.getMd5()); final MapperResult mapperResult = configInfoTagMapper.updateConfigInfo4TagCas(context); - + Timestamp time = new Timestamp(System.currentTimeMillis()); EmbeddedStorageContextUtils.onModifyConfigTagInfo(configInfo, tagTmp, srcIp, time); EmbeddedStorageContextHolder.addSqlContext(mapperResult.getSql(), mapperResult.getParamList().toArray()); diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedHistoryConfigInfoPersistServiceImpl.java b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedHistoryConfigInfoPersistServiceImpl.java index 4bfdf8ec65b..565adbd5576 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedHistoryConfigInfoPersistServiceImpl.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedHistoryConfigInfoPersistServiceImpl.java @@ -38,6 +38,7 @@ import com.alibaba.nacos.plugin.datasource.constants.FieldConstant; import com.alibaba.nacos.plugin.datasource.constants.TableConstant; import com.alibaba.nacos.plugin.datasource.mapper.HistoryConfigInfoMapper; +import com.alibaba.nacos.plugin.datasource.model.ColumnFunctionPair; import com.alibaba.nacos.plugin.datasource.model.MapperContext; import com.alibaba.nacos.plugin.datasource.model.MapperResult; import com.alibaba.nacos.sys.env.EnvUtil; @@ -95,15 +96,26 @@ public void insertConfigHistoryAtomic(long configHistoryId, ConfigInfo configInf String tenantTmp = StringUtils.defaultEmptyIfBlank(configInfo.getTenant()); final String md5Tmp = MD5Utils.md5Hex(configInfo.getContent(), Constants.ENCODE); String encryptedDataKey = StringUtils.defaultEmptyIfBlank(configInfo.getEncryptedDataKey()); - + HistoryConfigInfoMapper historyConfigInfoMapper = mapperManager.findMapper( dataSourceService.getDataSourceType(), TableConstant.HIS_CONFIG_INFO); final String sql = historyConfigInfoMapper.insert( - Arrays.asList("id", "data_id", "group_id", "tenant_id", "app_name", "content", "md5", "src_ip", - "src_user", "gmt_modified", "op_type", "encrypted_data_key")); - final Object[] args = new Object[] {configHistoryId, configInfo.getDataId(), configInfo.getGroup(), tenantTmp, + Arrays.asList( + ColumnFunctionPair.withColumn("id"), + ColumnFunctionPair.withColumn("data_id"), + ColumnFunctionPair.withColumn("group_id"), + ColumnFunctionPair.withColumn("tenant_id"), + ColumnFunctionPair.withColumn("app_name"), + ColumnFunctionPair.withColumn("content"), + ColumnFunctionPair.withColumn("md5"), + ColumnFunctionPair.withColumn("src_ip"), + ColumnFunctionPair.withColumn("src_user"), + ColumnFunctionPair.withColumn("gmt_modified"), + ColumnFunctionPair.withColumn("op_type"), + ColumnFunctionPair.withColumn("encrypted_data_key"))); + final Object[] args = new Object[]{configHistoryId, configInfo.getDataId(), configInfo.getGroup(), tenantTmp, appNameTmp, configInfo.getContent(), md5Tmp, srcIp, srcUser, time, ops, encryptedDataKey}; - + EmbeddedStorageContextHolder.addSqlContext(sql, args); } diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoAggrPersistServiceImpl.java b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoAggrPersistServiceImpl.java index 03c7d093e1c..03e8086f90b 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoAggrPersistServiceImpl.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoAggrPersistServiceImpl.java @@ -31,7 +31,9 @@ import com.alibaba.nacos.plugin.datasource.constants.CommonConstant; import com.alibaba.nacos.plugin.datasource.constants.FieldConstant; import com.alibaba.nacos.plugin.datasource.constants.TableConstant; +import com.alibaba.nacos.plugin.datasource.enums.TrustedSqlFunctionEnum; import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoAggrMapper; +import com.alibaba.nacos.plugin.datasource.model.ColumnFunctionPair; import com.alibaba.nacos.plugin.datasource.model.MapperContext; import com.alibaba.nacos.plugin.datasource.model.MapperResult; import com.alibaba.nacos.sys.env.EnvUtil; @@ -45,7 +47,6 @@ import org.springframework.transaction.TransactionSystemException; import org.springframework.transaction.support.TransactionTemplate; -import java.sql.Timestamp; import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -91,28 +92,34 @@ public boolean addAggrConfigInfo(final String dataId, final String group, String String appName, final String content) { String appNameTmp = StringUtils.isBlank(appName) ? StringUtils.EMPTY : appName; String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant; - final Timestamp now = new Timestamp(System.currentTimeMillis()); ConfigInfoAggrMapper configInfoAggrMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO_AGGR); String select = configInfoAggrMapper.select(Collections.singletonList("content"), Arrays.asList("data_id", "group_id", "tenant_id", "datum_id")); String insert = configInfoAggrMapper.insert( - Arrays.asList("data_id", "group_id", "tenant_id", "datum_id", "app_name", "content", "gmt_modified")); - String update = configInfoAggrMapper.update(Arrays.asList("content", "gmt_modified"), + Arrays.asList( + ColumnFunctionPair.withColumn("data_id"), + ColumnFunctionPair.withColumn("group_id"), + ColumnFunctionPair.withColumn("tenant_id"), + ColumnFunctionPair.withColumn("datum_id"), + ColumnFunctionPair.withColumn("app_name"), + ColumnFunctionPair.withColumn("content"), + ColumnFunctionPair.withColumnAndFunction("gmt_modified", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP))); + String update = configInfoAggrMapper.update(Arrays.asList( + ColumnFunctionPair.withColumn("content"), + ColumnFunctionPair.withColumnAndFunction("gmt_modified", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP)), Arrays.asList("data_id", "group_id", "tenant_id", "datum_id")); - try { try { - String dbContent = jt.queryForObject(select, new Object[] {dataId, group, tenantTmp, datumId}, + String dbContent = jt.queryForObject(select, new Object[]{dataId, group, tenantTmp, datumId}, String.class); - if (dbContent != null && dbContent.equals(content)) { return true; } else { - return jt.update(update, content, now, dataId, group, tenantTmp, datumId) > 0; + return jt.update(update, content, dataId, group, tenantTmp, datumId) > 0; } } catch (EmptyResultDataAccessException ex) { // no data, insert - return jt.update(insert, dataId, group, tenantTmp, datumId, appNameTmp, content, now) > 0; + return jt.update(insert, dataId, group, tenantTmp, datumId, appNameTmp, content) > 0; } } catch (DataAccessException e) { LogUtil.FATAL_LOG.error("[db-error] " + e, e); diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoBetaPersistServiceImpl.java b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoBetaPersistServiceImpl.java index d0b62bd498d..602a2c09b02 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoBetaPersistServiceImpl.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoBetaPersistServiceImpl.java @@ -35,7 +35,9 @@ import com.alibaba.nacos.plugin.datasource.constants.CommonConstant; import com.alibaba.nacos.plugin.datasource.constants.FieldConstant; import com.alibaba.nacos.plugin.datasource.constants.TableConstant; +import com.alibaba.nacos.plugin.datasource.enums.TrustedSqlFunctionEnum; import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoBetaMapper; +import com.alibaba.nacos.plugin.datasource.model.ColumnFunctionPair; import com.alibaba.nacos.plugin.datasource.model.MapperContext; import com.alibaba.nacos.plugin.datasource.model.MapperResult; import com.alibaba.nacos.sys.env.EnvUtil; @@ -46,7 +48,6 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.support.TransactionTemplate; -import java.sql.Timestamp; import java.util.Arrays; import java.util.List; @@ -94,15 +95,23 @@ public ConfigOperateResult addConfigInfo4Beta(ConfigInfo configInfo, String beta try { ConfigInfoBetaMapper configInfoBetaMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO_BETA); - Timestamp time = new Timestamp(System.currentTimeMillis()); - jt.update(configInfoBetaMapper.insert( - Arrays.asList("data_id", "group_id", "tenant_id", "app_name", "content", "md5", "beta_ips", - "src_ip", "src_user", "gmt_create", "gmt_modified", "encrypted_data_key")), + Arrays.asList( + ColumnFunctionPair.withColumn("data_id"), + ColumnFunctionPair.withColumn("group_id"), + ColumnFunctionPair.withColumn("tenant_id"), + ColumnFunctionPair.withColumn("app_name"), + ColumnFunctionPair.withColumn("content"), + ColumnFunctionPair.withColumn("md5"), + ColumnFunctionPair.withColumn("beta_ips"), + ColumnFunctionPair.withColumn("src_ip"), + ColumnFunctionPair.withColumn("src_user"), + ColumnFunctionPair.withColumnAndFunction("gmt_create", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP), + ColumnFunctionPair.withColumnAndFunction("gmt_modified", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP), + ColumnFunctionPair.withColumn("encrypted_data_key"))), configInfo.getDataId(), configInfo.getGroup(), tenantTmp, appNameTmp, configInfo.getContent(), md5, - betaIps, srcIp, srcUser, time, time, encryptedDataKey); + betaIps, srcIp, srcUser, encryptedDataKey); return getBetaOperateResult(configInfo.getDataId(), configInfo.getGroup(), tenantTmp); - } catch (CannotGetJdbcConnectionException e) { LogUtil.FATAL_LOG.error("[db-error] " + e, e); throw e; @@ -117,7 +126,6 @@ public ConfigOperateResult insertOrUpdateBeta(final ConfigInfo configInfo, final configInfo.getGroup(), configInfo.getTenant()); if (configInfo4BetaState == null) { return addConfigInfo4Beta(configInfo, betaIps, srcIp, srcUser); - } else { return updateConfigInfo4Beta(configInfo, betaIps, srcIp, srcUser); } @@ -165,15 +173,20 @@ public ConfigOperateResult updateConfigInfo4Beta(ConfigInfo configInfo, String b try { ConfigInfoBetaMapper configInfoBetaMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO_BETA); - Timestamp time = new Timestamp(System.currentTimeMillis()); - jt.update(configInfoBetaMapper.update( - Arrays.asList("content", "md5", "beta_ips", "src_ip", "src_user", "gmt_modified", "app_name", - "encrypted_data_key"), Arrays.asList("data_id", "group_id", "tenant_id")), - configInfo.getContent(), md5, betaIps, srcIp, srcUser, time, appNameTmp, encryptedDataKey, + Arrays.asList( + ColumnFunctionPair.withColumn("content"), + ColumnFunctionPair.withColumn("md5"), + ColumnFunctionPair.withColumn("beta_ips"), + ColumnFunctionPair.withColumn("src_ip"), + ColumnFunctionPair.withColumn("src_user"), + ColumnFunctionPair.withColumnAndFunction("gmt_modified", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP), + ColumnFunctionPair.withColumn("app_name"), + ColumnFunctionPair.withColumn("encrypted_data_key")), + Arrays.asList("data_id", "group_id", "tenant_id")), + configInfo.getContent(), md5, betaIps, srcIp, srcUser, appNameTmp, encryptedDataKey, configInfo.getDataId(), configInfo.getGroup(), tenantTmp); return getBetaOperateResult(configInfo.getDataId(), configInfo.getGroup(), tenantTmp); - } catch (CannotGetJdbcConnectionException e) { LogUtil.FATAL_LOG.error("[db-error] " + e, e); throw e; @@ -214,15 +227,13 @@ public ConfigOperateResult updateConfigInfo4BetaCas(ConfigInfo configInfo, Strin try { ConfigInfoBetaMapper configInfoBetaMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO_BETA); - Timestamp time = new Timestamp(System.currentTimeMillis()); - + MapperContext context = new MapperContext(); context.putUpdateParameter(FieldConstant.CONTENT, configInfo.getContent()); context.putUpdateParameter(FieldConstant.MD5, md5); context.putUpdateParameter(FieldConstant.BETA_IPS, betaIps); context.putUpdateParameter(FieldConstant.SRC_IP, srcIp); context.putUpdateParameter(FieldConstant.SRC_USER, srcUser); - context.putUpdateParameter(FieldConstant.GMT_MODIFIED, time); context.putUpdateParameter(FieldConstant.APP_NAME, appNameTmp); context.putWhereParameter(FieldConstant.DATA_ID, configInfo.getDataId()); diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoPersistServiceImpl.java b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoPersistServiceImpl.java index 916fe373637..6638c9a993b 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoPersistServiceImpl.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoPersistServiceImpl.java @@ -45,8 +45,10 @@ import com.alibaba.nacos.plugin.datasource.constants.ContextConstant; import com.alibaba.nacos.plugin.datasource.constants.FieldConstant; import com.alibaba.nacos.plugin.datasource.constants.TableConstant; +import com.alibaba.nacos.plugin.datasource.enums.TrustedSqlFunctionEnum; import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoMapper; import com.alibaba.nacos.plugin.datasource.mapper.ConfigTagsRelationMapper; +import com.alibaba.nacos.plugin.datasource.model.ColumnFunctionPair; import com.alibaba.nacos.plugin.datasource.model.MapperContext; import com.alibaba.nacos.plugin.datasource.model.MapperResult; import com.alibaba.nacos.plugin.encryption.handler.EncryptionHandler; @@ -234,11 +236,8 @@ public long addConfigInfoAtomic(final long configId, final String srcIp, final S PreparedStatement createPsForInsertConfigInfo(final String srcIp, final String srcUser, final ConfigInfo configInfo, Map configAdvanceInfo, Connection connection, ConfigInfoMapper configInfoMapper) throws SQLException { - - Timestamp now = new Timestamp(System.currentTimeMillis()); final String appNameTmp = StringUtils.defaultEmptyIfBlank(configInfo.getAppName()); final String tenantTmp = StringUtils.defaultEmptyIfBlank(configInfo.getTenant()); - final String desc = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("desc"); final String use = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("use"); final String effect = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("effect"); @@ -246,13 +245,24 @@ PreparedStatement createPsForInsertConfigInfo(final String srcIp, final String s final String schema = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("schema"); final String encryptedDataKey = configInfo.getEncryptedDataKey() == null ? StringUtils.EMPTY : configInfo.getEncryptedDataKey(); - final String md5Tmp = MD5Utils.md5Hex(configInfo.getContent(), Constants.ENCODE); - String insertSql = configInfoMapper.insert( - Arrays.asList("data_id", "group_id", "tenant_id", "app_name", "content", "md5", "src_ip", "src_user", - "gmt_create", "gmt_modified", "c_desc", "c_use", "effect", "type", "c_schema", - "encrypted_data_key")); + Arrays.asList(ColumnFunctionPair.withColumn("data_id"), + ColumnFunctionPair.withColumn("group_id"), + ColumnFunctionPair.withColumn("tenant_id"), + ColumnFunctionPair.withColumn("app_name"), + ColumnFunctionPair.withColumn("content"), + ColumnFunctionPair.withColumn("md5"), + ColumnFunctionPair.withColumn("src_ip"), + ColumnFunctionPair.withColumn("src_user"), + ColumnFunctionPair.withColumnAndFunction("gmt_create", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP), + ColumnFunctionPair.withColumnAndFunction("gmt_modified", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP), + ColumnFunctionPair.withColumn("c_desc"), + ColumnFunctionPair.withColumn("c_use"), + ColumnFunctionPair.withColumn("effect"), + ColumnFunctionPair.withColumn("type"), + ColumnFunctionPair.withColumn("c_schema"), + ColumnFunctionPair.withColumn("encrypted_data_key"))); PreparedStatement ps = connection.prepareStatement(insertSql, configInfoMapper.getPrimaryKeyGeneratedKeys()); ps.setString(1, configInfo.getDataId()); ps.setString(2, configInfo.getGroup()); @@ -262,14 +272,12 @@ PreparedStatement createPsForInsertConfigInfo(final String srcIp, final String s ps.setString(6, md5Tmp); ps.setString(7, srcIp); ps.setString(8, srcUser); - ps.setTimestamp(9, now); - ps.setTimestamp(10, now); - ps.setString(11, desc); - ps.setString(12, use); - ps.setString(13, effect); - ps.setString(14, type); - ps.setString(15, schema); - ps.setString(16, encryptedDataKey); + ps.setString(9, desc); + ps.setString(10, use); + ps.setString(11, effect); + ps.setString(12, type); + ps.setString(13, schema); + ps.setString(14, encryptedDataKey); return ps; } @@ -279,8 +287,14 @@ public void addConfigTagRelationAtomic(long configId, String tagName, String dat ConfigTagsRelationMapper configTagsRelationMapper = mapperManager.findMapper( dataSourceService.getDataSourceType(), TableConstant.CONFIG_TAGS_RELATION); jt.update(configTagsRelationMapper.insert( - Arrays.asList("id", "tag_name", "tag_type", "data_id", "group_id", "tenant_id")), configId, tagName, - StringUtils.EMPTY, dataId, group, tenant); + Arrays.asList( + ColumnFunctionPair.withColumn("id"), + ColumnFunctionPair.withColumn("tag_name"), + ColumnFunctionPair.withColumn("tag_type"), + ColumnFunctionPair.withColumn("data_id"), + ColumnFunctionPair.withColumn("group_id"), + ColumnFunctionPair.withColumn("tenant_id"))), + configId, tagName, StringUtils.EMPTY, dataId, group, tenant); } catch (CannotGetJdbcConnectionException e) { LogUtil.FATAL_LOG.error("[db-error] " + e, e); throw e; @@ -615,14 +629,11 @@ private int updateConfigInfoAtomicCas(final ConfigInfo configInfo, final String try { ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO); - Timestamp now = new Timestamp(System.currentTimeMillis()); - MapperContext context = new MapperContext(); context.putUpdateParameter(FieldConstant.CONTENT, configInfo.getContent()); context.putUpdateParameter(FieldConstant.MD5, md5Tmp); context.putUpdateParameter(FieldConstant.SRC_IP, srcIp); context.putUpdateParameter(FieldConstant.SRC_USER, srcUser); - context.putUpdateParameter(FieldConstant.GMT_MODIFIED, now); context.putUpdateParameter(FieldConstant.APP_NAME, appNameTmp); context.putUpdateParameter(FieldConstant.C_DESC, desc); context.putUpdateParameter(FieldConstant.C_USE, use); @@ -656,17 +667,24 @@ public void updateConfigInfoAtomic(final ConfigInfo configInfo, final String src String schema = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("schema"); final String encryptedDataKey = configInfo.getEncryptedDataKey() == null ? StringUtils.EMPTY : configInfo.getEncryptedDataKey(); - try { ConfigInfoMapper configInfoMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO); - Timestamp now = new Timestamp(System.currentTimeMillis()); - jt.update(configInfoMapper.update( - Arrays.asList("content", "md5", "src_ip", "src_user", "gmt_modified", "app_name", "c_desc", "c_use", - "effect", "type", "c_schema", "encrypted_data_key"), + Arrays.asList(ColumnFunctionPair.withColumn("content"), + ColumnFunctionPair.withColumn("md5"), + ColumnFunctionPair.withColumn("src_ip"), + ColumnFunctionPair.withColumn("src_user"), + ColumnFunctionPair.withColumnAndFunction("gmt_modified", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP), + ColumnFunctionPair.withColumn("app_name"), + ColumnFunctionPair.withColumn("c_desc"), + ColumnFunctionPair.withColumn("c_use"), + ColumnFunctionPair.withColumn("effect"), + ColumnFunctionPair.withColumn("type"), + ColumnFunctionPair.withColumn("c_schema"), + ColumnFunctionPair.withColumn("encrypted_data_key")), Arrays.asList("data_id", "group_id", "tenant_id")), configInfo.getContent(), md5Tmp, srcIp, srcUser, - now, appNameTmp, desc, use, effect, type, schema, encryptedDataKey, configInfo.getDataId(), + appNameTmp, desc, use, effect, type, schema, encryptedDataKey, configInfo.getDataId(), configInfo.getGroup(), tenantTmp); } catch (CannotGetJdbcConnectionException e) { LogUtil.FATAL_LOG.error("[db-error] " + e, e); diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoTagPersistServiceImpl.java b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoTagPersistServiceImpl.java index d304ede1b72..5016f5ffd1d 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoTagPersistServiceImpl.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoTagPersistServiceImpl.java @@ -35,7 +35,9 @@ import com.alibaba.nacos.plugin.datasource.constants.CommonConstant; import com.alibaba.nacos.plugin.datasource.constants.FieldConstant; import com.alibaba.nacos.plugin.datasource.constants.TableConstant; +import com.alibaba.nacos.plugin.datasource.enums.TrustedSqlFunctionEnum; import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoTagMapper; +import com.alibaba.nacos.plugin.datasource.model.ColumnFunctionPair; import com.alibaba.nacos.plugin.datasource.model.MapperContext; import com.alibaba.nacos.plugin.datasource.model.MapperResult; import com.alibaba.nacos.sys.env.EnvUtil; @@ -122,14 +124,21 @@ public ConfigOperateResult addConfigInfo4Tag(ConfigInfo configInfo, String tag, try { ConfigInfoTagMapper configInfoTagMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO_TAG); - Timestamp time = new Timestamp(System.currentTimeMillis()); - jt.update(configInfoTagMapper.insert( - Arrays.asList("data_id", "group_id", "tenant_id", "tag_id", "app_name", "content", "md5", "src_ip", - "src_user", "gmt_create", "gmt_modified")), configInfo.getDataId(), configInfo.getGroup(), - tenantTmp, tagTmp, appNameTmp, configInfo.getContent(), md5, srcIp, srcUser, time, time); + Arrays.asList( + ColumnFunctionPair.withColumn("data_id"), + ColumnFunctionPair.withColumn("group_id"), + ColumnFunctionPair.withColumn("tenant_id"), + ColumnFunctionPair.withColumn("tag_id"), + ColumnFunctionPair.withColumn("app_name"), + ColumnFunctionPair.withColumn("content"), + ColumnFunctionPair.withColumn("md5"), + ColumnFunctionPair.withColumn("src_ip"), + ColumnFunctionPair.withColumn("src_user"), + ColumnFunctionPair.withColumnAndFunction("gmt_create", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP), + ColumnFunctionPair.withColumnAndFunction("gmt_modified", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP))), + configInfo.getDataId(), configInfo.getGroup(), tenantTmp, tagTmp, appNameTmp, configInfo.getContent(), md5, srcIp, srcUser); return getTagOperateResult(configInfo.getDataId(), configInfo.getGroup(), tenantTmp, tagTmp); - } catch (CannotGetJdbcConnectionException e) { LogUtil.FATAL_LOG.error("[db-error] " + e, e); throw e; @@ -183,13 +192,17 @@ public ConfigOperateResult updateConfigInfo4Tag(ConfigInfo configInfo, String ta String md5 = MD5Utils.md5Hex(configInfo.getContent(), Constants.ENCODE); ConfigInfoTagMapper configInfoTagMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO_TAG); - Timestamp time = new Timestamp(System.currentTimeMillis()); jt.update(configInfoTagMapper.update( - Arrays.asList("content", "md5", "src_ip", "src_user", "gmt_modified", "app_name"), - Arrays.asList("data_id", "group_id", "tenant_id", "tag_id")), configInfo.getContent(), md5, srcIp, - srcUser, time, appNameTmp, configInfo.getDataId(), configInfo.getGroup(), tenantTmp, tagTmp); + Arrays.asList( + ColumnFunctionPair.withColumn("content"), + ColumnFunctionPair.withColumn("md5"), + ColumnFunctionPair.withColumn("src_ip"), + ColumnFunctionPair.withColumn("src_user"), + ColumnFunctionPair.withColumnAndFunction("gmt_modified", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP), + ColumnFunctionPair.withColumn("app_name")), + Arrays.asList("data_id", "group_id", "tenant_id", "tag_id")), + configInfo.getContent(), md5, srcIp, srcUser, appNameTmp, configInfo.getDataId(), configInfo.getGroup(), tenantTmp, tagTmp); return getTagOperateResult(configInfo.getDataId(), configInfo.getGroup(), tenantTmp, tagTmp); - } catch (CannotGetJdbcConnectionException e) { LogUtil.FATAL_LOG.error("[db-error] " + e, e); throw e; @@ -206,14 +219,11 @@ public ConfigOperateResult updateConfigInfo4TagCas(ConfigInfo configInfo, String String md5 = MD5Utils.md5Hex(configInfo.getContent(), Constants.ENCODE); ConfigInfoTagMapper configInfoTagMapper = mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_INFO_TAG); - Timestamp time = new Timestamp(System.currentTimeMillis()); - MapperContext context = new MapperContext(); context.putUpdateParameter(FieldConstant.CONTENT, configInfo.getContent()); context.putUpdateParameter(FieldConstant.MD5, md5); context.putUpdateParameter(FieldConstant.SRC_IP, srcIp); context.putUpdateParameter(FieldConstant.SRC_USER, srcUser); - context.putUpdateParameter(FieldConstant.GMT_MODIFIED, time); context.putUpdateParameter(FieldConstant.APP_NAME, appNameTmp); context.putWhereParameter(FieldConstant.DATA_ID, configInfo.getDataId()); diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalHistoryConfigInfoPersistServiceImpl.java b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalHistoryConfigInfoPersistServiceImpl.java index eeec61d6130..5a5f19cef1d 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalHistoryConfigInfoPersistServiceImpl.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalHistoryConfigInfoPersistServiceImpl.java @@ -35,6 +35,7 @@ import com.alibaba.nacos.plugin.datasource.constants.FieldConstant; import com.alibaba.nacos.plugin.datasource.constants.TableConstant; import com.alibaba.nacos.plugin.datasource.mapper.HistoryConfigInfoMapper; +import com.alibaba.nacos.plugin.datasource.model.ColumnFunctionPair; import com.alibaba.nacos.plugin.datasource.model.MapperContext; import com.alibaba.nacos.plugin.datasource.model.MapperResult; import com.alibaba.nacos.sys.env.EnvUtil; @@ -93,15 +94,25 @@ public void insertConfigHistoryAtomic(long id, ConfigInfo configInfo, String src String tenantTmp = StringUtils.defaultEmptyIfBlank(configInfo.getTenant()); final String md5Tmp = MD5Utils.md5Hex(configInfo.getContent(), Constants.ENCODE); String encryptedDataKey = StringUtils.defaultEmptyIfBlank(configInfo.getEncryptedDataKey()); - try { HistoryConfigInfoMapper historyConfigInfoMapper = mapperManager.findMapper( dataSourceService.getDataSourceType(), TableConstant.HIS_CONFIG_INFO); jt.update(historyConfigInfoMapper.insert( - Arrays.asList("id", "data_id", "group_id", "tenant_id", "app_name", "content", "md5", "src_ip", - "src_user", "gmt_modified", "op_type", "encrypted_data_key")), id, configInfo.getDataId(), - configInfo.getGroup(), tenantTmp, appNameTmp, configInfo.getContent(), md5Tmp, srcIp, srcUser, time, - ops, encryptedDataKey); + Arrays.asList( + ColumnFunctionPair.withColumn("id"), + ColumnFunctionPair.withColumn("data_id"), + ColumnFunctionPair.withColumn("group_id"), + ColumnFunctionPair.withColumn("tenant_id"), + ColumnFunctionPair.withColumn("app_name"), + ColumnFunctionPair.withColumn("content"), + ColumnFunctionPair.withColumn("md5"), + ColumnFunctionPair.withColumn("src_ip"), + ColumnFunctionPair.withColumn("src_user"), + ColumnFunctionPair.withColumn("gmt_modified"), + ColumnFunctionPair.withColumn("op_type"), + ColumnFunctionPair.withColumn("encrypted_data_key"))), + id, configInfo.getDataId(), configInfo.getGroup(), tenantTmp, appNameTmp, + configInfo.getContent(), md5Tmp, srcIp, srcUser, time, ops, encryptedDataKey); } catch (DataAccessException e) { LogUtil.FATAL_LOG.error("[db-error] " + e, e); throw e; diff --git a/config/src/test/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedConfigInfoBetaPersistServiceImplTest.java b/config/src/test/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedConfigInfoBetaPersistServiceImplTest.java index 89f5e1f157d..c0f1388e0e0 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedConfigInfoBetaPersistServiceImplTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedConfigInfoBetaPersistServiceImplTest.java @@ -125,7 +125,7 @@ void testInsertOrUpdateBetaOfUpdate() { //verify update to be invoked embeddedStorageContextHolderMockedStatic.verify( () -> EmbeddedStorageContextHolder.addSqlContext(anyString(), eq(configInfo.getContent()), eq(configInfo.getMd5()), - eq(betaIps), eq(srcIp), eq(srcUser), any(Timestamp.class), eq(configInfo.getAppName()), + eq(betaIps), eq(srcIp), eq(srcUser), eq(configInfo.getAppName()), eq(configInfo.getEncryptedDataKey()), eq(dataId), eq(group), eq(tenant)), times(1)); } @@ -163,7 +163,7 @@ void testInsertOrUpdateBetaOfAdd() { embeddedStorageContextHolderMockedStatic.verify( () -> EmbeddedStorageContextHolder.addSqlContext(anyString(), eq(dataId), eq(group), eq(tenant), eq(configInfo.getAppName()), eq(configInfo.getContent()), eq(configInfo.getMd5()), eq(betaIps), eq(srcIp), - eq(srcUser), any(Timestamp.class), any(Timestamp.class), eq(configInfo.getEncryptedDataKey())), times(1)); + eq(srcUser), eq(configInfo.getEncryptedDataKey())), times(1)); } @Test @@ -240,7 +240,7 @@ void testInsertOrUpdateBetaCasOfAdd() { embeddedStorageContextHolderMockedStatic.verify( () -> EmbeddedStorageContextHolder.addSqlContext(anyString(), eq(dataId), eq(group), eq(tenant), eq(configInfo.getAppName()), eq(configInfo.getContent()), eq(configInfo.getMd5()), eq(betaIps), eq(srcIp), - eq(srcUser), any(Timestamp.class), any(Timestamp.class), eq(configInfo.getEncryptedDataKey())), times(1)); + eq(srcUser), eq(configInfo.getEncryptedDataKey())), times(1)); } diff --git a/config/src/test/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedConfigInfoPersistServiceImplTest.java b/config/src/test/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedConfigInfoPersistServiceImplTest.java index db8378a167a..0d42ca15c31 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedConfigInfoPersistServiceImplTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedConfigInfoPersistServiceImplTest.java @@ -164,8 +164,8 @@ void testInsertOrUpdateOfInsertConfigSuccess() { //expect insert config info invoked. embeddedStorageContextHolderMockedStatic.verify( () -> EmbeddedStorageContextHolder.addSqlContext(anyString(), anyLong(), eq(dataId), eq(group), eq(tenant), eq(appName), - eq(content), eq(MD5Utils.md5Hex(content, Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser), any(Timestamp.class), - any(Timestamp.class), eq(desc), eq(use), eq(effect), eq(type), eq(schema), eq(encryptedDataKey)), times(1)); + eq(content), eq(MD5Utils.md5Hex(content, Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser), + eq(desc), eq(use), eq(effect), eq(type), eq(schema), eq(encryptedDataKey)), times(1)); //expect insert config tags embeddedStorageContextHolderMockedStatic.verify( () -> EmbeddedStorageContextHolder.addSqlContext(anyString(), anyLong(), eq("tag1"), eq(StringUtils.EMPTY), eq(dataId), @@ -221,8 +221,8 @@ void testInsertOrUpdateCasOfInsertConfigSuccess() { //expect insert config info invoked. embeddedStorageContextHolderMockedStatic.verify( () -> EmbeddedStorageContextHolder.addSqlContext(anyString(), anyLong(), eq(dataId), eq(group), eq(tenant), eq(appName), - eq(content), eq(MD5Utils.md5Hex(content, Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser), any(Timestamp.class), - any(Timestamp.class), eq(desc), eq(use), eq(effect), eq(type), eq(schema), eq(encryptedDatakey)), times(1)); + eq(content), eq(MD5Utils.md5Hex(content, Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser), + eq(desc), eq(use), eq(effect), eq(type), eq(schema), eq(encryptedDatakey)), times(1)); //expect insert config tags embeddedStorageContextHolderMockedStatic.verify( () -> EmbeddedStorageContextHolder.addSqlContext(anyString(), anyLong(), eq("tag1"), eq(StringUtils.EMPTY), eq(dataId), @@ -281,7 +281,7 @@ void testInsertOrUpdateOfUpdateConfigSuccess() { //expect update config info invoked. embeddedStorageContextHolderMockedStatic.verify(() -> EmbeddedStorageContextHolder.addSqlContext(anyString(), eq(content), - eq(MD5Utils.md5Hex(content, Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser), any(Timestamp.class), eq(appName), eq(desc), + eq(MD5Utils.md5Hex(content, Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser), eq(appName), eq(desc), eq(use), eq(effect), eq(type), eq(schema), eq(encryptedDataKey), eq(dataId), eq(group), eq(tenant)), times(1)); //expect insert config tags @@ -347,7 +347,7 @@ void testInsertOrUpdateCasOfUpdateConfigSuccess() { //expect update config info invoked. embeddedStorageContextHolderMockedStatic.verify( () -> EmbeddedStorageContextHolder.addSqlContext(eq(Boolean.TRUE), anyString(), eq(content), - eq(MD5Utils.md5Hex(content, Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser), any(Timestamp.class), eq(appName), + eq(MD5Utils.md5Hex(content, Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser), eq(appName), eq(desc), eq(use), eq(effect), eq(type), eq(schema), eq(encryptedDataKey), eq(dataId), eq(group), eq(tenant), eq(casMd5)), times(1)); diff --git a/config/src/test/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedConfigInfoTagPersistServiceImplTest.java b/config/src/test/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedConfigInfoTagPersistServiceImplTest.java index 5c09eeef356..839ca164924 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedConfigInfoTagPersistServiceImplTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedConfigInfoTagPersistServiceImplTest.java @@ -121,8 +121,7 @@ void testInsertOrUpdateTagOfAdd() { //mock insert invoked. embeddedStorageContextHolderMockedStatic.verify( () -> EmbeddedStorageContextHolder.addSqlContext(anyString(), eq(dataId), eq(group), eq(tenant), eq(tag), eq(appName), - eq(content), eq(MD5Utils.md5Hex(content, Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser), any(Timestamp.class), - any(Timestamp.class)), times(1)); + eq(content), eq(MD5Utils.md5Hex(content, Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser)), times(1)); assertEquals(configInfoStateWrapper.getId(), configOperateResult.getId()); assertEquals(configInfoStateWrapper.getLastModified(), configOperateResult.getLastModified()); @@ -151,7 +150,7 @@ void testInsertOrUpdateTagOfUpdate() { ConfigOperateResult configOperateResult = embeddedConfigInfoTagPersistService.insertOrUpdateTag(configInfo, tag, srcIp, srcUser); //verify update to be invoked embeddedStorageContextHolderMockedStatic.verify(() -> EmbeddedStorageContextHolder.addSqlContext(anyString(), eq(content), - eq(MD5Utils.md5Hex(content, Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser), any(Timestamp.class), eq(appName), + eq(MD5Utils.md5Hex(content, Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser), eq(appName), eq(dataId), eq(group), eq(tenant), eq(tag)), times(1)); assertEquals(configInfoStateWrapper.getId(), configOperateResult.getId()); assertEquals(configInfoStateWrapper.getLastModified(), configOperateResult.getLastModified()); @@ -185,8 +184,7 @@ void testInsertOrUpdateTagCasOfAdd() { //mock insert invoked. embeddedStorageContextHolderMockedStatic.verify( () -> EmbeddedStorageContextHolder.addSqlContext(anyString(), eq(dataId), eq(group), eq(tenant), eq(tag), eq(appName), - eq(content), eq(MD5Utils.md5Hex(content, Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser), any(Timestamp.class), - any(Timestamp.class)), times(1)); + eq(content), eq(MD5Utils.md5Hex(content, Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser)), times(1)); assertEquals(configInfoStateWrapper.getId(), configOperateResult.getId()); assertEquals(configInfoStateWrapper.getLastModified(), configOperateResult.getLastModified()); diff --git a/config/src/test/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoAggrPersistServiceImplTest.java b/config/src/test/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoAggrPersistServiceImplTest.java index 93209fb85fa..e54aefb900b 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoAggrPersistServiceImplTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoAggrPersistServiceImplTest.java @@ -38,7 +38,6 @@ import org.springframework.transaction.TransactionSystemException; import org.springframework.transaction.support.TransactionTemplate; -import java.sql.Timestamp; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -50,7 +49,6 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.when; @@ -130,8 +128,7 @@ void testAddAggrConfigInfoOfAddNewContent() { when(jdbcTemplate.queryForObject(anyString(), eq(new Object[] {dataId, group, tenant, datumId}), eq(String.class))).thenThrow( new EmptyResultDataAccessException(1)); //mock insert success - when(jdbcTemplate.update(anyString(), eq(dataId), eq(group), eq(tenant), eq(datumId), eq(appName), eq(content), - any(Timestamp.class))).thenReturn(1); + when(jdbcTemplate.update(anyString(), eq(dataId), eq(group), eq(tenant), eq(datumId), eq(appName), eq(content))).thenReturn(1); //execute boolean result = externalConfigInfoAggrPersistService.addAggrConfigInfo(dataId, group, tenant, datumId, appName, content); @@ -152,12 +149,10 @@ void testAddAggrConfigInfoOfUpdateNotEqualContent() { when(jdbcTemplate.queryForObject(anyString(), eq(new Object[] {dataId, group, tenant, datumId}), eq(String.class))).thenReturn( existContent); //mock update success,return 1 - when(jdbcTemplate.update(anyString(), eq(content), any(Timestamp.class), eq(dataId), eq(group), eq(tenant), - eq(datumId))).thenReturn(1); + when(jdbcTemplate.update(anyString(), eq(content), eq(dataId), eq(group), eq(tenant), eq(datumId))).thenReturn(1); //mock update content boolean result = externalConfigInfoAggrPersistService.addAggrConfigInfo(dataId, group, tenant, datumId, appName, content); assertTrue(result); - } @Test diff --git a/config/src/test/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoBetaPersistServiceImplTest.java b/config/src/test/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoBetaPersistServiceImplTest.java index 4327deccb9b..57a9747f540 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoBetaPersistServiceImplTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoBetaPersistServiceImplTest.java @@ -129,8 +129,7 @@ void testInsertOrUpdateBetaOfUpdate() { //verify update to be invoked Mockito.verify(jdbcTemplate, times(1)) .update(anyString(), eq(configInfo.getContent()), eq(configInfo.getMd5()), eq(betaIps), eq(srcIp), eq(srcUser), - any(Timestamp.class), eq(configInfo.getAppName()), eq(configInfo.getEncryptedDataKey()), eq(dataId), eq(group), - eq(tenant)); + eq(configInfo.getAppName()), eq(configInfo.getEncryptedDataKey()), eq(dataId), eq(group), eq(tenant)); } @Test @@ -165,8 +164,7 @@ void testInsertOrUpdateBetaOfAdd() { //verify add to be invoked Mockito.verify(jdbcTemplate, times(1)) .update(anyString(), eq(dataId), eq(group), eq(tenant), eq(configInfo.getAppName()), eq(configInfo.getContent()), - eq(configInfo.getMd5()), eq(betaIps), eq(srcIp), eq(srcUser), any(Timestamp.class), any(Timestamp.class), - eq(configInfo.getEncryptedDataKey())); + eq(configInfo.getMd5()), eq(betaIps), eq(srcIp), eq(srcUser), eq(configInfo.getEncryptedDataKey())); } @@ -195,7 +193,7 @@ void testInsertOrUpdateBetaOfException() { // mock update throw CannotGetJdbcConnectionException when(jdbcTemplate.update(anyString(), eq(configInfo.getContent()), eq(configInfo.getMd5()), eq(betaIps), eq(srcIp), eq(srcUser), - any(Timestamp.class), eq(configInfo.getAppName()), eq(configInfo.getEncryptedDataKey()), eq(dataId), eq(group), + eq(configInfo.getAppName()), eq(configInfo.getEncryptedDataKey()), eq(dataId), eq(group), eq(tenant))).thenThrow(new CannotGetJdbcConnectionException("mock fail")); //execute of update& expect. try { @@ -210,7 +208,7 @@ void testInsertOrUpdateBetaOfException() { eq(CONFIG_INFO_STATE_WRAPPER_ROW_MAPPER))).thenReturn(null); //mock add throw CannotGetJdbcConnectionException when(jdbcTemplate.update(anyString(), eq(dataId), eq(group), eq(tenant), eq(configInfo.getAppName()), eq(configInfo.getContent()), - eq(configInfo.getMd5()), eq(betaIps), eq(srcIp), eq(srcUser), any(Timestamp.class), any(Timestamp.class), + eq(configInfo.getMd5()), eq(betaIps), eq(srcIp), eq(srcUser), eq(configInfo.getEncryptedDataKey()))).thenThrow(new CannotGetJdbcConnectionException("mock fail add")); //execute of add& expect. try { @@ -307,8 +305,7 @@ void testInsertOrUpdateBetaCasOfAdd() { //verify add to be invoked Mockito.verify(jdbcTemplate, times(1)) .update(anyString(), eq(dataId), eq(group), eq(tenant), eq(configInfo.getAppName()), eq(configInfo.getContent()), - eq(configInfo.getMd5()), eq(betaIps), eq(srcIp), eq(srcUser), any(Timestamp.class), any(Timestamp.class), - eq(configInfo.getEncryptedDataKey())); + eq(configInfo.getMd5()), eq(betaIps), eq(srcIp), eq(srcUser), eq(configInfo.getEncryptedDataKey())); } @@ -353,7 +350,7 @@ void testInsertOrUpdateBetaCasOfException() { //mock add throw CannotGetJdbcConnectionException when(jdbcTemplate.update(anyString(), eq(dataId), eq(group), eq(tenant), eq(configInfo.getAppName()), eq(configInfo.getContent()), eq(MD5Utils.md5Hex(configInfo.getContent(), Constants.PERSIST_ENCODE)), eq(betaIps), eq(srcIp), eq(srcUser), - any(Timestamp.class), any(Timestamp.class), eq(configInfo.getEncryptedDataKey()))).thenThrow( + eq(configInfo.getEncryptedDataKey()))).thenThrow( new CannotGetJdbcConnectionException("mock fail add")); //execute of add& expect. diff --git a/config/src/test/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoPersistServiceImplTest.java b/config/src/test/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoPersistServiceImplTest.java index 499ac9604d6..99f57d438e8 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoPersistServiceImplTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoPersistServiceImplTest.java @@ -34,7 +34,9 @@ import com.alibaba.nacos.persistence.datasource.DynamicDataSource; import com.alibaba.nacos.persistence.model.Page; import com.alibaba.nacos.plugin.datasource.constants.TableConstant; +import com.alibaba.nacos.plugin.datasource.enums.TrustedSqlFunctionEnum; import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoMapper; +import com.alibaba.nacos.plugin.datasource.model.ColumnFunctionPair; import com.alibaba.nacos.sys.env.EnvUtil; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -151,12 +153,24 @@ void testInsertOrUpdateOfInsertConfigSuccess() { Mockito.when(jdbcTemplate.update(any(PreparedStatementCreator.class), eq(generatedKeyHolder))).thenReturn(1); Mockito.when(jdbcTemplate.update(eq(externalConfigInfoPersistService.mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_TAGS_RELATION) - .insert(Arrays.asList("id", "tag_name", "tag_type", "data_id", "group_id", "tenant_id"))), eq(insertConfigIndoId), + .insert(Arrays.asList( + ColumnFunctionPair.withColumn("id"), + ColumnFunctionPair.withColumn("tag_name"), + ColumnFunctionPair.withColumn("tag_type"), + ColumnFunctionPair.withColumn("data_id"), + ColumnFunctionPair.withColumn("group_id"), + ColumnFunctionPair.withColumn("tenant_id")))), eq(insertConfigIndoId), eq("tag1"), eq(StringUtils.EMPTY), eq(dataId), eq(group), eq(tenant))).thenReturn(1); Mockito.when(jdbcTemplate.update(eq(externalConfigInfoPersistService.mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_TAGS_RELATION) - .insert(Arrays.asList("id", "tag_name", "tag_type", "data_id", "group_id", "tenant_id"))), eq(insertConfigIndoId), - eq("tag2"), eq(StringUtils.EMPTY), eq(dataId), eq(group), eq(tenant))).thenReturn(1); + .insert(Arrays.asList( + ColumnFunctionPair.withColumn("id"), + ColumnFunctionPair.withColumn("tag_name"), + ColumnFunctionPair.withColumn("tag_type"), + ColumnFunctionPair.withColumn("data_id"), + ColumnFunctionPair.withColumn("group_id"), + ColumnFunctionPair.withColumn("tenant_id")))), + eq(insertConfigIndoId), eq("tag2"), eq(StringUtils.EMPTY), eq(dataId), eq(group), eq(tenant))).thenReturn(1); String srcIp = "srcIp"; String srcUser = "srcUser"; //mock insert config info @@ -170,13 +184,25 @@ void testInsertOrUpdateOfInsertConfigSuccess() { Mockito.verify(jdbcTemplate, times(1)).update(eq( externalConfigInfoPersistService.mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_TAGS_RELATION) - .insert(Arrays.asList("id", "tag_name", "tag_type", "data_id", "group_id", "tenant_id"))), eq(insertConfigIndoId), - eq("tag1"), eq(StringUtils.EMPTY), eq(dataId), eq(group), eq(tenant)); + .insert(Arrays.asList( + ColumnFunctionPair.withColumn("id"), + ColumnFunctionPair.withColumn("tag_name"), + ColumnFunctionPair.withColumn("tag_type"), + ColumnFunctionPair.withColumn("data_id"), + ColumnFunctionPair.withColumn("group_id"), + ColumnFunctionPair.withColumn("tenant_id")))), + eq(insertConfigIndoId), eq("tag1"), eq(StringUtils.EMPTY), eq(dataId), eq(group), eq(tenant)); Mockito.verify(jdbcTemplate, times(1)).update(eq( externalConfigInfoPersistService.mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_TAGS_RELATION) - .insert(Arrays.asList("id", "tag_name", "tag_type", "data_id", "group_id", "tenant_id"))), eq(insertConfigIndoId), - eq("tag2"), eq(StringUtils.EMPTY), eq(dataId), eq(group), eq(tenant)); + .insert(Arrays.asList( + ColumnFunctionPair.withColumn("id"), + ColumnFunctionPair.withColumn("tag_name"), + ColumnFunctionPair.withColumn("tag_type"), + ColumnFunctionPair.withColumn("data_id"), + ColumnFunctionPair.withColumn("group_id"), + ColumnFunctionPair.withColumn("tenant_id")))), + eq(insertConfigIndoId), eq("tag2"), eq(StringUtils.EMPTY), eq(dataId), eq(group), eq(tenant)); //expect insert history info Mockito.verify(historyConfigInfoPersistService, times(1)) @@ -205,12 +231,24 @@ void testInsertOrUpdateCasOfInsertConfigSuccess() { Mockito.when(jdbcTemplate.update(any(PreparedStatementCreator.class), eq(generatedKeyHolder))).thenReturn(1); Mockito.when(jdbcTemplate.update(eq(externalConfigInfoPersistService.mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_TAGS_RELATION) - .insert(Arrays.asList("id", "tag_name", "tag_type", "data_id", "group_id", "tenant_id"))), eq(insertConfigIndoId), - eq("tag1"), eq(StringUtils.EMPTY), eq(dataId), eq(group), eq(tenant))).thenReturn(1); + .insert(Arrays.asList( + ColumnFunctionPair.withColumn("id"), + ColumnFunctionPair.withColumn("tag_name"), + ColumnFunctionPair.withColumn("tag_type"), + ColumnFunctionPair.withColumn("data_id"), + ColumnFunctionPair.withColumn("group_id"), + ColumnFunctionPair.withColumn("tenant_id")))), + eq(insertConfigIndoId), eq("tag1"), eq(StringUtils.EMPTY), eq(dataId), eq(group), eq(tenant))).thenReturn(1); Mockito.when(jdbcTemplate.update(eq(externalConfigInfoPersistService.mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_TAGS_RELATION) - .insert(Arrays.asList("id", "tag_name", "tag_type", "data_id", "group_id", "tenant_id"))), eq(insertConfigIndoId), - eq("tag2"), eq(StringUtils.EMPTY), eq(dataId), eq(group), eq(tenant))).thenReturn(1); + .insert(Arrays.asList( + ColumnFunctionPair.withColumn("id"), + ColumnFunctionPair.withColumn("tag_name"), + ColumnFunctionPair.withColumn("tag_type"), + ColumnFunctionPair.withColumn("data_id"), + ColumnFunctionPair.withColumn("group_id"), + ColumnFunctionPair.withColumn("tenant_id")))), + eq(insertConfigIndoId), eq("tag2"), eq(StringUtils.EMPTY), eq(dataId), eq(group), eq(tenant))).thenReturn(1); String srcIp = "srcIp"; String srcUser = "srcUser"; //mock insert config info @@ -224,13 +262,25 @@ void testInsertOrUpdateCasOfInsertConfigSuccess() { Mockito.verify(jdbcTemplate, times(1)).update(eq( externalConfigInfoPersistService.mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_TAGS_RELATION) - .insert(Arrays.asList("id", "tag_name", "tag_type", "data_id", "group_id", "tenant_id"))), eq(insertConfigIndoId), - eq("tag1"), eq(StringUtils.EMPTY), eq(dataId), eq(group), eq(tenant)); + .insert(Arrays.asList( + ColumnFunctionPair.withColumn("id"), + ColumnFunctionPair.withColumn("tag_name"), + ColumnFunctionPair.withColumn("tag_type"), + ColumnFunctionPair.withColumn("data_id"), + ColumnFunctionPair.withColumn("group_id"), + ColumnFunctionPair.withColumn("tenant_id")))), + eq(insertConfigIndoId), eq("tag1"), eq(StringUtils.EMPTY), eq(dataId), eq(group), eq(tenant)); Mockito.verify(jdbcTemplate, times(1)).update(eq( externalConfigInfoPersistService.mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_TAGS_RELATION) - .insert(Arrays.asList("id", "tag_name", "tag_type", "data_id", "group_id", "tenant_id"))), eq(insertConfigIndoId), - eq("tag2"), eq(StringUtils.EMPTY), eq(dataId), eq(group), eq(tenant)); + .insert(Arrays.asList( + ColumnFunctionPair.withColumn("id"), + ColumnFunctionPair.withColumn("tag_name"), + ColumnFunctionPair.withColumn("tag_type"), + ColumnFunctionPair.withColumn("data_id"), + ColumnFunctionPair.withColumn("group_id"), + ColumnFunctionPair.withColumn("tenant_id")))), + eq(insertConfigIndoId), eq("tag2"), eq(StringUtils.EMPTY), eq(dataId), eq(group), eq(tenant)); //expect insert history info Mockito.verify(historyConfigInfoPersistService, times(1)) @@ -301,9 +351,20 @@ void testInsertOrUpdateOfUpdateConfigSuccess() { String srcUser = "srcUser"; //mock update config info Mockito.when(jdbcTemplate.update(eq(externalConfigInfoPersistService.mapperManager.findMapper(dataSourceService.getDataSourceType(), - TableConstant.CONFIG_INFO) - .update(Arrays.asList("content", "md5", "src_ip", "src_user", "gmt_modified", "app_name", "c_desc", "c_use", "effect", - "type", "c_schema", "encrypted_data_key"), Arrays.asList("data_id", "group_id", "tenant_id"))), + TableConstant.CONFIG_INFO).update(Arrays.asList( + ColumnFunctionPair.withColumn("content"), + ColumnFunctionPair.withColumn("md5"), + ColumnFunctionPair.withColumn("src_ip"), + ColumnFunctionPair.withColumn("src_user"), + ColumnFunctionPair.withColumnAndFunction("gmt_modified", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP), + ColumnFunctionPair.withColumn("app_name"), + ColumnFunctionPair.withColumn("c_desc"), + ColumnFunctionPair.withColumn("c_use"), + ColumnFunctionPair.withColumn("effect"), + ColumnFunctionPair.withColumn("type"), + ColumnFunctionPair.withColumn("c_schema"), + ColumnFunctionPair.withColumn("encrypted_data_key")), + Arrays.asList("data_id", "group_id", "tenant_id"))), eq(configInfo.getContent()), eq(configInfo.getMd5()), eq(srcIp), eq(srcUser), any(), eq(configInfoWrapperOld.getAppName()), eq(configAdvanceInfo.get("desc")), eq(configAdvanceInfo.get("use")), eq(configAdvanceInfo.get("effect")), eq(configAdvanceInfo.get("type")), eq(configAdvanceInfo.get("schema")), eq(encryptedDataKey), eq(configInfo.getDataId()), @@ -312,8 +373,14 @@ void testInsertOrUpdateOfUpdateConfigSuccess() { //mock insert config tags. Mockito.when(jdbcTemplate.update(eq(externalConfigInfoPersistService.mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_TAGS_RELATION) - .insert(Arrays.asList("id", "tag_name", "tag_type", "data_id", "group_id", "tenant_id"))), eq(12345678765L), anyString(), - eq(StringUtils.EMPTY), eq(dataId), eq(group), eq(tenant))).thenReturn(1); + .insert(Arrays.asList( + ColumnFunctionPair.withColumn("id"), + ColumnFunctionPair.withColumn("tag_name"), + ColumnFunctionPair.withColumn("tag_type"), + ColumnFunctionPair.withColumn("data_id"), + ColumnFunctionPair.withColumn("group_id"), + ColumnFunctionPair.withColumn("tenant_id")))), + eq(12345678765L), anyString(), eq(StringUtils.EMPTY), eq(dataId), eq(group), eq(tenant))).thenReturn(1); //mock insert his config info Mockito.doNothing().when(historyConfigInfoPersistService) @@ -326,12 +393,24 @@ void testInsertOrUpdateOfUpdateConfigSuccess() { Mockito.verify(jdbcTemplate, times(1)).update(eq( externalConfigInfoPersistService.mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_TAGS_RELATION) - .insert(Arrays.asList("id", "tag_name", "tag_type", "data_id", "group_id", "tenant_id"))), + .insert(Arrays.asList( + ColumnFunctionPair.withColumn("id"), + ColumnFunctionPair.withColumn("tag_name"), + ColumnFunctionPair.withColumn("tag_type"), + ColumnFunctionPair.withColumn("data_id"), + ColumnFunctionPair.withColumn("group_id"), + ColumnFunctionPair.withColumn("tenant_id")))), eq(configInfoWrapperOld.getId()), eq("tag1"), eq(StringUtils.EMPTY), eq(dataId), eq(group), eq(tenant)); Mockito.verify(jdbcTemplate, times(1)).update(eq( externalConfigInfoPersistService.mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_TAGS_RELATION) - .insert(Arrays.asList("id", "tag_name", "tag_type", "data_id", "group_id", "tenant_id"))), + .insert(Arrays.asList( + ColumnFunctionPair.withColumn("id"), + ColumnFunctionPair.withColumn("tag_name"), + ColumnFunctionPair.withColumn("tag_type"), + ColumnFunctionPair.withColumn("data_id"), + ColumnFunctionPair.withColumn("group_id"), + ColumnFunctionPair.withColumn("tenant_id")))), eq(configInfoWrapperOld.getId()), eq("tag2"), eq(StringUtils.EMPTY), eq(dataId), eq(group), eq(tenant)); //expect insert history info @@ -343,7 +422,6 @@ void testInsertOrUpdateOfUpdateConfigSuccess() { @Test void testInsertOrUpdateCasOfUpdateConfigSuccess() { - Map configAdvanceInfo = new HashMap<>(); configAdvanceInfo.put("config_tags", "tag1,tag2"); configAdvanceInfo.put("desc", "desc11"); @@ -379,15 +457,21 @@ void testInsertOrUpdateCasOfUpdateConfigSuccess() { String srcUser = "srcUser"; //mock update config info cas Mockito.when(jdbcTemplate.update(anyString(), eq(content), eq(MD5Utils.md5Hex(content, Constants.PERSIST_ENCODE)), eq(srcIp), - eq(srcUser), any(Timestamp.class), eq(configInfoWrapperOld.getAppName()), eq(configAdvanceInfo.get("desc")), + eq(srcUser), eq(configInfoWrapperOld.getAppName()), eq(configAdvanceInfo.get("desc")), eq(configAdvanceInfo.get("use")), eq(configAdvanceInfo.get("effect")), eq(configAdvanceInfo.get("type")), eq(configAdvanceInfo.get("schema")), eq(encryptedDataKey), eq(dataId), eq(group), eq(tenant), eq(casMd5))).thenReturn(1); //mock insert config tags. Mockito.when(jdbcTemplate.update(eq(externalConfigInfoPersistService.mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_TAGS_RELATION) - .insert(Arrays.asList("id", "tag_name", "tag_type", "data_id", "group_id", "tenant_id"))), eq(configInfoWrapperOld.getId()), - anyString(), eq(StringUtils.EMPTY), eq(dataId), eq(group), eq(tenant))).thenReturn(1); + .insert(Arrays.asList( + ColumnFunctionPair.withColumn("id"), + ColumnFunctionPair.withColumn("tag_name"), + ColumnFunctionPair.withColumn("tag_type"), + ColumnFunctionPair.withColumn("data_id"), + ColumnFunctionPair.withColumn("group_id"), + ColumnFunctionPair.withColumn("tenant_id")))), + eq(configInfoWrapperOld.getId()), anyString(), eq(StringUtils.EMPTY), eq(dataId), eq(group), eq(tenant))).thenReturn(1); //mock insert his config info Mockito.doNothing().when(historyConfigInfoPersistService) @@ -398,7 +482,7 @@ void testInsertOrUpdateCasOfUpdateConfigSuccess() { //expect update config cas Mockito.verify(jdbcTemplate, times(1)) .update(anyString(), eq(content), eq(MD5Utils.md5Hex(content, Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser), - any(Timestamp.class), eq(configInfoWrapperOld.getAppName()), eq(configAdvanceInfo.get("desc")), + eq(configInfoWrapperOld.getAppName()), eq(configAdvanceInfo.get("desc")), eq(configAdvanceInfo.get("use")), eq(configAdvanceInfo.get("effect")), eq(configAdvanceInfo.get("type")), eq(configAdvanceInfo.get("schema")), eq(encryptedDataKey), eq(dataId), eq(group), eq(tenant), eq(casMd5)); @@ -406,12 +490,24 @@ void testInsertOrUpdateCasOfUpdateConfigSuccess() { Mockito.verify(jdbcTemplate, times(1)).update(eq( externalConfigInfoPersistService.mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_TAGS_RELATION) - .insert(Arrays.asList("id", "tag_name", "tag_type", "data_id", "group_id", "tenant_id"))), + .insert(Arrays.asList( + ColumnFunctionPair.withColumn("id"), + ColumnFunctionPair.withColumn("tag_name"), + ColumnFunctionPair.withColumn("tag_type"), + ColumnFunctionPair.withColumn("data_id"), + ColumnFunctionPair.withColumn("group_id"), + ColumnFunctionPair.withColumn("tenant_id")))), eq(configInfoWrapperOld.getId()), eq("tag1"), eq(StringUtils.EMPTY), eq(dataId), eq(group), eq(tenant)); Mockito.verify(jdbcTemplate, times(1)).update(eq( externalConfigInfoPersistService.mapperManager.findMapper(dataSourceService.getDataSourceType(), TableConstant.CONFIG_TAGS_RELATION) - .insert(Arrays.asList("id", "tag_name", "tag_type", "data_id", "group_id", "tenant_id"))), + .insert(Arrays.asList( + ColumnFunctionPair.withColumn("id"), + ColumnFunctionPair.withColumn("tag_name"), + ColumnFunctionPair.withColumn("tag_type"), + ColumnFunctionPair.withColumn("data_id"), + ColumnFunctionPair.withColumn("group_id"), + ColumnFunctionPair.withColumn("tenant_id")))), eq(configInfoWrapperOld.getId()), eq("tag2"), eq(StringUtils.EMPTY), eq(dataId), eq(group), eq(tenant)); //expect insert history info @@ -448,8 +544,6 @@ void testCreatePsForInsertConfigInfo() throws SQLException { externalConfigInfoPersistService.createPsForInsertConfigInfo(srcIp, srcUser, configInfo, configAdvanceInfo, mockConnection, configInfoMapper); Mockito.verify(preparedStatement, times(14)).setString(anyInt(), anyString()); - Mockito.verify(preparedStatement, times(2)).setTimestamp(anyInt(), any(Timestamp.class)); - } @Test diff --git a/config/src/test/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoTagPersistServiceImplTest.java b/config/src/test/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoTagPersistServiceImplTest.java index e19fc4224e0..8febcdee733 100644 --- a/config/src/test/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoTagPersistServiceImplTest.java +++ b/config/src/test/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoTagPersistServiceImplTest.java @@ -115,7 +115,7 @@ void testInsertOrUpdateTagOfAdd() { configInfoStateWrapper.setLastModified(System.currentTimeMillis()); configInfoStateWrapper.setId(234567890L); String tag = "tag123"; - Mockito.when(jdbcTemplate.queryForObject(anyString(), eq(new Object[] {dataId, group, tenant, tag}), + Mockito.when(jdbcTemplate.queryForObject(anyString(), eq(new Object[]{dataId, group, tenant, tag}), eq(CONFIG_INFO_STATE_WRAPPER_ROW_MAPPER))).thenThrow(new EmptyResultDataAccessException(1)) .thenReturn(configInfoStateWrapper); String srcIp = "ip345678"; @@ -124,10 +124,10 @@ void testInsertOrUpdateTagOfAdd() { //verify insert to be invoked Mockito.verify(jdbcTemplate, times(1)) .update(anyString(), eq(dataId), eq(group), eq(tenant), eq(tag), eq(appName), eq(configInfo.getContent()), - eq(configInfo.getMd5()), eq(srcIp), eq(srcUser), any(Timestamp.class), any(Timestamp.class)); + eq(configInfo.getMd5()), eq(srcIp), eq(srcUser)); assertEquals(configInfoStateWrapper.getId(), configOperateResult.getId()); assertEquals(configInfoStateWrapper.getLastModified(), configOperateResult.getLastModified()); - + } @Test @@ -152,7 +152,7 @@ void testInsertOrUpdateTagOfUpdate() { ConfigOperateResult configOperateResult = externalConfigInfoTagPersistService.insertOrUpdateTag(configInfo, tag, srcIp, srcUser); //verify update to be invoked Mockito.verify(jdbcTemplate, times(1)) - .update(anyString(), eq(configInfo.getContent()), eq(configInfo.getMd5()), eq(srcIp), eq(srcUser), any(Timestamp.class), + .update(anyString(), eq(configInfo.getContent()), eq(configInfo.getMd5()), eq(srcIp), eq(srcUser), eq(appName), eq(dataId), eq(group), eq(tenant), eq(tag)); assertEquals(configInfoStateWrapper.getId(), configOperateResult.getId()); assertEquals(configInfoStateWrapper.getLastModified(), configOperateResult.getLastModified()); @@ -184,8 +184,7 @@ void testInsertOrUpdateTagCasOfAdd() { //verify insert to be invoked Mockito.verify(jdbcTemplate, times(1)) .update(anyString(), eq(dataId), eq(group), eq(tenant), eq(tag), eq(appName), eq(configInfo.getContent()), - eq(MD5Utils.md5Hex(configInfo.getContent(), Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser), - any(Timestamp.class), any(Timestamp.class)); + eq(MD5Utils.md5Hex(configInfo.getContent(), Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser)); assertEquals(configInfoStateWrapper.getId(), configOperateResult.getId()); assertEquals(configInfoStateWrapper.getLastModified(), configOperateResult.getLastModified()); @@ -253,11 +252,11 @@ void testInsertOrUpdateTagCasOfException() { assertEquals("state query throw exception", e.getMessage()); } //mock get state return null,and execute add throw CannotGetJdbcConnectionException - Mockito.when(jdbcTemplate.queryForObject(anyString(), eq(new Object[] {dataId, group, tenant, tag}), + Mockito.when(jdbcTemplate.queryForObject(anyString(), eq(new Object[]{dataId, group, tenant, tag}), eq(CONFIG_INFO_STATE_WRAPPER_ROW_MAPPER))).thenReturn(null); Mockito.when(jdbcTemplate.update(anyString(), eq(dataId), eq(group), eq(tenant), eq(tag), eq(appName), eq(configInfo.getContent()), - eq(MD5Utils.md5Hex(configInfo.getContent(), Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser), any(Timestamp.class), - any(Timestamp.class))).thenThrow(new CannotGetJdbcConnectionException("throw exception add config tag")); + eq(MD5Utils.md5Hex(configInfo.getContent(), Constants.PERSIST_ENCODE)), eq(srcIp), eq(srcUser))) + .thenThrow(new CannotGetJdbcConnectionException("throw exception add config tag")); try { externalConfigInfoTagPersistService.insertOrUpdateTagCas(configInfo, tag, srcIp, srcUser); assertTrue(false); diff --git a/core/src/main/java/com/alibaba/nacos/core/namespace/repository/EmbeddedNamespacePersistServiceImpl.java b/core/src/main/java/com/alibaba/nacos/core/namespace/repository/EmbeddedNamespacePersistServiceImpl.java index 0e6860e507f..08123134768 100644 --- a/core/src/main/java/com/alibaba/nacos/core/namespace/repository/EmbeddedNamespacePersistServiceImpl.java +++ b/core/src/main/java/com/alibaba/nacos/core/namespace/repository/EmbeddedNamespacePersistServiceImpl.java @@ -29,7 +29,9 @@ import com.alibaba.nacos.plugin.datasource.MapperManager; import com.alibaba.nacos.plugin.datasource.constants.CommonConstant; import com.alibaba.nacos.plugin.datasource.constants.TableConstant; +import com.alibaba.nacos.plugin.datasource.enums.TrustedSqlFunctionEnum; import com.alibaba.nacos.plugin.datasource.mapper.TenantInfoMapper; +import com.alibaba.nacos.plugin.datasource.model.ColumnFunctionPair; import com.alibaba.nacos.sys.env.EnvUtil; import org.springframework.context.annotation.Conditional; import org.springframework.stereotype.Service; @@ -70,20 +72,23 @@ public EmbeddedNamespacePersistServiceImpl(DatabaseOperate databaseOperate) { this.mapperManager = MapperManager.instance(isDataSourceLogEnable); NotifyCenter.registerToSharePublisher(DerbyImportEvent.class); } - + @Override public void insertTenantInfoAtomic(String kp, String tenantId, String tenantName, String tenantDesc, - String createResource, final long time) { - + String createResource) { TenantInfoMapper tenantInfoMapper = mapperManager .findMapper(dataSourceService.getDataSourceType(), TableConstant.TENANT_INFO); 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, createResource, time, time}; - + .asList( + ColumnFunctionPair.withColumn("kp"), + ColumnFunctionPair.withColumn("tenant_id"), + ColumnFunctionPair.withColumn("tenant_name"), + ColumnFunctionPair.withColumn("tenant_desc"), + ColumnFunctionPair.withColumn("create_source"), + ColumnFunctionPair.withColumnAndFunction("gmt_create", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP), + ColumnFunctionPair.withColumnAndFunction("gmt_modified", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP))); + final Object[] args = new Object[]{kp, tenantId, tenantName, tenantDesc, createResource}; EmbeddedStorageContextHolder.addSqlContext(sql, args); - try { boolean result = databaseOperate.update(EmbeddedStorageContextHolder.getCurrentSqlContext()); if (!result) { @@ -110,15 +115,19 @@ public void removeTenantInfoAtomic(final String kp, final String tenantId) { @Override public void updateTenantNameAtomic(String kp, String tenantId, String tenantName, String tenantDesc) { - + TenantInfoMapper tenantInfoMapper = mapperManager .findMapper(dataSourceService.getDataSourceType(), TableConstant.TENANT_INFO); 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}; - + .update(Arrays.asList( + ColumnFunctionPair.withColumn("tenant_name"), + ColumnFunctionPair.withColumn("tenant_desc"), + ColumnFunctionPair.withColumnAndFunction("gmt_modified", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP)), + Arrays.asList("kp", "tenant_id")); + final Object[] args = new Object[]{tenantName, tenantDesc, kp, tenantId}; + EmbeddedStorageContextHolder.addSqlContext(sql, args); - + try { boolean result = databaseOperate.update(EmbeddedStorageContextHolder.getCurrentSqlContext()); if (!result) { diff --git a/core/src/main/java/com/alibaba/nacos/core/namespace/repository/ExternalNamespacePersistServiceImpl.java b/core/src/main/java/com/alibaba/nacos/core/namespace/repository/ExternalNamespacePersistServiceImpl.java index 014354f2ba1..b10cab0c609 100644 --- a/core/src/main/java/com/alibaba/nacos/core/namespace/repository/ExternalNamespacePersistServiceImpl.java +++ b/core/src/main/java/com/alibaba/nacos/core/namespace/repository/ExternalNamespacePersistServiceImpl.java @@ -24,7 +24,9 @@ import com.alibaba.nacos.plugin.datasource.MapperManager; import com.alibaba.nacos.plugin.datasource.constants.CommonConstant; import com.alibaba.nacos.plugin.datasource.constants.TableConstant; +import com.alibaba.nacos.plugin.datasource.enums.TrustedSqlFunctionEnum; import com.alibaba.nacos.plugin.datasource.mapper.TenantInfoMapper; +import com.alibaba.nacos.plugin.datasource.model.ColumnFunctionPair; import com.alibaba.nacos.sys.env.EnvUtil; import org.springframework.context.annotation.Conditional; import org.springframework.dao.DataAccessException; @@ -67,16 +69,22 @@ public ExternalNamespacePersistServiceImpl() { .getProperty(CommonConstant.NACOS_PLUGIN_DATASOURCE_LOG, Boolean.class, false); this.mapperManager = MapperManager.instance(isDataSourceLogEnable); } - + @Override - public void insertTenantInfoAtomic(String kp, String tenantId, String tenantName, String tenantDesc, - String createResource, final long time) { + public void insertTenantInfoAtomic(String kp, String tenantId, String tenantName, String tenantDesc, String createResource) { try { TenantInfoMapper tenantInfoMapper = mapperManager .findMapper(dataSourceService.getDataSourceType(), TableConstant.TENANT_INFO); jt.update(tenantInfoMapper.insert(Arrays - .asList("kp", "tenant_id", "tenant_name", "tenant_desc", "create_source", "gmt_create", - "gmt_modified")), kp, tenantId, tenantName, tenantDesc, createResource, time, time); + .asList( + ColumnFunctionPair.withColumn("kp"), + ColumnFunctionPair.withColumn("tenant_id"), + ColumnFunctionPair.withColumn("tenant_name"), + ColumnFunctionPair.withColumn("tenant_desc"), + ColumnFunctionPair.withColumn("create_source"), + ColumnFunctionPair.withColumnAndFunction("gmt_create", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP), + ColumnFunctionPair.withColumnAndFunction("gmt_modified", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP))), + kp, tenantId, tenantName, tenantDesc, createResource); } catch (DataAccessException e) { Loggers.CLUSTER.error("[db-error] " + e, e); throw e; @@ -100,8 +108,11 @@ public void updateTenantNameAtomic(String kp, String tenantId, String tenantName try { TenantInfoMapper tenantInfoMapper = mapperManager .findMapper(dataSourceService.getDataSourceType(), TableConstant.TENANT_INFO); - jt.update(tenantInfoMapper.update(Arrays.asList("tenant_name", "tenant_desc", "gmt_modified"), - Arrays.asList("kp", "tenant_id")), tenantName, tenantDesc, System.currentTimeMillis(), kp, + jt.update(tenantInfoMapper.update(Arrays.asList( + ColumnFunctionPair.withColumn("tenant_name"), + ColumnFunctionPair.withColumn("tenant_desc"), + ColumnFunctionPair.withColumnAndFunction("gmt_modified", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP)), + Arrays.asList("kp", "tenant_id")), tenantName, tenantDesc, kp, tenantId); } catch (DataAccessException e) { Loggers.CLUSTER.error("[db-error] " + e, e); diff --git a/core/src/main/java/com/alibaba/nacos/core/namespace/repository/NamespacePersistService.java b/core/src/main/java/com/alibaba/nacos/core/namespace/repository/NamespacePersistService.java index 40061b0928b..0f445c39488 100644 --- a/core/src/main/java/com/alibaba/nacos/core/namespace/repository/NamespacePersistService.java +++ b/core/src/main/java/com/alibaba/nacos/core/namespace/repository/NamespacePersistService.java @@ -39,10 +39,8 @@ public interface NamespacePersistService { * @param tenantName tenant name * @param tenantDesc tenant description * @param createResource create resource - * @param time time */ - void insertTenantInfoAtomic(String kp, String tenantId, String tenantName, String tenantDesc, String createResource, - final long time); + void insertTenantInfoAtomic(String kp, String tenantId, String tenantName, String tenantDesc, String createResource); //------------------------------------------delete---------------------------------------------// diff --git a/core/src/main/java/com/alibaba/nacos/core/service/NamespaceOperationService.java b/core/src/main/java/com/alibaba/nacos/core/service/NamespaceOperationService.java index 47ec6e44367..f3a5c8ee764 100644 --- a/core/src/main/java/com/alibaba/nacos/core/service/NamespaceOperationService.java +++ b/core/src/main/java/com/alibaba/nacos/core/service/NamespaceOperationService.java @@ -122,8 +122,7 @@ public Boolean createNamespace(String namespaceId, String namespaceName, String } namespacePersistService - .insertTenantInfoAtomic(DEFAULT_KP, namespaceId, namespaceName, namespaceDesc, DEFAULT_CREATE_SOURCE, - System.currentTimeMillis()); + .insertTenantInfoAtomic(DEFAULT_KP, namespaceId, namespaceName, namespaceDesc, DEFAULT_CREATE_SOURCE); return true; } diff --git a/core/src/test/java/com/alibaba/nacos/core/namespace/repository/EmbeddedNamespacePersistServiceTest.java b/core/src/test/java/com/alibaba/nacos/core/namespace/repository/EmbeddedNamespacePersistServiceTest.java index bf5d292acc2..58d0ad2f252 100644 --- a/core/src/test/java/com/alibaba/nacos/core/namespace/repository/EmbeddedNamespacePersistServiceTest.java +++ b/core/src/test/java/com/alibaba/nacos/core/namespace/repository/EmbeddedNamespacePersistServiceTest.java @@ -80,8 +80,7 @@ void insertTenantInfoAtomicTest1() { when(databaseOperate.update(anyList())).thenReturn(true); when(dataSourceService.getDataSourceType()).thenReturn("derby"); - embeddedNamespacePersistService.insertTenantInfoAtomic(kp, namespaceId, namespaceName, namespaceDesc, createRes, - System.currentTimeMillis()); + embeddedNamespacePersistService.insertTenantInfoAtomic(kp, namespaceId, namespaceName, namespaceDesc, createRes); verify(databaseOperate).update(anyList()); } @@ -96,10 +95,9 @@ void insertTenantInfoAtomicTest2() { String createRes = "nacos"; when(databaseOperate.update(anyList())).thenReturn(false); when(dataSourceService.getDataSourceType()).thenReturn("derby"); - + assertThrows(NacosRuntimeException.class, - () -> embeddedNamespacePersistService.insertTenantInfoAtomic(kp, namespaceId, namespaceName, namespaceDesc, createRes, - System.currentTimeMillis())); + () -> embeddedNamespacePersistService.insertTenantInfoAtomic(kp, namespaceId, namespaceName, namespaceDesc, createRes)); verify(databaseOperate).update(anyList()); } diff --git a/core/src/test/java/com/alibaba/nacos/core/namespace/repository/ExternalNamespacePersistServiceTest.java b/core/src/test/java/com/alibaba/nacos/core/namespace/repository/ExternalNamespacePersistServiceTest.java index 8cd3947f080..1ba67037b32 100644 --- a/core/src/test/java/com/alibaba/nacos/core/namespace/repository/ExternalNamespacePersistServiceTest.java +++ b/core/src/test/java/com/alibaba/nacos/core/namespace/repository/ExternalNamespacePersistServiceTest.java @@ -21,7 +21,6 @@ import com.alibaba.nacos.persistence.datasource.DataSourceService; import com.alibaba.nacos.persistence.datasource.DynamicDataSource; import com.alibaba.nacos.persistence.exception.NJdbcException; -import com.alibaba.nacos.persistence.repository.embedded.operate.DatabaseOperate; import com.alibaba.nacos.sys.env.EnvUtil; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -45,17 +44,13 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) class ExternalNamespacePersistServiceTest { - - @Mock - private DatabaseOperate databaseOperate; - + @Mock private DataSourceService dataSourceService; @@ -86,14 +81,12 @@ void insertTenantInfoAtomicTest() { String namespaceName = "namespaceName"; String namespaceDesc = "namespaceDesc"; when(dataSourceService.getDataSourceType()).thenReturn("mysql"); - externalNamespacePersistService.insertTenantInfoAtomic(kp, namespaceId, namespaceName, namespaceDesc, "nacos", - System.currentTimeMillis()); + externalNamespacePersistService.insertTenantInfoAtomic(kp, namespaceId, namespaceName, namespaceDesc, "nacos"); - when(jt.update(anyString(), anyString(), anyString(), anyString(), anyString(), anyString(), anyLong(), anyLong())).thenThrow( + when(jt.update(anyString(), anyString(), anyString(), anyString(), anyString(), anyString())).thenThrow( new NJdbcException("test")); assertThrows(DataAccessException.class, - () -> externalNamespacePersistService.insertTenantInfoAtomic(kp, namespaceId, namespaceName, namespaceDesc, "nacos", - System.currentTimeMillis())); + () -> externalNamespacePersistService.insertTenantInfoAtomic(kp, namespaceId, namespaceName, namespaceDesc, "nacos")); } @@ -117,8 +110,8 @@ void updateTenantNameAtomicTest() { String namespaceDesc = "namespaceDesc"; when(dataSourceService.getDataSourceType()).thenReturn("mysql"); externalNamespacePersistService.updateTenantNameAtomic(kp, namespaceId, namespaceName, namespaceDesc); - - when(jt.update(anyString(), anyString(), anyString(), anyLong(), anyString(), anyString())).thenThrow(new NJdbcException("test")); + + when(jt.update(anyString(), anyString(), anyString(), anyString(), anyString())).thenThrow(new NJdbcException("test")); assertThrows(DataAccessException.class, () -> externalNamespacePersistService.updateTenantNameAtomic(kp, namespaceId, namespaceName, namespaceDesc)); } diff --git a/core/src/test/java/com/alibaba/nacos/core/service/NamespaceOperationServiceTest.java b/core/src/test/java/com/alibaba/nacos/core/service/NamespaceOperationServiceTest.java index 6cb70e825bb..ef38d6b616f 100644 --- a/core/src/test/java/com/alibaba/nacos/core/service/NamespaceOperationServiceTest.java +++ b/core/src/test/java/com/alibaba/nacos/core/service/NamespaceOperationServiceTest.java @@ -36,7 +36,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.verify; @@ -133,7 +132,7 @@ void testCreateNamespace() throws NacosException { when(namespacePersistService.tenantInfoCountByTenantId(anyString())).thenReturn(0); namespaceOperationService.createNamespace(TEST_NAMESPACE_ID, TEST_NAMESPACE_NAME, TEST_NAMESPACE_DESC); verify(namespacePersistService).insertTenantInfoAtomic(eq(DEFAULT_KP), eq(TEST_NAMESPACE_ID), eq(TEST_NAMESPACE_NAME), - eq(TEST_NAMESPACE_DESC), any(), anyLong()); + eq(TEST_NAMESPACE_DESC), any()); } @Test diff --git a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/enums/TrustedSqlFunctionEnum.java b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/enums/TrustedSqlFunctionEnum.java new file mode 100644 index 00000000000..bdee62357a7 --- /dev/null +++ b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/enums/TrustedSqlFunctionEnum.java @@ -0,0 +1,47 @@ +/* + * Copyright 1999-2018 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.enums; + +/** + * The TrustedSqlFunctionEnum enum class is used to enumerate and manage a list of trusted built-in SQL functions. + * By using this enum, you can verify whether a given SQL function is part of the trusted functions list + * to avoid potential SQL injection risks. + * + * @author blake.qiu + */ +public enum TrustedSqlFunctionEnum { + + /** + * CURRENT_TIMESTAMP. + */ + CURRENT_TIMESTAMP("CURRENT_TIMESTAMP"), + + /** + * NOW(). + */ + NOW("NOW()"); + + private final String function; + + TrustedSqlFunctionEnum(String function) { + this.function = function; + } + + public String getFunction() { + return function; + } +} diff --git a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/AbstractMapper.java b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/AbstractMapper.java index 571c7219ade..5796652d41c 100644 --- a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/AbstractMapper.java +++ b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/AbstractMapper.java @@ -17,6 +17,8 @@ package com.alibaba.nacos.plugin.datasource.mapper; import com.alibaba.nacos.common.utils.CollectionUtils; +import com.alibaba.nacos.common.utils.StringUtils; +import com.alibaba.nacos.plugin.datasource.model.ColumnFunctionPair; import java.util.List; @@ -52,60 +54,72 @@ public String select(List columns, List where) { appendWhereClause(where, sql); return sql.toString(); } - + @Override - public String insert(List columns) { - StringBuilder sql = new StringBuilder(); - String method = "INSERT INTO "; - sql.append(method); - sql.append(getTableName()); - - int size = columns.size(); - sql.append("("); - for (int i = 0; i < size; i++) { - sql.append(columns.get(i)); - if (i != columns.size() - 1) { - sql.append(", "); - } + public String insert(List columnFunctionPairs) { + if (columnFunctionPairs == null || columnFunctionPairs.isEmpty()) { + throw new IllegalArgumentException("The columnFunctionPairs list is null or empty."); } - sql.append(") "); - - sql.append("VALUES"); - sql.append("("); - for (int i = 0; i < size; i++) { - sql.append("?"); - if (i != columns.size() - 1) { - sql.append(","); + + StringBuilder columns = new StringBuilder(); + StringBuilder values = new StringBuilder(); + + // Iterate and validate each ColumnFunctionPair object, building column names and ?/functions + for (int i = 0; i < columnFunctionPairs.size(); i++) { + ColumnFunctionPair pair = columnFunctionPairs.get(i); + + if (pair == null || StringUtils.isBlank(pair.getColumn())) { + throw new IllegalArgumentException("The column is blank, creating SQL error."); + } + + columns.append(pair.getColumn()); + values.append(StringUtils.isBlank(pair.getFunction()) ? "?" : pair.getFunction()); + + if (i != columnFunctionPairs.size() - 1) { + columns.append(", "); + values.append(", "); } } - sql.append(")"); - return sql.toString(); + + // Construct SQL + return "INSERT INTO " + getTableName() + " (" + + columns + ") VALUES (" + + values + ")"; } - + @Override - public String update(List columns, List where) { + public String update(List columnFunctionPairs, List where) { + if (columnFunctionPairs == null || columnFunctionPairs.isEmpty()) { + throw new IllegalArgumentException("The columnFunctionPairs list is null or empty."); + } + StringBuilder sql = new StringBuilder(); String method = "UPDATE "; sql.append(method); sql.append(getTableName()).append(" ").append("SET "); - - for (int i = 0; i < columns.size(); i++) { - sql.append(columns.get(i)).append(" = ").append("?"); - if (i != columns.size() - 1) { - sql.append(","); + + for (int i = 0; i < columnFunctionPairs.size(); i++) { + ColumnFunctionPair pair = columnFunctionPairs.get(i); + if (pair == null || StringUtils.isBlank(pair.getColumn())) { + throw new IllegalArgumentException("The column is blank, creating SQL error."); + } + sql.append(pair.getColumn()).append(" = ") + .append(StringUtils.isBlank(pair.getFunction()) ? "?" : pair.getFunction()); + if (i != columnFunctionPairs.size() - 1) { + sql.append(", "); } } - + if (CollectionUtils.isEmpty(where)) { return sql.toString(); } - + sql.append(" "); appendWhereClause(where, sql); - + return sql.toString(); } - + @Override public String delete(List params) { StringBuilder sql = new StringBuilder(); diff --git a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/ConfigInfoBetaMapper.java b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/ConfigInfoBetaMapper.java index 511ca2bf57b..e6cf972e2bc 100644 --- a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/ConfigInfoBetaMapper.java +++ b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/ConfigInfoBetaMapper.java @@ -42,7 +42,7 @@ public interface ConfigInfoBetaMapper extends Mapper { * @return The result of updating beta configuration information. */ default MapperResult updateConfigInfo4BetaCas(MapperContext context) { - final String sql = "UPDATE config_info_beta SET content = ?,md5 = ?,beta_ips = ?,src_ip = ?,src_user = ?,gmt_modified = ?,app_name = ? " + final String sql = "UPDATE config_info_beta SET content = ?,md5 = ?,beta_ips = ?,src_ip = ?,src_user = ?,gmt_modified = CURRENT_TIMESTAMP,app_name = ? " + "WHERE data_id = ? AND group_id = ? AND tenant_id = ? AND (md5 = ? OR md5 is null OR md5 = '')"; List paramList = new ArrayList<>(); @@ -52,7 +52,6 @@ default MapperResult updateConfigInfo4BetaCas(MapperContext context) { paramList.add(context.getUpdateParameter(FieldConstant.BETA_IPS)); paramList.add(context.getUpdateParameter(FieldConstant.SRC_IP)); paramList.add(context.getUpdateParameter(FieldConstant.SRC_USER)); - paramList.add(context.getUpdateParameter(FieldConstant.GMT_MODIFIED)); paramList.add(context.getUpdateParameter(FieldConstant.APP_NAME)); paramList.add(context.getWhereParameter(FieldConstant.DATA_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 ba7f7a521d2..914b124a5d7 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 @@ -492,7 +492,6 @@ default MapperResult updateConfigInfoAtomicCas(MapperContext context) { paramList.add(context.getUpdateParameter(FieldConstant.MD5)); paramList.add(context.getUpdateParameter(FieldConstant.SRC_IP)); paramList.add(context.getUpdateParameter(FieldConstant.SRC_USER)); - paramList.add(context.getUpdateParameter(FieldConstant.GMT_MODIFIED)); paramList.add(context.getUpdateParameter(FieldConstant.APP_NAME)); paramList.add(context.getUpdateParameter(FieldConstant.C_DESC)); paramList.add(context.getUpdateParameter(FieldConstant.C_USE)); @@ -504,7 +503,7 @@ default MapperResult updateConfigInfoAtomicCas(MapperContext context) { paramList.add(context.getWhereParameter(FieldConstant.GROUP_ID)); paramList.add(context.getWhereParameter(FieldConstant.TENANT_ID)); paramList.add(context.getWhereParameter(FieldConstant.MD5)); - String sql = "UPDATE config_info SET " + "content=?, md5 = ?, src_ip=?,src_user=?,gmt_modified=?," + String sql = "UPDATE config_info SET " + "content=?, md5 = ?, src_ip=?,src_user=?,gmt_modified=CURRENT_TIMESTAMP," + " app_name=?,c_desc=?,c_use=?,effect=?,type=?,c_schema=?,encrypted_data_key=? " + "WHERE data_id=? AND group_id=? AND tenant_id=? AND (md5=? OR md5 IS NULL OR md5='')"; return new MapperResult(sql, paramList); diff --git a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/ConfigInfoTagMapper.java b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/ConfigInfoTagMapper.java index 628170040d4..610f4100583 100644 --- a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/ConfigInfoTagMapper.java +++ b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/ConfigInfoTagMapper.java @@ -44,7 +44,6 @@ default MapperResult updateConfigInfo4TagCas(MapperContext context) { Object md5 = context.getUpdateParameter(FieldConstant.MD5); Object srcIp = context.getUpdateParameter(FieldConstant.SRC_IP); Object srcUser = context.getUpdateParameter(FieldConstant.SRC_USER); - Object gmtModified = context.getUpdateParameter(FieldConstant.GMT_MODIFIED); Object appName = context.getUpdateParameter(FieldConstant.APP_NAME); Object dataId = context.getWhereParameter(FieldConstant.DATA_ID); @@ -53,10 +52,10 @@ default MapperResult updateConfigInfo4TagCas(MapperContext context) { Object tagId = context.getWhereParameter(FieldConstant.TAG_ID); Object oldMd5 = context.getWhereParameter(FieldConstant.MD5); String sql = - "UPDATE config_info_tag SET content = ?, md5 = ?, src_ip = ?,src_user = ?,gmt_modified = ?,app_name = ? " + "UPDATE config_info_tag SET content = ?, md5 = ?, src_ip = ?,src_user = ?,gmt_modified = CURRENT_TIMESTAMP,app_name = ? " + "WHERE data_id = ? AND group_id = ? AND tenant_id = ? AND tag_id = ? AND (md5 = ? OR md5 IS NULL OR md5 = '')"; return new MapperResult(sql, - CollectionUtils.list(content, md5, srcIp, srcUser, gmtModified, appName, dataId, groupId, tenantId, + CollectionUtils.list(content, md5, srcIp, srcUser, appName, dataId, groupId, tenantId, tagId, oldMd5)); } diff --git a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/Mapper.java b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/Mapper.java index 20259194b1b..71d029ecbbd 100644 --- a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/Mapper.java +++ b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/Mapper.java @@ -16,6 +16,8 @@ package com.alibaba.nacos.plugin.datasource.mapper; +import com.alibaba.nacos.plugin.datasource.model.ColumnFunctionPair; + import java.util.List; /** @@ -25,7 +27,7 @@ **/ public interface Mapper { - + /** * The select method contains columns and where params. * @param columns The columns @@ -33,29 +35,32 @@ public interface Mapper { * @return The sql of select */ String select(List columns, List where); - + /** - * The insert method contains columns. - * @param columns The columns + * The insert method contains columnFunctionPairs. + * + * @param columnFunctionPairs the columnFunctionPairs. * @return The sql of insert */ - String insert(List columns); - + String insert(List columnFunctionPairs); + /** - * The update method contains columns and where params. - * @param columns The columns - * @param where The where params + * The update method contains columnFunctionPairs and where params. + * + * @param columnFunctionPairs The columnFunctionPairs + * @param where The where params * @return The sql of update */ - String update(List columns, List where); - + String update(List columnFunctionPairs, List where); + /** * The delete method contains. + * * @param params The params * @return The sql of delete */ String delete(List params); - + /** * The count method contains where params. * @@ -63,19 +68,19 @@ public interface Mapper { * @return The sql of count */ String count(List where); - + /** * Get the name of table. * @return The name of table. */ String getTableName(); - + /** * Get the datasource name. * @return The name of datasource. */ String getDataSource(); - + /** * Get config_info table primary keys name. * The old default value: Statement.RETURN_GENERATED_KEYS diff --git a/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/model/ColumnFunctionPair.java b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/model/ColumnFunctionPair.java new file mode 100644 index 00000000000..2d2d762d60b --- /dev/null +++ b/plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/model/ColumnFunctionPair.java @@ -0,0 +1,96 @@ +/* + * Copyright 1999-2018 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.model; + +import com.alibaba.nacos.plugin.datasource.enums.TrustedSqlFunctionEnum; + +import java.util.Objects; + +/** + * Column function pair. + * + * @author blake.qiu + */ +public class ColumnFunctionPair { + private final String column; + + private final TrustedSqlFunctionEnum functionEnum; + + public ColumnFunctionPair(String column) { + this.column = column; + this.functionEnum = null; + } + + public ColumnFunctionPair(String column, TrustedSqlFunctionEnum functionEnum) { + this.column = column; + this.functionEnum = functionEnum; + } + + /** + * New ColumnFunctionPair with column. + * + * @param columnName columnName + * @return ColumnFunctionPair + */ + public static ColumnFunctionPair withColumn(String columnName) { + return new ColumnFunctionPair(columnName); + } + + /** + * New ColumnFunctionPair with column and function. + * + * @param columnName columnName + * @param function function + * @return ColumnFunctionPair + */ + public static ColumnFunctionPair withColumnAndFunction(String columnName, TrustedSqlFunctionEnum function) { + return new ColumnFunctionPair(columnName, function); + } + + public String getColumn() { + return column; + } + + public String getFunction() { + return functionEnum == null ? null : functionEnum.getFunction(); + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + ColumnFunctionPair that = (ColumnFunctionPair) o; + return Objects.equals(getColumn(), that.getColumn()); + } + + @Override + public int hashCode() { + return Objects.hash(getColumn()); + } + + @Override + public String toString() { + return "ColumnFunctionPair{" + + "column='" + column + '\'' + + ", function='" + this.getFunction() + '\'' + + '}'; + } +} diff --git a/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/enums/TrustedSqlFunctionEnumTest.java b/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/enums/TrustedSqlFunctionEnumTest.java new file mode 100644 index 00000000000..a1f4a46db12 --- /dev/null +++ b/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/enums/TrustedSqlFunctionEnumTest.java @@ -0,0 +1,35 @@ +/* + * Copyright 1999-2018 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.enums; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Trusted sql function enum test. + * + * @author blake.qiu + */ +public class TrustedSqlFunctionEnumTest { + @Test + public void testGetFunction() { + TrustedSqlFunctionEnum currentTimestamp = TrustedSqlFunctionEnum.CURRENT_TIMESTAMP; + Assert.assertEquals("CURRENT_TIMESTAMP", currentTimestamp.getFunction()); + TrustedSqlFunctionEnum now = TrustedSqlFunctionEnum.NOW; + Assert.assertEquals("NOW()", now.getFunction()); + } +} diff --git a/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/impl/derby/ConfigInfoBetaMapperByDerbyTest.java b/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/impl/derby/ConfigInfoBetaMapperByDerbyTest.java index 2ed3be3c601..189c7d69174 100644 --- a/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/impl/derby/ConfigInfoBetaMapperByDerbyTest.java +++ b/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/impl/derby/ConfigInfoBetaMapperByDerbyTest.java @@ -72,7 +72,6 @@ public void testUpdateConfigInfo4BetaCas() { String newMD5 = "newMD5"; String srcIp = "1.1.1.1"; Object srcUser = "nacos"; - Object time = new Timestamp(System.currentTimeMillis()); Object appNameTmp = "newAppName"; Object desc = "description"; Object use = "use"; @@ -86,7 +85,6 @@ public void testUpdateConfigInfo4BetaCas() { context.putUpdateParameter(FieldConstant.BETA_IPS, betaIps); context.putUpdateParameter(FieldConstant.SRC_IP, srcIp); context.putUpdateParameter(FieldConstant.SRC_USER, srcUser); - context.putUpdateParameter(FieldConstant.GMT_MODIFIED, time); context.putUpdateParameter(FieldConstant.APP_NAME, appNameTmp); context.putUpdateParameter(FieldConstant.C_DESC, desc); context.putUpdateParameter(FieldConstant.C_USE, use); @@ -107,11 +105,10 @@ public void testUpdateConfigInfo4BetaCas() { String sql = mapperResult.getSql(); Assert.assertEquals(sql, "UPDATE config_info_beta SET content = ?,md5 = ?,beta_ips = ?,src_ip = ?,src_user = ?," - + "gmt_modified = ?,app_name = ? WHERE data_id = ? AND group_id = ? AND tenant_id = ? AND " + + "gmt_modified = CURRENT_TIMESTAMP,app_name = ? WHERE data_id = ? AND group_id = ? AND tenant_id = ? AND " + "(md5 = ? OR md5 is null OR md5 = '')"); Assert.assertArrayEquals(mapperResult.getParamList().toArray(), - new Object[] {newContent, newMD5, betaIps, srcIp, srcUser, time, appNameTmp, dataId, group, tenantId, - md5}); + new Object[]{newContent, newMD5, betaIps, srcIp, srcUser, appNameTmp, dataId, group, tenantId, md5}); } @Test diff --git a/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/impl/derby/ConfigInfoMapperByDerbyTest.java b/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/impl/derby/ConfigInfoMapperByDerbyTest.java index 29c382781f6..41b9485d718 100644 --- a/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/impl/derby/ConfigInfoMapperByDerbyTest.java +++ b/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/impl/derby/ConfigInfoMapperByDerbyTest.java @@ -328,7 +328,6 @@ public void testUpdateConfigInfoAtomicCas() { String newMD5 = "newMD5"; String srcIp = "1.1.1.1"; Object srcUser = "nacos"; - Object time = new Timestamp(System.currentTimeMillis()); Object appNameTmp = "newAppName"; Object desc = "description"; Object use = "use"; @@ -340,7 +339,6 @@ public void testUpdateConfigInfoAtomicCas() { context.putUpdateParameter(FieldConstant.MD5, newMD5); context.putUpdateParameter(FieldConstant.SRC_IP, srcIp); context.putUpdateParameter(FieldConstant.SRC_USER, srcUser); - context.putUpdateParameter(FieldConstant.GMT_MODIFIED, time); context.putUpdateParameter(FieldConstant.APP_NAME, appNameTmp); context.putUpdateParameter(FieldConstant.C_DESC, desc); context.putUpdateParameter(FieldConstant.C_USE, use); @@ -359,11 +357,11 @@ public void testUpdateConfigInfoAtomicCas() { MapperResult mapperResult = configInfoMapperByDerby.updateConfigInfoAtomicCas(context); Assert.assertEquals(mapperResult.getSql(), "UPDATE config_info SET " - + "content=?, md5 = ?, src_ip=?,src_user=?,gmt_modified=?, app_name=?,c_desc=?,c_use=?," + + "content=?, md5 = ?, src_ip=?,src_user=?,gmt_modified=CURRENT_TIMESTAMP, app_name=?,c_desc=?,c_use=?," + "effect=?,type=?,c_schema=?,encrypted_data_key=? " + "WHERE data_id=? AND group_id=? AND tenant_id=? AND (md5=? OR md5 IS NULL OR md5='')"); Assert.assertArrayEquals(mapperResult.getParamList().toArray(), - new Object[] {newContent, newMD5, srcIp, srcUser, time, appNameTmp, desc, use, effect, type, schema, + new Object[] {newContent, newMD5, srcIp, srcUser, appNameTmp, desc, use, effect, type, schema, encrypedDataKey, dataId, group, tenantId, md5}); } } diff --git a/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/impl/derby/ConfigInfoTagMapperByDerbyTest.java b/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/impl/derby/ConfigInfoTagMapperByDerbyTest.java index 0c552144a5a..21e227d76b9 100644 --- a/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/impl/derby/ConfigInfoTagMapperByDerbyTest.java +++ b/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/impl/derby/ConfigInfoTagMapperByDerbyTest.java @@ -73,7 +73,6 @@ public void testUpdateConfigInfo4TagCas() { String newMD5 = "newMD5"; String srcIp = "1.1.1.1"; Object srcUser = "nacos"; - Object time = new Timestamp(System.currentTimeMillis()); Object appNameTmp = "newAppName"; Object desc = "description"; Object use = "use"; @@ -85,7 +84,6 @@ public void testUpdateConfigInfo4TagCas() { context.putUpdateParameter(FieldConstant.MD5, newMD5); context.putUpdateParameter(FieldConstant.SRC_IP, srcIp); context.putUpdateParameter(FieldConstant.SRC_USER, srcUser); - context.putUpdateParameter(FieldConstant.GMT_MODIFIED, time); context.putUpdateParameter(FieldConstant.APP_NAME, appNameTmp); context.putUpdateParameter(FieldConstant.C_DESC, desc); context.putUpdateParameter(FieldConstant.C_USE, use); @@ -107,12 +105,11 @@ public void testUpdateConfigInfo4TagCas() { MapperResult mapperResult = configInfoTagMapperByDerby.updateConfigInfo4TagCas(context); Assert.assertEquals(mapperResult.getSql(), - "UPDATE config_info_tag SET content = ?, md5 = ?, src_ip = ?,src_user = ?,gmt_modified = ?," + "UPDATE config_info_tag SET content = ?, md5 = ?, src_ip = ?,src_user = ?,gmt_modified = CURRENT_TIMESTAMP," + "app_name = ? WHERE data_id = ? AND group_id = ? AND tenant_id = ? AND tag_id = ? AND " + "(md5 = ? OR md5 IS NULL OR md5 = '')"); Assert.assertArrayEquals(mapperResult.getParamList().toArray(), - new Object[] {newContent, newMD5, srcIp, srcUser, time, appNameTmp, dataId, group, tenantId, tagId, - md5}); + new Object[]{newContent, newMD5, srcIp, srcUser, appNameTmp, dataId, group, tenantId, tagId, md5}); } @Test diff --git a/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/impl/mysql/ConfigInfoBetaMapperByMySqlTest.java b/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/impl/mysql/ConfigInfoBetaMapperByMySqlTest.java index a06c21437c6..c92ba5f225f 100644 --- a/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/impl/mysql/ConfigInfoBetaMapperByMySqlTest.java +++ b/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/impl/mysql/ConfigInfoBetaMapperByMySqlTest.java @@ -72,7 +72,6 @@ public void testUpdateConfigInfo4BetaCas() { String newMD5 = "newMD5"; String srcIp = "1.1.1.1"; Object srcUser = "nacos"; - Object time = new Timestamp(System.currentTimeMillis()); Object appNameTmp = "newAppName"; Object desc = "description"; Object use = "use"; @@ -86,7 +85,6 @@ public void testUpdateConfigInfo4BetaCas() { context.putUpdateParameter(FieldConstant.BETA_IPS, betaIps); context.putUpdateParameter(FieldConstant.SRC_IP, srcIp); context.putUpdateParameter(FieldConstant.SRC_USER, srcUser); - context.putUpdateParameter(FieldConstant.GMT_MODIFIED, time); context.putUpdateParameter(FieldConstant.APP_NAME, appNameTmp); context.putUpdateParameter(FieldConstant.C_DESC, desc); context.putUpdateParameter(FieldConstant.C_USE, use); @@ -106,12 +104,11 @@ public void testUpdateConfigInfo4BetaCas() { MapperResult mapperResult = configInfoBetaMapperByMySql.updateConfigInfo4BetaCas(context); String sql = mapperResult.getSql(); - List paramList = mapperResult.getParamList(); Assert.assertEquals(sql, - "UPDATE config_info_beta SET content = ?,md5 = ?,beta_ips = ?,src_ip = ?,src_user = ?,gmt_modified = ?,app_name = ? " + "UPDATE config_info_beta SET content = ?,md5 = ?,beta_ips = ?,src_ip = ?,src_user = ?,gmt_modified = CURRENT_TIMESTAMP,app_name = ? " + "WHERE data_id = ? AND group_id = ? AND tenant_id = ? AND (md5 = ? OR md5 is null OR md5 = '')"); Assert.assertArrayEquals(mapperResult.getParamList().toArray(), - new Object[] {newContent, newMD5, betaIps, srcIp, srcUser, time, appNameTmp, dataId, group, tenantId, + new Object[]{newContent, newMD5, betaIps, srcIp, srcUser, appNameTmp, dataId, group, tenantId, md5}); } diff --git a/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/impl/mysql/ConfigInfoTagMapperByMySqlTest.java b/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/impl/mysql/ConfigInfoTagMapperByMySqlTest.java index 7632785b5d4..6f819ca34a3 100644 --- a/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/impl/mysql/ConfigInfoTagMapperByMySqlTest.java +++ b/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/impl/mysql/ConfigInfoTagMapperByMySqlTest.java @@ -73,7 +73,6 @@ public void testUpdateConfigInfo4TagCas() { String newMD5 = "newMD5"; String srcIp = "1.1.1.1"; Object srcUser = "nacos"; - Object time = new Timestamp(System.currentTimeMillis()); Object appNameTmp = "newAppName"; Object desc = "description"; Object use = "use"; @@ -85,7 +84,6 @@ public void testUpdateConfigInfo4TagCas() { context.putUpdateParameter(FieldConstant.MD5, newMD5); context.putUpdateParameter(FieldConstant.SRC_IP, srcIp); context.putUpdateParameter(FieldConstant.SRC_USER, srcUser); - context.putUpdateParameter(FieldConstant.GMT_MODIFIED, time); context.putUpdateParameter(FieldConstant.APP_NAME, appNameTmp); context.putUpdateParameter(FieldConstant.C_DESC, desc); context.putUpdateParameter(FieldConstant.C_USE, use); @@ -107,12 +105,11 @@ public void testUpdateConfigInfo4TagCas() { MapperResult mapperResult = configInfoTagMapperByMySql.updateConfigInfo4TagCas(context); Assert.assertEquals(mapperResult.getSql(), - "UPDATE config_info_tag SET content = ?, md5 = ?, src_ip = ?,src_user = ?,gmt_modified = ?," + "UPDATE config_info_tag SET content = ?, md5 = ?, src_ip = ?,src_user = ?,gmt_modified = CURRENT_TIMESTAMP," + "app_name = ? WHERE data_id = ? AND group_id = ? AND tenant_id = ? AND tag_id = ? AND " + "(md5 = ? OR md5 IS NULL OR md5 = '')"); Assert.assertArrayEquals(mapperResult.getParamList().toArray(), - new Object[] {newContent, newMD5, srcIp, srcUser, time, appNameTmp, dataId, group, tenantId, tagId, - md5}); + new Object[]{newContent, newMD5, srcIp, srcUser, appNameTmp, dataId, group, tenantId, tagId, md5}); } @Test diff --git a/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/mapper/AbstractMapperTest.java b/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/mapper/AbstractMapperTest.java index 3f88eb05769..10832f6461e 100644 --- a/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/mapper/AbstractMapperTest.java +++ b/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/mapper/AbstractMapperTest.java @@ -16,7 +16,9 @@ package com.alibaba.nacos.plugin.datasource.mapper; +import com.alibaba.nacos.plugin.datasource.enums.TrustedSqlFunctionEnum; import com.alibaba.nacos.plugin.datasource.impl.mysql.TenantInfoMapperByMySql; +import com.alibaba.nacos.plugin.datasource.model.ColumnFunctionPair; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -37,25 +39,48 @@ public void testSelect() { String sql = abstractMapper.select(Arrays.asList("id", "name"), Arrays.asList("id")); Assert.assertEquals(sql, "SELECT id,name FROM tenant_info WHERE id = ?"); } - + @Test public void testInsert() { - String sql = abstractMapper.insert(Arrays.asList("id", "name")); - Assert.assertEquals(sql, "INSERT INTO tenant_info(id, name) VALUES(?,?)"); + String sql = abstractMapper.insert(Arrays.asList( + ColumnFunctionPair.withColumn("id"), + ColumnFunctionPair.withColumn("name"))); + Assert.assertEquals(sql, "INSERT INTO tenant_info (id, name) VALUES (?, ?)"); } - + + @Test + public void testInsertWithFunction() { + String sql = abstractMapper.insert(Arrays.asList( + ColumnFunctionPair.withColumn("id"), + ColumnFunctionPair.withColumn("name"), + ColumnFunctionPair.withColumnAndFunction("gmt_modified", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP))); + Assert.assertEquals(sql, "INSERT INTO tenant_info (id, name, gmt_modified) VALUES (?, ?, CURRENT_TIMESTAMP)"); + } + @Test public void testUpdate() { - String sql = abstractMapper.update(Arrays.asList("id", "name"), Arrays.asList("id")); - Assert.assertEquals(sql, "UPDATE tenant_info SET id = ?,name = ? WHERE id = ?"); + String sql = abstractMapper.update(Arrays.asList( + ColumnFunctionPair.withColumn("id"), + ColumnFunctionPair.withColumn("name")), Arrays.asList("id")); + Assert.assertEquals(sql, "UPDATE tenant_info SET id = ?, name = ? WHERE id = ?"); } - + + @Test + public void testUpdateWithFunction() { + String sql = abstractMapper.update(Arrays.asList( + ColumnFunctionPair.withColumn("id"), + ColumnFunctionPair.withColumn("name"), + ColumnFunctionPair.withColumnAndFunction("gmt_modified", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP)), + Arrays.asList("id")); + Assert.assertEquals(sql, "UPDATE tenant_info SET id = ?, name = ?, gmt_modified = CURRENT_TIMESTAMP WHERE id = ?"); + } + @Test public void testDelete() { String sql = abstractMapper.delete(Arrays.asList("id")); Assert.assertEquals(sql, "DELETE FROM tenant_info WHERE id = ? "); } - + @Test public void testCount() { String sql = abstractMapper.count(Arrays.asList("id")); diff --git a/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/model/ColumnFunctionPairTest.java b/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/model/ColumnFunctionPairTest.java new file mode 100644 index 00000000000..f0181a559b9 --- /dev/null +++ b/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/model/ColumnFunctionPairTest.java @@ -0,0 +1,54 @@ +/* + * Copyright 1999-2018 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.model; + +import com.alibaba.nacos.plugin.datasource.enums.TrustedSqlFunctionEnum; +import org.junit.Assert; +import org.junit.Test; + +/** + * Column function pair Test. + * + * @author blake.qiu + */ +public class ColumnFunctionPairTest { + @Test + public void testBuildWithColumn() { + ColumnFunctionPair columnFunctionPair = ColumnFunctionPair.withColumn("column"); + Assert.assertNotNull(columnFunctionPair); + Assert.assertEquals("column", columnFunctionPair.getColumn()); + Assert.assertNull(columnFunctionPair.getFunction()); + } + + @Test + public void testBuildWithColumnAndFunction() { + ColumnFunctionPair columnFunctionPair = ColumnFunctionPair.withColumnAndFunction("column", null); + Assert.assertNotNull(columnFunctionPair); + Assert.assertEquals("column", columnFunctionPair.getColumn()); + Assert.assertNull(columnFunctionPair.getFunction()); + + ColumnFunctionPair columnFunctionPair2 = ColumnFunctionPair.withColumnAndFunction("column", TrustedSqlFunctionEnum.CURRENT_TIMESTAMP); + Assert.assertNotNull(columnFunctionPair2); + Assert.assertEquals("column", columnFunctionPair.getColumn()); + Assert.assertEquals(TrustedSqlFunctionEnum.CURRENT_TIMESTAMP.getFunction(), columnFunctionPair2.getFunction()); + + ColumnFunctionPair columnFunctionPair3 = ColumnFunctionPair.withColumnAndFunction("column", TrustedSqlFunctionEnum.NOW); + Assert.assertNotNull(columnFunctionPair3); + Assert.assertEquals("column", columnFunctionPair.getColumn()); + Assert.assertEquals(TrustedSqlFunctionEnum.NOW.getFunction(), columnFunctionPair3.getFunction()); + } +} diff --git a/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/proxy/MapperProxyTest.java b/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/proxy/MapperProxyTest.java index c848ebc530f..18b99fba383 100644 --- a/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/proxy/MapperProxyTest.java +++ b/plugin/datasource/src/test/java/com/alibaba/nacos/plugin/datasource/proxy/MapperProxyTest.java @@ -17,6 +17,7 @@ package com.alibaba.nacos.plugin.datasource.proxy; import com.alibaba.nacos.plugin.datasource.mapper.Mapper; +import com.alibaba.nacos.plugin.datasource.model.ColumnFunctionPair; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -40,22 +41,22 @@ public void testCreateProxy() { public String select(List columns, List where) { return "select-test"; } - + @Override - public String insert(List columns) { + public String insert(List columnFunctionPairs) { return "insert-test"; } - + @Override - public String update(List columns, List where) { + public String update(List columnFunctionPairs, List where) { return "update-test"; } - + @Override public String delete(List params) { return "delete-test"; } - + @Override public String count(List where) { return "count-test";