All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.hibernate.query.criteria.internal.expression.function.CastFunction Maven / Gradle / Ivy

There is a newer version: 7.0.0.Alpha1
Show newest version
/*
 * 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();
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy