FileDocCategorySizeDatePackage
SQLQueryImpl.javaAPI DocHibernate 3.2.510888Tue Nov 21 17:11:26 GMT 2006org.hibernate.impl

SQLQueryImpl

public class SQLQueryImpl extends AbstractQueryImpl implements org.hibernate.SQLQuery
Implements SQL query passthrough.


SELECT {person}.NAME AS {person.name}, {person}.AGE AS {person.age}, {person}.SEX AS {person.sex}
FROM PERSON {person} WHERE {person}.NAME LIKE 'Hiber%'

author
Max Andersen

Fields Summary
private final List
queryReturns
private Collection
querySpaces
private final boolean
callable
private boolean
autodiscovertypes
private static final org.hibernate.engine.query.sql.NativeSQLQueryReturn[]
NO_SQL_RETURNS
Constructors Summary
SQLQueryImpl(org.hibernate.engine.NamedSQLQueryDefinition queryDef, org.hibernate.engine.SessionImplementor session, org.hibernate.engine.query.ParameterMetadata parameterMetadata)
Constructs a SQLQueryImpl given a sql query defined in the mappings.

param
queryDef The representation of the defined .
param
session The session to which this SQLQueryImpl belongs.
param
parameterMetadata Metadata about parameters found in the query.

		super( queryDef.getQueryString(), queryDef.getFlushMode(), session, parameterMetadata );
		if ( queryDef.getResultSetRef() != null ) {
			ResultSetMappingDefinition definition = session.getFactory()
					.getResultSetMapping( queryDef.getResultSetRef() );
			if (definition == null) {
				throw new MappingException(
						"Unable to find resultset-ref definition: " +
						queryDef.getResultSetRef() 
					);
			}
			this.queryReturns = Arrays.asList( definition.getQueryReturns() );
		}
		else {
			this.queryReturns = Arrays.asList( queryDef.getQueryReturns() );
		}

		this.querySpaces = queryDef.getQuerySpaces();
		this.callable = queryDef.isCallable();
	
SQLQueryImpl(String sql, List queryReturns, Collection querySpaces, org.hibernate.FlushMode flushMode, boolean callable, org.hibernate.engine.SessionImplementor session, org.hibernate.engine.query.ParameterMetadata parameterMetadata)

		// TODO : absolutely no usages of this constructor form; can it go away?
		super( sql, flushMode, session, parameterMetadata );
		this.queryReturns = queryReturns;
		this.querySpaces = querySpaces;
		this.callable = callable;
	
SQLQueryImpl(String sql, String[] returnAliases, Class[] returnClasses, org.hibernate.LockMode[] lockModes, org.hibernate.engine.SessionImplementor session, Collection querySpaces, org.hibernate.FlushMode flushMode, org.hibernate.engine.query.ParameterMetadata parameterMetadata)

		// TODO : this constructor form is *only* used from constructor directly below us; can it go away?
		super( sql, flushMode, session, parameterMetadata );
		queryReturns = new ArrayList(returnAliases.length);
		for ( int i=0; i<returnAliases.length; i++ ) {
			NativeSQLQueryRootReturn ret = new NativeSQLQueryRootReturn(
					returnAliases[i],
					returnClasses[i].getName(),
					lockModes==null ? LockMode.NONE : lockModes[i]
			);
			queryReturns.add(ret);
		}
		this.querySpaces = querySpaces;
		this.callable = false;
	
SQLQueryImpl(String sql, String[] returnAliases, Class[] returnClasses, org.hibernate.engine.SessionImplementor session, org.hibernate.engine.query.ParameterMetadata parameterMetadata)

		this( sql, returnAliases, returnClasses, null, session, null, null, parameterMetadata );
	
SQLQueryImpl(String sql, org.hibernate.engine.SessionImplementor session, org.hibernate.engine.query.ParameterMetadata parameterMetadata)

		super( sql, null, session, parameterMetadata );
		queryReturns = new ArrayList();
		querySpaces = null;
		callable = false;
	
Methods Summary
public org.hibernate.SQLQueryaddEntity(java.lang.Class entityClass)

		return addEntity( StringHelper.unqualify( entityClass.getName() ), entityClass );
	
public org.hibernate.SQLQueryaddEntity(java.lang.String entityName)

		return addEntity( StringHelper.unqualify( entityName ), entityName );
	
public org.hibernate.SQLQueryaddEntity(java.lang.String alias, java.lang.String entityName)

		return addEntity(alias, entityName, LockMode.READ);
	
public org.hibernate.SQLQueryaddEntity(java.lang.String alias, java.lang.Class entityClass)

		return addEntity( alias, entityClass.getName() );
	
public org.hibernate.SQLQueryaddEntity(java.lang.String alias, java.lang.String entityName, org.hibernate.LockMode lockMode)

		queryReturns.add( new NativeSQLQueryRootReturn(alias, entityName, lockMode) );
		return this;
	
public org.hibernate.SQLQueryaddEntity(java.lang.String alias, java.lang.Class entityClass, org.hibernate.LockMode lockMode)

		return addEntity( alias, entityClass.getName(), lockMode );
	
public org.hibernate.SQLQueryaddJoin(java.lang.String alias, java.lang.String path)

		return addJoin(alias, path, LockMode.READ);
	
