cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r1510306 - in /cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne: access/DataDomainDBDiffBuilder.java access/ObjectDiff.java map/DbRelationship.java
Date Sun, 04 Aug 2013 18:47:40 GMT
Author: aadamchik
Date: Sun Aug  4 18:47:39 2013
New Revision: 1510306

URL: http://svn.apache.org/r1510306
Log:
CAY-1861  Remove runtime relationships

tracking complimentary FK change for one-way to-many
(commit processing)

Modified:
    cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/DataDomainDBDiffBuilder.java
    cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/ObjectDiff.java
    cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/map/DbRelationship.java

Modified: cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/DataDomainDBDiffBuilder.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/DataDomainDBDiffBuilder.java?rev=1510306&r1=1510305&r2=1510306&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/DataDomainDBDiffBuilder.java
(original)
+++ cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/DataDomainDBDiffBuilder.java
Sun Aug  4 18:47:39 2013
@@ -21,9 +21,11 @@ package org.apache.cayenne.access;
 
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Map.Entry;
 
 import org.apache.cayenne.ObjectId;
 import org.apache.cayenne.access.DataDomainSyncBucket.PropagatedValueFactory;
+import org.apache.cayenne.exp.parser.ASTDbPath;
 import org.apache.cayenne.graph.GraphChangeHandler;
 import org.apache.cayenne.graph.GraphDiff;
 import org.apache.cayenne.map.DbAttribute;
