org.hibernate.query.criteria.internal.expression.function.LocateFunction 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
Hibernate's core ORM functionality
/*
* 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 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;
import org.hibernate.query.criteria.internal.expression.LiteralExpression;
/**
* Models the ANSI SQL LOCATE function.
*
* @author Steve Ebersole
*/
public class LocateFunction
extends BasicFunctionExpression
implements Serializable {
public static final String NAME = "locate";
private final Expression pattern;
private final Expression string;
private final Expression start;
public LocateFunction(
CriteriaBuilderImpl criteriaBuilder,
Expression pattern,
Expression string,
Expression start) {
super( criteriaBuilder, Integer.class, NAME );
this.pattern = pattern;
this.string = string;
this.start = start;
}
public LocateFunction(
CriteriaBuilderImpl criteriaBuilder,
Expression pattern,
Expression string) {
this( criteriaBuilder, pattern, string, null );
}
public LocateFunction(CriteriaBuilderImpl criteriaBuilder, String pattern, Expression string) {
this(
criteriaBuilder,
new LiteralExpression( criteriaBuilder, pattern ),
string,
null
);
}
public LocateFunction(CriteriaBuilderImpl criteriaBuilder, String pattern, Expression string, int start) {
this(
criteriaBuilder,
new LiteralExpression( criteriaBuilder, pattern ),
string,
new LiteralExpression( criteriaBuilder, start )
);
}
public Expression getPattern() {
return pattern;
}
public Expression getStart() {
return start;
}
public Expression getString() {
return string;
}
@Override
public void registerParameters(ParameterRegistry registry) {
Helper.possibleParameter( getPattern(), registry );
Helper.possibleParameter( getStart(), registry );
Helper.possibleParameter( getString(), registry );
}
@Override
public String render(RenderingContext renderingContext) {
StringBuilder buffer = new StringBuilder();
buffer.append( "locate(" )
.append( ( (Renderable) getPattern() ).render( renderingContext ) )
.append( ',' )
.append( ( (Renderable) getString() ).render( renderingContext ) );
if ( getStart() != null ) {
buffer.append( ',' )
.append( ( (Renderable) getStart() ).render( renderingContext ) );
}
buffer.append( ')' );
return buffer.toString();
}
}