cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r531213 - in /cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src: main/java/org/apache/cayenne/access/ main/java/org/apache/cayenne/dba/ main/java/org/apache/cayenne/dba/mysql/ test/java/org/apache/cayenne/access/ test/java/org/apa...
Date Sun, 22 Apr 2007 15:57:19 GMT
Author: aadamchik
Date: Sun Apr 22 08:57:18 2007
New Revision: 531213

URL: http://svn.apache.org/viewvc?view=rev&rev=531213
Log:
CAY-717: Modeler doesn't manage FK constraints with MYSQL

Removed:
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DbGeneratorCrossDBTest.java
Modified:
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/DbGenerator.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/dba/AutoAdapter.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/dba/DbAdapter.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/dba/mysql/MySQLAdapter.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DbLoaderPartialTest.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DbLoaderTest.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/unit/AbstractAccessStack.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/unit/AccessStackAdapter.java
    cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/unit/MySQLStackAdapter.java

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/DbGenerator.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/DbGenerator.java?view=diff&rev=531213&r1=531212&r2=531213
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/DbGenerator.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/access/DbGenerator.java
Sun Apr 22 08:57:18 2007
@@ -35,7 +35,6 @@
 
 import javax.sql.DataSource;
 
-import org.apache.cayenne.CayenneRuntimeException;
 import org.apache.cayenne.conn.DataSourceInfo;
 import org.apache.cayenne.conn.DriverDataSource;
 import org.apache.cayenne.dba.DbAdapter;
@@ -71,7 +70,7 @@
     // stores generated SQL statements
     protected Map dropTables;
     protected Map createTables;
-    protected Map createFK;
+    protected Map createConstraints;
     protected List createPK;
     protected List dropPK;
 
@@ -153,11 +152,10 @@
     protected void buildStatements() {
         dropTables = new HashMap();
         createTables = new HashMap();
-        createFK = new HashMap();
+        createConstraints = new HashMap();
 
         DbAdapter adapter = getAdapter();
         Iterator it = dbEntitiesInInsertOrder.iterator();
-        boolean supportsFK = adapter.supportsFkConstraints();
         while (it.hasNext()) {
             DbEntity dbe = (DbEntity) it.next();
 
@@ -169,10 +167,8 @@
             // build "CREATE TABLE"
             createTables.put(name, adapter.createTable(dbe));
 
-            // build "FK"
-            if (supportsFK) {
-                createFK.put(name, createFkConstraintsQueries(dbe));
-            }
+            // build constraints
+            createConstraints.put(name, createConstraintsQueries(dbe));
         }
 
         PkGenerator pkGenerator = adapter.getPkGenerator();
@@ -230,11 +226,11 @@
             }
         }
 
