JDocCoverage Report - 21.04.2006 22:02:51

Namemethod, %comment, %TODO@see
smallsql.database.SortedResult176,3%   (148/2183)10

/* =============================================================
 * SmallSQL : a free Java DBMS library for the Java(tm) platform
 * =============================================================
 *
 * (C) Copyright 2004-2006, by Volker Berlin.
 *
 * Project Info:  http://www.smallsql.de/
 *
 * This library is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as published by 
 * the Free Software Foundation; either version 2.1 of the License, or 
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
 * USA.  
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
 * in the United States and other countries.]
 *
 * ---------------
 * SortedResult.java
 * ---------------
 * Author: Volker Berlin
 * 
 */
package smallsql.database;


/**
 * Is used to implements the ORDER BY clause.
 * 
 * @author Volker Berlin
 */
final class SortedResult extends RowSource {

	final private Expressions orderBy;
	final private RowSource rowSource;
	private IndexScrollStatus scrollStatus;
	private int row;
	private boolean useSetRowPosition;
	
	SortedResult(RowSource rowSource, Expressions orderBy){
		this.rowSource = rowSource;
		this.orderBy = orderBy;
	}

	
	final boolean isScrollable(){
		return true; //TODO can scrollable implement if Index implement it
	}

	
	final void execute() throws Exception{
		rowSource.execute();
		Index index = new Index(false);	
		while(rowSource.next()){
			index.addValues( rowSource.getRowPosition(), orderBy);
		}
		scrollStatus = index.createScrollStatus(orderBy);
		useSetRowPosition = false;
	}
	

	void beforeFirst() throws Exception {
		scrollStatus.reset();
		row = 0;
		useSetRowPosition = false;
	}
	

	boolean first() throws Exception {
		beforeFirst();
		return next();
	}
	

	boolean previous() throws Exception{
		return nextPrevious( -1 );
	}
	
	
	boolean next() throws Exception {
		return nextPrevious( +1 );
	}
	
	
	final private boolean nextPrevious(int scroll) throws Exception{
		if(useSetRowPosition) throw Utils.createSQLException("Internal Error with ORDER BY");
		long rowPosition = scrollStatus.getRowOffset(scroll == 1);
		if(rowPosition >= 0){
			rowSource.setRowPosition( rowPosition );
			row += scroll;
			return true;
		}else{
			noRow();
			return false;
		}
	}
	
	
	boolean last() throws Exception{
		afterLast();
		return previous();
	}
	
	
	void afterLast(){
		scrollStatus.afterLast();
		noRow();
		useSetRowPosition = false;
	}
	
    
	int getRow(){
		return row;
	}
	
	
	final long getRowPosition(){
		return rowSource.getRowPosition();
	}
	
	
	final void setRowPosition(long rowPosition) throws Exception{
		rowSource.setRowPosition(rowPosition);
		useSetRowPosition = true;
	}
	

	final boolean rowInserted(){
		return rowSource.rowInserted();
	}
	
	
	final boolean rowDeleted(){
		return rowSource.rowDeleted();
	}
	
	
	void nullRow() {
		rowSource.nullRow();
		row = 0;
		
	}
	

	void noRow() {
		rowSource.noRow();
		row = 0;
	}

}