Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Push JDBC-specific simple types into JdbcPostgresDialect instead of having these in the top-level dialect that shouldn't be tied to any driver technology.

Introduce profile to run Postgres tests only.
  • Loading branch information
mp911de authored and schauder committed Sep 6, 2023
1 parent 8e6f33e commit 879984d
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 39 deletions.
31 changes: 31 additions & 0 deletions spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,37 @@


<profiles>
<profile>
<id>postgres</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<executions>
<execution>
<id>postgres-test</id>
<phase>test</phase>
<goals>
<goal>test</goal>
</goals>
<configuration>
<includes>
<include>**/*IntegrationTests.java</include>
</includes>
<excludes>
<exclude>**/*HsqlIntegrationTests.java</exclude>
</excludes>
<systemPropertyVariables>
<spring.profiles.active>postgres</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
Expand Up @@ -16,7 +16,6 @@
package org.springframework.data.jdbc.aot;

import java.util.Arrays;
import java.util.UUID;

import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
Expand Down Expand Up @@ -63,7 +62,5 @@ public void registerHints(RuntimeHints hints, @Nullable ClassLoader classLoader)
for (Class<?> simpleType : JdbcPostgresDialect.INSTANCE.simpleTypes()) {
hints.reflection().registerType(TypeReference.of(simpleType), MemberCategory.PUBLIC_CLASSES);
}

hints.reflection().registerType(TypeReference.of(UUID.class.getName()), MemberCategory.PUBLIC_CLASSES);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.function.Consumer;

import org.postgresql.core.Oid;
import org.postgresql.jdbc.TypeInfoCache;
Expand All @@ -46,11 +50,47 @@ public class JdbcPostgresDialect extends PostgresDialect implements JdbcDialect

private static final JdbcPostgresArrayColumns ARRAY_COLUMNS = new JdbcPostgresArrayColumns();

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

static {

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

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

@Override
public JdbcArrayColumns getArraySupport() {
return ARRAY_COLUMNS;
}

/**
* 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, PostgresDialect.class.getClassLoader())) {
action.accept(ClassUtils.resolveClassName(className, PostgresDialect.class.getClassLoader()));
}
}

static class JdbcPostgresArrayColumns implements JdbcArrayColumns {

private static final boolean TYPE_INFO_PRESENT = ClassUtils.isPresent("org.postgresql.jdbc.TypeInfoCache",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@

import io.r2dbc.postgresql.codec.Json;

import java.net.InetAddress;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.stream.Stream;

Expand Down Expand Up @@ -51,7 +46,7 @@ public class PostgresDialect extends org.springframework.data.relational.core.di
static {

Set<Class<?>> simpleTypes = new HashSet<>(
Arrays.asList(UUID.class, URL.class, URI.class, InetAddress.class, Map.class));
org.springframework.data.relational.core.dialect.PostgresDialect.INSTANCE.simpleTypes());

// conditional Postgres Geo support.
Stream.of("io.r2dbc.postgresql.codec.Box", //
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
*/
package org.springframework.data.relational.core.dialect;

import java.util.Arrays;
import java.net.InetAddress;
import java.net.URI;
import java.net.URL;
import java.rmi.server.UID;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;

import org.springframework.data.relational.core.sql.Functions;
import org.springframework.data.relational.core.sql.IdentifierProcessing;
Expand All @@ -32,7 +34,6 @@
import org.springframework.data.relational.core.sql.SimpleFunction;
import org.springframework.data.relational.core.sql.SqlIdentifier;
import org.springframework.data.relational.core.sql.TableLike;
import org.springframework.util.ClassUtils;

/**
* An SQL dialect for Postgres.
Expand All @@ -50,6 +51,9 @@ public class PostgresDialect extends AbstractDialect {
*/
public static final PostgresDialect INSTANCE = new PostgresDialect();

private static final Set<Class<?>> POSTGRES_SIMPLE_TYPES = Set.of(UID.class, URL.class, URI.class, InetAddress.class,
Map.class);

protected PostgresDialect() {}

private static final LimitClause LIMIT_CLAUSE = new LimitClause() {
Expand Down Expand Up @@ -152,32 +156,7 @@ public IdentifierProcessing getIdentifierProcessing() {

@Override
public Set<Class<?>> simpleTypes() {

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

/**
* 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, PostgresDialect.class.getClassLoader())) {
action.accept(ClassUtils.resolveClassName(className, PostgresDialect.class.getClassLoader()));
}
return POSTGRES_SIMPLE_TYPES;
}

@Override
Expand Down

0 comments on commit 879984d

Please sign in to comment.