Author: tv Date: Tue Jul 18 12:20:09 2006 New Revision: 423210 URL: http://svn.apache.org/viewvc?rev=423210&view=rev Log: Reworked limit and offset support. Fixed MS-SQL failure in LargeSelect (fixes TORQUE-20).
Modified: db/torque/runtime/trunk/src/java/org/apache/torque/adapter/AbstractDBAdapter.java db/torque/runtime/trunk/src/java/org/apache/torque/adapter/DB.java db/torque/runtime/trunk/src/java/org/apache/torque/adapter/DBSybase.java db/torque/runtime/trunk/src/java/org/apache/torque/util/BasePeer.java db/torque/runtime/trunk/src/java/org/apache/torque/util/LargeSelect.java Modified: db/torque/runtime/trunk/src/java/org/apache/torque/adapter/AbstractDBAdapter.java URL: http://svn.apache.org/viewvc/db/torque/runtime/trunk/src/java/org/apache/torque/adapter/AbstractDBAdapter.java?rev=423210&r1=423209&r2=423210&view=diff ============================================================================== --- db/torque/runtime/trunk/src/java/org/apache/torque/adapter/AbstractDBAdapter.java (original) +++ db/torque/runtime/trunk/src/java/org/apache/torque/adapter/AbstractDBAdapter.java Tue Jul 18 12:20:09 2006 @@ -21,6 +21,7 @@ import java.sql.Timestamp; import java.util.Date; +import org.apache.torque.TorqueException; import org.apache.torque.util.Query; /** @@ -176,8 +177,11 @@ * @param query The query to modify * @param offset the offset Value * @param limit the limit Value + * + * @throws TorqueException if any error occurs when building the query */ public void generateLimits(Query query, int offset, int limit) + throws TorqueException { if (supportsNativeLimit()) { Modified: db/torque/runtime/trunk/src/java/org/apache/torque/adapter/DB.java URL: http://svn.apache.org/viewvc/db/torque/runtime/trunk/src/java/org/apache/torque/adapter/DB.java?rev=423210&r1=423209&r2=423210&view=diff ============================================================================== --- db/torque/runtime/trunk/src/java/org/apache/torque/adapter/DB.java (original) +++ db/torque/runtime/trunk/src/java/org/apache/torque/adapter/DB.java Tue Jul 18 12:20:09 2006 @@ -21,6 +21,7 @@ import java.sql.SQLException; import java.util.Date; +import org.apache.torque.TorqueException; import org.apache.torque.util.Query; /** @@ -199,8 +200,11 @@ * @param query The query to modify * @param offset the offset Value * @param limit the limit Value + * + * @throws TorqueException if any error occurs when building the query */ - public void generateLimits(Query query, int offset, int limit); + public void generateLimits(Query query, int offset, int limit) + throws TorqueException; /** * This method is for the SqlExpression.quoteAndEscape rules. The rule is, Modified: db/torque/runtime/trunk/src/java/org/apache/torque/adapter/DBSybase.java URL: http://svn.apache.org/viewvc/db/torque/runtime/trunk/src/java/org/apache/torque/adapter/DBSybase.java?rev=423210&r1=423209&r2=423210&view=diff ============================================================================== --- db/torque/runtime/trunk/src/java/org/apache/torque/adapter/DBSybase.java (original) +++ db/torque/runtime/trunk/src/java/org/apache/torque/adapter/DBSybase.java Tue Jul 18 12:20:09 2006 @@ -19,10 +19,13 @@ import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; -import java.util.Date; import java.text.SimpleDateFormat; +import java.util.Date; +import org.apache.torque.TorqueException; +import org.apache.torque.util.Criteria; import org.apache.torque.util.Query; +import org.apache.torque.util.SqlExpression; /** * This is used to connect to a Sybase database using Sybase's @@ -154,10 +157,21 @@ * @param query The query to modify * @param offset the offset Value * @param limit the limit Value + * + * @throws TorqueException if any error occurs when building the query */ public void generateLimits(Query query, int offset, int limit) + throws TorqueException { - query.setRowcount(String.valueOf(limit+offset)); + if (limit + offset > 0) + { + query.setRowcount(String.valueOf(limit + offset)); + } + else if (limit + offset == 0) + { + // This is necessary to create the empty result set that Torque expects + query.getWhereClause().add(SqlExpression.build("1", new Integer(0), Criteria.EQUAL)); + } } /** Modified: db/torque/runtime/trunk/src/java/org/apache/torque/util/BasePeer.java URL: http://svn.apache.org/viewvc/db/torque/runtime/trunk/src/java/org/apache/torque/util/BasePeer.java?rev=423210&r1=423209&r2=423210&view=diff ============================================================================== --- db/torque/runtime/trunk/src/java/org/apache/torque/util/BasePeer.java (original) +++ db/torque/runtime/trunk/src/java/org/apache/torque/util/BasePeer.java Tue Jul 18 12:20:09 2006 @@ -999,14 +999,10 @@ results = new ArrayList(numberOfResults); qds.fetchRecords(start, numberOfResults); } - if (qds.size() > 1 && singleRecord) - { - handleMultipleRecords(qds); - } int startRecord = 0; - //Offset the correct number of people + //Offset the correct number of records if (start > 0 && numberOfResults <= 0) { startRecord = start; @@ -1017,6 +1013,11 @@ { Record rec = qds.getRecord(i); results.add(rec); + } + + if (results.size() > 1 && singleRecord) + { + handleMultipleRecords(qds); } } catch (Exception e) Modified: db/torque/runtime/trunk/src/java/org/apache/torque/util/LargeSelect.java URL: http://svn.apache.org/viewvc/db/torque/runtime/trunk/src/java/org/apache/torque/util/LargeSelect.java?rev=423210&r1=423209&r2=423210&view=diff ============================================================================== --- db/torque/runtime/trunk/src/java/org/apache/torque/util/LargeSelect.java (original) +++ db/torque/runtime/trunk/src/java/org/apache/torque/util/LargeSelect.java Tue Jul 18 12:20:09 2006 @@ -671,10 +671,13 @@ public void run() { boolean dbSupportsNativeLimit; + boolean dbSupportsNativeOffset; try { dbSupportsNativeLimit = (Torque.getDB(dbName).supportsNativeLimit()); + dbSupportsNativeOffset + = (Torque.getDB(dbName).supportsNativeOffset()); } catch (TorqueException e) { @@ -685,7 +688,7 @@ } int size; - if (dbSupportsNativeLimit) + if (dbSupportsNativeLimit && dbSupportsNativeOffset) { // retrieve one page at a time size = pageSize; @@ -707,14 +710,21 @@ // 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) { - // Use the criteria to limit the rows that are retrieved to the - // block of records that fit in the predefined memoryLimit. - criteria.setOffset(blockBegin); - // Add 1 to memory limit to check if the query ends on a - // page break. - criteria.setLimit(memoryLimit + 1); + 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); + } } query = BasePeer.createQueryString(criteria); @@ -758,17 +768,17 @@ List tempResults = BasePeer.getSelectResults(qds, size, false); + int startIndex = dbSupportsNativeOffset ? 0 : blockBegin; + synchronized (results) { - for (int i = 0, n = tempResults.size(); i < n; i++) + for (int i = startIndex, n = tempResults.size(); i < n; i++) { - if (dbSupportsNativeLimit - || (i >= blockBegin)) results.add(tempResults.get(i)); } } - if (dbSupportsNativeLimit) + if (dbSupportsNativeLimit && dbSupportsNativeOffset) { currentlyFilledTo += tempResults.size(); } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]