FileDocCategorySizeDatePackage
QueryParameters.javaAPI DocHibernate 3.2.512361Thu Mar 16 07:14:48 GMT 2006org.hibernate.engine

QueryParameters

public final class QueryParameters extends Object
author
Gavin King

Fields Summary
private static final Log
log
private org.hibernate.type.Type[]
positionalParameterTypes
private Object[]
positionalParameterValues
private Map
namedParameters
private Map
lockModes
private RowSelection
rowSelection
private boolean
cacheable
private String
cacheRegion
private String
comment
private org.hibernate.ScrollMode
scrollMode
private Serializable[]
collectionKeys
private Object
optionalObject
private String
optionalEntityName
private Serializable
optionalId
private boolean
readOnly
private boolean
callable
private boolean
autodiscovertypes
private boolean
isNaturalKeyLookup
private final org.hibernate.transform.ResultTransformer
resultTransformer
private String
processedSQL
private org.hibernate.type.Type[]
processedPositionalParameterTypes
private Object[]
processedPositionalParameterValues
Constructors Summary
public QueryParameters()

	
	  
		this( ArrayHelper.EMPTY_TYPE_ARRAY, ArrayHelper.EMPTY_OBJECT_ARRAY );
	
public QueryParameters(org.hibernate.type.Type type, Object value)

		this( new Type[] {type}, new Object[] {value} );
	
public QueryParameters(org.hibernate.type.Type[] positionalParameterTypes, Object[] postionalParameterValues, Object optionalObject, String optionalEntityName, Serializable optionalObjectId)

		this(positionalParameterTypes, postionalParameterValues);
		this.optionalObject = optionalObject;
		this.optionalId = optionalObjectId;
		this.optionalEntityName = optionalEntityName;

	
public QueryParameters(org.hibernate.type.Type[] positionalParameterTypes, Object[] postionalParameterValues)

		this(
			positionalParameterTypes,
			postionalParameterValues, 
			null, 
			null, 
			false, 
			null, 
			null,
			false,
			null
		);
	
public QueryParameters(org.hibernate.type.Type[] positionalParameterTypes, Object[] postionalParameterValues, Serializable[] collectionKeys)

		this(
			positionalParameterTypes,
			postionalParameterValues,
			null,
			collectionKeys
		);
	
public QueryParameters(org.hibernate.type.Type[] positionalParameterTypes, Object[] postionalParameterValues, Map namedParameters, Serializable[] collectionKeys)

			this(
				positionalParameterTypes,
				postionalParameterValues,
				namedParameters,
				null,
				null,
				false,
				false,
				null, 
				null,
				collectionKeys,
				null
			);
		
public QueryParameters(org.hibernate.type.Type[] positionalParameterTypes, Object[] positionalParameterValues, Map lockModes, RowSelection rowSelection, boolean cacheable, String cacheRegion, String comment, boolean isLookupByNaturalKey, org.hibernate.transform.ResultTransformer transformer)

		this(
			positionalParameterTypes,
			positionalParameterValues,
			null,
			lockModes,
			rowSelection,
			false,
			cacheable,
			cacheRegion, 
			comment,
			null,
			transformer
		);
		isNaturalKeyLookup = isLookupByNaturalKey;
	
public QueryParameters(org.hibernate.type.Type[] positionalParameterTypes, Object[] positionalParameterValues, Map namedParameters, Map lockModes, RowSelection rowSelection, boolean readOnly, boolean cacheable, String cacheRegion, String comment, Serializable[] collectionKeys, org.hibernate.transform.ResultTransformer transformer)

		this.positionalParameterTypes = positionalParameterTypes;
		this.positionalParameterValues = positionalParameterValues;
		this.namedParameters = namedParameters;
		this.lockModes = lockModes;
		this.rowSelection = rowSelection;
		this.cacheable = cacheable;
		this.cacheRegion = cacheRegion;
		//this.forceCacheRefresh = forceCacheRefresh;
		this.comment = comment;
		this.collectionKeys = collectionKeys;
		this.readOnly = readOnly;
		this.resultTransformer = transformer;
	
