cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r676373 - in /cayenne/main/trunk: framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/ framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/hsqldb/ framework/cayenne-jdk1.5-unpublished/src/...
Date Sun, 13 Jul 2008 17:20:14 GMT
Author: aadamchik
Date: Sun Jul 13 10:20:13 2008
New Revision: 676373

URL: http://svn.apache.org/viewvc?rev=676373&view=rev
Log:
CAY-1068 Add support for start index/offset of queries
(extending for EJBQL, JPA, ProcedureQuery and SQLTemplate )

Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLAction.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SQLTemplateAction.java
    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/hsqldb/HSQLSelectAction.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/mysql/MySQLSelectAction.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/oracle/OracleSelectAction.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/postgres/PostgresSelectAction.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/BaseQueryMetadata.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EJBQLQuery.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/ProcedureQuery.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/QueryMetadata.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/SQLTemplate.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/SelectQuery.java
    cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/JpaQuery.java
    cayenne/main/trunk/itests/jpa-chapter3/src/test/java/org/apache/cayenne/jpa/itest/ch3/_3_6_1_QueryAPITest.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLAction.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLAction.java?rev=676373&r1=676372&r2=676373&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLAction.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLAction.java
Sun Jul 13 10:20:13 2008
@@ -96,6 +96,7 @@
         // update with metadata
         QueryMetadata md = query.getMetaData(getEntityResolver());
         sqlQuery.setFetchLimit(md.getFetchLimit());
+        sqlQuery.setFetchOffset(md.getFetchOffset());
 
         actionFactory.sqlAction(sqlQuery).performAction(connection, observer);
     }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SQLTemplateAction.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SQLTemplateAction.java?rev=676373&r1=676372&r2=676373&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SQLTemplateAction.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/SQLTemplateAction.java
Sun Jul 13 10:20:13 2008
@@ -217,6 +217,10 @@
 
         boolean iteratedResult = callback.isIteratedResult();
 
+        int i = getFetchOffset();
+        while (i-- > 0 && resultSet.next())
+            ;
+        
         ExtendedTypeMap types = adapter.getExtendedTypes();
         RowDescriptorBuilder builder = configureRowDescriptorBuilder(compiled, resultSet);
 
@@ -350,4 +354,11 @@
     public SQLTemplate getQuery() {
         return query;
     }
+    
+    /**
+     * @since 3.0
+     */
+    protected int getFetchOffset() {
+        return query.getFetchOffset();
+    }
 }

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=676373&r1=676372&r2=676373&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 10:20:13 2008
@@ -72,7 +72,7 @@
         PreparedStatement prepStmt = translator.createStatement();
         ResultSet rs = prepStmt.executeQuery();
 
-        int i = getInitialCursorPosition();
+        int i = getFetchOffset();
         while (i-- > 0 && rs.next())
             ;
 
@@ -163,7 +163,10 @@
         }
     }
 
-    protected int getInitialCursorPosition() {
+    /**
+     * @since 3.0
+     */
+    protected int getFetchOffset() {
         return query.getFetchOffset();
     }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/hsqldb/HSQLSelectAction.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/hsqldb/HSQLSelectAction.java?rev=676373&r1=676372&r2=676373&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/hsqldb/HSQLSelectAction.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/hsqldb/HSQLSelectAction.java
Sun Jul 13 10:20:13 2008
@@ -33,7 +33,7 @@
     }
 
     @Override
-    protected int getInitialCursorPosition() {
+    protected int getFetchOffset() {
         return 0;
     }
 

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/mysql/MySQLSelectAction.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/mysql/MySQLSelectAction.java?rev=676373&r1=676372&r2=676373&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/mysql/MySQLSelectAction.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/mysql/MySQLSelectAction.java
Sun Jul 13 10:20:13 2008
@@ -33,7 +33,7 @@
     }
 
     @Override
-    protected int getInitialCursorPosition() {
+    protected int getFetchOffset() {
         return 0;
     }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/oracle/OracleSelectAction.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/oracle/OracleSelectAction.java?rev=676373&r1=676372&r2=676373&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/oracle/OracleSelectAction.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/oracle/OracleSelectAction.java
Sun Jul 13 10:20:13 2008
@@ -49,7 +49,7 @@
     }
 
     @Override
-    protected int getInitialCursorPosition() {
+    protected int getFetchOffset() {
         return 0;
     }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/postgres/PostgresSelectAction.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/postgres/PostgresSelectAction.java?rev=676373&r1=676372&r2=676373&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/postgres/PostgresSelectAction.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/postgres/PostgresSelectAction.java
Sun Jul 13 10:20:13 2008
@@ -34,7 +34,7 @@
     }
 
     @Override
-    protected int getInitialCursorPosition() {
+    protected int getFetchOffset() {
         return 0;
     }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/BaseQueryMetadata.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/BaseQueryMetadata.java?rev=676373&r1=676372&r2=676373&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/BaseQueryMetadata.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/BaseQueryMetadata.java
Sun Jul 13 10:20:13 2008
@@ -46,7 +46,7 @@
 class BaseQueryMetadata implements QueryMetadata, XMLSerializable, Serializable {
 
     int fetchLimit = QueryMetadata.FETCH_LIMIT_DEFAULT;
-    int fetchStartIndex = QueryMetadata.FETCH_START_INDEX_DEFAULT;
+    int fetchOffset = QueryMetadata.FETCH_OFFSET_DEFAULT;
     
     int pageSize = QueryMetadata.PAGE_SIZE_DEFAULT;
     boolean fetchingDataRows = QueryMetadata.FETCHING_DATA_ROWS_DEFAULT;
@@ -442,7 +442,7 @@
      * @since 3.0
      */
     public int getFetchOffset() {
-        return fetchStartIndex;
+        return fetchOffset;
     }
     
     /**
@@ -468,8 +468,8 @@
         fetchLimit = i;
     }
     
-    void setFetchStartIndex(int i) {
-        fetchStartIndex = i;
+    void setFetchOffset(int i) {
+        fetchOffset = i;
     }
 
     void setPageSize(int i) {

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EJBQLQuery.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EJBQLQuery.java?rev=676373&r1=676372&r2=676373&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EJBQLQuery.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/EJBQLQuery.java
Sun Jul 13 10:20:13 2008
@@ -161,6 +161,14 @@
      * return.
      */
     public void setFetchLimit(int fetchLimit) {
-        this.metadata.setFetchLimit(fetchLimit);
+        metadata.setFetchLimit(fetchLimit);
+    }
+
+    public int getFetchOffset() {
+        return metadata.getFetchOffset();
+    }
+
+    public void setFetchOffset(int fetchOffset) {
+        metadata.setFetchOffset(fetchOffset);
     }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/ProcedureQuery.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/ProcedureQuery.java?rev=676373&r1=676372&r2=676373&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/ProcedureQuery.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/ProcedureQuery.java
Sun Jul 13 10:20:13 2008
@@ -336,6 +336,20 @@
     public void setFetchLimit(int fetchLimit) {
         this.metaData.setFetchLimit(fetchLimit);
     }
+    
+    /**
+     * @since 3.0
+     */
+    public int getFetchOffset() {
+        return metaData.getFetchOffset();
+    }
+
+    /**
+     * @since 3.0
+     */
+    public void setFetchOffset(int fetchOffset) {
+        metaData.setFetchOffset(fetchOffset);
+    }
 
     public int getPageSize() {
         return metaData.getPageSize();

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/QueryMetadata.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/QueryMetadata.java?rev=676373&r1=676372&r2=676373&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/QueryMetadata.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/QueryMetadata.java
Sun Jul 13 10:20:13 2008
@@ -89,7 +89,7 @@
      * 
      * @since 3.0
      */
-    public static final int FETCH_START_INDEX_DEFAULT = 0;
+    public static final int FETCH_OFFSET_DEFAULT = 0;
 
     /**
      * Defines the name of the property for the query {@link #getPageSize() page size}.

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/SQLTemplate.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/SQLTemplate.java?rev=676373&r1=676372&r2=676373&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/SQLTemplate.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/SQLTemplate.java
Sun Jul 13 10:20:13 2008
@@ -397,6 +397,20 @@
     public void setFetchLimit(int fetchLimit) {
         this.metaData.setFetchLimit(fetchLimit);
     }
+    
+    /**
+     * @since 3.0
+     */
+    public int getFetchOffset() {
+        return metaData.getFetchOffset();
+    }
+
+    /**
+     * @since 3.0
+     */
+    public void setFetchOffset(int fetchOffset) {
+        metaData.setFetchOffset(fetchOffset);
+    }
 
     public int getPageSize() {
         return metaData.getPageSize();

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/SelectQuery.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/SelectQuery.java?rev=676373&r1=676372&r2=676373&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/SelectQuery.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/query/SelectQuery.java
Sun Jul 13 10:20:13 2008
@@ -621,7 +621,7 @@
      * @since 3.0
      */
     public void setFetchOffset(int fetchOffset) {
-        this.metaData.setFetchStartIndex(fetchOffset);
+        this.metaData.setFetchOffset(fetchOffset);
     }
 
     /**

Modified: cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/JpaQuery.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/JpaQuery.java?rev=676373&r1=676372&r2=676373&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/JpaQuery.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jpa-unpublished/src/main/java/org/apache/cayenne/jpa/JpaQuery.java
Sun Jul 13 10:20:13 2008
@@ -169,9 +169,28 @@
             throw new IllegalArgumentException("Invalid first result value: "
                     + startPosition);
         }
-        // TODO: support in core like fetchLimit?
-        // TODO: hack a temp solution here based on sub-list?
-        throw new UnsupportedOperationException("TODO");
+
+        Object query = getQuery();
+
+        // the first two types are probably the only queries anyone would run via JPA
+        if (query instanceof EJBQLQuery) {
+            ((EJBQLQuery) query).setFetchOffset(startPosition);
+        }
+        else if (query instanceof SQLTemplate) {
+            ((SQLTemplate) query).setFetchOffset(startPosition);
+        }
+        else if (query instanceof SelectQuery) {
+            ((SelectQuery) query).setFetchOffset(startPosition);
+        }
+        else if (query instanceof ProcedureQuery) {
+            ((ProcedureQuery) query).setFetchOffset(startPosition);
+        }
+        else {
+            throw new IllegalArgumentException("query does not support maxResult: "
+                    + query);
+        }
+
+        return this;
     }
 
     /**

Modified: cayenne/main/trunk/itests/jpa-chapter3/src/test/java/org/apache/cayenne/jpa/itest/ch3/_3_6_1_QueryAPITest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/itests/jpa-chapter3/src/test/java/org/apache/cayenne/jpa/itest/ch3/_3_6_1_QueryAPITest.java?rev=676373&r1=676372&r2=676373&view=diff
==============================================================================
--- cayenne/main/trunk/itests/jpa-chapter3/src/test/java/org/apache/cayenne/jpa/itest/ch3/_3_6_1_QueryAPITest.java
(original)
+++ cayenne/main/trunk/itests/jpa-chapter3/src/test/java/org/apache/cayenne/jpa/itest/ch3/_3_6_1_QueryAPITest.java
Sun Jul 13 10:20:13 2008
@@ -27,6 +27,8 @@
 import org.apache.cayenne.itest.jpa.EntityManagerCase;
 import org.apache.cayenne.jpa.itest.ch3.entity.SimpleEntity;
 
+import sun.java2d.pipe.SpanShapeRenderer.Simple;
+
 public class _3_6_1_QueryAPITest extends EntityManagerCase {
 
     public void testGetResultListEntity() throws Exception {
@@ -113,4 +115,54 @@
             // expected
         }
     }
+
+    public void testSetMaxResults() throws Exception {
+        getTableHelper("SimpleEntity").deleteAll().setColumns("id", "property1").insert(
+                1,
+                "A").insert(2, "B").insert(3, "C").insert(4, "D");
+        
+        EntityManager em = getEntityManager();
+        Query query = em.createQuery("SELECT a FROM SimpleEntity a ORDER BY a.property1");
+        query.setMaxResults(2);
+        
+        List<?> results = query.getResultList();
+        assertEquals(2, results.size());
+        SimpleEntity e1 = (SimpleEntity) results.get(0);
+        SimpleEntity e2 = (SimpleEntity) results.get(1);
+        assertEquals("A", e1.getProperty1());
+        assertEquals("B", e2.getProperty1());
+    }
+    
+    public void testSetFirstResult() throws Exception {
+        getTableHelper("SimpleEntity").deleteAll().setColumns("id", "property1").insert(
+                1,
+                "A").insert(2, "B").insert(3, "C").insert(4, "D");
+        
+        EntityManager em = getEntityManager();
+        Query query = em.createQuery("SELECT a FROM SimpleEntity a ORDER BY a.property1");
+        query.setFirstResult(2);
+        
+        List<?> results = query.getResultList();
+        assertEquals(2, results.size());
+        SimpleEntity e1 = (SimpleEntity) results.get(0);
+        SimpleEntity e2 = (SimpleEntity) results.get(1);
+        assertEquals("C", e1.getProperty1());
+        assertEquals("D", e2.getProperty1());
+    }
+    
+    public void testSetFirstResultMaxResults() throws Exception {
+        getTableHelper("SimpleEntity").deleteAll().setColumns("id", "property1").insert(
+                1,
+                "A").insert(2, "B").insert(3, "C").insert(4, "D");
+        
+        EntityManager em = getEntityManager();
+        Query query = em.createQuery("SELECT a FROM SimpleEntity a ORDER BY a.property1");
+        query.setFirstResult(1);
+        query.setMaxResults(1);
+        
+        List<?> results = query.getResultList();
+        assertEquals(1, results.size());
+        SimpleEntity e1 = (SimpleEntity) results.get(0);
+        assertEquals("B", e1.getProperty1());
+    }
 }



Mime
View raw message