package railo.runtime.type.it;

import java.util.Iterator;

import railo.runtime.exp.PageException;
import railo.runtime.exp.PageRuntimeException;
import railo.runtime.type.Collection.Key;
import railo.runtime.type.Query;
import railo.runtime.type.Struct;
import railo.runtime.type.StructImpl;

public class ForEachQueryIterator implements Iterator {

	private Query qry;
	private int pid;
	private int start,current=0;


	public ForEachQueryIterator(Query qry, int pid){
		this.qry=qry;
		this.pid=pid;
		this.start=qry.getCurrentrow(pid);
	}
	
	@Override
	public boolean hasNext() {
		return current<qry.getRecordcount();
	}

	@Override
	public Object next() {
		try {
			if(qry.go(++current,pid)) {
				Struct sct=new StructImpl();
				Key[] names = qry.getColumnNames();
				for(int i=0;i<names.length;i++){
					sct.setEL(names[i], qry.get(names[i],null));
				}
				return sct;
			}
		} catch (PageException pe) {
			throw new PageRuntimeException(pe);
		}
		return null;
	}

	@Override
	public void remove() {
		try {
			qry.removeRow(current);
		} catch (PageException pe) {
			throw new PageRuntimeException(pe);
		}
	}

	public void reset() throws PageException {
		qry.go(start,pid);
	}

}

@@ -413,16 +413,38 @@
 rent=0;%0A
+%09private Key%5B%5D names;%0A
 %0A%0A%09publi
@@ -556,16 +556,53 @@
 w(pid);%0A
+%09%09this.names = qry.getColumnNames();%0A
 %09%7D%0A%09%0A%09@O
@@ -791,48 +791,8 @@
 ();%0A
-%09%09%09%09Key%5B%5D names = qry.getColumnNames();%0A
 %09%09%09%09

move names otside the loop