cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r676399 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src: main/java/org/apache/cayenne/access/jdbc/ main/java/org/apache/cayenne/dba/postgres/ test/java/org/apache/cayenne/access/ test/java/org/apache/cayenne/query/
Date Sun, 13 Jul 2008 18:35:29 GMT
Author: aadamchik
Date: Sun Jul 13 11:35:29 2008
New Revision: 676399

URL: http://svn.apache.org/viewvc?rev=676399&view=rev
Log:
CAY-1068 Add support for start index/offset of queries
(adding more unit tests; fixing a postgres glitch with LIMIT = max int)

Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SelectAction.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/postgres/PostgresSelectTranslator.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextSQLTemplateTest.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/query/SelectQueryTest.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SelectAction.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SelectAction.java?rev=676399&r1=676398&r2=676399&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SelectAction.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SelectAction.java
Sun Jul 13 11:35:29 2008
@@ -164,6 +164,10 @@
     }
 
     /**
+     * Returns a value of the offset that will be used to rewind the ResultSet before
+     * starting reading the result rows. If the adapter supports setting offset at the SQL
+     * level, this method must be overridden to return zero for no manual offset.
+     * 
      * @since 3.0
      */
     protected int getFetchOffset() {

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/postgres/PostgresSelectTranslator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/postgres/PostgresSelectTranslator.java?rev=676399&r1=676398&r2=676399&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/postgres/PostgresSelectTranslator.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/postgres/PostgresSelectTranslator.java
Sun Jul 13 11:35:29 2008
@@ -32,17 +32,17 @@
     public String createSqlString() throws Exception {
         String sql = super.createSqlString();
         QueryMetadata metadata = getQuery().getMetaData(getEntityResolver());
-        
-     // limit results
+
+        // limit results
         int offset = metadata.getFetchOffset();
         int limit = metadata.getFetchLimit();
+
+        if (limit > 0) {
+            sql += " LIMIT " + limit;
+        }
         
-        if (offset > 0 || limit > 0) {
-            sql += " LIMIT ";
-            if (limit == 0) {
-                limit = Integer.MAX_VALUE;
-            }
-            sql += limit + " OFFSET " +  offset; 
+        if (offset > 0) {
+            sql += " OFFSET " + offset;
         }
         return sql;
     }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextSQLTemplateTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextSQLTemplateTest.java?rev=676399&r1=676398&r2=676399&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextSQLTemplateTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextSQLTemplateTest.java
Sun Jul 13 11:35:29 2008
@@ -88,7 +88,7 @@
         assertEquals(2, array3.length);
         Object[] array4 = (Object[]) objects.get(3);
         assertEquals(2, array3.length);
-        
+
         assertEquals(new Integer(1), array1[1]);
         assertEquals(new Integer(1), array2[1]);
         assertEquals(new Integer(0), array3[1]);
@@ -442,6 +442,39 @@
         assertTrue(objects.get(0) instanceof Artist);
     }
 
+    public void testFetchOffset() throws Exception {
+        getAccessStack().createTestData(DataContextCase.class, "testArtists", null);
+
+        int fetchOffset = 3;
+
+        // sanity check
+        assertTrue(fetchOffset < DataContextCase.artistCount);
+        String template = "SELECT * FROM ARTIST ORDER BY ARTIST_ID";
+        SQLTemplate query = getSQLTemplateBuilder().createSQLTemplate(
+                Artist.class,
+                template);
+        query.setFetchOffset(fetchOffset);
+
+        List objects = context.performQuery(query);
+        assertEquals(DataContextCase.artistCount - fetchOffset, objects.size());
+        assertTrue(objects.get(0) instanceof Artist);
+    }
+
+    public void testFetchOffsetFetchLimit() throws Exception {
+        getAccessStack().createTestData(DataContextCase.class, "testArtists", null);
+
+        String template = "SELECT * FROM ARTIST ORDER BY ARTIST_ID";
+        SQLTemplate query = getSQLTemplateBuilder().createSQLTemplate(
+                Artist.class,
+                template);
+        query.setFetchOffset(1);
+        query.setFetchLimit(2);
+
+        List objects = context.performQuery(query);
+        assertEquals(2, objects.size());
+        assertTrue(objects.get(0) instanceof Artist);
+    }
+
     public void testPageSize() throws Exception {
         getAccessStack().createTestData(DataContextCase.class, "testArtists", null);
 

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/query/SelectQueryTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/query/SelectQueryTest.java?rev=676399&r1=676398&r2=676399&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/query/SelectQueryTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/query/SelectQueryTest.java
Sun Jul 13 11:35:29 2008
@@ -52,17 +52,13 @@
     }
 
     public void testFetchOffset() throws Exception {
-        query.setRoot(Artist.class);
+        SelectQuery query = new SelectQuery(Artist.class);
         query.setFetchOffset(5);
-        performQuery();
-        int size = opObserver.rowsForQuery(query).size();
+        List results = createDataContext().performQuery(query);
 
-        SelectQuery sizeQ = new SelectQuery();
-        sizeQ.setRoot(Artist.class);
-        query = sizeQ;
-        performQuery();
-        int sizeAll = opObserver.rowsForQuery(query).size();
-        assertEquals(size, sizeAll - 5);
+        SelectQuery sizeQ = new SelectQuery(Artist.class);
+        List allResults = createDataContext().performQuery(sizeQ);
+        assertEquals(allResults.size() - 5, results.size());
     }
 
     public void testFetchLimitWithOffset() throws Exception {



Mime
View raw message