throw new DatabaseException("no sql string defined, inside query tag",null,null,null);
// cannot use attribute params and queryparam tag
if(items.size()>0 && params!=null)
throw new DatabaseException("you cannot use the attribute params and sub tags queryparam at the same time",null,null,null);
// create SQL
SQL sql;
if(params!=null) {
if(Decision.isArray(params))
sql=QueryParamConverter.convert(strSQL, Caster.toArray(params));
else if(Decision.isStruct(params))
sql=QueryParamConverter.convert(strSQL, Caster.toStruct(params));
else
throw new DatabaseException("value of the attribute [params] has to be a struct or a array",null,null,null);
}
else sql=items.size()>0?new SQLImpl(strSQL,items.toArray(new SQLItem[items.size()])):new SQLImpl(strSQL);
railo.runtime.type.Query query=null;
long exe=0;
boolean hasCached=cachedWithin!=null || cachedafter!=null;
if(clearCache) {
hasCached=false;
pageContext.getQueryCache().remove(pageContext,sql,datasource!=null?datasource.getName():null,username,password);
}
else if(hasCached) {
query=pageContext.getQueryCache().getQuery(pageContext,sql,datasource!=null?datasource.getName():null,username,password,cachedafter);
}
if(query==null) {
if("query".equals(dbtype)) query=executeQoQ(sql);
else if("orm".equals(dbtype) || "hql".equals(dbtype)) {
long start=System.nanoTime();
Object obj = executeORM(sql,returntype,ormoptions);
if(obj instanceof railo.runtime.type.Query){
query=(railo.runtime.type.Query) obj;
}
else {
if(!StringUtil.isEmpty(name)) {
pageContext.setVariable(name,obj);
}
if(result!=null){
Struct sct=new StructImpl();
sct.setEL(KeyConstants._cached, Boolean.FALSE);
long time=System.nanoTime()-start;
sct.setEL(KeyConstants._executionTime, Caster.toDouble(time/1000000));
sct.setEL(KeyConstants._executionTimeNano, Caster.toDouble(time));
sct.setEL(KeyConstants._SQL, sql.getSQLString());
if(Decision.isArray(obj)){
}
else sct.setEL(KeyConstants._RECORDCOUNT, Caster.toDouble(1));
pageContext.setVariable(result, sct);
}
else
setExecutionTime((System.nanoTime()-start)/1000000);
return EVAL_PAGE;
}
}
else query=executeDatasoure(sql,result!=null,pageContext.getTimeZone());
//query=(dbtype!=null && dbtype.equals("query"))?executeQoQ(sql):executeDatasoure(sql,result!=null);
if(cachedWithin!=null) {
DateTimeImpl cachedBefore = null;
//if(cachedWithin!=null)
cachedBefore=new DateTimeImpl(pageContext,System.currentTimeMillis()+cachedWithin.getMillis(),false);
pageContext.getQueryCache().set(pageContext,sql,datasource!=null?datasource.getName():null,username,password,query,cachedBefore);
}
exe=query.getExecutionTime();
}
else query.setCached(hasCached);
if(pageContext.getConfig().debug() && debug) {
boolean logdb=((ConfigImpl)pageContext.getConfig()).hasDebugOptions(ConfigImpl.DEBUG_DATABASE);
if(logdb){
boolean debugUsage=DebuggerUtil.debugQueryUsage(pageContext,query);
((DebuggerPro)pageContext.getDebugger()).addQuery(debugUsage?query:null,datasource!=null?datasource.getName():null,name,sql,query.getRecordcount(),pageContext.getCurrentPageSource(),exe);
}
}
if(!query.isEmpty() && !StringUtil.isEmpty(name)) {
pageContext.setVariable(name,query);
}
// Result
if(result!=null) {
Struct sct=new StructImpl();
sct.setEL(KeyConstants._cached, Caster.toBoolean(query.isCached()));
if(!query.isEmpty())sct.setEL(KeyConstants._COLUMNLIST, ListUtil.arrayToList(query.getColumnNamesAsString(),","));
int rc=query.getRecordcount();
if(rc==0)rc=query.getUpdateCount();
sct.setEL(KeyConstants._RECORDCOUNT, Caster.toDouble(rc));
sct.setEL(KeyConstants._executionTime, Caster.toDouble(query.getExecutionTime()/1000000));
sct.setEL(KeyConstants._executionTimeNano, Caster.toDouble(query.getExecutionTime()));
sct.setEL(KeyConstants._SQL, sql.getSQLString());
// GENERATED KEYS
railo.runtime.type.Query qi = Caster.toQuery(query,null);
if(qi !=null){
railo.runtime.type.Query qryKeys = qi.getGeneratedKeys();
if(qryKeys!=null){
StringBuilder generatedKey=new StringBuilder(),sb;
Collection.Key[] columnNames = qryKeys.getColumnNames();
QueryColumn column;
for(int c=0;c<columnNames.length;c++){
column = qryKeys.getColumn(columnNames[c]);
sb=new StringBuilder();
int size=column.size();
for(int row=1;row<=size;row++) {
if(row>1)sb.append(',');
sb.append(Caster.toString(column.get(row,null)));
}
if(sb.length()>0){
sct.setEL(columnNames[c], sb.toString());
if(generatedKey.length()>0)generatedKey.append(',');
generatedKey.append(sb);
}
}
if(generatedKey.length()>0)
sct.setEL(GENERATEDKEY, generatedKey.toString());
}
}
// sqlparameters
SQLItem[] params = sql.getItems();
if(params!=null && params.length>0) {
Array arr=new ArrayImpl();
sct.setEL(SQL_PARAMETERS, arr);
for(int i=0;i<params.length;i++) {
arr.append(params[i].getValue());