// Make sure the datastore is prepared (new objects flushed as required)
prepareDatastore();
boolean failed = true; // flag to use for checking the state of the execution results
long start = 0;
QueryRuntime queryRuntime = om.getOMFContext().getQueryManager().getQueryRuntime();
if (queryRuntime != null)
{
start = System.currentTimeMillis();
queryRuntime.queryBegin();
}
try
{
// Execute the query
Object result = performExecute(inputParameters);
// Process the results
if (type == BULK_DELETE || type == BULK_UPDATE)
{
// Bulk update/delete return a Long
return result;
}
else
{
// Select, so return the range of objects
Collection qr = (Collection)result;
failed = false;
if (shouldReturnSingleRow())
{
// Single row only needed (unique specified, or using aggregates etc), so just take first row
try
{
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 results right now because we don't return it,
// also user cannot close it otherwise except for calling closeAll()
if (qr != null)
{
close(qr);
}
}
}
else
{
if (qr instanceof QueryResult)
{
// Result handler, so register the results so we can close later
queryResults.add((QueryResult)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);
}
}
}
}