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

org.hibernate.query.criteria.internal.expression.function.TrimFunction 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 javax.persistence.criteria.CriteriaBuilder.Trimspec;
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 TRIM function.
 *
 * @author Steve Ebersole
 * @author Brett Meyer
 */
public class TrimFunction
		extends BasicFunctionExpression
		implements Serializable {
	public static final String NAME = "trim";
	public static final Trimspec DEFAULT_TRIMSPEC = Trimspec.BOTH;
	public static final char DEFAULT_TRIM_CHAR = ' ';

	private final Trimspec trimspec;
	private final Expression trimCharacter;
	private final Expression trimSource;

	public TrimFunction(
			CriteriaBuilderImpl criteriaBuilder,
			Trimspec trimspec,
			Expression trimCharacter,
			Expression trimSource) {
		super( criteriaBuilder, String.class, NAME );
		this.trimspec = trimspec;
		this.trimCharacter = trimCharacter;
		this.trimSource = trimSource;
	}

	public TrimFunction(
			CriteriaBuilderImpl criteriaBuilder,
			Trimspec trimspec,
			char trimCharacter,
			Expression trimSource) {
		super( criteriaBuilder, String.class, NAME );
		this.trimspec = trimspec;
		this.trimCharacter = new LiteralExpression( criteriaBuilder, trimCharacter );
		this.trimSource = trimSource;
	}

	public TrimFunction(
			CriteriaBuilderImpl criteriaBuilder,
			Expression trimSource) {
		this( criteriaBuilder, DEFAULT_TRIMSPEC, DEFAULT_TRIM_CHAR, trimSource );
	}

	public TrimFunction(
			CriteriaBuilderImpl criteriaBuilder,
			Expression trimCharacter,
			Expression trimSource) {
		this( criteriaBuilder, DEFAULT_TRIMSPEC, trimCharacter, trimSource );
	}

	public TrimFunction(
			CriteriaBuilderImpl criteriaBuilder,
			char trimCharacter,
			Expression trimSource) {
		this( criteriaBuilder, DEFAULT_TRIMSPEC, trimCharacter, trimSource );
	}

	public TrimFunction(
			CriteriaBuilderImpl criteriaBuilder,
			Trimspec trimspec,
			Expression trimSource) {
		this( criteriaBuilder, trimspec, DEFAULT_TRIM_CHAR, trimSource );
	}

	public Expression getTrimCharacter() {
		return trimCharacter;
	}

	public Expression getTrimSource() {
		return trimSource;
	}

	public Trimspec getTrimspec() {
		return trimspec;
	}

	@Override
	public void registerParameters(ParameterRegistry registry) {
		Helper.possibleParameter( getTrimCharacter(), registry );
		Helper.possibleParameter( getTrimSource(), registry );
	}

	@Override
	public String render(RenderingContext renderingContext) {
		renderingContext.getFunctionStack().push( this );

		try {
			String renderedTrimChar;
			if ( trimCharacter.getClass().isAssignableFrom( LiteralExpression.class ) ) {
				// If the character is a literal, treat it as one.  A few dialects
				// do not support parameters as trim() arguments.
				renderedTrimChar = '\'' + ( (LiteralExpression)
						trimCharacter ).getLiteral().toString() + '\'';
			}
			else {
				renderedTrimChar = ( (Renderable) trimCharacter ).render( renderingContext );
			}
			return new StringBuilder()
					.append( "trim(" )
					.append( trimspec.name() )
					.append( ' ' )
					.append( renderedTrimChar )
					.append( " from " )
					.append( ( (Renderable) trimSource ).render( renderingContext ) )
					.append( ')' )
					.toString();
		}
		finally {
			renderingContext.getFunctionStack().pop();
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy