org.hibernate.dialect.function.SQLFunction Java Examples

The following examples show how to use org.hibernate.dialect.function.SQLFunction. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IdentNode.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public Type getDataType() {
	Type type = super.getDataType();
	if ( type != null ) {
		return type;
	}
	FromElement fe = getFromElement();
	if ( fe != null ) {
		return fe.getDataType();
	}
	SQLFunction sf = getWalker().getSessionFactoryHelper().findSQLFunction( getText() );
	if ( sf != null ) {
		return sf.getReturnType( null, getWalker().getSessionFactoryHelper().getFactory() );
	}
	return null;
}
 
Example #2
Source File: ComponentTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void afterConfigurationBuilt(Mappings mappings, Dialect dialect) {
	super.afterConfigurationBuilt( mappings, dialect );
	// Oracle and Postgres do not have year() functions, so we need to
	// redefine the 'User.person.yob' formula
	//
	// consider temporary until we add the capability to define
	// mapping foprmulas which can use dialect-registered functions...
	PersistentClass user = mappings.getClass( User.class.getName() );
	org.hibernate.mapping.Property personProperty = user.getProperty( "person" );
	Component component = ( Component ) personProperty.getValue();
	Formula f = ( Formula ) component.getProperty( "yob" ).getValue().getColumnIterator().next();

	SQLFunction yearFunction = ( SQLFunction ) dialect.getFunctions().get( "year" );
	if ( yearFunction == null ) {
		// the dialect not know to support a year() function, so rely on the
		// ANSI SQL extract function
		f.setFormula( "extract( year from dob )");
	}
	else {
		List args = new ArrayList();
		args.add( "dob" );
		f.setFormula( yearFunction.render( args, null ) );
	}
}
 
Example #3
Source File: InFlightMetadataCollectorImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public InFlightMetadataCollectorImpl(
		BootstrapContext bootstrapContext,
		MetadataBuildingOptions options) {
	this.bootstrapContext = bootstrapContext;
	this.uuid = UUID.randomUUID();
	this.options = options;

	this.identifierGeneratorFactory = options.getServiceRegistry()
			.getService( MutableIdentifierGeneratorFactory.class );

	for ( Map.Entry<String, SQLFunction> sqlFunctionEntry : bootstrapContext.getSqlFunctions().entrySet() ) {
		if ( sqlFunctionMap == null ) {
			// we need this to be a ConcurrentHashMap for the one we ultimately pass along to the SF
			// but is this the reference that gets passed along?
			sqlFunctionMap = new ConcurrentHashMap<>( 16, .75f, 1 );
		}
		sqlFunctionMap.put( sqlFunctionEntry.getKey(), sqlFunctionEntry.getValue() );
	}

	bootstrapContext.getAuxiliaryDatabaseObjectList().forEach( getDatabase()::addAuxiliaryDatabaseObject );
}
 
Example #4
Source File: SqlGenerator.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void beginFunctionTemplate(AST node, AST nameNode) {
	// NOTE for AGGREGATE both nodes are the same; for METHOD the first is the METHOD, the second is the
	// 		METHOD_NAME
	FunctionNode functionNode = (FunctionNode) node;
	SQLFunction sqlFunction = functionNode.getSQLFunction();
	if ( sqlFunction == null ) {
		// if SQLFunction is null we just write the function out as it appears in the hql statement
		super.beginFunctionTemplate( node, nameNode );
	}
	else {
		// this function has a registered SQLFunction -> redirect output and catch the arguments
		outputStack.addFirst( writer );
		if ( node.getType() == CAST ) {
			writer = new CastFunctionArguments();
		}
		else {
			writer = new StandardFunctionArguments();
		}
	}
}
 
Example #5
Source File: SessionFactoryHelper.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Find the function return type given the function name and the first argument expression node.
 *
 * @param functionName The function name.
 * @param first        The first argument expression.
 * @return the function return type given the function name and the first argument expression node.
 */
public Type findFunctionReturnType(String functionName, AST first) {
	// locate the registered function by the given name
	SQLFunction sqlFunction = requireSQLFunction( functionName );

	// determine the type of the first argument...
	Type argumentType = null;
	if ( first != null ) {
		if ( "cast".equals(functionName) ) {
			argumentType = TypeFactory.heuristicType( first.getNextSibling().getText() );
		}
		else if ( first instanceof SqlNode ) {
			argumentType = ( (SqlNode) first ).getDataType();
		}
	}

	return sqlFunction.getReturnType( argumentType, sfi );
}
 
Example #6
Source File: OrderByFragmentParser.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
@SuppressWarnings("SimplifiableIfStatement")
protected boolean isFunctionName(AST ast) {
	/*
	 * Semantic predicate used to determine whether a given AST node represents a function call
	 */

	AST child = ast.getFirstChild();
	// assume it is a function if it has parameters
	if ( child != null && "{param list}".equals( child.getText() ) ) {
		return true;
	}

	// otherwise, in order for this to be a function logically it has to be a function that does not
	// have arguments.  So try to assert that using the registry of known functions
	final SQLFunction function = context.getSqlFunctionRegistry().findSQLFunction( ast.getText() );
	if ( function == null ) {
		// no registered function, so we cannot know for certain
		return false;
	}
	else {
		// if function.hasParenthesesIfNoArguments() is true, then assume the node is not a function
		return !function.hasParenthesesIfNoArguments();
	}
}
 
Example #7
Source File: SqlGenerator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected void endFunctionTemplate(AST node) {
	FunctionNode functionNode = (FunctionNode) node;
	SQLFunction sqlFunction = functionNode.getSQLFunction();
	if ( sqlFunction == null ) {
		super.endFunctionTemplate( node );
	}
	else {
		final Type functionType = functionNode.getFirstArgumentType();
		// this function has a registered SQLFunction -> redirect output and catch the arguments
		FunctionArgumentsCollectingWriter functionArguments = (FunctionArgumentsCollectingWriter) writer;
		writer = outputStack.removeFirst();
		out( sqlFunction.render( functionType, functionArguments.getArgs(), sessionFactory ) );
	}
}
 
Example #8
Source File: SessionFactoryHelper.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public Type findFunctionReturnType(String functionName, SQLFunction sqlFunction, AST firstArgument) {
	// determine the type of the first argument...
	Type argumentType = null;
	if ( firstArgument != null ) {
		if ( "cast".equals( functionName ) ) {
			argumentType = sfi.getTypeResolver().heuristicType( firstArgument.getNextSibling().getText() );
		}
		else if ( SqlNode.class.isInstance( firstArgument ) ) {
			argumentType = ( (SqlNode) firstArgument ).getDataType();
		}
	}

	return sqlFunction.getReturnType( argumentType, sfi );
}
 
Example #9
Source File: AbstractDelegatingSessionFactoryBuilder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public T applySqlFunction(
		String registrationName,
		SQLFunction sqlFunction) {
	delegate.applySqlFunction( registrationName, sqlFunction );
	return getThis();
}
 
Example #10
Source File: IdentNode.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public Type getDataType() {
	Type type = super.getDataType();
	if (type != null) return type;
	FromElement fe = getFromElement();
	if (fe != null) return fe.getDataType();
	SQLFunction sf = getWalker().getSessionFactoryHelper().findSQLFunction(getText());
	return sf == null ? null : sf.getReturnType(null, null);
}
 
Example #11
Source File: SqlGenerator.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected void beginFunctionTemplate(AST m, AST i) {
	MethodNode methodNode = ( MethodNode ) m;
	SQLFunction template = methodNode.getSQLFunction();
	if ( template == null ) {
		// if template is null we just write the function out as it appears in the hql statement
		super.beginFunctionTemplate( m, i );
	}
	else {
		// this function has a template -> redirect output and catch the arguments
		outputStack.addFirst( writer );
		writer = new FunctionArguments();
	}
}
 
