Skip to content

Commit

Permalink
Move type adjustment from AdjustableBasicType to AdjustableJdbcTypeDe…
Browse files Browse the repository at this point in the history
…scriptor
  • Loading branch information
beikov committed Oct 7, 2021
1 parent bb52778 commit bfe2da9
Show file tree
Hide file tree
Showing 25 changed files with 148 additions and 356 deletions.
Expand Up @@ -40,8 +40,9 @@
import org.hibernate.cfg.PropertyHolderBuilder;
import org.hibernate.cfg.PropertyPreloadedData;
import org.hibernate.cfg.SecondPass;
import org.hibernate.dialect.HSQLDialect;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.Size;
import org.hibernate.engine.jdbc.spi.JdbcServices;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.mapping.BasicValue;
import org.hibernate.mapping.Collection;
Expand Down Expand Up @@ -471,7 +472,10 @@ else if ( sourceValueColumn instanceof Formula ) {
}

if ( fromAndWhere != null ) {
formulaString = Template.renderWhereStringTemplate( formulaString, "$alias$", new HSQLDialect() );
final Dialect dialect = buildingContext.getBootstrapContext().getServiceRegistry()
.getService( JdbcServices.class )
.getDialect();
formulaString = Template.renderWhereStringTemplate( formulaString, "$alias$", dialect );
formulaString = "(select " + formulaString + fromAndWhere + ")";
formulaString = StringHelper.replace( formulaString, "$alias$", "a987" );
}
Expand Down Expand Up @@ -513,7 +517,10 @@ else if ( current instanceof Formula ) {
throw new AssertionFailure( "Unknown element in column iterator: " + current.getClass() );
}
if ( fromAndWhere != null ) {
formulaString = Template.renderWhereStringTemplate( formulaString, "$alias$", new HSQLDialect() );
final Dialect dialect = buildingContext.getBootstrapContext().getServiceRegistry()
.getService( JdbcServices.class )
.getDialect();
formulaString = Template.renderWhereStringTemplate( formulaString, "$alias$", dialect );
formulaString = "(select " + formulaString + fromAndWhere + ")";
formulaString = StringHelper.replace(
formulaString,
Expand Down
Expand Up @@ -7,6 +7,8 @@
package org.hibernate.type;

import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.jdbc.AdjustableJdbcTypeDescriptor;
import org.hibernate.type.descriptor.jdbc.JdbcTypeDescriptor;
import org.hibernate.type.descriptor.jdbc.JdbcTypeDescriptorIndicators;

/**
Expand All @@ -18,5 +20,18 @@ public interface AdjustableBasicType<J> extends BasicType<J> {
/**
* Perform the adjustment
*/
<X> BasicType<X> resolveIndicatedType(JdbcTypeDescriptorIndicators indicators, JavaTypeDescriptor<X> domainJtd);
default <X> BasicType<X> resolveIndicatedType(JdbcTypeDescriptorIndicators indicators, JavaTypeDescriptor<X> domainJtd) {
final JdbcTypeDescriptor jdbcTypeDescriptor = getJdbcTypeDescriptor();
if ( jdbcTypeDescriptor instanceof AdjustableJdbcTypeDescriptor ) {
final JdbcTypeDescriptor resolvedJdbcTypeDescriptor = ( (AdjustableJdbcTypeDescriptor) jdbcTypeDescriptor ).resolveIndicatedType(
indicators,
domainJtd
);
if ( resolvedJdbcTypeDescriptor != jdbcTypeDescriptor ) {
return indicators.getTypeConfiguration().getBasicTypeRegistry()
.resolve( domainJtd, resolvedJdbcTypeDescriptor, getName() );
}
}
return (BasicType<X>) this;
}
}
13 changes: 0 additions & 13 deletions hibernate-core/src/main/java/org/hibernate/type/BinaryType.java
Expand Up @@ -89,17 +89,4 @@ public Comparator<byte[]> getComparator() {
return PrimitiveByteArrayTypeDescriptor.INSTANCE.getComparator();
}

@Override
@SuppressWarnings("unchecked")
public <X> BasicType<X> resolveIndicatedType(JdbcTypeDescriptorIndicators indicators, JavaTypeDescriptor<X> domainJtd) {
if ( ! indicators.isLob() ) {
return (BasicType<X>) this;
}

final TypeConfiguration typeConfiguration = indicators.getTypeConfiguration();
final JdbcTypeDescriptorRegistry jdbcTypeRegistry = typeConfiguration.getJdbcTypeDescriptorRegistry();
final JdbcTypeDescriptor jdbcType = jdbcTypeRegistry.getDescriptor( Types.BLOB );

return typeConfiguration.getBasicTypeRegistry().resolve( domainJtd, jdbcType, getName() );
}
}
27 changes: 0 additions & 27 deletions hibernate-core/src/main/java/org/hibernate/type/BooleanType.java
Expand Up @@ -67,31 +67,4 @@ public String objectToSQLString(Boolean value, Dialect dialect) {
return dialect.toBooleanValueString( value );
}

@Override
public <X> BasicType<X> resolveIndicatedType(
JdbcTypeDescriptorIndicators indicators,
JavaTypeDescriptor<X> domainJtd) {
final int preferredSqlTypeCodeForBoolean = indicators.getPreferredSqlTypeCodeForBoolean();
final JdbcTypeDescriptor jdbcTypeDescriptor;
// We treat BIT like BOOLEAN because it uses the same JDBC access methods
if ( preferredSqlTypeCodeForBoolean != Types.BIT && preferredSqlTypeCodeForBoolean != getJdbcTypeDescriptor().getJdbcTypeCode() ) {
jdbcTypeDescriptor = indicators.getTypeConfiguration()
.getJdbcTypeDescriptorRegistry()
.getDescriptor( preferredSqlTypeCodeForBoolean );
}
else {
jdbcTypeDescriptor = indicators.getTypeConfiguration()
.getJdbcTypeDescriptorRegistry()
.getDescriptor( Types.BOOLEAN );
}
if ( jdbcTypeDescriptor != getJdbcTypeDescriptor() ) {
//noinspection unchecked
return (BasicType<X>) indicators.getTypeConfiguration()
.getBasicTypeRegistry()
.resolve( getJavaTypeDescriptor(), jdbcTypeDescriptor, getName() );
}

//noinspection unchecked
return (BasicType<X>) this;
}
}
Expand Up @@ -5,15 +5,11 @@
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.type;

import java.sql.Types;

import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.java.PrimitiveCharacterArrayTypeDescriptor;
import org.hibernate.type.descriptor.jdbc.JdbcTypeDescriptor;
import org.hibernate.type.descriptor.jdbc.JdbcTypeDescriptorIndicators;
import org.hibernate.type.descriptor.jdbc.VarcharTypeDescriptor;
import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeDescriptorRegistry;
import org.hibernate.type.spi.TypeConfiguration;

/**
* A type that maps between {@link Types#VARCHAR VARCHAR} and {@code char[]}
Expand All @@ -39,24 +35,4 @@ public String[] getRegistrationKeys() {
return new String[] { getName(), "char[]", char[].class.getName() };
}

@Override
public <X> BasicType<X> resolveIndicatedType(
JdbcTypeDescriptorIndicators indicators,
JavaTypeDescriptor<X> domainJtd) {
assert domainJtd != null;

final TypeConfiguration typeConfiguration = indicators.getTypeConfiguration();
final JdbcTypeDescriptorRegistry jdbcTypeRegistry = typeConfiguration.getJdbcTypeDescriptorRegistry();

final int jdbcTypeCode;
if ( indicators.isLob() ) {
jdbcTypeCode = indicators.isNationalized() ? Types.NCLOB : Types.CLOB;
}
else {
jdbcTypeCode = indicators.isNationalized() ? Types.NVARCHAR : Types.VARCHAR;
}

final JdbcTypeDescriptor jdbcType = jdbcTypeRegistry.getDescriptor( jdbcTypeCode );
return typeConfiguration.getBasicTypeRegistry().resolve( domainJtd, jdbcType, getName() );
}
}
Expand Up @@ -37,26 +37,4 @@ public String getName() {
// todo name these annotation types for addition to the registry
return null;
}

@Override
public <X> BasicType<X> resolveIndicatedType(
JdbcTypeDescriptorIndicators indicators,
JavaTypeDescriptor<X> domainJtd) {
if ( domainJtd != null && domainJtd.getJavaTypeClass() == char[].class ) {
// domainJtd is a `char[]` instead of a `Character[]`....
final TypeConfiguration typeConfiguration = indicators.getTypeConfiguration();
final JdbcTypeDescriptorRegistry jdbcTypeRegistry = typeConfiguration.getJdbcTypeDescriptorRegistry();
final JdbcTypeDescriptor jdbcType = indicators.isNationalized()
? jdbcTypeRegistry.getDescriptor( Types.NCLOB )
: jdbcTypeRegistry.getDescriptor( Types.CLOB );

return typeConfiguration.getBasicTypeRegistry().resolve(
typeConfiguration.getJavaTypeDescriptorRegistry().getDescriptor( domainJtd.getJavaType() ),
jdbcType,
getName()
);
}

return (BasicType<X>) ( indicators.isNationalized() ? CharacterArrayNClobType.INSTANCE : this );
}
}
Expand Up @@ -9,12 +9,7 @@
import java.sql.Types;

import org.hibernate.type.descriptor.java.CharacterArrayTypeDescriptor;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.jdbc.JdbcTypeDescriptor;
import org.hibernate.type.descriptor.jdbc.JdbcTypeDescriptorIndicators;
import org.hibernate.type.descriptor.jdbc.NClobTypeDescriptor;
import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeDescriptorRegistry;
import org.hibernate.type.spi.TypeConfiguration;

/**
* A type that maps between {@link Types#NCLOB NCLOB} and {@link Character Character[]}
Expand All @@ -38,24 +33,4 @@ public String getName() {
return null;
}

@Override
public <X> BasicType<X> resolveIndicatedType(
JdbcTypeDescriptorIndicators indicators,
JavaTypeDescriptor<X> domainJtd) {
if ( domainJtd != null && domainJtd.getJavaTypeClass() == char[].class ) {
// domainJtd is a `char[]` instead of a `Character[]`....
final TypeConfiguration typeConfiguration = indicators.getTypeConfiguration();
final JdbcTypeDescriptorRegistry jdbcTypeRegistry = typeConfiguration.getJdbcTypeDescriptorRegistry();
final JdbcTypeDescriptor jdbcType = jdbcTypeRegistry.getDescriptor( Types.NCLOB );

return typeConfiguration.getBasicTypeRegistry().resolve(
typeConfiguration.getJavaTypeDescriptorRegistry().getDescriptor( domainJtd.getJavaType() ),
jdbcType,
getName()
);
}

//noinspection unchecked
return (BasicType<X>) this;
}
}
Expand Up @@ -5,15 +5,11 @@
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.type;

import java.sql.Types;

import org.hibernate.type.descriptor.java.CharacterArrayTypeDescriptor;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.jdbc.JdbcTypeDescriptor;
import org.hibernate.type.descriptor.jdbc.JdbcTypeDescriptorIndicators;
import org.hibernate.type.descriptor.jdbc.VarcharTypeDescriptor;
import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeDescriptorRegistry;
import org.hibernate.type.spi.TypeConfiguration;

/**
* A type that maps between {@link Types#VARCHAR VARCHAR} and {@link Character Character[]}
Expand All @@ -38,40 +34,4 @@ public String getName() {
public String[] getRegistrationKeys() {
return new String[] { getName(), Character[].class.getName(), "Character[]" };
}

@Override
public <X> BasicType<X> resolveIndicatedType(
JdbcTypeDescriptorIndicators indicators,
JavaTypeDescriptor<X> domainJtd) {
final TypeConfiguration typeConfiguration = indicators.getTypeConfiguration();
final JdbcTypeDescriptorRegistry jdbcTypeRegistry = typeConfiguration.getJdbcTypeDescriptorRegistry();

final int jdbcTypeCode;
if ( indicators.isLob() ) {
jdbcTypeCode = indicators.isNationalized() ? Types.NCLOB : Types.CLOB;
}
else {
jdbcTypeCode = indicators.isNationalized() ? Types.NVARCHAR : Types.VARCHAR;
}

final JdbcTypeDescriptor indicatedJdbcType = jdbcTypeRegistry.getDescriptor( jdbcTypeCode );

if ( domainJtd != null && domainJtd.getJavaTypeClass() == Character[].class ) {
return typeConfiguration.getBasicTypeRegistry().resolve(
typeConfiguration.getJavaTypeDescriptorRegistry().resolveDescriptor( domainJtd.getJavaType() ),
indicatedJdbcType,
getName()
);
}

if ( getJdbcTypeDescriptor() == indicatedJdbcType ) {
return (BasicType<X>) this;
}

return (BasicType<X>) typeConfiguration.getBasicTypeRegistry().resolve(
getJavaTypeDescriptor(),
indicatedJdbcType,
getName()
);
}
}
17 changes: 0 additions & 17 deletions hibernate-core/src/main/java/org/hibernate/type/CharacterType.java
Expand Up @@ -11,12 +11,7 @@

import org.hibernate.dialect.Dialect;
import org.hibernate.type.descriptor.java.CharacterTypeDescriptor;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.jdbc.CharTypeDescriptor;
import org.hibernate.type.descriptor.jdbc.JdbcTypeDescriptor;
import org.hibernate.type.descriptor.jdbc.JdbcTypeDescriptorIndicators;
import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeDescriptorRegistry;
import org.hibernate.type.spi.TypeConfiguration;

/**
* A type that maps between {@link Types#CHAR CHAR(1)} and {@link Character}
Expand Down Expand Up @@ -65,16 +60,4 @@ public Character stringToObject(CharSequence sequence) {
return fromString( sequence );
}

@Override
public <X> BasicType<X> resolveIndicatedType(
JdbcTypeDescriptorIndicators indicators,
JavaTypeDescriptor<X> domainJtd) {
final TypeConfiguration typeConfiguration = indicators.getTypeConfiguration();
final JdbcTypeDescriptorRegistry jdbcTypeRegistry = typeConfiguration.getJdbcTypeDescriptorRegistry();
final JdbcTypeDescriptor jdbcType = indicators.isNationalized()
? jdbcTypeRegistry.getDescriptor( Types.NCHAR )
: jdbcTypeRegistry.getDescriptor( Types.CHAR );

return typeConfiguration.getBasicTypeRegistry().resolve( domainJtd, jdbcType, getName() );
}
}
23 changes: 0 additions & 23 deletions hibernate-core/src/main/java/org/hibernate/type/ClobType.java
Expand Up @@ -11,10 +11,6 @@

import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.type.descriptor.java.ClobTypeDescriptor;
import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.jdbc.JdbcTypeDescriptorIndicators;
import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeDescriptorRegistry;
import org.hibernate.type.spi.TypeConfiguration;

/**
* A type that maps between {@link Types#CLOB CLOB} and {@link Clob}
Expand Down Expand Up @@ -43,23 +39,4 @@ protected boolean registerUnderJavaType() {
protected Clob getReplacement(Clob original, Clob target, SharedSessionContractImplementor session) {
return session.getJdbcServices().getJdbcEnvironment().getDialect().getLobMergeStrategy().mergeClob( original, target, session );
}

@Override
@SuppressWarnings( "unchecked" )
public <X> BasicType<X> resolveIndicatedType(
JdbcTypeDescriptorIndicators indicators,
JavaTypeDescriptor<X> domainJtd) {
if ( ! indicators.isNationalized() ) {
return (BasicType<X>) this;
}

final TypeConfiguration typeConfiguration = indicators.getTypeConfiguration();
final JdbcTypeDescriptorRegistry jdbcTypeRegistry = typeConfiguration.getJdbcTypeDescriptorRegistry();

return typeConfiguration.getBasicTypeRegistry().resolve(
domainJtd,
jdbcTypeRegistry.getDescriptor( Types.NCLOB ),
getName()
);
}
}
Expand Up @@ -5,15 +5,11 @@
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.type;

import java.sql.Types;

import org.hibernate.type.descriptor.java.JavaTypeDescriptor;
import org.hibernate.type.descriptor.java.StringTypeDescriptor;
import org.hibernate.type.descriptor.jdbc.ClobTypeDescriptor;
import org.hibernate.type.descriptor.jdbc.JdbcTypeDescriptor;
import org.hibernate.type.descriptor.jdbc.JdbcTypeDescriptorIndicators;
import org.hibernate.type.descriptor.jdbc.spi.JdbcTypeDescriptorRegistry;
import org.hibernate.type.spi.TypeConfiguration;

/**
* A type that maps between {@link Types#CLOB CLOB} and {@link String}
Expand All @@ -34,24 +30,4 @@ public MaterializedClobType() {
public String getName() {
return "materialized_clob";
}

@Override
public <X> BasicType<X> resolveIndicatedType(
JdbcTypeDescriptorIndicators indicators,
JavaTypeDescriptor<X> domainJtd) {
if ( indicators.isNationalized() ) {
final TypeConfiguration typeConfiguration = indicators.getTypeConfiguration();
final JdbcTypeDescriptorRegistry jdbcTypeRegistry = typeConfiguration.getJdbcTypeDescriptorRegistry();
final JdbcTypeDescriptor nclobType = jdbcTypeRegistry.getDescriptor( Types.NCLOB );

return typeConfiguration.getBasicTypeRegistry().resolve(
domainJtd,
nclobType,
getName()
);
}

//noinspection unchecked
return (BasicType<X>) this;
}
}

0 comments on commit bfe2da9

Please sign in to comment.