Skip to content

Commit 2f87f32

Browse files
committed
add appropriate casts on parameters in binary numeric expressions on Informix
1 parent feca553 commit 2f87f32

File tree

3 files changed

+38
-13
lines changed

3 files changed

+38
-13
lines changed

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/InformixDialect.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@
106106
import static org.hibernate.internal.util.JdbcExceptionHelper.extractErrorCode;
107107
import static org.hibernate.query.sqm.produce.function.FunctionParameterType.STRING;
108108
import static org.hibernate.query.sqm.produce.function.StandardFunctionArgumentTypeResolvers.impliedOrInvariant;
109-
import static org.hibernate.type.SqlTypes.BIGINT;
110109
import static org.hibernate.type.SqlTypes.BINARY;
111110
import static org.hibernate.type.SqlTypes.FLOAT;
112111
import static org.hibernate.type.SqlTypes.LONG32NVARCHAR;
@@ -214,8 +213,8 @@ protected String columnType(int sqlTypeCode) {
214213
switch ( sqlTypeCode ) {
215214
case TINYINT:
216215
return "smallint";
217-
case BIGINT:
218-
return "int8";
216+
// case BIGINT:
217+
// return "int8";
219218
case TIME:
220219
return "datetime hour to second";
221220
case TIMESTAMP:
@@ -982,14 +981,13 @@ public void appendDateTimeLiteral(SqlAppender appender, Date date, TemporalType
982981
@Override
983982
public String getSelectClauseNullString(int sqlType, TypeConfiguration typeConfiguration) {
984983
final DdlType descriptor = typeConfiguration.getDdlTypeRegistry().getDescriptor( sqlType );
985-
if ( descriptor == null ) {
986-
// just cast it to an arbitrary SQL type,
987-
// which we expect to be ignored by higher layers
988-
return "cast(null as int)";
989-
}
990-
else {
991-
return "cast(null as " + castType( descriptor ) + ")";
992-
}
984+
final String castType =
985+
descriptor != null
986+
? castType( descriptor )
987+
// just cast it to an arbitrary SQL type,
988+
// which we expect to be ignored by higher layers
989+
: "integer";
990+
return "cast(null as " + castType + ")";
993991
}
994992

995993
private static String castType(DdlType descriptor) {

hibernate-community-dialects/src/main/java/org/hibernate/community/dialect/InformixSqlAstTranslator.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,33 @@ public void visitQuerySpec(QuerySpec querySpec) {
246246
}
247247
}
248248

249+
@Override
250+
protected void visitArithmeticOperand(Expression expression) {
251+
if ( expression instanceof SqmParameterInterpretation
252+
&& expression.getExpressionType() != null
253+
&& expression.getExpressionType().getJdbcTypeCount() == 1 ) {
254+
final String castType =
255+
switch ( expression.getExpressionType().getSingleJdbcMapping().getCastType() ) {
256+
case FLOAT, DOUBLE -> "float" ;
257+
case INTEGER -> "integer" ;
258+
case LONG -> "bigint";
259+
default -> null;
260+
};
261+
if ( castType != null ) {
262+
append( "cast(" );
263+
}
264+
super.visitArithmeticOperand( expression );
265+
if ( castType != null ) {
266+
append( " as " );
267+
append( castType );
268+
append( ")" );
269+
}
270+
}
271+
else {
272+
super.visitArithmeticOperand( expression );
273+
}
274+
}
275+
249276
@Override
250277
public void visitSelfRenderingExpression(SelfRenderingExpression expression) {
251278
final boolean isStringFunctionWithParameterArg =

hibernate-core/src/main/java/org/hibernate/type/descriptor/sql/spi/DdlTypeRegistry.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ public DdlType getDescriptor(int sqlTypeCode) {
135135
}
136136

137137
/**
138-
* Get the SQL type name for the specified {@link java.sql.Types JDBC type code},
139-
* filling in the placemarkers {@code $l}, {@code $p}, and {@code $s}
138+
* Get the SQL type name for the specified {@linkplain java.sql.Types JDBC
139+
* type code}, filling in the placemarkers {@code $l}, {@code $p}, and {@code $s}
140140
* with the default length, precision, and scale for the given SQL dialect.
141141
*
142142
* @param typeCode the JDBC type code

0 commit comments

Comments
 (0)