Skip to content

Commit

Permalink
Apply float/double type related changes to properly preserve precisio…
Browse files Browse the repository at this point in the history
…n according to @gavinking
  • Loading branch information
beikov committed Jul 28, 2021
1 parent bce5990 commit 5a33db1
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 1 deletion.
Expand Up @@ -25,7 +25,7 @@ public class DoubleType
public static final Double ZERO = 0.0;

public DoubleType() {
super( org.hibernate.type.descriptor.jdbc.FloatTypeDescriptor.INSTANCE, DoubleTypeDescriptor.INSTANCE );
super( org.hibernate.type.descriptor.jdbc.DoubleSqlFloatTypeDescriptor.INSTANCE, DoubleTypeDescriptor.INSTANCE );
}
@Override
public String getName() {
Expand Down
Expand Up @@ -13,6 +13,8 @@
import org.hibernate.dialect.Dialect;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.spi.Primitive;
import org.hibernate.type.descriptor.jdbc.JdbcTypeDescriptor;
import org.hibernate.type.descriptor.jdbc.JdbcTypeDescriptorIndicators;

/**
* Descriptor for {@link Float} handling.
Expand All @@ -25,6 +27,12 @@ public class FloatTypeDescriptor extends AbstractClassTypeDescriptor<Float> impl
public FloatTypeDescriptor() {
super( Float.class );
}

@Override
public JdbcTypeDescriptor getRecommendedJdbcType(JdbcTypeDescriptorIndicators indicators) {
return org.hibernate.type.descriptor.jdbc.FloatTypeDescriptor.INSTANCE;
}

@Override
public String toString(Float value) {
return value == null ? null : value.toString();
Expand Down
@@ -0,0 +1,100 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.type.descriptor.jdbc;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;

import org.hibernate.type.descriptor.ValueBinder;
import org.hibernate.type.descriptor.ValueExtractor;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.BasicJavaDescriptor;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.jdbc.internal.JdbcLiteralFormatterNumericData;
import org.hibernate.type.spi.TypeConfiguration;

/**
* Descriptor for {@link Types#DOUBLE DOUBLE} handling.
*
* @author Steve Ebersole
*/
public class DoubleSqlFloatTypeDescriptor implements JdbcTypeDescriptor {
public static final DoubleSqlFloatTypeDescriptor INSTANCE = new DoubleSqlFloatTypeDescriptor();

public DoubleSqlFloatTypeDescriptor() {
}

@Override
public int getJdbcType() {
return Types.FLOAT;
}

@Override
public String getFriendlyName() {
return "DOUBLE";
}

@Override
public String toString() {
return "SqlTypeDescriptor(" + getFriendlyName() + ")";
}

@Override
public boolean canBeRemapped() {
return true;
}

@Override
public <T> BasicJavaDescriptor<T> getJdbcRecommendedJavaTypeMapping(TypeConfiguration typeConfiguration) {
return (BasicJavaDescriptor<T>) typeConfiguration.getJavaTypeDescriptorRegistry().getDescriptor( Double.class );
}

@Override
public <T> JdbcLiteralFormatter<T> getJdbcLiteralFormatter(JavaTypeDescriptor<T> javaTypeDescriptor) {
//noinspection unchecked
return new JdbcLiteralFormatterNumericData( javaTypeDescriptor, Double.class );
}

@Override
public <X> ValueBinder<X> getBinder(final JavaTypeDescriptor<X> javaTypeDescriptor) {
return new BasicBinder<X>( javaTypeDescriptor, this ) {
@Override
protected void doBind(PreparedStatement st, X value, int index, WrapperOptions options) throws SQLException {
st.setDouble( index, javaTypeDescriptor.unwrap( value, Double.class, options ) );
}

@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
st.setDouble( name, javaTypeDescriptor.unwrap( value, Double.class, options ) );
}
};
}

@Override
public <X> ValueExtractor<X> getExtractor(final JavaTypeDescriptor<X> javaTypeDescriptor) {
return new BasicExtractor<X>( javaTypeDescriptor, this ) {
@Override
protected X doExtract(ResultSet rs, int paramIndex, WrapperOptions options) throws SQLException {
return javaTypeDescriptor.wrap( rs.getDouble( paramIndex ), options );
}

@Override
protected X doExtract(CallableStatement statement, int index, WrapperOptions options) throws SQLException {
return javaTypeDescriptor.wrap( statement.getDouble( index ), options );
}

@Override
protected X doExtract(CallableStatement statement, String name, WrapperOptions options) throws SQLException {
return javaTypeDescriptor.wrap( statement.getDouble( name ), options );
}
};
}
}

2 comments on commit 5a33db1

@gavinking
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@beikov I don't understand how these changes related to the fix I already included in #4105.

@beikov
Copy link
Contributor Author

@beikov beikov commented on 5a33db1 Jul 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just about to ask you to review this or tell me how far your PR for this is :)

I can drop this commit after rebasing on top of your merged PR

Please sign in to comment.