Skip to content

Commit 1b9b9b2

Browse files
mipo256schauder
authored andcommitted
Add converter for microsoft.sql.DateTimeOffset.
Original pull request spring-projects#1875 Closes spring-projects#1873
1 parent 44f96b8 commit 1b9b9b2

File tree

5 files changed

+46
-13
lines changed

5 files changed

+46
-13
lines changed

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

+14
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import microsoft.sql.DateTimeOffset;
1919

20+
import java.time.Instant;
2021
import java.time.OffsetDateTime;
2122
import java.util.ArrayList;
2223
import java.util.Collection;
@@ -31,6 +32,7 @@
3132
*
3233
* @author Jens Schauder
3334
* @author Christoph Strobl
35+
* @author Mikhail Polivakha
3436
* @since 2.3
3537
*/
3638
public class JdbcSqlServerDialect extends SqlServerDialect {
@@ -42,6 +44,7 @@ public Collection<Object> getConverters() {
4244

4345
List<Object> converters = new ArrayList<>(super.getConverters());
4446
converters.add(DateTimeOffsetToOffsetDateTimeConverter.INSTANCE);
47+
converters.add(DateTimeOffsetToInstantConverter.INSTANCE);
4548
return converters;
4649
}
4750

@@ -55,4 +58,15 @@ public OffsetDateTime convert(DateTimeOffset source) {
5558
return source.getOffsetDateTime();
5659
}
5760
}
61+
62+
@ReadingConverter
63+
enum DateTimeOffsetToInstantConverter implements Converter<DateTimeOffset, Instant> {
64+
65+
INSTANCE;
66+
67+
@Override
68+
public Instant convert(DateTimeOffset source) {
69+
return source.getOffsetDateTime().toInstant();
70+
}
71+
}
5872
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.springframework.data.jdbc.core.dialect;
2+
3+
import java.time.Instant;
4+
import java.util.List;
5+
6+
import org.assertj.core.api.Assertions;
7+
import org.junit.jupiter.api.Test;
8+
import org.springframework.data.jdbc.core.convert.JdbcCustomConversions;
9+
10+
/**
11+
* Tests for {@link JdbcSqlServerDialect}
12+
*
13+
* @author Mikhail Polivakha
14+
*/
15+
class JdbcSqlServerDialectTest {
16+
17+
@Test
18+
void testCustomConversions() {
19+
JdbcCustomConversions jdbcCustomConversions = new JdbcCustomConversions(
20+
(List<?>) JdbcSqlServerDialect.INSTANCE.getConverters());
21+
22+
Assertions
23+
.assertThat(jdbcCustomConversions.hasCustomReadTarget(microsoft.sql.DateTimeOffset.class, Instant.class))
24+
.isTrue();
25+
}
26+
}

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/EnabledOnDatabase.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@
4444
*/
4545
@Retention(RetentionPolicy.RUNTIME)
4646
@Target(ElementType.TYPE)
47-
// required twice as the annotation lookup doesn't merge multiple occurences of the same annotation
47+
// required twice as the annotation lookup doesn't merge multiple occurrences of the same annotation
4848
@ContextCustomizerFactories(value = { TestClassCustomizerFactory.class, EnabledOnDatabaseCustomizerFactory.class })
4949
@Documented
5050
@Inherited

spring-data-jdbc/src/test/java/org/springframework/data/jdbc/testing/IntegrationTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
* @see EnabledOnDatabase
5050
*/
5151
@TestExecutionListeners(value = AssumeFeatureTestExecutionListener.class, mergeMode = MERGE_WITH_DEFAULTS)
52-
// required twice as the annotation lookup doesn't merge multiple occurences of the same annotation
52+
// required twice as the annotation lookup doesn't merge multiple occurrences of the same annotation
5353
@ContextCustomizerFactories(value = { TestClassCustomizerFactory.class, EnabledOnDatabaseCustomizerFactory.class })
5454
@ActiveProfiles(resolver = CombiningActiveProfileResolver.class)
5555
@ExtendWith(SpringExtension.class)

spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect/SqlServerDialect.java

+4-11
Original file line numberDiff line numberDiff line change
@@ -81,17 +81,10 @@ public Position getClausePosition() {
8181

8282
@Override
8383
public String getLock(LockOptions lockOptions) {
84-
switch (lockOptions.getLockMode()) {
85-
86-
case PESSIMISTIC_WRITE:
87-
return "WITH (UPDLOCK, ROWLOCK)";
88-
89-
case PESSIMISTIC_READ:
90-
return "WITH (HOLDLOCK, ROWLOCK)";
91-
92-
default:
93-
return "";
94-
}
84+
return switch (lockOptions.getLockMode()) {
85+
case PESSIMISTIC_WRITE -> "WITH (UPDLOCK, ROWLOCK)";
86+
case PESSIMISTIC_READ -> "WITH (HOLDLOCK, ROWLOCK)";
87+
};
9588
}
9689

9790
@Override

0 commit comments

Comments
 (0)