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 3 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 @@ -18,6 +18,7 @@
import java.util.Arrays;
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 @@ -35,7 +36,7 @@
*/
public class JdbcCustomConversions extends CustomConversions {

private static final List<Object> STORE_CONVERTERS = Arrays
public static final List<Object> STORE_CONVERTERS = Arrays
.asList(Jsr310TimestampBasedConverters.getConvertersToRegister().toArray());
private static final StoreConversions STORE_CONVERSIONS = StoreConversions.of(JdbcSimpleTypes.HOLDER,
STORE_CONVERTERS);
Expand All @@ -48,14 +49,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 Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.util.ArrayList;
import java.util.Collection;

import org.springframework.data.relational.core.dialect.Db2Dialect;
import org.springframework.data.relational.core.sql.IdentifierProcessing;

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

public static JdbcDb2Dialect INSTANCE = new JdbcDb2Dialect();

@Override
public Collection<Object> getConverters() {

ArrayList<Object> converters = new ArrayList<>(super.getConverters());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: List<Object> instead of ArrayList

converters.add(OffsetDateTime2TimestampConverter.INSTANCE);

return converters;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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 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;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.Collection;
import java.util.Collections;

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

public static JdbcH2Dialect INSTANCE = new JdbcH2Dialect();

@Override
public Collection<Object> getConverters() {
return Collections.singletonList(TimestampWithTimeZone2OffsetDateTimeConverter.INSTANCE);
}

@ReadingConverter
enum TimestampWithTimeZone2OffsetDateTimeConverter implements Converter<TimestampWithTimeZone, OffsetDateTime> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TimestampWithTimeZoneToOffsetDateTimeConverter

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,60 @@
/*
* 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
* @since 2.3
*/
public class JdbcMySqlDialect extends MySqlDialect {

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

@Override
public Collection<Object> getConverters() {

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

return converters;
}

@WritingConverter
enum OffsetDateTime2TimestampJdbcValueConverter implements Converter<OffsetDateTime, JdbcValue> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OffsetDateTimeToJdbcValueConverter

INSTANCE;

@Override
public JdbcValue convert(OffsetDateTime source) {
return JdbcValue.of(source, JDBCType.TIMESTAMP);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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 microsoft.sql.DateTimeOffset;

import java.time.OffsetDateTime;
import java.util.Collection;
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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: SQL Server

*
* @author Jens Schauder
* @since 2.3
*/
public class JdbcSqlServerDialect extends SqlServerDialect {

public static JdbcSqlServerDialect INSTANCE = new JdbcSqlServerDialect();

@Override
public Collection<Object> getConverters() {
return Collections.singletonList(DateTimeOffset2OffsetDateTimeConverter.INSTANCE);
}

@ReadingConverter
enum DateTimeOffset2OffsetDateTimeConverter implements Converter<DateTimeOffset, OffsetDateTime> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DateTimeOffsetToOffsetDateTimeConverter

INSTANCE;

@Override
public OffsetDateTime convert(DateTimeOffset source) {
return source.getOffsetDateTime();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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 org.springframework.core.convert.converter.Converter;
import org.springframework.data.convert.WritingConverter;
import org.springframework.data.relational.core.dialect.Db2Dialect;

import java.sql.Timestamp;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

/**
* {@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 OffsetDateTime2TimestampConverter implements Converter<OffsetDateTime, Timestamp> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OffsetDateTimeToTimestampConverter


INSTANCE;
@Override
public Timestamp convert(OffsetDateTime source) {
return Timestamp.from(source.toInstant());
}
}
Loading