clean up JdbcXxxxJavaTypes

This commit is contained in:
Gavin King 2024-09-10 07:43:22 +02:00
parent 2e4c2ff565
commit 0dedc62270
3 changed files with 31 additions and 61 deletions

View File

@ -19,7 +19,6 @@ import java.util.Date;
import java.util.GregorianCalendar;
import org.hibernate.HibernateException;
import org.hibernate.internal.util.CharSequenceHelper;
import org.hibernate.sql.ast.spi.SqlAppender;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.jdbc.JdbcType;
@ -28,6 +27,8 @@ import org.hibernate.type.spi.TypeConfiguration;
import jakarta.persistence.TemporalType;
import static org.hibernate.internal.util.CharSequenceHelper.subSequence;
/**
* Descriptor for {@link java.sql.Date} handling.
*
@ -39,15 +40,13 @@ import jakarta.persistence.TemporalType;
public class JdbcDateJavaType extends AbstractTemporalJavaType<Date> {
public static final JdbcDateJavaType INSTANCE = new JdbcDateJavaType();
public static final String DATE_FORMAT = "dd MMMM yyyy";
/**
* Intended for use in reading HQL literals and writing SQL literals
*
* @see #DATE_FORMAT
* @see DateTimeFormatter#ISO_LOCAL_DATE
*/
@SuppressWarnings("unused")
public static final DateTimeFormatter LITERAL_FORMATTER = DateTimeFormatter.ISO_LOCAL_DATE;
private static final DateTimeFormatter ENCODED_FORMATTER = new DateTimeFormatterBuilder()
.append( DateTimeFormatter.ISO_DATE )
.optionalStart()
@ -156,16 +155,15 @@ public class JdbcDateJavaType extends AbstractTemporalJavaType<Date> {
}
private LocalDate unwrapLocalDate(Date value) {
return value instanceof java.sql.Date
? ( (java.sql.Date) value ).toLocalDate()
return value instanceof java.sql.Date date
? date.toLocalDate()
: new java.sql.Date( unwrapDateEpoch( value ) ).toLocalDate();
}
private java.sql.Date unwrapSqlDate(Date value) {
if ( value instanceof java.sql.Date ) {
final java.sql.Date sqlDate = (java.sql.Date) value;
final long dateEpoch = toDateEpoch( sqlDate.getTime() );
return dateEpoch == sqlDate.getTime() ? sqlDate : new java.sql.Date( dateEpoch );
if ( value instanceof java.sql.Date date ) {
final long dateEpoch = toDateEpoch( date.getTime() );
return dateEpoch == date.getTime() ? date : new java.sql.Date( dateEpoch );
}
return new java.sql.Date( unwrapDateEpoch( value ) );
@ -238,13 +236,7 @@ public class JdbcDateJavaType extends AbstractTemporalJavaType<Date> {
@Override
public Date fromEncodedString(CharSequence charSequence, int start, int end) {
try {
final TemporalAccessor accessor = ENCODED_FORMATTER.parse(
CharSequenceHelper.subSequence(
charSequence,
start,
end
)
);
final TemporalAccessor accessor = ENCODED_FORMATTER.parse( subSequence( charSequence, start, end ) );
return java.sql.Date.valueOf( accessor.query( LocalDate::from ) );
}
catch ( DateTimeParseException pe) {

View File

@ -21,7 +21,6 @@ import java.util.GregorianCalendar;
import org.hibernate.HibernateException;
import org.hibernate.dialect.Dialect;
import org.hibernate.internal.util.CharSequenceHelper;
import org.hibernate.sql.ast.spi.SqlAppender;
import org.hibernate.type.descriptor.DateTimeUtils;
import org.hibernate.type.descriptor.WrapperOptions;
@ -31,6 +30,8 @@ import org.hibernate.type.spi.TypeConfiguration;
import jakarta.persistence.TemporalType;
import static org.hibernate.internal.util.CharSequenceHelper.subSequence;
/**
* Descriptor for {@link Time} handling.
*
@ -42,19 +43,8 @@ import jakarta.persistence.TemporalType;
public class JdbcTimeJavaType extends AbstractTemporalJavaType<Date> {
public static final JdbcTimeJavaType INSTANCE = new JdbcTimeJavaType();
public static final String TIME_FORMAT = "HH:mm:ss.SSS";
public static final DateTimeFormatter LITERAL_FORMATTER = DateTimeFormatter.ISO_LOCAL_TIME;
/**
* Alias for {@link DateTimeFormatter#ISO_LOCAL_TIME}.
*
* Intended for use with logging
*
* @see #LITERAL_FORMATTER
*/
@SuppressWarnings("unused")
public static final DateTimeFormatter LOGGABLE_FORMATTER = DateTimeFormatter.ISO_LOCAL_TIME;
private static final DateTimeFormatter ENCODED_FORMATTER = new DateTimeFormatterBuilder()
.optionalStart()
.append( DateTimeFormatter.ISO_DATE )
@ -63,6 +53,7 @@ public class JdbcTimeJavaType extends AbstractTemporalJavaType<Date> {
.append( DateTimeFormatter.ISO_LOCAL_TIME )
.toFormatter();
public JdbcTimeJavaType() {
super( Time.class, TimeMutabilityPlan.INSTANCE );
}
@ -217,8 +208,8 @@ public class JdbcTimeJavaType extends AbstractTemporalJavaType<Date> {
@Override
public String toString(Date value) {
if ( value instanceof java.sql.Time ) {
return LITERAL_FORMATTER.format( ( (java.sql.Time) value ).toLocalTime() );
if ( value instanceof java.sql.Time time ) {
return LITERAL_FORMATTER.format( time.toLocalTime() );
}
else {
return LITERAL_FORMATTER.format( LocalTime.ofInstant( value.toInstant(), ZoneOffset.systemDefault() ) );
@ -242,13 +233,7 @@ public class JdbcTimeJavaType extends AbstractTemporalJavaType<Date> {
@Override
public Date fromEncodedString(CharSequence charSequence, int start, int end) {
try {
final TemporalAccessor accessor = ENCODED_FORMATTER.parse(
CharSequenceHelper.subSequence(
charSequence,
start,
end
)
);
final TemporalAccessor accessor = ENCODED_FORMATTER.parse( subSequence( charSequence, start, end ) );
return java.sql.Time.valueOf( accessor.query( LocalTime::from ) );
}
catch ( DateTimeParseException pe) {
@ -258,8 +243,8 @@ public class JdbcTimeJavaType extends AbstractTemporalJavaType<Date> {
@Override
public void appendEncodedString(SqlAppender sb, Date value) {
if ( value instanceof java.sql.Time ) {
LITERAL_FORMATTER.formatTo( ( (java.sql.Time) value ).toLocalTime(), sb );
if ( value instanceof java.sql.Time time ) {
LITERAL_FORMATTER.formatTo( time.toLocalTime(), sb );
}
else {
LITERAL_FORMATTER.formatTo( LocalTime.ofInstant( value.toInstant(), ZoneOffset.systemDefault() ), sb );

View File

@ -25,7 +25,6 @@ import jakarta.persistence.TemporalType;
import org.hibernate.HibernateException;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.internal.util.CharSequenceHelper;
import org.hibernate.sql.ast.spi.SqlAppender;
import org.hibernate.type.descriptor.DateTimeUtils;
import org.hibernate.type.descriptor.WrapperOptions;
@ -33,6 +32,8 @@ import org.hibernate.type.descriptor.jdbc.JdbcType;
import org.hibernate.type.descriptor.jdbc.JdbcTypeIndicators;
import org.hibernate.type.spi.TypeConfiguration;
import static org.hibernate.internal.util.CharSequenceHelper.subSequence;
/**
* Descriptor for {@link Timestamp} handling.
*
@ -51,7 +52,6 @@ public class JdbcTimestampJavaType extends AbstractTemporalJavaType<Date> implem
*
* @see #TIMESTAMP_FORMAT
*/
@SuppressWarnings("unused")
public static final DateTimeFormatter LITERAL_FORMATTER = DateTimeFormatter.ofPattern( TIMESTAMP_FORMAT )
.withZone( ZoneId.from( ZoneOffset.UTC ) );
@ -231,13 +231,7 @@ public class JdbcTimestampJavaType extends AbstractTemporalJavaType<Date> implem
@Override
public Date fromEncodedString(CharSequence charSequence, int start, int end) {
try {
final TemporalAccessor accessor = DateTimeUtils.DATE_TIME.parse(
CharSequenceHelper.subSequence(
charSequence,
start,
end
)
);
final TemporalAccessor accessor = DateTimeUtils.DATE_TIME.parse( subSequence( charSequence, start, end ) );
final Timestamp timestamp;
if ( accessor.isSupported( ChronoField.INSTANT_SECONDS ) ) {
timestamp = new Timestamp( accessor.getLong( ChronoField.INSTANT_SECONDS ) * 1000L );
@ -297,11 +291,10 @@ public class JdbcTimestampJavaType extends AbstractTemporalJavaType<Date> implem
public static final TimestampMutabilityPlan INSTANCE = new TimestampMutabilityPlan();
@Override
public Date deepCopyNotNull(Date value) {
if ( value instanceof Timestamp ) {
if ( value instanceof Timestamp timestamp ) {
// make sure to get the nanos
final Timestamp orig = (Timestamp) value;
final Timestamp copy = new Timestamp( orig.getTime() );
copy.setNanos( orig.getNanos() );
final Timestamp copy = new Timestamp( timestamp.getTime() );
copy.setNanos( timestamp.getNanos() );
return copy;
}
else {