Skip to content

Commit 6a98d8d

Browse files
feat(datasource): 添加 OpenGauss 数据库方言和映射 数据源支持
- 实现兼容OpenGauss功能,整理Mapper结果,不在继承Mysql - 添加 GaussDB 函数枚举类 - 创建 OpenGauss 兼容 MySQL 的数据库模式 - 在数据库类型常量中添加 GaussDB
1 parent c60e26a commit 6a98d8d

20 files changed

+2040
-1
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class DatabaseTypeConstant {
2525

2626
public static final String POSTGRESQL = "postgresql";
2727

28+
public static final String GUASSDB = "gaussdb";
2829

2930
public static final String MYSQL = "mysql";
3031

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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-opengauss-datasource-plugin-ext</artifactId>
13+
<version>1.0.0</version>
14+
15+
<properties>
16+
<maven.compiler.source>8</maven.compiler.source>
17+
<maven.compiler.target>8</maven.compiler.target>
18+
<jdbc.postgresql.version>42.2.19</jdbc.postgresql.version>
19+
</properties>
20+
21+
22+
<dependencies>
23+
<dependency>
24+
<groupId>org.opengauss</groupId>
25+
<artifactId>opengauss-jdbc</artifactId>
26+
<version>5.1.0-og</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>com.alibaba.nacos</groupId>
30+
<artifactId>nacos-datasource-plugin-ext-base</artifactId>
31+
<version>${revision}</version>
32+
<scope>compile</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>com.alibaba.nacos</groupId>
36+
<artifactId>nacos-aes-encryption-plugin</artifactId>
37+
<version>1.0.0-SNAPSHOT</version>
38+
</dependency>
39+
</dependencies>
40+
41+
<build>
42+
<plugins>
43+
<!--Packaging plug-in-->
44+
<plugin>
45+
<groupId>org.apache.maven.plugins</groupId>
46+
<artifactId>maven-shade-plugin</artifactId>
47+
<version>${maven-shade-plugin.version}</version>
48+
<configuration>
49+
<createDependencyReducedPom>false</createDependencyReducedPom>
50+
</configuration>
51+
<executions>
52+
<execution>
53+
<phase>package</phase>
54+
<goals>
55+
<goal>shade</goal>
56+
</goals>
57+
<configuration>
58+
<transformers>
59+
<transformer implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer"/>
60+
</transformers>
61+
<filters>
62+
<filter>
63+
<artifact>*:*</artifact>
64+
<excludes>
65+
<exclude>module-info.class</exclude>
66+
<exclude>META-INF/NOTICE.txt</exclude>
67+
<exclude>META-INF/LICENSE</exclude>
68+
<exclude>META-INF/NOTICE</exclude>
69+
<exclude>META-INF/DEPENDENCIES</exclude>
70+
<exclude>META-INF/*.SF</exclude>
71+
<exclude>META-INF/*.DSA</exclude>
72+
<exclude>META-INF/*.RSA</exclude>
73+
<exclude>META-INF/*.MF</exclude>
74+
</excludes>
75+
</filter>
76+
</filters>
77+
</configuration>
78+
</execution>
79+
</executions>
80+
</plugin>
81+
</plugins>
82+
</build>
83+
84+
85+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 1999-2022 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.plugin.datasource.constants.DatabaseTypeConstant;
20+
21+
/**
22+
* PostgreSQL database dialect.
23+
* @author Long Yu
24+
*/
25+
public class GaussdbDatabaseDialect extends AbstractDatabaseDialect {
26+
27+
@Override
28+
public String getType() {
29+
return DatabaseTypeConstant.GUASSDB;
30+
}
31+
32+
@Override
33+
public String getLimitTopSqlWithMark(String sql) {
34+
return sql + " LIMIT ? ";
35+
}
36+
37+
@Override
38+
public String getLimitPageSqlWithMark(String sql) {
39+
return sql + " OFFSET ? LIMIT ? ";
40+
}
41+
42+
@Override
43+
public String getLimitPageSql(String sql, int pageNo, int pageSize) {
44+
return sql + " OFFSET " + getPagePrevNum(pageNo, pageSize) + " LIMIT " + pageSize;
45+
}
46+
47+
@Override
48+
public String getLimitPageSqlWithOffset(String sql, int startOffset, int pageSize){
49+
return sql + " OFFSET " + startOffset + " LIMIT " + pageSize;
50+
}
51+
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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.impl.enums;
18+
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
22+
/**
23+
* The TrustedSqlFunctionEnum enum class is used to enumerate and manage a list of trusted built-in SQL functions.
24+
* By using this enum, you can verify whether a given SQL function is part of the trusted functions list
25+
* to avoid potential SQL injection risks.
26+
*
27+
* @author blake.qiu
28+
*/
29+
public enum GaussdbFunctionEnum {
30+
31+
/**
32+
* NOW().
33+
*/
34+
NOW("NOW()", "NOW()");
35+
36+
private static final Map<String, GaussdbFunctionEnum> LOOKUP_MAP = new HashMap<>();
37+
38+
static {
39+
for (GaussdbFunctionEnum entry : GaussdbFunctionEnum.values()) {
40+
LOOKUP_MAP.put(entry.functionName, entry);
41+
}
42+
}
43+
44+
private final String functionName;
45+
46+
private final String function;
47+
48+
GaussdbFunctionEnum(String functionName, String function) {
49+
this.functionName = functionName;
50+
this.function = function;
51+
}
52+
53+
/**
54+
* Get the function name.
55+
*
56+
* @param functionName function name
57+
* @return function
58+
*/
59+
public static String getFunctionByName(String functionName) {
60+
GaussdbFunctionEnum entry = LOOKUP_MAP.get(functionName);
61+
if (entry != null) {
62+
return entry.function;
63+
}
64+
throw new IllegalArgumentException(String.format("Invalid function name: %s", functionName));
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 1999-2022 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.impl.opengauss;
18+
19+
import com.alibaba.nacos.plugin.datasource.constants.DatabaseTypeConstant;
20+
import com.alibaba.nacos.plugin.datasource.dialect.DatabaseDialect;
21+
import com.alibaba.nacos.plugin.datasource.impl.enums.GaussdbFunctionEnum;
22+
import com.alibaba.nacos.plugin.datasource.manager.DatabaseDialectManager;
23+
import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper;
24+
25+
/**
26+
* The base implementation of ConfigTagsRelationMapper.
27+
*
28+
* @author Long Yu
29+
**/
30+
public abstract class AbstractMapperByGaussdb extends AbstractMapper {
31+
32+
private DatabaseDialect databaseDialect;
33+
34+
public DatabaseDialect getDatabaseDialect() {
35+
return databaseDialect;
36+
}
37+
38+
public AbstractMapperByGaussdb() {
39+
databaseDialect = DatabaseDialectManager.getInstance().getDialect(getDataSource());
40+
}
41+
42+
@Override
43+
public String getDataSource() {
44+
return DatabaseTypeConstant.GUASSDB;
45+
}
46+
47+
@Override
48+
public String getFunction(String functionName) {
49+
return GaussdbFunctionEnum.getFunctionByName(functionName);
50+
}
51+
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 1999-2022 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.impl.opengauss;
18+
19+
20+
import java.util.List;
21+
22+
import com.alibaba.nacos.common.utils.CollectionUtils;
23+
import com.alibaba.nacos.plugin.datasource.constants.FieldConstant;
24+
import com.alibaba.nacos.plugin.datasource.constants.TableConstant;
25+
import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoAggrMapper;
26+
import com.alibaba.nacos.plugin.datasource.model.MapperContext;
27+
import com.alibaba.nacos.plugin.datasource.model.MapperResult;
28+
29+
/**
30+
* The base implementation of ConfigInfoBetaMapper.
31+
*
32+
* @author Long Yu
33+
**/
34+
public class OpenGaussConfigInfoAggrMapper extends AbstractMapperByGaussdb implements ConfigInfoAggrMapper {
35+
36+
@Override
37+
public String getTableName() {
38+
return TableConstant.CONFIG_INFO_AGGR;
39+
}
40+
41+
@Override
42+
public MapperResult findConfigInfoAggrByPageFetchRows(MapperContext context) {
43+
int startRow = context.getStartRow();
44+
int pageSize = context.getPageSize();
45+
String dataId = (String) context.getWhereParameter(FieldConstant.DATA_ID);
46+
String groupId = (String) context.getWhereParameter(FieldConstant.GROUP_ID);
47+
String tenantId = (String) context.getWhereParameter(FieldConstant.TENANT_ID);
48+
String sql = getDatabaseDialect().getLimitPageSqlWithOffset(
49+
"SELECT data_id,group_id,tenant_id,datum_id,app_name,content FROM config_info_aggr WHERE data_id= ? AND "
50+
+ "group_id= ? AND tenant_id= ? ORDER BY datum_id ", startRow, pageSize);
51+
List<Object> paramList = CollectionUtils.list(dataId, groupId, tenantId);
52+
return new MapperResult(sql, paramList);
53+
}
54+
55+
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* Copyright 1999-2022 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.impl.opengauss;
18+
19+
20+
import java.util.Collections;
21+
22+
import com.alibaba.nacos.plugin.datasource.constants.TableConstant;
23+
import com.alibaba.nacos.plugin.datasource.mapper.ConfigInfoBetaMapper;
24+
import com.alibaba.nacos.plugin.datasource.model.MapperContext;
25+
import com.alibaba.nacos.plugin.datasource.model.MapperResult;
26+
27+
/**
28+
* The base implementation of ConfigInfoBetaMapper.
29+
*
30+
* @author Long Yu
31+
**/
32+
public class OpenGaussConfigInfoBetaMapper extends AbstractMapperByGaussdb implements ConfigInfoBetaMapper {
33+
34+
@Override
35+
public String getTableName() {
36+
return TableConstant.CONFIG_INFO_BETA;
37+
}
38+
39+
public String getLimitPageSqlWithOffset(String sql, int startRow, int pageSize) {
40+
return getDatabaseDialect().getLimitPageSqlWithOffset(sql, startRow, pageSize);
41+
}
42+
43+
@Override
44+
public MapperResult findAllConfigInfoBetaForDumpAllFetchRows(MapperContext context) {
45+
int startRow = context.getStartRow();
46+
int pageSize = context.getPageSize();
47+
String sqlInner = getLimitPageSqlWithOffset("SELECT id FROM config_info_beta ORDER BY id ", startRow,
48+
pageSize);
49+
String sql =
50+
" SELECT t.id,data_id,group_id,tenant_id,app_name,content,md5,gmt_modified,beta_ips,encrypted_data_key "
51+
+ " FROM ( " + sqlInner + " )" + " g, config_info_beta t WHERE g.id = t.id ";
52+
return new MapperResult(sql, Collections.emptyList());
53+
}
54+
55+
56+
}

0 commit comments

Comments
 (0)