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 queryaddcolumn
021 */
022package lucee.runtime.functions.query;
023
024import lucee.commons.lang.StringUtil;
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.op.Decision;
031import lucee.runtime.type.ArrayImpl;
032import lucee.runtime.type.KeyImpl;
033import lucee.runtime.type.Query;
034
035public final class QueryAddColumn extends BIF {
036
037        private static final long serialVersionUID = -242783888553490683L;
038
039        public static double call(PageContext pc , Query query, String string) throws PageException {
040                return call(pc, query, string,null,new ArrayImpl());
041        }
042        
043        public static double call(PageContext pc , Query query, String string, Object arrayOrDataType) throws PageException {
044                if(!Decision.isArray(arrayOrDataType))
045                        return call(pc, query, string, Caster.toString(arrayOrDataType), new ArrayImpl());
046                return call(pc, query, string, null, Caster.toArray(arrayOrDataType));
047        }
048        
049        public static double call(PageContext pc , Query query, String string, Object datatype, Object array) throws PageException {
050                if(StringUtil.isEmpty(datatype))
051                        query.addColumn(KeyImpl.init(string),Caster.toArray(array));
052                else
053                        query.addColumn(KeyImpl.init(string),Caster.toArray(array),SQLCaster.toSQLType(Caster.toString(datatype)));
054                return query.size();
055        }
056        
057        @Override
058        public Object invoke(PageContext pc, Object[] args) throws PageException {
059                if(args.length==2)return call(pc,Caster.toQuery(args[0]),Caster.toString(args[1]));
060                if(args.length==3)return call(pc,Caster.toQuery(args[0]),Caster.toString(args[1]),args[2]);
061                return call(pc,Caster.toQuery(args[0]),Caster.toString(args[1]),args[2],args[3]);
062        }
063}