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

org.hibernate.dialect.function.SQLFunctionTemplate 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.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.Type;

/**
 * Represents HQL functions that can have different representations in different SQL dialects where that
 * difference can be handled via a template/pattern.
 * 

* E.g. in HQL we can define function concat(?1, ?2) to concatenate two strings * p1 and p2. Dialects would register different versions of this class *using the same name* (concat) but with * different templates or patterns; (?1 || ?2) for Oracle, concat(?1, ?2) for MySql, * (?1 + ?2) for MS SQL. Each dialect will define a template as a string (exactly like above) marking function * parameters with '?' followed by parameter's index (first index is 1). * * @author Alexey Loubyansky */ public class SQLFunctionTemplate implements SQLFunction { private final Type type; private final TemplateRenderer renderer; private final boolean hasParenthesesIfNoArgs; /** * Constructs a SQLFunctionTemplate * * @param type The functions return type * @param template The function template */ public SQLFunctionTemplate(Type type, String template) { this( type, template, true ); } /** * Constructs a SQLFunctionTemplate * * @param type The functions return type * @param template The function template * @param hasParenthesesIfNoArgs If there are no arguments, are parentheses required? */ public SQLFunctionTemplate(Type type, String template, boolean hasParenthesesIfNoArgs) { this.type = type; this.renderer = new TemplateRenderer( template ); this.hasParenthesesIfNoArgs = hasParenthesesIfNoArgs; } @Override public String render(Type argumentType, List args, SessionFactoryImplementor factory) { return renderer.render( args, factory ); } @Override public Type getReturnType(Type argumentType, Mapping mapping) throws QueryException { return type; } @Override public boolean hasArguments() { return renderer.getAnticipatedNumberOfArguments() > 0; } @Override public boolean hasParenthesesIfNoArguments() { return hasParenthesesIfNoArgs; } @Override public String toString() { return renderer.getTemplate(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy