Browse Source

Guard `JdbcSqlServerDialect` against absence of SQL Server JDBC driver.

Check for presence of microsoft.sql.DateTimeOffset class before returning it as type and associated converters.

Closes #2153
3.4.x
Mark Paluch 2 months ago
parent
commit
9cd79d07e5
No known key found for this signature in database
GPG Key ID: 55BC6374BAA9D973
  1. 21
      spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/dialect/JdbcSqlServerDialect.java

21
spring-data-jdbc/src/main/java/org/springframework/data/jdbc/core/dialect/JdbcSqlServerDialect.java

@ -27,6 +27,8 @@ import java.util.Set; @@ -27,6 +27,8 @@ import java.util.Set;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.ReadingConverter;
import org.springframework.data.relational.core.dialect.SqlServerDialect;
import org.springframework.data.util.ReflectionUtils;
import org.springframework.lang.Nullable;
/**
* {@link SqlServerDialect} that registers JDBC specific converters.
@ -41,7 +43,21 @@ public class JdbcSqlServerDialect extends SqlServerDialect { @@ -41,7 +43,21 @@ public class JdbcSqlServerDialect extends SqlServerDialect {
public static JdbcSqlServerDialect INSTANCE = new JdbcSqlServerDialect();
private static final Set<Class<?>> SIMPLE_TYPES = Set.of(DateTimeOffset.class);
private static final @Nullable Class<?> DATE_TIME_OFFSET_CLASS = ReflectionUtils
.loadIfPresent("microsoft.sql.DateTimeOffset", JdbcSqlServerDialect.class.getClassLoader());
private static final Set<Class<?>> SIMPLE_TYPES;
private static final List<Object> CONVERTERS;
static {
if (DATE_TIME_OFFSET_CLASS != null) {
SIMPLE_TYPES = Set.of(DATE_TIME_OFFSET_CLASS);
CONVERTERS = List.of(DateTimeOffsetToOffsetDateTimeConverter.INSTANCE, DateTimeOffsetToInstantConverter.INSTANCE);
} else {
SIMPLE_TYPES = Set.of();
CONVERTERS = List.of();
}
}
@Override
public Set<Class<?>> simpleTypes() {
@ -52,8 +68,7 @@ public class JdbcSqlServerDialect extends SqlServerDialect { @@ -52,8 +68,7 @@ public class JdbcSqlServerDialect extends SqlServerDialect {
public Collection<Object> getConverters() {
List<Object> converters = new ArrayList<>(super.getConverters());
converters.add(DateTimeOffsetToOffsetDateTimeConverter.INSTANCE);
converters.add(DateTimeOffsetToInstantConverter.INSTANCE);
converters.addAll(CONVERTERS);
return converters;
}

Loading…
Cancel
Save