Skip to content

Commit 76a9fba

Browse files
committed
Add GaussDB support for spring data jdbc
Signed-off-by: liubao68 <[email protected]>
1 parent 1f2e694 commit 76a9fba

File tree

33 files changed

+1511
-15
lines changed

33 files changed

+1511
-15
lines changed

Diff for: pom.xml

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
<mysql-connector-java.version>8.0.33</mysql-connector-java.version>
4141
<postgresql.version>42.7.4</postgresql.version>
4242
<oracle.version>23.7.0.25.01</oracle.version>
43+
<gaussdb.version>506.0.0.b058</gaussdb.version>
4344

4445
<!-- R2DBC driver dependencies-->
4546
<r2dbc-postgresql.version>1.0.7.RELEASE</r2dbc-postgresql.version>

Diff for: spring-data-jdbc/pom.xml

+38
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,13 @@
137137
<optional>true</optional>
138138
</dependency>
139139

140+
<dependency>
141+
<groupId>com.huaweicloud.gaussdb</groupId>
142+
<artifactId>gaussdbjdbc</artifactId>
143+
<version>${gaussdb.version}</version>
144+
<optional>true</optional>
145+
</dependency>
146+
140147
<dependency>
141148
<groupId>org.mariadb.jdbc</groupId>
142149
<artifactId>mariadb-java-client</artifactId>
@@ -318,6 +325,37 @@
318325
</plugins>
319326
</build>
320327
</profile>
328+
<profile>
329+
<id>gaussdb</id>
330+
<build>
331+
<plugins>
332+
<plugin>
333+
<groupId>org.apache.maven.plugins</groupId>
334+
<artifactId>maven-failsafe-plugin</artifactId>
335+
<executions>
336+
<execution>
337+
<id>gaussdb-test</id>
338+
<phase>integration-test</phase>
339+
<goals>
340+
<goal>integration-test</goal>
341+
</goals>
342+
<configuration>
343+
<includes>
344+
<include>**/*IntegrationTests.java</include>
345+
</includes>
346+
<excludes>
347+
<exclude/>
348+
</excludes>
349+
<systemPropertyVariables>
350+
<spring.profiles.active>gaussdb</spring.profiles.active>
351+
</systemPropertyVariables>
352+
</configuration>
353+
</execution>
354+
</executions>
355+
</plugin>
356+
</plugins>
357+
</build>
358+
</profile>
321359
<profile>
322360
<id>all-dbs</id>
323361
<build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2021-2025 the original author or authors.
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+
* https://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 org.springframework.data.jdbc.core.dialect;
17+
18+
import org.springframework.data.relational.core.dialect.GaussDBDialect;
19+
import org.springframework.util.ClassUtils;
20+
21+
import java.util.Arrays;
22+
import java.util.Collections;
23+
import java.util.HashSet;
24+
import java.util.List;
25+
import java.util.Set;
26+
import java.util.function.Consumer;
27+
28+
/**
29+
* JDBC specific GaussDBDialect Dialect.
30+
*
31+
* Notes: this file is token from JdbcPostgresDialect and add specific changes for GaussDB
32+
*
33+
* @author liubao
34+
*/
35+
public class JdbcGaussDBDialect extends GaussDBDialect {
36+
37+
public static final JdbcGaussDBDialect INSTANCE = new JdbcGaussDBDialect();
38+
39+
private static final Set<Class<?>> SIMPLE_TYPES;
40+
41+
static {
42+
43+
Set<Class<?>> simpleTypes = new HashSet<>(GaussDBDialect.INSTANCE.simpleTypes());
44+
List<String> simpleTypeNames = Arrays.asList( //
45+
"com.huawei.gaussdb.jdbc.util.PGobject", //
46+
"com.huawei.gaussdb.jdbc.geometric.PGpoint", //
47+
"com.huawei.gaussdb.jdbc.geometric.PGbox", //
48+
"com.huawei.gaussdb.jdbc.geometric.PGcircle", //
49+
"com.huawei.gaussdb.jdbc.geometric.PGline", //
50+
"com.huawei.gaussdb.jdbc.geometric.PGpath", //
51+
"com.huawei.gaussdb.jdbc.geometric.PGpolygon", //
52+
"com.huawei.gaussdb.jdbc.geometric.PGlseg" //
53+
);
54+
simpleTypeNames.forEach(name -> ifClassPresent(name, simpleTypes::add));
55+
SIMPLE_TYPES = Collections.unmodifiableSet(simpleTypes);
56+
}
57+
58+
@Override
59+
public Set<Class<?>> simpleTypes() {
60+
return SIMPLE_TYPES;
61+
}
62+
63+
/**
64+
* If the class is present on the class path, invoke the specified consumer {@code action} with the class object,
65+
* otherwise do nothing.
66+
*
67+
* @param action block to be executed if a value is present.
68+
*/
69+
private static void ifClassPresent(String className, Consumer<Class<?>> action) {
70+
if (ClassUtils.isPresent(className, GaussDBDialect.class.getClassLoader())) {
71+
action.accept(ClassUtils.resolveClassName(className, GaussDBDialect.class.getClassLoader()));
72+
}
73+
}
74+
}

Diff for: spring-data-jdbc/src/main/java/org/springframework/data/jdbc/repository/config/DialectResolver.java

+12-10
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,12 @@
1515
*/
1616
package org.springframework.data.jdbc.repository.config;
1717

18-
import java.sql.Connection;
19-
import java.sql.DatabaseMetaData;
20-
import java.sql.SQLException;
21-
import java.util.List;
22-
import java.util.Locale;
23-
import java.util.Optional;
24-
25-
import javax.sql.DataSource;
26-
2718
import org.apache.commons.logging.Log;
2819
import org.apache.commons.logging.LogFactory;
2920
import org.springframework.core.io.support.SpringFactoriesLoader;
3021
import org.springframework.dao.NonTransientDataAccessException;
3122
import org.springframework.data.jdbc.core.dialect.JdbcDb2Dialect;
23+
import org.springframework.data.jdbc.core.dialect.JdbcGaussDBDialect;
3224
import org.springframework.data.jdbc.core.dialect.JdbcMySqlDialect;
3325
import org.springframework.data.jdbc.core.dialect.JdbcPostgresDialect;
3426
import org.springframework.data.jdbc.core.dialect.JdbcSqlServerDialect;
@@ -44,6 +36,14 @@
4436
import org.springframework.lang.Nullable;
4537
import org.springframework.util.StringUtils;
4638

39+
import javax.sql.DataSource;
40+
import java.sql.Connection;
41+
import java.sql.DatabaseMetaData;
42+
import java.sql.SQLException;
43+
import java.util.List;
44+
import java.util.Locale;
45+
import java.util.Optional;
46+
4747
/**
4848
* Resolves a {@link Dialect}. Resolution typically uses {@link JdbcOperations} to obtain and inspect a
4949
* {@link Connection}. Dialect resolution uses Spring's {@link SpringFactoriesLoader spring.factories} to determine
@@ -139,7 +139,9 @@ private static Dialect getDialect(Connection connection) throws SQLException {
139139
if (name.contains("oracle")) {
140140
return OracleDialect.INSTANCE;
141141
}
142-
142+
if (name.contains("gaussdb")) {
143+
return JdbcGaussDBDialect.INSTANCE;
144+
}
143145
LOG.info(String.format("Couldn't determine Dialect for \"%s\"", name));
144146
return null;
145147
}

0 commit comments

Comments
 (0)