Issue 24682: Simplifiy implemenation.
authorStefan Hühner <stefan.huehner@openbravo.com>
Fri, 06 Sep 2013 11:30:25 +0200
changeset 463 64ee18562b3a
parent 462 253902e2aa5a
child 464 621081475315
Issue 24682: Simplifiy implemenation.
The code for disabling/enabling the datasetFK's was nearly identical
on postgres & oracle. Make it completely identical and then pull up
the implementation into the PlatformImplBase superclass removing the
duplicate code.
As a nice side-effect the changes done earlier to make some functions from
SqlBuilder (and all its subclasses) are not longer needed and have been
reverted.
src/org/apache/ddlutils/platform/PlatformImplBase.java
src/org/apache/ddlutils/platform/SqlBuilder.java
src/org/apache/ddlutils/platform/maxdb/MaxDbBuilder.java
src/org/apache/ddlutils/platform/mssql/MSSqlBuilder.java
src/org/apache/ddlutils/platform/mysql/MySqlBuilder.java
src/org/apache/ddlutils/platform/oracle/Oracle8Platform.java
src/org/apache/ddlutils/platform/postgresql/PostgreSqlPlatform.java
src/org/apache/ddlutils/platform/sapdb/SapDbBuilder.java
src/org/apache/ddlutils/platform/sybase/SybaseBuilder.java
--- a/src/org/apache/ddlutils/platform/PlatformImplBase.java	Thu Sep 05 16:03:48 2013 +0200
+++ b/src/org/apache/ddlutils/platform/PlatformImplBase.java	Fri Sep 06 11:30:25 2013 +0200
@@ -2561,7 +2561,29 @@
   @Override
   public void disableDatasetFK(Connection connection, Database model, OBDataset dataset,
       boolean continueOnError) throws DatabaseOperationException {
-    throw new DatabaseOperationException("Error: Operation not supported");
+    try {
+      StringWriter buffer = new StringWriter();
+      getSqlBuilder().setWriter(buffer);
+      for (int i = 0; i < model.getTableCount(); i++) {
+        Table table = model.getTable(i);
+        if (dataset.getTable(table.getName()) != null) {
+          for (int idx = 0; idx < table.getForeignKeyCount(); idx++) {
+            getSqlBuilder().writeExternalForeignKeyDropStmt(table, table.getForeignKey(idx));
+          }
+        } else {
+          for (int j = 0; j < table.getForeignKeyCount(); j++) {
+            ForeignKey fk = table.getForeignKey(j);
+            if (dataset.getTable(fk.getForeignTableName()) != null) {
+              getSqlBuilder().writeExternalForeignKeyDropStmt(table, fk);
+            }
+          }
+        }
+      }
+      evaluateBatchRealBatch(connection, buffer.toString(), continueOnError);
+    } catch (Exception e) {
+      e.printStackTrace();
+      throw new DatabaseOperationException("Error while disabling foreign key ", e);
+    }
   }
 
   /**
@@ -2583,7 +2605,31 @@
   @Override
   public boolean enableDatasetFK(Connection connection, Database model, OBDataset dataset,
       boolean continueOnError) throws DatabaseOperationException {
-    throw new DatabaseOperationException("Error: Operation not supported");
+    try {
+      StringWriter buffer = new StringWriter();
+      getSqlBuilder().setWriter(buffer);
+      for (int i = 0; i < model.getTableCount(); i++) {
+        Table table = model.getTable(i);
+        if (dataset.getTable(table.getName()) != null) {
+          getSqlBuilder().createExternalForeignKeys(model, table);
+        } else {
+          for (int j = 0; j < table.getForeignKeyCount(); j++) {
+            ForeignKey fk = table.getForeignKey(j);
+            if (dataset.getTable(fk.getForeignTableName()) != null) {
+              getSqlBuilder().writeExternalForeignKeyCreateStmt(model, table, fk);
+            }
+          }
+        }
+      }
+      int numErrors = evaluateBatchRealBatch(connection, buffer.toString(), continueOnError);
+      if (numErrors > 0) {
+        return false;
+      }
+      return true;
+    } catch (Exception e) {
+      e.printStackTrace();
+      throw new DatabaseOperationException("Error while enabling foreign key ", e);
+    }
   }
 
   /**
--- a/src/org/apache/ddlutils/platform/SqlBuilder.java	Thu Sep 05 16:03:48 2013 +0200
+++ b/src/org/apache/ddlutils/platform/SqlBuilder.java	Fri Sep 06 11:30:25 2013 +0200
@@ -3518,7 +3518,7 @@
    * @param key
    *          The foreign key
    */
