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 isquery
021 */
022package lucee.runtime.functions.query;
023
024import lucee.commons.lang.ExceptionUtil;
025import lucee.runtime.PageContext;
026import lucee.runtime.exp.FunctionException;
027import lucee.runtime.exp.PageException;
028import lucee.runtime.functions.BIF;
029import lucee.runtime.op.Caster;
030import lucee.runtime.tag.TagUtil;
031import lucee.runtime.type.Struct;
032import lucee.transformer.library.tag.TagLibTag;
033
034public final class QueryExecute extends BIF {
035
036        private static final long serialVersionUID = -4714201927377662500L;
037
038        public static Object call(PageContext pc , String sql) throws PageException {
039                return call(pc, sql, null, null);
040        }
041        public static Object call(PageContext pc , String sql, Object params) throws PageException {
042                return call(pc, sql, params, null);
043        }
044        public static Object call(PageContext pc , String sql, Object params, Struct options) throws PageException {
045                
046                lucee.runtime.tag.Query qry = (lucee.runtime.tag.Query) pc.use(lucee.runtime.tag.Query.class.getName());
047                try { 
048                        try {
049                                // set attributes
050                                qry.setReturnVariable(true);
051                                qry.setName("QueryExecute");
052                                if(options!=null)TagUtil.setAttributeCollection(pc, qry, null, options, TagLibTag.ATTRIBUTE_TYPE_FIXED); 
053                                qry.setParams(params);
054                        
055                                int res=qry.doStartTag();
056                                pc.initBody(qry, res);
057                                pc.forceWrite(sql);
058                                qry.doAfterBody();
059                        }
060                        catch(Throwable t){
061                                ExceptionUtil.rethrowIfNecessary(t);
062                                try {
063                                        qry.doCatch(t);
064                                }
065                                catch (Throwable t2) {
066                                        throw Caster.toPageException(t2);
067                                }
068                        }
069                        finally {
070                                pc.popBody();
071                                qry.doFinally();
072                        }
073                        qry.doEndTag();
074                        return qry.getReturnVariable();
075                }
076                finally {
077                        pc.reuse(qry);
078                }
079                
080        }
081
082        @Override
083        public Object invoke(PageContext pc, Object[] args) throws PageException {
084                if(args.length<1 || args.length>3)
085                        throw new FunctionException(pc, "QueryExecute", 1, 3, args.length);
086
087                if(args.length==3)
088                        return call(pc,Caster.toString(args[0]),args[1],Caster.toStruct(args[2]));
089                if(args.length==2)
090                        return call(pc,Caster.toString(args[0]),args[1]);
091                return call(pc,Caster.toString(args[0]));
092        }
093}