}
Struct res=new StructImpl();
DataSourceManager manager = pageContext.getDataSourceManager();
DatasourceConnection dc = ds instanceof DataSource?
manager.getConnection(pageContext,(DataSource)ds,username,password):
manager.getConnection(pageContext,Caster.toString(ds),username,password);
// create returnValue
returnValue(dc);
// create SQL
StringBuilder sql=createSQL();
// add returnValue to params
if(returnValue!=null){
params.add(0,returnValue);
}
SQLImpl _sql=new SQLImpl(sql.toString());
CallableStatement callStat=null;
try {
callStat = dc.getConnection().prepareCall(sql.toString());
//ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
//ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
if(blockfactor>0)callStat.setFetchSize(blockfactor);
if(timeout>0)callStat.setQueryTimeout(timeout);
// set IN register OUT
Iterator<ProcParamBean> it = params.iterator();
ProcParamBean param;
int index=1;
while(it.hasNext()) {
param= it.next();
param.setIndex(index);
_sql.addItems(new SQLItemImpl(param.getValue()));
if(param.getDirection()!=ProcParamBean.DIRECTION_OUT) {
SQLCaster.setValue(pageContext.getTimeZone(),callStat, index, param);
}
if(param.getDirection()!=ProcParamBean.DIRECTION_IN) {
registerOutParameter(callStat,param);
}
index++;
}
// cache
boolean isFromCache=false;
boolean hasCached=cachedbefore!=null || cachedafter!=null;
Object cacheValue=null;
String dsn = ds instanceof DataSource?((DataSource)ds).getName():Caster.toString(ds);
if(clearCache) {
hasCached=false;
pageContext.getQueryCache().remove(pageContext,_sql,dsn,username,password);
}
else if(hasCached) {
cacheValue = pageContext.getQueryCache().get(pageContext,_sql,dsn,username,password,cachedafter);
}
int count=0;
if(cacheValue==null){
// execute
boolean isResult=callStat.execute();
Struct cache=hasCached?new StructImpl():null;
// resultsets
ProcResultBean result;
index=1;
do {
if(isResult){
ResultSet rs=callStat.getResultSet();
if(rs!=null) {
try{
result=(ProcResultBean) results.get(index++,null);
if(result!=null) {
railo.runtime.type.Query q = new QueryImpl(rs,result.getMaxrows(),result.getName(),pageContext.getTimeZone());
count+=q.getRecordcount();
setVariable(result.getName(), q);
if(hasCached)cache.set(KeyImpl.getInstance(result.getName()), q);
}
}
finally{
IOUtil.closeEL(rs);
}
}
}
}
while((isResult=callStat.getMoreResults()) || (callStat.getUpdateCount() != -1));
// params
it = params.iterator();
while(it.hasNext()) {
param= it.next();
if(param.getDirection()!=ProcParamBean.DIRECTION_IN){
Object value=null;
if(!StringUtil.isEmpty(param.getVariable())){
try{
value=SQLCaster.toCFType(callStat.getObject(param.getIndex()));
}
catch(Throwable t){}
value=emptyIfNull(value);
if(param==STATUS_CODE) res.set(STATUSCODE, value);
else setVariable(param.getVariable(), value);
if(hasCached)cache.set(KeyImpl.getInstance(param.getVariable()), value);
}
}
}
if(hasCached){
cache.set(COUNT, Caster.toDouble(count));
pageContext.getQueryCache().set(pageContext,_sql,dsn,username,password,cache,cachedbefore);
}
}
else if(cacheValue instanceof Struct) {
Struct sctCache = (Struct) cacheValue;
count=Caster.toIntValue(sctCache.removeEL(COUNT),0);
Iterator<Entry<Key, Object>> cit = sctCache.entryIterator();
Entry<Key, Object> ce;
while(cit.hasNext()){
ce = cit.next();
if(STATUS_CODE.getVariable().equals(ce.getKey().getString()))
res.set(KEY_SC, ce.getValue());
else setVariable(ce.getKey().getString(), ce.getValue());
}
isFromCache=true;
}
// result
long exe;
setVariable(this.result, res);
res.set(KeyConstants._executionTime,Caster.toDouble(exe=(System.nanoTime()-startNS)));
res.set(KeyConstants._cached,Caster.toBoolean(isFromCache));
if(pageContext.getConfig().debug() && debug) {
boolean logdb=((ConfigImpl)pageContext.getConfig()).hasDebugOptions(ConfigImpl.DEBUG_DATABASE);
if(logdb)
pageContext.getDebugger().addQuery(null,dsn,procedure,_sql,count,pageContext.getCurrentPageSource(),(int)exe);
}
}
catch (SQLException e) {
throw new DatabaseException(e,new SQLImpl(sql.toString()),dc);
}
finally {
if(callStat!=null){
try {
callStat.close();
} catch (SQLException e) {}
}
manager.releaseConnection(pageContext,dc);
}
return EVAL_PAGE;
}