Example #12
Source File: Template.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isFunction(String lcToken, String nextToken, SQLFunctionRegistry functionRegistry) {
	// checking for "(" is currently redundant because it is checked before getting here;
	// doing the check anyhow, in case that earlier check goes away;
	if ( "(".equals( nextToken ) ) {
		return true;
	}
	SQLFunction function = functionRegistry.findSQLFunction(lcToken);
	if ( function == null ) {
		// lcToken does not refer to a function
		return false;
	}
	// if function.hasParenthesesIfNoArguments() is true, then assume
	// lcToken is not a function (since it is not followed by '(')
	return ! function.hasParenthesesIfNoArguments();
}
 
Example #13
Source File: Template.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Same functionality as {@link #renderWhereStringTemplate(String, String, Dialect, SQLFunctionRegistry)},
 * except that a SQLFunctionRegistry is not provided (i.e., only the dialect-defined functions are
 * considered).  This is only intended for use by the annotations project until the
 * many-to-many/map-key-from-target-table feature is pulled into core.
 *
 * @deprecated Only intended for annotations usage; use {@link #renderWhereStringTemplate(String, String, Dialect, SQLFunctionRegistry)} instead
 */
@Deprecated
@SuppressWarnings({ "JavaDoc" })
public static String renderWhereStringTemplate(String sqlWhereString, String placeholder, Dialect dialect) {
	return renderWhereStringTemplate(
			sqlWhereString,
			placeholder,
			dialect,
			new SQLFunctionRegistry( dialect, java.util.Collections.<String, SQLFunction>emptyMap() )
	);
}
 
Example #14
Source File: SqlGenerator.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected void endFunctionTemplate(AST m) {
	MethodNode methodNode = ( MethodNode ) m;
	SQLFunction template = methodNode.getSQLFunction();
	if ( template == null ) {
		super.endFunctionTemplate( m );
	}
	else {
		// this function has a template -> restore output, apply the template and write the result out
		FunctionArguments functionArguments = ( FunctionArguments ) writer;   // TODO: Downcast to avoid using an interface?  Yuck.
		writer = ( SqlWriter ) outputStack.removeFirst();
		out( template.render( functionArguments.getArgs(), sessionFactory ) );
	}
}
 
Example #15
Source File: SessionFactoryHelper.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Locate a registered sql function by name, requiring that such a registered function exist.
 *
 * @param functionName The name of the function to locate
 * @return The sql function.
 * @throws QueryException Indicates no matching sql functions could be found.
 */
private SQLFunction requireSQLFunction(String functionName) {
	SQLFunction f = findSQLFunction( functionName );
	if ( f == null ) {
		throw new QueryException( "Unable to find SQL function: " + functionName );
	}
	return f;
}
 
Example #16
Source File: SessionFactoryBuilderImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public SessionFactoryBuilderImpl(MetadataImplementor metadata, BootstrapContext bootstrapContext) {
	this.metadata = metadata;
	this.bootstrapContext = bootstrapContext;

	this.optionsBuilder = new SessionFactoryOptionsBuilder(
			metadata.getMetadataBuildingOptions().getServiceRegistry(),
			bootstrapContext
	);

	if ( metadata.getSqlFunctionMap() != null ) {
		for ( Map.Entry<String, SQLFunction> sqlFunctionEntry : metadata.getSqlFunctionMap().entrySet() ) {
			applySqlFunction( sqlFunctionEntry.getKey(), sqlFunctionEntry.getValue() );
		}
	}
}
 
Example #17
Source File: SQLFunctionsTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private String locateAppropriateDialectFunctionNameForAliasTest() {
	for (Iterator itr = getDialect().getFunctions().entrySet().iterator(); itr.hasNext(); ) {
		final Map.Entry entry = (Map.Entry) itr.next();
		final SQLFunction function = (SQLFunction) entry.getValue();
		if ( !function.hasArguments() && !function.hasParenthesesIfNoArguments() ) {
			return (String) entry.getKey();
		}
	}
	return null;
}
 
Example #18
Source File: AggregateProjection.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected SQLFunction getFunction(String functionName, CriteriaQuery criteriaQuery) {
	final SQLFunction function = criteriaQuery.getFactory()
			.getSqlFunctionRegistry()
			.findSQLFunction( functionName );
	if ( function == null ) {
		throw new HibernateException( "Unable to locate mapping for function named [" + functionName + "]" );
	}
	return function;
}
 
Example #19
Source File: CompositeElementTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void afterConfigurationBuilt(Mappings mappings, Dialect dialect) {
	super.afterConfigurationBuilt( mappings, dialect );
	Collection children = mappings.getCollection( Parent.class.getName() + ".children" );
	Component childComponents = ( Component ) children.getElement();
	Formula f = ( Formula ) childComponents.getProperty( "bioLength" ).getValue().getColumnIterator().next();

	SQLFunction lengthFunction = ( SQLFunction ) dialect.getFunctions().get( "length" );
	if ( lengthFunction != null ) {
		ArrayList args = new ArrayList();
		args.add( "bio" );
		f.setFormula( lengthFunction.render( args, null ) );
	}
}
 
Example #20
Source File: RowCountProjection.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected SQLFunction getFunction(CriteriaQuery criteriaQuery) {
	final SQLFunctionRegistry sqlFunctionRegistry = criteriaQuery.getFactory().getSqlFunctionRegistry();
	final SQLFunction function = sqlFunctionRegistry.findSQLFunction( "count" );
	if ( function == null ) {
		throw new HibernateException( "Unable to locate count function mapping" );
	}
	return function;
}
 
Example #21
Source File: SQLFunctionsInterSystemsTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private String locateAppropriateDialectFunctionNameForAliasTest() {
	for (Iterator itr = getDialect().getFunctions().entrySet().iterator(); itr.hasNext(); ) {
		final Map.Entry entry = (Map.Entry) itr.next();
		final SQLFunction function = (SQLFunction) entry.getValue();
		if ( !function.hasArguments() && !function.hasParenthesesIfNoArguments() ) {
			return (String) entry.getKey();
		}
	}
	return null;
}
 
Example #22
Source File: HQLTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testExpressionInFunction() throws Exception {
	assertTranslation( "from Animal an where an.bodyWeight > abs(3-5)" );
	assertTranslation( "from Animal an where an.bodyWeight > abs(3/5)" );
	assertTranslation( "from Animal an where an.bodyWeight > abs(3+5)" );
	assertTranslation( "from Animal an where an.bodyWeight > abs(3*5)" );
	SQLFunction concat = getSessionFactoryImplementor().getSqlFunctionRegistry().findSQLFunction( "concat");
	List list = new ArrayList(); list.add("'fat'"); list.add("'skinny'");
	assertTranslation( "from Animal an where an.description = " + concat.render(list, getSessionFactoryImplementor()) );
}
 
Example #23
Source File: MethodNode.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public SQLFunction getSQLFunction() {
	return function;
}
 
Example #24
Source File: SelectParser.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
private SQLFunction getFunction(String name, QueryTranslatorImpl q) {
	return q.getFactory().getSqlFunctionRegistry().findSQLFunction( name );
}
 
Example #25
Source File: MetadataImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Map<String, SQLFunction> getSqlFunctionMap() {
	return sqlFunctionMap;
}
 
Example #26
Source File: Dialect.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
protected void registerFunction(String name, SQLFunction function) {
	sqlFunctions.put( name, function );
}
 
Example #27
Source File: MethodNode.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public SQLFunction getSQLFunction() {
	return function;
}
 
Example #28
Source File: AggregateNode.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
public SQLFunction getSQLFunction() {
	return sqlFunction;
}
 
Example #29
Source File: SessionFactoryBuilderImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public SessionFactoryBuilder applySqlFunction(String registrationName, SQLFunction sqlFunction) {
	this.optionsBuilder.applySqlFunction( registrationName, sqlFunction );
	return this;
}
 
Example #30
Source File: MetadataBuilderImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Map<String, SQLFunction> getSqlFunctions() {
	return bootstrapContext.getSqlFunctions();
}