Skip to content

Support for Dialect specific custom conversions and OffsetDateTime. #981

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

Closed
wants to merge 5 commits into from
Closed
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
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.3.0-SNAPSHOT</version>
<version>2.3.0-gh-935-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data Relational Parent</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-jdbc-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.3.0-SNAPSHOT</version>
<version>2.3.0-gh-935-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
8 changes: 4 additions & 4 deletions spring-data-jdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<modelVersion>4.0.0</modelVersion>

<artifactId>spring-data-jdbc</artifactId>
<version>2.3.0-SNAPSHOT</version>
<version>2.3.0-gh-935-SNAPSHOT</version>

<name>Spring Data JDBC</name>
<description>Spring Data module for JDBC repositories.</description>
Expand All @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-relational-parent</artifactId>
<version>2.3.0-SNAPSHOT</version>
<version>2.3.0-gh-935-SNAPSHOT</version>
</parent>

<properties>
Expand Down Expand Up @@ -141,7 +141,7 @@
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>${h2.version}</version>
<scope>test</scope>
<optional>true</optional>
</dependency>

<dependency>
Expand Down Expand Up @@ -190,7 +190,7 @@
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>${mssql.version}</version>
<scope>test</scope>
<optional>true</optional>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package org.springframework.data.jdbc.core.convert;

import java.sql.Timestamp;
import java.time.OffsetDateTime;
import java.time.ZonedDateTime;
import java.time.temporal.Temporal;
import java.util.Date;
Expand Down Expand Up @@ -52,6 +53,7 @@ public Class<?> resolvePrimitiveType(Class<?> type) {

javaToDbType.put(Enum.class, String.class);
javaToDbType.put(ZonedDateTime.class, String.class);
javaToDbType.put(OffsetDateTime.class, OffsetDateTime.class);
javaToDbType.put(Temporal.class, Timestamp.class);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
package org.springframework.data.jdbc.core.convert;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;

import org.springframework.core.convert.converter.GenericConverter.ConvertiblePair;
import org.springframework.data.convert.CustomConversions;
Expand All @@ -29,14 +31,14 @@
*
* @author Mark Paluch
* @author Jens Schauder
* @author Christoph Strobl
* @see CustomConversions
* @see org.springframework.data.mapping.model.SimpleTypeHolder
* @see JdbcSimpleTypes
*/
public class JdbcCustomConversions extends CustomConversions {

private static final List<Object> STORE_CONVERTERS = Arrays
.asList(Jsr310TimestampBasedConverters.getConvertersToRegister().toArray());
private static final Collection<Object> STORE_CONVERTERS = Collections.unmodifiableCollection(Jsr310TimestampBasedConverters.getConvertersToRegister());
private static final StoreConversions STORE_CONVERSIONS = StoreConversions.of(JdbcSimpleTypes.HOLDER,
STORE_CONVERTERS);

Expand All @@ -48,14 +50,23 @@ public JdbcCustomConversions() {
}

/**
* Create a new {@link JdbcCustomConversions} instance registering the given converters.
* Create a new {@link JdbcCustomConversions} instance registering the given converters and the default store converters.
*
* @param converters must not be {@literal null}.
*/
public JdbcCustomConversions(List<?> converters) {
super(new ConverterConfiguration(STORE_CONVERSIONS, converters, JdbcCustomConversions::isDateTimeApiConversion));
}

/**
* Create a new {@link JdbcCustomConversions} instance registering the given converters and the default store converters.
*
* @since 2.3
*/
public JdbcCustomConversions(StoreConversions storeConversions, List<?> userConverters) {
super(new ConverterConfiguration(storeConversions, userConverters, JdbcCustomConversions::isDateTimeApiConversion));
}

/**
* Create a new {@link JdbcCustomConversions} instance given
* {@link org.springframework.data.convert.CustomConversions.ConverterConfiguration}.
Expand All @@ -67,6 +78,16 @@ public JdbcCustomConversions(ConverterConfiguration converterConfiguration) {
super(converterConfiguration);
}

/**
* Obtain a read only copy of default store converters.
*
* @return never {@literal null}.
* @since 2.3
*/
public static Collection<Object> storeConverters() {
return STORE_CONVERTERS;
}

private static boolean isDateTimeApiConversion(ConvertiblePair cp) {

if (cp.getSourceType().equals(java.util.Date.class)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2021 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.sql.Timestamp;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.data.relational.core.dialect.Db2Dialect;

/**
* {@link Db2Dialect} that registers JDBC specific converters.
*
* @author Jens Schauder
* @author Christoph Strobl
* @since 2.3
*/
public class JdbcDb2Dialect extends Db2Dialect {

public static JdbcDb2Dialect INSTANCE = new JdbcDb2Dialect();

protected JdbcDb2Dialect() {}

@Override
public Collection<Object> getConverters() {

List<Object> converters = new ArrayList<>(super.getConverters());
converters.add(OffsetDateTimeToTimestampConverter.INSTANCE);

return converters;
}

/**
* {@link WritingConverter} from {@link OffsetDateTime} to {@link Timestamp}. The conversion preserves the
* {@link java.time.Instant} represented by {@link OffsetDateTime}
*
* @author Jens Schauder
* @since 2.3
*/
@WritingConverter
enum OffsetDateTimeToTimestampConverter implements Converter<OffsetDateTime, Timestamp> {

INSTANCE;

@Override
public Timestamp convert(OffsetDateTime source) {
return Timestamp.from(source.toInstant());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Copyright 2021 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.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.h2.api.TimestampWithTimeZone;
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.H2Dialect;

/**
* {@link Db2Dialect} that registers JDBC specific converters.
*
* @author Jens Schauder
* @author Christoph Strobl
* @since 2.3
*/
public class JdbcH2Dialect extends H2Dialect {

public static JdbcH2Dialect INSTANCE = new JdbcH2Dialect();

protected JdbcH2Dialect() {}

@Override
public Collection<Object> getConverters() {

List<Object> converters = new ArrayList<>(super.getConverters());
converters.add(TimestampWithTimeZoneToOffsetDateTimeConverter.INSTANCE);
return converters;
}

@ReadingConverter
enum TimestampWithTimeZoneToOffsetDateTimeConverter implements Converter<TimestampWithTimeZone, OffsetDateTime> {

INSTANCE;

@Override
public OffsetDateTime convert(TimestampWithTimeZone source) {

long nanosInSecond = 1_000_000_000;
long nanosInMinute = nanosInSecond * 60;
long nanosInHour = nanosInMinute * 60;

long hours = (source.getNanosSinceMidnight() / nanosInHour);

long nanosInHours = hours * nanosInHour;
long nanosLeft = source.getNanosSinceMidnight() - nanosInHours;
long minutes = nanosLeft / nanosInMinute;

long nanosInMinutes = minutes * nanosInMinute;
nanosLeft -= nanosInMinutes;
long seconds = nanosLeft / nanosInSecond;

long nanosInSeconds = seconds * nanosInSecond;
nanosLeft -= nanosInSeconds;
ZoneOffset offset = ZoneOffset.ofTotalSeconds(source.getTimeZoneOffsetSeconds());

return OffsetDateTime.of(source.getYear(), source.getMonth(), source.getDay(), (int) hours, (int) minutes,
(int) seconds, (int) nanosLeft, offset);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2021 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.sql.JDBCType;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Collection;

import org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.data.jdbc.core.convert.JdbcValue;
import org.springframework.data.relational.core.dialect.Db2Dialect;
import org.springframework.data.relational.core.dialect.MySqlDialect;
import org.springframework.data.relational.core.sql.IdentifierProcessing;

/**
* {@link Db2Dialect} that registers JDBC specific converters.
*
* @author Jens Schauder
* @author Christoph Strobl
* @since 2.3
*/
public class JdbcMySqlDialect extends MySqlDialect {

public JdbcMySqlDialect(IdentifierProcessing identifierProcessing) {
super(identifierProcessing);
}

protected JdbcMySqlDialect() {}

@Override
public Collection<Object> getConverters() {

ArrayList<Object> converters = new ArrayList<>(super.getConverters());
converters.add(OffsetDateTimeToTimestampJdbcValueConverter.INSTANCE);

return converters;
}

@WritingConverter
enum OffsetDateTimeToTimestampJdbcValueConverter implements Converter<OffsetDateTime, JdbcValue> {

INSTANCE;

@Override
public JdbcValue convert(OffsetDateTime source) {
return JdbcValue.of(source, JDBCType.TIMESTAMP);
}
}
}
Loading