cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From torehal...@apache.org
Subject svn commit: r602840 - in /cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src: main/java/org/apache/cayenne/merge/ test/java/org/apache/cayenne/merge/
Date Mon, 10 Dec 2007 10:12:26 GMT
Author: torehalset
Date: Mon Dec 10 02:12:21 2007
New Revision: 602840

URL: http://svn.apache.org/viewvc?rev=602840&view=rev
Log:
CAY-934: merge create table to db will also create pk support

Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/AbstractToDbToken.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/AddColumnToDb.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/CreateTableToDb.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DropColumnToDb.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DropTableToDb.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/SetAllowNullToDb.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/SetColumnTypeToDb.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/SetNotNullToDb.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/MergerFactoryTest.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/AbstractToDbToken.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/AbstractToDbToken.java?rev=602840&r1=602839&r2=602840&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/AbstractToDbToken.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/AbstractToDbToken.java
Mon Dec 10 02:12:21 2007
@@ -18,6 +18,8 @@
  ****************************************************************/
 package org.apache.cayenne.merge;
 
+import java.util.List;
+
 import org.apache.cayenne.dba.DbAdapter;
 
 /**
@@ -33,9 +35,11 @@
     }
 
     public void execute(MergerContext mergerContext) {
-        mergerContext.executeSql(createSql(mergerContext.getAdapter()));
+        for (String sql : createSql(mergerContext.getAdapter())) {
+            mergerContext.executeSql(sql);
+        }
     }
 
-    public abstract String createSql(DbAdapter adapter);
+    public abstract List<String> createSql(DbAdapter adapter);
 
 }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/AddColumnToDb.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/AddColumnToDb.java?rev=602840&r1=602839&r2=602840&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/AddColumnToDb.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/AddColumnToDb.java
Mon Dec 10 02:12:21 2007
@@ -18,6 +18,9 @@
  ****************************************************************/
 package org.apache.cayenne.merge;
 
+import java.util.Collections;
+import java.util.List;
+
 import org.apache.cayenne.CayenneRuntimeException;
 import org.apache.cayenne.dba.DbAdapter;
 import org.apache.cayenne.dba.TypesMapping;
@@ -45,7 +48,7 @@
         sqlBuffer.append(" ");
     }
 
