001 /** 002 * Implements the CFML 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.functions.BIF; 010 import railo.runtime.op.Caster; 011 import railo.runtime.type.Collection; 012 import railo.runtime.type.Query; 013 import railo.runtime.type.QueryImpl; 014 015 public final class QuerySlice extends BIF { 016 017 private static final long serialVersionUID = -2760070317171532995L; 018 019 public static Query call(PageContext pc , Query qry,double offset) throws PageException { 020 return call(pc , qry, offset,0); 021 } 022 public static Query call(PageContext pc , Query qry,double offset,double length) throws PageException { 023 024 int len=qry.getRecordcount(); 025 if(offset>0) { 026 if(len<offset)throw new FunctionException(pc,"querySlice",2,"offset","offset can be greater than recordcount of the query"); 027 028 int to=0; 029 if(length>0)to=(int)(offset+length-1); 030 else if(length<=0)to=(int)(len+length); 031 if(len<to) 032 throw new FunctionException(pc,"querySlice",3,"length","offset+length can be greater than recordcount of the query"); 033 034 return get(qry,(int)offset,to); 035 } 036 return call(pc ,qry,len+offset,length); 037 } 038 039 @Override 040 public Object invoke(PageContext pc, Object[] args) throws PageException { 041 if(args.length==2)return call(pc,Caster.toQuery(args[0]),Caster.toDoubleValue(args[1])); 042 return call(pc,Caster.toQuery(args[0]),Caster.toDoubleValue(args[1]),Caster.toDoubleValue(args[2])); 043 } 044 045 private static Query get(Query qry, int from, int to) throws PageException { 046 Collection.Key[] columns; 047 //print.out(from+"::"+to); 048 Query nq=new QueryImpl(columns=qry.getColumnNames(),0,qry.getName()); 049 050 int row=1; 051 for(int i=from;i<=to;i++) {nq.addRow(); 052 for(int y=0;y<columns.length;y++) { 053 nq.setAt(columns[y], row, qry.getAt(columns[y], i)); 054 } 055 row++; 056 } 057 return nq; 058 } 059 060 061 }