From c0da20c0283423527e16ba8f5ca15fd61df3f4dd Mon Sep 17 00:00:00 2001 From: "David M. Lloyd" Date: Wed, 13 Dec 2023 10:21:46 -0600 Subject: [PATCH] Fix test by adding ad-hoc `LocalDate` support Fixes #37659. A follow-up issue will be filed to revert this commit once ORM 7.x is integrated. --- .../orm/graphql/panache/Author.java | 3 + .../graphql/panache/LocalDateJdbcType.java | 104 ++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 integration-tests/hibernate-orm-graphql-panache/src/main/java/io/quarkus/it/hibertnate/orm/graphql/panache/LocalDateJdbcType.java diff --git a/integration-tests/hibernate-orm-graphql-panache/src/main/java/io/quarkus/it/hibertnate/orm/graphql/panache/Author.java b/integration-tests/hibernate-orm-graphql-panache/src/main/java/io/quarkus/it/hibertnate/orm/graphql/panache/Author.java index 0e8ce19a4ada3..543c72d29b885 100644 --- a/integration-tests/hibernate-orm-graphql-panache/src/main/java/io/quarkus/it/hibertnate/orm/graphql/panache/Author.java +++ b/integration-tests/hibernate-orm-graphql-panache/src/main/java/io/quarkus/it/hibertnate/orm/graphql/panache/Author.java @@ -4,11 +4,14 @@ import jakarta.persistence.Entity; +import org.hibernate.annotations.JdbcType; + import io.quarkus.hibernate.orm.panache.PanacheEntity; @Entity public class Author extends PanacheEntity { public String name; + @JdbcType(LocalDateJdbcType.class) public LocalDate dob; } diff --git a/integration-tests/hibernate-orm-graphql-panache/src/main/java/io/quarkus/it/hibertnate/orm/graphql/panache/LocalDateJdbcType.java b/integration-tests/hibernate-orm-graphql-panache/src/main/java/io/quarkus/it/hibertnate/orm/graphql/panache/LocalDateJdbcType.java new file mode 100644 index 0000000000000..db6f3d4914436 --- /dev/null +++ b/integration-tests/hibernate-orm-graphql-panache/src/main/java/io/quarkus/it/hibertnate/orm/graphql/panache/LocalDateJdbcType.java @@ -0,0 +1,104 @@ +package io.quarkus.it.hibertnate.orm.graphql.panache; + +import java.io.Serial; +import java.sql.CallableStatement; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Types; +import java.time.LocalDate; + +import jakarta.persistence.TemporalType; + +import org.hibernate.type.descriptor.ValueBinder; +import org.hibernate.type.descriptor.ValueExtractor; +import org.hibernate.type.descriptor.WrapperOptions; +import org.hibernate.type.descriptor.java.JavaType; +import org.hibernate.type.descriptor.jdbc.BasicBinder; +import org.hibernate.type.descriptor.jdbc.BasicExtractor; +import org.hibernate.type.descriptor.jdbc.JdbcLiteralFormatter; +import org.hibernate.type.descriptor.jdbc.JdbcType; +import org.hibernate.type.descriptor.jdbc.internal.JdbcLiteralFormatterTemporal; +import org.hibernate.type.spi.TypeConfiguration; + +/** + * A "correct" local date JDBC type. + */ +public final class LocalDateJdbcType implements JdbcType { + @Serial + private static final long serialVersionUID = -3375371178728213643L; + + public static final LocalDateJdbcType INSTANCE = new LocalDateJdbcType(); + + private LocalDateJdbcType() { + } + + @Override + public int getJdbcTypeCode() { + return Types.DATE; + } + + @Override + public String getFriendlyName() { + return "DATE"; + } + + @Override + public String toString() { + return "LocalDateTypeDescriptor"; + } + + @Override + public JavaType getJdbcRecommendedJavaTypeMapping( + Integer length, + Integer scale, + TypeConfiguration typeConfiguration) { + return typeConfiguration.getJavaTypeRegistry().getDescriptor(LocalDate.class); + } + + @Override + public JdbcLiteralFormatter getJdbcLiteralFormatter(JavaType javaType) { + return new JdbcLiteralFormatterTemporal<>(javaType, TemporalType.DATE); + } + + @Override + public Class getPreferredJavaTypeClass(WrapperOptions options) { + return LocalDate.class; + } + + @Override + public ValueBinder getBinder(final JavaType javaType) { + return new BasicBinder<>(javaType, this) { + @Override + protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException { + st.setObject(index, javaType.unwrap(value, LocalDate.class, options)); + } + + @Override + protected void doBind(CallableStatement st, X value, String name, WrapperOptions options) + throws SQLException { + st.setObject(name, javaType.unwrap(value, LocalDate.class, options)); + } + }; + } + + @Override + public ValueExtractor getExtractor(final JavaType javaType) { + return new BasicExtractor<>(javaType, this) { + @Override + protected X doExtract(ResultSet rs, int paramIndex, WrapperOptions options) throws SQLException { + return javaType.wrap(rs.getObject(paramIndex, LocalDate.class), options); + } + + @Override + protected X doExtract(CallableStatement statement, int index, WrapperOptions options) throws SQLException { + return javaType.wrap(statement.getObject(index, LocalDate.class), options); + } + + @Override + protected X doExtract(CallableStatement statement, String name, WrapperOptions options) throws SQLException { + return javaType.wrap(statement.getObject(name, LocalDate.class), options); + } + }; + } +}