public QueryParameters(org.hibernate.type.Type[] positionalParameterTypes, Object[] positionalParameterValues, Map namedParameters, Map lockModes, RowSelection rowSelection, boolean readOnly, boolean cacheable, String cacheRegion, String comment, Serializable[] collectionKeys, Object optionalObject, String optionalEntityName, Serializable optionalId, org.hibernate.transform.ResultTransformer transformer)

		this(
			positionalParameterTypes, 
			positionalParameterValues, 
			namedParameters, 
			lockModes, 
			rowSelection, 
			readOnly, 
			cacheable, 
			cacheRegion,
			comment,
			collectionKeys,
			transformer
		);
		this.optionalEntityName = optionalEntityName;
		this.optionalId = optionalId;
		this.optionalObject = optionalObject;
	
Methods Summary
public org.hibernate.engine.QueryParameterscreateCopyUsing(RowSelection selection)

		QueryParameters copy = new QueryParameters(
				this.positionalParameterTypes,
		        this.positionalParameterValues,
		        this.namedParameters,
		        this.lockModes,
	            selection,
		        this.readOnly,
		        this.cacheable,
	            this.cacheRegion,
		        this.comment,
		        this.collectionKeys,
		        this.optionalObject,
				this.optionalEntityName,
				this.optionalId,
				this.resultTransformer
		);
		copy.processedSQL = this.processedSQL;
		copy.processedPositionalParameterTypes = this.processedPositionalParameterTypes;
		copy.processedPositionalParameterValues = this.processedPositionalParameterValues;
		return copy;
	
public java.lang.StringgetCacheRegion()

		return cacheRegion;
	
public java.io.Serializable[]getCollectionKeys()

		return collectionKeys;
	
public java.lang.StringgetComment()

		return comment;
	
public org.hibernate.type.Type[]getFilteredPositionalParameterTypes()

		return processedPositionalParameterTypes;
	
public java.lang.Object[]getFilteredPositionalParameterValues()

		return processedPositionalParameterValues;
	
public java.lang.StringgetFilteredSQL()

		return processedSQL;
	
public java.util.MapgetLockModes()

		return lockModes;
	
public java.util.MapgetNamedParameters()

		return namedParameters;
	
public java.lang.StringgetOptionalEntityName()

		return optionalEntityName;
	
public java.io.SerializablegetOptionalId()

		return optionalId;
	
public java.lang.ObjectgetOptionalObject()

		return optionalObject;
	
public org.hibernate.type.Type[]getPositionalParameterTypes()

		return positionalParameterTypes;
	
public java.lang.Object[]getPositionalParameterValues()

		return positionalParameterValues;
	
public org.hibernate.transform.ResultTransformergetResultTransformer()

		return resultTransformer;
	
public RowSelectiongetRowSelection()

		return rowSelection;
	
public org.hibernate.ScrollModegetScrollMode()

		return scrollMode;
	
public booleanhasAutoDiscoverScalarTypes()

		return autodiscovertypes;
	
public booleanhasRowSelection()

		return rowSelection!=null;
	
public booleanisCacheable()

		return cacheable;
	
public booleanisCallable()

		return callable;
	
public booleanisNaturalKeyLookup()

		return isNaturalKeyLookup;
	
public booleanisReadOnly()

		return readOnly;
	
