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.commons.lang.ExceptionUtil;
025import lucee.runtime.PageContext;
026import lucee.runtime.db.SQLCaster;
027import lucee.runtime.exp.PageException;
028import lucee.runtime.functions.BIF;
029import lucee.runtime.op.Caster;
030import lucee.runtime.type.Array;
031import lucee.runtime.type.ArrayImpl;
032import lucee.runtime.type.QueryColumn;
033
034public class ValueArray extends BIF {
035        
036        private static final long serialVersionUID = -1810991362001086246L;
037
038        public static Array call(PageContext pc , QueryColumn column) throws PageException {
039                Array arr=new ArrayImpl();
040            int size=column.size();
041            Object obj;
042            short type = SQLCaster.toCFType(column.getType(), lucee.commons.lang.CFTypes.TYPE_UNDEFINED);
043            
044                for(int i=1;i<=size;i++) {
045                        obj=column.get(i,null);
046                        try{
047                                obj=Caster.castTo(pc, type, column.getTypeAsString(), obj);
048                        }
049                        catch(Throwable t){
050                                ExceptionUtil.rethrowIfNecessary(t);
051                        }
052                        arr.append(obj);
053                }
054                return arr;     
055        }
056        
057        public static Array call(PageContext pc , String strQueryColumn) throws PageException {
058            return call(pc, ValueList.toColumn(pc,strQueryColumn));
059        } 
060        
061        @Override
062        public Object invoke(PageContext pc, Object[] args) throws PageException {
063                if(args[0] instanceof QueryColumn) return call(pc, (QueryColumn)args[0]);
064                return call(pc,Caster.toString(args[0]));
065        }
066}