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.functions.query; 020 021import java.util.Iterator; 022 023import lucee.commons.lang.ExceptionUtil; 024import lucee.runtime.PageContext; 025import lucee.runtime.config.NullSupportHelper; 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.KeyImpl; 033import lucee.runtime.type.Query; 034import lucee.runtime.type.QueryColumn; 035import lucee.runtime.type.UDF; 036 037public class QueryColumnData extends BIF { 038 039 private static final long serialVersionUID = 3915214686428831274L; 040 041 public static Array call(PageContext pc, Query query, String columnName) throws PageException { 042 return call(pc, query, columnName, null); 043 } 044 public static Array call(PageContext pc, Query query, String columnName, UDF udf) throws PageException { 045 Array arr=new ArrayImpl(); 046 QueryColumn column = query.getColumn(KeyImpl.init(columnName)); 047 Iterator<Object> it = column.valueIterator(); 048 Object value; 049 short type = SQLCaster.toCFType(column.getType(), lucee.commons.lang.CFTypes.TYPE_UNDEFINED); 050 051 while(it.hasNext()) { 052 value=it.next(); 053 if(!NullSupportHelper.full() && value==null) value=""; 054 055 // callback call 056 if(udf!=null)value=udf.call(pc, new Object[]{value}, true); 057 058 // convert (if necessary) 059 try{ 060 value=Caster.castTo(pc, type, column.getTypeAsString(), value); 061 } 062 catch(Throwable t){ 063 ExceptionUtil.rethrowIfNecessary(t); 064 t.printStackTrace(); 065 } 066 067 arr.append(value); 068 } 069 return arr; 070 } 071 072 @Override 073 public Object invoke(PageContext pc, Object[] args) throws PageException { 074 if(args.length==2)return call(pc,Caster.toQuery(args[0]),Caster.toString(args[1])); 075 return call(pc,Caster.toQuery(args[0]),Caster.toString(args[1]),Caster.toFunction(args[2])); 076 } 077}