public org.hibernate.SQLQueryaddJoin(java.lang.String alias, java.lang.String path, org.hibernate.LockMode lockMode)

		int loc = path.indexOf('.");
		if ( loc < 0 ) {
			throw new QueryException( "not a property path: " + path );
		}
		String ownerAlias = path.substring(0, loc);
		String role = path.substring(loc+1);
		queryReturns.add( new NativeSQLQueryJoinReturn(alias, ownerAlias, role, CollectionHelper.EMPTY_MAP, lockMode) );
		return this;
	
private org.hibernate.SQLQueryaddQuerySpaces(java.io.Serializable[] spaces)

		if ( spaces != null ) {
			if ( querySpaces == null ) {
				querySpaces = new ArrayList();
			}
			for ( int i = 0; i < spaces.length; i++ ) {
				querySpaces.add( spaces[i] );
			}
		}
		return this;
	
public org.hibernate.SQLQueryaddScalar(java.lang.String columnAlias, org.hibernate.type.Type type)

		queryReturns.add( new NativeSQLQueryScalarReturn( columnAlias, type ) );
		return this;
	
public org.hibernate.SQLQueryaddScalar(java.lang.String columnAlias)

		autodiscovertypes = true;
		queryReturns.add( new NativeSQLQueryScalarReturn( columnAlias, null ) );
		return this;
	
public org.hibernate.SQLQueryaddSynchronizedEntityClass(java.lang.Class entityClass)

		return addQuerySpaces( getSession().getFactory().getEntityPersister( entityClass.getName() ).getQuerySpaces() );
	
public org.hibernate.SQLQueryaddSynchronizedEntityName(java.lang.String entityName)

		return addQuerySpaces( getSession().getFactory().getEntityPersister( entityName ).getQuerySpaces() );
	
public org.hibernate.SQLQueryaddSynchronizedQuerySpace(java.lang.String querySpace)

		if ( querySpaces == null ) {
			querySpaces = new ArrayList();
		}
		querySpaces.add( querySpace );
		return this;
	
public intexecuteUpdate()

		Map namedParams = getNamedParams();
		before();
		try {
			return getSession().executeNativeUpdate(
					generateQuerySpecification( namedParams ),
					getQueryParameters( namedParams )
			);
		}
		finally {
			after();
		}
	
private org.hibernate.engine.query.sql.NativeSQLQuerySpecificationgenerateQuerySpecification(java.util.Map namedParams)

		return new NativeSQLQuerySpecification(
		        expandParameterLists(namedParams),
		        getQueryReturns(),
		        querySpaces
		);
	
protected java.util.MapgetLockModes()

		//we never need to apply locks to the SQL
		return CollectionHelper.EMPTY_MAP;
	
public org.hibernate.engine.QueryParametersgetQueryParameters(java.util.Map namedParams)

		QueryParameters qp = super.getQueryParameters(namedParams);
		qp.setCallable(callable);
		qp.setAutoDiscoverScalarTypes(autodiscovertypes);
		return qp;
	
private org.hibernate.engine.query.sql.NativeSQLQueryReturn[]getQueryReturns()

	
	   
		return ( NativeSQLQueryReturn[] ) queryReturns.toArray( NO_SQL_RETURNS );
	
public java.lang.String[]getReturnAliases()

		throw new UnsupportedOperationException("SQL queries do not currently support returning aliases");
	
public org.hibernate.type.Type[]getReturnTypes()

		throw new UnsupportedOperationException("not yet implemented for SQL queries");
	
public java.util.Iteratoriterate()

		throw new UnsupportedOperationException("SQL queries do not currently support iteration");
	
public java.util.Listlist()

		verifyParameters();
		before();

		Map namedParams = getNamedParams();
		NativeSQLQuerySpecification spec = generateQuerySpecification( namedParams );

		try {
			return getSession().list( spec, getQueryParameters( namedParams ) );
		}
		finally {
			after();
		}
	
public org.hibernate.ScrollableResultsscroll()

		return scroll(ScrollMode.SCROLL_INSENSITIVE);
	
public org.hibernate.ScrollableResultsscroll(org.hibernate.ScrollMode scrollMode)

		verifyParameters();
		before();

		Map namedParams = getNamedParams();
		NativeSQLQuerySpecification spec = generateQuerySpecification( namedParams );

		QueryParameters qp = getQueryParameters( namedParams );
		qp.setScrollMode( scrollMode );

		try {
			return getSession().scroll( spec, qp );
		}
		finally {
			after();
		}
	
public org.hibernate.QuerysetLockMode(java.lang.String alias, org.hibernate.LockMode lockMode)

		throw new UnsupportedOperationException("cannot set the lock mode for a native SQL query");
	
public org.hibernate.SQLQuerysetResultSetMapping(java.lang.String name)

		ResultSetMappingDefinition mapping = session.getFactory().getResultSetMapping( name );
		if ( mapping == null ) {
			throw new MappingException( "Unknown SqlResultSetMapping [" + name + "]" );
		}
		NativeSQLQueryReturn[] returns = mapping.getQueryReturns();
		int length = returns.length;
		for ( int index = 0 ; index < length ; index++ ) {
			queryReturns.add( returns[index] );
		}
		return this;
	
protected voidverifyParameters()

		verifyParameters( callable );
		boolean noReturns = queryReturns==null || queryReturns.isEmpty();
		if ( noReturns ) {
			this.autodiscovertypes = noReturns;
		}
		else {
			Iterator itr = queryReturns.iterator();
			while ( itr.hasNext() ) {
				NativeSQLQueryReturn rtn = ( NativeSQLQueryReturn ) itr.next();
				if ( rtn instanceof NativeSQLQueryScalarReturn ) {
					NativeSQLQueryScalarReturn scalar = ( NativeSQLQueryScalarReturn ) rtn;
					if ( scalar.getType() == null ) {
						autodiscovertypes = true;
						break;
					}
				}
			}
		}