Implementation of ScrollableResults which can handle collection fetches.
| Method from org.hibernate.impl.FetchingScrollableResultsImpl Detail: |
public void afterLast() throws HibernateException {
// TODO : not sure the best way to handle this.
// The non-performant way :
last();
next();
afterScrollOperation();
}
Go to a location just after the last result |
public void beforeFirst() throws HibernateException {
try {
getResultSet().beforeFirst();
}
catch( SQLException e ) {
throw JDBCExceptionHelper.convert(
getSession().getFactory().getSQLExceptionConverter(),
e,
"exception calling beforeFirst()"
);
}
currentRow = null;
currentPosition = 0;
}
Go to a location just before first result (this is the initial location) |
public boolean first() throws HibernateException {
beforeFirst();
boolean more = next();
afterScrollOperation();
return more;
}
|
protected Object[] getCurrentRow() {
return currentRow;
}
|
public int getRowNumber() throws HibernateException {
return currentPosition;
}
Get the current location in the result set. The first row is number 0, contrary to JDBC. |
public boolean isFirst() throws HibernateException {
return currentPosition == 1;
}
Is this the first result? |
public boolean isLast() throws HibernateException {
if ( maxPosition == null ) {
// we have not yet hit the last result...
return false;
}
else {
return currentPosition == maxPosition.intValue();
}
}
|
public boolean last() throws HibernateException {
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 boolean next() throws HibernateException {
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;
}
Advance to the next result |
public boolean previous() throws HibernateException {
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;
}
Retreat to the previous result |
public boolean scroll(int positions) throws HibernateException {
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;
}
Scroll an arbitrary number of locations |
public boolean setRowNumber(int rowNumber) throws HibernateException {
if ( rowNumber == 1 ) {
return first();
}
else if ( rowNumber == -1 ) {
return last();
}
else if ( maxPosition != null && rowNumber == maxPosition.intValue() ) {
return last();
}
return scroll( rowNumber - currentPosition );
}
Set the current location in the result set, numbered from either the first row (row number 0), or the last
row (row number -1). |