Skip to content

Commit ca8e1fd

Browse files
committed
Validate against an enum field
1 parent 38062c3 commit ca8e1fd

File tree

5 files changed

+33
-2
lines changed

5 files changed

+33
-2
lines changed

hibernate-core/src/main/java/org/hibernate/dialect/function/OrdinalFunction.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.hibernate.metamodel.mapping.JdbcMapping;
1111
import org.hibernate.query.ReturnableType;
1212
import org.hibernate.query.sqm.function.AbstractSqmSelfRenderingFunctionDescriptor;
13+
import org.hibernate.query.sqm.produce.function.ArgumentTypesValidator;
1314
import org.hibernate.query.sqm.produce.function.StandardArgumentsValidators;
1415
import org.hibernate.query.sqm.produce.function.StandardFunctionReturnTypeResolvers;
1516
import org.hibernate.sql.ast.SqlAstTranslator;
@@ -21,6 +22,8 @@
2122
import org.hibernate.type.descriptor.jdbc.JdbcType;
2223
import org.hibernate.type.spi.TypeConfiguration;
2324

25+
import static org.hibernate.query.sqm.produce.function.FunctionParameterType.ENUM;
26+
2427

2528
/**
2629
* The HQL {@code ordinal()} function returns the ordinal value of an enum
@@ -36,7 +39,10 @@ public class OrdinalFunction
3639
public OrdinalFunction(TypeConfiguration typeConfiguration) {
3740
super(
3841
"ordinal",
39-
StandardArgumentsValidators.exactly( 1 ),
42+
StandardArgumentsValidators.composite(
43+
StandardArgumentsValidators.exactly( 1 ),
44+
new ArgumentTypesValidator( null, ENUM )
45+
),
4046
StandardFunctionReturnTypeResolvers.invariant(
4147
typeConfiguration.getBasicTypeRegistry().resolve( StandardBasicTypes.INTEGER )
4248
),

hibernate-core/src/main/java/org/hibernate/query/sqm/produce/function/ArgumentTypesValidator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ private static boolean isCompatible(FunctionParameterType type, JdbcType jdbcTyp
253253
case IMPLICIT_JSON -> jdbcType.isImplicitJson();
254254
case XML -> jdbcType.isXml();
255255
case IMPLICIT_XML -> jdbcType.isImplicitXml();
256+
case ENUM -> jdbcType.isEnum();
256257
default -> true; // TODO: should we throw here?
257258
};
258259
}

hibernate-core/src/main/java/org/hibernate/query/sqm/produce/function/FunctionParameterType.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,12 @@ public enum FunctionParameterType {
9898
* @since 7.0
9999
*/
100100
IMPLICIT_JSON,
101+
/**
102+
* Indicates that the argument should be an ENUM type
103+
* @see org.hibernate.type.SqlTypes#isEnumType(int)
104+
* @since 7.0
105+
*/
106+
ENUM,
101107
/**
102108
* Indicates that the argument should be a XML type
103109
* @see org.hibernate.type.SqlTypes#isXmlType(int)

hibernate-core/src/main/java/org/hibernate/type/SqlTypes.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,6 +1022,24 @@ public static boolean isImplicitJsonType(int typeCode) {
10221022
}
10231023
}
10241024

1025+
/**
1026+
* Does the typecode represent an ENUM type, whether it's mapped as a native ENUM, a STRING or an INT.
1027+
*
1028+
* @param typeCode - a JDBC type code
1029+
* @since 7.0
1030+
*/
1031+
public static boolean isImplicitEnumType(int typeCode) {
1032+
switch ( typeCode ) {
1033+
case ENUM:
1034+
case VARCHAR:
1035+
case INTEGER:
1036+
case TINYINT:
1037+
return true;
1038+
default:
1039+
return false;
1040+
}
1041+
}
1042+
10251043
/**
10261044
* Does the typecode represent a XML type.
10271045
*

hibernate-core/src/main/java/org/hibernate/type/descriptor/jdbc/JdbcType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ default boolean isArray() {
296296
}
297297

298298
default boolean isEnum() {
299-
return getDdlTypeCode() == ENUM;
299+
return isImplicitEnumType( getDdlTypeCode() );
300300
}
301301

302302
static boolean isArray(int jdbcTypeCode) {

0 commit comments

Comments
 (0)