/*
 * 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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
 */
package org.hibernate.dialect.function;
import java.util.List;

import org.hibernate.QueryException;
import org.hibernate.engine.spi.Mapping;
import org.hibernate.engine.spi.SessionFactoryImplementor;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.Type;

A Caché defintion of a convert function.
Author:Jonathan Levinson
/** * A Cach&eacute; defintion of a convert function. * * @author Jonathan Levinson */
public class ConvertFunction implements SQLFunction { @Override public boolean hasArguments() { return true; } @Override public boolean hasParenthesesIfNoArguments() { return true; } @Override public Type getReturnType(Type firstArgumentType, Mapping mapping) throws QueryException { return StandardBasicTypes.STRING; } @Override public String render(Type firstArgumentType, List args, SessionFactoryImplementor factory) throws QueryException { if ( args.size() != 2 && args.size() != 3 ) { throw new QueryException( "convert() requires two or three arguments" ); } final String type = (String) args.get( 1 ); if ( args.size() == 2 ) { return "{fn convert(" + args.get( 0 ) + " , " + type + ")}"; } else { return "convert(" + args.get( 0 ) + " , " + type + "," + args.get( 2 ) + ")"; } } }