From 8b4d6b75bc53b02c09b4f2c38ad8bb775aa1dad3 Mon Sep 17 00:00:00 2001 From: Gavin King Date: Fri, 13 Dec 2024 23:45:23 +0100 Subject: [PATCH] clean up formatters and remove TypeConfigurationWrapperOptions It's wrong to build a WrapperOptions from a TypeConfiguration --- .../H2DurationIntervalSecondJdbcType.java | 10 +-- .../hibernate/dialect/OracleEnumJdbcType.java | 8 ++- .../dialect/PostgreSQLEnumJdbcType.java | 9 ++- .../PostgreSQLIntervalSecondJdbcType.java | 10 +-- .../dialect/aggregate/AggregateSupport.java | 2 +- .../aggregate/OracleAggregateSupport.java | 48 +++++++------- .../descriptor/jdbc/JdbcLiteralFormatter.java | 6 +- .../type/descriptor/jdbc/JdbcType.java | 3 +- .../internal/JdbcLiteralFormatterArray.java | 11 ++-- .../JdbcLiteralFormatterTemporal.java | 62 +++++-------------- .../JdbcLiteralFormatterUUIDData.java | 3 +- .../TypeConfigurationWrapperOptions.java | 58 ----------------- .../type/internal/UserTypeSqlTypeAdapter.java | 12 ++-- 13 files changed, 69 insertions(+), 173 deletions(-) delete mode 100644 hibernate-core/src/main/java/org/hibernate/type/internal/TypeConfigurationWrapperOptions.java diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/H2DurationIntervalSecondJdbcType.java b/hibernate-core/src/main/java/org/hibernate/dialect/H2DurationIntervalSecondJdbcType.java index 9baf99f8c9..e0e4e46996 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/H2DurationIntervalSecondJdbcType.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/H2DurationIntervalSecondJdbcType.java @@ -45,14 +45,8 @@ public class H2DurationIntervalSecondJdbcType implements JdbcType { @Override public JdbcLiteralFormatter getJdbcLiteralFormatter(JavaType 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 diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/OracleEnumJdbcType.java b/hibernate-core/src/main/java/org/hibernate/dialect/OracleEnumJdbcType.java index 175c6d17d4..42aaf82b2f 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/OracleEnumJdbcType.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/OracleEnumJdbcType.java @@ -63,9 +63,11 @@ public class OracleEnumJdbcType implements JdbcType { public JdbcLiteralFormatter getJdbcLiteralFormatter(JavaType javaType) { @SuppressWarnings("unchecked") final Class> enumClass = (Class>) 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 diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLEnumJdbcType.java b/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLEnumJdbcType.java index 5df1a60ec4..d6865273fd 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLEnumJdbcType.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLEnumJdbcType.java @@ -64,9 +64,12 @@ public class PostgreSQLEnumJdbcType implements JdbcType { public JdbcLiteralFormatter getJdbcLiteralFormatter(JavaType javaType) { @SuppressWarnings("unchecked") final Class> enumClass = (Class>) 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 diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLIntervalSecondJdbcType.java b/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLIntervalSecondJdbcType.java index 61e1c5e4c0..b5aa756db2 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLIntervalSecondJdbcType.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/PostgreSQLIntervalSecondJdbcType.java @@ -70,14 +70,8 @@ public class PostgreSQLIntervalSecondJdbcType implements AdjustableJdbcType { @Override public JdbcLiteralFormatter getJdbcLiteralFormatter(JavaType 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 diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/aggregate/AggregateSupport.java b/hibernate-core/src/main/java/org/hibernate/dialect/aggregate/AggregateSupport.java index 7457b23d46..31414c3f90 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/aggregate/AggregateSupport.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/aggregate/AggregateSupport.java @@ -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 */ diff --git a/hibernate-core/src/main/java/org/hibernate/dialect/aggregate/OracleAggregateSupport.java b/hibernate-core/src/main/java/org/hibernate/dialect/aggregate/OracleAggregateSupport.java index 531d69040d..8394a4dad9 100644 --- a/hibernate-core/src/main/java/org/hibernate/dialect/aggregate/OracleAggregateSupport.java +++ b/hibernate-core/src/main/java/org/hibernate/dialect/aggregate/OracleAggregateSupport.java @@ -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 jdbcLiteralFormatter = (JdbcLiteralFormatter) 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 jdbcLiteralFormatter = (JdbcLiteralFormatter) 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 jdbcLiteralFormatter = (JdbcLiteralFormatter) 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 jdbcLiteralFormatter = (JdbcLiteralFormatter) 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')"; diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/JdbcLiteralFormatter.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/JdbcLiteralFormatter.java index f3ba0b12b4..200947c915 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/JdbcLiteralFormatter.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/JdbcLiteralFormatter.java @@ -31,9 +31,9 @@ public interface JdbcLiteralFormatter 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(); } /** diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/JdbcType.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/JdbcType.java index 25c2fd5c57..bff178a26c 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/JdbcType.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/JdbcType.java @@ -118,7 +118,8 @@ public interface JdbcType extends Serializable { */ // todo (6.0) : move to {@link org.hibernate.metamodel.mapping.JdbcMapping}? default JdbcLiteralFormatter getJdbcLiteralFormatter(JavaType javaType) { - return (appender, value, dialect, wrapperOptions) -> appender.appendSql( value.toString() ); + return (appender, value, dialect, wrapperOptions) -> + appender.appendSql( value.toString() ); } /** diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/internal/JdbcLiteralFormatterArray.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/internal/JdbcLiteralFormatterArray.java index d694bdc7f1..f444ad2cb4 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/internal/JdbcLiteralFormatterArray.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/internal/JdbcLiteralFormatterArray.java @@ -23,11 +23,10 @@ public class JdbcLiteralFormatterArray extends BasicJdbcLiteralFormatter { @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 ); } } diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/internal/JdbcLiteralFormatterTemporal.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/internal/JdbcLiteralFormatterTemporal.java index 6697f86363..8e0542e3c8 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/internal/JdbcLiteralFormatterTemporal.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/internal/JdbcLiteralFormatterTemporal.java @@ -32,60 +32,28 @@ public class JdbcLiteralFormatterTemporal extends BasicJdbcLiteralFormatter 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() diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/internal/JdbcLiteralFormatterUUIDData.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/internal/JdbcLiteralFormatterUUIDData.java index c7dbf742b7..4d06305974 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/internal/JdbcLiteralFormatterUUIDData.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/internal/JdbcLiteralFormatterUUIDData.java @@ -24,7 +24,6 @@ public class JdbcLiteralFormatterUUIDData extends BasicJdbcLiteralFormatter implements JdbcType { @Override public JdbcLiteralFormatter getJdbcLiteralFormatter(JavaType 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 type = (EnhancedUserType) 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 implements ValueExtractor {