cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r1150305 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc: EJBQLConditionTranslator.java EJBQLJoinAppender.java EJBQLTranslationContext.java
Date Sun, 24 Jul 2011 09:39:57 GMT
Author: aadamchik
Date: Sun Jul 24 09:39:56 2011
New Revision: 1150305

URL: http://svn.apache.org/viewvc?rev=1150305&view=rev
Log:
CAY-1582 EJBQL queries are broken for vertical inheritance entities

patch by Victor Antonovich : vertical_inheritance_ejbql_fix_2.patch , 07.04.2011

Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLConditionTranslator.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLJoinAppender.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLTranslationContext.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLConditionTranslator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLConditionTranslator.java?rev=1150305&r1=1150304&r2=1150305&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLConditionTranslator.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLConditionTranslator.java
Sun Jul 24 09:39:56 2011
@@ -639,14 +639,24 @@ public class EJBQLConditionTranslator ex
     @Override
     public boolean visitIdentificationVariable(EJBQLExpression expression) {
         // this is a match on a variable, like "x = :x"
-
+        DbEntity table;
+        // first looking for joined entity
+        List<DbRelationship> joinRelationships = context.getIncomingRelationships(
+                new EJBQLTableId(expression.getText()));
+        if (!joinRelationships.isEmpty()) {
+            // joined entity, get last joined table as entity table
+            DbRelationship incoming = joinRelationships.get(joinRelationships.size() - 1);
+            table = (DbEntity) incoming.getTargetEntity();
+        } else {
+            // not joined entity, get entity table
         ClassDescriptor descriptor = context.getEntityDescriptor(expression.getText());
         if (descriptor == null) {
             throw new EJBQLException("Invalid identification variable: "
                     + expression.getText());
         }
+            table = descriptor.getEntity().getDbEntity();
+        }
 
-        DbEntity table = descriptor.getEntity().getDbEntity();
         String alias = context.getTableAlias(expression.getText(), table
                 .getFullyQualifiedName());
 

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLJoinAppender.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLJoinAppender.java?rev=1150305&r1=1150304&r2=1150305&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLJoinAppender.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLJoinAppender.java
Sun Jul 24 09:39:56 2011
@@ -164,7 +164,7 @@ public class EJBQLJoinAppender {
 
                     }
 
-                    context.append(" JOIN ");
+                    context.append(' ').append(semantics).append(' ');
                     context.append(subqueryTargetTableName).append(' ').append(
                             subqueryTargetAlias);
                     generateJoiningExpression(

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLTranslationContext.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLTranslationContext.java?rev=1150305&r1=1150304&r2=1150305&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLTranslationContext.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/access/jdbc/EJBQLTranslationContext.java
Sun Jul 24 09:39:56 2011
@@ -19,6 +19,7 @@
 package org.apache.cayenne.access.jdbc;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
@@ -29,12 +30,15 @@ import org.apache.cayenne.ejbql.EJBQLExc
 import org.apache.cayenne.map.DbEntity;
 import org.apache.cayenne.map.DbRelationship;
 import org.apache.cayenne.map.EntityResolver;
+import org.apache.cayenne.map.ObjAttribute;
+import org.apache.cayenne.map.ObjEntity;
 import org.apache.cayenne.query.EJBQLQuery;
 import org.apache.cayenne.query.EntityResultSegment;
 import org.apache.cayenne.query.QueryMetadata;
 import org.apache.cayenne.query.SQLTemplate;
 import org.apache.cayenne.query.ScalarResultSegment;
 import org.apache.cayenne.reflect.ClassDescriptor;
+import org.apache.cayenne.util.CayenneMapEntry;
 
 /**
  * A context used for translating of EJBQL to SQL.
@@ -142,9 +146,9 @@ public class EJBQLTranslationContext {
     }
 
     List<DbRelationship> getIncomingRelationships(EJBQLTableId id) {
-
+        String resolvedEntityId = resolveId(id.getEntityId());
         List<DbRelationship> incoming = compiledExpression
-                .getIncomingRelationships(resolveId(id.getEntityId()));
+                .getIncomingRelationships(resolvedEntityId);
 
         // append tail of flattened relationships...
         if (id.getDbPath() != null) {
@@ -168,6 +172,39 @@ public class EJBQLTranslationContext {
             while (it.hasNext()) {
                 incoming.add((DbRelationship) it.next());
             }
+        } else {
+            // Check for vertical inheritance relationships
+            ClassDescriptor descriptor = compiledExpression.getEntityDescriptor(
+                    resolvedEntityId);
+            ObjEntity entity = descriptor.getEntity();
+            Collection<ObjAttribute> entityAttributes = entity.getDeclaredAttributes();
+            // Iterate over entity declared attributes, check for flattened attributes
+            for (ObjAttribute entityAttribute : entityAttributes) {
+                Iterator<CayenneMapEntry> entityAttributeDbPathIterator =
+                    entityAttribute.getDbPathIterator();
+                ArrayList<DbRelationship> entityAttributeDbPathRelationships =
+                    new ArrayList<DbRelationship>();
+                // Collect DB path relationships into list
+                while (entityAttributeDbPathIterator.hasNext()) {
+                    Object entityAttributeDbObject = entityAttributeDbPathIterator.next();
+                    if (entityAttributeDbObject instanceof DbRelationship) {
+                        entityAttributeDbPathRelationships.add((DbRelationship)
+                                entityAttributeDbObject);
+                    }
+                }
+                // Check attribute last DB relationship target entity
+                if (entityAttributeDbPathRelationships.size() > 0 &&
+                        !entityAttributeDbPathRelationships.get(
+                                entityAttributeDbPathRelationships.size() - 1)
+                                .getTargetEntityName().equals(id.getEntityId())) {
+                    // Entity attribute last DB relationship target is flattened vertical
+                    // inheritance attribute, so add its relationships to incoming
+                    // relationships
+                    incoming = new ArrayList<DbRelationship>(incoming);
+                    incoming.addAll(entityAttributeDbPathRelationships);
+                    break;
+                }
+            }
         }
 
         return incoming;
@@ -346,7 +383,6 @@ public class EJBQLTranslationContext {
      * such alias hasn't been used, it is created on the fly.
      */
     protected String getTableAlias(String idPath, String tableName) {
-
         if (!isUsingAliases()) {
             return tableName;
         }



Mime
View raw message