FileDocCategorySizeDatePackage
SQLFunctionTemplate.javaAPI DocHibernate 3.2.53520Fri Apr 29 10:32:30 BST 2005org.hibernate.dialect.function

SQLFunctionTemplate

public class SQLFunctionTemplate extends Object implements SQLFunction
Represents HQL functions that can have different representations in different SQL dialects. E.g. in HQL we can define function concat(?1, ?2) to concatenate two strings p1 and p2. Target SQL function will be dialect-specific, e.g. (?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
version
$Revision: 6608 $

Fields Summary
private final org.hibernate.type.Type
type
private final boolean
hasArguments
private final boolean
hasParenthesesIfNoArgs
private final String
template
private final String[]
chunks
private final int[]
paramIndexes
Constructors Summary
public SQLFunctionTemplate(org.hibernate.type.Type type, String template)

		this( type, template, true );
	
public SQLFunctionTemplate(org.hibernate.type.Type type, String template, boolean hasParenthesesIfNoArgs)

		this.type = type;
		this.template = template;

		List chunkList = new ArrayList();
		List paramList = new ArrayList();
		StringBuffer chunk = new StringBuffer( 10 );
		StringBuffer index = new StringBuffer( 2 );

		for ( int i = 0; i < template.length(); ++i ) {
			char c = template.charAt( i );
			if ( c == '?" ) {
				chunkList.add( chunk.toString() );
				chunk.delete( 0, chunk.length() );

				while ( ++i < template.length() ) {
					c = template.charAt( i );
					if ( Character.isDigit( c ) ) {
						index.append( c );
					}
					else {
						chunk.append( c );
						break;
					}
				}

				paramList.add( new Integer( Integer.parseInt( index.toString() ) - 1 ) );
				index.delete( 0, index.length() );
			}
			else {
				chunk.append( c );
			}
		}

		if ( chunk.length() > 0 ) {
			chunkList.add( chunk.toString() );
		}

		chunks = ( String[] ) chunkList.toArray( new String[chunkList.size()] );
		paramIndexes = new int[paramList.size()];
		for ( int i = 0; i < paramIndexes.length; ++i ) {
			paramIndexes[i] = ( ( Integer ) paramList.get( i ) ).intValue();
		}

		hasArguments = paramIndexes.length > 0;
		this.hasParenthesesIfNoArgs = hasParenthesesIfNoArgs;
	
Methods Summary
public org.hibernate.type.TypegetReturnType(org.hibernate.type.Type columnType, org.hibernate.engine.Mapping mapping)

		return type;
	
public booleanhasArguments()

		return hasArguments;
	
public booleanhasParenthesesIfNoArguments()

		return hasParenthesesIfNoArgs;
	
public java.lang.Stringrender(java.util.List args, org.hibernate.engine.SessionFactoryImplementor factory)
Applies the template to passed in arguments.

param
args function arguments
return
generated SQL function call

		StringBuffer buf = new StringBuffer();
		for ( int i = 0; i < chunks.length; ++i ) {
			if ( i < paramIndexes.length ) {
				Object arg = paramIndexes[i] < args.size() ? args.get( paramIndexes[i] ) : null;
				if ( arg != null ) {
					buf.append( chunks[i] ).append( arg );
				}
			}
			else {
				buf.append( chunks[i] );
			}
		}
		return buf.toString();
	
public java.lang.StringtoString()

		return template;