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 valuelist
021 */
022package lucee.runtime.functions.query;
023
024import lucee.runtime.PageContext;
025import lucee.runtime.PageContextImpl;
026import lucee.runtime.exp.ExpressionException;
027import lucee.runtime.exp.PageException;
028import lucee.runtime.ext.function.Function;
029import lucee.runtime.op.Caster;
030import lucee.runtime.type.Query;
031import lucee.runtime.type.QueryColumn;
032import lucee.runtime.type.ref.VariableReference;
033import lucee.runtime.type.scope.Scope;
034
035public class ValueList implements Function {
036        
037        private static final long serialVersionUID = -6503473251723048160L;
038
039        
040        public static String call(PageContext pc , String strQueryColumn) throws PageException {
041                return call(pc,toColumn(pc,strQueryColumn),",");
042        }
043        public static String call(PageContext pc , String strQueryColumn, String delimiter) throws PageException {
044                return call(pc,toColumn(pc,strQueryColumn),delimiter);          
045        }
046        public static String call(PageContext pc , QueryColumn column) throws PageException {
047                return call(pc,column,",");
048        }
049        public static String call(PageContext pc , QueryColumn column, String delimiter) throws PageException {
050                StringBuilder sb=new StringBuilder();
051                int size=column.size();
052                for(int i=1;i<=size;i++) {
053                        if(i>1)sb.append(delimiter);
054                        sb.append(Caster.toString(column.get(i,null)));
055                }
056                return sb.toString();
057        }
058        
059        protected static QueryColumn toColumn(PageContext pc,String strQueryColumn) throws PageException {
060            //if(strQueryColumn.indexOf('.')<1)
061            //    throw new ExpressionException("invalid query column definition ["+strQueryColumn+"]");
062                VariableReference ref = ((PageContextImpl)pc).getVariableReference(strQueryColumn);
063                if(ref.getParent() instanceof Scope)
064                    throw new ExpressionException("invalid query column definition ["+strQueryColumn+"]");
065                
066                
067                Query query=Caster.toQuery(ref.getParent());
068                return query.getColumn(ref.getKey());
069        }
070}