}
boolean failed = true; // flag to use for checking the state of the execution results
long start = System.currentTimeMillis();
QueryRuntime queryRuntime = om.getOMFContext().getQueryManager().getQueryRuntime();
if (queryRuntime != null)
{
queryRuntime.queryBegin();
}
try
{
// Execute the query
Collection qr = (Collection)performExecute(parameters);
failed = false;
if (shouldReturnSingleRow())
{
try
{
// Single row only needed (unique specified, or using aggregates etc), so just take the first row of the results
if (qr == null || qr.size() == 0)
{
throw new NoQueryResultsException("No query results were returned");
}
else
{
if (applyRangeChecks() && (toExclNo - fromInclNo <= 0))
{
// JDO2 spec 14.6.8 (range excludes instances, so return null)
throw new NoQueryResultsException("No query results were returned in the required range");
}
Iterator qrIter = qr.iterator();
Object firstRow = qrIter.next();
if (qrIter.hasNext())
{
failed = true;
throw new QueryNotUniqueException();
}
return firstRow;
}
}
finally
{
// can close qr right now because we don't return it,
// also user cannot close it otherwise except for calling closeAll()
if (qr != null)
{
close(qr);
}
}
}
// Process any specified range
if (applyRangeChecks())
{
// Range not applied in the compiled query so throw away objects outside the required range
int i=0;
Iterator qr_iter=qr.iterator();
Collection res=new ArrayList();
while (qr_iter.hasNext())
{
Object obj=qr_iter.next();
if (i >= fromInclNo && i < toExclNo)
{
// Accept the item if within range
res.add(obj);
}
i++;
}
return res;
}
else
{
// Range applied in the compiled query (or no range) so just return it
return qr;
}
}
finally
{
if (queryRuntime != null)
{
if (failed)
{
queryRuntime.queryExecutedWithError();
}
else
{
queryRuntime.queryExecuted(System.currentTimeMillis()-start);
}
}
}
}