FileDocCategorySizeDatePackage
FetchingScrollableResultsImpl.javaAPI DocHibernate 3.2.56887Thu Jul 14 08:12:20 BST 2005org.hibernate.impl

FetchingScrollableResultsImpl

public class FetchingScrollableResultsImpl extends AbstractScrollableResults
Implementation of ScrollableResults which can handle collection fetches.
author
Steve Ebersole

Fields Summary
private Object[]
currentRow
private int
currentPosition
private Integer
maxPosition
Constructors Summary
public FetchingScrollableResultsImpl(ResultSet rs, PreparedStatement ps, org.hibernate.engine.SessionImplementor sess, org.hibernate.loader.Loader loader, org.hibernate.engine.QueryParameters queryParameters, org.hibernate.type.Type[] types, org.hibernate.hql.HolderInstantiator holderInstantiator)

		super( rs, ps, sess, loader, queryParameters, types, holderInstantiator );
	
Methods Summary
public voidafterLast()
Go to a location just after the last result

		// TODO : not sure the best way to handle this.
		// The non-performant way :
		last();
		next();
		afterScrollOperation();
	
public voidbeforeFirst()
Go to a location just before first result (this is the initial location)

		try {
			getResultSet().beforeFirst();
		}
		catch( SQLException e ) {
			throw JDBCExceptionHelper.convert(
			        getSession().getFactory().getSQLExceptionConverter(),
			        e,
			        "exception calling beforeFirst()"
				);
		}
		currentRow = null;
		currentPosition = 0;
	
public booleanfirst()
Go to the first result

return
true if there are any results

		beforeFirst();
		boolean more = next();

		afterScrollOperation();

		return more;
	
protected java.lang.Object[]getCurrentRow()


	   
		return currentRow;
	
public intgetRowNumber()
Get the current location in the result set. The first row is number 0, contrary to JDBC.

return
the row number, numbered from 0, or -1 if there is no current row

		return currentPosition;
	
public booleanisFirst()
Is this the first result?

return
true if this is the first row of results
throws
org.hibernate.HibernateException

		return currentPosition == 1;
	
public booleanisLast()
Is this the last result?

return
true if this is the last row of results
throws
org.hibernate.HibernateException

		if ( maxPosition == null ) {
			// we have not yet hit the last result...
			return false;
		}
		else {
			return currentPosition == maxPosition.intValue();
		}
	
public booleanlast()
Go to the last result

return
true if there are any results

		boolean more = false;
		if ( maxPosition != null ) {
			for ( int i = currentPosition; i < maxPosition.intValue(); i++ ) {
				more = next();
			}
		}
		else {
			try {
				if ( getResultSet().isAfterLast() ) {
					// should not be able to reach last without maxPosition being set
					// unless there are no results
					return false;
				}

				while ( !getResultSet().isAfterLast() ) {
					more = next();
				}
			}
			catch( SQLException e ) {
				throw JDBCExceptionHelper.convert(
						getSession().getFactory().getSQLExceptionConverter(),
						e,
						"exception calling isAfterLast()"
					);
			}
		}

		afterScrollOperation();

		return more;
	
public booleannext()
Advance to the next result

return
true if there is another result

		if ( maxPosition != null && maxPosition.intValue() <= currentPosition ) {
			currentRow = null;
			currentPosition = maxPosition.intValue() + 1;
			return false;
		}

		Object row = getLoader().loadSequentialRowsForward(
				getResultSet(),
				getSession(),
				getQueryParameters(),
				false
		);


		boolean afterLast;
		try {
			afterLast = getResultSet().isAfterLast();
		}
		catch( SQLException e ) {
			throw JDBCExceptionHelper.convert(
			        getSession().getFactory().getSQLExceptionConverter(),
			        e,
			        "exception calling isAfterLast()"
				);
		}

		currentPosition++;
		currentRow = new Object[] { row };

		if ( afterLast ) {
			if ( maxPosition == null ) {
				// we just hit the last position
				maxPosition = new Integer( currentPosition );
			}
		}

		afterScrollOperation();

		return true;
	
public booleanprevious()
Retreat to the previous result

return
true if there is a previous result

		if ( currentPosition <= 1 ) {
			currentPosition = 0;
			currentRow = null;
			return false;
		}

		Object loadResult = getLoader().loadSequentialRowsReverse(
				getResultSet(),
				getSession(),
				getQueryParameters(),
				false,
		        ( maxPosition != null && currentPosition > maxPosition.intValue() )
		);

		currentRow = new Object[] { loadResult };
		currentPosition--;

		afterScrollOperation();

		return true;

	
public booleanscroll(int positions)
Scroll an arbitrary number of locations

param
positions a positive (forward) or negative (backward) number of rows
return
true if there is a result at the new location

		boolean more = false;
		if ( positions > 0 ) {
			// scroll ahead
			for ( int i = 0; i < positions; i++ ) {
				more = next();
				if ( !more ) {
					break;
				}
			}
		}
		else if ( positions < 0 ) {
			// scroll backward
			for ( int i = 0; i < ( 0 - positions ); i++ ) {
				more = previous();
				if ( !more ) {
					break;
				}
			}
		}
		else {
			throw new HibernateException( "scroll(0) not valid" );
		}

		afterScrollOperation();

		return more;
	
public booleansetRowNumber(int rowNumber)
Set the current location in the result set, numbered from either the first row (row number 0), or the last row (row number -1).

param
rowNumber the row number, numbered from the last row, in the case of a negative row number
return
true if there is a row at that row number

		if ( rowNumber == 1 ) {
			return first();
		}
		else if ( rowNumber == -1 ) {
			return last();
		}
		else if ( maxPosition != null && rowNumber == maxPosition.intValue() ) {
			return last();
		}
		return scroll( rowNumber - currentPosition );