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 **/ 019/** 020 * Implements the CFML Function querysetcell 021 */ 022package lucee.runtime.functions.query; 023 024import lucee.runtime.PageContext; 025import lucee.runtime.exp.FunctionException; 026import lucee.runtime.exp.PageException; 027import lucee.runtime.functions.BIF; 028import lucee.runtime.op.Caster; 029import lucee.runtime.type.Collection; 030import lucee.runtime.type.Query; 031import lucee.runtime.type.QueryImpl; 032import lucee.runtime.type.Struct; 033import lucee.runtime.type.StructImpl; 034import lucee.runtime.type.util.KeyConstants; 035 036public final class QueryConvertForGrid extends BIF { 037 038 private static final long serialVersionUID = 871091293736619034L; 039 040 public static Struct call(PageContext pc , Query src, double dpage,double dpageSize) throws PageException { 041 int page=(int) dpage; 042 int pageSize=(int) dpageSize; 043 if(page<1) { 044 throw new FunctionException(pc,"QueryConvertForGrid",2,"page","page must be a positive number now ("+page+")"); 045 } 046 047 int start = ((page-1)*pageSize)+1; 048 int end = start+pageSize; 049 050 Collection.Key[] srcColumns = src.getColumnNames(); 051 int srcRows = src.getRowCount(); 052 053 int trgRows = srcRows-start+1; 054 if (trgRows > pageSize) trgRows = pageSize; 055 if(trgRows<0)trgRows=0; 056 057 Query trg=new QueryImpl(srcColumns,trgRows,src.getName()); 058 int trgRow=0; 059 for (int srcRow = start; (srcRow <= end) && (srcRow <= srcRows); srcRow++) { 060 trgRow++; 061 for (int col = 0; col < srcColumns.length; col++){ 062 trg.setAtEL( 063 srcColumns[col], 064 trgRow, 065 src.getAt(srcColumns[col],srcRow, null)); 066 } 067 } 068 069 Struct sct = new StructImpl(); 070 sct.setEL(KeyConstants._QUERY, trg); 071 sct.setEL("TOTALROWCOUNT", new Integer(srcRows)); 072 return sct; 073 } 074 075 @Override 076 public Object invoke(PageContext pc, Object[] args) throws PageException { 077 return call(pc,Caster.toQuery(args[0]),Caster.toDoubleValue(args[1]),Caster.toDoubleValue(args[2])); 078 } 079}