cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r561941 - in /cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src: main/java/org/apache/cayenne/access/jdbc/ main/java/org/apache/cayenne/ejbql/ main/java/org/apache/cayenne/ejbql/parser/ test/java/org/apache/cayenne/access/ test/re...
Date Wed, 01 Aug 2007 20:41:01 GMT
Author: aadamchik
Date: Wed Aug  1 13:40:56 2007
New Revision: 561941

URL: http://svn.apache.org/viewvc?view=rev&rev=561941
Log:
CAY-837 EJBQL Update Statement Support
(compound FK updates support)

Added:
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLUpdateItemTranslator.java
      - copied, changed from r561931, cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLUpdateFieldTranslator.java
Removed:
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLUpdateFieldTranslator.java
Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLConditionTranslator.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLUpdateTranslator.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLBaseVisitor.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLExpressionVisitor.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/EJBQLUpdateItem.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextEJBQLUpdateTest.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/resources/dml/access.DataContextEJBQLUpdateTest.xml

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLConditionTranslator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLConditionTranslator.java?view=diff&rev=561941&r1=561940&r2=561941
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLConditionTranslator.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLConditionTranslator.java
Wed Aug  1 13:40:56 2007
@@ -38,8 +38,7 @@
 class EJBQLConditionTranslator extends EJBQLBaseVisitor {
 
     protected EJBQLTranslationContext context;
-
-    private List multiColumnOperands;
+    protected List multiColumnOperands;
 
     EJBQLConditionTranslator(EJBQLTranslationContext context) {
         this.context = context;

Copied: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLUpdateItemTranslator.java
(from r561931, cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLUpdateFieldTranslator.java)
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLUpdateItemTranslator.java?view=diff&rev=561941&p1=cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLUpdateFieldTranslator.java&r1=561931&p2=cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLUpdateItemTranslator.java&r2=561941
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLUpdateFieldTranslator.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLUpdateItemTranslator.java
Wed Aug  1 13:40:56 2007
@@ -18,6 +18,8 @@
  ****************************************************************/
 package org.apache.cayenne.access.jdbc;
 
+import java.util.Iterator;
+
 import org.apache.cayenne.ejbql.EJBQLException;
 import org.apache.cayenne.ejbql.EJBQLExpression;
 
@@ -25,19 +27,64 @@
  * @since 3.0
  * @author Andrus Adamchik
  */
-class EJBQLUpdateFieldTranslator extends EJBQLConditionTranslator {
+class EJBQLUpdateItemTranslator extends EJBQLConditionTranslator {
 
-    public EJBQLUpdateFieldTranslator(EJBQLTranslationContext context) {
+    public EJBQLUpdateItemTranslator(EJBQLTranslationContext context) {
         super(context);
     }
 
+    public boolean visitPath(EJBQLExpression expression, int finishedChildIndex) {
+        // unexpected, but make sure super is not called....
+        return false;
+    }
+
+    public boolean visitUpdateItem(EJBQLExpression expression, int finishedChildIndex) {
+
+        if (finishedChildIndex == expression.getChildrenCount() - 1) {
+
+            // check multicolumn match condition and undo op insertion and append it
+            // from scratch if needed
+            if (multiColumnOperands != null) {
+
+                if (multiColumnOperands.size() != 2) {
+                    throw new EJBQLException(
+                            "Invalid multi-column equals expression. Expected 2 multi-column
operands, got "
+                                    + multiColumnOperands.size());
+                }
+
+                context.trim(2);
+
+                EJBQLMultiColumnOperand lhs = (EJBQLMultiColumnOperand) multiColumnOperands
+                        .get(0);
+                EJBQLMultiColumnOperand rhs = (EJBQLMultiColumnOperand) multiColumnOperands
+                        .get(1);
+
+                Iterator it = lhs.getKeys().iterator();
+                while (it.hasNext()) {
+                    Object key = it.next();
+
+                    lhs.appendValue(key);
+                    context.append(" =");
+                    rhs.appendValue(key);
+
+                    if (it.hasNext()) {
+                        context.append(',');
+                    }
+                }
+
+                multiColumnOperands = null;
+            }
+        }
+        
+        return true;
+    }
+
     public boolean visitUpdateField(EJBQLExpression expression, int finishedChildIndex) {
 
         EJBQLPathTranslator pathTranslator = new EJBQLPathTranslator(context) {
 
             protected void appendMultiColumnPath(EJBQLMultiColumnOperand operand) {
-                throw new EJBQLException(
-                        "Multi-column paths are not yet supported in UPDATEs");
+                EJBQLUpdateItemTranslator.this.addMultiColumnOperand(operand);
             }
 
             public boolean visitUpdateField(
@@ -51,8 +98,9 @@
         return false;
     }
 
-    public boolean visitUpdateValue(EJBQLExpression expression) {
+    public boolean visitEquals(EJBQLExpression expression, int finishedChildIndex) {
+        // unlike super, Equals here has no children and is itself a child of UpdateItem
         context.append(" =");
-        return true;
+        return false;
     }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLUpdateTranslator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLUpdateTranslator.java?view=diff&rev=561941&r1=561940&r2=561941
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLUpdateTranslator.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLUpdateTranslator.java
Wed Aug  1 13:40:56 2007
@@ -57,7 +57,7 @@
         return false;
     }
 
-    public boolean visitUpdateItem(EJBQLExpression expression) {
+    public boolean visitUpdateItem(EJBQLExpression expression, int finishedChildIndex) {
         if (itemCount++ > 0) {
             context.append(',');
         }
@@ -65,7 +65,7 @@
             context.append(" SET");
         }
 
-        expression.visit(new EJBQLUpdateFieldTranslator(context));
+        expression.visit(new EJBQLUpdateItemTranslator(context));
         return false;
     }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLBaseVisitor.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLBaseVisitor.java?view=diff&rev=561941&r1=561940&r2=561941
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLBaseVisitor.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLBaseVisitor.java
Wed Aug  1 13:40:56 2007
@@ -374,7 +374,7 @@
         return continueFlag;
     }
 
-    public boolean visitUpdateItem(EJBQLExpression expression) {
+    public boolean visitUpdateItem(EJBQLExpression expression, int finishedChildIndex) {
         return continueFlag;
     }
 

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLExpressionVisitor.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLExpressionVisitor.java?view=diff&rev=561941&r1=561940&r2=561941
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLExpressionVisitor.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/EJBQLExpressionVisitor.java
Wed Aug  1 13:40:56 2007
@@ -324,7 +324,7 @@
 
     boolean visitUpdateField(EJBQLExpression expression, int finishedChildIndex);
 
-    boolean visitUpdateItem(EJBQLExpression expression);
+    boolean visitUpdateItem(EJBQLExpression expression, int finishedChildIndex);
 
     boolean visitUpdateValue(EJBQLExpression expression);
 

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/EJBQLUpdateItem.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/EJBQLUpdateItem.java?view=diff&rev=561941&r1=561940&r2=561941
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/EJBQLUpdateItem.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/EJBQLUpdateItem.java
Wed Aug  1 13:40:56 2007
@@ -31,6 +31,11 @@
     }
 
     protected boolean visitNode(EJBQLExpressionVisitor visitor) {
-        return visitor.visitUpdateItem(this);
+        return visitor.visitUpdateItem(this, -1);
+    }
+
+    protected boolean visitChild(EJBQLExpressionVisitor visitor, int childIndex) {
+        return super.visitChild(visitor, childIndex)
+                && visitor.visitUpdateItem(this, childIndex);
     }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextEJBQLUpdateTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextEJBQLUpdateTest.java?view=diff&rev=561941&r1=561940&r2=561941
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextEJBQLUpdateTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextEJBQLUpdateTest.java
Wed Aug  1 13:40:56 2007
@@ -18,8 +18,12 @@
  ****************************************************************/
 package org.apache.cayenne.access;
 
+import java.util.HashMap;
+import java.util.Map;
+
 import org.apache.art.Artist;
 import org.apache.art.BooleanTestEntity;
+import org.apache.art.CompoundPkTestEntity;
 import org.apache.cayenne.DataObjectUtils;
 import org.apache.cayenne.ObjectContext;
 import org.apache.cayenne.QueryResponse;
@@ -186,6 +190,40 @@
         String ejbql = "UPDATE Painting AS p SET p.toArtist = :artist";
         EJBQLQuery query = new EJBQLQuery(ejbql);
         query.setParameter("artist", object);
+
+        QueryResponse result = context.performGenericQuery(query);
+
+        int[] count = result.firstUpdateCount();
+        assertNotNull(count);
+        assertEquals(1, count.length);
+        assertEquals(2, count[0]);
+
+        notUpdated = DataObjectUtils.objectForQuery(context, check);
+        assertEquals(new Long(0l), notUpdated);
+    }
+
+    public void testUpdateNoQualifierToOneCompoundPK() throws Exception {
+        createTestData("prepareCompound");
+
+        ObjectContext context = createDataContext();
+        Map key1 = new HashMap();
+        key1.put(CompoundPkTestEntity.KEY1_PK_COLUMN, "b1");
+        key1.put(CompoundPkTestEntity.KEY2_PK_COLUMN, "b2");
+        CompoundPkTestEntity object = (CompoundPkTestEntity) DataObjectUtils.objectForPK(
+                context,
+                CompoundPkTestEntity.class,
+                key1);
+
+        EJBQLQuery check = new EJBQLQuery(
+                "select count(e) from CompoundFkTestEntity e WHERE e.toCompoundPk <>
:param");
+        check.setParameter("param", object);
+
+        Object notUpdated = DataObjectUtils.objectForQuery(context, check);
+        assertEquals(new Long(1l), notUpdated);
+
+        String ejbql = "UPDATE CompoundFkTestEntity e SET e.toCompoundPk = :param";
+        EJBQLQuery query = new EJBQLQuery(ejbql);
+        query.setParameter("param", object);
 
         QueryResponse result = context.performGenericQuery(query);
 

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/resources/dml/access.DataContextEJBQLUpdateTest.xml
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/resources/dml/access.DataContextEJBQLUpdateTest.xml?view=diff&rev=561941&r1=561940&r2=561941
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/resources/dml/access.DataContextEJBQLUpdateTest.xml
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/resources/dml/access.DataContextEJBQLUpdateTest.xml
Wed Aug  1 13:40:56 2007
@@ -41,6 +41,34 @@
 		</value></constructor-arg>
 	</bean>
 	
+	<bean id="CP1" class="org.apache.cayenne.unit.util.UpdatingSQLTemplate">
+		<constructor-arg type="java.lang.Class"><value>org.apache.art.CompoundPkTestEntity</value></constructor-arg>
+		<constructor-arg><value>
+		insert into COMPOUND_PK_TEST (KEY1, KEY2) values ('a1', 'a2')
+		</value></constructor-arg>
+	</bean>
+	
+	<bean id="CP2" class="org.apache.cayenne.unit.util.UpdatingSQLTemplate">
+		<constructor-arg type="java.lang.Class"><value>org.apache.art.CompoundPkTestEntity</value></constructor-arg>
+		<constructor-arg><value>
+		insert into COMPOUND_PK_TEST (KEY1, KEY2) values ('b1', 'b2')
+		</value></constructor-arg>
+	</bean>
+	
+	<bean id="CF1" class="org.apache.cayenne.unit.util.UpdatingSQLTemplate">
+		<constructor-arg type="java.lang.Class"><value>org.apache.art.CompoundFkTestEntity</value></constructor-arg>
+		<constructor-arg><value>
+		insert into COMPOUND_FK_TEST (PKEY, F_KEY1, F_KEY2) values (33001, 'a1', 'a2')
+		</value></constructor-arg>
+	</bean>
+	
+	<bean id="CF2" class="org.apache.cayenne.unit.util.UpdatingSQLTemplate">
+		<constructor-arg type="java.lang.Class"><value>org.apache.art.CompoundFkTestEntity</value></constructor-arg>
+		<constructor-arg><value>
+		insert into COMPOUND_FK_TEST (PKEY, F_KEY1, F_KEY2) values (33002, 'b1', 'b2')
+		</value></constructor-arg>
+	</bean>
+	
 
 	<!-- ======================================= -->
 	<!-- Data Sets -->
@@ -58,5 +86,14 @@
 		</constructor-arg>
 	</bean>
 	
-	
+		<bean id="prepareCompound" class="java.util.ArrayList">
+		<constructor-arg>
+			<list>
+				<ref bean="CP1"/>
+				<ref bean="CP2"/>
+				<ref bean="CF1"/>
+				<ref bean="CF2"/>
+			</list>
+		</constructor-arg>
+	</bean>
 </beans>



Mime
View raw message