public voidprocessFilters(java.lang.String sql, SessionImplementor session)

		
		if ( session.getEnabledFilters().size()==0 || sql.indexOf(ParserHelper.HQL_VARIABLE_PREFIX)<0 ) {
			// HELLA IMPORTANT OPTIMIZATION!!!
			processedPositionalParameterValues = getPositionalParameterValues();
			processedPositionalParameterTypes = getPositionalParameterTypes();
			processedSQL = sql;
		}
		else {
			
			Dialect dialect = session.getFactory().getDialect();
			String symbols = new StringBuffer().append( ParserHelper.HQL_SEPARATORS )
					.append( dialect.openQuote() )
					.append( dialect.closeQuote() )
					.toString();
			StringTokenizer tokens = new StringTokenizer( sql, symbols, true );
			StringBuffer result = new StringBuffer();
		
			List parameters = new ArrayList();
			List parameterTypes = new ArrayList();
		
			while ( tokens.hasMoreTokens() ) {
				final String token = tokens.nextToken();
				if ( token.startsWith( ParserHelper.HQL_VARIABLE_PREFIX ) ) {
					String filterParameterName = token.substring( 1 );
					Object value = session.getFilterParameterValue( filterParameterName );
					Type type = session.getFilterParameterType( filterParameterName );
					if ( value != null && Collection.class.isAssignableFrom( value.getClass() ) ) {
						Iterator itr = ( ( Collection ) value ).iterator();
						while ( itr.hasNext() ) {
							Object elementValue = itr.next();
							result.append( '?" );
							parameters.add( elementValue );
							parameterTypes.add( type );
							if ( itr.hasNext() ) {
								result.append( ", " );
							}
						}
					}
					else {
						result.append( '?" );
						parameters.add( value );
						parameterTypes.add( type );
					}
				}
				else {
					result.append( token );
				}
			}
			parameters.addAll( Arrays.asList( getPositionalParameterValues() ) );
			parameterTypes.addAll( Arrays.asList( getPositionalParameterTypes() ) );
			processedPositionalParameterValues = parameters.toArray();
			processedPositionalParameterTypes = ( Type[] ) parameterTypes.toArray( new Type[0] );
			processedSQL = result.toString();
			
		}
	
public voidsetAutoDiscoverScalarTypes(boolean autodiscovertypes)

		this.autodiscovertypes = autodiscovertypes;
	
public voidsetCacheRegion(java.lang.String cacheRegion)

		this.cacheRegion = cacheRegion;
	
public voidsetCacheable(boolean b)

		cacheable = b;
	
public voidsetCallable(boolean callable)

		this.callable = callable;		
	
public voidsetCollectionKeys(java.io.Serializable[] collectionKeys)

		this.collectionKeys = collectionKeys;
	
public voidsetComment(java.lang.String comment)

		this.comment = comment;
	
public voidsetLockModes(java.util.Map map)

		lockModes = map;
	
public voidsetNamedParameters(java.util.Map map)

		namedParameters = map;
	
public voidsetNaturalKeyLookup(boolean isNaturalKeyLookup)

		this.isNaturalKeyLookup = isNaturalKeyLookup;
	
public voidsetOptionalEntityName(java.lang.String optionalEntityName)

		this.optionalEntityName = optionalEntityName;
	
public voidsetOptionalId(java.io.Serializable optionalId)

		this.optionalId = optionalId;
	
public voidsetOptionalObject(java.lang.Object optionalObject)

		this.optionalObject = optionalObject;
	
public voidsetPositionalParameterTypes(org.hibernate.type.Type[] types)

		positionalParameterTypes = types;
	
public voidsetPositionalParameterValues(java.lang.Object[] objects)

		positionalParameterValues = objects;
	
public voidsetReadOnly(boolean readOnly)

		this.readOnly = readOnly;
	
public voidsetRowSelection(RowSelection selection)

		rowSelection = selection;
	
public voidsetScrollMode(org.hibernate.ScrollMode scrollMode)

		this.scrollMode = scrollMode;
	
public voidtraceParameters(SessionFactoryImplementor factory)

		Printer print = new Printer(factory);
		if (positionalParameterValues.length!=0) {
			log.trace(
					"parameters: " + 
					print.toString(positionalParameterTypes, positionalParameterValues) 
				);
		}
		if (namedParameters!=null) {
			log.trace( "named parameters: " + print.toString(namedParameters) );
		}
	
public voidvalidateParameters()

		int types = positionalParameterTypes==null ? 0 : positionalParameterTypes.length;
		int values = positionalParameterValues==null ? 0 : positionalParameterValues.length;
		if (types!=values) {
			throw new QueryException(
					"Number of positional parameter types:" + types + 
					" does not match number of positional parameters: " + values
				);
		}