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

org.hibernate.query.criteria.internal.expression.SearchedCaseExpression Maven / Gradle / Ivy

There is a newer version: 6.5.0.CR2
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;

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;

import org.hibernate.query.criteria.internal.CriteriaBuilderImpl;
import org.hibernate.query.criteria.internal.ParameterRegistry;
import org.hibernate.query.criteria.internal.Renderable;
import org.hibernate.query.criteria.internal.compile.RenderingContext;

/**
 * Models what ANSI SQL terms a searched case expression.  This is a CASE expression
 * in the form
 * CASE
 *     WHEN [firstCondition] THEN [firstResult]
 *     WHEN [secondCondition] THEN [secondResult]
 *     ELSE [defaultResult]
 * END
 * 
* * @author Steve Ebersole */ public class SearchedCaseExpression extends ExpressionImpl implements Case, Serializable { private List whenClauses = new ArrayList(); private Expression otherwiseResult; public class WhenClause { private final Expression condition; private final Expression result; public WhenClause(Expression condition, Expression result) { this.condition = condition; this.result = result; } public Expression getCondition() { return condition; } public Expression getResult() { return result; } } public SearchedCaseExpression( CriteriaBuilderImpl criteriaBuilder, Class javaType) { super( criteriaBuilder, javaType ); } public Case when(Expression condition, R result) { return when( condition, buildLiteral( result ) ); } @SuppressWarnings({"unchecked"}) private LiteralExpression buildLiteral(R result) { final Class type = result != null ? (Class) result.getClass() : getJavaType(); return new LiteralExpression( criteriaBuilder(), type, result ); } public Case when(Expression condition, Expression result) { WhenClause whenClause = new WhenClause( condition, result ); whenClauses.add( whenClause ); resetJavaType( result.getJavaType() ); return this; } public Expression otherwise(R result) { return otherwise( buildLiteral( result ) ); } public Expression otherwise(Expression result) { this.otherwiseResult = result; resetJavaType( result.getJavaType() ); return this; } public Expression getOtherwiseResult() { return otherwiseResult; } public List getWhenClauses() { return whenClauses; } public void registerParameters(ParameterRegistry registry) { Helper.possibleParameter( getOtherwiseResult(), registry ); for ( WhenClause whenClause : getWhenClauses() ) { Helper.possibleParameter( whenClause.getCondition(), registry ); Helper.possibleParameter( whenClause.getResult(), registry ); } } public String render(RenderingContext renderingContext) { StringBuilder caseStatement = new StringBuilder( "case" ); for ( WhenClause whenClause : getWhenClauses() ) { caseStatement.append( " when " ) .append( ( (Renderable) whenClause.getCondition() ).render( renderingContext ) ) .append( " then " ) .append( ( (Renderable) whenClause.getResult() ).render( renderingContext ) ); } Expression otherwiseResult = getOtherwiseResult(); if ( otherwiseResult != null ) { caseStatement.append( " else " ) .append( ( (Renderable) otherwiseResult ).render( renderingContext ) ); } caseStatement.append( " end" ); return caseStatement.toString(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy