Skip to content

Commit

Permalink
Fix test by adding ad-hoc LocalDate support
Browse files Browse the repository at this point in the history
Fixes quarkusio#37659. A follow-up issue will be filed to revert this commit once ORM 7.x is integrated.
  • Loading branch information
dmlloyd committed Dec 13, 2023
1 parent 3493670 commit c0da20c
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
@@ -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 <T> JavaType<T> getJdbcRecommendedJavaTypeMapping(
Integer length,
Integer scale,
TypeConfiguration typeConfiguration) {
return typeConfiguration.getJavaTypeRegistry().getDescriptor(LocalDate.class);
}

@Override
public <T> JdbcLiteralFormatter<T> getJdbcLiteralFormatter(JavaType<T> javaType) {
return new JdbcLiteralFormatterTemporal<>(javaType, TemporalType.DATE);
}

@Override
public Class<?> getPreferredJavaTypeClass(WrapperOptions options) {
return LocalDate.class;
}

@Override
public <X> ValueBinder<X> getBinder(final JavaType<X> 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 <X> ValueExtractor<X> getExtractor(final JavaType<X> 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);
}
};
}
}

0 comments on commit c0da20c

Please sign in to comment.