cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From torehal...@apache.org
Subject svn commit: r812516 - in /cayenne/main/trunk: docs/doc/src/main/resources/ framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/postgres/ framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/ framework/cayenn...
Date Tue, 08 Sep 2009 14:05:54 GMT
Author: torehalset
Date: Tue Sep  8 14:05:53 2009
New Revision: 812516

URL: http://svn.apache.org/viewvc?rev=812516&view=rev
Log:
CAY-1270: merge primary key changes
tested on PostgreSQL, hsqldb and Derby

Added:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/SetPrimaryKeyToDb.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/SetPrimaryKeyToModel.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/SetPrimaryKeyToDbTest.java
Modified:
    cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/postgres/PostgresMergerFactory.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DbMerger.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/MergerFactory.java

Modified: cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt?rev=812516&r1=812515&r2=812516&view=diff
==============================================================================
--- cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt (original)
+++ cayenne/main/trunk/docs/doc/src/main/resources/RELEASE-NOTES.txt Tue Sep  8 14:05:53 2009
@@ -26,6 +26,7 @@
 CAY-1254 filtering an inExp(a, b) does not work when the path (a) refers to a collection
of objects
 CAY-1261 Auto-inject values in NEW objects with declared qualifiers
 CAY-1268 Implement 'setStatementFetchSize' for RelationshipQuery
+CAY-1270 Merge primary key changes
 
 Bug Fixes Since M6:
 

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/postgres/PostgresMergerFactory.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/postgres/PostgresMergerFactory.java?rev=812516&r1=812515&r2=812516&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/postgres/PostgresMergerFactory.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/dba/postgres/PostgresMergerFactory.java
Tue Sep  8 14:05:53 2009
@@ -18,12 +18,16 @@
  ****************************************************************/
 package org.apache.cayenne.dba.postgres;
 
+import java.util.Collection;
+import java.util.List;
+
 import org.apache.cayenne.dba.QuotingStrategy;
 import org.apache.cayenne.map.DbAttribute;
 import org.apache.cayenne.map.DbEntity;
 import org.apache.cayenne.merge.MergerFactory;
 import org.apache.cayenne.merge.MergerToken;
 import org.apache.cayenne.merge.SetColumnTypeToDb;
+import org.apache.cayenne.merge.SetPrimaryKeyToDb;
 
 public class PostgresMergerFactory extends MergerFactory {
 
@@ -47,4 +51,26 @@
         };
     }
 
+    @Override
+    public MergerToken createSetPrimaryKeyToDb(
+            DbEntity entity,
+            final Collection<DbAttribute> primaryKeyOriginal,
+            final Collection<DbAttribute> primaryKeyNew) {
+        return new SetPrimaryKeyToDb(entity, primaryKeyOriginal, primaryKeyNew) {
+
+            @Override
+            protected void appendDropOriginalPrimaryKeySQL(List<String> sqls) {
+                sqls.add("ALTER TABLE "
+                        + getEntity().getFullyQualifiedName()
+                        + " DROP CONSTRAINT "
+                        + getConstraintName());
+            }
+
+            @Override
+            protected String getConstraintName() {
+                return getEntity().getName().toLowerCase() + "_pkey";
+            }
+        };
+    }
+
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DbMerger.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DbMerger.java?rev=812516&r1=812515&r2=812516&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DbMerger.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DbMerger.java
Tue Sep  8 14:05:53 2009
@@ -27,8 +27,10 @@
 import java.util.Collections;
 import java.util.Comparator;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 import javax.sql.DataSource;
 
@@ -38,6 +40,7 @@
 import org.apache.cayenne.access.DbLoader;
 import org.apache.cayenne.access.DbLoaderDelegate;
 import org.apache.cayenne.dba.DbAdapter;
+import org.apache.cayenne.map.Attribute;
 import org.apache.cayenne.map.DataMap;
 import org.apache.cayenne.map.DbAttribute;
 import org.apache.cayenne.map.DbEntity;
@@ -138,6 +141,7 @@
 
                 checkRelationshipsToDrop(adapter, tokens, dbEntity, detectedEntity);
                 checkRows(tokens, dbEntity, detectedEntity);
+                checkPrimaryKeyChange(adapter, tokens, dbEntity, detectedEntity);
                 checkRelationshipsToAdd(adapter, tokens, dbEntity, detectedEntity);
             }
 
@@ -326,6 +330,28 @@
         }
     }
     
+    private void checkPrimaryKeyChange(DbAdapter adapter,
+            List<MergerToken> tokens,
+            DbEntity dbEntity,
+            DbEntity detectedEntity) {
+        Collection<DbAttribute> primaryKeyOriginal = detectedEntity.getPrimaryKeys();
+        Collection<DbAttribute> primaryKeyNew = dbEntity.getPrimaryKeys();
+
+        if (upperCaseEntityNames(primaryKeyOriginal).equals(upperCaseEntityNames(primaryKeyNew)))
{
+            return;
+        }
+        
+        tokens.add(factory.createSetPrimaryKeyToDb(dbEntity, primaryKeyOriginal, primaryKeyNew));
+    }
+    
+    private Set<String> upperCaseEntityNames(Collection<? extends Attribute>
attrs) {
+        Set<String> names = new HashSet<String>();
+        for (Attribute attr : attrs) {
+            names.add(attr.getName().toUpperCase());
+        }
+        return names;
+    }
+    
     /**
      * case insensitive search for a {@link DbEntity} in a {@link DataMap} by name
      */

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/MergerFactory.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/MergerFactory.java?rev=812516&r1=812515&r2=812516&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/MergerFactory.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/MergerFactory.java
Tue Sep  8 14:05:53 2009
@@ -18,6 +18,8 @@
  ****************************************************************/
 package org.apache.cayenne.merge;
 
+import java.util.Collection;
+
 import org.apache.cayenne.dba.DbAdapter;
 import org.apache.cayenne.map.DbAttribute;
 import org.apache.cayenne.map.DbEntity;
@@ -114,4 +116,17 @@
         return new DropRelationshipToModel(entity, rel);
     }
     
+    public MergerToken createSetPrimaryKeyToDb(
+            DbEntity entity,
+            Collection<DbAttribute> primaryKeyOriginal,
+            Collection<DbAttribute> primaryKeyNew) {
+        return new SetPrimaryKeyToDb(entity, primaryKeyOriginal, primaryKeyNew);
+    }
+
+    public MergerToken createSetPrimaryKeyToModel(
+            DbEntity entity,
+            Collection<DbAttribute> primaryKeyOriginal,
+            Collection<DbAttribute> primaryKeyNew) {
+        return new SetPrimaryKeyToModel(entity, primaryKeyOriginal, primaryKeyNew);
+    }
 }

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/SetPrimaryKeyToDb.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/SetPrimaryKeyToDb.java?rev=812516&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/SetPrimaryKeyToDb.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/SetPrimaryKeyToDb.java
Tue Sep  8 14:05:53 2009
@@ -0,0 +1,88 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+package org.apache.cayenne.merge;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.cayenne.dba.DbAdapter;
+import org.apache.cayenne.map.DbAttribute;
+import org.apache.cayenne.map.DbEntity;
+
+public class SetPrimaryKeyToDb extends AbstractToDbToken.Entity {
+
+    private Collection<DbAttribute> primaryKeyOriginal;
+    private Collection<DbAttribute> primaryKeyNew;
+
+    public SetPrimaryKeyToDb(DbEntity entity, Collection<DbAttribute> primaryKeyOriginal,
+            Collection<DbAttribute> primaryKeyNew) {
+        super(entity);
+        this.primaryKeyOriginal = primaryKeyOriginal;
+        this.primaryKeyNew = primaryKeyNew;
+    }
+
+    @Override
+    public List<String> createSql(DbAdapter adapter) {
+        List<String> sqls = new ArrayList<String>();
+        if (!primaryKeyOriginal.isEmpty()) {
+            appendDropOriginalPrimaryKeySQL(sqls);
+        }
+        appendAddNewPrimaryKeySQL(sqls);
+        return sqls;
+    }
+
+    protected void appendDropOriginalPrimaryKeySQL(List<String> sqls) {
+        sqls.add("ALTER TABLE "
+                + getEntity().getFullyQualifiedName()
+                + " DROP PRIMARY KEY");
+    }
+
+    protected void appendAddNewPrimaryKeySQL(List<String> sqls) {
+        StringBuilder sql = new StringBuilder();
+        sql.append("ALTER TABLE ");
+        sql.append(getEntity().getFullyQualifiedName());
+        sql.append(" ADD PRIMARY KEY (");
+        for (Iterator<DbAttribute> it = primaryKeyNew.iterator(); it.hasNext();) {
+            sql.append(it.next().getName());
+            if (it.hasNext()) {
+                sql.append(", ");
+            }
+        }
+        sql.append(")");
+        sqls.add(sql.toString());
+    }
+
+    public MergerToken createReverse(MergerFactory factory) {
+        return factory.createSetPrimaryKeyToModel(
+                getEntity(),
+                primaryKeyNew,
+                primaryKeyOriginal);
+    }
+
+    protected String getConstraintName() {
+        return getEntity().getName() + "_PK";
+    }
+
+    public String getTokenName() {
+        return "Set Primary Key";
+    }
+
+}

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/SetPrimaryKeyToModel.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/SetPrimaryKeyToModel.java?rev=812516&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/SetPrimaryKeyToModel.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/SetPrimaryKeyToModel.java
Tue Sep  8 14:05:53 2009
@@ -0,0 +1,77 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+package org.apache.cayenne.merge;
+
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.apache.cayenne.map.DbAttribute;
+import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.map.event.AttributeEvent;
+
+public class SetPrimaryKeyToModel extends AbstractToModelToken.Entity {
+
+    private Collection<DbAttribute> primaryKeyOriginal;
+    private Collection<DbAttribute> primaryKeyNew;
+    private Set<String> primaryKeyNewNames = new HashSet<String>();
+
+    public SetPrimaryKeyToModel(DbEntity entity,
+            Collection<DbAttribute> primaryKeyOriginal,
+            Collection<DbAttribute> primaryKeyNew) {
+        super(entity);
+        this.primaryKeyOriginal = primaryKeyOriginal;
+        this.primaryKeyNew = primaryKeyNew;
+        for (DbAttribute attr : primaryKeyNew) {
+            primaryKeyNewNames.add(attr.getName().toUpperCase());
+        }
+    }
+
+    public MergerToken createReverse(MergerFactory factory) {
+        return factory.createSetPrimaryKeyToDb(
+                getEntity(),
+                primaryKeyNew,
+                primaryKeyOriginal);
+    }
+
+    public void execute(MergerContext mergerContext) {
+        DbEntity e = getEntity();
+
+        for (DbAttribute attr : e.getAttributes()) {
+
+            boolean wasPrimaryKey = attr.isPrimaryKey();
+            boolean willBePrimaryKey = primaryKeyNewNames.contains(attr
+                    .getName()
+                    .toUpperCase());
+
+            if (wasPrimaryKey != willBePrimaryKey) {
+                attr.setPrimaryKey(willBePrimaryKey);
+                e.dbAttributeChanged(new AttributeEvent(this, attr, e));
+                mergerContext.getModelMergeDelegate().dbAttributeModified(attr);
+            }
+
+        }
+
+    }
+
+    public String getTokenName() {
+        return "Set Primary Key";
+    }
+
+}

