size = blockBegin + memoryLimit + 1;
}
/* The connection to the database. */
Connection conn = null;
/** Used to retrieve query results from Village. */
QueryDataSet qds = null;
try
{
// Add 1 to memory limit to check if the query ends on a page break.
results = new ArrayList(memoryLimit + 1);
// Use the criteria to limit the rows that are retrieved to the
// block of records that fit in the predefined memoryLimit.
if (dbSupportsNativeLimit)
{
if (dbSupportsNativeOffset)
{
criteria.setOffset(blockBegin);
// Add 1 to memory limit to check if the query ends on a
// page break.
criteria.setLimit(memoryLimit + 1);
}
else
{
criteria.setLimit(blockBegin + memoryLimit + 1);
}
}
/*
* Fix criterions relating to booleanint or booleanchar columns
* The defaultTableMap parameter in this call is null because we have
* no default peer class inside LargeSelect. This means that all
* columns not fully qualified will not be modified.
*/
BasePeer.correctBooleans(criteria, null);
query = BasePeer.createQueryString(criteria);
// Get a connection to the db.
conn = Torque.getConnection(dbName);
// Execute the query.
if (log.isDebugEnabled())
{
log.debug("run(): query = " + query);
log.debug("run(): memoryLimit = " + memoryLimit);
log.debug("run(): blockBegin = " + blockBegin);
log.debug("run(): blockEnd = " + blockEnd);
}
qds = new QueryDataSet(conn, query);
// Continue getting rows one page at a time until the memory limit
// is reached, all results have been retrieved, or the rest
// of the results have been determined to be irrelevant.
while (!killThread
&& !qds.allRecordsRetrieved()
&& currentlyFilledTo + pageSize <= blockEnd)
{
// This caters for when memoryLimit is not a multiple of
// pageSize which it never is because we always add 1 above.
// not applicable if the db has no native limit where this
// was already considered
if ((currentlyFilledTo + pageSize) >= blockEnd
&& dbSupportsNativeLimit)
{
// Add 1 to check if the query ends on a page break.
size = blockEnd - currentlyFilledTo + 1;
}
if (log.isDebugEnabled())
{
log.debug("run(): Invoking BasePeer.getSelectResults(qds, "
+ size + ", false)");
}
List tempResults
= BasePeer.getSelectResults(qds, size, false);
int startIndex = dbSupportsNativeOffset ? 0 : blockBegin;
synchronized (results)
{
for (int i = startIndex, n = tempResults.size(); i < n; i++)
{
results.add(tempResults.get(i));
}
}
if (dbSupportsNativeLimit && dbSupportsNativeOffset)
{
currentlyFilledTo += tempResults.size();
}
else
{
currentlyFilledTo = tempResults.size() - 1 - blockBegin;
}
boolean perhapsLastPage = true;
// If the extra record was indeed found then we know we are not
// on the last page but we must now get rid of it.
if ((dbSupportsNativeLimit
&& (results.size() == memoryLimit + 1))
|| (!dbSupportsNativeLimit
&& currentlyFilledTo >= memoryLimit))
{
synchronized (results)
{
results.remove(currentlyFilledTo--);
}
perhapsLastPage = false;
}
if (results.size() > 0
&& blockBegin + currentlyFilledTo >= totalRecords)
{
// Add 1 because index starts at 0
totalRecords = blockBegin + currentlyFilledTo + 1;
}
// if the db has limited the datasets, we must retrieve all
// datasets. If not, we are always finished because we fetch
// the whole block at once.
if (qds.allRecordsRetrieved()
|| !dbSupportsNativeLimit)
{
queryCompleted = true;
// The following ugly condition ensures that the totals are
// not finalized when a user does something like requesting
// a page greater than what exists in the database.
if (perhapsLastPage
&& getCurrentPageNumber() <= getTotalPages())
{
totalsFinalized = true;
}
}
qds.clearRecords();
}
if (log.isDebugEnabled())
{
log.debug("run(): While loop terminated because either:");
log.debug("run(): 1. qds.allRecordsRetrieved(): "
+ qds.allRecordsRetrieved());
log.debug("run(): 2. killThread: " + killThread);
log.debug("run(): 3. !(currentlyFilledTo + size <= blockEnd): !"
+ (currentlyFilledTo + pageSize <= blockEnd));
log.debug("run(): - currentlyFilledTo: " + currentlyFilledTo);
log.debug("run(): - size: " + pageSize);
log.debug("run(): - blockEnd: " + blockEnd);
log.debug("run(): - results.size(): " + results.size());
}
}
catch (TorqueException e)
{
log.error(e);
}
catch (SQLException e)
{
log.error(e);
}
catch (DataSetException e)
{
log.error(e);
}
finally
{
try
{
if (qds != null)
{
qds.close();
}
Torque.closeConnection(conn);
}
catch (SQLException e)
{