clean up formatters and remove TypeConfigurationWrapperOptions

It's wrong to build a WrapperOptions from a TypeConfiguration
This commit is contained in:
Gavin King 2024-12-13 23:45:23 +01:00
parent 626f9a7281
commit 8b4d6b75bc
13 changed files with 69 additions and 173 deletions

View File

@ -45,14 +45,8 @@ public class H2DurationIntervalSecondJdbcType implements JdbcType {
@Override @Override
public <T> JdbcLiteralFormatter<T> getJdbcLiteralFormatter(JavaType<T> javaType) { public <T> JdbcLiteralFormatter<T> getJdbcLiteralFormatter(JavaType<T> javaType) {
return (appender, value, dialect, wrapperOptions) -> dialect.appendIntervalLiteral( return (appender, value, dialect, wrapperOptions) ->
appender, dialect.appendIntervalLiteral( appender, javaType.unwrap( value, Duration.class, wrapperOptions ) );
javaType.unwrap(
value,
Duration.class,
wrapperOptions
)
);
} }
@Override @Override

View File

@ -63,9 +63,11 @@ public class OracleEnumJdbcType implements JdbcType {
public <T> JdbcLiteralFormatter<T> getJdbcLiteralFormatter(JavaType<T> javaType) { public <T> JdbcLiteralFormatter<T> getJdbcLiteralFormatter(JavaType<T> javaType) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
final Class<? extends Enum<?>> enumClass = (Class<? extends Enum<?>>) javaType.getJavaType(); final Class<? extends Enum<?>> enumClass = (Class<? extends Enum<?>>) javaType.getJavaType();
return (appender, value, dialect, wrapperOptions) return (appender, value, dialect, wrapperOptions) -> {
-> appender.appendSql( dialect.getEnumTypeDeclaration( enumClass ) appender.appendSql( dialect.getEnumTypeDeclaration( enumClass ) );
+ "." + ((Enum<?>) value).name() ); appender.appendSql( '.' );
appender.appendSql( ((Enum<?>) value).name() );
};
} }
@Override @Override

View File

@ -64,9 +64,12 @@ public class PostgreSQLEnumJdbcType implements JdbcType {
public <T> JdbcLiteralFormatter<T> getJdbcLiteralFormatter(JavaType<T> javaType) { public <T> JdbcLiteralFormatter<T> getJdbcLiteralFormatter(JavaType<T> javaType) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
final Class<? extends Enum<?>> enumClass = (Class<? extends Enum<?>>) javaType.getJavaType(); final Class<? extends Enum<?>> enumClass = (Class<? extends Enum<?>>) javaType.getJavaType();
return (appender, value, dialect, wrapperOptions) return (appender, value, dialect, wrapperOptions) -> {
-> appender.appendSql( "'" + ((Enum<?>) value).name() + "'::" appender.appendSql( "'" );
+ dialect.getEnumTypeDeclaration( enumClass ) ); appender.appendSql( ((Enum<?>) value).name() );
appender.appendSql( "'::" );
appender.appendSql( dialect.getEnumTypeDeclaration( enumClass ) );
};
} }
@Override @Override

View File

@ -70,14 +70,8 @@ public class PostgreSQLIntervalSecondJdbcType implements AdjustableJdbcType {
@Override @Override
public <T> JdbcLiteralFormatter<T> getJdbcLiteralFormatter(JavaType<T> javaType) { public <T> JdbcLiteralFormatter<T> getJdbcLiteralFormatter(JavaType<T> javaType) {
return (appender, value, dialect, wrapperOptions) -> dialect.appendIntervalLiteral( return (appender, value, dialect, wrapperOptions) ->
appender, dialect.appendIntervalLiteral( appender, javaType.unwrap( value, Duration.class, wrapperOptions ) );
javaType.unwrap(
value,
Duration.class,
wrapperOptions
)
);
} }
@Override @Override

View File

@ -23,7 +23,7 @@ import org.hibernate.type.spi.TypeConfiguration;
/** /**
* A set of operations providing support for aggregate column types * A set of operations providing support for aggregate column types
* in a certain {@link Dialect SQL dialect}. * in a certain {@linkplain Dialect SQL dialect}.
* *
* @since 6.2 * @since 6.2
*/ */

View File

@ -4,6 +4,7 @@
*/ */
package org.hibernate.dialect.aggregate; package org.hibernate.dialect.aggregate;
import org.hibernate.HibernateException;
import org.hibernate.boot.model.naming.Identifier; import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.relational.AuxiliaryDatabaseObject; import org.hibernate.boot.model.relational.AuxiliaryDatabaseObject;
import org.hibernate.boot.model.relational.Namespace; import org.hibernate.boot.model.relational.Namespace;
@ -37,7 +38,6 @@ import org.hibernate.type.descriptor.jdbc.JdbcType;
import org.hibernate.type.descriptor.jdbc.StructJdbcType; import org.hibernate.type.descriptor.jdbc.StructJdbcType;
import org.hibernate.type.descriptor.sql.DdlType; import org.hibernate.type.descriptor.sql.DdlType;
import org.hibernate.type.descriptor.sql.spi.DdlTypeRegistry; import org.hibernate.type.descriptor.sql.spi.DdlTypeRegistry;
import org.hibernate.type.internal.TypeConfigurationWrapperOptions;
import org.hibernate.type.spi.TypeConfiguration; import org.hibernate.type.spi.TypeConfiguration;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
@ -76,26 +76,15 @@ public class OracleAggregateSupport extends AggregateSupportImpl {
public static AggregateSupport valueOf(Dialect dialect) { public static AggregateSupport valueOf(Dialect dialect) {
final DatabaseVersion version = dialect.getVersion(); final DatabaseVersion version = dialect.getVersion();
switch ( version.getMajor() ) { return switch ( version.getMajor() ) {
case 12: case 12, 13, 14, 15, 16, 17 -> V12_INSTANCE;
case 13: case 18 -> V18_INSTANCE;
case 14: case 19, 20 -> V19_INSTANCE;
case 15: case 21, 22 -> V21_INSTANCE;
case 16: default -> version.isSameOrAfter( 23 )
case 17:
return V12_INSTANCE;
case 18:
return V18_INSTANCE;
case 19:
case 20:
return V19_INSTANCE;
case 21:
case 22:
return V21_INSTANCE;
}
return version.isSameOrAfter( 23 )
? OracleAggregateSupport.V23_INSTANCE ? OracleAggregateSupport.V23_INSTANCE
: OracleAggregateSupport.LEGACY_INSTANCE; : OracleAggregateSupport.LEGACY_INSTANCE;
};
} }
@Override @Override
@ -132,7 +121,7 @@ public class OracleAggregateSupport extends AggregateSupportImpl {
final JdbcLiteralFormatter<Boolean> jdbcLiteralFormatter = (JdbcLiteralFormatter<Boolean>) column.getJdbcMapping().getJdbcType() final JdbcLiteralFormatter<Boolean> jdbcLiteralFormatter = (JdbcLiteralFormatter<Boolean>) column.getJdbcMapping().getJdbcType()
.getJdbcLiteralFormatter( column.getJdbcMapping().getMappedJavaType() ); .getJdbcLiteralFormatter( column.getJdbcMapping().getMappedJavaType() );
final Dialect dialect = typeConfiguration.getCurrentBaseSqlTypeIndicators().getDialect(); final Dialect dialect = typeConfiguration.getCurrentBaseSqlTypeIndicators().getDialect();
final WrapperOptions wrapperOptions = new TypeConfigurationWrapperOptions( typeConfiguration ); final WrapperOptions wrapperOptions = getWrapperOptions( typeConfiguration );
final String trueLiteral = jdbcLiteralFormatter.toJdbcLiteral( true, dialect, wrapperOptions ); final String trueLiteral = jdbcLiteralFormatter.toJdbcLiteral( true, dialect, wrapperOptions );
final String falseLiteral = jdbcLiteralFormatter.toJdbcLiteral( false, dialect, wrapperOptions ); final String falseLiteral = jdbcLiteralFormatter.toJdbcLiteral( false, dialect, wrapperOptions );
return template.replace( return template.replace(
@ -242,7 +231,7 @@ public class OracleAggregateSupport extends AggregateSupportImpl {
final JdbcLiteralFormatter<Boolean> jdbcLiteralFormatter = (JdbcLiteralFormatter<Boolean>) column.getJdbcMapping().getJdbcType() final JdbcLiteralFormatter<Boolean> jdbcLiteralFormatter = (JdbcLiteralFormatter<Boolean>) column.getJdbcMapping().getJdbcType()
.getJdbcLiteralFormatter( column.getJdbcMapping().getMappedJavaType() ); .getJdbcLiteralFormatter( column.getJdbcMapping().getMappedJavaType() );
final Dialect dialect = typeConfiguration.getCurrentBaseSqlTypeIndicators().getDialect(); final Dialect dialect = typeConfiguration.getCurrentBaseSqlTypeIndicators().getDialect();
final WrapperOptions wrapperOptions = new TypeConfigurationWrapperOptions( typeConfiguration ); final WrapperOptions wrapperOptions = getWrapperOptions( typeConfiguration );
final String trueLiteral = jdbcLiteralFormatter.toJdbcLiteral( true, dialect, wrapperOptions ); final String trueLiteral = jdbcLiteralFormatter.toJdbcLiteral( true, dialect, wrapperOptions );
final String falseLiteral = jdbcLiteralFormatter.toJdbcLiteral( false, dialect, wrapperOptions ); final String falseLiteral = jdbcLiteralFormatter.toJdbcLiteral( false, dialect, wrapperOptions );
return template.replace( return template.replace(
@ -318,6 +307,15 @@ public class OracleAggregateSupport extends AggregateSupportImpl {
throw new IllegalArgumentException( "Unsupported aggregate SQL type: " + aggregateColumnTypeCode ); throw new IllegalArgumentException( "Unsupported aggregate SQL type: " + aggregateColumnTypeCode );
} }
private static WrapperOptions getWrapperOptions(TypeConfiguration typeConfiguration) {
try {
return typeConfiguration.getSessionFactory().getWrapperOptions();
}
catch (HibernateException e) {
return null;
}
}
private static String xmlExtractArguments(String aggregateParentReadExpression, String xpathFragment) { private static String xmlExtractArguments(String aggregateParentReadExpression, String xpathFragment) {
final String extractArguments; final String extractArguments;
int separatorIndex; int separatorIndex;
@ -433,7 +431,7 @@ public class OracleAggregateSupport extends AggregateSupportImpl {
final JdbcLiteralFormatter<Boolean> jdbcLiteralFormatter = (JdbcLiteralFormatter<Boolean>) jdbcMapping.getJdbcType() final JdbcLiteralFormatter<Boolean> jdbcLiteralFormatter = (JdbcLiteralFormatter<Boolean>) jdbcMapping.getJdbcType()
.getJdbcLiteralFormatter( jdbcMapping.getMappedJavaType() ); .getJdbcLiteralFormatter( jdbcMapping.getMappedJavaType() );
final Dialect dialect = typeConfiguration.getCurrentBaseSqlTypeIndicators().getDialect(); final Dialect dialect = typeConfiguration.getCurrentBaseSqlTypeIndicators().getDialect();
final WrapperOptions wrapperOptions = new TypeConfigurationWrapperOptions( typeConfiguration ); final WrapperOptions wrapperOptions = getWrapperOptions( typeConfiguration );
final String trueLiteral = jdbcLiteralFormatter.toJdbcLiteral( true, dialect, wrapperOptions ); final String trueLiteral = jdbcLiteralFormatter.toJdbcLiteral( true, dialect, wrapperOptions );
final String falseLiteral = jdbcLiteralFormatter.toJdbcLiteral( false, dialect, wrapperOptions ); final String falseLiteral = jdbcLiteralFormatter.toJdbcLiteral( false, dialect, wrapperOptions );
return "decode(" + customWriteExpression + "," + trueLiteral + ",'true'," + falseLiteral + ",'false')"; return "decode(" + customWriteExpression + "," + trueLiteral + ",'true'," + falseLiteral + ",'false')";
@ -458,7 +456,7 @@ public class OracleAggregateSupport extends AggregateSupportImpl {
final JdbcLiteralFormatter<Boolean> jdbcLiteralFormatter = (JdbcLiteralFormatter<Boolean>) jdbcMapping.getJdbcType() final JdbcLiteralFormatter<Boolean> jdbcLiteralFormatter = (JdbcLiteralFormatter<Boolean>) jdbcMapping.getJdbcType()
.getJdbcLiteralFormatter( jdbcMapping.getMappedJavaType() ); .getJdbcLiteralFormatter( jdbcMapping.getMappedJavaType() );
final Dialect dialect = typeConfiguration.getCurrentBaseSqlTypeIndicators().getDialect(); final Dialect dialect = typeConfiguration.getCurrentBaseSqlTypeIndicators().getDialect();
final WrapperOptions wrapperOptions = new TypeConfigurationWrapperOptions( typeConfiguration ); final WrapperOptions wrapperOptions = getWrapperOptions( typeConfiguration );
final String trueLiteral = jdbcLiteralFormatter.toJdbcLiteral( true, dialect, wrapperOptions ); final String trueLiteral = jdbcLiteralFormatter.toJdbcLiteral( true, dialect, wrapperOptions );
final String falseLiteral = jdbcLiteralFormatter.toJdbcLiteral( false, dialect, wrapperOptions ); final String falseLiteral = jdbcLiteralFormatter.toJdbcLiteral( false, dialect, wrapperOptions );
return "decode(" + customWriteExpression + "," + trueLiteral + ",'true'," + falseLiteral + ",'false')"; return "decode(" + customWriteExpression + "," + trueLiteral + ",'true'," + falseLiteral + ",'false')";

View File

@ -31,9 +31,9 @@ public interface JdbcLiteralFormatter<T> extends Serializable {
* @return the SQL literal as a string * @return the SQL literal as a string
*/ */
default String toJdbcLiteral(T value, Dialect dialect, WrapperOptions wrapperOptions) { default String toJdbcLiteral(T value, Dialect dialect, WrapperOptions wrapperOptions) {
final StringBuilder sb = new StringBuilder(); final StringBuilder result = new StringBuilder();
appendJdbcLiteral( new StringBuilderSqlAppender( sb ), value, dialect, wrapperOptions ); appendJdbcLiteral( new StringBuilderSqlAppender( result ), value, dialect, wrapperOptions );
return sb.toString(); return result.toString();
} }
/** /**

View File

@ -118,7 +118,8 @@ public interface JdbcType extends Serializable {
*/ */
// todo (6.0) : move to {@link org.hibernate.metamodel.mapping.JdbcMapping}? // todo (6.0) : move to {@link org.hibernate.metamodel.mapping.JdbcMapping}?
default <T> JdbcLiteralFormatter<T> getJdbcLiteralFormatter(JavaType<T> javaType) { default <T> JdbcLiteralFormatter<T> getJdbcLiteralFormatter(JavaType<T> javaType) {
return (appender, value, dialect, wrapperOptions) -> appender.appendSql( value.toString() ); return (appender, value, dialect, wrapperOptions) ->
appender.appendSql( value.toString() );
} }
/** /**

View File

@ -23,11 +23,10 @@ public class JdbcLiteralFormatterArray<T> extends BasicJdbcLiteralFormatter<T> {
@Override @Override
public void appendJdbcLiteral(SqlAppender appender, Object value, Dialect dialect, WrapperOptions wrapperOptions) { public void appendJdbcLiteral(SqlAppender appender, Object value, Dialect dialect, WrapperOptions wrapperOptions) {
dialect.appendArrayLiteral( dialect.appendArrayLiteral( appender, unwrapArray( value, wrapperOptions ), elementFormatter, wrapperOptions );
appender, }
unwrap( value, Object[].class, wrapperOptions ),
elementFormatter, private Object[] unwrapArray(Object value, WrapperOptions wrapperOptions) {
wrapperOptions return unwrap( value, Object[].class, wrapperOptions );
);
} }
} }

View File

@ -32,58 +32,26 @@ public class JdbcLiteralFormatterTemporal<T> extends BasicJdbcLiteralFormatter<T
public void appendJdbcLiteral(SqlAppender appender, Object value, Dialect dialect, WrapperOptions options) { public void appendJdbcLiteral(SqlAppender appender, Object value, Dialect dialect, WrapperOptions options) {
final TimeZone jdbcTimeZone = getJdbcTimeZone( options ); final TimeZone jdbcTimeZone = getJdbcTimeZone( options );
// for performance reasons, avoid conversions if we can // for performance reasons, avoid conversions if we can
if ( value instanceof java.util.Date ) { if ( value instanceof java.util.Date date ) {
dialect.appendDateTimeLiteral( dialect.appendDateTimeLiteral( appender, date, precision, jdbcTimeZone );
appender,
(java.util.Date) value,
precision,
jdbcTimeZone
);
} }
else if ( value instanceof java.util.Calendar ) { else if ( value instanceof java.util.Calendar calendar ) {
dialect.appendDateTimeLiteral( dialect.appendDateTimeLiteral( appender, calendar, precision, jdbcTimeZone );
appender,
(java.util.Calendar) value,
precision,
jdbcTimeZone
);
} }
else if ( value instanceof TemporalAccessor ) { else if ( value instanceof TemporalAccessor temporalAccessor ) {
dialect.appendDateTimeLiteral( dialect.appendDateTimeLiteral( appender, temporalAccessor, precision, jdbcTimeZone );
appender,
(TemporalAccessor) value,
precision,
jdbcTimeZone
);
} }
else { else {
switch ( precision ) { dialect.appendDateTimeLiteral( appender, unwrap( value, options ), precision, jdbcTimeZone );
case DATE:
dialect.appendDateTimeLiteral(
appender,
unwrap( value, java.sql.Date.class, options ),
precision,
jdbcTimeZone
);
break;
case TIME:
dialect.appendDateTimeLiteral(
appender,
unwrap( value, java.sql.Time.class, options ),
precision,
jdbcTimeZone
);
break;
default:
dialect.appendDateTimeLiteral(
appender,
unwrap( value, java.util.Date.class, options ),
precision,
jdbcTimeZone
);
break;
} }
} }
private java.util.Date unwrap(Object value, WrapperOptions options) {
return switch ( precision ) {
case DATE -> unwrap( value, java.sql.Date.class, options );
case TIME -> unwrap( value, java.sql.Time.class, options );
case TIMESTAMP -> unwrap( value, java.util.Date.class, options );
};
} }
private static TimeZone getJdbcTimeZone(WrapperOptions options) { private static TimeZone getJdbcTimeZone(WrapperOptions options) {

View File

@ -24,7 +24,6 @@ public class JdbcLiteralFormatterUUIDData<T> extends BasicJdbcLiteralFormatter<T
@Override @Override
public void appendJdbcLiteral(SqlAppender appender, Object value, Dialect dialect, WrapperOptions wrapperOptions) { public void appendJdbcLiteral(SqlAppender appender, Object value, Dialect dialect, WrapperOptions wrapperOptions) {
final UUID literalValue = unwrap( value, UUID.class, wrapperOptions ); dialect.appendUUIDLiteral( appender, unwrap( value, UUID.class, wrapperOptions ) );
dialect.appendUUIDLiteral( appender, literalValue );
} }
} }

View File

@ -1,58 +0,0 @@
/*
* SPDX-License-Identifier: LGPL-2.1-or-later
* Copyright Red Hat Inc. and Hibernate Authors
*/
package org.hibernate.type.internal;
import org.hibernate.dialect.Dialect;
import org.hibernate.engine.jdbc.LobCreator;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.type.descriptor.WrapperOptions;
import org.hibernate.type.spi.TypeConfiguration;
import java.util.TimeZone;
public class TypeConfigurationWrapperOptions implements WrapperOptions {
private final TypeConfiguration typeConfiguration;
public TypeConfigurationWrapperOptions(TypeConfiguration typeConfiguration) {
this.typeConfiguration = typeConfiguration;
}
@Override
public Dialect getDialect() {
return typeConfiguration.getCurrentBaseSqlTypeIndicators().getDialect();
}
@Override
public SharedSessionContractImplementor getSession() {
return typeConfiguration.getSessionFactory().getWrapperOptions().getSession();
}
@Override
public SessionFactoryImplementor getSessionFactory() {
return typeConfiguration.getSessionFactory();
}
@Override
public boolean useStreamForLobBinding() {
return typeConfiguration.getSessionFactory().getWrapperOptions().useStreamForLobBinding();
}
@Override
public int getPreferredSqlTypeCodeForBoolean() {
return typeConfiguration.getCurrentBaseSqlTypeIndicators().getPreferredSqlTypeCodeForBoolean();
}
@Override
public LobCreator getLobCreator() {
return typeConfiguration.getSessionFactory().getWrapperOptions().getLobCreator();
}
@Override
public TimeZone getJdbcTimeZone() {
return typeConfiguration.getSessionFactory().getWrapperOptions().getJdbcTimeZone();
}
}

View File

@ -81,16 +81,12 @@ public class UserTypeSqlTypeAdapter<J> implements JdbcType {
@Override @Override
public <T> JdbcLiteralFormatter<T> getJdbcLiteralFormatter(JavaType<T> javaType) { public <T> JdbcLiteralFormatter<T> getJdbcLiteralFormatter(JavaType<T> javaType) {
if ( !( userType instanceof EnhancedUserType<?> ) ) { if ( !( userType instanceof EnhancedUserType<?> ) ) {
throw new HibernateException( throw new HibernateException( "Could not create JdbcLiteralFormatter because UserType class '"
String.format( + userType.getClass().getName() + "' did not implement EnhancedUserType" );
"Could not create JdbcLiteralFormatter, UserType class [%s] did not implement %s",
userType.getClass().getName(),
EnhancedUserType.class.getName()
)
);
} }
final EnhancedUserType<T> type = (EnhancedUserType<T>) userType; final EnhancedUserType<T> type = (EnhancedUserType<T>) userType;
return (appender, value, dialect, wrapperOptions) -> appender.append( type.toSqlLiteral( value ) ); return (appender, value, dialect, wrapperOptions) ->
appender.append( type.toSqlLiteral( value ) );
} }
private static class ValueExtractorImpl<J> implements ValueExtractor<J> { private static class ValueExtractorImpl<J> implements ValueExtractor<J> {