|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.dialect.function; |
| 6 | + |
| 7 | +import org.hibernate.QueryException; |
| 8 | +import org.hibernate.metamodel.mapping.JdbcMapping; |
| 9 | +import org.hibernate.metamodel.model.domain.ReturnableType; |
| 10 | +import org.hibernate.query.sqm.function.AbstractSqmSelfRenderingFunctionDescriptor; |
| 11 | +import org.hibernate.query.sqm.produce.function.ArgumentTypesValidator; |
| 12 | +import org.hibernate.query.sqm.produce.function.StandardFunctionReturnTypeResolvers; |
| 13 | +import org.hibernate.sql.ast.SqlAstTranslator; |
| 14 | +import org.hibernate.sql.ast.spi.SqlAppender; |
| 15 | +import org.hibernate.sql.ast.tree.SqlAstNode; |
| 16 | +import org.hibernate.sql.ast.tree.expression.Expression; |
| 17 | +import org.hibernate.type.SqlTypes; |
| 18 | +import org.hibernate.type.StandardBasicTypes; |
| 19 | +import org.hibernate.type.descriptor.java.EnumJavaType; |
| 20 | +import org.hibernate.type.descriptor.jdbc.JdbcType; |
| 21 | +import org.hibernate.type.spi.TypeConfiguration; |
| 22 | + |
| 23 | +import java.util.List; |
| 24 | + |
| 25 | +import static org.hibernate.query.sqm.produce.function.FunctionParameterType.ENUM; |
| 26 | + |
| 27 | + |
| 28 | +/** |
| 29 | + * The HQL {@code string()} function returns the string value of an enum |
| 30 | + * <p> |
| 31 | + * For enum fields mapped as STRING or ENUM it's a synonym for {@code cast(x as String)}. Same as {@link CastStrEmulation}. |
| 32 | + * For enum fields mapped as ORDINAL it's a case statement that returns the bane if enum. |
| 33 | + * |
| 34 | + * @author Luca Molteni, Cedomir Igaly |
| 35 | + */ |
| 36 | +public class StringFunction |
| 37 | + extends AbstractSqmSelfRenderingFunctionDescriptor { |
| 38 | + |
| 39 | + public StringFunction(TypeConfiguration typeConfiguration) { |
| 40 | + super( |
| 41 | + "string", |
| 42 | + new ArgumentTypesValidator( null, ENUM ), |
| 43 | + StandardFunctionReturnTypeResolvers.invariant( |
| 44 | + typeConfiguration.getBasicTypeRegistry().resolve( StandardBasicTypes.STRING ) |
| 45 | + ), |
| 46 | + null |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void render( |
| 52 | + SqlAppender sqlAppender, |
| 53 | + List<? extends SqlAstNode> arguments, |
| 54 | + ReturnableType<?> returnType, |
| 55 | + SqlAstTranslator<?> walker) { |
| 56 | + Expression singleExpression = (Expression) arguments.get( 0 ); |
| 57 | + |
| 58 | + JdbcMapping singleJdbcMapping = singleExpression.getExpressionType().getSingleJdbcMapping(); |
| 59 | + JdbcType argumentType = singleJdbcMapping.getJdbcType(); |
| 60 | + |
| 61 | + if ( argumentType.isString() || argumentType.getDefaultSqlTypeCode() == SqlTypes.ENUM ) { |
| 62 | + singleExpression.accept( walker ); |
| 63 | + } |
| 64 | + else if ( argumentType.isInteger() ) { |
| 65 | + EnumJavaType<?> enumJavaType = (EnumJavaType<?>) singleJdbcMapping.getMappedJavaType(); |
| 66 | + Object[] enumConstants = enumJavaType.getJavaTypeClass().getEnumConstants(); |
| 67 | + |
| 68 | + sqlAppender.appendSql( "case " ); |
| 69 | + singleExpression.accept( walker ); |
| 70 | + for ( Object e : enumConstants ) { |
| 71 | + Enum<?> enumValue = (Enum<?>) e; |
| 72 | + sqlAppender.appendSql( " when " ); |
| 73 | + sqlAppender.appendSql( enumValue.ordinal() ); |
| 74 | + sqlAppender.appendSql( " then " ); |
| 75 | + sqlAppender.appendSingleQuoteEscapedString( enumValue.name() ); |
| 76 | + } |
| 77 | + sqlAppender.appendSql( " end" ); |
| 78 | + } |
| 79 | + else { |
| 80 | + throw new QueryException( "Unsupported enum type passed to 'string()' function: " + argumentType ); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public String getArgumentListSignature() { |
| 86 | + return "(ENUM arg)"; |
| 87 | + } |
| 88 | +} |
0 commit comments