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 structkeyexists
021 */
022package lucee.runtime.functions.struct;
023
024import lucee.runtime.PageContext;
025import lucee.runtime.config.NullSupportHelper;
026import lucee.runtime.exp.FunctionException;
027import lucee.runtime.exp.PageException;
028import lucee.runtime.functions.BIF;
029import lucee.runtime.functions.query.QueryColumnExists;
030import lucee.runtime.op.Caster;
031import lucee.runtime.type.Collection;
032import lucee.runtime.type.CollectionStruct;
033import lucee.runtime.type.KeyImpl;
034import lucee.runtime.type.Query;
035
036public final class StructKeyExists extends BIF {
037
038        private static final long serialVersionUID = 7659087310641834209L;
039
040        public static boolean call(PageContext pc , lucee.runtime.type.Struct struct, String key) {
041                return call(pc, struct, KeyImpl.init(key));
042        }
043        
044        public static boolean call(PageContext pc , lucee.runtime.type.Struct struct, Collection.Key key) {
045                if(struct instanceof CollectionStruct) {
046                        Collection c=((CollectionStruct) struct).getCollection();
047                        if(c instanceof Query) {
048                                return QueryColumnExists.call(pc, (Query)c, key);
049                        }
050                }
051                if(NullSupportHelper.full()) return struct.containsKey(key);
052                
053                return struct.containsKey(key) && struct.get(key,null)!=null;// do not change, this has do be this way
054        }
055
056        @Override
057        public Object invoke(PageContext pc, Object[] args) throws PageException {
058                if(args.length==2)
059                        return call(pc,Caster.toStruct(args[0]),Caster.toKey(args[1]));
060                        
061                throw new FunctionException(pc, "StructKeyExists", 2, 2, args.length);
062        }
063}