001    /**
002     * Implements the Cold Fusion Function arraymin
003     */
004    package railo.runtime.functions.query;
005    
006    import railo.runtime.PageContext;
007    import railo.runtime.exp.FunctionException;
008    import railo.runtime.exp.PageException;
009    import railo.runtime.ext.function.Function;
010    import railo.runtime.type.Query;
011    import railo.runtime.type.QueryImpl;
012    
013    public final class QuerySlice implements Function {
014            
015            public static Query call(PageContext pc , Query qry,double offset) throws PageException {
016                    return call(pc , qry, offset,0);
017            }
018            public static Query call(PageContext pc , Query qry,double offset,double length) throws PageException {
019                            
020                    int len=qry.getRecordcount();
021                    if(offset>0) {
022                            if(len<offset)throw new FunctionException(pc,"querySlice",2,"offset","offset can be greater than recordcount of the query");
023                            
024                            int to=0;
025                            if(length>0)to=(int)(offset+length-1);
026                            else if(length<=0)to=(int)(len+length);
027                            if(len<to)
028                                    throw new FunctionException(pc,"querySlice",3,"length","offset+length can be greater than recordcount of the query");
029                            
030                            return get(qry,(int)offset,to);
031                    }
032                    return call(pc ,qry,len+offset,length);
033            }
034    
035            private static Query get(Query qry, int from, int to) throws PageException {
036                    String[] columns;
037                    //print.out(from+"::"+to);
038                    Query nq=new QueryImpl(columns=qry.getColumns(),0,qry.getName());
039                    
040                    int row=1;
041                    for(int i=from;i<=to;i++) {nq.addRow();
042                            for(int y=0;y<columns.length;y++) {
043                                    nq.setAt(columns[y], row, qry.getAt(columns[y], i));
044                            }
045                            row++;
046                    }
047                    return nq;
048            }
049            
050    
051    }