Skip to content

Added converter for SQL Server dialect #1875

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading