Add converter for microsoft.sql.DateTimeOffset.

Original pull request #1875
Closes #1873
This commit is contained in:
Mikhail2048
2024-09-07 10:30:54 +03:00
committed by Jens Schauder
parent 44f96b86f7
commit 1b9b9b2ed2
5 changed files with 46 additions and 13 deletions

View File

@@ -17,6 +17,7 @@ package org.springframework.data.jdbc.core.dialect;
import microsoft.sql.DateTimeOffset;
import java.time.Instant;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.Collection;
@@ -31,6 +32,7 @@ import org.springframework.data.relational.core.dialect.SqlServerDialect;
*
* @author Jens Schauder
* @author Christoph Strobl
* @author Mikhail Polivakha
* @since 2.3
*/
public class JdbcSqlServerDialect extends SqlServerDialect {
@@ -42,6 +44,7 @@ public class JdbcSqlServerDialect extends SqlServerDialect {
List<Object> converters = new ArrayList<>(super.getConverters());
converters.add(DateTimeOffsetToOffsetDateTimeConverter.INSTANCE);
converters.add(DateTimeOffsetToInstantConverter.INSTANCE);
return converters;
}
@@ -55,4 +58,15 @@ public class JdbcSqlServerDialect extends SqlServerDialect {
return source.getOffsetDateTime();
}
}
@ReadingConverter
enum DateTimeOffsetToInstantConverter implements Converter<DateTimeOffset, Instant> {
INSTANCE;
@Override
public Instant convert(DateTimeOffset source) {
return source.getOffsetDateTime().toInstant();
}
}
}

View File

@@ -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();
}
}

View File

@@ -44,7 +44,7 @@ import org.springframework.test.context.ContextCustomizerFactories;
*/
@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

View File

@@ -49,7 +49,7 @@ import org.springframework.transaction.annotation.Transactional;
* @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)

View File

@@ -81,17 +81,10 @@ public class SqlServerDialect extends AbstractDialect {
@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