Skip to content

Commit 2bfbc04

Browse files
authored
feat: 支持nacos2.4.3版本 (#116)
1 parent eb40f7d commit 2bfbc04

File tree

18 files changed

+2092
-0
lines changed

18 files changed

+2092
-0
lines changed

nacos-datasource-plugin-ext/nacos-datasource-plugin-ext-base/src/main/java/com/alibaba/nacos/plugin/datasource/constants/DatabaseTypeConstant.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,6 @@ public class DatabaseTypeConstant {
3737

3838
public static final String KINGBASE = "kingbase";
3939

40+
public static final String YASDB = "yasdb";
41+
4042
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>nacos-datasource-plugin-ext</artifactId>
7+
<groupId>com.alibaba.nacos</groupId>
8+
<version>${revision}</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>nacos-yasdb-datasource-plugin-ext</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>8</maven.compiler.source>
16+
<maven.compiler.target>8</maven.compiler.target>
17+
<jdbc.yasdb.version>1.9.3</jdbc.yasdb.version>
18+
</properties>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>com.alibaba.nacos</groupId>
23+
<artifactId>nacos-datasource-plugin-ext-base</artifactId>
24+
<version>${revision}</version>
25+
<scope>compile</scope>
26+
</dependency>
27+
</dependencies>
28+
29+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 1999-2023 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.dialect;
18+
19+
import com.alibaba.nacos.common.utils.NamespaceUtil;
20+
import com.alibaba.nacos.plugin.datasource.constants.DatabaseTypeConstant;
21+
import com.alibaba.nacos.plugin.datasource.enums.TrustedYasdbFunctionEnum;
22+
23+
/***
24+
* yasdb datasource dialect.
25+
* @author onewe
26+
*/
27+
public class YasdbDatabaseDialect extends AbstractDatabaseDialect {
28+
29+
private static final String DEFAULT_NAMESPACE_ID = "PUBLIC";
30+
31+
static {
32+
NamespaceUtil.namespaceDefaultId = DEFAULT_NAMESPACE_ID;
33+
}
34+
35+
@Override
36+
public String getType() {
37+
return DatabaseTypeConstant.YASDB;
38+
}
39+
40+
@Override
41+
public String getLimitTopSqlWithMark(String sql) {
42+
return sql + " FETCH FIRST ? ROWS ONLY ";
43+
}
44+
45+
@Override
46+
public String getLimitPageSqlWithMark(String sql) {
47+
return sql + " OFFSET ? ROWS FETCH NEXT ? ROWS ONLY ";
48+
}
49+
50+
@Override
51+
public String getLimitPageSqlWithOffset(String sql, int startOffset, int pageSize) {
52+
return sql + " OFFSET " + startOffset + " ROWS FETCH NEXT " + pageSize + " ROWS ONLY ";
53+
}
54+
55+
@Override
56+
public String getLimitPageSql(String sql, int pageNo, int pageSize) {
57+
return sql + " OFFSET " + getPagePrevNum(pageNo, pageSize) + " ROWS FETCH NEXT " + pageSize + " ROWS ONLY ";
58+
}
59+
60+
61+
@Override
62+
public String getFunction(String functionName) {
63+
return TrustedYasdbFunctionEnum.getFunctionByName(functionName);
64+
}
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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.enums;
18+
19+
20+
import java.util.HashMap;
21+
import java.util.Map;
22+
23+
/**
24+
* The TrustedSqlFunctionEnum enum class is used to enumerate and manage a list of trusted built-in SQL functions.
25+
* By using this enum, you can verify whether a given SQL function is part of the trusted functions list
26+
* to avoid potential SQL injection risks.
27+
*
28+
* @author blake.qiu
29+
*/
30+
public enum TrustedYasdbFunctionEnum {
31+
32+
/**
33+
* NOW().
34+
*/
35+
NOW("NOW()", "NOW()");
36+
37+
private static final Map<String, TrustedYasdbFunctionEnum> LOOKUP_MAP = new HashMap<>();
38+
39+
static {
40+
for (TrustedYasdbFunctionEnum entry : TrustedYasdbFunctionEnum.values()) {
41+
LOOKUP_MAP.put(entry.functionName, entry);
42+
}
43+
}
44+
45+
private final String functionName;
46+
47+
private final String function;
48+
49+
TrustedYasdbFunctionEnum(String functionName, String function) {
50+
this.functionName = functionName;
51+
this.function = function;
52+
}
53+
54+
/**
55+
* Get the function name.
56+
*
57+
* @param functionName function name
58+
* @return function
59+
*/
60+
public static String getFunctionByName(String functionName) {
61+
TrustedYasdbFunctionEnum entry = LOOKUP_MAP.get(functionName);
62+
if (entry != null) {
63+
return entry.function;
64+
}
65+
throw new IllegalArgumentException(String.format("Invalid function name: %s", functionName));
66+
}
67+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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+
package com.alibaba.nacos.plugin.datasource.impl.yasdb;
17+
18+
import java.util.List;
19+
20+
import com.alibaba.nacos.common.utils.NamespaceUtil;
21+
import com.alibaba.nacos.common.utils.StringUtils;
22+
import com.alibaba.nacos.plugin.datasource.constants.DatabaseTypeConstant;
23+
import com.alibaba.nacos.plugin.datasource.dialect.DatabaseDialect;
24+
import com.alibaba.nacos.plugin.datasource.manager.DatabaseDialectManager;
25+
import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper;
26+
27+
/**
28+
* @author onewe
29+
*/
30+
public abstract class AbstractYasdbMapper extends AbstractMapper {
31+
32+
private final DatabaseDialect databaseDialect;
33+
34+
public AbstractYasdbMapper() {
35+
databaseDialect = DatabaseDialectManager.getInstance()
36+
.getDialect(getDataSource());
37+
}
38+
39+
@Override
40+
public String getDataSource() {
41+
return DatabaseTypeConstant.YASDB;
42+
}
43+
44+
@Override
45+
public String select(List<String> columns, List<String> where) {
46+
StringBuilder sql = new StringBuilder();
47+
String method = "SELECT ";
48+
sql.append(method);
49+
for (int i = 0; i < columns.size(); i++) {
50+
sql.append(columns.get(i));
51+
if (i == columns.size() - 1) {
52+
sql.append(" ");
53+
}
54+
else {
55+
sql.append(",");
56+
}
57+
}
58+
sql.append("FROM ");
59+
sql.append(getTableName());
60+
sql.append(" ");
61+
62+
if (where.size() == 0) {
63+
return sql.toString();
64+
}
65+
66+
sql.append("WHERE ");
67+
for (int i = 0; i < where.size(); i++) {
68+
String condition = where.get(i);
69+
if (StringUtils.equalsIgnoreCase(condition, "tenant_id")) {
70+
sql.append("tenant_id").append(" = ").append("NVL(?, '").append(NamespaceUtil.getNamespaceDefaultId())
71+
.append("')");
72+
}
73+
else {
74+
sql.append(condition).append(" = ").append("?");
75+
}
76+
77+
if (i != where.size() - 1) {
78+
sql.append(" AND ");
79+
}
80+
}
81+
return sql.toString();
82+
}
83+
84+
@Override
85+
public String update(List<String> columns, List<String> where) {
86+
StringBuilder sql = new StringBuilder();
87+
String method = "UPDATE ";
88+
sql.append(method);
89+
sql.append(getTableName()).append(" ").append("SET ");
90+
91+
for (int i = 0; i < columns.size(); i++) {
92+
String[] parts = columns.get(i).split("@");
93+
String column = parts[0];
94+
if (parts.length == 2) {
95+
sql.append(column).append(" = ").append(getFunction(parts[1]));
96+
} else {
97+
sql.append(column).append(" = ").append("?");
98+
}
99+
if (i != columns.size() - 1) {
100+
sql.append(",");
101+
}
102+
}
103+
104+
if (where.size() == 0) {
105+
return sql.toString();
106+
}
107+
108+
sql.append(" WHERE ");
109+
110+
for (int i = 0; i < where.size(); i++) {
111+
String condition = where.get(i);
112+
if (StringUtils.equalsIgnoreCase(condition, "tenant_id")) {
113+
sql.append("tenant_id").append(" = ").append("NVL(?, '").append(NamespaceUtil.getNamespaceDefaultId())
114+
.append("')");
115+
}
116+
else {
117+
sql.append(condition).append(" = ").append("?");
118+
}
119+
if (i != where.size() - 1) {
120+
sql.append(" AND ");
121+
}
122+
}
123+
return sql.toString();
124+
}
125+
126+
@Override
127+
public String delete(List<String> params) {
128+
StringBuilder sql = new StringBuilder();
129+
String method = "DELETE ";
130+
sql.append(method).append("FROM ").append(getTableName()).append(" ")
131+
.append("WHERE ");
132+
for (int i = 0; i < params.size(); i++) {
133+
134+
String condition = params.get(i);
135+
if (StringUtils.equalsIgnoreCase(condition, "tenant_id")) {
136+
sql.append("tenant_id").append(" = ").append("NVL(?, '").append(NamespaceUtil.getNamespaceDefaultId())
137+
.append("')");
138+
}
139+
else {
140+
sql.append(condition).append(" = ").append("?");
141+
}
142+
143+
if (i != params.size() - 1) {
144+
sql.append("AND ");
145+
}
146+
}
147+
148+
return sql.toString();
149+
}
150+
151+
@Override
152+
public String count(List<String> where) {
153+
StringBuilder sql = new StringBuilder();
154+
String method = "SELECT ";
155+
sql.append(method);
156+
sql.append("COUNT(*) FROM ");
157+
sql.append(getTableName());
158+
sql.append(" ");
159+
160+
if (null == where || where.size() == 0) {
161+
return sql.toString();
162+
}
163+
164+
sql.append("WHERE ");
165+
for (int i = 0; i < where.size(); i++) {
166+
String condition = where.get(i);
167+
if (StringUtils.equalsIgnoreCase(condition, "tenant_id")) {
168+
sql.append("tenant_id").append(" = ").append("NVL(?, '").append(NamespaceUtil.getNamespaceDefaultId())
169+
.append("')");
170+
}
171+
else {
172+
sql.append(condition).append(" = ").append("?");
173+
}
174+
if (i != where.size() - 1) {
175+
sql.append(" AND ");
176+
}
177+
}
178+
return sql.toString();
179+
}
180+
181+
protected DatabaseDialect getDatabaseDialect() {
182+
return databaseDialect;
183+
}
184+
185+
@Override
186+
public String getFunction(String functionName) {
187+
return databaseDialect.getFunction(functionName);
188+
}
189+
}

0 commit comments

Comments
 (0)