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 * creates a CFML query Column
021 */
022package lucee.runtime.functions.query;
023
024import lucee.runtime.PageContext;
025import lucee.runtime.exp.DatabaseException;
026import lucee.runtime.exp.PageException;
027import lucee.runtime.functions.BIF;
028import lucee.runtime.type.Array;
029import lucee.runtime.type.FunctionValue;
030import lucee.runtime.type.Query;
031import lucee.runtime.type.QueryImpl;
032
033public final class Query_ extends BIF {
034
035        private static final long serialVersionUID = -3496695992298284984L;
036
037        public static Query call(PageContext pc , Object[] arr) throws DatabaseException {
038                String[] names=new String[arr.length];
039                Array[] columns=new Array[arr.length];
040                int count=0;
041                
042                for(int i=0;i<arr.length;i++) {
043                        if(arr[i] instanceof FunctionValue) {
044                                FunctionValue vf = (FunctionValue)arr[i];
045                                if(vf.getValue() instanceof Array) {
046                                        names[count]=vf.getNameAsString();
047                                        columns[count]=(Array) vf.getValue();
048                                        count++;
049                                }
050                                else throw new DatabaseException("invalid argument for function query, only array as value are allowed","example: query(column1:array(1,2,3))",null,null);
051                        }
052                        else throw new DatabaseException("invalid argument for function query, only named argument are allowed","example: query(column1:array(1,2,3))",null,null);
053                }
054                Query query=new QueryImpl(names,columns,"query");
055                return query;
056        }
057        
058        @Override
059        public Object invoke(PageContext pc, Object[] args) throws PageException {
060                return call(pc,(Object[])args[0]);
061        }
062}