-  public void writeExternalForeignKeyCreateStmt(Database database, Table table, ForeignKey key)
+  protected void writeExternalForeignKeyCreateStmt(Database database, Table table, ForeignKey key)
       throws IOException {
     if (key.getForeignTableName() == null) {
       _log.warn("Foreign key table is null for key " + key);
@@ -3611,7 +3611,7 @@
    * @param foreignKey
    *          The foreign key
    */
-  public void writeExternalForeignKeyDropStmt(Table table, ForeignKey foreignKey)
+  protected void writeExternalForeignKeyDropStmt(Table table, ForeignKey foreignKey)
       throws IOException {
     writeTableAlterStmt(table);
     print("DROP CONSTRAINT ");
--- a/src/org/apache/ddlutils/platform/maxdb/MaxDbBuilder.java	Thu Sep 05 16:03:48 2013 +0200
+++ b/src/org/apache/ddlutils/platform/maxdb/MaxDbBuilder.java	Fri Sep 06 11:30:25 2013 +0200
@@ -64,7 +64,7 @@
   /**
    * {@inheritDoc}
    */
-  public void writeExternalForeignKeyCreateStmt(Database database, Table table, ForeignKey key)
+  protected void writeExternalForeignKeyCreateStmt(Database database, Table table, ForeignKey key)
       throws IOException {
     if (key.getForeignTableName() == null) {
       _log.warn("Foreign key table is null for key " + key);
@@ -87,7 +87,7 @@
   /**
    * {@inheritDoc}
    */
-  public void writeExternalForeignKeyDropStmt(Table table, ForeignKey foreignKey)
+  protected void writeExternalForeignKeyDropStmt(Table table, ForeignKey foreignKey)
       throws IOException {
     writeTableAlterStmt(table);
     print("DROP CONSTRAINT ");
--- a/src/org/apache/ddlutils/platform/mssql/MSSqlBuilder.java	Thu Sep 05 16:03:48 2013 +0200
+++ b/src/org/apache/ddlutils/platform/mssql/MSSqlBuilder.java	Fri Sep 06 11:30:25 2013 +0200
@@ -226,7 +226,7 @@
   /**
    * {@inheritDoc}
    */
-  public void writeExternalForeignKeyDropStmt(Table table, ForeignKey foreignKey)
+  protected void writeExternalForeignKeyDropStmt(Table table, ForeignKey foreignKey)
       throws IOException {
     String constraintName = getForeignKeyName(table, foreignKey);
 
--- a/src/org/apache/ddlutils/platform/mysql/MySqlBuilder.java	Thu Sep 05 16:03:48 2013 +0200
+++ b/src/org/apache/ddlutils/platform/mysql/MySqlBuilder.java	Fri Sep 06 11:30:25 2013 +0200
@@ -136,7 +136,7 @@
   /**
    * {@inheritDoc}
    */
-  public void writeExternalForeignKeyDropStmt(Table table, ForeignKey foreignKey)
+  protected void writeExternalForeignKeyDropStmt(Table table, ForeignKey foreignKey)
       throws IOException {
     writeTableAlterStmt(table);
     print("DROP FOREIGN KEY ");
--- a/src/org/apache/ddlutils/platform/oracle/Oracle8Platform.java	Thu Sep 05 16:03:48 2013 +0200
+++ b/src/org/apache/ddlutils/platform/oracle/Oracle8Platform.java	Fri Sep 06 11:30:25 2013 +0200
@@ -32,9 +32,7 @@
 import org.apache.ddlutils.DatabaseOperationException;
 import org.apache.ddlutils.PlatformInfo;
 import org.apache.ddlutils.model.Database;
-import org.apache.ddlutils.model.ForeignKey;
 import org.apache.ddlutils.model.Function;
-import org.apache.ddlutils.model.Table;
 import org.apache.ddlutils.model.Trigger;
 import org.apache.ddlutils.platform.PlatformImplBase;
 import org.apache.ddlutils.platform.postgresql.PostgrePLSQLFunctionStandarization;
@@ -45,7 +43,6 @@
 import org.apache.ddlutils.translation.CommentFilter;
 import org.apache.ddlutils.translation.LiteralFilter;
 import org.apache.ddlutils.util.ExtTypes;
-import org.openbravo.ddlutils.util.OBDataset;
 
 /**
  * The platform for Oracle 8.
@@ -164,36 +161,6 @@
     }
   }
 
-  @Override
-  public void disableDatasetFK(Connection connection, Database model, OBDataset dataset,
-      boolean continueOnError) throws DatabaseOperationException {
-
-    try {
-      StringWriter buffer = new StringWriter();
-      getSqlBuilder().setWriter(buffer);
-      for (int i = 0; i < model.getTableCount(); i++) {
-        Table table = model.getTable(i);
-        if (dataset.getTable(table.getName()) != null) {
-          for (int j = 0; j < table.getForeignKeyCount(); j++) {
-            ForeignKey fk = table.getForeignKey(j);
-            getSqlBuilder().writeExternalForeignKeyDropStmt(table, fk);
-          }
-        } else {
-          for (int j = 0; j < table.getForeignKeyCount(); j++) {
-            ForeignKey fk = table.getForeignKey(j);
-            if (dataset.getTable(fk.getForeignTableName()) != null) {
-              getSqlBuilder().writeExternalForeignKeyDropStmt(table, fk);
-            }
-          }
-        }
-      }
-      evaluateBatchRealBatch(connection, buffer.toString(), continueOnError);
-    } catch (Exception e) {
-      e.printStackTrace();
-      throw new DatabaseOperationException("Error while disabling foreign key ", e);
-    }
-  }
-
   /**
    * {@inheritDoc}
    */
@@ -225,40 +192,6 @@
   /**
    * {@inheritDoc}
    */
-  @Override
-  public boolean enableDatasetFK(Connection connection, Database model, OBDataset dataset,
-      boolean continueOnError) throws DatabaseOperationException {
-
-    try {
-      StringWriter buffer = new StringWriter();
-      getSqlBuilder().setWriter(buffer);
-      for (int i = 0; i < model.getTableCount(); i++) {
-        Table table = model.getTable(i);
-        if (dataset.getTable(table.getName()) != null) {
-          getSqlBuilder().createExternalForeignKeys(model, table);
-        } else {
-          for (int j = 0; j < table.getForeignKeyCount(); j++) {
-            ForeignKey fk = table.getForeignKey(j);
-            if (dataset.getTable(fk.getForeignTableName()) != null) {
-              getSqlBuilder().writeExternalForeignKeyCreateStmt(model, table, fk);
-            }
-          }
-        }
-      }
-      int numErrors = evaluateBatchRealBatch(connection, buffer.toString(), continueOnError);
-      if (numErrors > 0) {
-        return false;
-      }
-      return true;
-    } catch (Exception e) {
-      e.printStackTrace();
-      throw new DatabaseOperationException("Error while enabling foreign key ", e);
-    }
-  }
-
-  /**
-   * {@inheritDoc}
-   */
   public void disableAllTriggers(Connection connection, Database model, boolean continueOnError)
       throws DatabaseOperationException {
 
--- a/src/org/apache/ddlutils/platform/postgresql/PostgreSqlPlatform.java	Thu Sep 05 16:03:48 2013 +0200
+++ b/src/org/apache/ddlutils/platform/postgresql/PostgreSqlPlatform.java	Fri Sep 06 11:30:25 2013 +0200
@@ -37,9 +37,7 @@
 import org.apache.ddlutils.PlatformInfo;
 import org.apache.ddlutils.dynabean.SqlDynaProperty;
 import org.apache.ddlutils.model.Database;
-import org.apache.ddlutils.model.ForeignKey;
 import org.apache.ddlutils.model.Function;
-import org.apache.ddlutils.model.Table;
 import org.apache.ddlutils.model.Trigger;
 import org.apache.ddlutils.platform.PlatformImplBase;
 import org.apache.ddlutils.translation.CommentFilter;
@@ -294,38 +292,6 @@
     }
   }
 
-  @Override
-  public void disableDatasetFK(Connection connection, Database model, OBDataset dataset,
-      boolean continueOnError) throws DatabaseOperationException {
-
-    try {
-      StringWriter buffer = new StringWriter();
-      getSqlBuilder().setWriter(buffer);
-      for (int i = 0; i < model.getTableCount(); i++) {
-        Table table = model.getTable(i);
-        if (dataset.getTable(table.getName()) != null) {
-          getSqlBuilder().dropExternalForeignKeys(table);
-        } else {
-          for (int j = 0; j < table.getForeignKeyCount(); j++) {
-            ForeignKey fk = table.getForeignKey(j);
-            if (dataset.getTable(fk.getForeignTableName()) != null) {
-              getSqlBuilder().writeExternalForeignKeyDropStmt(table, fk);
-            }
-          }
-        }
-      }
-      evaluateBatchRealBatch(connection, buffer.toString(), continueOnError);
-      /*
-       * PreparedStatementpstmt=connection.prepareStatement(
-       * "update pg_class set reltriggers=0 WHERE PG_CLASS.RELNAMESPACE IN (SELECT PG_NAMESPACE.OID FROM PG_NAMESPACE WHERE PG_NAMESPACE.NSPNAME = CURRENT_SCHEMA())"
-       * ); pstmt.executeUpdate(); pstmt.close();
-       */
-    } catch (Exception e) {
-      e.printStackTrace();
-      throw new DatabaseOperationException("Error while disabling foreign key ", e);
-    }
-  }
-
   /**
    * {@inheritDoc}
    */
@@ -355,42 +321,6 @@
     }
   }
 
-  @Override
-  public boolean enableDatasetFK(Connection connection, Database model, OBDataset dataset,
-      boolean continueOnError) throws DatabaseOperationException {
-
-    try {
-      StringWriter buffer = new StringWriter();
-      getSqlBuilder().setWriter(buffer);
-      for (int i = 0; i < model.getTableCount(); i++) {
-        Table table = model.getTable(i);
-        if (dataset.getTable(table.getName()) != null) {
-          getSqlBuilder().createExternalForeignKeys(model, table);
-        } else {
-          for (int j = 0; j < table.getForeignKeyCount(); j++) {
-            ForeignKey fk = table.getForeignKey(j);
-            if (dataset.getTable(fk.getForeignTableName()) != null) {
-              getSqlBuilder().writeExternalForeignKeyCreateStmt(model, table, fk);
-            }
-          }
-        }
-      }
-      int numErrors = evaluateBatchRealBatch(connection, buffer.toString(), continueOnError);
-      if (numErrors > 0) {
-        return false;
-      }
-      return true;
-      /*
-       * PreparedStatementpstmt=connection.prepareStatement(
-       * "update pg_class set reltriggers = (SELECT count(*) from pg_trigger where pg_class.oid=tgrelid) WHERE PG_CLASS.RELNAMESPACE IN (SELECT PG_NAMESPACE.OID FROM PG_NAMESPACE WHERE PG_NAMESPACE.NSPNAME = CURRENT_SCHEMA())"
-       * ); pstmt.executeUpdate(); pstmt.close();
-       */
-    } catch (Exception e) {
-      e.printStackTrace();
-      throw new DatabaseOperationException("Error while enabling foreign key ", e);
-    }
-  }
-
   /**
    * {@inheritDoc}
    */
--- a/src/org/apache/ddlutils/platform/sapdb/SapDbBuilder.java	Thu Sep 05 16:03:48 2013 +0200
+++ b/src/org/apache/ddlutils/platform/sapdb/SapDbBuilder.java	Fri Sep 06 11:30:25 2013 +0200
@@ -92,7 +92,7 @@
   /**
    * {@inheritDoc}
    */
-  public void writeExternalForeignKeyCreateStmt(Database database, Table table, ForeignKey key)
+  protected void writeExternalForeignKeyCreateStmt(Database database, Table table, ForeignKey key)
       throws IOException {
     if (key.getForeignTableName() == null) {
       _log.warn("Foreign key table is null for key " + key);
@@ -115,7 +115,7 @@
   /**
    * {@inheritDoc}
    */
-  public void writeExternalForeignKeyDropStmt(Table table, ForeignKey foreignKey)
+  protected void writeExternalForeignKeyDropStmt(Table table, ForeignKey foreignKey)
       throws IOException {
     writeTableAlterStmt(table);
     print("DROP FOREIGN KEY ");
--- a/src/org/apache/ddlutils/platform/sybase/SybaseBuilder.java	Thu Sep 05 16:03:48 2013 +0200
+++ b/src/org/apache/ddlutils/platform/sybase/SybaseBuilder.java	Fri Sep 06 11:30:25 2013 +0200
@@ -192,7 +192,7 @@
   /**
    * {@inheritDoc}
    */
-  public void writeExternalForeignKeyDropStmt(Table table, ForeignKey foreignKey)
+  protected void writeExternalForeignKeyDropStmt(Table table, ForeignKey foreignKey)
       throws IOException {
     String constraintName = getForeignKeyName(table, foreignKey);