-        if (shouldCreateFKConstraints && getAdapter().supportsFkConstraints()) {
+        if (shouldCreateFKConstraints) {
             Iterator it = dbEntitiesInInsertOrder.iterator();
             while (it.hasNext()) {
                 DbEntity ent = (DbEntity) it.next();
-                List fks = (List) createFK.get(ent.getName());
+                List fks = (List) createConstraints.get(ent.getName());
                 list.addAll(fks);
             }
         }
@@ -308,14 +304,13 @@
 
             // create FK
             if (shouldCreateTables
-                    && shouldCreateFKConstraints
-                    && getAdapter().supportsFkConstraints()) {
+                    && shouldCreateFKConstraints) {
                 Iterator it = dbEntitiesInInsertOrder.iterator();
                 while (it.hasNext()) {
                     DbEntity ent = (DbEntity) it.next();
 
                     if (createdTables.contains(ent.getName())) {
-                        List fks = (List) createFK.get(ent.getName());
+                        List fks = (List) createConstraints.get(ent.getName());
                         Iterator fkIt = fks.iterator();
                         while (fkIt.hasNext()) {
                             safeExecute(connection, (String) fkIt.next());
@@ -380,17 +375,23 @@
 
     /**
      * Returns an array of queries to create foreign key constraints for a particular
-     * DbEntity. Throws CayenneRuntimeException, if called for adapter that does not
-     * support FK constraints.
+     * DbEntity.
+     * 
+     * @deprecated since 3.0 as this method is used to generate both FK and UNIQUE
+     *             constraints, use 'createConstraintsQueries' instead.
      */
-    public List createFkConstraintsQueries(DbEntity dbEnt) {
-        if (!getAdapter().supportsFkConstraints()) {
-            throw new CayenneRuntimeException(
-                    "FK constraints are not supported by adapter.");
-        }
-
+    public List createFkConstraintsQueries(DbEntity table) {
+        return createConstraintsQueries(table);
+    }
+    
+    /**
+     * Creates FK and UNIQUE constraint statements for a given table.
+     * 
+     * @since 3.0
+     */
+    public List createConstraintsQueries(DbEntity table) {
         List list = new ArrayList();
-        Iterator it = dbEnt.getRelationships().iterator();
+        Iterator it = table.getRelationships().iterator();
         while (it.hasNext()) {
             DbRelationship rel = (DbRelationship) it.next();
 
@@ -427,7 +428,10 @@
                     }
                 }
 
-                list.add(getAdapter().createFkConstraint(rel));
+                String fk = getAdapter().createFkConstraint(rel);
+                if (fk != null) {
+                    list.add(fk);
+                }
             }
         }
         return list;

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/dba/AutoAdapter.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/dba/AutoAdapter.java?view=diff&rev=531213&r1=531212&r2=531213
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/dba/AutoAdapter.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/dba/AutoAdapter.java
Sun Apr 22 08:57:18 2007
@@ -182,6 +182,10 @@
         return getAdapter().getAction(query, node);
     }
 
+    /**
+     * @deprecated since 3.0 - almost all DB's support FK's now and also this flag is less
+     *             relevant for Cayenne now.
+     */
     public boolean supportsFkConstraints() {
         return getAdapter().supportsFkConstraints();
     }

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/dba/DbAdapter.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/dba/DbAdapter.java?view=diff&rev=531213&r1=531212&r2=531213
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/dba/DbAdapter.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/dba/DbAdapter.java
Sun Apr 22 08:57:18 2007
@@ -65,6 +65,9 @@
 
     /**
      * Returns true if a target database supports FK constraints.
+     * 
+     * @deprecated since 3.0 - almost all DB's support FK's now and also this flag is less
+     *             relevant for Cayenne now.
      */
     public boolean supportsFkConstraints();
 
@@ -109,7 +112,7 @@
 
     /**
      * Returns a SQL string that can be used to create a foreign key constraint for the
-     * relationship.
+     * relationship, or null if foreign keys are not supported.
      */
     public String createFkConstraint(DbRelationship rel);
 

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/dba/mysql/MySQLAdapter.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/dba/mysql/MySQLAdapter.java?view=diff&rev=531213&r1=531212&r2=531213
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/dba/mysql/MySQLAdapter.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/main/java/org/apache/cayenne/dba/mysql/MySQLAdapter.java
Sun Apr 22 08:57:18 2007
@@ -176,12 +176,8 @@
      */
     public String createTable(DbEntity entity) {
         String ddlSQL = super.createTable(entity);
-
-        // force InnoDB tables if constraints are enabled
-        if (supportsFkConstraints()) {
-            ddlSQL += " ENGINE=InnoDB";
-        }
-
+        // force InnoDB tables - by default constraints are enabled
+        ddlSQL += " ENGINE=InnoDB";
         return ddlSQL;
     }
 

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DbLoaderPartialTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DbLoaderPartialTest.java?view=diff&rev=531213&r1=531212&r2=531213
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DbLoaderPartialTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DbLoaderPartialTest.java
Sun Apr 22 08:57:18 2007
@@ -73,11 +73,6 @@
     public void testPartialLoad() throws Exception {
 
         try {
-            boolean supportsFK = getNode().getAdapter().supportsFkConstraints();
-
-            if (!supportsFK) {
-                return;
-            }
 
             DataMap map = new DataMap();
             String tableLabel = getNode().getAdapter().tableTypeForTable();

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DbLoaderTest.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DbLoaderTest.java?view=diff&rev=531213&r1=531212&r2=531213
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DbLoaderTest.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/access/DbLoaderTest.java
Sun Apr 22 08:57:18 2007
@@ -104,7 +104,6 @@
      */
     public void testLoad() throws Exception {
         try {
-            boolean supportsFK = getNode().getAdapter().supportsFkConstraints();
             boolean supportsUnique = getNode().getAdapter().supportsUniqueConstraints();
             boolean supportsLobs = getAccessStackAdapter().supportsLobs();
 
@@ -122,41 +121,39 @@
                 assertLobDbEntities(map);
             }
 
-            if (supportsFK) {
-                // *** TESTING THIS ***
-                loader.loadDbRelationships(map);
-
-                Collection rels = getDbEntity(map, "ARTIST").getRelationships();
-                assertNotNull(rels);
-                assertTrue(rels.size() > 0);
-
-                // test one-to-one
-                rels = getDbEntity(map, "PAINTING").getRelationships();
-                assertNotNull(rels);
-
-                // find relationship to PAINTING_INFO
-                DbRelationship oneToOne = null;
-                Iterator it = rels.iterator();
-                while (it.hasNext()) {
-                    DbRelationship rel = (DbRelationship) it.next();
-                    if ("PAINTING_INFO".equalsIgnoreCase(rel.getTargetEntityName())) {
-                        oneToOne = rel;
-                        break;
-                    }
-                }
+            // *** TESTING THIS ***
+            loader.loadDbRelationships(map);
 
-                assertNotNull("No relationship to PAINTING_INFO", oneToOne);
-                assertFalse("Relationship to PAINTING_INFO must be to-one", oneToOne
-                        .isToMany());
-                assertTrue("Relationship to PAINTING_INFO must be to-one", oneToOne
-                        .isToDependentPK());
-
-                // test UNIQUE only if FK is supported...
-                if (supportsUnique) {
-                    assertUniqueConstraintsInRelationships(map);
+            Collection rels = getDbEntity(map, "ARTIST").getRelationships();
+            assertNotNull(rels);
+            assertTrue(rels.size() > 0);
+
+            // test one-to-one
+            rels = getDbEntity(map, "PAINTING").getRelationships();
+            assertNotNull(rels);
+
+            // find relationship to PAINTING_INFO
+            DbRelationship oneToOne = null;
+            Iterator it = rels.iterator();
+            while (it.hasNext()) {
+                DbRelationship rel = (DbRelationship) it.next();
+                if ("PAINTING_INFO".equalsIgnoreCase(rel.getTargetEntityName())) {
+                    oneToOne = rel;
+                    break;
                 }
             }
 
+            assertNotNull("No relationship to PAINTING_INFO", oneToOne);
+            assertFalse("Relationship to PAINTING_INFO must be to-one", oneToOne
+                    .isToMany());
+            assertTrue("Relationship to PAINTING_INFO must be to-one", oneToOne
+                    .isToDependentPK());
+
+            // test UNIQUE only if FK is supported...
+            if (supportsUnique) {
+                assertUniqueConstraintsInRelationships(map);
+            }
+
             // *** TESTING THIS ***
             loader.loadObjEntities(map);
             ObjEntity ae = map.getObjEntity("Artist");
@@ -168,11 +165,10 @@
                 assertLobObjEntities(map);
             }
 
-            if (supportsFK) {
-                Collection rels = ae.getRelationships();
-                assertNotNull(rels);
-                assertTrue(rels.size() > 0);
-            } // now when the map is loaded, test
+            Collection rels1 = ae.getRelationships();
+            assertNotNull(rels1);
+            assertTrue(rels1.size() > 0);
+            // now when the map is loaded, test
             // various things
             // selectively check how different types were processed
             checkTypes(map);

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/unit/AbstractAccessStack.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/unit/AbstractAccessStack.java?view=diff&rev=531213&r1=531212&r2=531213
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/unit/AbstractAccessStack.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/unit/AbstractAccessStack.java
Sun Apr 22 08:57:18 2007
@@ -295,18 +295,17 @@
         }
 
         // FK constraints
-        if (adapter.supportsFkConstraints()) {
-            it = orderedEnts.iterator();
-            while (it.hasNext()) {
-                DbEntity ent = (DbEntity) it.next();
-                if (ent instanceof DerivedDbEntity
-                        || !getAdapter(node).supportsFKConstraints(ent)) {
-                    continue;
-                }
 
-                List qs = gen.createFkConstraintsQueries(ent);
-                queries.addAll(qs);
+        it = orderedEnts.iterator();
+        while (it.hasNext()) {
+            DbEntity ent = (DbEntity) it.next();
+            if (ent instanceof DerivedDbEntity
+                    || !getAdapter(node).supportsFKConstraints(ent)) {
+                continue;
             }
+
+            List qs = gen.createConstraintsQueries(ent);
+            queries.addAll(qs);
         }
 
         return queries.iterator();

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/unit/AccessStackAdapter.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/unit/AccessStackAdapter.java?view=diff&rev=531213&r1=531212&r2=531213
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/unit/AccessStackAdapter.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/unit/AccessStackAdapter.java
Sun Apr 22 08:57:18 2007
@@ -17,7 +17,6 @@
  *  under the License.
  ****************************************************************/
 
-
 package org.apache.cayenne.unit;
 
 import java.io.BufferedReader;
@@ -78,27 +77,28 @@
     public void willDropTables(Connection conn, DataMap map, Collection tablesToDrop)
             throws Exception {
 
-        if (adapter.supportsFkConstraints()) {
-            Map constraintsMap = getConstraints(conn, map, tablesToDrop);
+        Map constraintsMap = getConstraints(conn, map, tablesToDrop);
+
+        Iterator it = constraintsMap.entrySet().iterator();
+        while (it.hasNext()) {
+            Map.Entry entry = (Map.Entry) it.next();
+
+            Collection constraints = (Collection) entry.getValue();
+            if (constraints == null || constraints.isEmpty()) {
+                continue;
+            }
 
-            Iterator it = constraintsMap.entrySet().iterator();
-            while (it.hasNext()) {
-                Map.Entry entry = (Map.Entry) it.next();
-
-                Collection constraints = (Collection) entry.getValue();
-                if (constraints == null || constraints.isEmpty()) {
-                    continue;
-                }
-
-                Object tableName = entry.getKey();
-                Iterator cit = constraints.iterator();
-                while (cit.hasNext()) {
-                    Object constraint = cit.next();
-                    StringBuffer drop = new StringBuffer();
-                    drop.append("ALTER TABLE ").append(tableName).append(
-                            " DROP CONSTRAINT ").append(constraint);
-                    executeDDL(conn, drop.toString());
-                }
+            Object tableName = entry.getKey();
+            Iterator cit = constraints.iterator();
+            while (cit.hasNext()) {
+                Object constraint = cit.next();
+                StringBuffer drop = new StringBuffer();
+                drop
+                        .append("ALTER TABLE ")
+                        .append(tableName)
+                        .append(" DROP CONSTRAINT ")
+                        .append(constraint);
+                executeDDL(conn, drop.toString());
             }
         }
     }
@@ -138,13 +138,13 @@
     public boolean supportsLobInsertsAsStrings() {
         return supportsLobs();
     }
-    
+
     public boolean supportsFKConstraints(DbEntity entity) {
-        if("FK_OF_DIFFERENT_TYPE".equals(entity.getName())) {
+        if ("FK_OF_DIFFERENT_TYPE".equals(entity.getName())) {
             return false;
         }
-        
-        return adapter.supportsFkConstraints();
+
+        return true;
     }
 
     /**

Modified: cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/unit/MySQLStackAdapter.java
URL: http://svn.apache.org/viewvc/cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/unit/MySQLStackAdapter.java?view=diff&rev=531213&r1=531212&r2=531213
==============================================================================
--- cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/unit/MySQLStackAdapter.java
(original)
+++ cayenne/main/trunk/framework/cayenne-jdk1.4-unpublished/src/test/java/org/apache/cayenne/unit/MySQLStackAdapter.java
Sun Apr 22 08:57:18 2007
@@ -36,7 +36,7 @@
 public class MySQLStackAdapter extends AccessStackAdapter {
 
     static final Collection NO_CONSTRAINTS_TABLES = Arrays.asList(new Object[] {
-            "REFLEXIVE_AND_TO_ONE", "ARTGROUP"
+            "REFLEXIVE_AND_TO_ONE", "ARTGROUP", "FK_OF_DIFFERENT_TYPE"
     });
 
     public MySQLStackAdapter(DbAdapter adapter) {
@@ -67,27 +67,29 @@
     public void willDropTables(Connection conn, DataMap map, Collection tablesToDrop)
             throws Exception {
         // special DROP CONSTRAINT syntax for MySQL
-        if (adapter.supportsFkConstraints()) {
-            Map constraintsMap = getConstraints(conn, map, tablesToDrop);
 
-            Iterator it = constraintsMap.entrySet().iterator();
-            while (it.hasNext()) {
-                Map.Entry entry = (Map.Entry) it.next();
-
-                Collection constraints = (Collection) entry.getValue();
-                if (constraints == null || constraints.isEmpty()) {
-                    continue;
-                }
-
-                Object tableName = entry.getKey();
-                Iterator cit = constraints.iterator();
-                while (cit.hasNext()) {
-                    Object constraint = cit.next();
-                    StringBuffer drop = new StringBuffer();
-                    drop.append("ALTER TABLE ").append(tableName).append(
-                            " DROP FOREIGN KEY ").append(constraint);
-                    executeDDL(conn, drop.toString());
-                }
+        Map constraintsMap = getConstraints(conn, map, tablesToDrop);
+
+        Iterator it = constraintsMap.entrySet().iterator();
+        while (it.hasNext()) {
+            Map.Entry entry = (Map.Entry) it.next();
+
+            Collection constraints = (Collection) entry.getValue();
+            if (constraints == null || constraints.isEmpty()) {
+                continue;
+            }
+
+            Object tableName = entry.getKey();
+            Iterator cit = constraints.iterator();
+            while (cit.hasNext()) {
+                Object constraint = cit.next();
+                StringBuffer drop = new StringBuffer();
+                drop
+                        .append("ALTER TABLE ")
+                        .append(tableName)
+                        .append(" DROP FOREIGN KEY ")
+                        .append(constraint);
+                executeDDL(conn, drop.toString());
             }
         }
 
@@ -103,8 +105,6 @@
     public boolean supportsFKConstraints(DbEntity entity) {
         // MySQL supports that, but there are problems deleting objects from such
         // tables...
-        return adapter.supportsFkConstraints()
-                && !NO_CONSTRAINTS_TABLES.contains(entity.getName());
+        return !NO_CONSTRAINTS_TABLES.contains(entity.getName());
     }
-
 }



Mime
View raw message