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

org.hibernate.query.criteria.internal.expression.function.LocateFunction 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.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) {
		renderingContext.getFunctionStack().push( this );

		try {
			final 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();
		}
		finally {
			renderingContext.getFunctionStack().pop();
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy