cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r569658 - 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/
Date Sat, 25 Aug 2007 11:45:30 GMT
Author: aadamchik
Date: Sat Aug 25 04:45:24 2007
New Revision: 569658

URL: http://svn.apache.org/viewvc?rev=569658&view=rev
Log:
EJBQL - fixing handling of implicit id variables

Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLFromTranslator.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/Compiler.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/EJBQLFromItem.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextEJBQLDeleteTest.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLFromTranslator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLFromTranslator.java?rev=569658&r1=569657&r2=569658&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLFromTranslator.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLFromTranslator.java
Sat Aug 25 04:45:24 2007
@@ -58,7 +58,7 @@
         return true;
     }
 
-    public boolean visitFromItem(EJBQLFromItem expression) {
+    public boolean visitFromItem(EJBQLFromItem expression, int finishedChildIndex) {
 
         String id = expression.getId();
 

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?rev=569658&r1=569657&r2=569658&view=diff
==============================================================================
--- 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
Sat Aug 25 04:45:24 2007
@@ -158,7 +158,7 @@
         return continueFlag;
     }
 
-    public boolean visitFromItem(EJBQLFromItem expression) {
+    public boolean visitFromItem(EJBQLFromItem 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?rev=569658&r1=569657&r2=569658&view=diff
==============================================================================
--- 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
Sat Aug 25 04:45:24 2007
@@ -133,7 +133,7 @@
 
     boolean visitFrom(EJBQLExpression expression, int finishedChildIndex);
 
-    boolean visitFromItem(EJBQLFromItem expression);
+    boolean visitFromItem(EJBQLFromItem expression, int finishedChildIndex);
 
     /**
      * Called on visiting ">=" expression and also after visiting every expression child.

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/Compiler.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/Compiler.java?rev=569658&r1=569657&r2=569658&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/Compiler.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/Compiler.java
Sat Aug 25 04:45:24 2007
@@ -154,7 +154,7 @@
             return false;
         }
 
-        public boolean visitFromItem(EJBQLFromItem expression) {
+        public boolean visitFromItem(EJBQLFromItem expression, int finishedChildIndex) {
             expression.visit(fromItemVisitor);
             return false;
         }
@@ -201,37 +201,45 @@
 
         private String entityName;
 
-        public boolean visitIdentificationVariable(EJBQLExpression expression) {
-            entityName = expression.getText();
-            return true;
-        }
-
-        public boolean visitIdentifier(EJBQLExpression expression) {
+        public boolean visitFromItem(EJBQLFromItem expression, int finishedChildIndex) {
 
-            // per JPA spec, 4.4.2, "Identification variables are case insensitive."
-            String rootId = normalizeIdPath(expression.getText());
+            if (finishedChildIndex + 1 == expression.getChildrenCount()) {
 
-            // resolve class descriptor
-            ClassDescriptor descriptor = resolver.getClassDescriptor(entityName);
-            if (descriptor == null) {
-                throw new EJBQLException("Unmapped abstract schema name: " + entityName);
-            }
+                // resolve class descriptor
+                ClassDescriptor descriptor = resolver.getClassDescriptor(entityName);
+                if (descriptor == null) {
+                    throw new EJBQLException("Unmapped abstract schema name: "
+                            + entityName);
+                }
+
+                // per JPA spec, 4.4.2, "Identification variables are case insensitive."
+                String id = normalizeIdPath(expression.getId());
+
+                ClassDescriptor old = (ClassDescriptor) descriptorsById.put(
+                        id,
+                        descriptor);
+                if (old != null && old != descriptor) {
+                    throw new EJBQLException(
+                            "Duplicate identification variable definition: "
+                                    + id
+                                    + ", it is already used for "
+                                    + old.getEntity().getName());
+                }
+
+                // if root wasn't detected in the Select Clause, use the first id var as
+                // root
+                if (Compiler.this.rootId == null) {
+                    Compiler.this.rootId = id;
+                }
 
-            ClassDescriptor old = (ClassDescriptor) descriptorsById.put(
-                    rootId,
-                    descriptor);
-            if (old != null && old != descriptor) {
-                throw new EJBQLException("Duplicate identification variable definition: "
-                        + rootId
-                        + ", it is already used for "
-                        + old.getEntity().getName());
+                this.entityName = null;
             }
 
-            // if root wasn't detected in the Select Clause, use the first id var as root
-            if (Compiler.this.rootId == null) {
-                Compiler.this.rootId = rootId;
-            }
+            return true;
+        }
 
+        public boolean visitIdentificationVariable(EJBQLExpression expression) {
+            entityName = expression.getText();
             return true;
         }
     }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/EJBQLFromItem.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/EJBQLFromItem.java?rev=569658&r1=569657&r2=569658&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/EJBQLFromItem.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/ejbql/parser/EJBQLFromItem.java
Sat Aug 25 04:45:24 2007
@@ -18,6 +18,8 @@
  ****************************************************************/
 package org.apache.cayenne.ejbql.parser;
 
+import org.apache.cayenne.ejbql.EJBQLBaseVisitor;
+import org.apache.cayenne.ejbql.EJBQLExpression;
 import org.apache.cayenne.ejbql.EJBQLExpressionVisitor;
 
 /**
@@ -30,16 +32,63 @@
         super(id);
     }
 
+    /**
+     * Returns an id generated from the entity name. It is used when no user-specified id
+     * exists.
+     */
+    public String getSyntheticId() {
+        int len = getChildrenCount();
+        if (len < 1) {
+            return null;
+        }
+
+        final String[] entityNames = new String[1];
+        getChild(0).visit(new EJBQLBaseVisitor() {
+
+            public boolean visitIdentificationVariable(EJBQLExpression expression) {
+                entityNames[0] = expression.getText();
+                return false;
+            }
+        });
+        
+        if (entityNames[0] == null) {
+            return null;
+        }
+
+        // id's are case insensitive, while entity names are. Using simple encoding to
+        // transform the entity name in such way that two entities that differ only in
+        // capitalization would produce different lowercase ids
+
+        StringBuffer id = new StringBuffer(entityNames[0].length() + 2);
+        for (int i = 0; i < entityNames[0].length(); i++) {
+            char c = entityNames[0].charAt(i);
+            if (Character.isUpperCase(c)) {
+                id.append('%').append(Character.toLowerCase(c));
+            }
+            else {
+                id.append(c);
+            }
+        }
+
+        return id.toString();
+
+    }
+
     public String getId() {
         int len = getChildrenCount();
         if (len < 2) {
-            return null;
+            return getSyntheticId();
         }
 
         return jjtGetChild(len - 1).getText();
     }
 
     protected boolean visitNode(EJBQLExpressionVisitor visitor) {
-        return visitor.visitFromItem(this);
+        return visitor.visitFromItem(this, -1);
+    }
+
+    protected boolean visitChild(EJBQLExpressionVisitor visitor, int childIndex) {
+        return super.visitChild(visitor, childIndex)
+                && visitor.visitFromItem(this, childIndex);
     }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextEJBQLDeleteTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextEJBQLDeleteTest.java?rev=569658&r1=569657&r2=569658&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextEJBQLDeleteTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DataContextEJBQLDeleteTest.java
Sat Aug 25 04:45:24 2007
@@ -29,6 +29,20 @@
     protected void setUp() throws Exception {
         deleteTestData();
     }
+    
+    public void testDeleteNoIdVar() throws Exception {
+        createTestData("prepare");
+
+        String ejbql = "delete from Painting";
+        EJBQLQuery query = new EJBQLQuery(ejbql);
+
+        QueryResponse result = createDataContext().performGenericQuery(query);
+
+        int[] count = result.firstUpdateCount();
+        assertNotNull(count);
+        assertEquals(1, count.length);
+        assertEquals(2, count[0]);
+    }
 
     public void testDeleteNoQualifier() throws Exception {
         createTestData("prepare");



Mime
View raw message