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.intergral.fusiondebug.server.type.qry;
020
021import java.util.ArrayList;
022import java.util.List;
023
024import lucee.intergral.fusiondebug.server.type.FDValueNotMutability;
025import lucee.intergral.fusiondebug.server.type.FDVariable;
026import lucee.intergral.fusiondebug.server.type.simple.FDSimpleVariable;
027import lucee.runtime.op.Caster;
028import lucee.runtime.type.Query;
029
030import com.intergral.fusiondebug.server.IFDStackFrame;
031
032public class FDQuery extends FDValueNotMutability {
033
034        private static final int INTERVAL = 10;
035        private ArrayList children=new ArrayList();
036        private Query qry;
037        
038        public FDQuery(IFDStackFrame frame,Query qry){
039                this.qry=qry;
040                
041                // columns
042                String[] strColumns = qry.getColumns();
043                List lstColumns=new ArrayList();
044                String type;
045                for(int i=0;i<strColumns.length;i++){
046                        type=qry.getColumn(strColumns[i],null).getTypeAsString();
047                        //else type="";
048                        lstColumns.add(new FDSimpleVariable(frame,strColumns[i],type,null));
049                }
050                children.add(new FDSimpleVariable(frame,"Columns",Caster.toString(strColumns.length),lstColumns));
051                
052                // rows
053                int rowcount=qry.getRowCount();
054                List lstRows=new ArrayList();//,values;
055                fill(frame,qry,lstRows,1,rowcount-1,strColumns);
056                children.add(new FDSimpleVariable(frame,"Rows",Caster.toString(rowcount),lstRows));
057        }
058        
059        private static void fill(IFDStackFrame frame, Query qry, List lstRows, int start,int len, String[] strColumns) {
060                int to=start+len;
061                int interval = INTERVAL;
062                while(interval*interval<len)
063                        interval*=interval;
064                if(len>interval){
065                        int max;
066                        for(int i=start;i<to;i+=interval)    {
067                                max=(i+interval)<to?(interval-1):to-i;
068                                ArrayList group=new ArrayList();
069                                lstRows.add(new FDSimpleVariable(frame,"Rows","["+i+"-"+((i+max))+"]",group));
070                                fill(frame, qry, group, i, max, strColumns);
071                        }
072                }
073                else {
074                        ArrayList values;
075                        for(int r=start;r<=to;r++){
076                                values=new ArrayList();
077                                for(int c=0;c<strColumns.length;c++){
078                                        values.add(new FDVariable(frame,strColumns[c],new FDQueryNode(frame,qry,r,strColumns[c])));
079                                }
080                                lstRows.add(new FDSimpleVariable(frame,"Row","["+r+"]",values));
081                        }
082                }
083        }
084
085
086        @Override
087        public List getChildren() {
088                return children;
089        }
090
091        @Override
092        public boolean hasChildren() {
093                return true;
094        }
095        
096        @Override
097        public String toString() {
098                return "Query(Columns:"+qry.getColumns().length+", Rows:"+qry.getRecordcount()+")";
099        }
100
101}