Skip to content

Commit

Permalink
Add converter for microsoft.sql.DateTimeOffset.
Browse files Browse the repository at this point in the history
Original pull request #1875
Closes #1873
  • Loading branch information
mipo256 authored and schauder committed Sep 12, 2024
1 parent 44f96b8 commit 1b9b9b2
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import microsoft.sql.DateTimeOffset;

import java.time.Instant;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -31,6 +32,7 @@
*
* @author Jens Schauder
* @author Christoph Strobl
* @author Mikhail Polivakha
* @since 2.3
*/
public class JdbcSqlServerDialect extends SqlServerDialect {
Expand All @@ -42,6 +44,7 @@ public Collection<Object> getConverters() {

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

Expand All @@ -55,4 +58,15 @@ public OffsetDateTime convert(DateTimeOffset source) {
return source.getOffsetDateTime();
}
}

@ReadingConverter
enum DateTimeOffsetToInstantConverter implements Converter<DateTimeOffset, Instant> {

INSTANCE;

@Override
public Instant convert(DateTimeOffset source) {
return source.getOffsetDateTime().toInstant();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.springframework.data.jdbc.core.dialect;

import java.time.Instant;
import java.util.List;

import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.data.jdbc.core.convert.JdbcCustomConversions;

/**
* Tests for {@link JdbcSqlServerDialect}
*
* @author Mikhail Polivakha
*/
class JdbcSqlServerDialectTest {

@Test
void testCustomConversions() {
JdbcCustomConversions jdbcCustomConversions = new JdbcCustomConversions(
(List<?>) JdbcSqlServerDialect.INSTANCE.getConverters());

Assertions
.assertThat(jdbcCustomConversions.hasCustomReadTarget(microsoft.sql.DateTimeOffset.class, Instant.class))
.isTrue();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
// required twice as the annotation lookup doesn't merge multiple occurences of the same annotation
// required twice as the annotation lookup doesn't merge multiple occurrences of the same annotation
@ContextCustomizerFactories(value = { TestClassCustomizerFactory.class, EnabledOnDatabaseCustomizerFactory.class })
@Documented
@Inherited
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
* @see EnabledOnDatabase
*/
@TestExecutionListeners(value = AssumeFeatureTestExecutionListener.class, mergeMode = MERGE_WITH_DEFAULTS)
// required twice as the annotation lookup doesn't merge multiple occurences of the same annotation
// required twice as the annotation lookup doesn't merge multiple occurrences of the same annotation
@ContextCustomizerFactories(value = { TestClassCustomizerFactory.class, EnabledOnDatabaseCustomizerFactory.class })
@ActiveProfiles(resolver = CombiningActiveProfileResolver.class)
@ExtendWith(SpringExtension.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,10 @@ public Position getClausePosition() {

@Override
public String getLock(LockOptions lockOptions) {
switch (lockOptions.getLockMode()) {

case PESSIMISTIC_WRITE:
return "WITH (UPDLOCK, ROWLOCK)";

case PESSIMISTIC_READ:
return "WITH (HOLDLOCK, ROWLOCK)";

default:
return "";
}
return switch (lockOptions.getLockMode()) {
case PESSIMISTIC_WRITE -> "WITH (UPDLOCK, ROWLOCK)";
case PESSIMISTIC_READ -> "WITH (HOLDLOCK, ROWLOCK)";
};
}

@Override
Expand Down

0 comments on commit 1b9b9b2

Please sign in to comment.