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
public <T> JdbcLiteralFormatter<T> getJdbcLiteralFormatter(JavaType<T> javaType) {
return (appender, value, dialect, wrapperOptions) -> dialect.appendIntervalLiteral(
appender,
javaType.unwrap(
value,
Duration.class,
wrapperOptions
)
);
return (appender, value, dialect, wrapperOptions) ->
dialect.appendIntervalLiteral( appender, javaType.unwrap( value, Duration.class, wrapperOptions ) );
}
@Override

View File

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

View File

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

View File

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

View File

@ -23,7 +23,7 @@ import org.hibernate.type.spi.TypeConfiguration;
/**
* 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
*/

View File

@ -4,6 +4,7 @@
*/
package org.hibernate.dialect.aggregate;
import org.hibernate.HibernateException;
import org.hibernate.boot.model.naming.Identifier;
import org.hibernate.boot.model.relational.AuxiliaryDatabaseObject;
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.sql.DdlType;
import org.hibernate.type.descriptor.sql.spi.DdlTypeRegistry;
import org.hibernate.type.internal.TypeConfigurationWrapperOptions;
import org.hibernate.type.spi.TypeConfiguration;
import java.util.LinkedHashMap;
@ -76,26 +76,15 @@ public class OracleAggregateSupport extends AggregateSupportImpl {
public static AggregateSupport valueOf(Dialect dialect) {
final DatabaseVersion version = dialect.getVersion();
switch ( version.getMajor() ) {
case 12:
case 13:
case 14:
case 15:
case 16:
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.LEGACY_INSTANCE;
return switch ( version.getMajor() ) {
case 12, 13, 14, 15, 16, 17 -> V12_INSTANCE;
case 18 -> V18_INSTANCE;
case 19, 20 -> V19_INSTANCE;
case 21, 22 -> V21_INSTANCE;
default -> version.isSameOrAfter( 23 )
? OracleAggregateSupport.V23_INSTANCE
: OracleAggregateSupport.LEGACY_INSTANCE;
};
}
@Override
@ -132,7 +121,7 @@ public class OracleAggregateSupport extends AggregateSupportImpl {
final JdbcLiteralFormatter<Boolean> jdbcLiteralFormatter = (JdbcLiteralFormatter<Boolean>) column.getJdbcMapping().getJdbcType()
.getJdbcLiteralFormatter( column.getJdbcMapping().getMappedJavaType() );
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 falseLiteral = jdbcLiteralFormatter.toJdbcLiteral( false, dialect, wrapperOptions );
return template.replace(
@ -242,7 +231,7 @@ public class OracleAggregateSupport extends AggregateSupportImpl {
final JdbcLiteralFormatter<Boolean> jdbcLiteralFormatter = (JdbcLiteralFormatter<Boolean>) column.getJdbcMapping().getJdbcType()
.getJdbcLiteralFormatter( column.getJdbcMapping().getMappedJavaType() );
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 falseLiteral = jdbcLiteralFormatter.toJdbcLiteral( false, dialect, wrapperOptions );
return template.replace(
@ -318,6 +307,15 @@ public class OracleAggregateSupport extends AggregateSupportImpl {
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) {
final String extractArguments;
int separatorIndex;
@ -433,7 +431,7 @@ public class OracleAggregateSupport extends AggregateSupportImpl {
final JdbcLiteralFormatter<Boolean> jdbcLiteralFormatter = (JdbcLiteralFormatter<Boolean>) jdbcMapping.getJdbcType()
.getJdbcLiteralFormatter( jdbcMapping.getMappedJavaType() );
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 falseLiteral = jdbcLiteralFormatter.toJdbcLiteral( false, dialect, wrapperOptions );
return "decode(" + customWriteExpression + "," + trueLiteral + ",'true'," + falseLiteral + ",'false')";
@ -458,7 +456,7 @@ public class OracleAggregateSupport extends AggregateSupportImpl {
final JdbcLiteralFormatter<Boolean> jdbcLiteralFormatter = (JdbcLiteralFormatter<Boolean>) jdbcMapping.getJdbcType()
.getJdbcLiteralFormatter( jdbcMapping.getMappedJavaType() );
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 falseLiteral = jdbcLiteralFormatter.toJdbcLiteral( false, dialect, wrapperOptions );
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
*/
default String toJdbcLiteral(T value, Dialect dialect, WrapperOptions wrapperOptions) {
final StringBuilder sb = new StringBuilder();
appendJdbcLiteral( new StringBuilderSqlAppender( sb ), value, dialect, wrapperOptions );
return sb.toString();
final StringBuilder result = new StringBuilder();
appendJdbcLiteral( new StringBuilderSqlAppender( result ), value, dialect, wrapperOptions );
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}?
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
public void appendJdbcLiteral(SqlAppender appender, Object value, Dialect dialect, WrapperOptions wrapperOptions) {
dialect.appendArrayLiteral(
appender,
unwrap( value, Object[].class, wrapperOptions ),
elementFormatter,
wrapperOptions
);
dialect.appendArrayLiteral( appender, unwrapArray( value, wrapperOptions ), elementFormatter, wrapperOptions );
}
private Object[] unwrapArray(Object value, WrapperOptions wrapperOptions) {
return unwrap( value, Object[].class, wrapperOptions );
}
}

View File

@ -32,60 +32,28 @@ public class JdbcLiteralFormatterTemporal<T> extends BasicJdbcLiteralFormatter<T
public void appendJdbcLiteral(SqlAppender appender, Object value, Dialect dialect, WrapperOptions options) {
final TimeZone jdbcTimeZone = getJdbcTimeZone( options );
// for performance reasons, avoid conversions if we can
if ( value instanceof java.util.Date ) {
dialect.appendDateTimeLiteral(
appender,
(java.util.Date) value,
precision,
jdbcTimeZone
);
if ( value instanceof java.util.Date date ) {
dialect.appendDateTimeLiteral( appender, date, precision, jdbcTimeZone );
}
else if ( value instanceof java.util.Calendar ) {
dialect.appendDateTimeLiteral(
appender,
(java.util.Calendar) value,
precision,
jdbcTimeZone
);
else if ( value instanceof java.util.Calendar calendar ) {
dialect.appendDateTimeLiteral( appender, calendar, precision, jdbcTimeZone );
}
else if ( value instanceof TemporalAccessor ) {
dialect.appendDateTimeLiteral(
appender,
(TemporalAccessor) value,
precision,
jdbcTimeZone
);
else if ( value instanceof TemporalAccessor temporalAccessor ) {
dialect.appendDateTimeLiteral( appender, temporalAccessor, precision, jdbcTimeZone );
}
else {
switch ( precision ) {
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;
}
dialect.appendDateTimeLiteral( appender, unwrap( value, options ), precision, jdbcTimeZone );
}
}
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) {
return options == null || options.getJdbcTimeZone() == null
? TimeZone.getDefault()

View File

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

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
public <T> JdbcLiteralFormatter<T> getJdbcLiteralFormatter(JavaType<T> javaType) {
if ( !( userType instanceof EnhancedUserType<?> ) ) {
throw new HibernateException(
String.format(
"Could not create JdbcLiteralFormatter, UserType class [%s] did not implement %s",
userType.getClass().getName(),
EnhancedUserType.class.getName()
)
);
throw new HibernateException( "Could not create JdbcLiteralFormatter because UserType class '"
+ userType.getClass().getName() + "' did not implement EnhancedUserType" );
}
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> {