Polishing.
Removes superfluous method. Applied feedback from review: Formatting, Naming, and improved backward compatibility. Originial pull request #981 See #935
This commit is contained in:
@@ -17,9 +17,9 @@ package org.springframework.data.jdbc.core.dialect;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.relational.core.dialect.Db2Dialect;
|
||||
import org.springframework.data.relational.core.sql.IdentifierProcessing;
|
||||
|
||||
/**
|
||||
* {@link Db2Dialect} that registers JDBC specific converters.
|
||||
@@ -34,8 +34,8 @@ public class JdbcDb2Dialect extends Db2Dialect {
|
||||
@Override
|
||||
public Collection<Object> getConverters() {
|
||||
|
||||
ArrayList<Object> converters = new ArrayList<>(super.getConverters());
|
||||
converters.add(OffsetDateTime2TimestampConverter.INSTANCE);
|
||||
List<Object> converters = new ArrayList<>(super.getConverters());
|
||||
converters.add(OffsetDateTimeToTimestampConverter.INSTANCE);
|
||||
|
||||
return converters;
|
||||
}
|
||||
|
||||
@@ -38,11 +38,11 @@ public class JdbcH2Dialect extends H2Dialect {
|
||||
|
||||
@Override
|
||||
public Collection<Object> getConverters() {
|
||||
return Collections.singletonList(TimestampWithTimeZone2OffsetDateTimeConverter.INSTANCE);
|
||||
return Collections.singletonList(TimestampWithTimeZoneToOffsetDateTimeConverter.INSTANCE);
|
||||
}
|
||||
|
||||
@ReadingConverter
|
||||
enum TimestampWithTimeZone2OffsetDateTimeConverter implements Converter<TimestampWithTimeZone, OffsetDateTime> {
|
||||
enum TimestampWithTimeZoneToOffsetDateTimeConverter implements Converter<TimestampWithTimeZone, OffsetDateTime> {
|
||||
INSTANCE;
|
||||
|
||||
|
||||
|
||||
@@ -43,13 +43,13 @@ public class JdbcMySqlDialect extends MySqlDialect {
|
||||
public Collection<Object> getConverters() {
|
||||
|
||||
ArrayList<Object> converters = new ArrayList<>(super.getConverters());
|
||||
converters.add(OffsetDateTime2TimestampJdbcValueConverter.INSTANCE);
|
||||
converters.add(OffsetDateTimeToTimestampJdbcValueConverter.INSTANCE);
|
||||
|
||||
return converters;
|
||||
}
|
||||
|
||||
@WritingConverter
|
||||
enum OffsetDateTime2TimestampJdbcValueConverter implements Converter<OffsetDateTime, JdbcValue> {
|
||||
enum OffsetDateTimeToTimestampJdbcValueConverter implements Converter<OffsetDateTime, JdbcValue> {
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
|
||||
@@ -23,11 +23,10 @@ import java.util.Collections;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.data.convert.ReadingConverter;
|
||||
import org.springframework.data.relational.core.dialect.Db2Dialect;
|
||||
import org.springframework.data.relational.core.dialect.SqlServerDialect;
|
||||
|
||||
/**
|
||||
* {@link Db2Dialect} that registers JDBC specific converters.
|
||||
* {@link SqlServerDialect} that registers JDBC specific converters.
|
||||
*
|
||||
* @author Jens Schauder
|
||||
* @since 2.3
|
||||
@@ -38,11 +37,11 @@ public class JdbcSqlServerDialect extends SqlServerDialect {
|
||||
|
||||
@Override
|
||||
public Collection<Object> getConverters() {
|
||||
return Collections.singletonList(DateTimeOffset2OffsetDateTimeConverter.INSTANCE);
|
||||
return Collections.singletonList(DateTimeOffsetToOffsetDateTimeConverter.INSTANCE);
|
||||
}
|
||||
|
||||
@ReadingConverter
|
||||
enum DateTimeOffset2OffsetDateTimeConverter implements Converter<DateTimeOffset, OffsetDateTime> {
|
||||
enum DateTimeOffsetToOffsetDateTimeConverter implements Converter<DateTimeOffset, OffsetDateTime> {
|
||||
INSTANCE;
|
||||
|
||||
@Override
|
||||
|
||||
@@ -31,7 +31,7 @@ import java.time.ZoneOffset;
|
||||
* @since 2.3
|
||||
*/
|
||||
@WritingConverter
|
||||
enum OffsetDateTime2TimestampConverter implements Converter<OffsetDateTime, Timestamp> {
|
||||
enum OffsetDateTimeToTimestampConverter implements Converter<OffsetDateTime, Timestamp> {
|
||||
|
||||
INSTANCE;
|
||||
@Override
|
||||
@@ -20,7 +20,12 @@ import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextAware;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
@@ -55,13 +60,17 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations;
|
||||
* @since 1.1
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
public class AbstractJdbcConfiguration {
|
||||
public class AbstractJdbcConfiguration implements ApplicationContextAware {
|
||||
|
||||
private static Logger LOG = LoggerFactory.getLogger(AbstractJdbcConfiguration.class);
|
||||
|
||||
private ApplicationContext applicationContext;
|
||||
|
||||
/**
|
||||
* Register a {@link JdbcMappingContext} and apply an optional {@link NamingStrategy}.
|
||||
*
|
||||
* @param namingStrategy optional {@link NamingStrategy}. Use {@link NamingStrategy#INSTANCE} as fallback.
|
||||
* @param customConversions see {@link #jdbcCustomConversions(Dialect)}.
|
||||
* @param customConversions see {@link #jdbcCustomConversions()}.
|
||||
* @return must not be {@literal null}.
|
||||
*/
|
||||
@Bean
|
||||
@@ -76,10 +85,11 @@ public class AbstractJdbcConfiguration {
|
||||
|
||||
/**
|
||||
* Creates a {@link RelationalConverter} using the configured
|
||||
* {@link #jdbcMappingContext(Optional, JdbcCustomConversions)}. Will get {@link #jdbcCustomConversions(Dialect)} ()} applied.
|
||||
* {@link #jdbcMappingContext(Optional, JdbcCustomConversions)}. Will get {@link #jdbcCustomConversions()} ()}
|
||||
* applied.
|
||||
*
|
||||
* @see #jdbcMappingContext(Optional, JdbcCustomConversions)
|
||||
* @see #jdbcCustomConversions(Dialect) ()
|
||||
* @see #jdbcCustomConversions()
|
||||
* @return must not be {@literal null}.
|
||||
*/
|
||||
@Bean
|
||||
@@ -89,7 +99,7 @@ public class AbstractJdbcConfiguration {
|
||||
DefaultJdbcTypeFactory jdbcTypeFactory = new DefaultJdbcTypeFactory(operations.getJdbcOperations());
|
||||
|
||||
return new BasicJdbcConverter(mappingContext, relationResolver, conversions, jdbcTypeFactory,
|
||||
dialect.getIdentifierProcessing());
|
||||
dialect.getIdentifierProcessing());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -101,10 +111,20 @@ public class AbstractJdbcConfiguration {
|
||||
* @return will never be {@literal null}.
|
||||
*/
|
||||
@Bean
|
||||
public JdbcCustomConversions jdbcCustomConversions(Dialect dialect) {
|
||||
public JdbcCustomConversions jdbcCustomConversions() {
|
||||
|
||||
return new JdbcCustomConversions(CustomConversions.StoreConversions.of(JdbcSimpleTypes.HOLDER,
|
||||
storeConverters(dialect)), userConverters());
|
||||
try {
|
||||
|
||||
Dialect dialect = applicationContext.getBean(Dialect.class);
|
||||
|
||||
return new JdbcCustomConversions(
|
||||
CustomConversions.StoreConversions.of(JdbcSimpleTypes.HOLDER, storeConverters(dialect)), userConverters());
|
||||
} catch (NoSuchBeanDefinitionException exception) {
|
||||
|
||||
LOG.warn("No dialect found. CustomConversions will be configured without dialect specific conversions.");
|
||||
|
||||
return new JdbcCustomConversions();
|
||||
}
|
||||
}
|
||||
|
||||
private List<?> userConverters() {
|
||||
@@ -162,4 +182,9 @@ public class AbstractJdbcConfiguration {
|
||||
public Dialect jdbcDialect(NamedParameterJdbcOperations operations) {
|
||||
return DialectResolver.getDialect(operations.getJdbcOperations());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
|
||||
this.applicationContext = applicationContext;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,13 +1,3 @@
|
||||
package org.springframework.data.jdbc.core.dialect;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.time.OffsetDateTime;
|
||||
|
||||
import org.h2.api.TimestampWithTimeZone;
|
||||
import org.h2.util.DateTimeUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/*
|
||||
* Copyright 2021 the original author or authors.
|
||||
*
|
||||
@@ -23,6 +13,15 @@ import org.junit.jupiter.api.Test;
|
||||
* 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.time.OffsetDateTime;
|
||||
|
||||
import org.h2.api.TimestampWithTimeZone;
|
||||
import org.h2.util.DateTimeUtils;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Tests for {@link JdbcH2Dialect}.
|
||||
@@ -34,7 +33,7 @@ class JdbcH2DialectTests {
|
||||
@Test
|
||||
void TimestampWithTimeZone2OffsetDateTimeConverterConvertsProperly() {
|
||||
|
||||
JdbcH2Dialect.TimestampWithTimeZone2OffsetDateTimeConverter converter = JdbcH2Dialect.TimestampWithTimeZone2OffsetDateTimeConverter.INSTANCE;
|
||||
JdbcH2Dialect.TimestampWithTimeZoneToOffsetDateTimeConverter converter = JdbcH2Dialect.TimestampWithTimeZoneToOffsetDateTimeConverter.INSTANCE;
|
||||
long dateValue = 123456789;
|
||||
long timeNanos = 987654321;
|
||||
int timeZoneOffsetSeconds = 4 * 60 * 60;
|
||||
|
||||
@@ -7,7 +7,6 @@ import java.time.OffsetDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/*
|
||||
* Copyright 2021 the original author or authors.
|
||||
@@ -26,18 +25,18 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
*/
|
||||
|
||||
/**
|
||||
* Tests for {@link OffsetDateTime2TimestampConverter}.
|
||||
* Tests for {@link OffsetDateTimeToTimestampConverter}.
|
||||
*
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
class OffsetDateTime2TimestampConverterUnitTests {
|
||||
class OffsetDateTimeToTimestampConverterUnitTests {
|
||||
|
||||
@Test
|
||||
void conversionPreservesInstant() {
|
||||
|
||||
OffsetDateTime offsetDateTime = OffsetDateTime.of(5, 5, 5, 5,5,5,123456789, ZoneOffset.ofHours(3));
|
||||
|
||||
Timestamp timestamp = OffsetDateTime2TimestampConverter.INSTANCE.convert(offsetDateTime);
|
||||
Timestamp timestamp = OffsetDateTimeToTimestampConverter.INSTANCE.convert(offsetDateTime);
|
||||
|
||||
assertThat(timestamp.toInstant()).isEqualTo(offsetDateTime.toInstant());
|
||||
}
|
||||
@@ -110,7 +110,7 @@ public class AbstractJdbcConfigurationIntegrationTests {
|
||||
}
|
||||
|
||||
@Override
|
||||
public JdbcCustomConversions jdbcCustomConversions(Dialect dialect) {
|
||||
public JdbcCustomConversions jdbcCustomConversions() {
|
||||
return new JdbcCustomConversions(Collections.singletonList(Blah2BlubbConverter.INSTANCE));
|
||||
}
|
||||
|
||||
|
||||
@@ -1,12 +1,3 @@
|
||||
package org.springframework.data.jdbc.support;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.sql.Types;
|
||||
import java.time.OffsetDateTime;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/*
|
||||
* Copyright 2021 the original author or authors.
|
||||
*
|
||||
@@ -22,6 +13,14 @@ import org.junit.jupiter.api.Test;
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.jdbc.support;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.sql.Types;
|
||||
import java.time.OffsetDateTime;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Tests for {@link JdbcUtil}.
|
||||
|
||||
@@ -77,4 +77,15 @@ class MySqlDataSourceConfiguration extends DataSourceConfiguration implements In
|
||||
new ByteArrayResource("DROP DATABASE test;CREATE DATABASE test;".getBytes()));
|
||||
}
|
||||
}
|
||||
|
||||
private DataSource createRootDataSource() {
|
||||
|
||||
MysqlDataSource dataSource = new MysqlDataSource();
|
||||
dataSource.setUrl(MYSQL_CONTAINER.getJdbcUrl());
|
||||
dataSource.setUser("root");
|
||||
dataSource.setPassword(MYSQL_CONTAINER.getPassword());
|
||||
dataSource.setDatabaseName(MYSQL_CONTAINER.getDatabaseName());
|
||||
|
||||
return dataSource;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,6 +116,6 @@ public class Db2Dialect extends AbstractDialect {
|
||||
|
||||
@Override
|
||||
public Collection<Object> getConverters() {
|
||||
return Collections.singletonList(Timestamp2OffsetDateTimeConverter.INSTANCE);
|
||||
return Collections.singletonList(TimestampAtUtcToOffsetDateTimeConverter.INSTANCE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,6 +34,6 @@ public class MariaDbDialect extends MySqlDialect {
|
||||
|
||||
@Override
|
||||
public Collection<Object> getConverters() {
|
||||
return Collections.singletonList(Timestamp2OffsetDateTimeConverter.INSTANCE);
|
||||
return Collections.singletonList(TimestampAtUtcToOffsetDateTimeConverter.INSTANCE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -167,6 +167,6 @@ public class MySqlDialect extends AbstractDialect {
|
||||
|
||||
@Override
|
||||
public Collection<Object> getConverters() {
|
||||
return Collections.singletonList(Timestamp2OffsetDateTimeConverter.INSTANCE);
|
||||
return Collections.singletonList(TimestampAtUtcToOffsetDateTimeConverter.INSTANCE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +47,7 @@ public class OracleDialect extends AnsiDialect {
|
||||
|
||||
@Override
|
||||
public Collection<Object> getConverters() {
|
||||
return Collections.singletonList(Timestamp2OffsetDateTimeConverter.INSTANCE);
|
||||
return Collections.singletonList(TimestampAtUtcToOffsetDateTimeConverter.INSTANCE);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -117,7 +117,7 @@ public class PostgresDialect extends AbstractDialect {
|
||||
|
||||
@Override
|
||||
public Collection<Object> getConverters() {
|
||||
return Collections.singletonList(Timestamp2OffsetDateTimeConverter.INSTANCE);
|
||||
return Collections.singletonList(TimestampAtUtcToOffsetDateTimeConverter.INSTANCE);
|
||||
}
|
||||
|
||||
static class PostgresLockClause implements LockClause {
|
||||
|
||||
@@ -31,7 +31,7 @@ import org.springframework.data.convert.ReadingConverter;
|
||||
* @since 2.3
|
||||
*/
|
||||
@ReadingConverter
|
||||
enum Timestamp2OffsetDateTimeConverter implements Converter<Timestamp, OffsetDateTime> {
|
||||
enum TimestampAtUtcToOffsetDateTimeConverter implements Converter<Timestamp, OffsetDateTime> {
|
||||
|
||||
INSTANCE;
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
package org.springframework.data.relational.core.dialect;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import java.time.OffsetDateTime;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/*
|
||||
* Copyright 2021 the original author or authors.
|
||||
@@ -26,18 +25,18 @@ import static org.junit.jupiter.api.Assertions.*;
|
||||
*/
|
||||
|
||||
/**
|
||||
* Tests {@link Timestamp2OffsetDateTimeConverter}.
|
||||
* Tests {@link TimestampAtUtcToOffsetDateTimeConverter}.
|
||||
*
|
||||
* @author Jens Schauder
|
||||
*/
|
||||
class Timestamp2OffsetDateTimeConverterUnitTests {
|
||||
class TimestampAtUtcToOffsetDateTimeConverterUnitTests {
|
||||
|
||||
@Test
|
||||
void conversionMaintainsInstant() {
|
||||
|
||||
Timestamp timestamp = Timestamp.from(Instant.now());
|
||||
OffsetDateTime converted = Timestamp2OffsetDateTimeConverter.INSTANCE.convert(timestamp);
|
||||
OffsetDateTime converted = TimestampAtUtcToOffsetDateTimeConverter.INSTANCE.convert(timestamp);
|
||||
|
||||
assertThat(converted.toInstant()).isEqualTo(timestamp.toInstant());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user