org.hibernate.query.criteria.internal.expression.function.CastFunction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate-core Show documentation
Show all versions of hibernate-core Show documentation
JPMS Module-Info's for a few of the Jakarta Libraries just until they add them in themselves
/*
* 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 .
*/
package org.hibernate.query.criteria.internal.expression.function;
import java.io.Serializable;
import java.util.Locale;
import org.hibernate.query.criteria.internal.CriteriaBuilderImpl;
import org.hibernate.query.criteria.internal.ParameterRegistry;
import org.hibernate.query.criteria.internal.compile.RenderingContext;
import org.hibernate.query.criteria.internal.expression.ExpressionImpl;
/**
* Models a CAST function.
*
* @param The cast result type.
* @param The type of the expression to be cast.
*
* @author Steve Ebersole
*/
public class CastFunction
extends BasicFunctionExpression
implements FunctionExpression, Serializable {
public static final String CAST_NAME = "cast";
private final ExpressionImpl castSource;
public CastFunction(
CriteriaBuilderImpl criteriaBuilder,
Class javaType,
ExpressionImpl castSource) {
super( criteriaBuilder, javaType, CAST_NAME );
this.castSource = castSource;
}
public ExpressionImpl getCastSource() {
return castSource;
}
@Override
public void registerParameters(ParameterRegistry registry) {
Helper.possibleParameter( getCastSource(), registry );
}
@Override
public String render(RenderingContext renderingContext) {
renderingContext.getFunctionStack().push( this );
try {
return String.format(
Locale.ROOT,
"cast(%s as %s)",
castSource.render( renderingContext ),
renderingContext.getCastType( getJavaType() )
);
}
finally {
renderingContext.getFunctionStack().pop();
}
}
}