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.orm; 020 021import lucee.commons.lang.StringUtil; 022import lucee.runtime.PageContext; 023import lucee.runtime.exp.FunctionException; 024import lucee.runtime.exp.PageException; 025import lucee.runtime.op.Caster; 026import lucee.runtime.op.Decision; 027import lucee.runtime.orm.ORMSession; 028import lucee.runtime.orm.ORMUtil; 029import lucee.runtime.type.ArrayImpl; 030import lucee.runtime.type.Struct; 031import lucee.runtime.type.util.KeyConstants; 032 033public class ORMExecuteQuery { 034 035 public static Object call(PageContext pc,String hql) throws PageException { 036 return _call(pc,hql,null,false,null); 037 } 038 public static Object call(PageContext pc,String hql,Object paramsOrUnique) throws PageException { 039 if(Decision.isCastableToBoolean(paramsOrUnique)){ 040 return _call(pc,hql,null,Caster.toBooleanValue(paramsOrUnique),null); 041 } 042 return _call(pc,hql,paramsOrUnique,false,null); 043 } 044 public static Object call(PageContext pc,String hql,Object paramsOrUnique,Object uniqueOrQueryOptions) throws PageException { 045 if(Decision.isCastableToBoolean(paramsOrUnique)){ 046 return _call(pc,hql,null,Caster.toBooleanValue(paramsOrUnique),Caster.toStruct(uniqueOrQueryOptions)); 047 } 048 if(Decision.isCastableToBoolean(uniqueOrQueryOptions)){ 049 return _call(pc,hql,paramsOrUnique,Caster.toBooleanValue(uniqueOrQueryOptions),null); 050 } 051 return _call(pc,hql,paramsOrUnique,false,Caster.toStruct(uniqueOrQueryOptions)); 052 } 053 054 public static Object call(PageContext pc,String hql,Object params,Object unique, Object queryOptions) throws PageException { 055 return _call(pc,hql,params,Caster.toBooleanValue(unique),Caster.toStruct(queryOptions)); 056 } 057 private static Object _call(PageContext pc,String hql, Object params, boolean unique, Struct queryOptions) throws PageException { 058 ORMSession session=ORMUtil.getSession(pc); 059 String dsn = null; 060 if(queryOptions!=null) dsn = Caster.toString(queryOptions.get(KeyConstants._datasource,null),null); 061 if(StringUtil.isEmpty(dsn,true)) dsn=ORMUtil.getDefaultDataSource(pc).getName(); 062 063 if(params==null) 064 return session.executeQuery(pc,dsn,hql,new ArrayImpl(),unique,queryOptions); 065 else if(Decision.isStruct(params)) 066 return session.executeQuery(pc,dsn,hql,Caster.toStruct(params),unique,queryOptions); 067 else if(Decision.isArray(params)) 068 return session.executeQuery(pc,dsn,hql,Caster.toArray(params),unique,queryOptions); 069 else if(Decision.isCastableToStruct(params)) 070 return session.executeQuery(pc,dsn,hql,Caster.toStruct(params),unique,queryOptions); 071 else if(Decision.isCastableToArray(params)) 072 return session.executeQuery(pc,dsn,hql,Caster.toArray(params),unique,queryOptions); 073 else 074 throw new FunctionException(pc, "ORMExecuteQuery", 2, "params", "cannot convert the params to a array or a struct"); 075 } 076}