-    public String createSql(DbAdapter adapter) {
+    public List<String> createSql(DbAdapter adapter) {
         StringBuffer sqlBuffer = new StringBuffer();
 
         appendPrefix(sqlBuffer);
@@ -90,7 +93,7 @@
         // use separate token to set value and not null if needed
         // sqlBuffer.append(" NULL");
 
-        return sqlBuffer.toString();
+        return Collections.singletonList(sqlBuffer.toString());
     }
 
     public String getTokenName() {

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/CreateTableToDb.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/CreateTableToDb.java?rev=602840&r1=602839&r2=602840&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/CreateTableToDb.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/CreateTableToDb.java
Mon Dec 10 02:12:21 2007
@@ -18,6 +18,10 @@
  ****************************************************************/
 package org.apache.cayenne.merge;
 
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
 import org.apache.cayenne.dba.DbAdapter;
 import org.apache.cayenne.map.DbEntity;
 
@@ -29,8 +33,13 @@
         this.entity = entity;
     }
 
-    public String createSql(DbAdapter adapter) {
-        return adapter.createTable(entity);
+    @Override
+    public List<String> createSql(DbAdapter adapter) {
+        List<String> sqls = new ArrayList<String>();
+        sqls.addAll(adapter.getPkGenerator().createAutoPkStatements(
+                Collections.singletonList(entity)));
+        sqls.add(adapter.createTable(entity));
+        return sqls;
     }
 
     public String getTokenName() {

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DropColumnToDb.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DropColumnToDb.java?rev=602840&r1=602839&r2=602840&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DropColumnToDb.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DropColumnToDb.java
Mon Dec 10 02:12:21 2007
@@ -18,6 +18,9 @@
  ****************************************************************/
 package org.apache.cayenne.merge;
 
+import java.util.Collections;
+import java.util.List;
+
 import org.apache.cayenne.dba.DbAdapter;
 import org.apache.cayenne.map.DbAttribute;
 import org.apache.cayenne.map.DbEntity;
@@ -32,7 +35,8 @@
         this.column = column;
     }
 
-    public String createSql(DbAdapter adapter) {
+    @Override
+    public List<String> createSql(DbAdapter adapter) {
         StringBuffer sqlBuffer = new StringBuffer();
 
         sqlBuffer.append("ALTER TABLE ");
@@ -40,7 +44,7 @@
         sqlBuffer.append(" DROP COLUMN ");
         sqlBuffer.append(column.getName());
 
-        return sqlBuffer.toString();
+        return Collections.singletonList(sqlBuffer.toString());
     }
 
     public String getTokenName() {

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DropTableToDb.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DropTableToDb.java?rev=602840&r1=602839&r2=602840&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DropTableToDb.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/DropTableToDb.java
Mon Dec 10 02:12:21 2007
@@ -18,6 +18,9 @@
  ****************************************************************/
 package org.apache.cayenne.merge;
 
+import java.util.Collections;
+import java.util.List;
+
 import org.apache.cayenne.dba.DbAdapter;
 import org.apache.cayenne.map.DbEntity;
 
@@ -29,8 +32,9 @@
         this.entity = entity;
     }
 
-    public String createSql(DbAdapter adapter) {
-        return adapter.dropTable(entity);
+    @Override
+    public List<String> createSql(DbAdapter adapter) {
+        return Collections.singletonList(adapter.dropTable(entity));
     }
 
     public String getTokenName() {

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/SetAllowNullToDb.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/SetAllowNullToDb.java?rev=602840&r1=602839&r2=602840&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/SetAllowNullToDb.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/SetAllowNullToDb.java
Mon Dec 10 02:12:21 2007
@@ -18,6 +18,9 @@
  ****************************************************************/
 package org.apache.cayenne.merge;
 
+import java.util.Collections;
+import java.util.List;
+
 import org.apache.cayenne.dba.DbAdapter;
 import org.apache.cayenne.map.DbAttribute;
 import org.apache.cayenne.map.DbEntity;
@@ -37,7 +40,8 @@
         this.column = column;
     }
 
-    public String createSql(DbAdapter adapter) {
+    @Override
+    public List<String> createSql(DbAdapter adapter) {
         StringBuffer sqlBuffer = new StringBuffer();
 
         sqlBuffer.append("ALTER TABLE ");
@@ -46,7 +50,7 @@
         sqlBuffer.append(column.getName());
         sqlBuffer.append(" DROP NOT NULL");
 
-        return sqlBuffer.toString();
+        return Collections.singletonList(sqlBuffer.toString());
     }
 
     public String getTokenName() {

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/SetColumnTypeToDb.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/SetColumnTypeToDb.java?rev=602840&r1=602839&r2=602840&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/SetColumnTypeToDb.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/SetColumnTypeToDb.java
Mon Dec 10 02:12:21 2007
@@ -18,6 +18,9 @@
  ****************************************************************/
 package org.apache.cayenne.merge;
 
+import java.util.Collections;
+import java.util.List;
+
 import org.apache.cayenne.CayenneRuntimeException;
 import org.apache.cayenne.dba.DbAdapter;
 import org.apache.cayenne.dba.TypesMapping;
@@ -50,7 +53,8 @@
         sqlBuffer.append(" TYPE ");
     }
 
-    public String createSql(DbAdapter adapter) {
+    @Override
+    public List<String> createSql(DbAdapter adapter) {
         StringBuffer sqlBuffer = new StringBuffer();
 
         appendPrefix(sqlBuffer);
@@ -92,7 +96,7 @@
             }
         }
 
-        return sqlBuffer.toString();
+        return Collections.singletonList(sqlBuffer.toString());
     }
 
     public String getTokenName() {

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/SetNotNullToDb.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/SetNotNullToDb.java?rev=602840&r1=602839&r2=602840&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/SetNotNullToDb.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/SetNotNullToDb.java
Mon Dec 10 02:12:21 2007
@@ -18,6 +18,9 @@
  ****************************************************************/
 package org.apache.cayenne.merge;
 
+import java.util.Collections;
+import java.util.List;
+
 import org.apache.cayenne.dba.DbAdapter;
 import org.apache.cayenne.map.DbAttribute;
 import org.apache.cayenne.map.DbEntity;
@@ -37,7 +40,8 @@
         this.column = column;
     }
 
-    public String createSql(DbAdapter adapter) {
+    @Override
+    public List<String> createSql(DbAdapter adapter) {
         StringBuffer sqlBuffer = new StringBuffer();
 
         sqlBuffer.append("ALTER TABLE ");
@@ -46,7 +50,7 @@
         sqlBuffer.append(column.getName());
         sqlBuffer.append(" SET NOT NULL");
 
-        return sqlBuffer.toString();
+        return Collections.singletonList(sqlBuffer.toString());
     }
 
     public String getTokenName() {

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/MergerFactoryTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/MergerFactoryTest.java?rev=602840&r1=602839&r2=602840&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/MergerFactoryTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/test/java/org/apache/cayenne/merge/MergerFactoryTest.java
Mon Dec 10 02:12:21 2007
@@ -26,12 +26,17 @@
 import java.util.Iterator;
 import java.util.List;
 
+import org.apache.cayenne.CayenneDataObject;
+import org.apache.cayenne.Persistent;
+import org.apache.cayenne.access.DataContext;
 import org.apache.cayenne.access.DataNode;
 import org.apache.cayenne.access.QueryLogger;
 import org.apache.cayenne.dba.DbAdapter;
 import org.apache.cayenne.map.DataMap;
 import org.apache.cayenne.map.DbAttribute;
 import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.map.ObjAttribute;
+import org.apache.cayenne.map.ObjEntity;
 import org.apache.cayenne.unit.CayenneCase;
 
 public class MergerFactoryTest extends CayenneCase {
@@ -168,6 +173,7 @@
 
         DbAttribute column1 = new DbAttribute("ID", Types.INTEGER, dbEntity);
         column1.setMandatory(true);
+        column1.setPrimaryKey(true);
         dbEntity.addAttribute(column1);
 
         DbAttribute column2 = new DbAttribute("NAME", Types.VARCHAR, dbEntity);
@@ -179,9 +185,29 @@
 
         mergeToDb(node, map, 1);
         assertInSync(node, map);
+        
+        ObjEntity objEntity = new ObjEntity("NewTable");
+        objEntity.setDbEntity(dbEntity);
+        ObjAttribute oatr1 = new ObjAttribute("name");
+        oatr1.setDbAttribute(column2);
+        oatr1.setType("java.lang.String");
+        objEntity.addAttribute(oatr1);
+        map.addObjEntity(objEntity);
+        
+        // try to insert some rows to check that pk stuff is working
+        DataContext ctxt = createDataContext();
+        for (int i = 0; i < 5; i++) {
+            CayenneDataObject dao = (CayenneDataObject) ctxt.newObject(objEntity
+                    .getName());
+            dao.writeProperty(oatr1.getName(), "test " + i);
+        }
+        ctxt.commitChanges();
 
         // clear up
+        map.removeObjEntity(objEntity.getName(), true);
         map.removeDbEntity(dbEntity.getName(), true);
+        ctxt.getEntityResolver().clearCache();
+        assertNull(map.getObjEntity(objEntity.getName()));
         assertNull(map.getDbEntity(dbEntity.getName()));
         assertFalse(map.getDbEntities().contains(dbEntity));
         // TODO: mergeToDb(node, map, 1);
@@ -219,7 +245,9 @@
                 .mergerFactory()
                 .createDropTableToDb(entity);
         try {
-            executeSql(t.createSql(node.getAdapter()));
+            for (String sql : t.createSql(node.getAdapter())) {
+                executeSql(sql);
+            }
         }
         catch (Exception e) {
         }



Mime
View raw message