Skip to content

Commit bf4169a

Browse files
author
Myeonghyeon-Lee
committed
Apply JSR310 converters with timestamp (#39)
spring-projects/spring-data-relational@69fe41d (cherry picked from commit 4642663)
1 parent e1991c4 commit bf4169a

File tree

4 files changed

+121
-8
lines changed

4 files changed

+121
-8
lines changed

spring-jdbc-plus-support/src/main/java/com/navercorp/spring/jdbc/plus/support/parametersource/converter/DefaultJdbcParameterSourceConverter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ private static List<ConditionalUnwrapper<?>> getConditionalUnwrappers(List<Unwra
135135
@SuppressWarnings("CollectionAddAllCanBeReplacedWithConstructor")
136136
private static Map<Class<?>, Converter<?, ?>> getDefaultConverters() {
137137
List<Converter<?, ?>> converters = new ArrayList<>();
138-
converters.addAll(Java8TimeParameterTypeConverter.getConvertersToRegister());
138+
converters.addAll(Jsr310TimestampBasedConverters.getConvertersToRegister());
139139
converters.add(UuidParameterTypeConverter.UuidToStringTypeConverter.INSTANCE);
140140
return converters.stream()
141141
.collect(toMap(c -> resolveConverterGenerics(c.getClass()).get(0), c -> c));

spring-jdbc-plus-support/src/main/java/com/navercorp/spring/jdbc/plus/support/parametersource/converter/Java8TimeParameterTypeConverter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
* The type Java 8 time parameter type converter.
3535
*
3636
* @author Myeonghyeon Lee
37+
* @deprecated use {@link Jsr310TimestampBasedConverters}
3738
*/
39+
@Deprecated
3840
public class Java8TimeParameterTypeConverter {
3941
/**
4042
* Gets converters to register.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Spring JDBC Plus
3+
*
4+
* Copyright 2020-2021 NAVER Corp.
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package com.navercorp.spring.jdbc.plus.support.parametersource.converter;
20+
21+
import static java.time.ZoneId.*;
22+
23+
import java.sql.Timestamp;
24+
import java.time.Instant;
25+
import java.time.LocalDate;
26+
import java.time.LocalDateTime;
27+
import java.time.LocalTime;
28+
import java.time.ZonedDateTime;
29+
import java.util.Arrays;
30+
import java.util.List;
31+
32+
import org.springframework.core.convert.converter.Converter;
33+
import org.springframework.lang.NonNull;
34+
35+
/**
36+
* The type JSR 310 Timestamp based converters.
37+
*
38+
* @author Myeonghyeon Lee
39+
*/
40+
public abstract class Jsr310TimestampBasedConverters {
41+
42+
/**
43+
* Gets converters to register.
44+
*
45+
* @return the converters to register
46+
*/
47+
public static List<Converter<?, ?>> getConvertersToRegister() {
48+
return Arrays.asList(
49+
LocalDateTimeToTimestampConverter.INSTANCE,
50+
LocalDateToTimestampConverter.INSTANCE,
51+
LocalTimeToTimestampConverter.INSTANCE,
52+
InstantToTimestampConverter.INSTANCE,
53+
ZonedDateTimeToTimestampConverter.INSTANCE
54+
);
55+
}
56+
57+
public enum LocalDateTimeToTimestampConverter implements Converter<LocalDateTime, Timestamp> {
58+
59+
INSTANCE;
60+
61+
@NonNull
62+
@Override
63+
public Timestamp convert(LocalDateTime source) {
64+
return Timestamp.from(source.atZone(systemDefault()).toInstant());
65+
}
66+
}
67+
68+
public enum LocalDateToTimestampConverter implements Converter<LocalDate, Timestamp> {
69+
70+
INSTANCE;
71+
72+
@NonNull
73+
@Override
74+
public Timestamp convert(LocalDate source) {
75+
return Timestamp.from(source.atStartOfDay(systemDefault()).toInstant());
76+
}
77+
}
78+
79+
public enum LocalTimeToTimestampConverter implements Converter<LocalTime, Timestamp> {
80+
81+
INSTANCE;
82+
83+
@NonNull
84+
@Override
85+
public Timestamp convert(LocalTime source) {
86+
return Timestamp.from(source.atDate(LocalDate.now()).atZone(systemDefault()).toInstant());
87+
}
88+
}
89+
90+
public enum InstantToTimestampConverter implements Converter<Instant, Timestamp> {
91+
92+
INSTANCE;
93+
94+
@NonNull
95+
@Override
96+
public Timestamp convert(Instant source) {
97+
return Timestamp.from(source);
98+
}
99+
}
100+
101+
public enum ZonedDateTimeToTimestampConverter implements Converter<ZonedDateTime, Timestamp> {
102+
103+
INSTANCE;
104+
105+
@NonNull
106+
@Override
107+
public Timestamp convert(ZonedDateTime source) {
108+
return Timestamp.from(source.toInstant());
109+
}
110+
}
111+
}

spring-jdbc-plus-support/src/test/java/com/navercorp/spring/jdbc/plus/support/parametersource/converter/DefaultJdbcParameterSourceConverterTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@
2020

2121
import static org.assertj.core.api.Assertions.*;
2222

23+
import java.sql.Timestamp;
2324
import java.time.Instant;
2425
import java.time.LocalDate;
2526
import java.time.LocalDateTime;
2627
import java.util.ArrayList;
2728
import java.util.Arrays;
2829
import java.util.Collections;
29-
import java.util.Date;
3030
import java.util.Iterator;
3131
import java.util.List;
3232
import java.util.Optional;
@@ -64,10 +64,10 @@ void convert() {
6464

6565
// then
6666
assertThat(str).isEqualTo(":sample:");
67-
assertThat(instant).isExactlyInstanceOf(Date.class);
68-
assertThat(localDateTime).isExactlyInstanceOf(Date.class);
69-
assertThat(localDate).isExactlyInstanceOf(Date.class);
70-
assertThat(zonedDateTime).isExactlyInstanceOf(Date.class);
67+
assertThat(instant).isExactlyInstanceOf(Timestamp.class);
68+
assertThat(localDateTime).isExactlyInstanceOf(Timestamp.class);
69+
assertThat(localDate).isExactlyInstanceOf(Timestamp.class);
70+
assertThat(zonedDateTime).isExactlyInstanceOf(Timestamp.class);
7171
assertThat(enumName).isExactlyInstanceOf(String.class);
7272
assertThat(uuid).isExactlyInstanceOf(String.class);
7373

@@ -90,7 +90,7 @@ void convert() {
9090
void convertNullValue() {
9191
// given
9292
DefaultJdbcParameterSourceConverter sut = new DefaultJdbcParameterSourceConverter(
93-
Java8TimeParameterTypeConverter.getConvertersToRegister());
93+
Jsr310TimestampBasedConverters.getConvertersToRegister());
9494
String paramName = "name";
9595
Instant value = null;
9696

@@ -106,7 +106,7 @@ void convertNullValue() {
106106
void convertUnregisteredTypeValue() {
107107
// given
108108
DefaultJdbcParameterSourceConverter sut = new DefaultJdbcParameterSourceConverter(
109-
Java8TimeParameterTypeConverter.getConvertersToRegister());
109+
Jsr310TimestampBasedConverters.getConvertersToRegister());
110110
String paramName = "name";
111111
String value = "sample";
112112

0 commit comments

Comments
 (0)