HHH-13016 Bring back CaseLiteralExpression

It was removed as part of
https://github.com/hibernate/hibernate-orm/pull/1361 but this PR didn't
fix all the issues as there are still cases where we don't deduce the
expected type and we need to have a proper cast.
This commit is contained in:
Guillaume Smet 2019-01-17 13:23:31 +01:00
parent 624403e65c
commit 23153d5d55
3 changed files with 41 additions and 3 deletions

View File

@ -0,0 +1,38 @@
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.query.criteria.internal.expression;
import org.hibernate.query.criteria.internal.CriteriaBuilderImpl;
import org.hibernate.query.criteria.internal.compile.RenderingContext;
import org.hibernate.query.criteria.internal.expression.function.CastFunction;
/**
* @author Andrea Boriero
*/
public class CaseLiteralExpression<T> extends LiteralExpression<T> {
public CaseLiteralExpression(CriteriaBuilderImpl criteriaBuilder, Class<T> type, T literal) {
super( criteriaBuilder, type, literal );
}
@Override
public String render(RenderingContext renderingContext) {
// There's no need to cast a boolean value and it actually breaks on
// MySQL and MariaDB because they don't support casting to bit.
// Skip the cast for a boolean literal.
if ( getJavaType() == Boolean.class && Boolean.class.isInstance( getLiteral() ) ) {
return super.render( renderingContext );
}
// wrapping the result in a cast to determine the node type during the antlr hql parsing phase
return CastFunction.CAST_NAME + '(' +
super.render( renderingContext ) +
" as " +
renderingContext.getCastType( getJavaType() ) +
')';
}
}

View File

@ -9,7 +9,7 @@ package org.hibernate.query.criteria.internal.expression;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.function.BiFunction;
import javax.persistence.criteria.CriteriaBuilder.Case;
import javax.persistence.criteria.Expression;
@ -69,7 +69,7 @@ public class SearchedCaseExpression<R>
final Class<R> type = result != null
? (Class<R>) result.getClass()
: getJavaType();
return new LiteralExpression<R>( criteriaBuilder(), type, result );
return new CaseLiteralExpression<R>( criteriaBuilder(), type, result );
}
public Case<R> when(Expression<Boolean> condition, Expression<? extends R> result) {

View File

@ -76,7 +76,7 @@ public class SimpleCaseExpression<C,R>
final Class<R> type = result != null
? (Class<R>) result.getClass()
: getJavaType();
return new LiteralExpression<R>( criteriaBuilder(), type, result );
return new CaseLiteralExpression<R>( criteriaBuilder(), type, result );
}
public SimpleCase<C, R> when(C condition, Expression<? extends R> result) {