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.util;
020
021import lucee.runtime.PageContext;
022import lucee.runtime.config.NullSupportHelper;
023import lucee.runtime.exp.DeprecatedException;
024import lucee.runtime.exp.PageRuntimeException;
025import lucee.runtime.op.Duplicator;
026import lucee.runtime.type.Collection.Key;
027import lucee.runtime.type.KeyImpl;
028import lucee.runtime.type.Query;
029import lucee.runtime.type.QueryColumn;
030
031/**
032 * Stack for Query Objects
033 */
034public final class QueryStackImpl implements QueryStack {
035        Query[] queries=new Query[20];
036        int start=queries.length;
037        
038        @Override
039        public QueryStack duplicate(boolean deepCopy){
040                QueryStackImpl qs=new QueryStackImpl();
041                if(deepCopy) {
042                        qs.queries=new Query[queries.length];
043                        for(int i=0;i<queries.length;i++) {
044                                qs.queries[i]=(Query)Duplicator.duplicate(queries[i],deepCopy);
045                        }
046                }
047                else qs.queries=queries;
048                
049                qs.start=start;
050                return qs;
051        }
052
053        @Override
054        public void addQuery(Query query) {
055                if(start<1)grow();
056        queries[--start]= query;
057        }
058
059        @Override
060        public void removeQuery() {
061        //print.ln("queries["+start+"]=null;");
062        queries[start++]=null;
063        }
064        
065        @Override
066        public boolean isEmpty() {
067                return start==queries.length;
068        }
069
070        @Override
071        public Object getDataFromACollection(String key) {
072                throw new PageRuntimeException(new DeprecatedException("this method is no longer supported, use instead getDataFromACollection(PageContext pc,Key key, Object defaultValue)"));
073        }
074        
075        @Override
076        public Object getDataFromACollection(PageContext pc,String key) {
077                throw new PageRuntimeException(new DeprecatedException("this method is no longer supported, use instead getDataFromACollection(PageContext pc,Key key, Object defaultValue)"));
078        }
079
080        @Override
081        public Object getDataFromACollection(Key key) {
082                throw new PageRuntimeException(new DeprecatedException("this method is no longer supported, use instead getDataFromACollection(PageContext pc,Key key, Object defaultValue)"));
083        }
084        
085        @Override
086        public Object getDataFromACollection(PageContext pc,Key key) {
087                throw new PageRuntimeException(new DeprecatedException("this method is no longer supported, use instead getDataFromACollection(PageContext pc,Key key, Object defaultValue)"));
088        }
089        
090        // FUTURE add to interface and set above to deprecated
091        public Object getDataFromACollection(PageContext pc,Key key, Object defaultValue) {
092                //Object rtn;
093                QueryColumn col;
094                // get data from queries
095                for(int i=start;i<queries.length;i++) {
096                        col = queries[i].getColumn(key,null);
097                        if(col!=null) return col.get(queries[i].getCurrentrow(pc.getId()),NullSupportHelper.empty());
098                        //rtn=((Objects)queries[i]).get(pc,key,Null.NULL);
099                        //if(rtn!=Null.NULL) return rtn;
100                }
101                return defaultValue;
102        }
103
104        @Override
105        public QueryColumn getColumnFromACollection(String key) {
106                return getColumnFromACollection(KeyImpl.init(key));
107        }
108
109        @Override
110        public QueryColumn getColumnFromACollection(Key key) {
111                QueryColumn rtn=null;
112                
113                // get data from queries
114                for(int i=start;i<queries.length;i++) {
115                        rtn=queries[i].getColumn(key,null);
116                        if(rtn!=null) {
117                                return rtn;
118                        }
119                }
120                return null;
121        }
122        
123        @Override
124        public void clear() {
125                for(int i=start;i<queries.length;i++) {
126                        queries[i]=null;
127                }
128                start=queries.length;
129        }
130    
131    private void grow() {
132        Query[] tmp=new Query[queries.length+20];
133        for(int i=0;i<queries.length;i++) {
134            tmp[i+20]=queries[i];
135        }
136        queries=tmp;
137        start+=20;
138    }
139    
140    @Override
141        public Query[] getQueries() {
142                Query[] tmp=new Query[queries.length-start];
143                int count=0;
144                for(int i=start;i<queries.length;i++) {
145                        tmp[count++]=queries[i];
146                }
147                return tmp;
148        }
149}