remove BitTypeDescriptor

This commit is contained in:
Gavin King 2021-01-23 13:23:03 +01:00 committed by Christian Beikov
parent 005c4cea63
commit fc3c20f669
8 changed files with 103 additions and 141 deletions

View File

@ -1014,8 +1014,6 @@ protected SqlTypeDescriptor getSqlTypeDescriptorOverride(final int sqlCode) {
case Types.TINYINT:
// tinyint is unsigned on HANA
return SmallIntTypeDescriptor.INSTANCE;
case Types.BOOLEAN:
return this.useLegacyBooleanType ? BitTypeDescriptor.INSTANCE : BooleanTypeDescriptor.INSTANCE;
case Types.VARCHAR:
return this.isUseUnicodeStringTypes() ? NVarcharTypeDescriptor.INSTANCE : VarcharTypeDescriptor.INSTANCE;
case Types.CHAR:

View File

@ -55,9 +55,7 @@
import org.hibernate.tool.schema.extract.internal.SequenceInformationExtractorOracleDatabaseImpl;
import org.hibernate.tool.schema.extract.spi.SequenceInformationExtractor;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.descriptor.sql.BitTypeDescriptor;
import org.hibernate.type.descriptor.sql.BlobTypeDescriptor;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
import java.sql.CallableStatement;
import java.sql.ResultSet;
@ -597,13 +595,6 @@ public boolean supportsBitType() {
return false;
}
@Override
protected SqlTypeDescriptor getSqlTypeDescriptorOverride(int sqlCode) {
return sqlCode == Types.BOOLEAN
? BitTypeDescriptor.INSTANCE
: super.getSqlTypeDescriptorOverride( sqlCode );
}
@Override
public void contributeTypes(TypeContributions typeContributions, ServiceRegistry serviceRegistry) {
super.contributeTypes( typeContributions, serviceRegistry );

View File

@ -18,8 +18,6 @@
import org.hibernate.sql.ast.spi.StandardSqlAstTranslatorFactory;
import org.hibernate.sql.ast.tree.Statement;
import org.hibernate.sql.exec.spi.JdbcOperation;
import org.hibernate.type.descriptor.sql.BitTypeDescriptor;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
import java.sql.Types;
@ -78,13 +76,6 @@ public boolean supportsTimezoneTypes() {
return true;
}
@Override
protected SqlTypeDescriptor getSqlTypeDescriptorOverride(int sqlCode) {
return sqlCode == Types.BOOLEAN
? BitTypeDescriptor.INSTANCE
: super.getSqlTypeDescriptorOverride( sqlCode );
}
@Override
public String currentDate() {
return "current date";

View File

@ -9,7 +9,6 @@
import org.hibernate.dialect.Dialect;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.descriptor.java.spi.Primitive;
import org.hibernate.type.descriptor.sql.BitTypeDescriptor;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
/**
@ -159,7 +158,7 @@ public int getDefaultSqlScale() {
@Override
public String getCheckCondition(String columnName, SqlTypeDescriptor sqlTypeDescriptor, Dialect dialect) {
return sqlTypeDescriptor instanceof BitTypeDescriptor && !dialect.supportsBitType()
return dialect.toBooleanValueString(true).equals("1") && !dialect.supportsBitType()
? columnName + " in (0,1)"
: null;
}

View File

@ -1,114 +0,0 @@
/*
* 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.sql;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.hibernate.dialect.Dialect;
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.sql.spi.BasicJdbcLiteralFormatter;
import org.hibernate.type.spi.TypeConfiguration;
/**
* Descriptor for {@link Types#BIT BIT} handling.
* <p/>
* Note that JDBC is very specific about its use of the type BIT to mean a single binary digit, whereas
* SQL defines BIT having a parameterized length.
*
* @author Steve Ebersole
*/
public class BitTypeDescriptor implements SqlTypeDescriptor {
public static final BitTypeDescriptor INSTANCE = new BitTypeDescriptor();
public BitTypeDescriptor() {
}
public int getSqlType() {
return Types.BIT;
}
@Override
public String getFriendlyName() {
return "BIT";
}
@Override
public String toString() {
return "BitTypeDescriptor";
}
@Override
public boolean canBeRemapped() {
return true;
}
@Override
public <T> BasicJavaDescriptor<T> getJdbcRecommendedJavaTypeMapping(TypeConfiguration typeConfiguration) {
return (BasicJavaDescriptor<T>) typeConfiguration.getJavaTypeDescriptorRegistry().getDescriptor( Boolean.class );
}
public <T> JdbcLiteralFormatter<T> getJdbcLiteralFormatter(JavaTypeDescriptor<T> javaTypeDescriptor) {
if ( javaTypeDescriptor.getJavaTypeClass().equals(Boolean.class) ) {
//this is to allow literals to be formatted correctly when
//we are in the legacy Boolean-to-BIT JDBC type mapping mode
return new BasicJdbcLiteralFormatter( javaTypeDescriptor ) {
@Override
public String toJdbcLiteral(Object value, Dialect dialect, WrapperOptions wrapperOptions) {
Boolean bool = unwrap( value, Boolean.class, wrapperOptions );
return bool ? "1" : "0";
}
};
}
else {
return (value, dialect, wrapperOptions) -> value.toString();
}
}
@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.setBoolean( index, javaTypeDescriptor.unwrap( value, Boolean.class, options ) );
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
st.setBoolean( name, javaTypeDescriptor.unwrap( value, Boolean.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.getBoolean( paramIndex ), options );
}
@Override
protected X doExtract(CallableStatement statement, int index, WrapperOptions options) throws SQLException {
return javaTypeDescriptor.wrap( statement.getBoolean( index ), options );
}
@Override
protected X doExtract(CallableStatement statement, String name, WrapperOptions options) throws SQLException {
return javaTypeDescriptor.wrap( statement.getBoolean( name ), options );
}
};
}
}

View File

@ -12,6 +12,7 @@
import java.sql.SQLException;
import java.sql.Types;
import org.hibernate.dialect.Dialect;
import org.hibernate.type.descriptor.ValueBinder;
import org.hibernate.type.descriptor.ValueExtractor;
import org.hibernate.type.descriptor.WrapperOptions;

View File

@ -8,7 +8,6 @@
import org.hibernate.type.descriptor.sql.BigIntTypeDescriptor;
import org.hibernate.type.descriptor.sql.BinaryTypeDescriptor;
import org.hibernate.type.descriptor.sql.BitTypeDescriptor;
import org.hibernate.type.descriptor.sql.BlobTypeDescriptor;
import org.hibernate.type.descriptor.sql.BooleanTypeDescriptor;
import org.hibernate.type.descriptor.sql.CharTypeDescriptor;
@ -46,8 +45,6 @@ public interface BaselineTarget {
public static void prime(BaselineTarget target) {
target.addDescriptor( BooleanTypeDescriptor .INSTANCE );
target.addDescriptor( BitTypeDescriptor.INSTANCE );
target.addDescriptor( BigIntTypeDescriptor.INSTANCE );
target.addDescriptor( DecimalTypeDescriptor.INSTANCE );
target.addDescriptor( DoubleTypeDescriptor.INSTANCE );

View File

@ -8,15 +8,19 @@
import java.math.BigDecimal;
import java.sql.Blob;
import java.sql.CallableStatement;
import java.sql.Clob;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.sql.Timestamp;
import java.sql.Types;
import java.time.Month;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import javax.persistence.Column;
@ -40,14 +44,21 @@
import org.hibernate.jpa.boot.spi.Bootstrap;
import org.hibernate.jpa.test.PersistenceUnitDescriptorAdapter;
import org.hibernate.type.AbstractSingleColumnStandardBasicType;
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.BigDecimalTypeDescriptor;
import org.hibernate.type.descriptor.java.BooleanTypeDescriptor;
import org.hibernate.type.descriptor.java.FloatTypeDescriptor;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.java.PrimitiveByteArrayTypeDescriptor;
import org.hibernate.type.descriptor.java.StringTypeDescriptor;
import org.hibernate.type.descriptor.sql.BasicBinder;
import org.hibernate.type.descriptor.sql.BasicExtractor;
import org.hibernate.type.descriptor.sql.BinaryTypeDescriptor;
import org.hibernate.type.descriptor.sql.BitTypeDescriptor;
import org.hibernate.type.descriptor.sql.CharTypeDescriptor;
import org.hibernate.type.descriptor.sql.JdbcLiteralFormatter;
import org.hibernate.type.descriptor.sql.NumericTypeDescriptor;
import org.hibernate.testing.RequiresDialect;
@ -55,6 +66,9 @@
import org.hibernate.testing.TestForIssue;
import org.hibernate.testing.junit4.CustomRunner;
import org.hibernate.testing.transaction.TransactionUtil;
import org.hibernate.type.descriptor.sql.SqlTypeDescriptor;
import org.hibernate.type.descriptor.sql.spi.BasicJdbcLiteralFormatter;
import org.hibernate.type.spi.TypeConfiguration;
import org.junit.After;
import org.junit.Assert;
import org.junit.Test;
@ -594,3 +608,88 @@ public String getName() {
}
}
class BitTypeDescriptor implements SqlTypeDescriptor {
public static final BitTypeDescriptor INSTANCE = new BitTypeDescriptor();
public BitTypeDescriptor() {
}
public int getSqlType() {
return Types.BIT;
}
@Override
public String getFriendlyName() {
return "BIT";
}
@Override
public String toString() {
return "BitTypeDescriptor";
}
@Override
public boolean canBeRemapped() {
return true;
}
@Override
public <T> BasicJavaDescriptor<T> getJdbcRecommendedJavaTypeMapping(TypeConfiguration typeConfiguration) {
return (BasicJavaDescriptor<T>) typeConfiguration.getJavaTypeDescriptorRegistry().getDescriptor( Boolean.class );
}
public <T> JdbcLiteralFormatter<T> getJdbcLiteralFormatter(JavaTypeDescriptor<T> javaTypeDescriptor) {
if ( javaTypeDescriptor.getJavaType().equals(Boolean.class) ) {
//this is to allow literals to be formatted correctly when
//we are in the legacy Boolean-to-BIT JDBC type mapping mode
return new BasicJdbcLiteralFormatter( javaTypeDescriptor ) {
@Override
public String toJdbcLiteral(Object value, Dialect dialect, WrapperOptions wrapperOptions) {
Boolean bool = unwrap( value, Boolean.class, wrapperOptions );
return bool ? "1" : "0";
}
};
}
else {
return (value, dialect, wrapperOptions) -> value.toString();
}
}
@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.setBoolean( index, javaTypeDescriptor.unwrap( value, Boolean.class, options ) );
}
@Override
protected void doBind(CallableStatement st, X value, String name, WrapperOptions options)
throws SQLException {
st.setBoolean( name, javaTypeDescriptor.unwrap( value, Boolean.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.getBoolean( paramIndex ), options );
}
@Override
protected X doExtract(CallableStatement statement, int index, WrapperOptions options) throws SQLException {
return javaTypeDescriptor.wrap( statement.getBoolean( index ), options );
}
@Override
protected X doExtract(CallableStatement statement, String name, WrapperOptions options) throws SQLException {
return javaTypeDescriptor.wrap( statement.getBoolean( name ), options );
}
};
}
}