Added: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/SetPrimaryKeyToDbTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/SetPrimaryKeyToDbTest.java?rev=812516&view=auto
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/SetPrimaryKeyToDbTest.java
(added)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/SetPrimaryKeyToDbTest.java
Tue Sep  8 14:05:53 2009
@@ -0,0 +1,57 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+package org.apache.cayenne.merge;
+
+import java.sql.Types;
+
+import org.apache.cayenne.map.DbAttribute;
+import org.apache.cayenne.map.DbEntity;
+
+
+public class SetPrimaryKeyToDbTest extends MergeCase {
+
+    public void test() throws Exception {
+        dropTableIfPresent(node, "NEW_TABLE");
+        assertTokensAndExecute(node, map, 0, 0);
+
+        DbEntity dbEntity1 = new DbEntity("NEW_TABLE");
+
+        DbAttribute e1col1 = new DbAttribute("ID1", Types.INTEGER, dbEntity1);
+        e1col1.setMandatory(true);
+        e1col1.setPrimaryKey(true);
+        dbEntity1.addAttribute(e1col1);
+        map.addDbEntity(dbEntity1);
+
+        assertTokensAndExecute(node, map, 1, 0);
+        assertTokensAndExecute(node, map, 0, 0);
+        
+        DbAttribute e1col2 = new DbAttribute("ID2", Types.INTEGER, dbEntity1);
+        e1col2.setMandatory(true);
+        dbEntity1.addAttribute(e1col2);
+        
+        assertTokensAndExecute(node, map, 2, 0);
+        assertTokensAndExecute(node, map, 0, 0);
+
+        e1col1.setPrimaryKey(false);
+        e1col2.setPrimaryKey(true);
+        
+        assertTokensAndExecute(node, map, 1, 0);
+        assertTokensAndExecute(node, map, 0, 0);
+    }
+}



Mime
View raw message