File tree Expand file tree Collapse file tree 5 files changed +46
-13
lines changed
main/java/org/springframework/data/jdbc/core/dialect
test/java/org/springframework/data/jdbc
spring-data-relational/src/main/java/org/springframework/data/relational/core/dialect Expand file tree Collapse file tree 5 files changed +46
-13
lines changed Original file line number Diff line number Diff line change 1717
1818import microsoft .sql .DateTimeOffset ;
1919
20+ import java .time .Instant ;
2021import java .time .OffsetDateTime ;
2122import java .util .ArrayList ;
2223import java .util .Collection ;
3132 *
3233 * @author Jens Schauder
3334 * @author Christoph Strobl
35+ * @author Mikhail Polivakha
3436 * @since 2.3
3537 */
3638public 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 number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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 )
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments