001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.type.it;
020
021import java.util.Iterator;
022
023import lucee.runtime.config.NullSupportHelper;
024import lucee.runtime.exp.PageException;
025import lucee.runtime.exp.PageRuntimeException;
026import lucee.runtime.type.Collection.Key;
027import lucee.runtime.type.Query;
028import lucee.runtime.type.Struct;
029import lucee.runtime.type.StructImpl;
030
031public class ForEachQueryIterator implements Iterator {
032
033        private Query qry;
034        private int pid;
035        private int start,current=0;
036        private Key[] names;
037
038
039        public ForEachQueryIterator(Query qry, int pid){
040                this.qry=qry;
041                this.pid=pid;
042                this.start=qry.getCurrentrow(pid);
043                this.names = qry.getColumnNames();
044        }
045        
046        @Override
047        public boolean hasNext() {
048                return current<qry.getRecordcount();
049        }
050
051        @Override
052        public Object next() {
053                try {
054                        if(qry.go(++current,pid)) {
055                                Struct sct=new StructImpl();
056                                for(int i=0;i<names.length;i++){
057                                        sct.setEL(names[i], qry.get(names[i],NullSupportHelper.empty()));
058                                }
059                                return sct;
060                        }
061                } catch (PageException pe) {
062                        throw new PageRuntimeException(pe);
063                }
064                return null;
065        }
066
067        @Override
068        public void remove() {
069                try {
070                        qry.removeRow(current);
071                } catch (PageException pe) {
072                        throw new PageRuntimeException(pe);
073                }
074        }
075
076        public void reset() throws PageException {
077                qry.go(start,pid);
078        }
079
080}