cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From torehal...@apache.org
Subject svn commit: r813306 - in /cayenne/main/trunk: docs/doc/src/main/resources/ framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/ framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/db/
Date Thu, 10 Sep 2009 08:36:58 GMT
Author: torehalset
Date: Thu Sep 10 08:36:57 2009
New Revision: 813306

URL: http://svn.apache.org/viewvc?rev=813306&view=rev
Log:
CAY-1274: CreateTableToDb token should not create existing AUTO_PK_SUPPORT

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/merge/AbstractToDbToken.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/ExecutingMergerContext.java
    cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/MergerContext.java
    cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/db/MergerOptions.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=813306&r1=813305&r2=813306&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 Thu Sep 10 08:36:57 2009
@@ -42,6 +42,7 @@
 CAY-1250 Prefetching doesn't work with prefetched subentity
 CAY-1252 Bad XML generated when saving DBEntity qualifiers
 CAY-1265 error while search inheritance relationship
+CAY-1274 CreateTableToDb should not create existing AUTO_PK_SUPPORT
 
 ----------------------------------
 Release: 3.0M6

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=813306&r1=813305&r2=813306&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
Thu Sep 10 08:36:57 2009
@@ -18,30 +18,66 @@
  ****************************************************************/
 package org.apache.cayenne.merge;
 
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Statement;
 import java.util.List;
 
+import org.apache.cayenne.access.QueryLogger;
 import org.apache.cayenne.dba.DbAdapter;
 import org.apache.cayenne.dba.QuotingStrategy;
 import org.apache.cayenne.map.DbAttribute;
 import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.validation.SimpleValidationFailure;
 
 /**
  * Common abstract superclass for all {@link MergerToken}s going from the model to the
  * database.
- * 
  */
 public abstract class AbstractToDbToken implements MergerToken, Comparable<MergerToken>
{
-    
+
     public final MergeDirection getDirection() {
         return MergeDirection.TO_DB;
     }
 
     public void execute(MergerContext mergerContext) {
         for (String sql : createSql(mergerContext.getAdapter())) {
-            mergerContext.executeSql(sql);
+            executeSql(mergerContext, sql);
+        }
+    }
+
+    protected void executeSql(MergerContext mergerContext, String sql) {
+        Connection conn = null;
+        Statement st = null;
+        try {
+            QueryLogger.log(sql);
+            conn = mergerContext.getDataNode().getDataSource().getConnection();
+            st = conn.createStatement();
+            st.execute(sql);
+        }
+        catch (SQLException e) {
+            mergerContext.getValidationResult().addFailure(
+                    new SimpleValidationFailure(sql, e.getMessage()));
+            QueryLogger.logQueryError(e);
+        }
+        finally {
+            if (st != null) {
+                try {
+                    st.close();
+                }
+                catch (SQLException e) {
+                }
+            }
+            if (conn != null) {
+                try {
+                    conn.close();
+                }
+                catch (SQLException e) {
+                }
+            }
         }
     }
-    
+
     @Override
     public String toString() {
         StringBuilder ts = new StringBuilder();
@@ -54,9 +90,9 @@
     }
 
     public abstract List<String> createSql(DbAdapter adapter);
-    
+
     abstract static class Entity extends AbstractToDbToken {
-        
+
         private DbEntity entity;
 
