Skip to content

Simplify handling of Instant and OffsetDateTime and remove incorrect/dubious comments about JDBC 4.2 #10104

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 @@ -84,14 +84,9 @@ protected void doBind(
int index,
WrapperOptions wrapperOptions) throws SQLException {
final Instant instant = javaType.unwrap( value, Instant.class, wrapperOptions );
try {
// supposed to be supported in JDBC 4.2
st.setObject( index, instant, Types.TIMESTAMP_WITH_TIMEZONE );
}
catch (SQLException|AbstractMethodError e) {
// fall back to treating it as a JDBC Timestamp
st.setTimestamp( index, Timestamp.from( instant ), UTC_CALENDAR );
}
// some jdbc drivers may support java.time.Instant directly but
// this is the only standard support in the jdbc 4.2 specification
st.setTimestamp( index, Timestamp.from( instant ), UTC_CALENDAR );
}

@Override
Expand All @@ -102,14 +97,9 @@ protected void doBind(
WrapperOptions wrapperOptions)
throws SQLException {
final Instant instant = javaType.unwrap( value, Instant.class, wrapperOptions );
try {
// supposed to be supported in JDBC 4.2
st.setObject( name, instant, Types.TIMESTAMP_WITH_TIMEZONE );
}
catch (SQLException|AbstractMethodError e) {
// fall back to treating it as a JDBC Timestamp
st.setTimestamp( name, Timestamp.from( instant ), UTC_CALENDAR );
}
// some jdbc drivers may support java.time.Instant directly but
// this is the only standard support in the jdbc 4.2 specification
st.setTimestamp( name, Timestamp.from( instant ), UTC_CALENDAR );
}
};
}
Expand All @@ -119,38 +109,23 @@ public <X> ValueExtractor<X> getExtractor(final JavaType<X> javaType) {
return new BasicExtractor<>( javaType, this ) {
@Override
protected X doExtract(ResultSet rs, int position, WrapperOptions wrapperOptions) throws SQLException {
try {
// supposed to be supported in JDBC 4.2
return javaType.wrap( rs.getObject( position, Instant.class ), wrapperOptions );
}
catch (SQLException|AbstractMethodError e) {
// fall back to treating it as a JDBC Timestamp
return javaType.wrap( rs.getTimestamp( position, UTC_CALENDAR ), wrapperOptions );
}
// some jdbc drivers may support java.time.Instant directly but
// this is the only standard support in the jdbc 4.2 specification
return javaType.wrap( rs.getTimestamp( position, UTC_CALENDAR ), wrapperOptions );
}

@Override
protected X doExtract(CallableStatement statement, int position, WrapperOptions wrapperOptions) throws SQLException {
try {
// supposed to be supported in JDBC 4.2
return javaType.wrap( statement.getObject( position, Instant.class ), wrapperOptions );
}
catch (SQLException|AbstractMethodError e) {
// fall back to treating it as a JDBC Timestamp
return javaType.wrap( statement.getTimestamp( position, UTC_CALENDAR ), wrapperOptions );
}
// some jdbc drivers may support java.time.Instant directly but
// this is the only standard support in the jdbc 4.2 specification
return javaType.wrap( statement.getTimestamp( position, UTC_CALENDAR ), wrapperOptions );
}

@Override
protected X doExtract(CallableStatement statement, String name, WrapperOptions wrapperOptions) throws SQLException {
try {
// supposed to be supported in JDBC 4.2
return javaType.wrap( statement.getObject( name, Instant.class ), wrapperOptions );
}
catch (SQLException|AbstractMethodError e) {
// fall back to treating it as a JDBC Timestamp
return javaType.wrap( statement.getTimestamp( name, UTC_CALENDAR ), wrapperOptions );
}
// some jdbc drivers may support java.time.Instant directly but
// this is the only standard support in the jdbc 4.2 specification
return javaType.wrap( statement.getTimestamp( name, UTC_CALENDAR ), wrapperOptions );
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ protected void doBind(
int index,
WrapperOptions wrapperOptions) throws SQLException {
final OffsetDateTime dateTime = javaType.unwrap( value, OffsetDateTime.class, wrapperOptions );
// supposed to be supported in JDBC 4.2
st.setObject( index, dateTime.withOffsetSameInstant( ZoneOffset.UTC ), Types.TIMESTAMP_WITH_TIMEZONE );
}

Expand All @@ -95,7 +94,6 @@ protected void doBind(
WrapperOptions wrapperOptions)
throws SQLException {
final OffsetDateTime dateTime = javaType.unwrap( value, OffsetDateTime.class, wrapperOptions );
// supposed to be supported in JDBC 4.2
st.setObject( name, dateTime.withOffsetSameInstant( ZoneOffset.UTC ), Types.TIMESTAMP_WITH_TIMEZONE );
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.time.OffsetDateTime;
import java.util.Calendar;

/**
* Descriptor for {@link Types#TIMESTAMP_WITH_TIMEZONE TIMESTAMP_WITH_TIMEZONE} handling.
Expand Down Expand Up @@ -74,24 +72,8 @@ protected void doBind(
X value,
int index,
WrapperOptions options) throws SQLException {
try {
final OffsetDateTime dateTime = javaType.unwrap( value, OffsetDateTime.class, options );
// supposed to be supported in JDBC 4.2
st.setObject( index, dateTime, Types.TIMESTAMP_WITH_TIMEZONE );
}
catch (SQLException|AbstractMethodError e) {
// fall back to treating it as a JDBC Timestamp
final Timestamp timestamp = javaType.unwrap( value, Timestamp.class, options );
if ( value instanceof Calendar calendar ) {
st.setTimestamp( index, timestamp, calendar );
}
else if ( options.getJdbcTimeZone() != null ) {
st.setTimestamp( index, timestamp, Calendar.getInstance( options.getJdbcTimeZone() ) );
}
else {
st.setTimestamp( index, timestamp );
}
}
final OffsetDateTime dateTime = javaType.unwrap( value, OffsetDateTime.class, options );
st.setObject( index, dateTime, Types.TIMESTAMP_WITH_TIMEZONE );
}

@Override
Expand All @@ -101,24 +83,8 @@ protected void doBind(
String name,
WrapperOptions options)
throws SQLException {
try {
final OffsetDateTime dateTime = javaType.unwrap( value, OffsetDateTime.class, options );
// supposed to be supported in JDBC 4.2
st.setObject( name, dateTime, Types.TIMESTAMP_WITH_TIMEZONE );
}
catch (SQLException|AbstractMethodError e) {
// fall back to treating it as a JDBC Timestamp
final Timestamp timestamp = javaType.unwrap( value, Timestamp.class, options );
if ( value instanceof Calendar calendar ) {
st.setTimestamp( name, timestamp, calendar );
}
else if ( options.getJdbcTimeZone() != null ) {
st.setTimestamp( name, timestamp, Calendar.getInstance( options.getJdbcTimeZone() ) );
}
else {
st.setTimestamp( name, timestamp );
}
}
final OffsetDateTime dateTime = javaType.unwrap( value, OffsetDateTime.class, options );
st.setObject( name, dateTime, Types.TIMESTAMP_WITH_TIMEZONE );
}
};
}
Expand All @@ -128,44 +94,17 @@ public <X> ValueExtractor<X> getExtractor(final JavaType<X> javaType) {
return new BasicExtractor<>( javaType, this ) {
@Override
protected X doExtract(ResultSet rs, int position, WrapperOptions options) throws SQLException {
try {
// supposed to be supported in JDBC 4.2
return javaType.wrap( rs.getObject( position, OffsetDateTime.class ), options );
}
catch (SQLException|AbstractMethodError e) {
// fall back to treating it as a JDBC Timestamp
return options.getJdbcTimeZone() != null ?
javaType.wrap( rs.getTimestamp( position, Calendar.getInstance( options.getJdbcTimeZone() ) ), options ) :
javaType.wrap( rs.getTimestamp( position ), options );
}
return javaType.wrap( rs.getObject( position, OffsetDateTime.class ), options );
}

@Override
protected X doExtract(CallableStatement statement, int position, WrapperOptions options) throws SQLException {
try {
// supposed to be supported in JDBC 4.2
return javaType.wrap( statement.getObject( position, OffsetDateTime.class ), options );
}
catch (SQLException|AbstractMethodError e) {
// fall back to treating it as a JDBC Timestamp
return options.getJdbcTimeZone() != null ?
javaType.wrap( statement.getTimestamp( position, Calendar.getInstance( options.getJdbcTimeZone() ) ), options ) :
javaType.wrap( statement.getTimestamp( position ), options );
}
return javaType.wrap( statement.getObject( position, OffsetDateTime.class ), options );
}

@Override
protected X doExtract(CallableStatement statement, String name, WrapperOptions options) throws SQLException {
try {
// supposed to be supported in JDBC 4.2
return javaType.wrap( statement.getObject( name, OffsetDateTime.class ), options );
}
catch (SQLException|AbstractMethodError e) {
// fall back to treating it as a JDBC Timestamp
return options.getJdbcTimeZone() != null ?
javaType.wrap( statement.getTimestamp( name, Calendar.getInstance( options.getJdbcTimeZone() ) ), options ) :
javaType.wrap( statement.getTimestamp( name ), options );
}
return javaType.wrap( statement.getObject( name, OffsetDateTime.class ), options );
}
};
}
Expand Down
Loading