Skip to content

Commit c2e5ee6

Browse files
authoredAug 19, 2024
[ISSUE alibaba#12483] Configuration list adds configuration format (alibaba#12491)
* ✨Configuration type display and advanced search by type added to configuration list * ✨Generate static files * ✨Add license
1 parent 9efee81 commit c2e5ee6

File tree

20 files changed

+1211
-846
lines changed

20 files changed

+1211
-846
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 1999-2018 Alibaba Group Holding Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.alibaba.nacos.common.constant;
18+
19+
/**
20+
* Symbols.
21+
*
22+
* @author haiqi.wang
23+
* @date 2024/08/13
24+
*/
25+
public final class Symbols {
26+
27+
/**
28+
* Comma.
29+
*/
30+
public static final String COMMA = ",";
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 1999-2018 Alibaba Group Holding Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.alibaba.nacos.config.server.constant;
18+
19+
/**
20+
* Parameters Field.
21+
*
22+
* @author haiqi.wang
23+
* @date 2024/08/13
24+
*/
25+
public final class ParametersField {
26+
27+
/**
28+
* Types.
29+
*/
30+
public static final String TYPES = "types";
31+
}

‎config/src/main/java/com/alibaba/nacos/config/server/controller/ConfigController.java

+5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.alibaba.nacos.common.utils.Pair;
2727
import com.alibaba.nacos.common.utils.StringUtils;
2828
import com.alibaba.nacos.config.server.constant.Constants;
29+
import com.alibaba.nacos.config.server.constant.ParametersField;
2930
import com.alibaba.nacos.config.server.controller.parameters.SameNamespaceCloneConfigBean;
3031
import com.alibaba.nacos.config.server.model.ConfigAdvanceInfo;
3132
import com.alibaba.nacos.config.server.model.ConfigAllInfo;
@@ -418,6 +419,7 @@ public Page<ConfigInfo> fuzzySearchConfig(@RequestParam("dataId") String dataId,
418419
@RequestParam("group") String group, @RequestParam(value = "appName", required = false) String appName,
419420
@RequestParam(value = "tenant", required = false, defaultValue = StringUtils.EMPTY) String tenant,
420421
@RequestParam(value = "config_tags", required = false) String configTags,
422+
@RequestParam(value = "types", required = false) String types,
421423
@RequestParam("pageNo") int pageNo, @RequestParam("pageSize") int pageSize) {
422424
MetricsMonitor.getFuzzySearchMonitor().incrementAndGet();
423425
Map<String, Object> configAdvanceInfo = new HashMap<>(50);
@@ -427,6 +429,9 @@ public Page<ConfigInfo> fuzzySearchConfig(@RequestParam("dataId") String dataId,
427429
if (StringUtils.isNotBlank(configTags)) {
428430
configAdvanceInfo.put("config_tags", configTags);
429431
}
432+
if (StringUtils.isNotBlank(types)) {
433+
configAdvanceInfo.put(ParametersField.TYPES, types);
434+
}
430435
try {
431436
return configInfoPersistService.findConfigInfoLike4Page(pageNo, pageSize, dataId, group, tenant,
432437
configAdvanceInfo);

‎config/src/main/java/com/alibaba/nacos/config/server/service/repository/embedded/EmbeddedConfigInfoPersistServiceImpl.java

+7
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@
1818

1919
import com.alibaba.nacos.api.exception.NacosException;
2020
import com.alibaba.nacos.api.exception.runtime.NacosRuntimeException;
21+
import com.alibaba.nacos.common.constant.Symbols;
2122
import com.alibaba.nacos.common.notify.NotifyCenter;
2223
import com.alibaba.nacos.common.utils.MD5Utils;
2324
import com.alibaba.nacos.common.utils.Pair;
2425
import com.alibaba.nacos.common.utils.StringUtils;
2526
import com.alibaba.nacos.config.server.constant.Constants;
27+
import com.alibaba.nacos.config.server.constant.ParametersField;
2628
import com.alibaba.nacos.config.server.enums.FileTypeEnum;
2729
import com.alibaba.nacos.config.server.exception.NacosConfigException;
2830
import com.alibaba.nacos.config.server.model.ConfigAdvanceInfo;
@@ -800,6 +802,7 @@ public Page<ConfigInfo> findConfigInfoLike4Page(final int pageNo, final int page
800802
String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant;
801803
final String appName = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("appName");
802804
final String content = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("content");
805+
final String types = Optional.ofNullable(configAdvanceInfo).map(e -> (String) e.get(ParametersField.TYPES)).orElse(null);
803806
final String configTags = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("config_tags");
804807
MapperResult sqlCountRows;
805808
MapperResult sqlFetchRows;
@@ -819,6 +822,10 @@ public Page<ConfigInfo> findConfigInfoLike4Page(final int pageNo, final int page
819822
if (!StringUtils.isBlank(content)) {
820823
context.putWhereParameter(FieldConstant.CONTENT, generateLikeArgument(content));
821824
}
825+
if (StringUtils.isNotBlank(types)) {
826+
String[] typesArr = types.split(Symbols.COMMA);
827+
context.putWhereParameter(FieldConstant.TYPE, typesArr);
828+
}
822829

823830
if (StringUtils.isNotBlank(configTags)) {
824831
String[] tagArr = configTags.split(",");

‎config/src/main/java/com/alibaba/nacos/config/server/service/repository/extrnal/ExternalConfigInfoPersistServiceImpl.java

+8
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@
1717
package com.alibaba.nacos.config.server.service.repository.extrnal;
1818

1919
import com.alibaba.nacos.api.exception.NacosException;
20+
import com.alibaba.nacos.common.constant.Symbols;
2021
import com.alibaba.nacos.common.utils.MD5Utils;
2122
import com.alibaba.nacos.common.utils.Pair;
2223
import com.alibaba.nacos.common.utils.StringUtils;
2324
import com.alibaba.nacos.config.server.constant.Constants;
25+
import com.alibaba.nacos.config.server.constant.ParametersField;
2426
import com.alibaba.nacos.config.server.enums.FileTypeEnum;
2527
import com.alibaba.nacos.config.server.model.ConfigAdvanceInfo;
2628
import com.alibaba.nacos.config.server.model.ConfigAllInfo;
@@ -77,6 +79,7 @@
7779
import java.util.List;
7880
import java.util.Map;
7981
import java.util.Objects;
82+
import java.util.Optional;
8083

8184
import static com.alibaba.nacos.config.server.service.repository.ConfigRowMapperInjector.CONFIG_ADVANCE_INFO_ROW_MAPPER;
8285
import static com.alibaba.nacos.config.server.service.repository.ConfigRowMapperInjector.CONFIG_ALL_INFO_ROW_MAPPER;
@@ -836,6 +839,7 @@ public Page<ConfigInfo> findConfigInfoLike4Page(final int pageNo, final int page
836839
String tenantTmp = StringUtils.isBlank(tenant) ? StringUtils.EMPTY : tenant;
837840
final String appName = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("appName");
838841
final String content = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("content");
842+
final String types = Optional.ofNullable(configAdvanceInfo).map(e -> (String) e.get(ParametersField.TYPES)).orElse(null);
839843
final String configTags = configAdvanceInfo == null ? null : (String) configAdvanceInfo.get("config_tags");
840844
PaginationHelper<ConfigInfo> helper = createPaginationHelper();
841845
MapperResult sqlCountRows;
@@ -856,6 +860,10 @@ public Page<ConfigInfo> findConfigInfoLike4Page(final int pageNo, final int page
856860
if (!StringUtils.isBlank(content)) {
857861
context.putWhereParameter(FieldConstant.CONTENT, generateLikeArgument(content));
858862
}
863+
if (StringUtils.isNotBlank(types)) {
864+
String[] typesArr = types.split(Symbols.COMMA);
865+
context.putWhereParameter(FieldConstant.TYPE, typesArr);
866+
}
859867

860868
if (StringUtils.isNotBlank(configTags)) {
861869
String[] tagArr = configTags.split(",");

‎console-ui/README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ yarn --registry=https://registry.npm.taobao.org
1313

1414
NodeJS提供了一些安装程序,都可以在[nodejs.org](https://nodejs.org/download/release/) 这里下载并安装。mac系统选择.pkg结尾的文件下载安装。
1515
注意node版本号过高可能导致 `npm install` 时失败,建议版本:
16-
- node:v8.16.0
17-
- npm:6.4.1
16+
- node:v14.20.1
1817

1918
## 安装依赖
2019
```sh

‎console-ui/package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
"@alifd/theme-design-pro": "0.x",
7070
"@iarna/toml": "^3.0.0",
7171
"axios": "^0.21.1",
72+
"core-js": "^3.6.4",
7273
"js-yaml": "^4.1.0",
7374
"moment": "^2.23.0",
7475
"prop-types": "^15.6.2",
@@ -81,7 +82,6 @@
8182
"react-router-dom": "^5.1.2",
8283
"react-router-redux": "^4.0.8",
8384
"redux": "^4.0.5",
84-
"redux-thunk": "^2.3.0",
85-
"core-js": "^3.6.4"
85+
"redux-thunk": "^2.3.0"
8686
}
8787
}

‎console-ui/src/locales/en-US.js

+2
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,8 @@ const I18N_CONF = {
312312
app1: 'Enter App Name\n',
313313
tags: 'Tags',
314314
pleaseEnterTag: 'Enter Tag',
315+
types: 'Type',
316+
typeSelectedAlertContent: 'Please select the configuration type',
315317
configDetailLabel: 'DetailSearch',
316318
configDetailH: 'search config detail',
317319
application: 'Application',

‎console-ui/src/locales/zh-CN.js

+2
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ const I18N_CONF = {
309309
app1: '请输入应用名',
310310
tags: '标签',
311311
pleaseEnterTag: '请输入标签',
312+
types: '格式',
313+
typeSelectedAlertContent: '请选择配置类型',
312314
configDetailLabel: '配置项搜索',
313315
configDetailH: '搜索具体配置项',
314316
application: '归属应用',

‎console-ui/src/pages/ConfigurationManagement/ConfigurationManagement/ConfigurationManagement.js

+803-665
Large diffs are not rendered by default.

‎console/README.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
```shell
2+
#开发工具本地启动,增加以下 JVM 参数
3+
-Dnacos.standalone=true
4+
```

‎console/src/main/resources/static/index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<link rel="stylesheet" type="text/css" href="console-ui/public/css/icon.css">
3636
<link rel="stylesheet" type="text/css" href="console-ui/public/css/font-awesome.css">
3737
<!-- 第三方css结束 -->
38-
<link href="./css/main.css?0c44b904d360f744c82e" rel="stylesheet"></head>
38+
<link href="./css/main.css?a7f2f10835d488271566" rel="stylesheet"></head>
3939

4040
<body>
4141
<div id="root" style="overflow:hidden"></div>
@@ -56,6 +56,6 @@
5656
<script src="console-ui/public/js/merge.js"></script>
5757
<script src="console-ui/public/js/loader.js"></script>
5858
<!-- 第三方js结束 -->
59-
<script type="text/javascript" src="./js/main.js?0c44b904d360f744c82e"></script></body>
59+
<script type="text/javascript" src="./js/main.js?a7f2f10835d488271566"></script></body>
6060

6161
</html>

‎console/src/main/resources/static/js/main.js

+16-16
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/impl/derby/ConfigInfoMapperByDerby.java

+22-24
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616

1717
package com.alibaba.nacos.plugin.datasource.impl.derby;
1818

19+
import com.alibaba.nacos.common.utils.ArrayUtils;
1920
import com.alibaba.nacos.common.utils.CollectionUtils;
2021
import com.alibaba.nacos.common.utils.NamespaceUtil;
2122
import com.alibaba.nacos.common.utils.StringUtils;
2223
import com.alibaba.nacos.plugin.datasource.constants.ContextConstant;
2324
import com.alibaba.nacos.plugin.datasource.constants.DataSourceConstant;
2425
import com.alibaba.nacos.plugin.datasource.constants.FieldConstant;
2526
import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoMapper;
27+
import com.alibaba.nacos.plugin.datasource.mapper.ext.WhereBuilder;
2628
import com.alibaba.nacos.plugin.datasource.model.MapperContext;
2729
import com.alibaba.nacos.plugin.datasource.model.MapperResult;
2830

@@ -38,12 +40,12 @@
3840
**/
3941

4042
public class ConfigInfoMapperByDerby extends AbstractMapperByDerby implements ConfigInfoMapper {
41-
43+
4244
@Override
4345
public MapperResult findConfigInfoByAppFetchRows(MapperContext context) {
4446
final String appName = (String) context.getWhereParameter(FieldConstant.APP_NAME);
4547
final String tenantId = (String) context.getWhereParameter(FieldConstant.TENANT_ID);
46-
48+
4749
String sql =
4850
"SELECT ID,data_id,group_id,tenant_id,app_name,content FROM config_info WHERE tenant_id LIKE ? AND "
4951
+ "app_name = ?" + " OFFSET " + context.getStartRow() + " ROWS FETCH NEXT "
@@ -233,33 +235,29 @@ public MapperResult findConfigInfoLike4PageFetchRows(MapperContext context) {
233235
final String group = (String) context.getWhereParameter(FieldConstant.GROUP_ID);
234236
final String appName = (String) context.getWhereParameter(FieldConstant.APP_NAME);
235237
final String content = (String) context.getWhereParameter(FieldConstant.CONTENT);
238+
final String[] types = (String[]) context.getWhereParameter(FieldConstant.TYPE);
236239

237-
List<Object> paramList = new ArrayList<>();
238-
239-
final String sqlFetchRows = "SELECT id,data_id,group_id,tenant_id,app_name,content,encrypted_data_key FROM config_info";
240-
StringBuilder where = new StringBuilder(" WHERE ");
241-
where.append(" tenant_id LIKE ? ");
242-
paramList.add(tenantId);
243-
if (!StringUtils.isBlank(dataId)) {
244-
where.append(" AND data_id LIKE ? ");
245-
paramList.add(dataId);
240+
WhereBuilder where = new WhereBuilder(
241+
"SELECT id,data_id,group_id,tenant_id,app_name,content,encrypted_data_key,type FROM config_info");
242+
where.like("tenant_id", tenantId);
243+
if (StringUtils.isNotBlank(dataId)) {
244+
where.and().like("data_id", dataId);
246245
}
247-
if (!StringUtils.isBlank(group)) {
248-
where.append(" AND group_id LIKE ? ");
249-
paramList.add(group);
246+
if (StringUtils.isNotBlank(group)) {
247+
where.and().like("group_id", group);
250248
}
251-
if (!StringUtils.isBlank(appName)) {
252-
where.append(" AND app_name = ? ");
253-
paramList.add(appName);
249+
if (StringUtils.isNotBlank(appName)) {
250+
where.and().eq("app_name", appName);
254251
}
255-
if (!StringUtils.isBlank(content)) {
256-
where.append(" AND content LIKE ? ");
257-
paramList.add(content);
252+
if (StringUtils.isNotBlank(content)) {
253+
where.and().like("content", content);
258254
}
259-
String sql =
260-
sqlFetchRows + where + " OFFSET " + context.getStartRow() + " ROWS FETCH NEXT " + context.getPageSize()
261-
+ " ROWS ONLY";
262-
return new MapperResult(sql, paramList);
255+
if (!ArrayUtils.isEmpty(types)) {
256+
where.and().in("type", types);
257+
}
258+
259+
where.offset(context.getStartRow(), context.getPageSize());
260+
return where.build();
263261
}
264262

265263
@Override

‎plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/impl/derby/ConfigInfoTagsRelationMapperByDerby.java

+25-33
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616

1717
package com.alibaba.nacos.plugin.datasource.impl.derby;
1818

19+
import com.alibaba.nacos.common.utils.ArrayUtils;
1920
import com.alibaba.nacos.common.utils.StringUtils;
2021
import com.alibaba.nacos.plugin.datasource.constants.DataSourceConstant;
2122
import com.alibaba.nacos.plugin.datasource.constants.FieldConstant;
2223
import com.alibaba.nacos.plugin.datasource.mapper.ConfigTagsRelationMapper;
24+
import com.alibaba.nacos.plugin.datasource.mapper.ext.WhereBuilder;
2325
import com.alibaba.nacos.plugin.datasource.model.MapperContext;
2426
import com.alibaba.nacos.plugin.datasource.model.MapperResult;
2527

@@ -33,7 +35,7 @@
3335
**/
3436

3537
public class ConfigInfoTagsRelationMapperByDerby extends AbstractMapperByDerby implements ConfigTagsRelationMapper {
36-
38+
3739
@Override
3840
public MapperResult findConfigInfo4PageFetchRows(MapperContext context) {
3941
final String appName = (String) context.getWhereParameter(FieldConstant.APP_NAME);
@@ -42,7 +44,7 @@ public MapperResult findConfigInfo4PageFetchRows(MapperContext context) {
4244
final String content = (String) context.getWhereParameter(FieldConstant.CONTENT);
4345
final String tenantId = (String) context.getWhereParameter(FieldConstant.TENANT_ID);
4446
final String[] tagArr = (String[]) context.getWhereParameter(FieldConstant.TAG_ARR);
45-
47+
4648
List<Object> paramList = new ArrayList<>();
4749
StringBuilder where = new StringBuilder(" WHERE ");
4850
final String baseSql =
@@ -90,45 +92,35 @@ public MapperResult findConfigInfoLike4PageFetchRows(MapperContext context) {
9092
final String content = (String) context.getWhereParameter(FieldConstant.CONTENT);
9193
final String tenantId = (String) context.getWhereParameter(FieldConstant.TENANT_ID);
9294
final String[] tagArr = (String[]) context.getWhereParameter(FieldConstant.TAG_ARR);
95+
final String[] types = (String[]) context.getWhereParameter(FieldConstant.TYPE);
9396

94-
List<Object> paramList = new ArrayList<>();
95-
StringBuilder where = new StringBuilder(" WHERE ");
96-
final String baseSql =
97-
"SELECT a.ID,a.data_id,a.group_id,a.tenant_id,a.app_name,a.content FROM config_info a LEFT JOIN "
98-
+ "config_tags_relation b ON a.id=b.id ";
97+
WhereBuilder where = new WhereBuilder(
98+
"SELECT a.ID,a.data_id,a.group_id,a.tenant_id,a.app_name,a.content,a.type FROM config_info a LEFT JOIN "
99+
+ "config_tags_relation b ON a.id=b.id");
99100

100-
where.append(" a.tenant_id LIKE ? ");
101-
paramList.add(tenantId);
101+
where.like("a.tenant_id", tenantId);
102102

103-
if (!StringUtils.isBlank(dataId)) {
104-
where.append(" AND a.data_id LIKE ? ");
105-
paramList.add(dataId);
103+
if (StringUtils.isNotBlank(dataId)) {
104+
where.and().like("a.data_id", dataId);
106105
}
107-
if (!StringUtils.isBlank(group)) {
108-
where.append(" AND a.group_id LIKE ? ");
109-
paramList.add(group);
106+
if (StringUtils.isNotBlank(group)) {
107+
where.and().like("a.group_id", group);
110108
}
111-
if (!StringUtils.isBlank(appName)) {
112-
where.append(" AND a.app_name = ? ");
113-
paramList.add(appName);
109+
if (StringUtils.isNotBlank(appName)) {
110+
where.and().eq("a.app_name", appName);
114111
}
115-
if (!StringUtils.isBlank(content)) {
116-
where.append(" AND a.content LIKE ? ");
117-
paramList.add(content);
112+
if (StringUtils.isNotBlank(content)) {
113+
where.and().like("a.content", content);
118114
}
119-
120-
where.append(" AND b.tag_name IN (");
121-
for (int i = 0; i < tagArr.length; i++) {
122-
if (i != 0) {
123-
where.append(", ");
124-
}
125-
where.append('?');
126-
paramList.add(tagArr[i]);
115+
if (!ArrayUtils.isEmpty(tagArr)) {
116+
where.and().in("b.tag_name", tagArr);
127117
}
128-
where.append(") ");
129-
String sql = baseSql + where + " OFFSET " + context.getStartRow() + " ROWS FETCH NEXT " + context.getPageSize()
130-
+ " ROWS ONLY";
131-
return new MapperResult(sql, paramList);
118+
if (!ArrayUtils.isEmpty(types)) {
119+
where.and().in("a.type", types);
120+
}
121+
122+
where.offset(context.getStartRow(), context.getPageSize());
123+
return where.build();
132124
}
133125

134126
@Override

‎plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/impl/mysql/ConfigInfoMapperByMySql.java

+28-29
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616

1717
package com.alibaba.nacos.plugin.datasource.impl.mysql;
1818

19+
import com.alibaba.nacos.common.utils.ArrayUtils;
1920
import com.alibaba.nacos.common.utils.CollectionUtils;
2021
import com.alibaba.nacos.common.utils.NamespaceUtil;
2122
import com.alibaba.nacos.common.utils.StringUtils;
2223
import com.alibaba.nacos.plugin.datasource.constants.ContextConstant;
2324
import com.alibaba.nacos.plugin.datasource.constants.DataSourceConstant;
2425
import com.alibaba.nacos.plugin.datasource.constants.FieldConstant;
2526
import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoMapper;
27+
import com.alibaba.nacos.plugin.datasource.mapper.ext.WhereBuilder;
2628
import com.alibaba.nacos.plugin.datasource.model.MapperContext;
2729
import com.alibaba.nacos.plugin.datasource.model.MapperResult;
2830

@@ -38,15 +40,15 @@
3840
**/
3941

4042
public class ConfigInfoMapperByMySql extends AbstractMapperByMysql implements ConfigInfoMapper {
41-
43+
4244
private static final String DATA_ID = "dataId";
43-
45+
4446
private static final String GROUP = "group";
45-
47+
4648
private static final String APP_NAME = "appName";
47-
49+
4850
private static final String CONTENT = "content";
49-
51+
5052
private static final String TENANT = "tenant";
5153

5254
@Override
@@ -83,9 +85,10 @@ public MapperResult findAllConfigKey(MapperContext context) {
8385

8486
@Override
8587
public MapperResult findAllConfigInfoBaseFetchRows(MapperContext context) {
86-
String sql = "SELECT t.id,data_id,group_id,content,md5"
87-
+ " FROM ( SELECT id FROM config_info ORDER BY id LIMIT " + context.getStartRow() + ","
88-
+ context.getPageSize() + " )" + " g, config_info t WHERE g.id = t.id ";
88+
String sql =
89+
"SELECT t.id,data_id,group_id,content,md5" + " FROM ( SELECT id FROM config_info ORDER BY id LIMIT "
90+
+ context.getStartRow() + "," + context.getPageSize() + " )"
91+
+ " g, config_info t WHERE g.id = t.id ";
8992
return new MapperResult(sql, Collections.emptyList());
9093
}
9194

@@ -228,33 +231,29 @@ public MapperResult findConfigInfoLike4PageFetchRows(MapperContext context) {
228231
final String group = (String) context.getWhereParameter(FieldConstant.GROUP_ID);
229232
final String appName = (String) context.getWhereParameter(FieldConstant.APP_NAME);
230233
final String content = (String) context.getWhereParameter(FieldConstant.CONTENT);
234+
final String[] types = (String[]) context.getWhereParameter(FieldConstant.TYPE);
231235

232-
List<Object> paramList = new ArrayList<>();
236+
WhereBuilder where = new WhereBuilder(
237+
"SELECT id,data_id,group_id,tenant_id,app_name,content,encrypted_data_key,type FROM config_info");
238+
where.like("tenant_id", tenant);
233239

234-
final String sqlFetchRows = "SELECT id,data_id,group_id,tenant_id,app_name,content,encrypted_data_key FROM config_info";
235-
StringBuilder where = new StringBuilder(" WHERE ");
236-
where.append(" tenant_id LIKE ? ");
237-
paramList.add(tenant);
238-
239-
if (!StringUtils.isBlank(dataId)) {
240-
where.append(" AND data_id LIKE ? ");
241-
paramList.add(dataId);
242-
240+
if (StringUtils.isNotBlank(dataId)) {
241+
where.and().like("data_id", dataId);
243242
}
244-
if (!StringUtils.isBlank(group)) {
245-
where.append(" AND group_id LIKE ? ");
246-
paramList.add(group);
243+
if (StringUtils.isNotBlank(group)) {
244+
where.and().like("group_id", group);
247245
}
248-
if (!StringUtils.isBlank(appName)) {
249-
where.append(" AND app_name = ? ");
250-
paramList.add(appName);
246+
if (StringUtils.isNotBlank(appName)) {
247+
where.and().eq("app_name", appName);
251248
}
252-
if (!StringUtils.isBlank(content)) {
253-
where.append(" AND content LIKE ? ");
254-
paramList.add(content);
249+
if (StringUtils.isNotBlank(content)) {
250+
where.and().like("content", content);
255251
}
256-
return new MapperResult(sqlFetchRows + where + " LIMIT " + context.getStartRow() + "," + context.getPageSize(),
257-
paramList);
252+
if (!ArrayUtils.isEmpty(types)) {
253+
where.in("type", types);
254+
}
255+
where.limit(context.getStartRow(), context.getPageSize());
256+
return where.build();
258257
}
259258

260259
@Override

‎plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/impl/mysql/ConfigTagsRelationMapperByMySql.java

+26-31
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616

1717
package com.alibaba.nacos.plugin.datasource.impl.mysql;
1818

19+
import com.alibaba.nacos.common.utils.ArrayUtils;
1920
import com.alibaba.nacos.common.utils.StringUtils;
2021
import com.alibaba.nacos.plugin.datasource.constants.DataSourceConstant;
2122
import com.alibaba.nacos.plugin.datasource.constants.FieldConstant;
2223
import com.alibaba.nacos.plugin.datasource.mapper.ConfigTagsRelationMapper;
24+
import com.alibaba.nacos.plugin.datasource.mapper.ext.WhereBuilder;
2325
import com.alibaba.nacos.plugin.datasource.model.MapperContext;
2426
import com.alibaba.nacos.plugin.datasource.model.MapperResult;
2527

@@ -33,7 +35,7 @@
3335
**/
3436

3537
public class ConfigTagsRelationMapperByMySql extends AbstractMapperByMysql implements ConfigTagsRelationMapper {
36-
38+
3739
@Override
3840
public MapperResult findConfigInfo4PageFetchRows(MapperContext context) {
3941
final String tenant = (String) context.getWhereParameter(FieldConstant.TENANT_ID);
@@ -42,7 +44,7 @@ public MapperResult findConfigInfo4PageFetchRows(MapperContext context) {
4244
final String appName = (String) context.getWhereParameter(FieldConstant.APP_NAME);
4345
final String content = (String) context.getWhereParameter(FieldConstant.CONTENT);
4446
final String[] tagArr = (String[]) context.getWhereParameter(FieldConstant.TAG_ARR);
45-
47+
4648
List<Object> paramList = new ArrayList<>();
4749
StringBuilder where = new StringBuilder(" WHERE ");
4850
final String sql =
@@ -89,43 +91,36 @@ public MapperResult findConfigInfoLike4PageFetchRows(MapperContext context) {
8991
final String appName = (String) context.getWhereParameter(FieldConstant.APP_NAME);
9092
final String content = (String) context.getWhereParameter(FieldConstant.CONTENT);
9193
final String[] tagArr = (String[]) context.getWhereParameter(FieldConstant.TAG_ARR);
94+
final String[] types = (String[]) context.getWhereParameter(FieldConstant.TYPE);
9295

93-
List<Object> paramList = new ArrayList<>();
96+
WhereBuilder where = new WhereBuilder(
97+
"SELECT a.id,a.data_id,a.group_id,a.tenant_id,a.app_name,a.content,a.type "
98+
+ "FROM config_info a LEFT JOIN config_tags_relation b ON a.id=b.id");
9499

95-
StringBuilder where = new StringBuilder(" WHERE ");
96-
final String sqlFetchRows = "SELECT a.id,a.data_id,a.group_id,a.tenant_id,a.app_name,a.content "
97-
+ "FROM config_info a LEFT JOIN config_tags_relation b ON a.id=b.id ";
100+
where.like("a.tenant_id", tenant);
98101

99-
where.append(" a.tenant_id LIKE ? ");
100-
paramList.add(tenant);
101-
if (!StringUtils.isBlank(dataId)) {
102-
where.append(" AND a.data_id LIKE ? ");
103-
paramList.add(dataId);
102+
if (StringUtils.isNotBlank(dataId)) {
103+
where.and().like("a.data_id", dataId);
104104
}
105-
if (!StringUtils.isBlank(group)) {
106-
where.append(" AND a.group_id LIKE ? ");
107-
paramList.add(group);
105+
if (StringUtils.isNotBlank(group)) {
106+
where.and().like("a.group_id", group);
108107
}
109-
if (!StringUtils.isBlank(appName)) {
110-
where.append(" AND a.app_name = ? ");
111-
paramList.add(appName);
108+
if (StringUtils.isNotBlank(appName)) {
109+
where.and().eq("a.app_name", appName);
112110
}
113-
if (!StringUtils.isBlank(content)) {
114-
where.append(" AND a.content LIKE ? ");
115-
paramList.add(content);
111+
if (StringUtils.isNotBlank(content)) {
112+
where.and().like("a.content", content);
116113
}
117-
118-
where.append(" AND b.tag_name IN (");
119-
for (int i = 0; i < tagArr.length; i++) {
120-
if (i != 0) {
121-
where.append(", ");
122-
}
123-
where.append('?');
124-
paramList.add(tagArr[i]);
114+
if (!ArrayUtils.isEmpty(tagArr)) {
115+
where.and().in("b.tag_name", tagArr);
125116
}
126-
where.append(") ");
127-
return new MapperResult(sqlFetchRows + where + " LIMIT " + context.getStartRow() + "," + context.getPageSize(),
128-
paramList);
117+
if (!ArrayUtils.isEmpty(types)) {
118+
where.and().in("a.type", types);
119+
}
120+
121+
where.limit(context.getStartRow(), context.getPageSize());
122+
123+
return where.build();
129124
}
130125

131126
@Override

‎plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/ConfigInfoMapper.java

+17-18
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616

1717
package com.alibaba.nacos.plugin.datasource.mapper;
1818

19+
import com.alibaba.nacos.common.utils.ArrayUtils;
1920
import com.alibaba.nacos.common.utils.CollectionUtils;
2021
import com.alibaba.nacos.common.utils.NamespaceUtil;
2122
import com.alibaba.nacos.common.utils.StringUtils;
2223
import com.alibaba.nacos.plugin.datasource.constants.FieldConstant;
2324
import com.alibaba.nacos.plugin.datasource.constants.TableConstant;
25+
import com.alibaba.nacos.plugin.datasource.mapper.ext.WhereBuilder;
2426
import com.alibaba.nacos.plugin.datasource.model.MapperContext;
2527
import com.alibaba.nacos.plugin.datasource.model.MapperResult;
2628

@@ -384,30 +386,27 @@ default MapperResult findConfigInfoLike4PageCountRows(MapperContext context) {
384386
final String content = (String) context.getWhereParameter(FieldConstant.CONTENT);
385387
final String appName = (String) context.getWhereParameter(FieldConstant.APP_NAME);
386388
final String tenantId = (String) context.getWhereParameter(FieldConstant.TENANT_ID);
389+
final String[] types = (String[]) context.getWhereParameter(FieldConstant.TYPE);
387390

388-
final List<Object> paramList = new ArrayList<>();
391+
WhereBuilder where = new WhereBuilder("SELECT count(*) FROM config_info");
389392

390-
final String sqlCountRows = "SELECT count(*) FROM config_info";
391-
StringBuilder where = new StringBuilder(" WHERE ");
392-
where.append(" tenant_id LIKE ? ");
393-
paramList.add(tenantId);
394-
if (!StringUtils.isBlank(dataId)) {
395-
where.append(" AND data_id LIKE ? ");
396-
paramList.add(dataId);
393+
where.like("tenant_id", tenantId);
394+
if (StringUtils.isNotBlank(dataId)) {
395+
where.and().like("data_id", dataId);
397396
}
398-
if (!StringUtils.isBlank(group)) {
399-
where.append(" AND group_id LIKE ? ");
400-
paramList.add(group);
397+
if (StringUtils.isNotBlank(group)) {
398+
where.and().like("group_id", group);
401399
}
402-
if (!StringUtils.isBlank(appName)) {
403-
where.append(" AND app_name = ? ");
404-
paramList.add(appName);
400+
if (StringUtils.isNotBlank(appName)) {
401+
where.and().eq("app_name", appName);
405402
}
406-
if (!StringUtils.isBlank(content)) {
407-
where.append(" AND content LIKE ? ");
408-
paramList.add(content);
403+
if (StringUtils.isNotBlank(content)) {
404+
where.and().like("content", content);
409405
}
410-
return new MapperResult(sqlCountRows + where, paramList);
406+
if (!ArrayUtils.isEmpty(types)) {
407+
where.and().in("type", types);
408+
}
409+
return where.build();
411410
}
412411

413412
/**

‎plugin/datasource/src/main/java/com/alibaba/nacos/plugin/datasource/mapper/ConfigTagsRelationMapper.java

+17-24
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@
1616

1717
package com.alibaba.nacos.plugin.datasource.mapper;
1818

19+
import com.alibaba.nacos.common.utils.ArrayUtils;
1920
import com.alibaba.nacos.common.utils.StringUtils;
2021
import com.alibaba.nacos.plugin.datasource.constants.FieldConstant;
2122
import com.alibaba.nacos.plugin.datasource.constants.TableConstant;
23+
import com.alibaba.nacos.plugin.datasource.mapper.ext.WhereBuilder;
2224
import com.alibaba.nacos.plugin.datasource.model.MapperContext;
2325
import com.alibaba.nacos.plugin.datasource.model.MapperResult;
2426

@@ -111,40 +113,31 @@ default MapperResult findConfigInfoLike4PageCountRows(final MapperContext contex
111113
final String group = (String) context.getWhereParameter(FieldConstant.GROUP_ID);
112114
final String content = (String) context.getWhereParameter(FieldConstant.CONTENT);
113115
final String[] tagArr = (String[]) context.getWhereParameter(FieldConstant.TAG_ARR);
116+
final String[] types = (String[]) context.getWhereParameter(FieldConstant.TYPE);
114117

115-
List<Object> paramList = new ArrayList<>();
116-
StringBuilder where = new StringBuilder(" WHERE ");
117-
final String sqlCount = "SELECT count(*) FROM config_info a LEFT JOIN config_tags_relation b ON a.id=b.id ";
118+
WhereBuilder where = new WhereBuilder("SELECT count(*) FROM config_info a LEFT JOIN config_tags_relation b ON a.id=b.id");
118119

119-
where.append(" a.tenant_id LIKE ? ");
120-
paramList.add(tenantId);
121-
if (!StringUtils.isBlank(dataId)) {
122-
where.append(" AND a.data_id LIKE ? ");
123-
paramList.add(dataId);
120+
where.like("a.tenant_id", tenantId);
121+
if (StringUtils.isNotBlank(dataId)) {
122+
where.and().like("a.data_id", dataId);
124123
}
125124
if (StringUtils.isNotBlank(group)) {
126-
where.append(" AND a.group_id LIKE ? ");
127-
paramList.add(group);
125+
where.and().like("a.group_id", group);
128126
}
129127
if (StringUtils.isNotBlank(appName)) {
130-
where.append(" AND a.app_name = ? ");
131-
paramList.add(appName);
128+
where.and().eq("a.app_name", appName);
132129
}
133130
if (StringUtils.isNotBlank(content)) {
134-
where.append(" AND a.content LIKE ? ");
135-
paramList.add(content);
131+
where.and().like("a.content", content);
136132
}
137-
138-
where.append(" AND b.tag_name IN (");
139-
for (int i = 0; i < tagArr.length; i++) {
140-
if (i != 0) {
141-
where.append(", ");
142-
}
143-
where.append('?');
144-
paramList.add(tagArr[i]);
133+
if (!ArrayUtils.isEmpty(tagArr)) {
134+
where.and().in("b.tag_name", tagArr);
145135
}
146-
where.append(") ");
147-
return new MapperResult(sqlCount + where, paramList);
136+
if (!ArrayUtils.isEmpty(types)) {
137+
where.and().in("a.type", types);
138+
}
139+
140+
return where.build();
148141
}
149142

150143
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
/*
2+
* Copyright 1999-2018 Alibaba Group Holding Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.alibaba.nacos.plugin.datasource.mapper.ext;
18+
19+
import com.alibaba.nacos.common.constant.Symbols;
20+
import com.alibaba.nacos.plugin.datasource.model.MapperResult;
21+
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
25+
/**
26+
* Where Builder.
27+
*
28+
* @author haiqi.wang
29+
* @date 2024/08/13
30+
*/
31+
public final class WhereBuilder {
32+
33+
/**
34+
* Base sql.
35+
*/
36+
private final String sql;
37+
38+
/**
39+
* Parameters.
40+
*/
41+
private final List<Object> parameters = new ArrayList<>();
42+
43+
/**
44+
* Where Conditional.
45+
*/
46+
private final StringBuilder where = new StringBuilder(" WHERE ");
47+
48+
/**
49+
* Default Construct.
50+
*
51+
* @param sql Sql Script
52+
*/
53+
public WhereBuilder(String sql) {
54+
this.sql = sql;
55+
}
56+
57+
/**
58+
* Build AND.
59+
*
60+
* @return Return {@link WhereBuilder}
61+
*/
62+
public WhereBuilder and() {
63+
where.append(" AND ");
64+
return this;
65+
}
66+
67+
/**
68+
* Build OR.
69+
*
70+
* @return Return {@link WhereBuilder}
71+
*/
72+
public WhereBuilder or() {
73+
where.append(" OR ");
74+
return this;
75+
}
76+
77+
/**
78+
* Build Equals.
79+
*
80+
* @param filed Filed name
81+
* @param parameter Parameters
82+
* @return Return {@link WhereBuilder}
83+
*/
84+
public WhereBuilder eq(String filed, Object parameter) {
85+
where.append(filed).append(" = ? ");
86+
parameters.add(parameter);
87+
return this;
88+
}
89+
90+
/**
91+
* Build LIKE.
92+
*
93+
* @param filed Filed name
94+
* @param parameter Parameters
95+
* @return Return {@link WhereBuilder}
96+
*/
97+
public WhereBuilder like(String filed, Object parameter) {
98+
where.append(filed).append(" LIKE ? ");
99+
parameters.add(parameter);
100+
return this;
101+
}
102+
103+
/**
104+
* Build IN.
105+
*
106+
* @param filed Filed name
107+
* @param parameterArr Parameters Array
108+
* @return Return {@link WhereBuilder}
109+
*/
110+
public WhereBuilder in(String filed, Object[] parameterArr) {
111+
where.append(filed).append(" IN (");
112+
for (int i = 0; i < parameterArr.length; i++) {
113+
if (i != 0) {
114+
where.append(", ");
115+
}
116+
where.append('?');
117+
parameters.add(parameterArr[i]);
118+
}
119+
where.append(") ");
120+
return this;
121+
}
122+
123+
/**
124+
* Build offset.
125+
*
126+
* @param startRow Start row
127+
* @param pageSize Page size
128+
* @return Return {@link WhereBuilder}
129+
*/
130+
public WhereBuilder offset(int startRow, int pageSize) {
131+
where.append(" OFFSET ")
132+
.append(startRow)
133+
.append(" ROWS FETCH NEXT ")
134+
.append(pageSize)
135+
.append(" ROWS ONLY");
136+
return this;
137+
}
138+
139+
/**
140+
* Build limit.
141+
*
142+
* @param startRow Start row
143+
* @param pageSize Page size
144+
* @return Return {@link WhereBuilder}
145+
*/
146+
public WhereBuilder limit(int startRow, int pageSize) {
147+
where.append(" LIMIT ")
148+
.append(startRow)
149+
.append(Symbols.COMMA)
150+
.append(pageSize);
151+
return this;
152+
}
153+
154+
/**
155+
* Build.
156+
*
157+
* @return Return {@link WhereBuilder}
158+
*/
159+
public MapperResult build() {
160+
return new MapperResult(sql + where, parameters);
161+
}
162+
}

0 commit comments

Comments
 (0)
Please sign in to comment.