         public Entity(DbEntity entity) {
@@ -70,13 +106,13 @@
         public String getTokenValue() {
             return getEntity().getName();
         }
-        
+
         protected QuotingStrategy getQuotingStrategy(DbAdapter adapter) {
             return adapter.getQuotingStrategy(getEntity()
                     .getDataMap()
                     .isQuotingSQLIdentifiers());
         }
-        
+
         public int compareTo(MergerToken o) {
             // default order as tokens are created
             return 0;
@@ -85,9 +121,9 @@
     }
 
     abstract static class EntityAndColumn extends Entity {
-        
+
         private DbAttribute column;
-        
+
         public EntityAndColumn(DbEntity entity, DbAttribute column) {
             super(entity);
             this.column = column;
@@ -96,11 +132,11 @@
         public DbAttribute getColumn() {
             return column;
         }
-        
+
         @Override
         public String getTokenValue() {
             return getEntity().getName() + "." + getColumn().getName();
         }
-        
+
     }
 }

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=813306&r1=813305&r2=813306&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
Thu Sep 10 08:36:57 2009
@@ -22,8 +22,10 @@
 import java.util.Collections;
 import java.util.List;
 
+import org.apache.cayenne.access.DataNode;
 import org.apache.cayenne.dba.DbAdapter;
 import org.apache.cayenne.map.DbEntity;
+import org.apache.cayenne.validation.SimpleValidationFailure;
 
 public class CreateTableToDb extends AbstractToDbToken.Entity {
 
@@ -40,6 +42,22 @@
         return sqls;
     }
 
+    @Override
+    public void execute(MergerContext mergerContext) {
+        try {
+            DataNode node = mergerContext.getDataNode();
+            DbAdapter adapter = mergerContext.getAdapter();
+            adapter.getPkGenerator().createAutoPk(
+                    node,
+                    Collections.singletonList(getEntity()));
+            executeSql(mergerContext, adapter.createTable(getEntity()));
+        }
+        catch (Exception e) {
+            mergerContext.getValidationResult().addFailure(
+                    new SimpleValidationFailure(this, e.getMessage()));
+        }
+    }
+
     public String getTokenName() {
         return "Create Table";
     }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/ExecutingMergerContext.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/ExecutingMergerContext.java?rev=813306&r1=813305&r2=813306&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/ExecutingMergerContext.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/ExecutingMergerContext.java
Thu Sep 10 08:36:57 2009
@@ -34,31 +34,34 @@
 public class ExecutingMergerContext implements MergerContext {
 
     private DataMap map;
-    private DbAdapter adapter;
-    private DataSource dataSource;
+    private DataNode node;
     private ValidationResult result = new ValidationResult();
     private ModelMergeDelegate delegate;
 
     public ExecutingMergerContext(DataMap map, DataNode node) {
         this.map = map;
-        this.dataSource = node.getDataSource();
-        this.adapter = node.getAdapter();
+        this.node = node;
         this.delegate = new DefaultModelMergeDelegate();
     }
 
-    public ExecutingMergerContext(DataMap map, DataSource dataSource, DbAdapter adapter,
ModelMergeDelegate delegate) {
+    public ExecutingMergerContext(DataMap map, DataSource dataSource, DbAdapter adapter,
+            ModelMergeDelegate delegate) {
         this.map = map;
-        this.dataSource = dataSource;
-        this.adapter = adapter;
+        // create a fake DataNode as lots of DbAdapter/PkGenerator methods
+        // take a DataNode instead of just a DataSource
+        this.node = new DataNode();
+        this.node.setDataSource(dataSource);
+        this.node.setAdapter(adapter);
         this.delegate = delegate;
     }
 
+    @Deprecated
     public void executeSql(String sql) {
         Connection conn = null;
         Statement st = null;
         try {
             QueryLogger.log(sql);
-            conn = dataSource.getConnection();
+            conn = getDataNode().getDataSource().getConnection();
             st = conn.createStatement();
             st.execute(sql);
         }
@@ -85,13 +88,17 @@
     }
 
     public DbAdapter getAdapter() {
-        return adapter;
+        return getDataNode().getAdapter();
     }
 
     public DataMap getDataMap() {
         return map;
     }
 
+    public DataNode getDataNode() {
+        return node;
+    }
+
     public ValidationResult getValidationResult() {
         return result;
     }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/MergerContext.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/MergerContext.java?rev=813306&r1=813305&r2=813306&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/MergerContext.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.5-unpublished/src/main/java/org/apache/cayenne/merge/MergerContext.java
Thu Sep 10 08:36:57 2009
@@ -18,6 +18,7 @@
  ****************************************************************/
 package org.apache.cayenne.merge;
 
+import org.apache.cayenne.access.DataNode;
 import org.apache.cayenne.dba.DbAdapter;
 import org.apache.cayenne.map.DataMap;
 import org.apache.cayenne.validation.ValidationResult;
@@ -29,6 +30,7 @@
  */
 public interface MergerContext {
 
+    @Deprecated
     public void executeSql(String sql);
     
     public ModelMergeDelegate getModelMergeDelegate();
@@ -37,6 +39,8 @@
 
     public DataMap getDataMap();
     
+    public DataNode getDataNode();
+    
     public ValidationResult getValidationResult();
 
 }

Modified: cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/db/MergerOptions.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/db/MergerOptions.java?rev=813306&r1=813305&r2=813306&view=diff
==============================================================================
--- cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/db/MergerOptions.java
(original)
+++ cayenne/main/trunk/framework/cayenne-modeler/src/main/java/org/apache/cayenne/modeler/dialog/db/MergerOptions.java
Thu Sep 10 08:36:57 2009
@@ -46,6 +46,7 @@
 import org.apache.cayenne.map.ObjRelationship;
 import org.apache.cayenne.map.event.EntityEvent;
 import org.apache.cayenne.map.event.MapEvent;
+import org.apache.cayenne.merge.AbstractToDbToken;
 import org.apache.cayenne.merge.DbMerger;
 import org.apache.cayenne.merge.ExecutingMergerContext;
 import org.apache.cayenne.merge.MergeDirection;
@@ -187,38 +188,16 @@
                 + batchTerminator
                 + "\n\n" : "\n\n";
         
-        MergerContext context = new MergerContext() {
-
-            public void executeSql(String sql) {
-                buf.append(sql);
-                buf.append(lineEnd);
-            }
-
-            public DbAdapter getAdapter() {
-                return adapter;
-            }
-
-            public DataMap getDataMap() {
-                return dataMap;
-            }
-
-            public ValidationResult getValidationResult() {
-                return new ValidationResult();
-            }
-
-            public ModelMergeDelegate getModelMergeDelegate() {
-                return null;
-            }
-
-        };
-
         while (it.hasNext()) {
             MergerToken token = it.next();
-            
-            if (token.getDirection() == MergeDirection.TO_DB) {
-                token.execute(context);
+
+            if (token instanceof AbstractToDbToken) {
+                AbstractToDbToken tdb = (AbstractToDbToken) token;
+                for (String sql : tdb.createSql(adapter)) {
+                    buf.append(sql);
+                    buf.append(lineEnd);
+                }
             }
-            // buf.append(token.createSql(adapter)).append(lineEnd);
         }
 
         textForSQL = buf.toString();



Mime
View raw message