@@ -109,10 +111,17 @@ class DataDomainDBDiffBuilder implements
     private void appendForeignKeys(Map<Object, Object> dbDiff) {
         // populate changed FKs
         if (currentArcDiff != null) {
-            for (final Map.Entry<Object, Object> entry : currentArcDiff.entrySet())
{
-                ObjRelationship relation = objEntity.getRelationship(entry.getKey().toString());
+            for (Entry<Object, Object> entry : currentArcDiff.entrySet()) {
 
-                DbRelationship dbRelation = relation.getDbRelationships().get(0);
+                DbRelationship dbRelation;
+
+                String arcIdString = entry.getKey().toString();
+                ObjRelationship relation = objEntity.getRelationship(arcIdString);
+                if (relation == null) {
+                    dbRelation = dbEntity.getRelationship(arcIdString.substring(ASTDbPath.DB_PREFIX.length()));
+                } else {
+                    dbRelation = relation.getDbRelationships().get(0);
+                }
 
                 ObjectId targetId = (ObjectId) entry.getValue();
                 for (DbJoin join : dbRelation.getJoins()) {
@@ -151,32 +160,67 @@ class DataDomainDBDiffBuilder implements
     }
 
     public void arcCreated(Object nodeId, Object targetNodeId, Object arcId) {
+        String arcIdString = arcId.toString();
+        ObjRelationship relationship = objEntity.getRelationship(arcIdString);
 
-        ObjRelationship relationship = objEntity.getRelationship(arcId.toString());
-        if (!relationship.isSourceIndependentFromTargetChange()) {
-            if (currentArcDiff == null) {
-                currentArcDiff = new HashMap<Object, Object>();
+        if (relationship == null) {
+            // phantom FK
+            if (arcIdString.startsWith(ASTDbPath.DB_PREFIX)) {
+
+                DbRelationship dbRelationship = dbEntity.getRelationship(arcIdString.substring(ASTDbPath.DB_PREFIX
+                        .length()));
+                if (!dbRelationship.isSourceIndependentFromTargetChange()) {
+                    doArcCreated(targetNodeId, arcId);
+                }
+            } else {
+                throw new IllegalArgumentException("Bad arcId: " + arcId);
             }
-            currentArcDiff.put(arcId, targetNodeId);
+
+        } else if (!relationship.isSourceIndependentFromTargetChange()) {
+            doArcCreated(targetNodeId, arcId);
         }
     }
 
+    private void doArcCreated(Object targetNodeId, Object arcId) {
+        if (currentArcDiff == null) {
+            currentArcDiff = new HashMap<Object, Object>();
+        }
+        currentArcDiff.put(arcId, targetNodeId);
+    }
+
     public void arcDeleted(Object nodeId, Object targetNodeId, Object arcId) {
 
-        ObjRelationship relationship = objEntity.getRelationship(arcId.toString());
-        if (!relationship.isSourceIndependentFromTargetChange()) {
+        String arcIdString = arcId.toString();
+        ObjRelationship relationship = objEntity.getRelationship(arcIdString);
 
-            if (currentArcDiff == null) {
-                currentArcDiff = new HashMap<Object, Object>();
-                currentArcDiff.put(arcId, null);
-            } else {
-                // skip deletion record if a substitute arc was created prior to
-                // deleting
-                // the old arc...
-                Object existingTargetId = currentArcDiff.get(arcId);
-                if (existingTargetId == null || targetNodeId.equals(existingTargetId)) {
-                    currentArcDiff.put(arcId, null);
+        if (relationship == null) {
+            // phantom FK
+            if (arcIdString.startsWith(ASTDbPath.DB_PREFIX)) {
+
+                DbRelationship dbRelationship = dbEntity.getRelationship(arcIdString.substring(ASTDbPath.DB_PREFIX
+                        .length()));
+                if (!dbRelationship.isSourceIndependentFromTargetChange()) {
+                    doArcDeleted(targetNodeId, arcId);
                 }
+            } else {
+                throw new IllegalArgumentException("Bad arcId: " + arcId);
+            }
+
+        } else if (!relationship.isSourceIndependentFromTargetChange()) {
+            doArcDeleted(targetNodeId, arcId);
+        }
+    }
+
+    private void doArcDeleted(Object targetNodeId, Object arcId) {
+        if (currentArcDiff == null) {
+            currentArcDiff = new HashMap<Object, Object>();
+            currentArcDiff.put(arcId, null);
+        } else {
+            // skip deletion record if a substitute arc was created prior to
+            // deleting the old arc...
+            Object existingTargetId = currentArcDiff.get(arcId);
+            if (existingTargetId == null || targetNodeId.equals(existingTargetId)) {
+                currentArcDiff.put(arcId, null);
             }
         }
     }

Modified: cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/ObjectDiff.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/ObjectDiff.java?rev=1510306&r1=1510305&r2=1510306&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/ObjectDiff.java
(original)
+++ cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/access/ObjectDiff.java
Sun Aug  4 18:47:39 2013
@@ -202,7 +202,8 @@ class ObjectDiff extends NodeDiff {
             // TODO: andrus, 3.22.2006 - should we consider this a bug?
 
             if (property == null && arcId.startsWith(ASTDbPath.DB_PREFIX)) {
-                addDiff = addPhantomFkDiff(arcDiff);
+                addPhantomFkDiff(arcDiff);
+                addDiff = false;
             } else if (property instanceof ToManyProperty) {
 
                 // record flattened op changes
@@ -257,7 +258,7 @@ class ObjectDiff extends NodeDiff {
         }
     }
 
-    private boolean addPhantomFkDiff(ArcOperation arcDiff) {
+    private void addPhantomFkDiff(ArcOperation arcDiff) {
         String arcId = arcDiff.getArcId().toString();
 
         DbEntity dbEntity = classDescriptor.getEntity().getDbEntity();
@@ -265,11 +266,9 @@ class ObjectDiff extends NodeDiff {
                 .length()));
 
         if (dbRelationship.isToMany()) {
-            return false;
+            return;
         }
 
-        boolean addDiff = true;
-
         if (currentArcSnapshot == null) {
             currentArcSnapshot = new HashMap<String, Object>();
         }
@@ -284,7 +283,6 @@ class ObjectDiff extends NodeDiff {
 
         // "delete" cancels "create" and vice versa...
         if (oldOp != null && oldOp.isDelete() != arcDiff.isDelete()) {
-            addDiff = false;
             phantomFks.remove(arcDiff);
 
             if (otherDiffs != null) {
@@ -292,7 +290,6 @@ class ObjectDiff extends NodeDiff {
             }
         }
 
-        return addDiff;
     }
 
     /**
@@ -380,6 +377,13 @@ class ObjectDiff extends NodeDiff {
             }
         }
 
+        // phantomFks are not in 'otherDiffs', while flattened diffs are
+        if (phantomFks != null) {
+            for (GraphDiff diff : phantomFks.keySet()) {
+                diff.apply(handler);
+            }
+        }
+
         applySimplePropertyChanges(handler);
     }
 

Modified: cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/map/DbRelationship.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/map/DbRelationship.java?rev=1510306&r1=1510305&r2=1510306&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/map/DbRelationship.java
(original)
+++ cayenne/main/trunk/framework/cayenne-core-unpublished/src/main/java/org/apache/cayenne/map/DbRelationship.java
Sun Aug  4 18:47:39 2013
@@ -275,6 +275,19 @@ public class DbRelationship extends Rela
         DbRelationship revRel = getReverseRelationship();
         return (revRel != null) ? revRel.isToDependentPK() : false;
     }
+    
+    /**
+     * Returns a boolean indicating whether modifying a target of such
+     * relationship in any way will not change the underlying table row of the
+     * source.
+     * 
+     * @since 3.2
+     */
+    public boolean isSourceIndependentFromTargetChange() {
+        // note - call "isToPK" at the end of the chain, since
+        // if it is to a dependent PK, we still should return true...
+        return isToMany() || isToDependentPK() || !isToPK();
+    }
 
     /**
      * Returns <code>true</code> if relationship from source to target points
to



Mime
View raw message