Skip to content

Support for Dialect specific custom conversions and OffsetDateTime. #981

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 5 commits into from

Conversation

schauder
Copy link
Contributor

@schauder schauder commented May 19, 2021

Dialects may now register a list of converters to take into consideration when reading or writing properties.

See JdbcSqlServerDialect for an example.

By default OffsetDateTime does not get converted anymore.
If a database needs a conversion it can register it by implementing Dialect.getConverters() as described above.

Closes #935

schauder added 3 commits May 19, 2021 07:52
Dialects may now register a list of converters to take into consideration when reading or writing properties.

See `JdbcSqlServerDialect` for an example.

By default `OffsetDateTime` does not get converted anymore.
If a database needs a conversion it can register it by implementing `Dialect.getConverters()` as described above.

Closes #935
Removes superfluous method.
@schauder schauder requested a review from mp911de May 19, 2021 09:42
@@ -96,8 +101,22 @@ public JdbcConverter jdbcConverter(JdbcMappingContext mappingContext, NamedParam
* @return will never be {@literal null}.
*/
@Bean
public JdbcCustomConversions jdbcCustomConversions() {
return new JdbcCustomConversions();
public JdbcCustomConversions jdbcCustomConversions(Dialect dialect) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A potential way out could be injecting ApplicationContext into this class making it ApplicationContextAware and handle the lookup through context.getBean(…).


import org.junit.jupiter.api.Test;

/*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Something's off with formatting here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly? I double checked my configuration, and don't see the mistake.

*
* @return a collection of converters for this dialect.
*/
default Collection<Object> getConverters() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Member

@mp911de mp911de left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left a few comments, mostly about style. It would be a good idea to document that some dialects use driver-specific API and that these classes need to be present.

* @since 2.3
*/
@ReadingConverter
enum Timestamp2OffsetDateTimeConverter implements Converter<Timestamp, OffsetDateTime> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about TimestampAtUtcToOffsetDateTimeConverter to make it more descriptive than Timestamp2.

* @since 2.3
*/
@WritingConverter
enum OffsetDateTime2TimestampConverter implements Converter<OffsetDateTime, Timestamp> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OffsetDateTimeToTimestampConverter

import org.springframework.data.relational.core.dialect.SqlServerDialect;

/**
* {@link Db2Dialect} that registers JDBC specific converters.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: SQL Server

}

@ReadingConverter
enum DateTimeOffset2OffsetDateTimeConverter implements Converter<DateTimeOffset, OffsetDateTime> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DateTimeOffsetToOffsetDateTimeConverter

}

@WritingConverter
enum OffsetDateTime2TimestampJdbcValueConverter implements Converter<OffsetDateTime, JdbcValue> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OffsetDateTimeToJdbcValueConverter

}

@ReadingConverter
enum TimestampWithTimeZone2OffsetDateTimeConverter implements Converter<TimestampWithTimeZone, OffsetDateTime> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TimestampWithTimeZoneToOffsetDateTimeConverter

@Override
public Collection<Object> getConverters() {

ArrayList<Object> converters = new ArrayList<>(super.getConverters());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: List<Object> instead of ArrayList

import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.*;

/*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Formatting

Originial pull request #981
See #935
Dialects now can define a set of known simple types the driver can handle without further interaction. This is done to avoid warnings during converter registration for types known in one environment but not the other.
Also move types around a bit, change visibility and make sure jdbc specific dialects inherit converters from their parents.
schauder added a commit that referenced this pull request May 31, 2021
Dialects may now register a list of converters to take into consideration when reading or writing properties.

See `JdbcSqlServerDialect` for an example.

By default `OffsetDateTime` does not get converted anymore.
If a database needs a conversion it can register it by implementing `Dialect.getConverters()` as described above.

Closes #935
Original pull request #981
schauder added a commit that referenced this pull request May 31, 2021
Removes superfluous method.
Applied feedback from review: Formatting, Naming, and improved backward compatibility.

Originial pull request #981
See #935
schauder pushed a commit that referenced this pull request May 31, 2021
Dialects now can define a set of known simple types the driver can handle without further interaction. This is done to avoid warnings during converter registration for types known in one environment but not the other.
Also move types around a bit, change visibility and make sure jdbc specific dialects inherit converters from their parents.

Original pull request #981
See #935
@schauder
Copy link
Contributor Author

That's merged into main.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

spring-data-jdbc and postgres: Trailing junk on timestamp
3 participants