You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by aa...@apache.org on 2008/05/06 17:12:16 UTC

svn commit: r653800 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src: main/java/org/apache/cayenne/access/trans/ test/java/org/apache/cayenne/access/ test/java/org/apache/cayenne/access/trans/

Author: aadamchik
Date: Tue May  6 08:12:15 2008
New Revision: 653800

URL: http://svn.apache.org/viewvc?rev=653800&view=rev
Log:
CAY-802  Split Expressions
(works)

Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/DataObjectMatchTranslator.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/DeleteTranslator.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/QualifierTranslator.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/QueryAssembler.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/QueryAssemblerHelper.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/SelectTranslator.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/UpdateTranslator.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextJoinAliasesTest.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/trans/TstQueryAssembler.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/DataObjectMatchTranslator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/DataObjectMatchTranslator.java?rev=653800&r1=653799&r2=653800&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/DataObjectMatchTranslator.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/DataObjectMatchTranslator.java Tue May  6 08:12:15 2008
@@ -17,7 +17,6 @@
  *  under the License.
  ****************************************************************/
 
-
 package org.apache.cayenne.access.trans;
 
 import java.util.Collections;
@@ -38,11 +37,13 @@
  * @author Andrus Adamchik
  */
 public class DataObjectMatchTranslator {
+
     protected Map<String, DbAttribute> attributes;
     protected Map<String, Object> values;
     protected String operation;
     protected Expression expression;
     protected DbRelationship relationship;
+    protected String joinSplitAlias;
 
     public Expression getExpression() {
         return expression;
@@ -61,11 +62,13 @@
     }
 
     /**
-     * Initializes itself to do translation of the match ending 
-     * with a DbRelationship.
+     * Initializes itself to do translation of the match ending with a DbRelationship.
+     * 
+     * @since 3.0
      */
-    public void setRelationship(DbRelationship rel) {
+    public void setRelationship(DbRelationship rel, String joinSplitAlias) {
         this.relationship = rel;
+        this.joinSplitAlias = joinSplitAlias;
         attributes = new HashMap<String, DbAttribute>(rel.getJoins().size() * 2);
 
         if (rel.isToMany() || !rel.isToPK()) {
@@ -77,7 +80,8 @@
             for (DbAttribute pkAttr : ent.getPrimaryKeys()) {
                 attributes.put(pkAttr.getName(), pkAttr);
             }
-        } else {
+        }
+        else {
 
             // match on this FK
             for (DbJoin join : rel.getJoins()) {
@@ -92,10 +96,10 @@
             values = Collections.EMPTY_MAP;
             return;
         }
-        
+
         setObjectId(obj.getObjectId());
     }
-    
+
     /**
      * @since 1.2
      */
@@ -116,15 +120,22 @@
     public Iterator<String> keys() {
         if (attributes == null) {
             throw new IllegalStateException(
-                "An attempt to use uninitialized DataObjectMatchTranslator: "
-                    + "[attributes: null, values: "
-                    + values
-                    + "]");
+                    "An attempt to use uninitialized DataObjectMatchTranslator: "
+                            + "[attributes: null, values: "
+                            + values
+                            + "]");
         }
 
         return attributes.keySet().iterator();
     }
     
+    /**
+     * @since 3.0
+     */
+    public String getJoinSplitAlias() {
+        return joinSplitAlias;
+    }
+
     public DbRelationship getRelationship() {
         return relationship;
     }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/DeleteTranslator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/DeleteTranslator.java?rev=653800&r1=653799&r2=653800&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/DeleteTranslator.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/DeleteTranslator.java Tue May  6 08:12:15 2008
@@ -34,7 +34,10 @@
 public class DeleteTranslator extends QueryAssembler {
 
     @Override
-    public void dbRelationshipAdded(DbRelationship relationship, JoinType joinType) {
+    public void dbRelationshipAdded(
+            DbRelationship relationship,
+            JoinType joinType,
+            String joinAplitAlias) {
         throw new UnsupportedOperationException("db relationships not supported");
     }
 

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/QualifierTranslator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/QualifierTranslator.java?rev=653800&r1=653799&r2=653800&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/QualifierTranslator.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/QualifierTranslator.java Tue May  6 08:12:15 2008
@@ -138,7 +138,10 @@
 
         DbRelationship relationship = objectMatchTranslator.getRelationship();
         if (!relationship.isToMany() && !relationship.isToPK()) {
-            queryAssembler.dbRelationshipAdded(relationship, JoinType.INNER);
+            queryAssembler.dbRelationshipAdded(
+                    relationship,
+                    JoinType.INNER,
+                    objectMatchTranslator.getJoinSplitAlias());
         }
 
         Iterator<String> it = objectMatchTranslator.keys();
@@ -422,18 +425,20 @@
     }
 
     @Override
-    protected void processRelTermination(DbRelationship rel, JoinType joinType)
-            throws IOException {
+    protected void processRelTermination(
+            DbRelationship rel,
+            JoinType joinType,
+            String joinSplitAlias) throws IOException {
 
         if (!matchingObject) {
-            super.processRelTermination(rel, joinType);
+            super.processRelTermination(rel, joinType, joinSplitAlias);
         }
         else {
             if (rel.isToMany()) {
                 // append joins
-                queryAssembler.dbRelationshipAdded(rel, joinType);
+                queryAssembler.dbRelationshipAdded(rel, joinType, joinSplitAlias);
             }
-            objectMatchTranslator.setRelationship(rel);
+            objectMatchTranslator.setRelationship(rel, joinSplitAlias);
         }
     }
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/QueryAssembler.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/QueryAssembler.java?rev=653800&r1=653799&r2=653800&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/QueryAssembler.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/QueryAssembler.java Tue May  6 08:12:15 2008
@@ -81,7 +81,8 @@
      */
     public abstract void dbRelationshipAdded(
             DbRelationship relationship,
-            JoinType joinType);
+            JoinType joinType,
+            String joinSplitAlias);
 
     /**
      * Translates query into sql string. This is a workhorse method of QueryAssembler. It

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/QueryAssemblerHelper.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/QueryAssemblerHelper.java?rev=653800&r1=653799&r2=653800&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/QueryAssemblerHelper.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/QueryAssemblerHelper.java Tue May  6 08:12:15 2008
@@ -66,12 +66,12 @@
     public DbEntity getDbEntity() {
         return queryAssembler.getRootDbEntity();
     }
-    
+
     public void appendPart(Appendable out) throws IOException {
         this.out = out;
         doAppendPart();
     }
-    
+
     protected abstract void doAppendPart() throws IOException;
 
     /**
@@ -80,10 +80,42 @@
     protected void appendObjPath(Expression pathExp) throws IOException {
 
         queryAssembler.resetJoinStack();
+        String joinSplitAlias = null;
 
         for (PathComponent<ObjAttribute, ObjRelationship> component : getObjEntity()
                 .resolvePath(pathExp, queryAssembler.getPathAliases())) {
 
+            if (component.isAlias()) {
+                joinSplitAlias = component.getName();
+                for (PathComponent<ObjAttribute, ObjRelationship> aliasPart : component
+                        .getAliasedPath()) {
+
+                    ObjRelationship relationship = aliasPart.getRelationship();
+
+                    if (relationship == null) {
+                        throw new IllegalStateException(
+                                "Non-relationship aliased path part: "
+                                        + aliasPart.getName());
+                    }
+
+                    if (aliasPart.isLast() && component.isLast()) {
+                        processRelTermination(
+                                relationship,
+                                aliasPart.getJoinType(),
+                                joinSplitAlias);
+                    }
+                    else {
+                        // find and add joins ....
+                        for (DbRelationship dbRel : relationship.getDbRelationships()) {
+                            queryAssembler.dbRelationshipAdded(dbRel, aliasPart
+                                    .getJoinType(), joinSplitAlias);
+                        }
+                    }
+                }
+
+                continue;
+            }
+
             ObjRelationship relationship = component.getRelationship();
             ObjAttribute attribute = component.getAttribute();
 
@@ -92,13 +124,18 @@
                 // if this is a last relationship in the path,
                 // it needs special handling
                 if (component.isLast()) {
-                    processRelTermination(relationship, component.getJoinType());
+                    processRelTermination(
+                            relationship,
+                            component.getJoinType(),
+                            joinSplitAlias);
                 }
                 else {
                     // find and add joins ....
                     for (DbRelationship dbRel : relationship.getDbRelationships()) {
-                        queryAssembler
-                                .dbRelationshipAdded(dbRel, component.getJoinType());
+                        queryAssembler.dbRelationshipAdded(
+                                dbRel,
+                                component.getJoinType(),
+                                joinSplitAlias);
                     }
                 }
             }
@@ -114,7 +151,8 @@
                     else if (pathPart instanceof DbRelationship) {
                         queryAssembler.dbRelationshipAdded(
                                 (DbRelationship) pathPart,
-                                JoinType.INNER);
+                                JoinType.INNER,
+                                joinSplitAlias);
                     }
                     else if (pathPart instanceof DbAttribute) {
                         processColumn((DbAttribute) pathPart);
@@ -128,10 +166,39 @@
     protected void appendDbPath(Expression pathExp) throws IOException {
 
         queryAssembler.resetJoinStack();
+        String joinSplitAlias = null;
 
         for (PathComponent<DbAttribute, DbRelationship> component : getDbEntity()
                 .resolvePath(pathExp, queryAssembler.getPathAliases())) {
 
+            if (component.isAlias()) {
+                joinSplitAlias = component.getName();
+                for (PathComponent<DbAttribute, DbRelationship> aliasPart : component
+                        .getAliasedPath()) {
+
+                    DbRelationship relationship = aliasPart.getRelationship();
+
+                    if (relationship == null) {
+                        throw new IllegalStateException(
+                                "Non-relationship aliased path part: "
+                                        + aliasPart.getName());
+                    }
+
+                    if (aliasPart.isLast() && component.isLast()) {
+                        processRelTermination(
+                                relationship,
+                                aliasPart.getJoinType(),
+                                joinSplitAlias);
+                    }
+                    else {
+                        queryAssembler.dbRelationshipAdded(relationship, component
+                                .getJoinType(), joinSplitAlias);
+                    }
+                }
+
+                continue;
+            }
+
             DbRelationship relationship = component.getRelationship();
 
             if (relationship != null) {
@@ -139,12 +206,15 @@
                 // if this is a last relationship in the path,
                 // it needs special handling
                 if (component.isLast()) {
-                    processRelTermination(relationship, component.getJoinType());
+                    processRelTermination(
+                            relationship,
+                            component.getJoinType(),
+                            joinSplitAlias);
                 }
                 else {
                     // find and add joins ....
                     queryAssembler.dbRelationshipAdded(relationship, component
-                            .getJoinType());
+                            .getJoinType(), joinSplitAlias);
                 }
             }
             else {
@@ -340,8 +410,10 @@
      * 
      * @since 3.0
      */
-    protected void processRelTermination(ObjRelationship rel, JoinType joinType)
-            throws IOException {
+    protected void processRelTermination(
+            ObjRelationship rel,
+            JoinType joinType,
+            String joinSplitAlias) throws IOException {
 
         Iterator<DbRelationship> dbRels = rel.getDbRelationships().iterator();
 
@@ -352,11 +424,11 @@
             // if this is a last relationship in the path,
             // it needs special handling
             if (!dbRels.hasNext()) {
-                processRelTermination(dbRel, joinType);
+                processRelTermination(dbRel, joinType, joinSplitAlias);
             }
             else {
                 // find and add joins ....
-                queryAssembler.dbRelationshipAdded(dbRel, joinType);
+                queryAssembler.dbRelationshipAdded(dbRel, joinType, joinSplitAlias);
             }
         }
     }
@@ -369,11 +441,14 @@
      * 
      * @since 3.0
      */
-    protected void processRelTermination(DbRelationship rel, JoinType joinType)
-            throws IOException {
+    protected void processRelTermination(
+            DbRelationship rel,
+            JoinType joinType,
+            String joinSplitAlias) throws IOException {
+
         if (rel.isToMany()) {
             // append joins
-            queryAssembler.dbRelationshipAdded(rel, joinType);
+            queryAssembler.dbRelationshipAdded(rel, joinType, joinSplitAlias);
         }
 
         // get last DbRelationship on the list

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/SelectTranslator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/SelectTranslator.java?rev=653800&r1=653799&r2=653800&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/SelectTranslator.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/SelectTranslator.java Tue May  6 08:12:15 2008
@@ -282,7 +282,7 @@
                     }
                     else if (pathPart instanceof DbRelationship) {
                         DbRelationship rel = (DbRelationship) pathPart;
-                        dbRelationshipAdded(rel, JoinType.INNER);
+                        dbRelationshipAdded(rel, JoinType.INNER, null);
                     }
                     else if (pathPart instanceof DbAttribute) {
                         DbAttribute dbAttr = (DbAttribute) pathPart;
@@ -346,6 +346,7 @@
                 // add joins and find terminating element
 
                 resetJoinStack();
+
                 PathComponent<DbAttribute, DbRelationship> lastComponent = null;
                 for (PathComponent<DbAttribute, DbRelationship> component : table
                         .resolvePath(pathExp, getPathAliases())) {
@@ -353,7 +354,7 @@
                     // do not add join for the last DB Rel
                     if (component.getRelationship() != null && !component.isLast()) {
                         dbRelationshipAdded(component.getRelationship(), component
-                                .getJoinType());
+                                .getJoinType(), null);
                     }
 
                     lastComponent = component;
@@ -370,7 +371,7 @@
 
                         // add last join
                         if (relationship.isToMany()) {
-                            dbRelationshipAdded(relationship, JoinType.INNER);
+                            dbRelationshipAdded(relationship, JoinType.INNER, null);
                         }
 
                         for (DbJoin j : relationship.getJoins()) {
@@ -418,7 +419,7 @@
                 for (PathComponent<DbAttribute, DbRelationship> component : table
                         .resolvePath(dbPrefetch, getPathAliases())) {
                     r = component.getRelationship();
-                    dbRelationshipAdded(r, JoinType.INNER);
+                    dbRelationshipAdded(r, JoinType.INNER, null);
                 }
 
                 if (r == null) {
@@ -462,7 +463,7 @@
 
                         else if (pathPart instanceof DbRelationship) {
                             DbRelationship rel = (DbRelationship) pathPart;
-                            dbRelationshipAdded(rel, JoinType.INNER);
+                            dbRelationshipAdded(rel, JoinType.INNER, null);
                         }
                         else if (pathPart instanceof DbAttribute) {
                             DbAttribute attribute = (DbAttribute) pathPart;
@@ -580,18 +581,18 @@
     }
 
     /**
-     * Stores a new relationship in an internal list. Later it will be used to create
-     * joins to relationship destination table.
-     * 
      * @since 3.0
      */
     @Override
-    public void dbRelationshipAdded(DbRelationship relationship, JoinType joinType) {
+    public void dbRelationshipAdded(
+            DbRelationship relationship,
+            JoinType joinType,
+            String joinSplitAlias) {
         if (relationship.isToMany()) {
             forcingDistinct = true;
         }
 
-        joinStack.pushJoin(relationship, joinType, null);
+        joinStack.pushJoin(relationship, joinType, joinSplitAlias);
     }
 
     /**

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/UpdateTranslator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/UpdateTranslator.java?rev=653800&r1=653799&r2=653800&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/UpdateTranslator.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/trans/UpdateTranslator.java Tue May  6 08:12:15 2008
@@ -39,10 +39,13 @@
 public class UpdateTranslator extends QueryAssembler {
 
     @Override
-    public void dbRelationshipAdded(DbRelationship relationship, JoinType joinType) {
+    public void dbRelationshipAdded(
+            DbRelationship relationship,
+            JoinType joinType,
+            String joinSplitAlias) {
         throw new UnsupportedOperationException("db relationships not supported");
     }
-    
+
     @Override
     public String getCurrentAlias() {
         throw new UnsupportedOperationException("aliases not supported");

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextJoinAliasesTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextJoinAliasesTest.java?rev=653800&r1=653799&r2=653800&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextJoinAliasesTest.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/DataContextJoinAliasesTest.java Tue May  6 08:12:15 2008
@@ -20,6 +20,7 @@
 
 import java.util.Date;
 import java.util.HashMap;
+import java.util.List;
 import java.util.Map;
 
 import org.apache.art.Artist;
@@ -60,10 +61,10 @@
         query.andQualifier(ExpressionFactory.matchExp("p.toArtist", picasso));
         query.andQualifier(ExpressionFactory.matchExp("d.toArtist", dali));
 
-        // List<Gallery> galleries = context.performQuery(query);
-        //
-        // assertEquals(1, galleries.size());
-        // assertEquals("G1", galleries.get(0).getGalleryName());
+        List<Gallery> galleries = context.performQuery(query);
+
+        assertEquals(1, galleries.size());
+        assertEquals("G1", galleries.get(0).getGalleryName());
     }
 
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/trans/TstQueryAssembler.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/trans/TstQueryAssembler.java?rev=653800&r1=653799&r2=653800&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/trans/TstQueryAssembler.java (original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/access/trans/TstQueryAssembler.java Tue May  6 08:12:15 2008
@@ -52,10 +52,13 @@
     }
 
     @Override
-    public void dbRelationshipAdded(DbRelationship relationship, JoinType joinType) {
+    public void dbRelationshipAdded(
+            DbRelationship relationship,
+            JoinType joinType,
+            String joinSplitAlias) {
         dbRels.add(relationship);
     }
-    
+
     @Override
     public String getCurrentAlias() {
         return "ta";