cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r562270 - in /cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src: main/java/org/apache/cayenne/access/jdbc/ test/java/org/apache/cayenne/access/ test/resources/dml/
Date Thu, 02 Aug 2007 21:43:24 GMT
Author: aadamchik
Date: Thu Aug  2 14:43:23 2007
New Revision: 562270

URL: http://svn.apache.org/viewvc?view=rev&rev=562270
Log:
CAY-838 EJBQL GROUP BY and HAVING support
basic GROUP BY support

Added:
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLGroupByTranslator.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextEJBQLGroupByHavingTest.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/resources/dml/access.DataContextEJBQLGroupByHavingTest.xml
Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLSelectTranslator.java

Added: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLGroupByTranslator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLGroupByTranslator.java?view=auto&rev=562270
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLGroupByTranslator.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLGroupByTranslator.java
Thu Aug  2 14:43:23 2007
@@ -0,0 +1,54 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+package org.apache.cayenne.access.jdbc;
+
+import org.apache.cayenne.ejbql.EJBQLBaseVisitor;
+import org.apache.cayenne.ejbql.EJBQLException;
+import org.apache.cayenne.ejbql.EJBQLExpression;
+import org.apache.cayenne.ejbql.EJBQLExpressionVisitor;
+
+/**
+ * @since 3.0
+ * @author Andrus Adamchik
+ */
+class EJBQLGroupByTranslator extends EJBQLBaseVisitor {
+
+    private EJBQLTranslationContext context;
+    private int itemCount;
+
+    EJBQLGroupByTranslator(EJBQLTranslationContext context) {
+        this.context = context;
+    }
+
+    public boolean visitPath(EJBQLExpression expression, int finishedChildIndex) {
+
+        if (itemCount++ > 0) {
+            context.append(',');
+        }
+
+        EJBQLExpressionVisitor childVisitor = new EJBQLPathTranslator(context) {
+
+            protected void appendMultiColumnPath(EJBQLMultiColumnOperand operand) {
+                throw new EJBQLException("Can't GROUP BY on multi-column paths or objects");
+            }
+        };
+        expression.visit(childVisitor);
+        return false;
+    }
+}

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLSelectTranslator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLSelectTranslator.java?view=diff&rev=562270&r1=562269&r2=562270
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLSelectTranslator.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLSelectTranslator.java
Thu Aug  2 14:43:23 2007
@@ -50,6 +50,12 @@
         return false;
     }
 
+    public boolean visitGroupBy(EJBQLExpression expression) {
+        context.append(" GROUP BY");
+        expression.visit(new EJBQLGroupByTranslator(context));
+        return false;
+    }
+
     public boolean visitOrderBy(EJBQLExpression expression) {
         context.append(" ORDER BY");
         expression.visit(new EJBQLOrderByTranslator(context));

Added: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextEJBQLGroupByHavingTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextEJBQLGroupByHavingTest.java?view=auto&rev=562270
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextEJBQLGroupByHavingTest.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextEJBQLGroupByHavingTest.java
Thu Aug  2 14:43:23 2007
@@ -0,0 +1,81 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+package org.apache.cayenne.access;
+
+import java.math.BigDecimal;
+import java.util.List;
+
+import org.apache.cayenne.query.EJBQLQuery;
+import org.apache.cayenne.unit.CayenneCase;
+
+public class DataContextEJBQLGroupByHavingTest extends CayenneCase {
+    
+    protected void setUp() throws Exception {
+        deleteTestData();
+    }
+
+    public void testGroupBy() throws Exception {
+        createTestData("prepare");
+
+        String ejbql = "SELECT p.estimatedPrice, count(p) FROM Painting p"
+                + " GROUP BY p.estimatedPrice"
+                + " ORDER BY p.estimatedPrice";
+        EJBQLQuery query = new EJBQLQuery(ejbql);
+
+        List data = createDataContext().performQuery(query);
+        assertEquals(2, data.size());
+        assertTrue(data.get(0) instanceof Object[]);
+
+        Object[] row0 = (Object[]) data.get(0);
+        assertEquals(new BigDecimal(1), row0[0]);
+        assertEquals(new Long(3), row0[1]);
+        
+        Object[] row1 = (Object[]) data.get(1);
+        assertEquals(new BigDecimal(2), row1[0]);
+        assertEquals(new Long(2), row1[1]);
+    }
+    
+    public void testGroupByMultipleItems() throws Exception {
+        createTestData("prepare");
+
+        String ejbql = "SELECT p.estimatedPrice, p.paintingTitle, count(p) FROM Painting
p"
+                + " GROUP BY p.estimatedPrice, p.paintingTitle"
+                + " ORDER BY p.estimatedPrice, p.paintingTitle";
+        EJBQLQuery query = new EJBQLQuery(ejbql);
+
+        List data = createDataContext().performQuery(query);
+        assertEquals(3, data.size());
+        assertTrue(data.get(0) instanceof Object[]);
+
+        Object[] row0 = (Object[]) data.get(0);
+        assertEquals(new BigDecimal(1), row0[0]);
+        assertEquals("PX", row0[1]);
+        assertEquals(new Long(1), row0[2]);
+        
+        Object[] row1 = (Object[]) data.get(1);
+        assertEquals(new BigDecimal(1), row1[0]);
+        assertEquals("PZ", row1[1]);
+        assertEquals(new Long(2), row1[2]);
+        
+        Object[] row2 = (Object[]) data.get(2);
+        assertEquals(new BigDecimal(2), row2[0]);
+        assertEquals("PY", row2[1]);
+        assertEquals(new Long(2), row2[2]);
+    }
+}

Added: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/resources/dml/access.DataContextEJBQLGroupByHavingTest.xml
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/resources/dml/access.DataContextEJBQLGroupByHavingTest.xml?view=auto&rev=562270
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/resources/dml/access.DataContextEJBQLGroupByHavingTest.xml
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/resources/dml/access.DataContextEJBQLGroupByHavingTest.xml
Thu Aug  2 14:43:23 2007
@@ -0,0 +1,59 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
+
+<beans default-lazy-init="true">	
+	<!-- ======================================= -->
+	<!-- Named Queries -->
+	<!-- ======================================= -->
+	
+	<bean id="P1" class="org.apache.cayenne.unit.util.UpdatingSQLTemplate">
+		<constructor-arg type="java.lang.Class"><value>org.apache.art.Painting</value></constructor-arg>
+		<constructor-arg><value>
+		INSERT INTO PAINTING (PAINTING_ID, PAINTING_TITLE, ESTIMATED_PRICE) VALUES (33001, 'PX',
1)
+		</value></constructor-arg>
+	</bean>
+	
+	<bean id="P2" class="org.apache.cayenne.unit.util.UpdatingSQLTemplate">
+		<constructor-arg type="java.lang.Class"><value>org.apache.art.Painting</value></constructor-arg>
+		<constructor-arg><value>
+		INSERT INTO PAINTING (PAINTING_ID, PAINTING_TITLE, ESTIMATED_PRICE) VALUES (33002, 'PY',
2)
+		</value></constructor-arg>
+	</bean>
+	
+	<bean id="P3" class="org.apache.cayenne.unit.util.UpdatingSQLTemplate">
+		<constructor-arg type="java.lang.Class"><value>org.apache.art.Painting</value></constructor-arg>
+		<constructor-arg><value>
+		INSERT INTO PAINTING (PAINTING_ID, PAINTING_TITLE, ESTIMATED_PRICE) VALUES (33003, 'PY',
2)
+		</value></constructor-arg>
+	</bean>
+	
+	<bean id="P4" class="org.apache.cayenne.unit.util.UpdatingSQLTemplate">
+		<constructor-arg type="java.lang.Class"><value>org.apache.art.Painting</value></constructor-arg>
+		<constructor-arg><value>
+		INSERT INTO PAINTING (PAINTING_ID, PAINTING_TITLE, ESTIMATED_PRICE) VALUES (33004, 'PZ',
1)
+		</value></constructor-arg>
+	</bean>
+	
+	<bean id="P5" class="org.apache.cayenne.unit.util.UpdatingSQLTemplate">
+		<constructor-arg type="java.lang.Class"><value>org.apache.art.Painting</value></constructor-arg>
+		<constructor-arg><value>
+		INSERT INTO PAINTING (PAINTING_ID, PAINTING_TITLE, ESTIMATED_PRICE) VALUES (33005, 'PZ',
1)
+		</value></constructor-arg>
+	</bean>
+
+	<!-- ======================================= -->
+	<!-- Data Sets -->
+	<!-- ======================================= -->	
+	
+	<bean id="prepare" class="java.util.ArrayList">
+		<constructor-arg>
+			<list>
+				<ref bean="P1"/>
+				<ref bean="P2"/>
+				<ref bean="P3"/>
+				<ref bean="P4"/>
+				<ref bean="P5"/>
+			</list>
+		</constructor-arg>
+	</bean>
+</beans>
\ No newline at end of file



Mime
View raw message