Skip to content

Introduce new JDBC Dialect for GaussDB #2035

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<mysql-connector-java.version>8.0.33</mysql-connector-java.version>
<postgresql.version>42.7.4</postgresql.version>
<oracle.version>23.7.0.25.01</oracle.version>
<gaussdb.version>506.0.0.b058</gaussdb.version>

<!-- R2DBC driver dependencies-->
<r2dbc-postgresql.version>1.0.7.RELEASE</r2dbc-postgresql.version>
Expand Down
38 changes: 38 additions & 0 deletions spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,13 @@
<optional>true</optional>
</dependency>

<dependency>
<groupId>com.huaweicloud.gaussdb</groupId>
<artifactId>gaussdbjdbc</artifactId>
<version>${gaussdb.version}</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
Expand Down Expand Up @@ -318,6 +325,37 @@
</plugins>
</build>
</profile>
<profile>
<id>gaussdb</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<executions>
<execution>
<id>gaussdb-test</id>
<phase>integration-test</phase>
<goals>
<goal>integration-test</goal>
</goals>
<configuration>
<includes>
<include>**/*IntegrationTests.java</include>
</includes>
<excludes>
<exclude/>
</excludes>
<systemPropertyVariables>
<spring.profiles.active>gaussdb</spring.profiles.active>
</systemPropertyVariables>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>all-dbs</id>
<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2021-2025 the original author or authors.
*
* 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
*
* https://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 org.springframework.data.jdbc.core.dialect;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;

import org.springframework.data.relational.core.dialect.GaussDBDialect;
import org.springframework.util.ClassUtils;

/**
* JDBC specific GaussDBDialect Dialect.
*
* Notes: this file is token from JdbcPostgresDialect and add specific changes for GaussDB
*
* @author liubao
*/
public class JdbcGaussDBDialect extends GaussDBDialect {

public static final JdbcGaussDBDialect INSTANCE = new JdbcGaussDBDialect();

private static final Set<Class<?>> SIMPLE_TYPES;

static {

Set<Class<?>> simpleTypes = new HashSet<>(GaussDBDialect.INSTANCE.simpleTypes());
List<String> simpleTypeNames = Arrays.asList( //
"com.huawei.gaussdb.jdbc.util.PGobject", //
"com.huawei.gaussdb.jdbc.geometric.PGpoint", //
"com.huawei.gaussdb.jdbc.geometric.PGbox", //
"com.huawei.gaussdb.jdbc.geometric.PGcircle", //
"com.huawei.gaussdb.jdbc.geometric.PGline", //
"com.huawei.gaussdb.jdbc.geometric.PGpath", //
"com.huawei.gaussdb.jdbc.geometric.PGpolygon", //
"com.huawei.gaussdb.jdbc.geometric.PGlseg" //
);
simpleTypeNames.forEach(name -> ifClassPresent(name, simpleTypes::add));
SIMPLE_TYPES = Collections.unmodifiableSet(simpleTypes);
}

@Override
public Set<Class<?>> simpleTypes() {
return SIMPLE_TYPES;
}

/**
* If the class is present on the class path, invoke the specified consumer {@code action} with the class object,
* otherwise do nothing.
*
* @param action block to be executed if a value is present.
*/
private static void ifClassPresent(String className, Consumer<Class<?>> action) {
if (ClassUtils.isPresent(className, GaussDBDialect.class.getClassLoader())) {
action.accept(ClassUtils.resolveClassName(className, GaussDBDialect.class.getClassLoader()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.dao.NonTransientDataAccessException;
import org.springframework.data.jdbc.core.dialect.JdbcDb2Dialect;
import org.springframework.data.jdbc.core.dialect.JdbcGaussDBDialect;
import org.springframework.data.jdbc.core.dialect.JdbcMySqlDialect;
import org.springframework.data.jdbc.core.dialect.JdbcPostgresDialect;
import org.springframework.data.jdbc.core.dialect.JdbcSqlServerDialect;
Expand Down Expand Up @@ -139,7 +140,9 @@ private static Dialect getDialect(Connection connection) throws SQLException {
if (name.contains("oracle")) {
return OracleDialect.INSTANCE;
}

if (name.contains("gaussdb")) {
return JdbcGaussDBDialect.INSTANCE;
}
LOG.info(String.format("Couldn't determine Dialect for \"%s\"", name));
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
/*
* Copyright 2021-2025 the original author or authors.
*
* 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
*
* https://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 org.springframework.data.jdbc.core.dialect;

import static org.assertj.core.api.Assertions.*;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.Import;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.annotation.Id;
import org.springframework.data.convert.CustomConversions;
import org.springframework.data.jdbc.core.convert.JdbcCustomConversions;
import org.springframework.data.jdbc.core.mapping.JdbcSimpleTypes;
import org.springframework.data.jdbc.repository.config.EnableJdbcRepositories;
import org.springframework.data.jdbc.testing.DatabaseType;
import org.springframework.data.jdbc.testing.EnabledOnDatabase;
import org.springframework.data.jdbc.testing.IntegrationTest;
import org.springframework.data.jdbc.testing.TestConfiguration;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.springframework.data.relational.core.dialect.Dialect;
import org.springframework.data.relational.core.mapping.Table;
import org.springframework.data.repository.CrudRepository;

import com.huawei.gaussdb.jdbc.util.PGobject;

/**
* Integration tests for GaussDB Dialect. Start this test with {@code -Dspring.profiles.active=gaussdb}.
*
* Notes: this file is token from PostgresDialectIntegrationTests and add specific changes for GaussDB
*
* @author liubao
*/
@IntegrationTest
@EnabledOnDatabase(DatabaseType.GAUSSDB)
public class GaussDBDialectIntegrationTests {

@Autowired CustomerRepository customerRepository;

@Test
void shouldSaveAndLoadJson() throws SQLException {

PGobject sessionData = new PGobject();
sessionData.setType("jsonb");
sessionData.setValue("{\"hello\": \"json\"}");

Customer saved = customerRepository
.save(new Customer(null, "Adam Smith", new JsonHolder("{\"hello\": \"world\"}"), sessionData));

Optional<Customer> loaded = customerRepository.findById(saved.getId());

assertThat(loaded).hasValueSatisfying(actual -> {

assertThat(actual.getPersonData().getContent()).isEqualTo("{\"hello\": \"world\"}");
assertThat(actual.getSessionData().getValue()).isEqualTo("{\"hello\": \"json\"}");
});
}

@Configuration
@Import(TestConfiguration.class)
@EnableJdbcRepositories(considerNestedRepositories = true,
includeFilters = @ComponentScan.Filter(value = CustomerRepository.class, type = FilterType.ASSIGNABLE_TYPE))
static class Config {

@Bean
CustomConversions jdbcCustomConversions(Dialect dialect) {
SimpleTypeHolder simpleTypeHolder = new SimpleTypeHolder(dialect.simpleTypes(), JdbcSimpleTypes.HOLDER);

return new JdbcCustomConversions(
CustomConversions.StoreConversions.of(simpleTypeHolder, storeConverters(dialect)), userConverters());
}

private List<Object> storeConverters(Dialect dialect) {

List<Object> converters = new ArrayList<>();
converters.addAll(dialect.getConverters());
converters.addAll(JdbcCustomConversions.storeConverters());
return converters;
}

private List<Object> userConverters() {
return Arrays.asList(JsonHolderToPGobjectConverter.INSTANCE, PGobjectToJsonHolderConverter.INSTANCE);
}
}

enum JsonHolderToPGobjectConverter implements Converter<JsonHolder, PGobject> {

INSTANCE;

@Override
public PGobject convert(JsonHolder source) {
PGobject result = new PGobject();
result.setType("jsonb");
try {
result.setValue(source.getContent());
} catch (SQLException e) {
throw new RuntimeException(e);
}
return result;
}
}

enum PGobjectToJsonHolderConverter implements Converter<PGobject, JsonHolder> {

INSTANCE;

@Override
public JsonHolder convert(PGobject source) {
return new JsonHolder(source.getValue());
}
}

@Table("customers")
public static final class Customer {

@Id private final Long id;
private final String name;
private final JsonHolder personData;
private final PGobject sessionData;

public Customer(Long id, String name, JsonHolder personData, PGobject sessionData) {
this.id = id;
this.name = name;
this.personData = personData;
this.sessionData = sessionData;
}

public Long getId() {
return this.id;
}

public String getName() {
return this.name;
}

public JsonHolder getPersonData() {
return this.personData;
}

public PGobject getSessionData() {
return this.sessionData;
}

public boolean equals(final Object o) {
if (o == this)
return true;
if (!(o instanceof final Customer other))
return false;
final Object this$id = this.getId();
final Object other$id = other.getId();
if (!Objects.equals(this$id, other$id))
return false;
final Object this$name = this.getName();
final Object other$name = other.getName();
if (!Objects.equals(this$name, other$name))
return false;
final Object this$personData = this.getPersonData();
final Object other$personData = other.getPersonData();
if (!Objects.equals(this$personData, other$personData))
return false;
final Object this$sessionData = this.getSessionData();
final Object other$sessionData = other.getSessionData();
return Objects.equals(this$sessionData, other$sessionData);
}

public int hashCode() {
final int PRIME = 59;
int result = 1;
final Object $id = this.getId();
result = result * PRIME + ($id == null ? 43 : $id.hashCode());
final Object $name = this.getName();
result = result * PRIME + ($name == null ? 43 : $name.hashCode());
final Object $personData = this.getPersonData();
result = result * PRIME + ($personData == null ? 43 : $personData.hashCode());
final Object $sessionData = this.getSessionData();
result = result * PRIME + ($sessionData == null ? 43 : $sessionData.hashCode());
return result;
}

public String toString() {
return "PostgresDialectIntegrationTests.Customer(id=" + this.getId() + ", name=" + this.getName()
+ ", personData=" + this.getPersonData() + ", sessionData=" + this.getSessionData() + ")";
}
}

public static class JsonHolder {
String content;

public JsonHolder(String content) {
this.content = content;
}

public JsonHolder() {}

public String getContent() {
return this.content;
}

public void setContent(String content) {
this.content = content;
}
}

interface CustomerRepository extends CrudRepository<Customer, Long> {}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/
public enum DatabaseType {

DB2, HSQL, H2, MARIADB, MYSQL, ORACLE, POSTGRES, SQL_SERVER("mssql");
DB2, HSQL, H2, MARIADB, MYSQL, ORACLE, POSTGRES, SQL_SERVER("mssql"), GAUSSDB;

private final String profile;

Expand Down
Loading