-
Notifications
You must be signed in to change notification settings - Fork 356
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
Changes from 3 commits
d3fba89
60c0e6e
42df931
2e88a32
e35c4e9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
* Copyright 2021 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.jdbc.core.dialect; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
|
||
import org.springframework.data.relational.core.dialect.Db2Dialect; | ||
import org.springframework.data.relational.core.sql.IdentifierProcessing; | ||
|
||
/** | ||
* {@link Db2Dialect} that registers JDBC specific converters. | ||
* | ||
* @author Jens Schauder | ||
* @since 2.3 | ||
*/ | ||
public class JdbcDb2Dialect extends Db2Dialect { | ||
|
||
public static JdbcDb2Dialect INSTANCE = new JdbcDb2Dialect(); | ||
|
||
@Override | ||
public Collection<Object> getConverters() { | ||
|
||
ArrayList<Object> converters = new ArrayList<>(super.getConverters()); | ||
converters.add(OffsetDateTime2TimestampConverter.INSTANCE); | ||
|
||
return converters; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* Copyright 2021 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.jdbc.core.dialect; | ||
|
||
import org.h2.api.TimestampWithTimeZone; | ||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.data.convert.ReadingConverter; | ||
import org.springframework.data.relational.core.dialect.Db2Dialect; | ||
import org.springframework.data.relational.core.dialect.H2Dialect; | ||
|
||
import java.time.OffsetDateTime; | ||
import java.time.ZoneOffset; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
/** | ||
* {@link Db2Dialect} that registers JDBC specific converters. | ||
* | ||
* @author Jens Schauder | ||
* @since 2.3 | ||
*/ | ||
public class JdbcH2Dialect extends H2Dialect { | ||
|
||
public static JdbcH2Dialect INSTANCE = new JdbcH2Dialect(); | ||
|
||
@Override | ||
public Collection<Object> getConverters() { | ||
return Collections.singletonList(TimestampWithTimeZone2OffsetDateTimeConverter.INSTANCE); | ||
} | ||
|
||
@ReadingConverter | ||
enum TimestampWithTimeZone2OffsetDateTimeConverter implements Converter<TimestampWithTimeZone, OffsetDateTime> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
INSTANCE; | ||
|
||
|
||
@Override | ||
public OffsetDateTime convert(TimestampWithTimeZone source) { | ||
|
||
long nanosInSecond = 1_000_000_000; | ||
long nanosInMinute = nanosInSecond * 60; | ||
long nanosInHour = nanosInMinute * 60; | ||
|
||
long hours = (source.getNanosSinceMidnight() / nanosInHour); | ||
|
||
long nanosInHours = hours * nanosInHour; | ||
long nanosLeft = source.getNanosSinceMidnight() - nanosInHours; | ||
long minutes = nanosLeft / nanosInMinute; | ||
|
||
long nanosInMinutes = minutes * nanosInMinute; | ||
nanosLeft -= nanosInMinutes; | ||
long seconds = nanosLeft / nanosInSecond; | ||
|
||
long nanosInSeconds = seconds * nanosInSecond; | ||
nanosLeft -= nanosInSeconds; | ||
ZoneOffset offset = ZoneOffset.ofTotalSeconds(source.getTimeZoneOffsetSeconds()); | ||
|
||
return OffsetDateTime.of(source.getYear(), source.getMonth(), source.getDay(), (int)hours, (int)minutes, (int)seconds, (int)nanosLeft, offset ); | ||
|
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2021 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.jdbc.core.dialect; | ||
|
||
import java.sql.JDBCType; | ||
import java.time.OffsetDateTime; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
|
||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.data.convert.WritingConverter; | ||
import org.springframework.data.jdbc.core.convert.JdbcValue; | ||
import org.springframework.data.relational.core.dialect.Db2Dialect; | ||
import org.springframework.data.relational.core.dialect.MySqlDialect; | ||
import org.springframework.data.relational.core.sql.IdentifierProcessing; | ||
|
||
/** | ||
* {@link Db2Dialect} that registers JDBC specific converters. | ||
* | ||
* @author Jens Schauder | ||
* @since 2.3 | ||
*/ | ||
public class JdbcMySqlDialect extends MySqlDialect { | ||
|
||
public JdbcMySqlDialect(IdentifierProcessing identifierProcessing) { | ||
super(identifierProcessing); | ||
} | ||
|
||
@Override | ||
public Collection<Object> getConverters() { | ||
|
||
ArrayList<Object> converters = new ArrayList<>(super.getConverters()); | ||
converters.add(OffsetDateTime2TimestampJdbcValueConverter.INSTANCE); | ||
|
||
return converters; | ||
} | ||
|
||
@WritingConverter | ||
enum OffsetDateTime2TimestampJdbcValueConverter implements Converter<OffsetDateTime, JdbcValue> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
INSTANCE; | ||
|
||
@Override | ||
public JdbcValue convert(OffsetDateTime source) { | ||
return JdbcValue.of(source, JDBCType.TIMESTAMP); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright 2021 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.jdbc.core.dialect; | ||
|
||
import microsoft.sql.DateTimeOffset; | ||
|
||
import java.time.OffsetDateTime; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
|
||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.data.convert.ReadingConverter; | ||
import org.springframework.data.relational.core.dialect.Db2Dialect; | ||
import org.springframework.data.relational.core.dialect.SqlServerDialect; | ||
|
||
/** | ||
* {@link Db2Dialect} that registers JDBC specific converters. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: SQL Server |
||
* | ||
* @author Jens Schauder | ||
* @since 2.3 | ||
*/ | ||
public class JdbcSqlServerDialect extends SqlServerDialect { | ||
|
||
public static JdbcSqlServerDialect INSTANCE = new JdbcSqlServerDialect(); | ||
|
||
@Override | ||
public Collection<Object> getConverters() { | ||
return Collections.singletonList(DateTimeOffset2OffsetDateTimeConverter.INSTANCE); | ||
} | ||
|
||
@ReadingConverter | ||
enum DateTimeOffset2OffsetDateTimeConverter implements Converter<DateTimeOffset, OffsetDateTime> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
INSTANCE; | ||
|
||
@Override | ||
public OffsetDateTime convert(DateTimeOffset source) { | ||
return source.getOffsetDateTime(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2021 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.data.jdbc.core.dialect; | ||
|
||
import org.springframework.core.convert.converter.Converter; | ||
import org.springframework.data.convert.WritingConverter; | ||
import org.springframework.data.relational.core.dialect.Db2Dialect; | ||
|
||
import java.sql.Timestamp; | ||
import java.time.OffsetDateTime; | ||
import java.time.ZoneOffset; | ||
|
||
/** | ||
* {@link WritingConverter} from {@link OffsetDateTime} to {@link Timestamp}. | ||
* The conversion preserves the {@link java.time.Instant} represented by {@link OffsetDateTime} | ||
* | ||
* @author Jens Schauder | ||
* @since 2.3 | ||
*/ | ||
@WritingConverter | ||
enum OffsetDateTime2TimestampConverter implements Converter<OffsetDateTime, Timestamp> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
INSTANCE; | ||
@Override | ||
public Timestamp convert(OffsetDateTime source) { | ||
return Timestamp.from(source.toInstant()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
List<Object>
instead ofArrayList