package org.hibernate.jpa.criteria.expression.function;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import javax.persistence.criteria.Expression;
import org.hibernate.jpa.criteria.CriteriaBuilderImpl;
import org.hibernate.jpa.criteria.ParameterContainer;
import org.hibernate.jpa.criteria.ParameterRegistry;
import org.hibernate.jpa.criteria.Renderable;
import org.hibernate.jpa.criteria.compile.RenderingContext;
public class ParameterizedFunctionExpression<X>
extends BasicFunctionExpression<X>
implements FunctionExpression<X> {
public static List<String> STANDARD_JPA_FUNCTION_NAMES = Arrays.asList(
"CONCAT",
"SUBSTRING",
"TRIM",
"UPPER",
"LOWER",
"LOCATE",
"LENGTH",
"ABS",
"SQRT",
"MOD",
"SIZE",
"INDEX",
"CURRENT_DATE",
"CURRENT_TIME",
"CURRENT_TIMESTAMP"
);
private final List<Expression<?>> argumentExpressions;
private final boolean isStandardJpaFunction;
public ParameterizedFunctionExpression(
CriteriaBuilderImpl criteriaBuilder,
Class<X> javaType,
String functionName,
List<Expression<?>> argumentExpressions) {
super( criteriaBuilder, javaType, functionName );
this.argumentExpressions = argumentExpressions;
this.isStandardJpaFunction = STANDARD_JPA_FUNCTION_NAMES.contains( functionName.toUpperCase(Locale.ROOT) );
}
public ParameterizedFunctionExpression(
CriteriaBuilderImpl criteriaBuilder,
Class<X> javaType,
String functionName,
Expression<?>... argumentExpressions) {
super( criteriaBuilder, javaType, functionName );
this.argumentExpressions = Arrays.asList( argumentExpressions );
this.isStandardJpaFunction = STANDARD_JPA_FUNCTION_NAMES.contains( functionName.toUpperCase(Locale.ROOT) );
}
protected boolean isStandardJpaFunction() {
return isStandardJpaFunction;
}
protected static int properSize(int number) {
return number + (int)( number*.75 ) + 1;
}
public List<Expression<?>> getArgumentExpressions() {
return argumentExpressions;
}
@Override
public void registerParameters(ParameterRegistry registry) {
for ( Expression argument : getArgumentExpressions() ) {
if ( ParameterContainer.class.isInstance( argument ) ) {
( (ParameterContainer) argument ).registerParameters(registry);
}
}
}
@Override
public String render(RenderingContext renderingContext) {
StringBuilder buffer = new StringBuilder();
if ( isStandardJpaFunction() ) {
buffer.append( getFunctionName() )
.append( "(" );
}
else {
buffer.append( "function('" )
.append( getFunctionName() )
.append( "', " );
}
renderArguments( buffer, renderingContext );
buffer.append( ')' );
return buffer.toString();
}
protected void renderArguments(StringBuilder buffer, RenderingContext renderingContext) {
String sep = "";
for ( Expression argument : argumentExpressions ) {
buffer.append( sep ).append( ( (Renderable) argument ).render( renderingContext ) );
sep = ", ";
}
}
}